Use Z_STREAM_END to detect gz compression finish.

Checking for free space in the output buffer worked, but if the buffer was completely filled then deflate() would need to be called again, which was wasteful and a bit confusing for debugging.

Instead, use Z_STREAM_END to detect that compression is done.

This change was inspired by the bz2 implementation in a021c9fe since bz2 does not allow BZ2_bzCompress() to be called after BZ_STREAM_END is returned. That made it obvious that gz would prefer the same implementation, even if it is more tolerant. The documentation at https://www.zlib.net/manual.html agrees.
This commit is contained in:
David Steele
2020-05-07 10:22:22 -04:00
parent f8509ab76c
commit 6646446d2a
+2 -2
View File
@@ -109,13 +109,13 @@ gzCompressProcess(THIS_VOID, const Buffer *uncompressed, Buffer *compressed)
this->stream.next_out = bufPtr(compressed) + bufUsed(compressed);
// Perform compression
gzError(deflate(&this->stream, this->flushing ? Z_FINISH : Z_NO_FLUSH));
int result = gzError(deflate(&this->stream, this->flushing ? Z_FINISH : Z_NO_FLUSH));
// Set buffer used space
bufUsedSet(compressed, bufSize(compressed) - (size_t)this->stream.avail_out);
// Is compression done?
if (this->flushing && this->stream.avail_out > 0)
if (this->flushing && result == Z_STREAM_END)
this->done = true;
// Can more input be provided on the next call?