Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 15 additions & 22 deletions src/bufferutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,20 @@ napi_value Mask(napi_env env, napi_callback_info info) {
// 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;
}
uint64_t mask8 = ((uint64_t *)maskAlignedArray)[0];
uint64_t *pFrom8 = (uint64_t *)source;
uint64_t *pTo8 = (uint64_t *)destination;
for (uint32_t i = 0; i < loop; i++) pTo8[i] = pFrom8[i] ^ mask8;
source += 8 * loop;
destination += 8 * loop;

//
// Apply mask to remaining data.
//
uint8_t *pmaskAlignedArray = maskAlignedArray;

length %= 8;
while (length--) {
*destination++ = *source++ ^ *pmaskAlignedArray++;
for (uint32_t i = 0; i < length; i++) {
destination[i] = source[i] ^ maskAlignedArray[i];
}

return NULL;
Expand All @@ -91,8 +87,8 @@ napi_value Unmask(napi_env env, napi_callback_info info) {
assert(status == napi_ok);

uint8_t *source;
size_t length;
uint8_t *mask;
size_t length;

status = napi_get_buffer_info(env, argv[0], (void **)&source, &length);
assert(status == napi_ok);
Expand Down Expand Up @@ -127,22 +123,19 @@ napi_value Unmask(napi_env env, napi_callback_info info) {
// Apply 64 bit mask in 8 byte chunks.
//
uint32_t loop = length / 8;
uint64_t *pMask8 = (uint64_t *)maskAlignedArray;
uint64_t mask8 = ((uint64_t *)maskAlignedArray)[0];

while (loop--) {
uint64_t *pSource8 = (uint64_t *)source;
*pSource8 ^= *pMask8;
source += 8;
}
uint64_t *pSource8 = (uint64_t *)source;
for (uint32_t i = 0; i < loop; i++) pSource8[i] ^= mask8;
source += 8 * loop;

//
// Apply mask to remaining data.
//
uint8_t *pmaskAlignedArray = maskAlignedArray;

length %= 8;
while (length--) {
*source++ ^= *pmaskAlignedArray++;
for (uint32_t i = 0; i < length; i++) {
source[i] ^= maskAlignedArray[i];
}

return NULL;
Expand Down