wasm-hash.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. // 65536 is the size of a wasm memory page
  7. // 64 is the maximum chunk size for every possible wasm hash implementation
  8. // 4 is the maximum number of bytes per char for string encoding (max is utf-8)
  9. // ~3 makes sure that it's always a block of 4 chars, so avoid partially encoded bytes for base64
  10. const MAX_SHORT_STRING = Math.floor((65536 - 64) / 4) & ~3;
  11. class WasmHash {
  12. /**
  13. * @param {WebAssembly.Instance} instance wasm instance
  14. * @param {WebAssembly.Instance[]} instancesPool pool of instances
  15. * @param {number} chunkSize size of data chunks passed to wasm
  16. * @param {number} digestSize size of digest returned by wasm
  17. */
  18. constructor(instance, instancesPool, chunkSize, digestSize) {
  19. const exports = /** @type {any} */ (instance.exports);
  20. exports.init();
  21. this.exports = exports;
  22. this.mem = Buffer.from(exports.memory.buffer, 0, 65536);
  23. this.buffered = 0;
  24. this.instancesPool = instancesPool;
  25. this.chunkSize = chunkSize;
  26. this.digestSize = digestSize;
  27. }
  28. reset() {
  29. this.buffered = 0;
  30. this.exports.init();
  31. }
  32. /**
  33. * @param {Buffer | string} data data
  34. * @param {BufferEncoding=} encoding encoding
  35. * @returns {this} itself
  36. */
  37. update(data, encoding) {
  38. if (typeof data === "string") {
  39. while (data.length > MAX_SHORT_STRING) {
  40. this._updateWithShortString(data.slice(0, MAX_SHORT_STRING), encoding);
  41. data = data.slice(MAX_SHORT_STRING);
  42. }
  43. this._updateWithShortString(data, encoding);
  44. return this;
  45. }
  46. this._updateWithBuffer(data);
  47. return this;
  48. }
  49. /**
  50. * @param {string} data data
  51. * @param {BufferEncoding=} encoding encoding
  52. * @returns {void}
  53. */
  54. _updateWithShortString(data, encoding) {
  55. const { exports, buffered, mem, chunkSize } = this;
  56. let endPos;
  57. if (data.length < 70) {
  58. if (!encoding || encoding === "utf-8" || encoding === "utf8") {
  59. endPos = buffered;
  60. for (let i = 0; i < data.length; i++) {
  61. const cc = data.charCodeAt(i);
  62. if (cc < 0x80) mem[endPos++] = cc;
  63. else if (cc < 0x800) {
  64. mem[endPos] = (cc >> 6) | 0xc0;
  65. mem[endPos + 1] = (cc & 0x3f) | 0x80;
  66. endPos += 2;
  67. } else {
  68. // bail-out for weird chars
  69. endPos += mem.write(data.slice(i), endPos, encoding);
  70. break;
  71. }
  72. }
  73. } else if (encoding === "latin1") {
  74. endPos = buffered;
  75. for (let i = 0; i < data.length; i++) {
  76. const cc = data.charCodeAt(i);
  77. mem[endPos++] = cc;
  78. }
  79. } else {
  80. endPos = buffered + mem.write(data, buffered, encoding);
  81. }
  82. } else {
  83. endPos = buffered + mem.write(data, buffered, encoding);
  84. }
  85. if (endPos < chunkSize) {
  86. this.buffered = endPos;
  87. } else {
  88. const l = endPos & ~(this.chunkSize - 1);
  89. exports.update(l);
  90. const newBuffered = endPos - l;
  91. this.buffered = newBuffered;
  92. if (newBuffered > 0) mem.copyWithin(0, l, endPos);
  93. }
  94. }
  95. /**
  96. * @param {Buffer} data data
  97. * @returns {void}
  98. */
  99. _updateWithBuffer(data) {
  100. const { exports, buffered, mem } = this;
  101. const length = data.length;
  102. if (buffered + length < this.chunkSize) {
  103. data.copy(mem, buffered, 0, length);
  104. this.buffered += length;
  105. } else {
  106. const l = (buffered + length) & ~(this.chunkSize - 1);
  107. if (l > 65536) {
  108. let i = 65536 - buffered;
  109. data.copy(mem, buffered, 0, i);
  110. exports.update(65536);
  111. const stop = l - buffered - 65536;
  112. while (i < stop) {
  113. data.copy(mem, 0, i, i + 65536);
  114. exports.update(65536);
  115. i += 65536;
  116. }
  117. data.copy(mem, 0, i, l - buffered);
  118. exports.update(l - buffered - i);
  119. } else {
  120. data.copy(mem, buffered, 0, l - buffered);
  121. exports.update(l);
  122. }
  123. const newBuffered = length + buffered - l;
  124. this.buffered = newBuffered;
  125. if (newBuffered > 0) data.copy(mem, 0, length - newBuffered, length);
  126. }
  127. }
  128. digest(type) {
  129. const { exports, buffered, mem, digestSize } = this;
  130. exports.final(buffered);
  131. this.instancesPool.push(this);
  132. const hex = mem.toString("latin1", 0, digestSize);
  133. if (type === "hex") return hex;
  134. if (type === "binary" || !type) return Buffer.from(hex, "hex");
  135. return Buffer.from(hex, "hex").toString(type);
  136. }
  137. }
  138. const create = (wasmModule, instancesPool, chunkSize, digestSize) => {
  139. if (instancesPool.length > 0) {
  140. const old = instancesPool.pop();
  141. old.reset();
  142. return old;
  143. } else {
  144. return new WasmHash(
  145. new WebAssembly.Instance(wasmModule),
  146. instancesPool,
  147. chunkSize,
  148. digestSize
  149. );
  150. }
  151. };
  152. module.exports = create;
  153. module.exports.MAX_SHORT_STRING = MAX_SHORT_STRING;