This commit is contained in:
dborth 2008-10-14 23:35:19 +00:00
parent f5d6042073
commit d9f13b8203
2 changed files with 114 additions and 59 deletions

View File

@ -445,10 +445,13 @@ int FileSelector (int method)
WaitPrompt((char*) "Maximum filepath length reached!"); WaitPrompt((char*) "Maximum filepath length reached!");
return -1; return -1;
} }
maxfiles = SzParse(szpath, method); int szfiles = SzParse(szpath, method);
if(maxfiles) if(szfiles)
{
maxfiles = szfiles;
inSz = true; inSz = true;
} }
}
else else
{ {
// check that this is a valid ROM // check that this is a valid ROM

View File

@ -244,19 +244,25 @@ SZ_RESULT SzDecode2(const CFileSize *packSizes, const CFolder *folder,
// allocate memory for the temporary buffer // allocate memory for the temporary buffer
Byte *tmpBuffer = (Byte *)allocMain->Alloc(_LZMA_TEMP_BUFFER_SIZE); Byte *tmpBuffer = (Byte *)allocMain->Alloc(_LZMA_TEMP_BUFFER_SIZE);
// variables containing the number of the first and the last bytes of the buffer
size_t bufferStart, bufferEnd;
bufferStart = bufferEnd = 0;
// integers contains the offset, the size and the already copied data which will be
// copied from the tmpBuffer to outBuffer
size_t copyOffset, copySize, copyDone;
copyOffset = copySize = copyDone = 0;
UInt32 i = 0;
int bytesToCopy = 0;
// decompress data in _LZMA_TEMP_BUFFER_SIZE byte steps and copy the wanted file to outBuffer // decompress data in _LZMA_TEMP_BUFFER_SIZE byte steps and copy the wanted file to outBuffer
size_t bytesLeft = *fileSize; // total bytes remaining to be read
size_t bytesToRead = 0; // bytes to read on this pass
size_t bytesRead = 0; // total bytes read
size_t offset = 0; // buffer offset
do do
{ {
if(bytesLeft > _LZMA_TEMP_BUFFER_SIZE) if((*fileSize - copyDone) >= _LZMA_TEMP_BUFFER_SIZE)
bytesToRead = _LZMA_TEMP_BUFFER_SIZE; bytesToCopy = _LZMA_TEMP_BUFFER_SIZE;
else else
bytesToRead = bytesLeft; bytesToCopy = (*fileSize - copyDone);
bytesLeft -= bytesToRead;
// decompress next bytes // decompress next bytes
result = LzmaDecode(&state, result = LzmaDecode(&state,
@ -265,14 +271,18 @@ SZ_RESULT SzDecode2(const CFileSize *packSizes, const CFolder *folder,
#else #else
//inBuffer, (SizeT)inSize, &inProcessed, //TODO! //inBuffer, (SizeT)inSize, &inProcessed, //TODO!
#endif #endif
tmpBuffer, bytesToRead, &outSizeProcessedLoc tmpBuffer, bytesToCopy, &outSizeProcessedLoc
); );
// check result // check result
if(result == LZMA_RESULT_DATA_ERROR) if(result == LZMA_RESULT_DATA_ERROR)
{
return SZE_DATA_ERROR; return SZE_DATA_ERROR;
else if(result != LZMA_RESULT_OK) }
if(result != LZMA_RESULT_OK)
{
return SZE_FAIL; return SZE_FAIL;
}
// normally this should never happen // normally this should never happen
if(outSizeProcessedLoc > _LZMA_TEMP_BUFFER_SIZE) if(outSizeProcessedLoc > _LZMA_TEMP_BUFFER_SIZE)
@ -280,11 +290,53 @@ SZ_RESULT SzDecode2(const CFileSize *packSizes, const CFolder *folder,
return SZE_FAIL; return SZE_FAIL;
} }
memcpy(outBuffer + offset, tmpBuffer, outSizeProcessedLoc); // update bufferStart and bufferEnd
bytesRead += bytesToRead; bufferStart = _LZMA_TEMP_BUFFER_SIZE * i;
offset += outSizeProcessedLoc; bufferEnd = bufferStart + outSizeProcessedLoc;
i++;
// calculate copy offset and size
if(*fileOffset > bufferEnd)
{
// we haven't reached the start of the file yet
continue;
} }
while(bytesLeft > 0);
// calculate offset
if(*fileOffset < bufferStart)
{
// the file has already started before this decompression step
copyOffset = 0;
}
else
{
// the file starts somewhere inside this buffer
copyDone = 0;
copyOffset = _LZMA_TEMP_BUFFER_SIZE - (bufferEnd - *fileOffset);
}
// calculate size
if((*fileOffset + *fileSize) > bufferEnd)
{
// we'll need the whole buffer after copyOffset
copySize = _LZMA_TEMP_BUFFER_SIZE - copyOffset;
}
else
{
// we'll stop somewhere inside the buffer
copySize = (*fileOffset + *fileSize) - (bufferStart + copyOffset);
}
// copy bytes to the real output buffer
if(copySize == 0)
{
continue;
}
// printf("memcpy(outBuffer + %d, tmpBuffer + %d, %d)\n", copyDone, copyOffset, copySize);
memcpy(outBuffer + copyDone, tmpBuffer + copyOffset, copySize);
copyDone += copySize;
}
while((*fileOffset + *fileSize) > bufferEnd);
/* result = LzmaDecode(&state, /* result = LzmaDecode(&state,
#ifdef _LZMA_IN_CB #ifdef _LZMA_IN_CB
@ -294,7 +346,7 @@ SZ_RESULT SzDecode2(const CFileSize *packSizes, const CFolder *folder,
#endif #endif
outBuffer, (SizeT)outSize, &outSizeProcessedLoc);*/ outBuffer, (SizeT)outSize, &outSizeProcessedLoc);*/
//*outSizeProcessed = (size_t)outSizeProcessedLoc; //*outSizeProcessed = (size_t)outSizeProcessedLoc;
*outSizeProcessed = offset; *outSizeProcessed = copyDone;
allocMain->Free(tmpBuffer); // free the temporary buffer again allocMain->Free(tmpBuffer); // free the temporary buffer again
allocMain->Free(state.Probs); allocMain->Free(state.Probs);
allocMain->Free(state.Dictionary); allocMain->Free(state.Dictionary);