Skip to content
Open

Nan #14

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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 -e \"require('nan')\")",
"src/lzf"
],
"include_dirs": [ '<!(node -e "require(\'nan\')")' ],
"conditions": [
[
'OS=="linux" or OS=="freebsd" or OS=="openbsd" or OS=="solaris"',
{
"cflags": [ "-O3" ],
"conditions": [
["target_arch=='x64'", { "cflags": [ "-fPIC" ] }]
]
}
],
[
"OS=='mac'",
{
"xcode_settings": {
"OTHER_CFLAGS": [ "-O3" ]
}
}
],
[
"OS=='win'",
{
"msvs_settings": {
"VCCLCompilerTool": {
"Optimization": "2", # /O2
"FavorSizeOrSpeed": 1
}
}
}
]
]
}
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"url": "git://github.com/Topface/node-lzf.git"
},
"dependencies": {
"nan": "^2.0.9"
"nan": "^2.18.0"
},
"directories": {
"lib": "./lib"
Expand Down
85 changes: 32 additions & 53 deletions src/lzf.cc
Original file line number Diff line number Diff line change
@@ -1,88 +1,67 @@
/* node-lzf (C) 2011 Ian Babrou <ibobrik@gmail.com> */
/* node-lzf (C) 2025 Maintained Türkay Tanrikulu <trky.shorty@gmail.com> */

#include <node_buffer.h>
#include <stdlib.h>

#ifdef __APPLE__
#include <malloc/malloc.h>
#endif

#include "nan.h"

#include "lzf/lzf.h"


using namespace v8;
using namespace node;


// Handle<Value> ThrowNodeError(const char* what = NULL) {
// return Nan::ThrowError(Exception::Error(Nan::New<String>(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<Value> 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<Object> input = info[0].As<Object>();
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<Object> 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<Value> bufferIn = info[0];

size_t bytesUncompressed = 999 * 1024 * 1024; // it's about max size that V8 supports
Local<Object> input = info[0].As<Object>();
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<uint32_t>(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<Object> 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<Object> target) {

NAN_MODULE_INIT(init) {
Nan::SetMethod(target, "compress", compress);
Nan::SetMethod(target, "decompress", decompress);
}

NODE_MODULE(lzf, init)
NODE_MODULE(lzf, init)