🐛 Fix inflate error (output ended prematurely)
This commit is contained in:
parent
614aa91c94
commit
3923daf551
3 changed files with 11 additions and 2 deletions
|
@ -48,6 +48,7 @@ struct InflateOutput inflateInflate(unsigned long numInputBytes, bool hasMoreByt
|
||||||
uncomprBufferNext = uncomprBuffer;
|
uncomprBufferNext = uncomprBuffer;
|
||||||
|
|
||||||
struct InflateOutput out;
|
struct InflateOutput out;
|
||||||
|
out.hasReachedEnd = false;
|
||||||
|
|
||||||
while (remainingInput > 0 && spaceInOutput > 0) {
|
while (remainingInput > 0 && spaceInOutput > 0) {
|
||||||
in_bytes = remainingInput;
|
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) {
|
||||||
if (status == TINFL_STATUS_DONE) {
|
if (status == TINFL_STATUS_DONE) {
|
||||||
// Decompression completed successfully.
|
// Decompression completed successfully.
|
||||||
|
out.hasReachedEnd = true;
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
// Decompression failed.
|
// 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) {
|
if (spaceInOutput == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ void inflateInit(char* comprBuffer, char* uncomprBuffer, unsigned long uncomprBu
|
||||||
|
|
||||||
struct InflateOutput {
|
struct InflateOutput {
|
||||||
bool needMoreInput;
|
bool needMoreInput;
|
||||||
|
bool hasReachedEnd;
|
||||||
unsigned long numBytesWrittenToOutput;
|
unsigned long numBytesWrittenToOutput;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -58,6 +58,8 @@ int main(int argc, unsigned char *argv[]) {
|
||||||
fprintf(stderr, "Could not allocate %lu bytes for uncompressed buffer.\n", BUFFER_SIZE);
|
fprintf(stderr, "Could not allocate %lu bytes for uncompressed buffer.\n", BUFFER_SIZE);
|
||||||
exit(1);
|
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
|
//Read actual file
|
||||||
unsigned long remainingBytes = fileLength;
|
unsigned long remainingBytes = fileLength;
|
||||||
bool needToRead = true;
|
bool needToRead = true;
|
||||||
|
bool hasReachedEnd = false;
|
||||||
unsigned long chunkSize;
|
unsigned long chunkSize;
|
||||||
while (remainingBytes > 0) {
|
while (remainingBytes > 0 || !hasReachedEnd) {
|
||||||
if (needToRead) {
|
if (needToRead) {
|
||||||
chunkSize = min(BUFFER_SIZE, remainingBytes);
|
chunkSize = min(BUFFER_SIZE, remainingBytes);
|
||||||
|
memset(compressedChunk, 0, chunkSize);
|
||||||
getBytes(compressedChunk, chunkSize);
|
getBytes(compressedChunk, chunkSize);
|
||||||
remainingBytes -= chunkSize;
|
remainingBytes -= chunkSize;
|
||||||
|
|
||||||
|
@ -106,8 +110,10 @@ int main(int argc, unsigned char *argv[]) {
|
||||||
|
|
||||||
//Decompress file
|
//Decompress file
|
||||||
//bytes are contained in uncompressedChunk from [0, inflateResult.numBytesWrittenToOutput - 1]
|
//bytes are contained in uncompressedChunk from [0, inflateResult.numBytesWrittenToOutput - 1]
|
||||||
|
memset(uncompressedChunk, 0, BUFFER_SIZE);
|
||||||
inflateResult = inflateInflate(needToRead ? chunkSize : 0, remainingBytes > 0);
|
inflateResult = inflateInflate(needToRead ? chunkSize : 0, remainingBytes > 0);
|
||||||
needToRead = inflateResult.needMoreInput;
|
needToRead = inflateResult.needMoreInput;
|
||||||
|
hasReachedEnd = inflateResult.hasReachedEnd;
|
||||||
|
|
||||||
write(1, uncompressedChunk, inflateResult.numBytesWrittenToOutput);
|
write(1, uncompressedChunk, inflateResult.numBytesWrittenToOutput);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue