From 5f728b177d4dfb630e6414779509b1571f6754d9 Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Mon, 14 Sep 2015 15:01:53 +0300 Subject: [PATCH] code/comments cleanup --- lib/inflate.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/inflate.js b/lib/inflate.js index 8d7a987..d863f8f 100644 --- a/lib/inflate.js +++ b/lib/inflate.js @@ -177,8 +177,10 @@ Inflate.prototype.push = function(data, mode) { var chunkSize = this.options.chunkSize; var status, _mode; var next_out_utf8, tail, utf8str; - // allow Z_BUF_ERROR on next inflate call - var allowBufError = 0; + + // Flag to properly process Z_BUF_ERROR on testing inflate call + // when we check that all output data was flushed. + var allowBufError = false; if (this.ended) { return false; } _mode = (mode === ~~mode) ? mode : ((mode === true) ? c.Z_FINISH : c.Z_NO_FLUSH); @@ -205,9 +207,9 @@ Inflate.prototype.push = function(data, mode) { status = zlib_inflate.inflate(strm, c.Z_NO_FLUSH); /* no bad return value */ - if (status === c.Z_BUF_ERROR && allowBufError === 1) { + if (status === c.Z_BUF_ERROR && allowBufError === true) { status = c.Z_OK; - allowBufError = 0; + allowBufError = false; } if (status !== c.Z_STREAM_END && status !== c.Z_OK) { @@ -238,10 +240,18 @@ Inflate.prototype.push = function(data, mode) { } } } - // we need to check internal inflate buffers and state but inflate can return Z_BUF_ERROR if no output + + // When no more input data, we should check that internal inflate buffers + // are flushed. The only way to do it when avail_out = 0 - run one more + // inflate pass. But if output data not exists, inflate return Z_BUF_ERROR. + // Here we set flag to process this error properly. + // + // NOTE. Deflate does not return error in this case and does not needs such + // logic. if (strm.avail_in === 0 && strm.avail_out === 0) { - allowBufError = 1; + allowBufError = true; } + } while ((strm.avail_in > 0 || strm.avail_out === 0) && status !== c.Z_STREAM_END); if (status === c.Z_STREAM_END) {