Skip to content

Commit

Permalink
Merge pull request #2 from jeffreydwalter/v0.12.0-release
Browse files Browse the repository at this point in the history
Adding support for new zlib feature which allows setting windowBits = 0.
  • Loading branch information
jeffreydwalter committed Mar 23, 2015
2 parents a995a6a + 10a2c74 commit d693dd1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
40 changes: 30 additions & 10 deletions lib/zlib.js
Expand Up @@ -299,7 +299,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 &&
Expand All @@ -311,35 +311,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 &&
Expand Down Expand Up @@ -377,7 +385,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,
Expand Down
5 changes: 2 additions & 3 deletions src/node_zlib.cc
Expand Up @@ -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");
Expand All @@ -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);
}

Expand Down

0 comments on commit d693dd1

Please sign in to comment.