diff --git a/lib/inflate.js b/lib/inflate.js index 4028130..7535d15 100644 --- a/lib/inflate.js +++ b/lib/inflate.js @@ -144,6 +144,22 @@ function Inflate(options) { this.header = new GZheader(); zlib_inflate.inflateGetHeader(this.strm, this.header); + + // Setup dictionary + if (opt.dictionary) { + // Convert data if needed + if (typeof opt.dictionary === 'string') { + opt.dictionary = strings.string2buf(opt.dictionary); + } else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') { + opt.dictionary = new Uint8Array(opt.dictionary); + } + if (opt.raw) { //In raw mode we need to set the dictionary early + status = zlib_inflate.inflateSetDictionary(this.strm, opt.dictionary); + if (status !== c.Z_OK) { + throw new Error(msg[status]); + } + } + } } /** @@ -180,7 +196,6 @@ Inflate.prototype.push = function (data, mode) { var dictionary = this.options.dictionary; var status, _mode; var next_out_utf8, tail, utf8str; - var dict; // Flag to properly process Z_BUF_ERROR on testing inflate call // when we check that all output data was flushed. @@ -212,17 +227,7 @@ Inflate.prototype.push = function (data, mode) { status = zlib_inflate.inflate(strm, c.Z_NO_FLUSH); /* no bad return value */ if (status === c.Z_NEED_DICT && dictionary) { - // Convert data if needed - if (typeof dictionary === 'string') { - dict = strings.string2buf(dictionary); - } else if (toString.call(dictionary) === '[object ArrayBuffer]') { - dict = new Uint8Array(dictionary); - } else { - dict = dictionary; - } - - status = zlib_inflate.inflateSetDictionary(this.strm, dict); - + status = zlib_inflate.inflateSetDictionary(this.strm, dictionary); } if (status === c.Z_BUF_ERROR && allowBufError === true) { diff --git a/test/inflate.js b/test/inflate.js index 83f92f7..d7f7573 100644 --- a/test/inflate.js +++ b/test/inflate.js @@ -169,22 +169,44 @@ describe('Inflate with dictionary', function () { it('should throw on the wrong dictionary', function () { // var zCompressed = helpers.deflateSync('world', { dictionary: new Buffer('hello') }); - var zCompressed = new Buffer([ 120, 187, 6, 44, 2, 21, 43, 207, 47, 202, 73, 1, 0, 6, 166, 2, 41 ]); + var zCompressed = new Uint8Array([ 120, 187, 6, 44, 2, 21, 43, 207, 47, 202, 73, 1, 0, 6, 166, 2, 41 ]); assert.throws(function () { - pako.inflate(zCompressed, { dictionary: new Buffer('world') }); + pako.inflate(zCompressed, { dictionary: 'world' }); }, /data error/); }); it('trivial dictionary', function () { - var dict = new Buffer('abcdefghijklmnoprstuvwxyz'); + var dict = 'abcdefghijklmnoprstuvwxyz'; testInflate(samples, { dictionary: dict }, { dictionary: dict }); }); it('spdy dictionary', function () { var spdyDict = require('fs').readFileSync(require('path').join(__dirname, 'fixtures', 'spdy_dict.txt')); - - testInflate(samples, { dictionary: spdyDict }, { dictionary: helpers.spdyDict }); + testInflate(samples, { dictionary: spdyDict }, { dictionary: spdyDict }); }); + it('should throw if directory is not supplied to raw inflate', function () { + var dict = 'abcdefghijklmnoprstuvwxyz'; + assert.throws(function () { + testInflate(samples, { raw: true }, { raw: true, dictionary: dict }); + }); + }); + + it('tests raw inflate with spdy dictionary', function () { + var spdyDict = require('fs').readFileSync(require('path').join(__dirname, 'fixtures', 'spdy_dict.txt')); + testInflate(samples, { raw: true, dictionary: spdyDict }, { raw: true, dictionary: spdyDict }); + }); + + it('tests dictionary as Uint8Array', function () { + var dict = new Uint8Array(100); + for (var i = 0; i < 100; i++) dict[i] = Math.random() * 256; + testInflate(samples, { dictionary: dict }, { dictionary: dict }); + }); + + it('tests dictionary as ArrayBuffer', function () { + var dict = new Uint8Array(100); + for (var i = 0; i < 100; i++) dict[i] = Math.random() * 256; + testInflate(samples, { dictionary: dict.buffer }, { dictionary: dict }); + }); });