From a75010e2cc9a8a7956f4fa04c0fb60597ec78b62 Mon Sep 17 00:00:00 2001 From: Tino Lange Date: Tue, 9 Jun 2015 20:38:12 +0200 Subject: [PATCH] - high-level wrapper: make {in,de}flate.push(data, Z_SYNC_FLUSH) working (will fix issue #34) --- lib/deflate.js | 10 +++++++++- lib/inflate.js | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/deflate.js b/lib/deflate.js index 9cd0392..625ec67 100644 --- a/lib/deflate.js +++ b/lib/deflate.js @@ -17,6 +17,7 @@ var Z_FINISH = 4; var Z_OK = 0; var Z_STREAM_END = 1; +var Z_SYNC_FLUSH = 2; var Z_DEFAULT_COMPRESSION = -1; @@ -224,7 +225,7 @@ Deflate.prototype.push = function(data, mode) { this.ended = true; return false; } - if (strm.avail_out === 0 || (strm.avail_in === 0 && _mode === Z_FINISH)) { + if (strm.avail_out === 0 || (strm.avail_in === 0 && (_mode === Z_FINISH || _mode === Z_SYNC_FLUSH))) { if (this.options.to === 'string') { this.onData(strings.buf2binstring(utils.shrinkBuf(strm.output, strm.next_out))); } else { @@ -241,6 +242,13 @@ Deflate.prototype.push = function(data, mode) { return status === Z_OK; } + // callback interim results if Z_SYNC_FLUSH. + if (_mode === Z_SYNC_FLUSH) { + this.onEnd(Z_OK); + strm.avail_out = 0; + return true; + } + return true; }; diff --git a/lib/inflate.js b/lib/inflate.js index 96915bc..b271035 100644 --- a/lib/inflate.js +++ b/lib/inflate.js @@ -207,7 +207,7 @@ Inflate.prototype.push = function(data, mode) { } if (strm.next_out) { - if (strm.avail_out === 0 || status === c.Z_STREAM_END || (strm.avail_in === 0 && _mode === c.Z_FINISH)) { + if (strm.avail_out === 0 || status === c.Z_STREAM_END || (strm.avail_in === 0 && (_mode === c.Z_FINISH || _mode === c.Z_SYNC_FLUSH))) { if (this.options.to === 'string') { @@ -233,6 +233,7 @@ Inflate.prototype.push = function(data, mode) { if (status === c.Z_STREAM_END) { _mode = c.Z_FINISH; } + // Finalize on the last chunk. if (_mode === c.Z_FINISH) { status = zlib_inflate.inflateEnd(this.strm); @@ -241,6 +242,13 @@ Inflate.prototype.push = function(data, mode) { return status === c.Z_OK; } + // callback interim results if Z_SYNC_FLUSH. + if (_mode === c.Z_SYNC_FLUSH) { + this.onEnd(c.Z_OK); + strm.avail_out = 0; + return true; + } + return true; };