From c9e6d96a84252e3c156c64ecabad6cce39c57355 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 22 Sep 2018 12:43:12 +0200 Subject: [PATCH 1/3] src: refactor zlib dictionary to STL vector --- src/node_zlib.cc | 63 +++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 35 deletions(-) diff --git a/src/node_zlib.cc b/src/node_zlib.cc index 3d7c6b004797f2..ec3b6dd1239af6 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -78,8 +78,6 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { ZCtx(Environment* env, Local wrap, node_zlib_mode mode) : AsyncWrap(env, wrap, AsyncWrap::PROVIDER_ZLIB), ThreadPoolWork(env), - dictionary_(nullptr), - dictionary_len_(0), err_(0), flush_(0), init_done_(false), @@ -126,10 +124,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { CHECK(status == Z_OK || status == Z_DATA_ERROR); mode_ = NONE; - if (dictionary_ != nullptr) { - delete[] dictionary_; - dictionary_ = nullptr; - } + dictionary_.clear(); } @@ -294,9 +289,11 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { // SetDictionary, don't repeat that here) if (mode_ != INFLATERAW && err_ == Z_NEED_DICT && - dictionary_ != nullptr) { + !dictionary_.empty()) { // Load it - err_ = inflateSetDictionary(&strm_, dictionary_, dictionary_len_); + err_ = inflateSetDictionary(&strm_, + dictionary_.data(), + dictionary_.size()); if (err_ == Z_OK) { // And try to decode again err_ = inflate(&strm_, flush_); @@ -346,7 +343,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { // normal statuses, not fatal break; case Z_NEED_DICT: - if (dictionary_ == nullptr) + if (dictionary_.empty()) Error("Missing dictionary"); else Error("Bad dictionary"); @@ -483,23 +480,20 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { Local write_js_callback = args[5].As(); - char* dictionary = nullptr; - size_t dictionary_len = 0; + std::vector dictionary; if (Buffer::HasInstance(args[6])) { - const char* dictionary_ = Buffer::Data(args[6]); - dictionary_len = Buffer::Length(args[6]); - - dictionary = new char[dictionary_len]; - memcpy(dictionary, dictionary_, dictionary_len); + unsigned char* data = + reinterpret_cast(Buffer::Data(args[6])); + dictionary = std::vector( + data, + data + Buffer::Length(args[6])); } bool ret = Init(ctx, level, windowBits, memLevel, strategy, write_result, - write_js_callback, dictionary, dictionary_len); - if (!ret) goto end; + write_js_callback, std::move(dictionary)); + if (ret) + ctx->SetDictionary(); - ctx->SetDictionary(); - - end: return args.GetReturnValue().Set(ret); } @@ -524,8 +518,8 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { static bool Init(ZCtx* ctx, int level, int windowBits, int memLevel, int strategy, uint32_t* write_result, - Local write_js_callback, char* dictionary, - size_t dictionary_len) { + Local write_js_callback, + std::vector&& dictionary) { AllocScope alloc_scope(ctx); ctx->level_ = level; ctx->windowBits_ = windowBits; @@ -573,17 +567,13 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { UNREACHABLE(); } - ctx->dictionary_ = reinterpret_cast(dictionary); - ctx->dictionary_len_ = dictionary_len; + ctx->dictionary_ = std::move(dictionary); ctx->write_in_progress_ = false; ctx->init_done_ = true; if (ctx->err_ != Z_OK) { - if (dictionary != nullptr) { - delete[] dictionary; - ctx->dictionary_ = nullptr; - } + ctx->dictionary_.clear(); ctx->mode_ = NONE; return false; } @@ -594,7 +584,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { } void SetDictionary() { - if (dictionary_ == nullptr) + if (dictionary_.empty()) return; err_ = Z_OK; @@ -602,12 +592,16 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { switch (mode_) { case DEFLATE: case DEFLATERAW: - err_ = deflateSetDictionary(&strm_, dictionary_, dictionary_len_); + err_ = deflateSetDictionary(&strm_, + dictionary_.data(), + dictionary_.size()); break; case INFLATERAW: // The other inflate cases will have the dictionary set when inflate() // returns Z_NEED_DICT in Process() - err_ = inflateSetDictionary(&strm_, dictionary_, dictionary_len_); + err_ = inflateSetDictionary(&strm_, + dictionary_.data(), + dictionary_.size()); break; default: break; @@ -664,7 +658,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { void MemoryInfo(MemoryTracker* tracker) const override { tracker->TrackThis(this); - tracker->TrackFieldWithSize("dictionary", dictionary_len_); + tracker->TrackField("dictionary", dictionary_); tracker->TrackFieldWithSize("zlib memory", zlib_memory_ + unreported_allocations_); } @@ -732,8 +726,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { ZCtx* ctx; }; - Bytef* dictionary_; - size_t dictionary_len_; + std::vector dictionary_; int err_; int flush_; bool init_done_; From 94d29bb8c0d3f1daf28ee524b780948055cf8c67 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 22 Sep 2018 12:48:06 +0200 Subject: [PATCH 2/3] src: make `ZCtx::Init()` non-static --- src/node_zlib.cc | 76 ++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/src/node_zlib.cc b/src/node_zlib.cc index ec3b6dd1239af6..c4a0b98f777d90 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -489,8 +489,8 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { data + Buffer::Length(args[6])); } - bool ret = Init(ctx, level, windowBits, memLevel, strategy, write_result, - write_js_callback, std::move(dictionary)); + bool ret = ctx->Init(level, windowBits, memLevel, strategy, write_result, + write_js_callback, std::move(dictionary)); if (ret) ctx->SetDictionary(); @@ -516,70 +516,70 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { ctx->SetDictionary(); } - static bool Init(ZCtx* ctx, int level, int windowBits, int memLevel, - int strategy, uint32_t* write_result, - Local write_js_callback, - std::vector&& dictionary) { - AllocScope alloc_scope(ctx); - ctx->level_ = level; - ctx->windowBits_ = windowBits; - ctx->memLevel_ = memLevel; - ctx->strategy_ = strategy; + bool Init(int level, int windowBits, int memLevel, + int strategy, uint32_t* write_result, + Local write_js_callback, + std::vector&& dictionary) { + AllocScope alloc_scope(this); + level_ = level; + windowBits_ = windowBits; + memLevel_ = memLevel; + strategy_ = strategy; - ctx->strm_.zalloc = AllocForZlib; - ctx->strm_.zfree = FreeForZlib; - ctx->strm_.opaque = static_cast(ctx); + strm_.zalloc = AllocForZlib; + strm_.zfree = FreeForZlib; + strm_.opaque = static_cast(this); - ctx->flush_ = Z_NO_FLUSH; + flush_ = Z_NO_FLUSH; - ctx->err_ = Z_OK; + err_ = Z_OK; - if (ctx->mode_ == GZIP || ctx->mode_ == GUNZIP) { - ctx->windowBits_ += 16; + if (mode_ == GZIP || mode_ == GUNZIP) { + windowBits_ += 16; } - if (ctx->mode_ == UNZIP) { - ctx->windowBits_ += 32; + if (mode_ == UNZIP) { + windowBits_ += 32; } - if (ctx->mode_ == DEFLATERAW || ctx->mode_ == INFLATERAW) { - ctx->windowBits_ *= -1; + if (mode_ == DEFLATERAW || mode_ == INFLATERAW) { + windowBits_ *= -1; } - switch (ctx->mode_) { + switch (mode_) { case DEFLATE: case GZIP: case DEFLATERAW: - ctx->err_ = deflateInit2(&ctx->strm_, - ctx->level_, - Z_DEFLATED, - ctx->windowBits_, - ctx->memLevel_, - ctx->strategy_); + err_ = deflateInit2(&strm_, + level_, + Z_DEFLATED, + windowBits_, + memLevel_, + strategy_); break; case INFLATE: case GUNZIP: case INFLATERAW: case UNZIP: - ctx->err_ = inflateInit2(&ctx->strm_, ctx->windowBits_); + err_ = inflateInit2(&strm_, windowBits_); break; default: UNREACHABLE(); } - ctx->dictionary_ = std::move(dictionary); + dictionary_ = std::move(dictionary); - ctx->write_in_progress_ = false; - ctx->init_done_ = true; + write_in_progress_ = false; + init_done_ = true; - if (ctx->err_ != Z_OK) { - ctx->dictionary_.clear(); - ctx->mode_ = NONE; + if (err_ != Z_OK) { + dictionary_.clear(); + mode_ = NONE; return false; } - ctx->write_result_ = write_result; - ctx->write_js_callback_.Reset(ctx->env()->isolate(), write_js_callback); + write_result_ = write_result; + write_js_callback_.Reset(env()->isolate(), write_js_callback); return true; } From 64830cf5c9343f2f6b8074c09305fdd7108701d1 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 22 Sep 2018 13:59:40 +0200 Subject: [PATCH 3/3] src: define zlib constants in node_zlib.cc This is in order to avoid having two separate definitions for `node_zlib_mode`. --- src/node_constants.cc | 63 ------------------------------------------- src/node_internals.h | 2 ++ src/node_zlib.cc | 53 ++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 63 deletions(-) diff --git a/src/node_constants.cc b/src/node_constants.cc index b6c7bf37a3ad86..753b8f8fe16602 100644 --- a/src/node_constants.cc +++ b/src/node_constants.cc @@ -1241,69 +1241,6 @@ void DefineCryptoConstants(Local target) { NODE_DEFINE_CONSTANT(target, INT_MAX); } -void DefineZlibConstants(Local target) { - NODE_DEFINE_CONSTANT(target, Z_NO_FLUSH); - NODE_DEFINE_CONSTANT(target, Z_PARTIAL_FLUSH); - NODE_DEFINE_CONSTANT(target, Z_SYNC_FLUSH); - NODE_DEFINE_CONSTANT(target, Z_FULL_FLUSH); - NODE_DEFINE_CONSTANT(target, Z_FINISH); - NODE_DEFINE_CONSTANT(target, Z_BLOCK); - - // return/error codes - NODE_DEFINE_CONSTANT(target, Z_OK); - NODE_DEFINE_CONSTANT(target, Z_STREAM_END); - NODE_DEFINE_CONSTANT(target, Z_NEED_DICT); - NODE_DEFINE_CONSTANT(target, Z_ERRNO); - NODE_DEFINE_CONSTANT(target, Z_STREAM_ERROR); - NODE_DEFINE_CONSTANT(target, Z_DATA_ERROR); - NODE_DEFINE_CONSTANT(target, Z_MEM_ERROR); - NODE_DEFINE_CONSTANT(target, Z_BUF_ERROR); - NODE_DEFINE_CONSTANT(target, Z_VERSION_ERROR); - - NODE_DEFINE_CONSTANT(target, Z_NO_COMPRESSION); - NODE_DEFINE_CONSTANT(target, Z_BEST_SPEED); - NODE_DEFINE_CONSTANT(target, Z_BEST_COMPRESSION); - NODE_DEFINE_CONSTANT(target, Z_DEFAULT_COMPRESSION); - NODE_DEFINE_CONSTANT(target, Z_FILTERED); - NODE_DEFINE_CONSTANT(target, Z_HUFFMAN_ONLY); - NODE_DEFINE_CONSTANT(target, Z_RLE); - NODE_DEFINE_CONSTANT(target, Z_FIXED); - NODE_DEFINE_CONSTANT(target, Z_DEFAULT_STRATEGY); - NODE_DEFINE_CONSTANT(target, ZLIB_VERNUM); - - enum node_zlib_mode { - NONE, - DEFLATE, - INFLATE, - GZIP, - GUNZIP, - DEFLATERAW, - INFLATERAW, - UNZIP - }; - - NODE_DEFINE_CONSTANT(target, DEFLATE); - NODE_DEFINE_CONSTANT(target, INFLATE); - NODE_DEFINE_CONSTANT(target, GZIP); - NODE_DEFINE_CONSTANT(target, GUNZIP); - NODE_DEFINE_CONSTANT(target, DEFLATERAW); - NODE_DEFINE_CONSTANT(target, INFLATERAW); - NODE_DEFINE_CONSTANT(target, UNZIP); - - NODE_DEFINE_CONSTANT(target, Z_MIN_WINDOWBITS); - NODE_DEFINE_CONSTANT(target, Z_MAX_WINDOWBITS); - NODE_DEFINE_CONSTANT(target, Z_DEFAULT_WINDOWBITS); - NODE_DEFINE_CONSTANT(target, Z_MIN_CHUNK); - NODE_DEFINE_CONSTANT(target, Z_MAX_CHUNK); - NODE_DEFINE_CONSTANT(target, Z_DEFAULT_CHUNK); - NODE_DEFINE_CONSTANT(target, Z_MIN_MEMLEVEL); - NODE_DEFINE_CONSTANT(target, Z_MAX_MEMLEVEL); - NODE_DEFINE_CONSTANT(target, Z_DEFAULT_MEMLEVEL); - NODE_DEFINE_CONSTANT(target, Z_MIN_LEVEL); - NODE_DEFINE_CONSTANT(target, Z_MAX_LEVEL); - NODE_DEFINE_CONSTANT(target, Z_DEFAULT_LEVEL); -} - void DefineDLOpenConstants(Local target) { #ifdef RTLD_LAZY NODE_DEFINE_CONSTANT(target, RTLD_LAZY); diff --git a/src/node_internals.h b/src/node_internals.h index 9a34825b1e6df0..0c0b2e317620ce 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -936,6 +936,8 @@ void GetEGid(const v8::FunctionCallbackInfo& args); void GetGroups(const v8::FunctionCallbackInfo& args); #endif // __POSIX__ && !defined(__ANDROID__) && !defined(__CloudABI__) +void DefineZlibConstants(v8::Local target); + } // namespace node void napi_module_register_by_symbol(v8::Local exports, diff --git a/src/node_zlib.cc b/src/node_zlib.cc index c4a0b98f777d90..634bc1b1e42cd1 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -773,6 +773,59 @@ void Initialize(Local target, } } // anonymous namespace + +void DefineZlibConstants(Local target) { + NODE_DEFINE_CONSTANT(target, Z_NO_FLUSH); + NODE_DEFINE_CONSTANT(target, Z_PARTIAL_FLUSH); + NODE_DEFINE_CONSTANT(target, Z_SYNC_FLUSH); + NODE_DEFINE_CONSTANT(target, Z_FULL_FLUSH); + NODE_DEFINE_CONSTANT(target, Z_FINISH); + NODE_DEFINE_CONSTANT(target, Z_BLOCK); + + // return/error codes + NODE_DEFINE_CONSTANT(target, Z_OK); + NODE_DEFINE_CONSTANT(target, Z_STREAM_END); + NODE_DEFINE_CONSTANT(target, Z_NEED_DICT); + NODE_DEFINE_CONSTANT(target, Z_ERRNO); + NODE_DEFINE_CONSTANT(target, Z_STREAM_ERROR); + NODE_DEFINE_CONSTANT(target, Z_DATA_ERROR); + NODE_DEFINE_CONSTANT(target, Z_MEM_ERROR); + NODE_DEFINE_CONSTANT(target, Z_BUF_ERROR); + NODE_DEFINE_CONSTANT(target, Z_VERSION_ERROR); + + NODE_DEFINE_CONSTANT(target, Z_NO_COMPRESSION); + NODE_DEFINE_CONSTANT(target, Z_BEST_SPEED); + NODE_DEFINE_CONSTANT(target, Z_BEST_COMPRESSION); + NODE_DEFINE_CONSTANT(target, Z_DEFAULT_COMPRESSION); + NODE_DEFINE_CONSTANT(target, Z_FILTERED); + NODE_DEFINE_CONSTANT(target, Z_HUFFMAN_ONLY); + NODE_DEFINE_CONSTANT(target, Z_RLE); + NODE_DEFINE_CONSTANT(target, Z_FIXED); + NODE_DEFINE_CONSTANT(target, Z_DEFAULT_STRATEGY); + NODE_DEFINE_CONSTANT(target, ZLIB_VERNUM); + + NODE_DEFINE_CONSTANT(target, DEFLATE); + NODE_DEFINE_CONSTANT(target, INFLATE); + NODE_DEFINE_CONSTANT(target, GZIP); + NODE_DEFINE_CONSTANT(target, GUNZIP); + NODE_DEFINE_CONSTANT(target, DEFLATERAW); + NODE_DEFINE_CONSTANT(target, INFLATERAW); + NODE_DEFINE_CONSTANT(target, UNZIP); + + NODE_DEFINE_CONSTANT(target, Z_MIN_WINDOWBITS); + NODE_DEFINE_CONSTANT(target, Z_MAX_WINDOWBITS); + NODE_DEFINE_CONSTANT(target, Z_DEFAULT_WINDOWBITS); + NODE_DEFINE_CONSTANT(target, Z_MIN_CHUNK); + NODE_DEFINE_CONSTANT(target, Z_MAX_CHUNK); + NODE_DEFINE_CONSTANT(target, Z_DEFAULT_CHUNK); + NODE_DEFINE_CONSTANT(target, Z_MIN_MEMLEVEL); + NODE_DEFINE_CONSTANT(target, Z_MAX_MEMLEVEL); + NODE_DEFINE_CONSTANT(target, Z_DEFAULT_MEMLEVEL); + NODE_DEFINE_CONSTANT(target, Z_MIN_LEVEL); + NODE_DEFINE_CONSTANT(target, Z_MAX_LEVEL); + NODE_DEFINE_CONSTANT(target, Z_DEFAULT_LEVEL); +} + } // namespace node NODE_BUILTIN_MODULE_CONTEXT_AWARE(zlib, node::Initialize)