fallback.js 899 B

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. /**
  3. * Masks a buffer using the given mask.
  4. *
  5. * @param {Buffer} source The buffer to mask
  6. * @param {Buffer} mask The mask to use
  7. * @param {Buffer} output The buffer where to store the result
  8. * @param {Number} offset The offset at which to start writing
  9. * @param {Number} length The number of bytes to mask.
  10. * @public
  11. */
  12. const mask = (source, mask, output, offset, length) => {
  13. for (var i = 0; i < length; i++) {
  14. output[offset + i] = source[i] ^ mask[i & 3];
  15. }
  16. };
  17. /**
  18. * Unmasks a buffer using the given mask.
  19. *
  20. * @param {Buffer} buffer The buffer to unmask
  21. * @param {Buffer} mask The mask to use
  22. * @public
  23. */
  24. const unmask = (buffer, mask) => {
  25. // Required until https://github.com/nodejs/node/issues/9006 is resolved.
  26. const length = buffer.length;
  27. for (var i = 0; i < length; i++) {
  28. buffer[i] ^= mask[i & 3];
  29. }
  30. };
  31. module.exports = { mask, unmask };