From b3ff0b746e4ba0e26265baf94217a92ba3cf7d05 Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Wed, 12 Mar 2014 05:50:19 +0400 Subject: [PATCH] Autodetect inflate window size by default --- lib/inflate.js | 7 ++++--- test/helpers.js | 13 ++++++++++--- test/inflate.js | 6 +++--- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/inflate.js b/lib/inflate.js index 59e60b6..521aad8 100644 --- a/lib/inflate.js +++ b/lib/inflate.js @@ -89,19 +89,20 @@ var Inflate = function(options) { this.options = utils.assign({ chunkSize: 16384, - windowBits: 15 + windowBits: 0 }, options || {}); var opt = this.options; // check `raw` if `windowBits` NOT defined directly, // or we will get bug because of autodetect - if (opt.raw && (opt.windowBits > 0) && (opt.windowBits < 16)) { + if (opt.raw && (opt.windowBits >= 0) && (opt.windowBits < 16)) { opt.windowBits = -opt.windowBits; + if (opt.windowBits === 0) { opt.windowBits = -15; } } // If `windowBits` not defined (and mode not raw) - set autodetect flag for gzip/deflate - if ((opt.windowBits > 0) && (opt.windowBits < 16) && + if ((opt.windowBits >= 0) && (opt.windowBits < 16) && !(options && options.windowBits)) { opt.windowBits += 32; } diff --git a/test/helpers.js b/test/helpers.js index 211592a..2420e1a 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -131,7 +131,14 @@ function testDeflate(zlib_factory, pako_deflate, samples, options, callback) { function testInflate(samples, options, callback) { - var name, data, deflated, inflated; + var name, data, deflated, inflated, inflate_options; + + // inflate options have windowBits = 0 to force autodetect window size + // + inflate_options = pako_utils.assign({}, options); + if (inflate_options.windowBits > 0 && inflate_options.windowBits < 16) { + inflate_options.windowBits = 0; + } for (name in samples) { data = samples[name]; @@ -139,7 +146,7 @@ function testInflate(samples, options, callback) { // with untyped arrays pako_utils.forceUntyped = true; - inflated = pako.inflate(deflated, options); + inflated = pako.inflate(deflated, inflate_options); pako_utils.forceUntyped = false; if (!cmpBuf(inflated, data)) { @@ -148,7 +155,7 @@ function testInflate(samples, options, callback) { } // with typed arrays - inflated = pako.inflate(deflated, options); + inflated = pako.inflate(deflated, inflate_options); if (!cmpBuf(inflated, data)) { callback('Error in "' + name + '" - inflate result != original'); diff --git a/test/inflate.js b/test/inflate.js index 01a49bb..4a55624 100644 --- a/test/inflate.js +++ b/test/inflate.js @@ -30,11 +30,11 @@ describe('Inflate ungzip', function () { var gzipped = pako.gzip(samples.lorem_cat); it.skip('ungzip with autodetect', function(done) { - done(helpers.cmp(orig, pako.inflate(gzipped).result)); + done(helpers.cmp(orig, pako.inflate(gzipped))); }); it.skip('ungzip with method set directly', function(done) { - done(helpers.cmp(orig, pako.inflate(gzipped, { windowBits: 15 + 16 }).result)); + done(helpers.cmp(orig, pako.inflate(gzipped, { windowBits: 15 + 16 }))); }); }); @@ -99,7 +99,7 @@ describe('Inflate windowBits', function () { it('windowBits 9', function(done) { testInflate(samples, { windowBits: 9 }, done); }); - it.skip('windowBits 8', function(done) { + it('windowBits 8', function(done) { testInflate(samples, { windowBits: 8 }, done); });