Autodetect inflate window size by default

This commit is contained in:
Vitaly Puzrin 2014-03-12 05:50:19 +04:00
parent dfe21c62da
commit b3ff0b746e
3 changed files with 17 additions and 9 deletions

View file

@ -89,19 +89,20 @@ var Inflate = function(options) {
this.options = utils.assign({ this.options = utils.assign({
chunkSize: 16384, chunkSize: 16384,
windowBits: 15 windowBits: 0
}, options || {}); }, options || {});
var opt = this.options; var opt = this.options;
// check `raw` if `windowBits` NOT defined directly, // check `raw` if `windowBits` NOT defined directly,
// or we will get bug because of autodetect // 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; 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 `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)) { !(options && options.windowBits)) {
opt.windowBits += 32; opt.windowBits += 32;
} }

View file

@ -131,7 +131,14 @@ function testDeflate(zlib_factory, pako_deflate, samples, options, callback) {
function testInflate(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) { for (name in samples) {
data = samples[name]; data = samples[name];
@ -139,7 +146,7 @@ function testInflate(samples, options, callback) {
// with untyped arrays // with untyped arrays
pako_utils.forceUntyped = true; pako_utils.forceUntyped = true;
inflated = pako.inflate(deflated, options); inflated = pako.inflate(deflated, inflate_options);
pako_utils.forceUntyped = false; pako_utils.forceUntyped = false;
if (!cmpBuf(inflated, data)) { if (!cmpBuf(inflated, data)) {
@ -148,7 +155,7 @@ function testInflate(samples, options, callback) {
} }
// with typed arrays // with typed arrays
inflated = pako.inflate(deflated, options); inflated = pako.inflate(deflated, inflate_options);
if (!cmpBuf(inflated, data)) { if (!cmpBuf(inflated, data)) {
callback('Error in "' + name + '" - inflate result != original'); callback('Error in "' + name + '" - inflate result != original');

View file

@ -30,11 +30,11 @@ describe('Inflate ungzip', function () {
var gzipped = pako.gzip(samples.lorem_cat); var gzipped = pako.gzip(samples.lorem_cat);
it.skip('ungzip with autodetect', function(done) { 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) { 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) { it('windowBits 9', function(done) {
testInflate(samples, { windowBits: 9 }, done); testInflate(samples, { windowBits: 9 }, done);
}); });
it.skip('windowBits 8', function(done) { it('windowBits 8', function(done) {
testInflate(samples, { windowBits: 8 }, done); testInflate(samples, { windowBits: 8 }, done);
}); });