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] 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 }); + }); });