Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

buffer-util.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. let offset = 0;
  16. for (let i = 0; i < list.length; i++) {
  17. const buf = list[i];
  18. target.set(buf, offset);
  19. offset += buf.length;
  20. }
  21. if (offset < totalLength) return target.slice(0, offset);
  22. return target;
  23. }
  24. /**
  25. * Masks a buffer using the given mask.
  26. *
  27. * @param {Buffer} source The buffer to mask
  28. * @param {Buffer} mask The mask to use
  29. * @param {Buffer} output The buffer where to store the result
  30. * @param {Number} offset The offset at which to start writing
  31. * @param {Number} length The number of bytes to mask.
  32. * @public
  33. */
  34. function _mask(source, mask, output, offset, length) {
  35. for (let i = 0; i < length; i++) {
  36. output[offset + i] = source[i] ^ mask[i & 3];
  37. }
  38. }
  39. /**
  40. * Unmasks a buffer using the given mask.
  41. *
  42. * @param {Buffer} buffer The buffer to unmask
  43. * @param {Buffer} mask The mask to use
  44. * @public
  45. */
  46. function _unmask(buffer, mask) {
  47. // Required until https://github.com/nodejs/node/issues/9006 is resolved.
  48. const length = buffer.length;
  49. for (let i = 0; i < length; i++) {
  50. buffer[i] ^= mask[i & 3];
  51. }
  52. }
  53. /**
  54. * Converts a buffer to an `ArrayBuffer`.
  55. *
  56. * @param {Buffer} buf The buffer to convert
  57. * @return {ArrayBuffer} Converted buffer
  58. * @public
  59. */
  60. function toArrayBuffer(buf) {
  61. if (buf.byteLength === buf.buffer.byteLength) {
  62. return buf.buffer;
  63. }
  64. return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
  65. }
  66. /**
  67. * Converts `data` to a `Buffer`.
  68. *
  69. * @param {*} data The data to convert
  70. * @return {Buffer} The buffer
  71. * @throws {TypeError}
  72. * @public
  73. */
  74. function toBuffer(data) {
  75. toBuffer.readOnly = true;
  76. if (Buffer.isBuffer(data)) return data;
  77. let buf;
  78. if (data instanceof ArrayBuffer) {
  79. buf = Buffer.from(data);
  80. } else if (ArrayBuffer.isView(data)) {
  81. buf = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
  82. } else {
  83. buf = Buffer.from(data);
  84. toBuffer.readOnly = false;
  85. }
  86. return buf;
  87. }
  88. try {
  89. const bufferUtil = require('bufferutil');
  90. const bu = bufferUtil.BufferUtil || bufferUtil;
  91. module.exports = {
  92. concat,
  93. mask(source, mask, output, offset, length) {
  94. if (length < 48) _mask(source, mask, output, offset, length);
  95. else bu.mask(source, mask, output, offset, length);
  96. },
  97. toArrayBuffer,
  98. toBuffer,
  99. unmask(buffer, mask) {
  100. if (buffer.length < 32) _unmask(buffer, mask);
  101. else bu.unmask(buffer, mask);
  102. }
  103. };
  104. } catch (e) /* istanbul ignore next */ {
  105. module.exports = {
  106. concat,
  107. mask: _mask,
  108. toArrayBuffer,
  109. toBuffer,
  110. unmask: _unmask
  111. };
  112. }