Since
e503f572 in njs and
bb3dcf21 in QuickJS, inflateSync() has
tried to set a supplied dictionary before reading the zlib header.
Zlib rejects this for wrapped streams, even if no dictionary is
needed.
For wrapped streams, wait for Z_NEED_DICT before setting the
dictionary. Raw inflate still needs the dictionary up front.
NJS_CHB_MP_INIT(&chain, njs_vm_memory_pool(vm));
NJS_CHB_MP_INIT(&chain, njs_vm_memory_pool(vm));
- if (dictionary.start != NULL) {
+ if (raw && dictionary.start != NULL) {
rc = inflateSetDictionary(&stream, dictionary.start, dictionary.length);
if (njs_slow_path(rc != Z_OK)) {
rc = inflateSetDictionary(&stream, dictionary.start, dictionary.length);
if (njs_slow_path(rc != Z_OK)) {
- njs_vm_error(vm, "deflateSetDictionary() failed");
+ njs_vm_error(vm, "inflateSetDictionary() failed");
}
if (rc == Z_NEED_DICT) {
}
if (rc == Z_NEED_DICT) {
- njs_vm_type_error(vm, "failed to inflate, dictionary is required");
- goto fail;
+ if (dictionary.start == NULL) {
+ njs_vm_type_error(vm,
+ "failed to inflate, dictionary is required");
+ goto fail;
+ }
+
+ rc = inflateSetDictionary(&stream, dictionary.start,
+ dictionary.length);
+ if (njs_slow_path(rc != Z_OK)) {
+ njs_vm_error(vm, "inflateSetDictionary() failed");
+ goto fail;
+ }
+
+ continue;
}
njs_chb_written(&chain, chunk_size - stream.avail_out);
}
njs_chb_written(&chain, chunk_size - stream.avail_out);
- if (dictionary_copy != NULL) {
+ if (raw && dictionary_copy != NULL) {
rc = inflateSetDictionary(&stream, dictionary_copy, dictionary_length);
if (rc != Z_OK) {
qjs_throw_error(ctx, "inflateSetDictionary() failed");
rc = inflateSetDictionary(&stream, dictionary_copy, dictionary_length);
if (rc != Z_OK) {
qjs_throw_error(ctx, "inflateSetDictionary() failed");
}
if (rc == Z_NEED_DICT) {
}
if (rc == Z_NEED_DICT) {
- JS_ThrowTypeError(ctx, "failed to inflate, dictionary is required");
- goto fail;
+ if (dictionary_copy == NULL) {
+ JS_ThrowTypeError(ctx,
+ "failed to inflate, dictionary is required");
+ goto fail;
+ }
+
+ rc = inflateSetDictionary(&stream, dictionary_copy,
+ dictionary_length);
+ if (njs_slow_path(rc != Z_OK)) {
+ qjs_throw_error(ctx, "inflateSetDictionary() failed");
+ goto fail;
+ }
+
+ continue;
}
njs_chb_written(&chain, chunk_size - stream.avail_out);
}
njs_chb_written(&chain, chunk_size - stream.avail_out);
{ value: zlib.deflateSync('WAKA WAKA WAKA', {dictionary: Buffer.from('WAKA')}),
raw: false,
exception: 'TypeError: failed to inflate, dictionary is required' },
{ value: zlib.deflateSync('WAKA WAKA WAKA', {dictionary: Buffer.from('WAKA')}),
raw: false,
exception: 'TypeError: failed to inflate, dictionary is required' },
+ { value: zlib.deflateSync('WAKA', {dictionary: Buffer.from('WAKA')}),
+ raw: false, options: {dictionary: Buffer.from('WAKA')},
+ expected: 'WAKA' },
+ { value: zlib.deflateSync('WAKA'), raw: false,
+ options: {dictionary: Buffer.from('unused')}, expected: 'WAKA' },