Skip to content

Commit 88000c8

Browse files
committed
Allow raw deflation with a custom dictionary.
This is just roughly tested, but I just ported the change from nodejs/node#8512, or at least attempted to. Before, when trying to inflateRaw (or inflate({raw:true});) with a custom dictionary, you would get 'invalid distance too far back'. This no longer seems to happen, so I think I fixed it.
1 parent 893381a commit 88000c8

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

lib/inflate.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ function Inflate(options) {
144144
this.header = new GZheader();
145145

146146
zlib_inflate.inflateGetHeader(this.strm, this.header);
147+
148+
// With a raw dictionary, we need to set the dictionary early.
149+
if (opt.raw && opt.dictionary) {
150+
var dict;
151+
// Convert data if needed
152+
if (typeof opt.dictionary === 'string') {
153+
dict = strings.string2buf(opt.dictionary);
154+
} else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') {
155+
dict = new Uint8Array(opt.dictionary);
156+
} else {
157+
dict = opt.dictionary;
158+
}
159+
zlib_inflate.inflateSetDictionary(this.strm, dict);
160+
}
147161
}
148162

149163
/**
@@ -211,7 +225,8 @@ Inflate.prototype.push = function (data, mode) {
211225

212226
status = zlib_inflate.inflate(strm, c.Z_NO_FLUSH); /* no bad return value */
213227

214-
if (status === c.Z_NEED_DICT && dictionary) {
228+
// We have already set the dictionary if we used a raw stream.
229+
if (!this.options.raw && status === c.Z_NEED_DICT && dictionary) {
215230
// Convert data if needed
216231
if (typeof dictionary === 'string') {
217232
dict = strings.string2buf(dictionary);

0 commit comments

Comments
 (0)