diff --git a/src/metaUtils.cxx b/src/metaUtils.cxx index e37635b..725d38f 100644 --- a/src/metaUtils.cxx +++ b/src/metaUtils.cxx @@ -890,6 +890,29 @@ MET_PerformUncompression(const unsigned char * sourceCompressed, } } while (d_stream.avail_out == 0); } while (err != Z_STREAM_END && err >= 0); + // The output buffer can fill before the trailer arrives in a later input + // chunk; keep feeding input so zlib can reach the CRC and report stream end. + unsigned char trailerScratch[1]; + while (err == Z_BUF_ERROR && dest_pos == uncompressedDataSize) + { + if (d_stream.avail_in == 0) + { + if (source_pos >= sourceCompressedSize) + { + break; + } + d_stream.next_in = const_cast(sourceCompressed + source_pos); + d_stream.avail_in = static_cast(std::min(sourceCompressedSize - source_pos, max_chunk_size)); + source_pos += d_stream.avail_in; + } + d_stream.next_out = trailerScratch; + d_stream.avail_out = 1; + err = inflate(&d_stream, Z_NO_FLUSH); + if (d_stream.avail_out == 0) + { + break; + } + } inflateEnd(&d_stream); if (err != Z_STREAM_END) {