-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
zlib: add support for concatenated members
According to the spec gzipped archives can contain more than one compressed member. Previously Node's gzip implementation would only unzip the first member and throw away the rest of the compressed data. Issue #4306 is an example of this occurring in daily use. Fixes: #4306 PR-URL: #5120 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
1 parent
b16f67a
commit ef774ff
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
// Test unzipping a gzip file that contains multiple concatenated "members" | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const zlib = require('zlib'); | ||
|
||
const data = Buffer.concat([ | ||
zlib.gzipSync('abc'), | ||
zlib.gzipSync('def') | ||
]); | ||
|
||
assert.equal(zlib.gunzipSync(data).toString(), 'abcdef'); | ||
|
||
zlib.gunzip(data, common.mustCall((err, result) => { | ||
assert.ifError(err); | ||
assert.equal(result, 'abcdef', 'result should match original string'); | ||
})); |
50 changes: 50 additions & 0 deletions
50
test/parallel/test-zlib-from-gzip-with-trailing-garbage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
// test unzipping a gzip file that has trailing garbage | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const zlib = require('zlib'); | ||
|
||
// should ignore trailing null-bytes | ||
let data = Buffer.concat([ | ||
zlib.gzipSync('abc'), | ||
zlib.gzipSync('def'), | ||
Buffer(10).fill(0) | ||
]); | ||
|
||
assert.equal(zlib.gunzipSync(data).toString(), 'abcdef'); | ||
|
||
zlib.gunzip(data, common.mustCall((err, result) => { | ||
assert.ifError(err); | ||
assert.equal(result, 'abcdef', 'result should match original string'); | ||
})); | ||
|
||
// if the trailing garbage happens to look like a gzip header, it should | ||
// throw an error. | ||
data = Buffer.concat([ | ||
zlib.gzipSync('abc'), | ||
zlib.gzipSync('def'), | ||
Buffer([0x1f, 0x8b, 0xff, 0xff]), | ||
Buffer(10).fill(0) | ||
]); | ||
|
||
assert.throws(() => zlib.gunzipSync(data)); | ||
|
||
zlib.gunzip(data, common.mustCall((err, result) => { | ||
assert(err); | ||
})); | ||
|
||
// In this case the trailing junk is too short to be a gzip segment | ||
// So we ignore it and decompression succeeds. | ||
data = Buffer.concat([ | ||
zlib.gzipSync('abc'), | ||
zlib.gzipSync('def'), | ||
Buffer([0x1f, 0x8b, 0xff, 0xff]) | ||
]); | ||
|
||
assert.equal(zlib.gunzipSync(data).toString(), 'abcdef'); | ||
|
||
zlib.gunzip(data, common.mustCall((err, result) => { | ||
assert.ifError(err); | ||
assert.equal(result, 'abcdef', 'result should match original string'); | ||
})); |