|
@ -36,6 +36,7 @@ using namespace v8; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static Persistent<String> callback_sym; |
|
|
static Persistent<String> callback_sym; |
|
|
|
|
|
static Persistent<String> onerror_sym; |
|
|
|
|
|
|
|
|
enum node_zlib_mode { |
|
|
enum node_zlib_mode { |
|
|
DEFLATE = 1, |
|
|
DEFLATE = 1, |
|
@ -142,37 +143,41 @@ class ZCtx : public ObjectWrap { |
|
|
// If the avail_out is left at 0, then it means that it ran out
|
|
|
// If the avail_out is left at 0, then it means that it ran out
|
|
|
// of room. If there was avail_out left over, then it means
|
|
|
// of room. If there was avail_out left over, then it means
|
|
|
// that all of the input was consumed.
|
|
|
// that all of the input was consumed.
|
|
|
int err = Z_STREAM_ERROR; |
|
|
|
|
|
switch (ctx->mode_) { |
|
|
switch (ctx->mode_) { |
|
|
case DEFLATE: |
|
|
case DEFLATE: |
|
|
case GZIP: |
|
|
case GZIP: |
|
|
case DEFLATERAW: |
|
|
case DEFLATERAW: |
|
|
err = deflate(&ctx->strm_, ctx->flush_); |
|
|
ctx->err_ = deflate(&ctx->strm_, ctx->flush_); |
|
|
break; |
|
|
break; |
|
|
case UNZIP: |
|
|
case UNZIP: |
|
|
case INFLATE: |
|
|
case INFLATE: |
|
|
case GUNZIP: |
|
|
case GUNZIP: |
|
|
case INFLATERAW: |
|
|
case INFLATERAW: |
|
|
err = inflate(&ctx->strm_, ctx->flush_); |
|
|
ctx->err_ = inflate(&ctx->strm_, ctx->flush_); |
|
|
|
|
|
|
|
|
// If data was encoded with dictionary
|
|
|
// If data was encoded with dictionary
|
|
|
if (err == Z_NEED_DICT) { |
|
|
if (ctx->err_ == Z_NEED_DICT) { |
|
|
assert(ctx->dictionary_ != NULL && "Stream has no dictionary"); |
|
|
assert(ctx->dictionary_ != NULL && "Stream has no dictionary"); |
|
|
|
|
|
if (ctx->dictionary_ != NULL) { |
|
|
// Load it
|
|
|
|
|
|
err = inflateSetDictionary(&ctx->strm_, |
|
|
// Load it
|
|
|
ctx->dictionary_, |
|
|
ctx->err_ = inflateSetDictionary(&ctx->strm_, |
|
|
ctx->dictionary_len_); |
|
|
ctx->dictionary_, |
|
|
assert(err == Z_OK && "Failed to set dictionary"); |
|
|
ctx->dictionary_len_); |
|
|
|
|
|
assert(ctx->err_ == Z_OK && "Failed to set dictionary"); |
|
|
// And try to decode again
|
|
|
if (ctx->err_ == Z_OK) { |
|
|
err = inflate(&ctx->strm_, ctx->flush_); |
|
|
|
|
|
|
|
|
// And try to decode again
|
|
|
|
|
|
ctx->err_ = inflate(&ctx->strm_, ctx->flush_); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
break; |
|
|
break; |
|
|
default: |
|
|
default: |
|
|
assert(0 && "wtf?"); |
|
|
assert(0 && "wtf?"); |
|
|
} |
|
|
} |
|
|
assert(err != Z_STREAM_ERROR); |
|
|
|
|
|
|
|
|
// pass any errors back to the main thread to deal with.
|
|
|
|
|
|
|
|
|
// now After will emit the output, and
|
|
|
// now After will emit the output, and
|
|
|
// either schedule another call to Process,
|
|
|
// either schedule another call to Process,
|
|
@ -184,6 +189,19 @@ class ZCtx : public ObjectWrap { |
|
|
HandleScope scope; |
|
|
HandleScope scope; |
|
|
ZCtx *ctx = container_of(work_req, ZCtx, work_req_); |
|
|
ZCtx *ctx = container_of(work_req, ZCtx, work_req_); |
|
|
|
|
|
|
|
|
|
|
|
// Acceptable error states depend on the type of zlib stream.
|
|
|
|
|
|
switch (ctx->err_) { |
|
|
|
|
|
case Z_OK: |
|
|
|
|
|
case Z_STREAM_END: |
|
|
|
|
|
case Z_BUF_ERROR: |
|
|
|
|
|
// normal statuses, not fatal
|
|
|
|
|
|
break; |
|
|
|
|
|
default: |
|
|
|
|
|
// something else.
|
|
|
|
|
|
ZCtx::Error(ctx, "Zlib error"); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
Local<Integer> avail_out = Integer::New(ctx->strm_.avail_out); |
|
|
Local<Integer> avail_out = Integer::New(ctx->strm_.avail_out); |
|
|
Local<Integer> avail_in = Integer::New(ctx->strm_.avail_in); |
|
|
Local<Integer> avail_in = Integer::New(ctx->strm_.avail_in); |
|
|
|
|
|
|
|
@ -198,6 +216,25 @@ class ZCtx : public ObjectWrap { |
|
|
ctx->Unref(); |
|
|
ctx->Unref(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void Error(ZCtx *ctx, const char *msg_) { |
|
|
|
|
|
const char *msg; |
|
|
|
|
|
if (ctx->strm_.msg != NULL) { |
|
|
|
|
|
msg = ctx->strm_.msg; |
|
|
|
|
|
} else { |
|
|
|
|
|
msg = msg_; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
assert(ctx->handle_->Get(onerror_sym)->IsFunction() && |
|
|
|
|
|
"Invalid error handler"); |
|
|
|
|
|
HandleScope scope; |
|
|
|
|
|
Local<Value> args[2] = { String::New(msg), |
|
|
|
|
|
Local<Value>::New(Number::New(ctx->err_)) }; |
|
|
|
|
|
MakeCallback(ctx->handle_, "onerror", ARRAY_SIZE(args), args); |
|
|
|
|
|
|
|
|
|
|
|
// no hope of rescue.
|
|
|
|
|
|
ctx->Unref(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
static Handle<Value> New(const Arguments& args) { |
|
|
static Handle<Value> New(const Arguments& args) { |
|
|
HandleScope scope; |
|
|
HandleScope scope; |
|
|
if (args.Length() < 1 || !args[0]->IsInt32()) { |
|
|
if (args.Length() < 1 || !args[0]->IsInt32()) { |
|
@ -279,6 +316,8 @@ class ZCtx : public ObjectWrap { |
|
|
|
|
|
|
|
|
ctx->flush_ = Z_NO_FLUSH; |
|
|
ctx->flush_ = Z_NO_FLUSH; |
|
|
|
|
|
|
|
|
|
|
|
ctx->err_ = Z_OK; |
|
|
|
|
|
|
|
|
if (ctx->mode_ == GZIP || ctx->mode_ == GUNZIP) { |
|
|
if (ctx->mode_ == GZIP || ctx->mode_ == GUNZIP) { |
|
|
ctx->windowBits_ += 16; |
|
|
ctx->windowBits_ += 16; |
|
|
} |
|
|
} |
|
@ -291,29 +330,31 @@ class ZCtx : public ObjectWrap { |
|
|
ctx->windowBits_ *= -1; |
|
|
ctx->windowBits_ *= -1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int err; |
|
|
|
|
|
switch (ctx->mode_) { |
|
|
switch (ctx->mode_) { |
|
|
case DEFLATE: |
|
|
case DEFLATE: |
|
|
case GZIP: |
|
|
case GZIP: |
|
|
case DEFLATERAW: |
|
|
case DEFLATERAW: |
|
|
err = deflateInit2(&ctx->strm_, |
|
|
ctx->err_ = deflateInit2(&ctx->strm_, |
|
|
ctx->level_, |
|
|
ctx->level_, |
|
|
Z_DEFLATED, |
|
|
Z_DEFLATED, |
|
|
ctx->windowBits_, |
|
|
ctx->windowBits_, |
|
|
ctx->memLevel_, |
|
|
ctx->memLevel_, |
|
|
ctx->strategy_); |
|
|
ctx->strategy_); |
|
|
break; |
|
|
break; |
|
|
case INFLATE: |
|
|
case INFLATE: |
|
|
case GUNZIP: |
|
|
case GUNZIP: |
|
|
case INFLATERAW: |
|
|
case INFLATERAW: |
|
|
case UNZIP: |
|
|
case UNZIP: |
|
|
err = inflateInit2(&ctx->strm_, ctx->windowBits_); |
|
|
ctx->err_ = inflateInit2(&ctx->strm_, ctx->windowBits_); |
|
|
break; |
|
|
break; |
|
|
default: |
|
|
default: |
|
|
assert(0 && "wtf?"); |
|
|
assert(0 && "wtf?"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
assert(err == Z_OK); |
|
|
if (ctx->err_ != Z_OK) { |
|
|
|
|
|
ZCtx::Error(ctx, "Init error"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ctx->dictionary_ = reinterpret_cast<Bytef *>(dictionary); |
|
|
ctx->dictionary_ = reinterpret_cast<Bytef *>(dictionary); |
|
|
ctx->dictionary_len_ = dictionary_len; |
|
|
ctx->dictionary_len_ = dictionary_len; |
|
@ -325,39 +366,43 @@ class ZCtx : public ObjectWrap { |
|
|
static void SetDictionary(ZCtx* ctx) { |
|
|
static void SetDictionary(ZCtx* ctx) { |
|
|
if (ctx->dictionary_ == NULL) return; |
|
|
if (ctx->dictionary_ == NULL) return; |
|
|
|
|
|
|
|
|
int err = Z_OK; |
|
|
ctx->err_ = Z_OK; |
|
|
|
|
|
|
|
|
switch (ctx->mode_) { |
|
|
switch (ctx->mode_) { |
|
|
case DEFLATE: |
|
|
case DEFLATE: |
|
|
case DEFLATERAW: |
|
|
case DEFLATERAW: |
|
|
err = deflateSetDictionary(&ctx->strm_, |
|
|
ctx->err_ = deflateSetDictionary(&ctx->strm_, |
|
|
ctx->dictionary_, |
|
|
ctx->dictionary_, |
|
|
ctx->dictionary_len_); |
|
|
ctx->dictionary_len_); |
|
|
break; |
|
|
break; |
|
|
default: |
|
|
default: |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
assert(err == Z_OK && "Failed to set dictionary"); |
|
|
if (ctx->err_ != Z_OK) { |
|
|
|
|
|
ZCtx::Error(ctx, "Failed to set dictionary"); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static void Reset(ZCtx* ctx) { |
|
|
static void Reset(ZCtx* ctx) { |
|
|
int err = Z_OK; |
|
|
ctx->err_ = Z_OK; |
|
|
|
|
|
|
|
|
switch (ctx->mode_) { |
|
|
switch (ctx->mode_) { |
|
|
case DEFLATE: |
|
|
case DEFLATE: |
|
|
case DEFLATERAW: |
|
|
case DEFLATERAW: |
|
|
err = deflateReset(&ctx->strm_); |
|
|
ctx->err_ = deflateReset(&ctx->strm_); |
|
|
break; |
|
|
break; |
|
|
case INFLATE: |
|
|
case INFLATE: |
|
|
case INFLATERAW: |
|
|
case INFLATERAW: |
|
|
err = inflateReset(&ctx->strm_); |
|
|
ctx->err_ = inflateReset(&ctx->strm_); |
|
|
break; |
|
|
break; |
|
|
default: |
|
|
default: |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
assert(err == Z_OK && "Failed to reset stream"); |
|
|
if (ctx->err_ != Z_OK) { |
|
|
|
|
|
ZCtx::Error(ctx, "Failed to reset stream"); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private: |
|
|
private: |
|
@ -370,6 +415,8 @@ class ZCtx : public ObjectWrap { |
|
|
int memLevel_; |
|
|
int memLevel_; |
|
|
int strategy_; |
|
|
int strategy_; |
|
|
|
|
|
|
|
|
|
|
|
int err_; |
|
|
|
|
|
|
|
|
Bytef* dictionary_; |
|
|
Bytef* dictionary_; |
|
|
size_t dictionary_len_; |
|
|
size_t dictionary_len_; |
|
|
|
|
|
|
|
@ -399,6 +446,7 @@ void InitZlib(Handle<Object> target) { |
|
|
target->Set(String::NewSymbol("Zlib"), z->GetFunction()); |
|
|
target->Set(String::NewSymbol("Zlib"), z->GetFunction()); |
|
|
|
|
|
|
|
|
callback_sym = NODE_PSYMBOL("callback"); |
|
|
callback_sym = NODE_PSYMBOL("callback"); |
|
|
|
|
|
onerror_sym = NODE_PSYMBOL("onerror"); |
|
|
|
|
|
|
|
|
NODE_DEFINE_CONSTANT(target, Z_NO_FLUSH); |
|
|
NODE_DEFINE_CONSTANT(target, Z_NO_FLUSH); |
|
|
NODE_DEFINE_CONSTANT(target, Z_PARTIAL_FLUSH); |
|
|
NODE_DEFINE_CONSTANT(target, Z_PARTIAL_FLUSH); |
|
@ -406,6 +454,8 @@ void InitZlib(Handle<Object> target) { |
|
|
NODE_DEFINE_CONSTANT(target, Z_FULL_FLUSH); |
|
|
NODE_DEFINE_CONSTANT(target, Z_FULL_FLUSH); |
|
|
NODE_DEFINE_CONSTANT(target, Z_FINISH); |
|
|
NODE_DEFINE_CONSTANT(target, Z_FINISH); |
|
|
NODE_DEFINE_CONSTANT(target, Z_BLOCK); |
|
|
NODE_DEFINE_CONSTANT(target, Z_BLOCK); |
|
|
|
|
|
|
|
|
|
|
|
// return/error codes
|
|
|
NODE_DEFINE_CONSTANT(target, Z_OK); |
|
|
NODE_DEFINE_CONSTANT(target, Z_OK); |
|
|
NODE_DEFINE_CONSTANT(target, Z_STREAM_END); |
|
|
NODE_DEFINE_CONSTANT(target, Z_STREAM_END); |
|
|
NODE_DEFINE_CONSTANT(target, Z_NEED_DICT); |
|
|
NODE_DEFINE_CONSTANT(target, Z_NEED_DICT); |
|
@ -415,6 +465,7 @@ void InitZlib(Handle<Object> target) { |
|
|
NODE_DEFINE_CONSTANT(target, Z_MEM_ERROR); |
|
|
NODE_DEFINE_CONSTANT(target, Z_MEM_ERROR); |
|
|
NODE_DEFINE_CONSTANT(target, Z_BUF_ERROR); |
|
|
NODE_DEFINE_CONSTANT(target, Z_BUF_ERROR); |
|
|
NODE_DEFINE_CONSTANT(target, Z_VERSION_ERROR); |
|
|
NODE_DEFINE_CONSTANT(target, Z_VERSION_ERROR); |
|
|
|
|
|
|
|
|
NODE_DEFINE_CONSTANT(target, Z_NO_COMPRESSION); |
|
|
NODE_DEFINE_CONSTANT(target, Z_NO_COMPRESSION); |
|
|
NODE_DEFINE_CONSTANT(target, Z_BEST_SPEED); |
|
|
NODE_DEFINE_CONSTANT(target, Z_BEST_SPEED); |
|
|
NODE_DEFINE_CONSTANT(target, Z_BEST_COMPRESSION); |
|
|
NODE_DEFINE_CONSTANT(target, Z_BEST_COMPRESSION); |
|
|