@@ -78,8 +78,6 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
7878 ZCtx (Environment* env, Local<Object> wrap, node_zlib_mode mode)
7979 : AsyncWrap(env, wrap, AsyncWrap::PROVIDER_ZLIB),
8080 ThreadPoolWork (env),
81- dictionary_(nullptr ),
82- dictionary_len_(0 ),
8381 err_(0 ),
8482 flush_(0 ),
8583 init_done_(false ),
@@ -126,10 +124,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
126124 CHECK (status == Z_OK || status == Z_DATA_ERROR);
127125 mode_ = NONE;
128126
129- if (dictionary_ != nullptr ) {
130- delete[] dictionary_;
131- dictionary_ = nullptr ;
132- }
127+ dictionary_.clear ();
133128 }
134129
135130
@@ -294,9 +289,11 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
294289 // SetDictionary, don't repeat that here)
295290 if (mode_ != INFLATERAW &&
296291 err_ == Z_NEED_DICT &&
297- dictionary_ != nullptr ) {
292+ !dictionary_. empty () ) {
298293 // Load it
299- err_ = inflateSetDictionary (&strm_, dictionary_, dictionary_len_);
294+ err_ = inflateSetDictionary (&strm_,
295+ dictionary_.data (),
296+ dictionary_.size ());
300297 if (err_ == Z_OK) {
301298 // And try to decode again
302299 err_ = inflate (&strm_, flush_);
@@ -346,7 +343,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
346343 // normal statuses, not fatal
347344 break ;
348345 case Z_NEED_DICT:
349- if (dictionary_ == nullptr )
346+ if (dictionary_. empty () )
350347 Error (" Missing dictionary" );
351348 else
352349 Error (" Bad dictionary" );
@@ -483,23 +480,20 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
483480
484481 Local<Function> write_js_callback = args[5 ].As <Function>();
485482
486- char * dictionary = nullptr ;
487- size_t dictionary_len = 0 ;
483+ std::vector<unsigned char > dictionary;
488484 if (Buffer::HasInstance (args[6 ])) {
489- const char * dictionary_ = Buffer::Data (args[ 6 ]);
490- dictionary_len = Buffer::Length (args[6 ]);
491-
492- dictionary = new char [dictionary_len];
493- memcpy (dictionary, dictionary_, dictionary_len );
485+ unsigned char * data =
486+ reinterpret_cast < unsigned char *>( Buffer::Data (args[6 ]) );
487+ dictionary = std::vector< unsigned char >(
488+ data,
489+ data + Buffer::Length (args[ 6 ]) );
494490 }
495491
496492 bool ret = Init (ctx, level, windowBits, memLevel, strategy, write_result,
497- write_js_callback, dictionary, dictionary_len);
498- if (!ret) goto end;
493+ write_js_callback, std::move (dictionary));
494+ if (ret)
495+ ctx->SetDictionary ();
499496
500- ctx->SetDictionary ();
501-
502- end:
503497 return args.GetReturnValue ().Set (ret);
504498 }
505499
@@ -524,8 +518,8 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
524518
525519 static bool Init (ZCtx* ctx, int level, int windowBits, int memLevel,
526520 int strategy, uint32_t * write_result,
527- Local<Function> write_js_callback, char * dictionary,
528- size_t dictionary_len ) {
521+ Local<Function> write_js_callback,
522+ std::vector< unsigned char >&& dictionary ) {
529523 AllocScope alloc_scope (ctx);
530524 ctx->level_ = level;
531525 ctx->windowBits_ = windowBits;
@@ -573,17 +567,13 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
573567 UNREACHABLE ();
574568 }
575569
576- ctx->dictionary_ = reinterpret_cast <Bytef *>(dictionary);
577- ctx->dictionary_len_ = dictionary_len;
570+ ctx->dictionary_ = std::move (dictionary);
578571
579572 ctx->write_in_progress_ = false ;
580573 ctx->init_done_ = true ;
581574
582575 if (ctx->err_ != Z_OK) {
583- if (dictionary != nullptr ) {
584- delete[] dictionary;
585- ctx->dictionary_ = nullptr ;
586- }
576+ ctx->dictionary_ .clear ();
587577 ctx->mode_ = NONE;
588578 return false ;
589579 }
@@ -594,20 +584,24 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
594584 }
595585
596586 void SetDictionary () {
597- if (dictionary_ == nullptr )
587+ if (dictionary_. empty () )
598588 return ;
599589
600590 err_ = Z_OK;
601591
602592 switch (mode_) {
603593 case DEFLATE:
604594 case DEFLATERAW:
605- err_ = deflateSetDictionary (&strm_, dictionary_, dictionary_len_);
595+ err_ = deflateSetDictionary (&strm_,
596+ dictionary_.data (),
597+ dictionary_.size ());
606598 break ;
607599 case INFLATERAW:
608600 // The other inflate cases will have the dictionary set when inflate()
609601 // returns Z_NEED_DICT in Process()
610- err_ = inflateSetDictionary (&strm_, dictionary_, dictionary_len_);
602+ err_ = inflateSetDictionary (&strm_,
603+ dictionary_.data (),
604+ dictionary_.size ());
611605 break ;
612606 default :
613607 break ;
@@ -664,7 +658,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
664658
665659 void MemoryInfo (MemoryTracker* tracker) const override {
666660 tracker->TrackThis (this );
667- tracker->TrackFieldWithSize (" dictionary" , dictionary_len_ );
661+ tracker->TrackField (" dictionary" , dictionary_ );
668662 tracker->TrackFieldWithSize (" zlib memory" ,
669663 zlib_memory_ + unreported_allocations_);
670664 }
@@ -732,8 +726,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
732726 ZCtx* ctx;
733727 };
734728
735- Bytef* dictionary_;
736- size_t dictionary_len_;
729+ std::vector<unsigned char > dictionary_;
737730 int err_;
738731 int flush_;
739732 bool init_done_;
0 commit comments