Save a copy during zlib decompression

This commit is contained in:
sfan5 2024-04-20 21:38:05 +02:00
parent 7e4caacb9e
commit 40a5e16e21
1 changed files with 14 additions and 6 deletions

View File

@ -29,31 +29,39 @@ ustring ZlibDecompressor::decompress()
const size_t size = m_size - m_seekPos; const size_t size = m_size - m_seekPos;
ustring buffer; ustring buffer;
constexpr size_t BUFSIZE = 128 * 1024; constexpr size_t BUFSIZE = 32 * 1024;
unsigned char temp_buffer[BUFSIZE];
z_stream strm; z_stream strm;
strm.zalloc = Z_NULL; strm.zalloc = Z_NULL;
strm.zfree = Z_NULL; strm.zfree = Z_NULL;
strm.opaque = Z_NULL; strm.opaque = Z_NULL;
strm.next_in = Z_NULL; strm.next_in = Z_NULL;
strm.avail_in = size; strm.avail_in = 0;
if (inflateInit(&strm) != Z_OK) if (inflateInit(&strm) != Z_OK)
throw DecompressError(); throw DecompressError();
strm.next_in = const_cast<unsigned char *>(data); strm.next_in = const_cast<unsigned char *>(data);
strm.avail_in = size;
buffer.resize(BUFSIZE);
strm.next_out = &buffer[0];
strm.avail_out = BUFSIZE;
int ret = 0; int ret = 0;
do { do {
strm.avail_out = BUFSIZE;
strm.next_out = temp_buffer;
ret = inflate(&strm, Z_NO_FLUSH); ret = inflate(&strm, Z_NO_FLUSH);
buffer.append(temp_buffer, BUFSIZE - strm.avail_out); if (strm.avail_out == 0) {
const auto off = buffer.size();
buffer.reserve(off + BUFSIZE);
strm.next_out = &buffer[off];
strm.avail_out = BUFSIZE;
}
} while (ret == Z_OK); } while (ret == Z_OK);
if (ret != Z_STREAM_END) if (ret != Z_STREAM_END)
throw DecompressError(); throw DecompressError();
m_seekPos += strm.next_in - data; m_seekPos += strm.next_in - data;
buffer.resize(buffer.size() - strm.avail_out);
(void) inflateEnd(&strm); (void) inflateEnd(&strm);
return buffer; return buffer;