PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/browser-sync/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/node_modules/bufferutil/src/bufferutil.cc

https://gitlab.com/jeaster12/fabby
C++ | 120 lines | 104 code | 11 blank | 5 comment | 5 complexity | 77cf041679bd2b19fbceb4ad66f1fac7 MD5 | raw file
  1. /*!
  2. * bufferutil: WebSocket buffer utils
  3. * Copyright(c) 2015 Einar Otto Stangvik <einaros@gmail.com>
  4. * MIT Licensed
  5. */
  6. #include <v8.h>
  7. #include <node.h>
  8. #include <node_version.h>
  9. #include <node_buffer.h>
  10. #include <node_object_wrap.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <wchar.h>
  14. #include <stdio.h>
  15. #include "nan.h"
  16. using namespace v8;
  17. using namespace node;
  18. class BufferUtil : public ObjectWrap
  19. {
  20. public:
  21. static void Initialize(v8::Handle<v8::Object> target)
  22. {
  23. Nan::HandleScope scope;
  24. Local<FunctionTemplate> t = Nan::New<FunctionTemplate>(New);
  25. t->InstanceTemplate()->SetInternalFieldCount(1);
  26. Nan::SetMethod(t, "unmask", BufferUtil::Unmask);
  27. Nan::SetMethod(t, "mask", BufferUtil::Mask);
  28. Nan::SetMethod(t, "merge", BufferUtil::Merge);
  29. Nan::Set(target, Nan::New<String>("BufferUtil").ToLocalChecked(), t->GetFunction());
  30. }
  31. protected:
  32. static NAN_METHOD(New)
  33. {
  34. Nan::HandleScope scope;
  35. BufferUtil* bufferUtil = new BufferUtil();
  36. bufferUtil->Wrap(info.This());
  37. info.GetReturnValue().Set(info.This());
  38. }
  39. static NAN_METHOD(Merge)
  40. {
  41. Nan::HandleScope scope;
  42. Local<Object> bufferObj = info[0]->ToObject();
  43. char* buffer = Buffer::Data(bufferObj);
  44. Local<Array> array = Local<Array>::Cast(info[1]);
  45. unsigned int arrayLength = array->Length();
  46. size_t offset = 0;
  47. unsigned int i;
  48. for (i = 0; i < arrayLength; ++i) {
  49. Local<Object> src = array->Get(i)->ToObject();
  50. size_t length = Buffer::Length(src);
  51. memcpy(buffer + offset, Buffer::Data(src), length);
  52. offset += length;
  53. }
  54. info.GetReturnValue().Set(Nan::True());
  55. }
  56. static NAN_METHOD(Unmask)
  57. {
  58. Nan::HandleScope scope;
  59. Local<Object> buffer_obj = info[0]->ToObject();
  60. size_t length = Buffer::Length(buffer_obj);
  61. Local<Object> mask_obj = info[1]->ToObject();
  62. unsigned int *mask = (unsigned int*)Buffer::Data(mask_obj);
  63. unsigned int* from = (unsigned int*)Buffer::Data(buffer_obj);
  64. size_t len32 = length / 4;
  65. unsigned int i;
  66. for (i = 0; i < len32; ++i) *(from + i) ^= *mask;
  67. from += i;
  68. switch (length % 4) {
  69. case 3: *((unsigned char*)from+2) = *((unsigned char*)from+2) ^ ((unsigned char*)mask)[2];
  70. case 2: *((unsigned char*)from+1) = *((unsigned char*)from+1) ^ ((unsigned char*)mask)[1];
  71. case 1: *((unsigned char*)from ) = *((unsigned char*)from ) ^ ((unsigned char*)mask)[0];
  72. case 0:;
  73. }
  74. info.GetReturnValue().Set(Nan::True());
  75. }
  76. static NAN_METHOD(Mask)
  77. {
  78. Nan::HandleScope scope;
  79. Local<Object> buffer_obj = info[0]->ToObject();
  80. Local<Object> mask_obj = info[1]->ToObject();
  81. unsigned int *mask = (unsigned int*)Buffer::Data(mask_obj);
  82. Local<Object> output_obj = info[2]->ToObject();
  83. unsigned int dataOffset = info[3]->Int32Value();
  84. unsigned int length = info[4]->Int32Value();
  85. unsigned int* to = (unsigned int*)(Buffer::Data(output_obj) + dataOffset);
  86. unsigned int* from = (unsigned int*)Buffer::Data(buffer_obj);
  87. unsigned int len32 = length / 4;
  88. unsigned int i;
  89. for (i = 0; i < len32; ++i) *(to + i) = *(from + i) ^ *mask;
  90. to += i;
  91. from += i;
  92. switch (length % 4) {
  93. case 3: *((unsigned char*)to+2) = *((unsigned char*)from+2) ^ *((unsigned char*)mask+2);
  94. case 2: *((unsigned char*)to+1) = *((unsigned char*)from+1) ^ *((unsigned char*)mask+1);
  95. case 1: *((unsigned char*)to ) = *((unsigned char*)from ) ^ *((unsigned char*)mask);
  96. case 0:;
  97. }
  98. info.GetReturnValue().Set(Nan::True());
  99. }
  100. };
  101. #if !NODE_VERSION_AT_LEAST(0,10,0)
  102. extern "C"
  103. #endif
  104. void init (Handle<Object> target)
  105. {
  106. Nan::HandleScope scope;
  107. BufferUtil::Initialize(target);
  108. }
  109. NODE_MODULE(bufferutil, init)