bufferutil.cc
Go to the documentation of this file.
00001 
00008 #include <v8.h>
00009 #include <node.h>
00010 #include <node_version.h>
00011 #include <node_buffer.h>
00012 #include <node_object_wrap.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015 #include <wchar.h>
00016 #include <stdio.h>
00017 #include "nan.h"
00018 
00019 using namespace v8;
00020 using namespace node;
00021 
00022 class BufferUtil : public ObjectWrap
00023 {
00024 public:
00025 
00026   static void Initialize(v8::Handle<v8::Object> target)
00027   {
00028     Nan::HandleScope scope;
00029     Local<FunctionTemplate> t = Nan::New<FunctionTemplate>(New);
00030     t->InstanceTemplate()->SetInternalFieldCount(1);
00031     Nan::SetMethod(t, "unmask", BufferUtil::Unmask);
00032     Nan::SetMethod(t, "mask", BufferUtil::Mask);
00033     Nan::SetMethod(t, "merge", BufferUtil::Merge);
00034     Nan::Set(target, Nan::New<String>("BufferUtil").ToLocalChecked(), t->GetFunction());
00035   }
00036 
00037 protected:
00038 
00039   static NAN_METHOD(New)
00040   {
00041     Nan::HandleScope scope;
00042     BufferUtil* bufferUtil = new BufferUtil();
00043     bufferUtil->Wrap(info.This());
00044     info.GetReturnValue().Set(info.This());
00045   }
00046 
00047   static NAN_METHOD(Merge)
00048   {
00049     Nan::HandleScope scope;
00050     Local<Object> bufferObj = info[0]->ToObject();
00051     char* buffer = Buffer::Data(bufferObj);
00052     Local<Array> array = Local<Array>::Cast(info[1]);
00053     unsigned int arrayLength = array->Length();
00054     size_t offset = 0;
00055     unsigned int i;
00056     for (i = 0; i < arrayLength; ++i) {
00057       Local<Object> src = array->Get(i)->ToObject();
00058       size_t length = Buffer::Length(src);
00059       memcpy(buffer + offset, Buffer::Data(src), length);
00060       offset += length;
00061     }
00062     info.GetReturnValue().Set(Nan::True());
00063   }
00064 
00065   static NAN_METHOD(Unmask)
00066   {
00067     Nan::HandleScope scope;
00068     Local<Object> buffer_obj = info[0]->ToObject();
00069     size_t length = Buffer::Length(buffer_obj);
00070     Local<Object> mask_obj = info[1]->ToObject();
00071     unsigned int *mask = (unsigned int*)Buffer::Data(mask_obj);
00072     unsigned int* from = (unsigned int*)Buffer::Data(buffer_obj);
00073     size_t len32 = length / 4;
00074     unsigned int i;
00075     for (i = 0; i < len32; ++i) *(from + i) ^= *mask;
00076     from += i;
00077     switch (length % 4) {
00078       case 3: *((unsigned char*)from+2) = *((unsigned char*)from+2) ^ ((unsigned char*)mask)[2];
00079       case 2: *((unsigned char*)from+1) = *((unsigned char*)from+1) ^ ((unsigned char*)mask)[1];
00080       case 1: *((unsigned char*)from  ) = *((unsigned char*)from  ) ^ ((unsigned char*)mask)[0];
00081       case 0:;
00082     }
00083     info.GetReturnValue().Set(Nan::True());
00084   }
00085 
00086   static NAN_METHOD(Mask)
00087   {
00088     Nan::HandleScope scope;
00089     Local<Object> buffer_obj = info[0]->ToObject();
00090     Local<Object> mask_obj = info[1]->ToObject();
00091     unsigned int *mask = (unsigned int*)Buffer::Data(mask_obj);
00092     Local<Object> output_obj = info[2]->ToObject();
00093     unsigned int dataOffset = info[3]->Int32Value();
00094     unsigned int length = info[4]->Int32Value();
00095     unsigned int* to = (unsigned int*)(Buffer::Data(output_obj) + dataOffset);
00096     unsigned int* from = (unsigned int*)Buffer::Data(buffer_obj);
00097     unsigned int len32 = length / 4;
00098     unsigned int i;
00099     for (i = 0; i < len32; ++i) *(to + i) = *(from + i) ^ *mask;
00100     to += i;
00101     from += i;
00102     switch (length % 4) {
00103       case 3: *((unsigned char*)to+2) = *((unsigned char*)from+2) ^ *((unsigned char*)mask+2);
00104       case 2: *((unsigned char*)to+1) = *((unsigned char*)from+1) ^ *((unsigned char*)mask+1);
00105       case 1: *((unsigned char*)to  ) = *((unsigned char*)from  ) ^ *((unsigned char*)mask);
00106       case 0:;
00107     }
00108     info.GetReturnValue().Set(Nan::True());
00109   }
00110 };
00111 
00112 #if !NODE_VERSION_AT_LEAST(0,10,0)
00113 extern "C"
00114 #endif
00115 void init (Handle<Object> target)
00116 {
00117   Nan::HandleScope scope;
00118   BufferUtil::Initialize(target);
00119 }
00120 
00121 NODE_MODULE(bufferutil, init)


dji_ronin
Author(s):
autogenerated on Sat Jun 8 2019 20:15:31