Last active
December 16, 2015 04:38
-
-
Save bodokaiser/5378187 to your computer and use it in GitHub Desktop.
Nodejs C++ Addon which takes buffer and does bit operations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <v8.h> | |
#include <node.h> | |
#include <node_buffer.h> | |
#include <iostream> | |
using namespace v8; | |
Handle<Value> CalcHeadSize(const Arguments &args) { | |
HandleScope scope; | |
Local<Object> obj = args[0]->ToObject(); | |
if (!node::Buffer::HasInstance(obj)) { | |
ThrowException( | |
Exception::TypeError( | |
String::New("Argument must be a buffer."))); | |
return scope.Close(Undefined()); | |
} | |
unsigned char* data = static_cast<unsigned char*>(obj->GetIndexedPropertiesExternalArrayData()); | |
unsigned int length = 2; | |
if (data[1] & 0x80) | |
length += 4; | |
switch (data[1] & 0x7f) { | |
case 126: | |
length += 2; | |
break; | |
case 127: | |
length += 8; | |
break; | |
} | |
return scope.Close(Number::New(length)); | |
} | |
void init(Handle<Object> exports) { | |
exports->Set(String::New("calcHeadSize"), | |
FunctionTemplate::New(CalcHeadSize)->GetFunction()); | |
} | |
NODE_MODULE(parser, init); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment