2000-05-23 01:55:31 +00:00
|
|
|
/*
|
|
|
|
* NuFX archive manipulation library
|
2007-02-19 23:12:22 +00:00
|
|
|
* Copyright (C) 2000-2007 by Andy McFadden, All Rights Reserved.
|
2000-05-23 01:55:31 +00:00
|
|
|
* This is free software; you can redistribute it and/or modify it under the
|
2007-02-19 23:12:22 +00:00
|
|
|
* terms of the BSD License, see the file COPYING-LIB.
|
2000-05-23 01:55:31 +00:00
|
|
|
*
|
|
|
|
* Expand a thread from an archive.
|
|
|
|
*/
|
|
|
|
#include "NufxLibPriv.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "Expand" an uncompressed thread.
|
|
|
|
*/
|
2014-12-23 00:00:33 +00:00
|
|
|
static NuError Nu_ExpandUncompressed(NuArchive* pArchive,
|
|
|
|
const NuRecord* pRecord, const NuThread* pThread, FILE* infp,
|
|
|
|
NuFunnel* pFunnel, uint16_t* pCrc)
|
2000-05-23 01:55:31 +00:00
|
|
|
{
|
2002-09-21 00:49:42 +00:00
|
|
|
NuError err;
|
2014-12-22 02:35:09 +00:00
|
|
|
/*uint8_t* buffer = NULL;*/
|
|
|
|
uint32_t count, getsize;
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2014-12-22 02:17:23 +00:00
|
|
|
Assert(pArchive != NULL);
|
|
|
|
Assert(pThread != NULL);
|
|
|
|
Assert(infp != NULL);
|
|
|
|
Assert(pFunnel != NULL);
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2002-09-21 00:49:42 +00:00
|
|
|
/* doesn't have to be same size as funnel, but it's not a bad idea */
|
|
|
|
/*buffer = Nu_Malloc(pArchive, kNuFunnelBufSize);*/
|
|
|
|
/*BailAlloc(buffer);*/
|
|
|
|
err = Nu_AllocCompressionBufferIFN(pArchive);
|
|
|
|
BailError(err);
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2002-09-21 00:49:42 +00:00
|
|
|
/* quick assert for bad archive that should have been caught earlier */
|
|
|
|
/* (filename threads are uncompressed, but compThreadEOF is buf len) */
|
|
|
|
if (pThread->thThreadClass == kNuThreadClassData)
|
|
|
|
Assert(pThread->actualThreadEOF == pThread->thCompThreadEOF);
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2002-09-21 00:49:42 +00:00
|
|
|
count = pThread->actualThreadEOF;
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2002-09-21 00:49:42 +00:00
|
|
|
while (count) {
|
|
|
|
getsize = (count > kNuGenCompBufSize) ? kNuGenCompBufSize : count;
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2002-09-21 00:49:42 +00:00
|
|
|
err = Nu_FRead(infp, pArchive->compBuf, getsize);
|
|
|
|
BailError(err);
|
2014-12-22 02:17:23 +00:00
|
|
|
if (pCrc != NULL)
|
2002-09-21 00:49:42 +00:00
|
|
|
*pCrc = Nu_CalcCRC16(*pCrc, pArchive->compBuf, getsize);
|
|
|
|
err = Nu_FunnelWrite(pArchive, pFunnel, pArchive->compBuf, getsize);
|
|
|
|
BailError(err);
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2002-09-21 00:49:42 +00:00
|
|
|
count -= getsize;
|
|
|
|
}
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2002-09-21 00:49:42 +00:00
|
|
|
err = Nu_FunnelFlush(pArchive, pFunnel);
|
|
|
|
BailError(err);
|
2000-05-23 01:55:31 +00:00
|
|
|
|
|
|
|
bail:
|
2002-09-21 00:49:42 +00:00
|
|
|
/*Nu_Free(pArchive, buffer);*/
|
|
|
|
return err;
|
2000-05-23 01:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy the "raw" data out of the thread. Unlike the preceeding function,
|
|
|
|
* this reads up to "thCompThreadEOF", and doesn't even try to compute a CRC.
|
|
|
|
*/
|
2014-12-23 00:00:33 +00:00
|
|
|
static NuError Nu_ExpandRaw(NuArchive* pArchive, const NuThread* pThread,
|
|
|
|
FILE* infp, NuFunnel* pFunnel)
|
2000-05-23 01:55:31 +00:00
|
|
|
{
|
2002-09-21 00:49:42 +00:00
|
|
|
NuError err;
|
2014-12-22 02:35:09 +00:00
|
|
|
/*uint8_t* buffer = NULL;*/
|
|
|
|
uint32_t count, getsize;
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2014-12-22 02:17:23 +00:00
|
|
|
Assert(pArchive != NULL);
|
|
|
|
Assert(pThread != NULL);
|
|
|
|
Assert(infp != NULL);
|
|
|
|
Assert(pFunnel != NULL);
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2002-09-21 00:49:42 +00:00
|
|
|
/* doesn't have to be same size as funnel, but it's not a bad idea */
|
|
|
|
/*buffer = Nu_Malloc(pArchive, kNuFunnelBufSize);*/
|
|
|
|
/*BailAlloc(buffer);*/
|
|
|
|
err = Nu_AllocCompressionBufferIFN(pArchive);
|
|
|
|
BailError(err);
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2002-09-21 00:49:42 +00:00
|
|
|
count = pThread->thCompThreadEOF;
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2002-09-21 00:49:42 +00:00
|
|
|
while (count) {
|
|
|
|
getsize = (count > kNuGenCompBufSize) ? kNuGenCompBufSize : count;
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2002-09-21 00:49:42 +00:00
|
|
|
err = Nu_FRead(infp, pArchive->compBuf, getsize);
|
|
|
|
BailError(err);
|
|
|
|
err = Nu_FunnelWrite(pArchive, pFunnel, pArchive->compBuf, getsize);
|
|
|
|
BailError(err);
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2002-09-21 00:49:42 +00:00
|
|
|
count -= getsize;
|
|
|
|
}
|
2000-05-23 01:55:31 +00:00
|
|
|
|
2002-09-21 00:49:42 +00:00
|
|
|
err = Nu_FunnelFlush(pArchive, pFunnel);
|
|
|
|
BailError(err);
|
2000-05-23 01:55:31 +00:00
|
|
|
|
|
|
|
bail:
|
2002-09-21 00:49:42 +00:00
|
|
|
/*Nu_Free(pArchive, buffer);*/
|
|
|
|
return err;
|
2000-05-23 01:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Expand a thread from "infp" to "pFunnel", using the compression
|
|
|
|
* and stream length specified by "pThread".
|
|
|
|
*/
|
2014-12-23 00:00:33 +00:00
|
|
|
NuError Nu_ExpandStream(NuArchive* pArchive, const NuRecord* pRecord,
|
2002-09-21 00:49:42 +00:00
|
|
|
const NuThread* pThread, FILE* infp, NuFunnel* pFunnel)
|
2000-05-23 01:55:31 +00:00
|
|
|
{
|
2002-09-21 00:49:42 +00:00
|
|
|
NuError err = kNuErrNone;
|
2014-12-22 02:35:09 +00:00
|
|
|
uint16_t calcCrc;
|
|
|
|
uint16_t* pCalcCrc;
|
2002-09-21 00:49:42 +00:00
|
|
|
|
|
|
|
if (!pThread->thThreadEOF && !pThread->thCompThreadEOF) {
|
|
|
|
/* somebody stored an empty file! */
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A brief history of the "threadCRC" field in the thread header:
|
|
|
|
* record versions 0 and 1 didn't use the threadCRC field
|
|
|
|
* record version 2 put the CRC of the compressed data in threadCRC
|
|
|
|
* record version 3 put the CRC of the uncompressed data in threadCRC
|
|
|
|
*
|
|
|
|
* P8 ShrinkIt uses v1, GSHK uses v3. If something ever shipped with
|
|
|
|
* v2, it didn't last long enough to leave an impression, so I'm not
|
|
|
|
* going to support it. BTW, P8 ShrinkIt always uses LZW/1, which
|
|
|
|
* puts a CRC in the compressed stream. Your uncompressed data is,
|
|
|
|
* unfortunately, unprotected before v3.
|
|
|
|
*/
|
|
|
|
calcCrc = kNuInitialThreadCRC;
|
2014-12-22 02:17:23 +00:00
|
|
|
pCalcCrc = NULL;
|
2002-09-21 00:49:42 +00:00
|
|
|
if (Nu_ThreadHasCRC(pRecord->recVersionNumber, NuGetThreadID(pThread)) &&
|
|
|
|
!pArchive->valIgnoreCRC)
|
|
|
|
{
|
|
|
|
pCalcCrc = &calcCrc;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = Nu_ProgressDataExpandPrep(pArchive, pFunnel, pThread);
|
|
|
|
BailError(err);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're not expanding the data, use a simple copier.
|
|
|
|
*/
|
|
|
|
if (!Nu_FunnelGetDoExpand(pFunnel)) {
|
|
|
|
Nu_FunnelSetProgressState(pFunnel, kNuProgressCopying);
|
|
|
|
err = Nu_ExpandRaw(pArchive, pThread, infp, pFunnel);
|
|
|
|
BailError(err);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
Nu_FunnelSetProgressState(pFunnel, kNuProgressExpanding);
|
|
|
|
switch (pThread->thThreadFormat) {
|
|
|
|
case kNuThreadFormatUncompressed:
|
|
|
|
Nu_FunnelSetProgressState(pFunnel, kNuProgressCopying);
|
|
|
|
err = Nu_ExpandUncompressed(pArchive, pRecord, pThread, infp, pFunnel,
|
|
|
|
pCalcCrc);
|
|
|
|
break;
|
2002-10-09 23:12:06 +00:00
|
|
|
#ifdef ENABLE_SQ
|
2002-09-21 00:49:42 +00:00
|
|
|
case kNuThreadFormatHuffmanSQ:
|
2002-09-26 22:11:12 +00:00
|
|
|
err = Nu_ExpandHuffmanSQ(pArchive, pRecord, pThread, infp, pFunnel,
|
|
|
|
pCalcCrc);
|
2002-09-21 00:49:42 +00:00
|
|
|
break;
|
2002-10-09 23:12:06 +00:00
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_LZW
|
2002-09-21 00:49:42 +00:00
|
|
|
case kNuThreadFormatLZW1:
|
|
|
|
case kNuThreadFormatLZW2:
|
|
|
|
err = Nu_ExpandLZW(pArchive, pRecord, pThread, infp, pFunnel, pCalcCrc);
|
|
|
|
break;
|
2002-10-09 23:12:06 +00:00
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_LZC
|
2002-09-21 00:49:42 +00:00
|
|
|
case kNuThreadFormatLZC12:
|
|
|
|
case kNuThreadFormatLZC16:
|
2002-09-28 00:56:08 +00:00
|
|
|
err = Nu_ExpandLZC(pArchive, pRecord, pThread, infp, pFunnel, pCalcCrc);
|
2002-09-21 00:49:42 +00:00
|
|
|
break;
|
2002-10-09 23:12:06 +00:00
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_DEFLATE
|
2002-10-01 01:05:42 +00:00
|
|
|
case kNuThreadFormatDeflate:
|
|
|
|
err = Nu_ExpandDeflate(pArchive, pRecord, pThread, infp, pFunnel,
|
|
|
|
pCalcCrc);
|
|
|
|
break;
|
2002-10-09 23:12:06 +00:00
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_BZIP2
|
|
|
|
case kNuThreadFormatBzip2:
|
|
|
|
err = Nu_ExpandBzip2(pArchive, pRecord, pThread, infp, pFunnel,
|
|
|
|
pCalcCrc);
|
|
|
|
break;
|
|
|
|
#endif
|
2002-09-21 00:49:42 +00:00
|
|
|
default:
|
|
|
|
err = kNuErrBadFormat;
|
|
|
|
Nu_ReportError(NU_BLOB, err,
|
2002-10-09 23:12:06 +00:00
|
|
|
"compression format %u not supported", pThread->thThreadFormat);
|
2002-09-21 00:49:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-10-01 01:05:42 +00:00
|
|
|
BailError(err);
|
2002-09-21 00:49:42 +00:00
|
|
|
|
2002-10-01 01:05:42 +00:00
|
|
|
err = Nu_FunnelFlush(pArchive, pFunnel);
|
2002-09-21 00:49:42 +00:00
|
|
|
BailError(err);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we have a CRC to check, check it.
|
|
|
|
*/
|
2014-12-22 02:17:23 +00:00
|
|
|
if (pCalcCrc != NULL) {
|
2002-09-21 00:49:42 +00:00
|
|
|
if (calcCrc != pThread->thThreadCRC) {
|
|
|
|
if (!Nu_ShouldIgnoreBadCRC(pArchive, pRecord, kNuErrBadThreadCRC)) {
|
2002-09-28 00:56:08 +00:00
|
|
|
err = kNuErrBadDataCRC;
|
2002-09-21 00:49:42 +00:00
|
|
|
Nu_ReportError(NU_BLOB, err, "expected 0x%04x, got 0x%04x",
|
|
|
|
pThread->thThreadCRC, calcCrc);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DBUG(("--- thread CRCs match\n"));
|
|
|
|
}
|
|
|
|
}
|
2000-05-23 01:55:31 +00:00
|
|
|
|
|
|
|
done:
|
2002-09-21 00:49:42 +00:00
|
|
|
/* make sure we send a final "success" progress message at 100% */
|
|
|
|
(void) Nu_FunnelSetProgressState(pFunnel, kNuProgressDone);
|
|
|
|
err = Nu_FunnelSendProgressUpdate(pArchive, pFunnel);
|
|
|
|
BailError(err);
|
2000-05-23 01:55:31 +00:00
|
|
|
|
|
|
|
bail:
|
2002-09-21 00:49:42 +00:00
|
|
|
return err;
|
2000-05-23 01:55:31 +00:00
|
|
|
}
|
|
|
|
|