File size: 3,682 Bytes
f9f0fec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#define NAPI_VERSION 1
#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)