From 4d5c8a9ae267df6837b6a920da5010cea755b91a Mon Sep 17 00:00:00 2001 From: Adam Sutton Date: Wed, 1 Jan 2014 12:48:39 +0000 Subject: [PATCH] zlib: fix error on partial compression of files. Fixes #1902. This occurs where compressing small, already compressd files, typically images. I could avoid compression of very small (or already compressed) files. But its simple to keep things uniform and fixing this makes sense anyway! --- src/filebundle.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/filebundle.c b/src/filebundle.c index 60ae966c..97e05e30 100644 --- a/src/filebundle.c +++ b/src/filebundle.c @@ -127,12 +127,25 @@ static uint8_t *_fb_deflate ( const uint8_t *data, size_t orig, size_t *size ) zstr.next_out = bufout; /* Decompress */ - err = deflate(&zstr, Z_FINISH); - if ( (err != Z_STREAM_END && err != Z_OK) || zstr.total_out == 0 ) { - free(bufout); - bufout = NULL; - } else { - *size = zstr.total_out; + while (1) { + err = deflate(&zstr, Z_FINISH); + + /* Need more space */ + if (err == Z_OK && zstr.avail_out == 0) { + bufout = realloc(bufout, zstr.total_out * 2); + zstr.avail_out = zstr.total_out; + zstr.next_out = bufout + zstr.total_out; + continue; + } + + /* Error */ + if ( (err != Z_STREAM_END && err != Z_OK) || zstr.total_out == 0 ) { + free(bufout); + bufout = NULL; + } else { + *size = zstr.total_out; + } + break; } free(bufin); deflateEnd(&zstr);