Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 = viewToBuffer(data);
  82. } else {
  83. buf = Buffer.from(data);
  84. toBuffer.readOnly = false;
  85. }
  86. return buf;
  87. }
  88. /**
  89. * Converts an `ArrayBuffer` view into a buffer.
  90. *
  91. * @param {(DataView|TypedArray)} view The view to convert
  92. * @return {Buffer} Converted view
  93. * @private
  94. */
  95. function viewToBuffer(view) {
  96. const buf = Buffer.from(view.buffer);
  97. if (view.byteLength !== view.buffer.byteLength) {
  98. return buf.slice(view.byteOffset, view.byteOffset + view.byteLength);
  99. }
  100. return buf;
  101. }
  102. try {
  103. const bufferUtil = require('bufferutil');
  104. const bu = bufferUtil.BufferUtil || bufferUtil;
  105. module.exports = {
  106. concat,
  107. mask(source, mask, output, offset, length) {
  108. if (length < 48) _mask(source, mask, output, offset, length);
  109. else bu.mask(source, mask, output, offset, length);
  110. },
  111. toArrayBuffer,
  112. toBuffer,
  113. unmask(buffer, mask) {
  114. if (buffer.length < 32) _unmask(buffer, mask);
  115. else bu.unmask(buffer, mask);
  116. }
  117. };
  118. } catch (e) /* istanbul ignore next */ {
  119. module.exports = {
  120. concat,
  121. mask: _mask,
  122. toArrayBuffer,
  123. toBuffer,
  124. unmask: _unmask
  125. };
  126. }