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!
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);