pax_global_header00006660000000000000000000000064134076367650014533gustar00rootroot0000000000000052 comment=e367139397179f7f132900025e9b461e714a35bc bufferutil-4.0.1/000077500000000000000000000000001340763676500137045ustar00rootroot00000000000000bufferutil-4.0.1/.gitignore000066400000000000000000000000561340763676500156750ustar00rootroot00000000000000node_modules/ prebuilds/ build/ npm-debug.log bufferutil-4.0.1/.npmignore000066400000000000000000000000371340763676500157030ustar00rootroot00000000000000build/ appveyor.yml test.js .* bufferutil-4.0.1/.travis.yml000066400000000000000000000011231340763676500160120ustar00rootroot00000000000000sudo: false language: node_js node_js: - "10" - "8" - "6" os: - linux - osx before_deploy: - ARCHIVE_NAME="${TRAVIS_TAG:-latest}-$TRAVIS_OS_NAME-$(uname -m).tar" - npm run prebuild - tar --create --verbose --file="$ARCHIVE_NAME" --directory "$TRAVIS_BUILD_DIR/prebuilds" . deploy: provider: releases api_key: secure: OzQWRAFAR+oJ5uSFn2WwCun5RJDD5HQnPhXdJNj5ErpBy0CmiweF7YwnDhOywncw9Rz+iQEfg4MUCsnSkD0u5LRKmwvED/Y/ddol1VQTBmWREeQqCuGLr/DoD2nEnYoLIZWlIbqSEO3TtDaCuh818XqZhcfHms6jEMyF5WuKD/c= file: "$ARCHIVE_NAME" skip_cleanup: true on: tags: true node: "10" bufferutil-4.0.1/LICENSE000066400000000000000000000021411340763676500147070ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2011 Einar Otto Stangvik (http://2x.io) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. bufferutil-4.0.1/README.md000066400000000000000000000037201340763676500151650ustar00rootroot00000000000000# bufferutil [![Version npm](https://img.shields.io/npm/v/bufferutil.svg)](https://www.npmjs.com/package/bufferutil) [![Linux/macOS Build](https://travis-ci.org/websockets/bufferutil.svg?branch=master)](https://travis-ci.org/websockets/bufferutil) [![Windows Build](https://ci.appveyor.com/api/projects/status/github/websockets/bufferutil?branch=master&svg=true)](https://ci.appveyor.com/project/lpinca/bufferutil) `bufferutil` is what makes `ws` fast. It provides some utilities to efficiently perform some operations such as masking and unmasking the data payload of WebSocket frames. ## Installation ``` npm install bufferutil --save-optional ``` The `--save-optional` flag tells npm to save the package in your package.json under the [`optionalDependencies`](https://docs.npmjs.com/files/package.json#optionaldependencies) key. ## API The module exports two functions. ### `bufferUtil.mask(source, mask, output, offset, length)` Masks a buffer using the given masking-key as specified by the WebSocket protocol. #### Arguments - `source` - The buffer to mask. - `mask` - A buffer representing the masking-key. - `output` - The buffer where to store the result. - `offset` - The offset at which to start writing. - `length` - The number of bytes to mask. #### Example ```js 'use strict'; const bufferUtil = require('bufferutil'); const crypto = require('crypto'); const source = crypto.randomBytes(10); const mask = crypto.randomBytes(4); bufferUtil.mask(source, mask, source, 0, source.length); ``` ### `bufferUtil.unmask(buffer, mask)` Unmasks a buffer using the given masking-key as specified by the WebSocket protocol. #### Arguments - `buffer` - The buffer to unmask. - `mask` - A buffer representing the masking-key. #### Example ```js 'use strict'; const bufferUtil = require('bufferutil'); const crypto = require('crypto'); const buffer = crypto.randomBytes(10); const mask = crypto.randomBytes(4); bufferUtil.unmask(buffer, mask); ``` ## License [MIT](LICENSE) bufferutil-4.0.1/appveyor.yml000066400000000000000000000015061340763676500162760ustar00rootroot00000000000000build: off configuration: Release environment: matrix: - nodejs_version: "10" - nodejs_version: "8" - nodejs_version: "6" platform: - x86 - x64 matrix: fast_finish: true install: - ps: Install-Product node $env:nodejs_version $env:platform - npm install test_script: - node --version - npm --version - npm test after_test: - ps: If ($env:nodejs_version -eq "10") { npm run prebuild } - ps: $env:ARTIFACT_NAME_PREFIX = if (Test-Path env:APPVEYOR_REPO_TAG_NAME) { $env:APPVEYOR_REPO_TAG_NAME } else { 'latest' } artifacts: - path: prebuilds name: $(ARTIFACT_NAME_PREFIX)-win-$(PLATFORM) deploy: - provider: GitHub artifact: /.*\.zip/ auth_token: secure: CkWRNlzH+wmCa3+TRydWfvmSH7tevR81aAypEhXTR9ka5q0Ja8lAIPDwtPb+Ux9l on: appveyor_repo_tag: true nodejs_version: "10" bufferutil-4.0.1/binding.gyp000066400000000000000000000002121340763676500160320ustar00rootroot00000000000000{ 'targets': [ { 'target_name': 'bufferutil', 'sources': ['src/bufferutil.c'], 'cflags': ['-std=c99'] } ] } bufferutil-4.0.1/fallback.js000066400000000000000000000016031340763676500160010ustar00rootroot00000000000000'use strict'; /** * Masks a buffer using the given mask. * * @param {Buffer} source The buffer to mask * @param {Buffer} mask The mask to use * @param {Buffer} output The buffer where to store the result * @param {Number} offset The offset at which to start writing * @param {Number} length The number of bytes to mask. * @public */ const mask = (source, mask, output, offset, length) => { for (var i = 0; i < length; i++) { output[offset + i] = source[i] ^ mask[i & 3]; } }; /** * Unmasks a buffer using the given mask. * * @param {Buffer} buffer The buffer to unmask * @param {Buffer} mask The mask to use * @public */ const unmask = (buffer, mask) => { // Required until https://github.com/nodejs/node/issues/9006 is resolved. const length = buffer.length; for (var i = 0; i < length; i++) { buffer[i] ^= mask[i & 3]; } }; module.exports = { mask, unmask }; bufferutil-4.0.1/index.js000066400000000000000000000002101340763676500153420ustar00rootroot00000000000000'use strict'; try { module.exports = require('node-gyp-build')(__dirname); } catch (e) { module.exports = require('./fallback'); } bufferutil-4.0.1/package.json000066400000000000000000000013211340763676500161670ustar00rootroot00000000000000{ "name": "bufferutil", "version": "4.0.1", "description": "WebSocket buffer utils", "main": "index.js", "scripts": { "install": "node-gyp-build", "prebuild": "prebuildify --napi", "test": "mocha" }, "repository": { "type": "git", "url": "https://github.com/websockets/bufferutil" }, "keywords": [ "bufferutil" ], "author": "Einar Otto Stangvik (http://2x.io)", "license": "MIT", "bugs": { "url": "https://github.com/websockets/bufferutil/issues" }, "homepage": "https://github.com/websockets/bufferutil", "dependencies": { "node-gyp-build": "~3.7.0" }, "devDependencies": { "mocha": "~5.2.0", "prebuildify": "~2.10.0" } } bufferutil-4.0.1/src/000077500000000000000000000000001340763676500144735ustar00rootroot00000000000000bufferutil-4.0.1/src/bufferutil.c000066400000000000000000000071421340763676500170120ustar00rootroot00000000000000#define NAPI_VERSION 1 #include #include 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) bufferutil-4.0.1/test.js000066400000000000000000000025401340763676500152220ustar00rootroot00000000000000'use strict'; const assert = require('assert'); function use(bufferUtil) { return function () { it('masks a buffer (1/2)', function () { const buf = Buffer.from([0x6c, 0x3c, 0x58, 0xd9, 0x3e, 0x21, 0x09, 0x9f]); const mask = Buffer.from([0x48, 0x2a, 0xce, 0x24]); bufferUtil.mask(buf, mask, buf, 0, buf.length); assert.deepStrictEqual( buf, Buffer.from([0x24, 0x16, 0x96, 0xfd, 0x76, 0x0b, 0xc7, 0xbb]) ); }); it('masks a buffer (2/2)', function () { const src = Buffer.from([0x6c, 0x3c, 0x58, 0xd9, 0x3e, 0x21, 0x09, 0x9f]); const mask = Buffer.from([0x48, 0x2a, 0xce, 0x24]); const dest = Buffer.alloc(src.length + 2); bufferUtil.mask(src, mask, dest, 2, src.length); assert.deepStrictEqual( dest, Buffer.from([0x00, 0x00, 0x24, 0x16, 0x96, 0xfd, 0x76, 0x0b, 0xc7, 0xbb]) ); }); it('unmasks a buffer', function () { const buf = Buffer.from([0x24, 0x16, 0x96, 0xfd, 0x76, 0x0b, 0xc7, 0xbb]); const mask = Buffer.from([0x48, 0x2a, 0xce, 0x24]); bufferUtil.unmask(buf, mask); assert.deepStrictEqual( buf, Buffer.from([0x6c, 0x3c, 0x58, 0xd9, 0x3e, 0x21, 0x09, 0x9f]) ); }); }; } describe('bindings', use(require('node-gyp-build')(__dirname))); describe('fallback', use(require('./fallback')));