diff --git a/lib/deflate.js b/lib/deflate.js index d3f0c5f..d437ebb 100644 --- a/lib/deflate.js +++ b/lib/deflate.js @@ -131,6 +131,7 @@ var Deflate = function(options) { this.chunks = []; // chunks of compressed data this.strm = new zstream(); + this.strm.avail_out = 0; var status = zlib_deflate.deflateInit2( this.strm, @@ -193,7 +194,6 @@ Deflate.prototype.push = function(data, mode) { strm.next_in_index = 0; strm.avail_in = strm.next_in.length; - strm.avail_out = 0; do { if (strm.avail_out === 0) { @@ -208,7 +208,7 @@ Deflate.prototype.push = function(data, mode) { this.ended = true; return false; } - if (strm.avail_out === 0 || strm.avail_in === 0) { + if (strm.avail_out === 0 || (strm.avail_in === 0 && _mode === Z_FINISH)) { if (this.options.to === 'string') { this.onData(utils.buf2binstring(utils.shrinkBuf(strm.next_out, strm.next_out_index))); } else { diff --git a/lib/inflate.js b/lib/inflate.js index 36d54b0..d75ea0a 100644 --- a/lib/inflate.js +++ b/lib/inflate.js @@ -129,6 +129,7 @@ var Inflate = function(options) { this.chunks = []; // chunks of compressed data this.strm = new zstream(); + this.strm.avail_out = 0; var status = zlib_inflate.inflateInit2( this.strm, @@ -181,7 +182,7 @@ Inflate.prototype.push = function(data, mode) { var next_out_utf8_index, tail, utf8str; if (this.ended) { return false; } - _mode = c.Z_NO_FLUSH; + _mode = (mode === ~~mode) ? mode : ((mode === true) ? c.Z_FINISH : c.Z_NO_FLUSH); // Convert data if needed if (typeof data === 'string') { @@ -193,7 +194,6 @@ Inflate.prototype.push = function(data, mode) { strm.next_in_index = 0; strm.avail_in = strm.next_in.length; - strm.avail_out = 0; do { if (strm.avail_out === 0) { @@ -202,7 +202,7 @@ Inflate.prototype.push = function(data, mode) { strm.avail_out = chunkSize; } - status = zlib_inflate.inflate(strm, _mode); /* no bad return value */ + status = zlib_inflate.inflate(strm, c.Z_NO_FLUSH); /* no bad return value */ if (status !== c.Z_STREAM_END && status !== c.Z_OK) { this.onEnd(status); @@ -211,7 +211,7 @@ Inflate.prototype.push = function(data, mode) { } if (strm.next_out_index) { - if (strm.avail_out === 0 || strm.avail_in === 0 || status === c.Z_STREAM_END) { + if (strm.avail_out === 0 || status === c.Z_STREAM_END || (strm.avail_in === 0 && _mode === c.Z_FINISH)) { if (this.to === 'string') { @@ -249,8 +249,6 @@ Inflate.prototype.push = function(data, mode) { if (status === c.Z_STREAM_END) { _mode = c.Z_FINISH; - } else { - _mode = (mode === ~~mode) ? mode : ((mode === true) ? c.Z_FINISH : c.Z_NO_FLUSH); } // Finalize on the last chunk. if (_mode === c.Z_FINISH) {