diff --git a/binding.gyp b/binding.gyp index 6690863..d0dc725 100644 --- a/binding.gyp +++ b/binding.gyp @@ -5,21 +5,42 @@ "sources": [ "src/lzf.cc", "src/lzf/lzf_c.cc", - "src/lzf/lzf_d.cc", - "src/lzf/lzf.h", - "src/lzf/lzfP.h" + "src/lzf/lzf_d.cc" ], - 'conditions': [ - [ 'OS=="linux" or OS=="freebsd" or OS=="openbsd" or OS=="solaris"', { - 'cflags': ['-O2'] - }], - ['OS=="mac"', { - 'xcode_settings': { - 'OTHER_CFLAGS': ['-O2'] - } - }] + "include_dirs": [ + " */ +/* node-lzf (C) 2025 Maintained Türkay Tanrikulu */ #include -#include - -#ifdef __APPLE__ -#include -#endif - #include "nan.h" - #include "lzf/lzf.h" - using namespace v8; using namespace node; - -// Handle ThrowNodeError(const char* what = NULL) { -// return Nan::ThrowError(Exception::Error(Nan::New(what))); -// } NAN_METHOD(compress) { if (info.Length() < 1 || !Buffer::HasInstance(info[0])) { - return Nan::ThrowError("First argument must be a Buffer"); + return Nan::ThrowTypeError("First argument must be a Buffer"); } - Local bufferIn = info[0]; - size_t bytesIn = Buffer::Length(bufferIn); - char * dataPointer = Buffer::Data(bufferIn); - size_t bytesCompressed = bytesIn + 100; - char * bufferOut = (char*) malloc(bytesCompressed); - - if (!bufferOut) { - return Nan::ThrowError("LZF malloc failed!"); - } + Local input = info[0].As(); + char* inputData = Buffer::Data(input); + size_t inputLen = Buffer::Length(input); - unsigned result = lzf_compress(dataPointer, bytesIn, bufferOut, bytesCompressed); + size_t maxOut = inputLen + (inputLen / 16) + 64 + 3; + char* outBuf = new char[maxOut]; - if (!result) { - free(bufferOut); - return Nan::ThrowError("Compression failed, probably too small buffer"); + unsigned int outLen = lzf_compress(inputData, inputLen, outBuf, maxOut); + if (outLen == 0) { + delete[] outBuf; + return Nan::ThrowError("Compression failed"); } - bufferOut = (char*) realloc (bufferOut, result); - Nan::MaybeLocal BufferOut = Nan::NewBuffer(bufferOut, result); - - info.GetReturnValue().Set(BufferOut.ToLocalChecked()); + info.GetReturnValue().Set(Nan::NewBuffer(outBuf, outLen, [](char* data, void*) { + delete[] data; + }, nullptr).ToLocalChecked()); } - NAN_METHOD(decompress) { if (info.Length() < 1 || !Buffer::HasInstance(info[0])) { - return Nan::ThrowError("First argument must be a Buffer"); + return Nan::ThrowTypeError("First argument must be a Buffer"); } - Local bufferIn = info[0]; - - size_t bytesUncompressed = 999 * 1024 * 1024; // it's about max size that V8 supports + Local input = info[0].As(); + char* inputData = Buffer::Data(input); + size_t inputLen = Buffer::Length(input); - if (info.Length() > 1 && info[1]->IsNumber()) { // accept dest buffer size - bytesUncompressed = info[1]->Uint32Value(); + size_t expectedLen = 1024 * 1024 * 10; + if (info.Length() > 1 && info[1]->IsNumber()) { + expectedLen = Nan::To(info[1]).FromJust(); } + char* outBuf = new char[expectedLen]; + unsigned int outLen = lzf_decompress(inputData, inputLen, outBuf, expectedLen); - char * bufferOut = (char*) malloc(bytesUncompressed); - if (!bufferOut) { - return Nan::ThrowError("LZF malloc failed!"); + if (outLen == 0) { + delete[] outBuf; + return Nan::ThrowError("Decompression failed"); } - unsigned result = lzf_decompress(Buffer::Data(bufferIn), Buffer::Length(bufferIn), bufferOut, bytesUncompressed); - - if (!result) { - return Nan::ThrowError("Unrompression failed, probably too small buffer"); - } - - bufferOut = (char*) realloc (bufferOut, result); - Nan::MaybeLocal BufferOut = Nan::NewBuffer(bufferOut, result); - - info.GetReturnValue().Set(BufferOut.ToLocalChecked()); + info.GetReturnValue().Set(Nan::NewBuffer(outBuf, outLen, [](char* data, void*) { + delete[] data; + }, nullptr).ToLocalChecked()); } -extern "C" void -init (Handle target) { + +NAN_MODULE_INIT(init) { Nan::SetMethod(target, "compress", compress); Nan::SetMethod(target, "decompress", decompress); } -NODE_MODULE(lzf, init) +NODE_MODULE(lzf, init) \ No newline at end of file