Skip to content

Instantly share code, notes, and snippets.

@jeffreydwalter
Created March 5, 2015 16:38
Show Gist options
  • Save jeffreydwalter/f2650c262b28acbaf48b to your computer and use it in GitHub Desktop.
Save jeffreydwalter/f2650c262b28acbaf48b to your computer and use it in GitHub Desktop.
diff --git a/lib/zlib.js b/lib/zlib.js
index f80c983..998f14f 100644
--- a/lib/zlib.js
+++ b/lib/zlib.js
@@ -297,7 +297,7 @@ function Zlib(opts, mode) {
Transform.call(this, opts);
- if (opts.flush) {
+ if (opts.hasOwnProperty('flush')) {
if (opts.flush !== binding.Z_NO_FLUSH &&
opts.flush !== binding.Z_PARTIAL_FLUSH &&
opts.flush !== binding.Z_SYNC_FLUSH &&
@@ -309,35 +309,43 @@ function Zlib(opts, mode) {
}
this._flushFlag = opts.flush || binding.Z_NO_FLUSH;
- if (opts.chunkSize) {
+ if (opts.hasOwnProperty('chunkSize')) {
if (opts.chunkSize < exports.Z_MIN_CHUNK ||
opts.chunkSize > exports.Z_MAX_CHUNK) {
throw new Error('Invalid chunk size: ' + opts.chunkSize);
}
}
- if (opts.windowBits) {
- if (opts.windowBits < exports.Z_MIN_WINDOWBITS ||
- opts.windowBits > exports.Z_MAX_WINDOWBITS) {
- throw new Error('Invalid windowBits: ' + opts.windowBits);
+ if (opts.hasOwnProperty('windowBits')) {
+ // In inflate mode, windowBits is allowed to be 0, which indicates that windowBits should be based on the sender's zlib header.
+ if (mode === binding.INFLATE || mode === binding.INFLATERAW || mode === binding.UNZIP || mode === binding.GUNZIP) {
+ if (opts.windowBits !== 0 && (opts.windowBits < exports.Z_MIN_WINDOWBITS || opts.windowBits > exports.Z_MAX_WINDOWBITS) {
+ throw new Error('Invalid windowBits: ' + opts.windowBits);
+ }
+ }
+ // In deflate mode, windowBits is not allowed to be 0.
+ else {
+ if (opts.windowBits < exports.Z_MIN_WINDOWBITS || opts.windowBits > exports.Z_MAX_WINDOWBITS) {
+ throw new Error('Invalid windowBits: ' + opts.windowBits);
+ }
}
}
- if (opts.level) {
+ if (opts.hasOwnProperty('level')) {
if (opts.level < exports.Z_MIN_LEVEL ||
opts.level > exports.Z_MAX_LEVEL) {
throw new Error('Invalid compression level: ' + opts.level);
}
}
- if (opts.memLevel) {
+ if (opts.hasOwnProperty('memLevel')) {
if (opts.memLevel < exports.Z_MIN_MEMLEVEL ||
opts.memLevel > exports.Z_MAX_MEMLEVEL) {
throw new Error('Invalid memLevel: ' + opts.memLevel);
}
}
- if (opts.strategy) {
+ if (opts.hasOwnProperty('strategy')) {
if (opts.strategy != exports.Z_FILTERED &&
opts.strategy != exports.Z_HUFFMAN_ONLY &&
opts.strategy != exports.Z_RLE &&
@@ -375,7 +383,19 @@ function Zlib(opts, mode) {
var strategy = exports.Z_DEFAULT_STRATEGY;
if (util.isNumber(opts.strategy)) strategy = opts.strategy;
- this._handle.init(opts.windowBits || exports.Z_DEFAULT_WINDOWBITS,
+ // In inflate mode, windowBits is allowed to be 0, which indicates that windowBits should be based on the sender's zlib header.
+ var windowBits = 0;
+ if (mode === binding.INFLATE || mode === binding.INFLATERAW || mode === binding.UNZIP || mode === binding.GUNZIP) {
+ if (opts.windowBits !== 0) {
+ windowBits = opts.windowBits || exports.Z_DEFAULT_WINDOWBITS;
+ }
+ }
+ // In deflate mode, windowBits is not allowed to be 0.
+ else {
+ windowBits = opts.windowBits || exports.Z_DEFAULT_WINDOWBITS;
+ }
+
+ this._handle.init(windowBits,
level,
opts.memLevel || exports.Z_DEFAULT_MEMLEVEL,
strategy,
diff --git a/src/node_zlib.cc b/src/node_zlib.cc
index 0904c88..81b97fa 100644
--- a/src/node_zlib.cc
+++ b/src/node_zlib.cc
@@ -397,7 +397,7 @@ class ZCtx : public AsyncWrap {
ZCtx* ctx = Unwrap<ZCtx>(args.Holder());
int windowBits = args[0]->Uint32Value();
- assert((windowBits >= 8 && windowBits <= 15) && "invalid windowBits");
+ assert((((ctx->mode_ == GUNZIP || ctx->mode_ == UNZIP || ctx->mode_ == INFLATE || ctx->mode_ == INFLATERAW) && windowBits === 0) || (windowBits >= 8 && windowBits <= 15)) && "invalid windowBits");
int level = args[1]->Int32Value();
assert((level >= -1 && level <= 9) && "invalid compression level");
@@ -423,8 +423,7 @@ class ZCtx : public AsyncWrap {
memcpy(dictionary, Buffer::Data(dictionary_), dictionary_len);
}
- Init(ctx, level, windowBits, memLevel, strategy,
- dictionary, dictionary_len);
+ Init(ctx, level, windowBits, memLevel, strategy, dictionary, dictionary_len);
SetDictionary(ctx);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment