buffer-util.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. 'use strict';
  2. const { EMPTY_BUFFER } = require('./constants');
  3. /**
  4. * Merges an array of buffers into a new buffer.
  5. *
  6. * @param {Buffer[]} list The array of buffers to concat
  7. * @param {Number} totalLength The total length of buffers in the list
  8. * @return {Buffer} The resulting buffer
  9. * @public
  10. */
  11. function concat(list, totalLength) {
  12. if (list.length === 0) return EMPTY_BUFFER;
  13. if (list.length === 1) return list[0];
  14. const target = Buffer.allocUnsafe(totalLength);
  15. var offset = 0;
  16. for (var i = 0; i < list.length; i++) {
  17. const buf = list[i];
  18. buf.copy(target, offset);
  19. offset += buf.length;
  20. }
  21. return target;
  22. }
  23. /**
  24. * Masks a buffer using the given mask.
  25. *
  26. * @param {Buffer} source The buffer to mask
  27. * @param {Buffer} mask The mask to use
  28. * @param {Buffer} output The buffer where to store the result
  29. * @param {Number} offset The offset at which to start writing
  30. * @param {Number} length The number of bytes to mask.
  31. * @public
  32. */
  33. function _mask(source, mask, output, offset, length) {
  34. for (var i = 0; i < length; i++) {
  35. output[offset + i] = source[i] ^ mask[i & 3];
  36. }
  37. }
  38. /**
  39. * Unmasks a buffer using the given mask.
  40. *
  41. * @param {Buffer} buffer The buffer to unmask
  42. * @param {Buffer} mask The mask to use
  43. * @public
  44. */
  45. function _unmask(buffer, mask) {
  46. // Required until https://github.com/nodejs/node/issues/9006 is resolved.
  47. const length = buffer.length;
  48. for (var i = 0; i < length; i++) {
  49. buffer[i] ^= mask[i & 3];
  50. }
  51. }
  52. /**
  53. * Converts a buffer to an `ArrayBuffer`.
  54. *
  55. * @param {Buffer} buf The buffer to convert
  56. * @return {ArrayBuffer} Converted buffer
  57. * @public
  58. */
  59. function toArrayBuffer(buf) {
  60. if (buf.byteLength === buf.buffer.byteLength) {
  61. return buf.buffer;
  62. }
  63. return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
  64. }
  65. /**
  66. * Converts `data` to a `Buffer`.
  67. *
  68. * @param {*} data The data to convert
  69. * @return {Buffer} The buffer
  70. * @throws {TypeError}
  71. * @public
  72. */
  73. function toBuffer(data) {
  74. toBuffer.readOnly = true;
  75. if (Buffer.isBuffer(data)) return data;
  76. var buf;
  77. if (data instanceof ArrayBuffer) {
  78. buf = Buffer.from(data);
  79. } else if (ArrayBuffer.isView(data)) {
  80. buf = viewToBuffer(data);
  81. } else {
  82. buf = Buffer.from(data);
  83. toBuffer.readOnly = false;
  84. }
  85. return buf;
  86. }
  87. /**
  88. * Converts an `ArrayBuffer` view into a buffer.
  89. *
  90. * @param {(DataView|TypedArray)} view The view to convert
  91. * @return {Buffer} Converted view
  92. * @private
  93. */
  94. function viewToBuffer(view) {
  95. const buf = Buffer.from(view.buffer);
  96. if (view.byteLength !== view.buffer.byteLength) {
  97. return buf.slice(view.byteOffset, view.byteOffset + view.byteLength);
  98. }
  99. return buf;
  100. }
  101. try {
  102. const bufferUtil = require('bufferutil');
  103. const bu = bufferUtil.BufferUtil || bufferUtil;
  104. module.exports = {
  105. concat,
  106. mask(source, mask, output, offset, length) {
  107. if (length < 48) _mask(source, mask, output, offset, length);
  108. else bu.mask(source, mask, output, offset, length);
  109. },
  110. toArrayBuffer,
  111. toBuffer,
  112. unmask(buffer, mask) {
  113. if (buffer.length < 32) _unmask(buffer, mask);
  114. else bu.unmask(buffer, mask);
  115. }
  116. };
  117. } catch (e) /* istanbul ignore next */ {
  118. module.exports = {
  119. concat,
  120. mask: _mask,
  121. toArrayBuffer,
  122. toBuffer,
  123. unmask: _unmask
  124. };
  125. }