@@ -78,8 +78,6 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
78
78
ZCtx (Environment* env, Local<Object> wrap, node_zlib_mode mode)
79
79
: AsyncWrap(env, wrap, AsyncWrap::PROVIDER_ZLIB),
80
80
ThreadPoolWork (env),
81
- dictionary_(nullptr ),
82
- dictionary_len_(0 ),
83
81
err_(0 ),
84
82
flush_(0 ),
85
83
init_done_(false ),
@@ -126,10 +124,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
126
124
CHECK (status == Z_OK || status == Z_DATA_ERROR);
127
125
mode_ = NONE;
128
126
129
- if (dictionary_ != nullptr ) {
130
- delete[] dictionary_;
131
- dictionary_ = nullptr ;
132
- }
127
+ dictionary_.clear ();
133
128
}
134
129
135
130
@@ -294,9 +289,11 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
294
289
// SetDictionary, don't repeat that here)
295
290
if (mode_ != INFLATERAW &&
296
291
err_ == Z_NEED_DICT &&
297
- dictionary_ != nullptr ) {
292
+ !dictionary_. empty () ) {
298
293
// Load it
299
- err_ = inflateSetDictionary (&strm_, dictionary_, dictionary_len_);
294
+ err_ = inflateSetDictionary (&strm_,
295
+ dictionary_.data (),
296
+ dictionary_.size ());
300
297
if (err_ == Z_OK) {
301
298
// And try to decode again
302
299
err_ = inflate (&strm_, flush_);
@@ -346,7 +343,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
346
343
// normal statuses, not fatal
347
344
break ;
348
345
case Z_NEED_DICT:
349
- if (dictionary_ == nullptr )
346
+ if (dictionary_. empty () )
350
347
Error (" Missing dictionary" );
351
348
else
352
349
Error (" Bad dictionary" );
@@ -483,23 +480,20 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
483
480
484
481
Local<Function> write_js_callback = args[5 ].As <Function>();
485
482
486
- char * dictionary = nullptr ;
487
- size_t dictionary_len = 0 ;
483
+ std::vector<unsigned char > dictionary;
488
484
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 ]) );
494
490
}
495
491
496
492
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 ();
499
496
500
- ctx->SetDictionary ();
501
-
502
- end:
503
497
return args.GetReturnValue ().Set (ret);
504
498
}
505
499
@@ -524,8 +518,8 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
524
518
525
519
static bool Init (ZCtx* ctx, int level, int windowBits, int memLevel,
526
520
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 ) {
529
523
AllocScope alloc_scope (ctx);
530
524
ctx->level_ = level;
531
525
ctx->windowBits_ = windowBits;
@@ -573,17 +567,13 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
573
567
UNREACHABLE ();
574
568
}
575
569
576
- ctx->dictionary_ = reinterpret_cast <Bytef *>(dictionary);
577
- ctx->dictionary_len_ = dictionary_len;
570
+ ctx->dictionary_ = std::move (dictionary);
578
571
579
572
ctx->write_in_progress_ = false ;
580
573
ctx->init_done_ = true ;
581
574
582
575
if (ctx->err_ != Z_OK) {
583
- if (dictionary != nullptr ) {
584
- delete[] dictionary;
585
- ctx->dictionary_ = nullptr ;
586
- }
576
+ ctx->dictionary_ .clear ();
587
577
ctx->mode_ = NONE;
588
578
return false ;
589
579
}
@@ -594,20 +584,24 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
594
584
}
595
585
596
586
void SetDictionary () {
597
- if (dictionary_ == nullptr )
587
+ if (dictionary_. empty () )
598
588
return ;
599
589
600
590
err_ = Z_OK;
601
591
602
592
switch (mode_) {
603
593
case DEFLATE:
604
594
case DEFLATERAW:
605
- err_ = deflateSetDictionary (&strm_, dictionary_, dictionary_len_);
595
+ err_ = deflateSetDictionary (&strm_,
596
+ dictionary_.data (),
597
+ dictionary_.size ());
606
598
break ;
607
599
case INFLATERAW:
608
600
// The other inflate cases will have the dictionary set when inflate()
609
601
// returns Z_NEED_DICT in Process()
610
- err_ = inflateSetDictionary (&strm_, dictionary_, dictionary_len_);
602
+ err_ = inflateSetDictionary (&strm_,
603
+ dictionary_.data (),
604
+ dictionary_.size ());
611
605
break ;
612
606
default :
613
607
break ;
@@ -664,7 +658,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
664
658
665
659
void MemoryInfo (MemoryTracker* tracker) const override {
666
660
tracker->TrackThis (this );
667
- tracker->TrackFieldWithSize (" dictionary" , dictionary_len_ );
661
+ tracker->TrackField (" dictionary" , dictionary_ );
668
662
tracker->TrackFieldWithSize (" zlib memory" ,
669
663
zlib_memory_ + unreported_allocations_);
670
664
}
@@ -732,8 +726,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork {
732
726
ZCtx* ctx;
733
727
};
734
728
735
- Bytef* dictionary_;
736
- size_t dictionary_len_;
729
+ std::vector<unsigned char > dictionary_;
737
730
int err_;
738
731
int flush_;
739
732
bool init_done_;
0 commit comments