[major] Port to N-API

This commit is contained in:
Luigi Pinca
2018-07-02 14:40:13 +02:00
parent 9bc5bb935e
commit 5c0fbce8ff
4 changed files with 181 additions and 128 deletions
+2 -7
View File
@@ -2,13 +2,8 @@
'targets': [
{
'target_name': 'bufferutil',
'include_dirs': ["<!(node -e \"require('nan')\")"],
'sources': [ 'src/bufferutil.cc' ],
'xcode_settings': {
'MACOSX_DEPLOYMENT_TARGET': '10.8',
'CLANG_CXX_LANGUAGE_STANDARD': 'c++11',
'CLANG_CXX_LIBRARY': 'libc++'
}
'sources': ['src/bufferutil.c'],
'cflags': ['-std=c99']
}
]
}
+3 -8
View File
@@ -4,8 +4,7 @@
"description": "WebSocket buffer utils",
"main": "index.js",
"scripts": {
"install": "prebuild-install || node-gyp rebuild",
"test": "mocha && prebuild-ci"
"test": "mocha"
},
"repository": {
"type": "git",
@@ -21,13 +20,9 @@
},
"homepage": "https://github.com/websockets/bufferutil",
"dependencies": {
"bindings": "~1.3.0",
"nan": "~2.10.0",
"prebuild-install": "~4.0.0"
"bindings": "~1.3.0"
},
"devDependencies": {
"mocha": "~5.2.0",
"prebuild": "~7.6.0",
"prebuild-ci": "~2.2.0"
"mocha": "~5.2.0"
}
}
+176
View File
@@ -0,0 +1,176 @@
/*!
* bufferutil: WebSocket buffer utils
* Copyright(c) 2015 Einar Otto Stangvik <[email protected]>
* MIT Licensed
*/
#include <assert.h>
#include <node_api.h>
napi_value Mask(napi_env env, napi_callback_info info) {
napi_status status;
size_t argc = 5;
napi_value argv[5];
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
assert(status == napi_ok);
uint8_t *source;
uint8_t *mask;
uint8_t *destination;
uint32_t offset;
uint32_t length;
status = napi_get_buffer_info(env, argv[0], (void **)&source, NULL);
assert(status == napi_ok);
status = napi_get_buffer_info(env, argv[1], (void **)&mask, NULL);
assert(status == napi_ok);
status = napi_get_buffer_info(env, argv[2], (void **)&destination, NULL);
assert(status == napi_ok);
status = napi_get_value_uint32(env, argv[3], &offset);
assert(status == napi_ok);
status = napi_get_value_uint32(env, argv[4], &length);
assert(status == napi_ok);
destination += offset;
uint32_t index = 0;
//
// Alignment preamble.
//
while (index < length && ((size_t)source % 8)) {
*destination++ = *source++ ^ mask[index % 4];
index++;
}
length -= index;
if (!length)
return NULL;
//
// Realign mask and convert to 64 bit.
//
uint8_t maskAlignedArray[8];
for (uint8_t i = 0; i < 8; i++, index++) {
maskAlignedArray[i] = mask[index % 4];
}
//
// Apply 64 bit mask in 8 byte chunks.
//
uint32_t loop = length / 8;
uint64_t *pMask8 = (uint64_t *)maskAlignedArray;
while (loop--) {
uint64_t *pFrom8 = (uint64_t *)source;
uint64_t *pTo8 = (uint64_t *)destination;
*pTo8 = *pFrom8 ^ *pMask8;
source += 8;
destination += 8;
}
//
// Apply mask to remaining data.
//
uint8_t *pmaskAlignedArray = maskAlignedArray;
length %= 8;
while (length--) {
*destination++ = *source++ ^ *pmaskAlignedArray++;
}
return NULL;
}
napi_value Unmask(napi_env env, napi_callback_info info) {
napi_status status;
size_t argc = 2;
napi_value argv[2];
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
assert(status == napi_ok);
uint8_t *source;
size_t length;
uint8_t *mask;
status = napi_get_buffer_info(env, argv[0], (void **)&source, &length);
assert(status == napi_ok);
status = napi_get_buffer_info(env, argv[1], (void **)&mask, NULL);
assert(status == napi_ok);
uint32_t index = 0;
//
// Alignment preamble.
//
while (index < length && ((size_t)source % 8)) {
*source++ ^= mask[index % 4];
index++;
}
length -= index;
if (!length)
return NULL;
//
// Realign mask and convert to 64 bit.
//
uint8_t maskAlignedArray[8];
for (uint8_t i = 0; i < 8; i++, index++) {
maskAlignedArray[i] = mask[index % 4];
}
//
// Apply 64 bit mask in 8 byte chunks.
//
uint32_t loop = length / 8;
uint64_t *pMask8 = (uint64_t *)maskAlignedArray;
while (loop--) {
uint64_t *pSource8 = (uint64_t *)source;
*pSource8 ^= *pMask8;
source += 8;
}
//
// Apply mask to remaining data.
//
uint8_t *pmaskAlignedArray = maskAlignedArray;
length %= 8;
while (length--) {
*source++ ^= *pmaskAlignedArray++;
}
return NULL;
}
napi_value Init(napi_env env, napi_value exports) {
napi_status status;
napi_value mask;
napi_value unmask;
status = napi_create_function(env, NULL, 0, Mask, NULL, &mask);
assert(status == napi_ok);
status = napi_create_function(env, NULL, 0, Unmask, NULL, &unmask);
assert(status == napi_ok);
status = napi_set_named_property(env, exports, "mask", mask);
assert(status == napi_ok);
status = napi_set_named_property(env, exports, "unmask", unmask);
assert(status == napi_ok);
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
-113
View File
@@ -1,113 +0,0 @@
/*!
* bufferutil: WebSocket buffer utils
* Copyright(c) 2015 Einar Otto Stangvik <[email protected]>
* MIT Licensed
*/
#include <nan.h>
NAN_METHOD(mask) {
char* from = node::Buffer::Data(info[0]);
char* mask = node::Buffer::Data(info[1]);
char* to = node::Buffer::Data(info[2]) + info[3]->Int32Value();
size_t length = info[4]->Int32Value();
size_t index = 0;
//
// Alignment preamble.
//
while (index < length && (reinterpret_cast<size_t>(from) & 0x07)) {
*to++ = *from++ ^ mask[index % 4];
index++;
}
length -= index;
if (!length) return;
//
// Realign mask and convert to 64 bit.
//
char maskAlignedArray[8];
for (size_t i = 0; i < 8; i++, index++) {
maskAlignedArray[i] = mask[index % 4];
}
//
// Apply 64 bit mask in 8 byte chunks.
//
size_t loop = length / 8;
uint64_t* pMask8 = reinterpret_cast<uint64_t*>(maskAlignedArray);
while (loop--) {
uint64_t* pFrom8 = reinterpret_cast<uint64_t*>(from);
uint64_t* pTo8 = reinterpret_cast<uint64_t*>(to);
*pTo8 = *pFrom8 ^ *pMask8;
from += 8;
to += 8;
}
//
// Apply mask to remaining data.
//
char* pmaskAlignedArray = maskAlignedArray;
length &= 0x7;
while (length--) {
*to++ = *from++ ^ *pmaskAlignedArray++;
}
}
NAN_METHOD(unmask) {
char* from = node::Buffer::Data(info[0]);
size_t length = node::Buffer::Length(info[0]);
char* mask = node::Buffer::Data(info[1]);
size_t index = 0;
//
// Alignment preamble.
//
while (index < length && (reinterpret_cast<size_t>(from) & 0x07)) {
*from++ ^= mask[index % 4];
index++;
}
length -= index;
if (!length) return;
//
// Realign mask and convert to 64 bit.
//
char maskAlignedArray[8];
for (size_t i = 0; i < 8; i++, index++) {
maskAlignedArray[i] = mask[index % 4];
}
//
// Apply 64 bit mask in 8 byte chunks.
//
size_t loop = length / 8;
uint64_t* pMask8 = reinterpret_cast<uint64_t*>(maskAlignedArray);
while (loop--) {
uint64_t* pSource8 = reinterpret_cast<uint64_t*>(from);
*pSource8 ^= *pMask8;
from += 8;
}
//
// Apply mask to remaining data.
//
char* pmaskAlignedArray = maskAlignedArray;
length &= 0x7;
while (length--) {
*from++ ^= *pmaskAlignedArray++;
}
}
NAN_MODULE_INIT(init) {
NAN_EXPORT(target, mask);
NAN_EXPORT(target, unmask);
}
NODE_MODULE(bufferutil, init)