Permalink
Browse files

zlib: feature reset

* c++ style fixes
  • Loading branch information...
1 parent 0aa628d commit c16fa508e67f40b84bc7dd10bc7323dce31ffa89 @indutny committed Jan 10, 2012
Showing with 57 additions and 31 deletions.
  1. +4 −0 lib/zlib.js
  2. +53 −31 src/node_zlib.cc
View
@@ -309,6 +309,10 @@ Zlib.prototype.write = function write(chunk, cb) {
return empty;
};
+Zlib.prototype.reset = function reset() {
+ return this._binding.reset();
+};
+
Zlib.prototype.flush = function flush(cb) {
this._flush = binding.Z_SYNC_FLUSH;
return this.write(cb);
View
@@ -77,8 +77,7 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
}
// write(flush, in, in_off, in_len, out, out_off, out_len)
- static Handle<Value>
- Write(const Arguments& args) {
+ static Handle<Value> Write(const Arguments& args) {
HandleScope scope;
assert(args.Length() == 7);
@@ -155,28 +154,26 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
case DEFLATE:
case GZIP:
case DEFLATERAW:
- err = deflate(&(ctx->strm_), ctx->flush_);
+ err = deflate(&ctx->strm_, ctx->flush_);
break;
case UNZIP:
case INFLATE:
case GUNZIP:
case INFLATERAW:
- err = inflate(&(ctx->strm_), ctx->flush_);
+ err = inflate(&ctx->strm_, ctx->flush_);
// If data was encoded with dictionary
if (err == Z_NEED_DICT) {
assert(ctx->dictionary_ != NULL && "Stream has no dictionary");
// Load it
- err = inflateSetDictionary(
- &(ctx->strm_),
- ctx->dictionary_,
- ctx->dictionary_len_
- );
+ err = inflateSetDictionary(&ctx->strm_,
+ ctx->dictionary_,
+ ctx->dictionary_len_);
assert(err == Z_OK && "Failed to set dictionary");
// And try to decode again
- err = inflate(&(ctx->strm_), ctx->flush_);
+ err = inflate(&ctx->strm_, ctx->flush_);
}
break;
default:
@@ -190,8 +187,7 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
}
// v8 land!
- static void
- After(uv_work_t* work_req) {
+ static void After(uv_work_t* work_req) {
HandleScope scope;
ZCtx<mode> *ctx = container_of(work_req, ZCtx<mode>, work_req_);
Local<Integer> avail_out = Integer::New(ctx->strm_.avail_out);
@@ -208,17 +204,15 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
ctx->Unref();
}
- static Handle<Value>
- New(const Arguments& args) {
+ static Handle<Value> New(const Arguments& args) {
HandleScope scope;
ZCtx<mode> *ctx = new ZCtx<mode>();
ctx->Wrap(args.This());
return args.This();
}
// just pull the ints out of the args and call the other Init
- static Handle<Value>
- Init(const Arguments& args) {
+ static Handle<Value> Init(const Arguments& args) {
HandleScope scope;
assert((args.Length() == 4 || args.Length() == 5) &&
@@ -255,17 +249,22 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
Init(ctx, level, windowBits, memLevel, strategy,
dictionary, dictionary_len);
+ SetDictionary(ctx);
+ return Undefined();
+ }
+
+ static Handle<Value> Reset(const Arguments &args) {
+ HandleScope scope;
+
+ ZCtx<mode> *ctx = ObjectWrap::Unwrap< ZCtx<mode> >(args.This());
+
+ Reset(ctx);
+ SetDictionary(ctx);
return Undefined();
}
- static void
- Init(ZCtx *ctx,
- int level,
- int windowBits,
- int memLevel,
- int strategy,
- char* dictionary,
- size_t dictionary_len) {
+ static void Init(ZCtx *ctx, int level, int windowBits, int memLevel,
+ int strategy, char* dictionary, size_t dictionary_len) {
ctx->level_ = level;
ctx->windowBits_ = windowBits;
ctx->memLevel_ = memLevel;
@@ -294,7 +293,7 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
case DEFLATE:
case GZIP:
case DEFLATERAW:
- err = deflateInit2(&(ctx->strm_),
+ err = deflateInit2(&ctx->strm_,
ctx->level_,
Z_DEFLATED,
ctx->windowBits_,
@@ -305,7 +304,7 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
case GUNZIP:
case INFLATERAW:
case UNZIP:
- err = inflateInit2(&(ctx->strm_), ctx->windowBits_);
+ err = inflateInit2(&ctx->strm_, ctx->windowBits_);
break;
default:
assert(0 && "wtf?");
@@ -316,24 +315,46 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
ctx->dictionary_ = reinterpret_cast<Bytef *>(dictionary);
ctx->dictionary_len_ = dictionary_len;
- if (dictionary != NULL) {
+ ctx->init_done_ = true;
+ }
+
+ static void SetDictionary(ZCtx* ctx) {
+ int err;
+
+ if (ctx->dictionary_ != NULL) {
switch (mode) {
case DEFLATE:
case DEFLATERAW:
err = deflateSetDictionary(
- &(ctx->strm_),
+ &ctx->strm_,
ctx->dictionary_,
- dictionary_len
- );
+ ctx->dictionary_len_);
break;
default:
break;
}
assert(err == Z_OK && "Failed to set dictionary");
}
+ }
+
+ static void Reset(ZCtx* ctx) {
+ int err;
+
+ switch (mode) {
+ case DEFLATE:
+ case DEFLATERAW:
+ err = deflateReset(&ctx->strm_);
+ break;
+ case INFLATE:
+ case INFLATERAW:
+ err = inflateReset(&ctx->strm_);
+ break;
+ default:
+ break;
+ }
- ctx->init_done_ = true;
+ assert(err == Z_OK && "Failed to reset stream");
}
private:
@@ -365,6 +386,7 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
z->InstanceTemplate()->SetInternalFieldCount(1); \
NODE_SET_PROTOTYPE_METHOD(z, "write", ZCtx<mode>::Write); \
NODE_SET_PROTOTYPE_METHOD(z, "init", ZCtx<mode>::Init); \
+ NODE_SET_PROTOTYPE_METHOD(z, "reset", ZCtx<mode>::Reset); \
z->SetClassName(String::NewSymbol(name)); \
target->Set(String::NewSymbol(name), z->GetFunction()); \
}

0 comments on commit c16fa50

Please sign in to comment.