Skip to content

Commit

Permalink
COMMON: Fix INFLATE without size when size evenly divides frame size
Browse files Browse the repository at this point in the history
When decompressing without knowing the output size beforehand, we
decompress into buffers of frameSize length and then paste them
together into one output buffer at the end.

When pasting, we used to calculate the size of the last frame with
"% frameSize", which, if the output size evenly divides the frame
size, is of course 0.

We could just check for 0 there and use the frame size instead then,
but using a running counter of bytes left to write is easier.
  • Loading branch information
DrMcCoy committed Aug 5, 2018
1 parent b56b28c commit 43d7a44
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/common/deflate.cpp
Expand Up @@ -116,18 +116,12 @@ byte *decompressDeflateWithoutOutputSize(const byte *data, size_t inputSize, siz
zResult = inflate(&strm, Z_SYNC_FLUSH);
if (zResult != Z_STREAM_END && zResult != Z_OK)
throw Exception("Failed to inflate: %s (%d)", zError(zResult), zResult);
} while (strm.avail_in != 0);

if (zResult != Z_STREAM_END)
throw Exception("Failed to inflate: %s (%d)", zError(zResult), zResult);
} while (zResult != Z_STREAM_END);

ScopedArray<byte> decompressedData(new byte[strm.total_out]);
for (size_t i = 0; i < buffers.size(); ++i) {
if (i == buffers.size() - 1)
std::memcpy(decompressedData.get() + i * frameSize, buffers[i], strm.total_out % frameSize);
else
std::memcpy(decompressedData.get() + i * frameSize, buffers[i], frameSize);
}
for (size_t i = 0, size = strm.total_out; i < buffers.size(); ++i, size -= frameSize)
std::memcpy(decompressedData.get() + i * frameSize, buffers[i], MIN<size_t>(size, frameSize));

outputSize = strm.total_out;
return decompressedData.release();
Expand Down

0 comments on commit 43d7a44

Please sign in to comment.