@@ -249,6 +249,17 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
249249
250250 Init (ctx, level, windowBits, memLevel, strategy,
251251 dictionary, dictionary_len);
252+ SetDictionary (ctx);
253+ return Undefined ();
254+ }
255+
256+ static Handle<Value> Reset (const Arguments &args) {
257+ HandleScope scope;
258+
259+ ZCtx<mode> *ctx = ObjectWrap::Unwrap< ZCtx<mode> >(args.This ());
260+
261+ Reset (ctx);
262+ SetDictionary (ctx);
252263 return Undefined ();
253264 }
254265
@@ -304,23 +315,46 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
304315 ctx->dictionary_ = reinterpret_cast <Bytef *>(dictionary);
305316 ctx->dictionary_len_ = dictionary_len;
306317
307- if (dictionary != NULL ) {
308- switch (mode) {
309- case DEFLATE:
310- case DEFLATERAW:
311- err = deflateSetDictionary (&ctx->strm_ ,
312- ctx->dictionary_ ,
313- dictionary_len);
314- break ;
315- default :
316- break ;
317- }
318+ ctx->write_in_progress_ = false ;
319+ ctx->init_done_ = true ;
320+ }
321+
322+ static void SetDictionary (ZCtx* ctx) {
323+ if (ctx->dictionary_ == NULL ) return ;
318324
319- assert (err == Z_OK && " Failed to set dictionary" );
325+ int err;
326+
327+ switch (mode) {
328+ case DEFLATE:
329+ case DEFLATERAW:
330+ err = deflateSetDictionary (&ctx->strm_ ,
331+ ctx->dictionary_ ,
332+ ctx->dictionary_len_ );
333+ break ;
334+ default :
335+ break ;
320336 }
321337
322- ctx->write_in_progress_ = false ;
323- ctx->init_done_ = true ;
338+ assert (err == Z_OK && " Failed to set dictionary" );
339+ }
340+
341+ static void Reset (ZCtx* ctx) {
342+ int err;
343+
344+ switch (mode) {
345+ case DEFLATE:
346+ case DEFLATERAW:
347+ err = deflateReset (&ctx->strm_ );
348+ break ;
349+ case INFLATE:
350+ case INFLATERAW:
351+ err = inflateReset (&ctx->strm_ );
352+ break ;
353+ default :
354+ break ;
355+ }
356+
357+ assert (err == Z_OK && " Failed to reset stream" );
324358 }
325359
326360 private:
@@ -352,6 +386,7 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
352386 z->InstanceTemplate ()->SetInternalFieldCount (1 ); \
353387 NODE_SET_PROTOTYPE_METHOD (z, " write" , ZCtx<mode>::Write); \
354388 NODE_SET_PROTOTYPE_METHOD (z, " init" , ZCtx<mode>::Init); \
389+ NODE_SET_PROTOTYPE_METHOD (z, " reset" , ZCtx<mode>::Reset); \
355390 z->SetClassName (String::NewSymbol (name)); \
356391 target->Set (String::NewSymbol (name), z->GetFunction ()); \
357392 }
0 commit comments