🐛 Fix error in inflate function

This commit is contained in:
C-3PO 2018-08-09 16:20:21 +02:00
parent 6b9b31c2bc
commit 9353111085
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
2 changed files with 14 additions and 12 deletions

View file

@ -45,11 +45,14 @@ struct InflateOutput inflateInflate(unsigned long numInputBytes, bool hasMoreByt
} }
//output buffer was previously fully filled and all output was processed. New uncompressed data can be written starting at offset 0 //output buffer was previously fully filled and all output was processed. New uncompressed data can be written starting at offset 0
if (spaceInOutput == 0) {
uncomprBufferNext = (mz_uint8 *)uncomprBuffer; uncomprBufferNext = (mz_uint8 *)uncomprBuffer;
spaceInOutput = uncomprBufferSize; spaceInOutput = uncomprBufferSize;
}
struct InflateOutput out; struct InflateOutput out;
out.hasReachedEnd = false; out.hasReachedEnd = false;
out.numBytesWrittenToOutput = 0UL;
while (remainingInput > 0 && spaceInOutput > 0) { while (remainingInput > 0 && spaceInOutput > 0) {
in_bytes = remainingInput; in_bytes = remainingInput;
@ -71,6 +74,7 @@ struct InflateOutput inflateInflate(unsigned long numInputBytes, bool hasMoreByt
remainingInput -= in_bytes; remainingInput -= in_bytes;
uncomprBufferNext = (uint8_t *)(uncomprBufferNext + out_bytes); uncomprBufferNext = (uint8_t *)(uncomprBufferNext + out_bytes);
spaceInOutput -= out_bytes; spaceInOutput -= out_bytes;
out.numBytesWrittenToOutput += out_bytes;
//Check for errors //Check for errors
if (status <= TINFL_STATUS_DONE) { if (status <= TINFL_STATUS_DONE) {
@ -91,12 +95,6 @@ struct InflateOutput inflateInflate(unsigned long numInputBytes, bool hasMoreByt
if (status == TINFL_STATUS_HAS_MORE_OUTPUT && spaceInOutput != 0) { if (status == TINFL_STATUS_HAS_MORE_OUTPUT && spaceInOutput != 0) {
fprintf(stderr, "Received status HAS_MORE_OUTPUT, with remaining input %lu and space in output %lu.\n", remainingInput, spaceInOutput); fprintf(stderr, "Received status HAS_MORE_OUTPUT, with remaining input %lu and space in output %lu.\n", remainingInput, spaceInOutput);
} }
//commented out because we immediately exit while loop if it is zero
//If output buffer is filled, we need to clear it before decompressing more data
/*if (spaceInOutput == 0) {
break;
}*/
} }
//Return output //Return output
@ -105,7 +103,6 @@ struct InflateOutput inflateInflate(unsigned long numInputBytes, bool hasMoreByt
} else { } else {
out.needMoreInput = false; out.needMoreInput = false;
} }
out.numBytesWrittenToOutput = uncomprBufferSize - spaceInOutput;
return out; return out;
} }

View file

@ -11,7 +11,7 @@
#include "inflate.h" #include "inflate.h"
#include "utils/min.h" #include "utils/min.h"
#define BUFFER_SIZE 32UL * 1024UL //512 KiB #define BUFFER_SIZE 512UL * 1024UL //512 KiB
#define ENCRYPTION_HEADER_LENGTH 12UL #define ENCRYPTION_HEADER_LENGTH 12UL
#define LOCAL_FILE_HEADER_MAGIC (uint32_t)0x04034b50 #define LOCAL_FILE_HEADER_MAGIC (uint32_t)0x04034b50
@ -94,9 +94,10 @@ int main(int argc, unsigned char *argv[]) {
bool needToRead = true; bool needToRead = true;
bool hasReachedEnd = false; bool hasReachedEnd = false;
unsigned long chunkSize; unsigned long chunkSize;
unsigned long uncompressedPosition = 0UL;
while (remainingBytes > 0 || !hasReachedEnd) { while (remainingBytes > 0 || !hasReachedEnd) {
if (needToRead) { if (needToRead) {
chunkSize = min(BUFFER_SIZE, remainingBytes); chunkSize = min(BUFFER_SIZE - uncompressedPosition, remainingBytes);
//memset(compressedChunk, 0, chunkSize); //memset(compressedChunk, 0, chunkSize);
getBytes(compressedChunk, chunkSize); getBytes(compressedChunk, chunkSize);
remainingBytes -= chunkSize; remainingBytes -= chunkSize;
@ -116,7 +117,11 @@ int main(int argc, unsigned char *argv[]) {
//important: we must not modify uncompressedChunk since miniz may use it as dictionary and read from it during the next invocation of inflateInflate() //important: we must not modify uncompressedChunk since miniz may use it as dictionary and read from it during the next invocation of inflateInflate()
write(1, uncompressedChunk, inflateResult.numBytesWrittenToOutput); write(1, uncompressedChunk + uncompressedPosition, inflateResult.numBytesWrittenToOutput);
uncompressedPosition += inflateResult.numBytesWrittenToOutput;
while (uncompressedPosition >= BUFFER_SIZE) {
uncompressedPosition -= BUFFER_SIZE;
}
//Optionally perform xdelta3 //Optionally perform xdelta3
//TODO //TODO