/*! * ws: a node.js websocket client * Copyright(c) 2011 Einar Otto Stangvik * MIT Licensed */ 'use strict'; /** * Merges an array of buffers into a target buffer. * * @param {Buffer} target The target buffer * @param {Buffer[]} buffers The array of buffers to merge * @public */ const merge = (target, buffers) => { var offset = 0; for (var i = 0; i < buffers.length; i++) { const buf = buffers[i]; buf.copy(target, offset); offset += buf.length; } }; /** * 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 = { merge, mask, unmask };