BufferUtil.fallback.js 1.4 KB

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