diff --git a/src/inflate.c b/src/inflate.c index 458cd78..72f75b3 100644 --- a/src/inflate.c +++ b/src/inflate.c @@ -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 - uncomprBufferNext = (mz_uint8 *)uncomprBuffer; - spaceInOutput = uncomprBufferSize; + if (spaceInOutput == 0) { + uncomprBufferNext = (mz_uint8 *)uncomprBuffer; + spaceInOutput = uncomprBufferSize; + } struct InflateOutput out; out.hasReachedEnd = false; + out.numBytesWrittenToOutput = 0UL; while (remainingInput > 0 && spaceInOutput > 0) { in_bytes = remainingInput; @@ -71,6 +74,7 @@ struct InflateOutput inflateInflate(unsigned long numInputBytes, bool hasMoreByt remainingInput -= in_bytes; uncomprBufferNext = (uint8_t *)(uncomprBufferNext + out_bytes); spaceInOutput -= out_bytes; + out.numBytesWrittenToOutput += out_bytes; //Check for errors 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) { 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 @@ -105,7 +103,6 @@ struct InflateOutput inflateInflate(unsigned long numInputBytes, bool hasMoreByt } else { out.needMoreInput = false; } - out.numBytesWrittenToOutput = uncomprBufferSize - spaceInOutput; return out; } diff --git a/src/main.c b/src/main.c index 4182678..2948420 100644 --- a/src/main.c +++ b/src/main.c @@ -11,7 +11,7 @@ #include "inflate.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 LOCAL_FILE_HEADER_MAGIC (uint32_t)0x04034b50 @@ -94,9 +94,10 @@ int main(int argc, unsigned char *argv[]) { bool needToRead = true; bool hasReachedEnd = false; unsigned long chunkSize; + unsigned long uncompressedPosition = 0UL; while (remainingBytes > 0 || !hasReachedEnd) { if (needToRead) { - chunkSize = min(BUFFER_SIZE, remainingBytes); + chunkSize = min(BUFFER_SIZE - uncompressedPosition, remainingBytes); //memset(compressedChunk, 0, chunkSize); getBytes(compressedChunk, 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() - write(1, uncompressedChunk, inflateResult.numBytesWrittenToOutput); + write(1, uncompressedChunk + uncompressedPosition, inflateResult.numBytesWrittenToOutput); + uncompressedPosition += inflateResult.numBytesWrittenToOutput; + while (uncompressedPosition >= BUFFER_SIZE) { + uncompressedPosition -= BUFFER_SIZE; + } //Optionally perform xdelta3 //TODO