From 919ad7f228efe38250907337fb8cb7d664b4c9c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=BDubor=20Illek?= Date: Wed, 9 Jan 2019 23:57:19 +0100 Subject: [PATCH 1/4] Fix for raw inflate with directory --- lib/inflate.js | 24 ++++++++++++------------ test/inflate.js | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/lib/inflate.js b/lib/inflate.js index 4028130..104def2 100644 --- a/lib/inflate.js +++ b/lib/inflate.js @@ -144,6 +144,17 @@ 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) zlib_inflate.inflateSetDictionary(this.strm, opt.dictionary); //In raw mode we need to set the dictionary early + } } /** @@ -180,7 +191,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 +222,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..108e712 100644 --- a/test/inflate.js +++ b/test/inflate.js @@ -183,8 +183,18 @@ describe('Inflate with dictionary', function () { 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 = new Buffer('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 }); + }); }); From cfc92c2fb860f9c8923fc938c970fd8727cc7e84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=BDubor=20Illek?= Date: Thu, 10 Jan 2019 01:08:10 +0100 Subject: [PATCH 2/4] Replaced Buffer() in inflate tests with dictionary. --- test/inflate.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/inflate.js b/test/inflate.js index 108e712..464eae7 100644 --- a/test/inflate.js +++ b/test/inflate.js @@ -169,15 +169,15 @@ 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 }); }); @@ -187,7 +187,7 @@ describe('Inflate with dictionary', function () { }); it('should throw if directory is not supplied to raw inflate', function () { - var dict = new Buffer('abcdefghijklmnoprstuvwxyz'); + var dict = 'abcdefghijklmnoprstuvwxyz'; assert.throws(function () { testInflate(samples, { raw: true }, { raw: true, dictionary: dict }); }); From b4f1c205efdf998f512cbf5484445fc0b30f8051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=BDubor=20Illek?= Date: Thu, 10 Jan 2019 10:38:02 +0100 Subject: [PATCH 3/4] Added basic tests for inflate with dictionary as Uint8Array or ArrayBuffer --- test/inflate.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/inflate.js b/test/inflate.js index 464eae7..d7f7573 100644 --- a/test/inflate.js +++ b/test/inflate.js @@ -197,4 +197,16 @@ describe('Inflate with 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 }); + }); }); From a28d0181cb155d3068415d8d7a6109e11d5fc241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=BDubor=20Illek?= Date: Mon, 14 Jan 2019 17:54:51 +0100 Subject: [PATCH 4/4] Check return code of inflateSetDictionary() --- lib/inflate.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/inflate.js b/lib/inflate.js index 104def2..7535d15 100644 --- a/lib/inflate.js +++ b/lib/inflate.js @@ -153,7 +153,12 @@ function Inflate(options) { } else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') { opt.dictionary = new Uint8Array(opt.dictionary); } - if (opt.raw) zlib_inflate.inflateSetDictionary(this.strm, opt.dictionary); //In raw mode we need to set the dictionary early + 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]); + } + } } }