BufferUtil.fallback.js (1414B)
1/*! 2 * ws: a node.js websocket client 3 * Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com> 4 * MIT Licensed 5 */ 6 7exports.BufferUtil = { 8 merge: function(mergedBuffer, buffers) { 9 var offset = 0; 10 for (var i = 0, l = buffers.length; i < l; ++i) { 11 var buf = buffers[i]; 12 buf.copy(mergedBuffer, offset); 13 offset += buf.length; 14 } 15 }, 16 mask: function(source, mask, output, offset, length) { 17 var maskNum = mask.readUInt32LE(0, true); 18 var i = 0; 19 for (; i < length - 3; i += 4) { 20 var num = maskNum ^ source.readUInt32LE(i, true); 21 if (num < 0) num = 4294967296 + num; 22 output.writeUInt32LE(num, offset + i, true); 23 } 24 switch (length % 4) { 25 case 3: output[offset + i + 2] = source[i + 2] ^ mask[2]; 26 case 2: output[offset + i + 1] = source[i + 1] ^ mask[1]; 27 case 1: output[offset + i] = source[i] ^ mask[0]; 28 case 0:; 29 } 30 }, 31 unmask: function(data, mask) { 32 var maskNum = mask.readUInt32LE(0, true); 33 var length = data.length; 34 var i = 0; 35 for (; i < length - 3; i += 4) { 36 var num = maskNum ^ data.readUInt32LE(i, true); 37 if (num < 0) num = 4294967296 + num; 38 data.writeUInt32LE(num, i, true); 39 } 40 switch (length % 4) { 41 case 3: data[i + 2] = data[i + 2] ^ mask[2]; 42 case 2: data[i + 1] = data[i + 1] ^ mask[1]; 43 case 1: data[i] = data[i] ^ mask[0]; 44 case 0:; 45 } 46 } 47}