🐛 Fix inflate error (output ended prematurely)

This commit is contained in:
C-3PO 2018-07-31 13:38:21 +02:00
parent 614aa91c94
commit 3923daf551
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
3 changed files with 11 additions and 2 deletions

View file

@ -48,6 +48,7 @@ struct InflateOutput inflateInflate(unsigned long numInputBytes, bool hasMoreByt
uncomprBufferNext = uncomprBuffer;
struct InflateOutput out;
out.hasReachedEnd = false;
while (remainingInput > 0 && spaceInOutput > 0) {
in_bytes = remainingInput;
@ -74,6 +75,7 @@ struct InflateOutput inflateInflate(unsigned long numInputBytes, bool hasMoreByt
if (status <= TINFL_STATUS_DONE) {
if (status == TINFL_STATUS_DONE) {
// Decompression completed successfully.
out.hasReachedEnd = true;
break;
} else {
// Decompression failed.
@ -82,7 +84,7 @@ struct InflateOutput inflateInflate(unsigned long numInputBytes, bool hasMoreByt
}
}
//If output buffer is filled
//If output buffer is filled, we need to clear it before decompressing more data
if (spaceInOutput == 0) {
break;
}

View file

@ -4,6 +4,7 @@ void inflateInit(char* comprBuffer, char* uncomprBuffer, unsigned long uncomprBu
struct InflateOutput {
bool needMoreInput;
bool hasReachedEnd;
unsigned long numBytesWrittenToOutput;
};

View file

@ -58,6 +58,8 @@ int main(int argc, unsigned char *argv[]) {
fprintf(stderr, "Could not allocate %lu bytes for uncompressed buffer.\n", BUFFER_SIZE);
exit(1);
}
memset(compressedChunk, 0, BUFFER_SIZE);
memset(uncompressedChunk, 0, BUFFER_SIZE);
//-------------------------------------------------
@ -90,10 +92,12 @@ int main(int argc, unsigned char *argv[]) {
//Read actual file
unsigned long remainingBytes = fileLength;
bool needToRead = true;
bool hasReachedEnd = false;
unsigned long chunkSize;
while (remainingBytes > 0) {
while (remainingBytes > 0 || !hasReachedEnd) {
if (needToRead) {
chunkSize = min(BUFFER_SIZE, remainingBytes);
memset(compressedChunk, 0, chunkSize);
getBytes(compressedChunk, chunkSize);
remainingBytes -= chunkSize;
@ -106,8 +110,10 @@ int main(int argc, unsigned char *argv[]) {
//Decompress file
//bytes are contained in uncompressedChunk from [0, inflateResult.numBytesWrittenToOutput - 1]
memset(uncompressedChunk, 0, BUFFER_SIZE);
inflateResult = inflateInflate(needToRead ? chunkSize : 0, remainingBytes > 0);
needToRead = inflateResult.needMoreInput;
hasReachedEnd = inflateResult.hasReachedEnd;
write(1, uncompressedChunk, inflateResult.numBytesWrittenToOutput);