You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
4.8 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.alloc = alloc;
  6. exports.free = free;
  7. exports.resize = resize;
  8. exports.readInt = readInt;
  9. exports.readUInt = readUInt;
  10. exports.writeInt64 = writeInt64;
  11. exports.writeUInt64 = writeUInt64;
  12. // Copyright 2012 The Obvious Corporation.
  13. /*
  14. * bufs: Buffer utilities.
  15. */
  16. /*
  17. * Module variables
  18. */
  19. /** Pool of buffers, where `bufPool[x].length === x`. */
  20. var bufPool = [];
  21. /** Maximum length of kept temporary buffers. */
  22. var TEMP_BUF_MAXIMUM_LENGTH = 20;
  23. /** Minimum exactly-representable 64-bit int. */
  24. var MIN_EXACT_INT64 = -0x8000000000000000;
  25. /** Maximum exactly-representable 64-bit int. */
  26. var MAX_EXACT_INT64 = 0x7ffffffffffffc00;
  27. /** Maximum exactly-representable 64-bit uint. */
  28. var MAX_EXACT_UINT64 = 0xfffffffffffff800;
  29. /**
  30. * The int value consisting just of a 1 in bit #32 (that is, one more
  31. * than the maximum 32-bit unsigned value).
  32. */
  33. var BIT_32 = 0x100000000;
  34. /**
  35. * The int value consisting just of a 1 in bit #64 (that is, one more
  36. * than the maximum 64-bit unsigned value).
  37. */
  38. var BIT_64 = 0x10000000000000000;
  39. /*
  40. * Helper functions
  41. */
  42. /**
  43. * Masks off all but the lowest bit set of the given number.
  44. */
  45. function lowestBit(num) {
  46. return num & -num;
  47. }
  48. /**
  49. * Gets whether trying to add the second number to the first is lossy
  50. * (inexact). The first number is meant to be an accumulated result.
  51. */
  52. function isLossyToAdd(accum, num) {
  53. if (num === 0) {
  54. return false;
  55. }
  56. var lowBit = lowestBit(num);
  57. var added = accum + lowBit;
  58. if (added === accum) {
  59. return true;
  60. }
  61. if (added - lowBit !== accum) {
  62. return true;
  63. }
  64. return false;
  65. }
  66. /*
  67. * Exported functions
  68. */
  69. /**
  70. * Allocates a buffer of the given length, which is initialized
  71. * with all zeroes. This returns a buffer from the pool if it is
  72. * available, or a freshly-allocated buffer if not.
  73. */
  74. function alloc(length) {
  75. var result = bufPool[length];
  76. if (result) {
  77. bufPool[length] = undefined;
  78. } else {
  79. result = new Buffer(length);
  80. }
  81. result.fill(0);
  82. return result;
  83. }
  84. /**
  85. * Releases a buffer back to the pool.
  86. */
  87. function free(buffer) {
  88. var length = buffer.length;
  89. if (length < TEMP_BUF_MAXIMUM_LENGTH) {
  90. bufPool[length] = buffer;
  91. }
  92. }
  93. /**
  94. * Resizes a buffer, returning a new buffer. Returns the argument if
  95. * the length wouldn't actually change. This function is only safe to
  96. * use if the given buffer was allocated within this module (since
  97. * otherwise the buffer might possibly be shared externally).
  98. */
  99. function resize(buffer, length) {
  100. if (length === buffer.length) {
  101. return buffer;
  102. }
  103. var newBuf = alloc(length);
  104. buffer.copy(newBuf);
  105. free(buffer);
  106. return newBuf;
  107. }
  108. /**
  109. * Reads an arbitrary signed int from a buffer.
  110. */
  111. function readInt(buffer) {
  112. var length = buffer.length;
  113. var positive = buffer[length - 1] < 0x80;
  114. var result = positive ? 0 : -1;
  115. var lossy = false; // Note: We can't use bit manipulation here, since that stops
  116. // working if the result won't fit in a 32-bit int.
  117. if (length < 7) {
  118. // Common case which can't possibly be lossy (because the result has
  119. // no more than 48 bits, and loss only happens with 54 or more).
  120. for (var i = length - 1; i >= 0; i--) {
  121. result = result * 0x100 + buffer[i];
  122. }
  123. } else {
  124. for (var _i = length - 1; _i >= 0; _i--) {
  125. var one = buffer[_i];
  126. result *= 0x100;
  127. if (isLossyToAdd(result, one)) {
  128. lossy = true;
  129. }
  130. result += one;
  131. }
  132. }
  133. return {
  134. value: result,
  135. lossy: lossy
  136. };
  137. }
  138. /**
  139. * Reads an arbitrary unsigned int from a buffer.
  140. */
  141. function readUInt(buffer) {
  142. var length = buffer.length;
  143. var result = 0;
  144. var lossy = false; // Note: See above in re bit manipulation.
  145. if (length < 7) {
  146. // Common case which can't possibly be lossy (see above).
  147. for (var i = length - 1; i >= 0; i--) {
  148. result = result * 0x100 + buffer[i];
  149. }
  150. } else {
  151. for (var _i2 = length - 1; _i2 >= 0; _i2--) {
  152. var one = buffer[_i2];
  153. result *= 0x100;
  154. if (isLossyToAdd(result, one)) {
  155. lossy = true;
  156. }
  157. result += one;
  158. }
  159. }
  160. return {
  161. value: result,
  162. lossy: lossy
  163. };
  164. }
  165. /**
  166. * Writes a little-endian 64-bit signed int into a buffer.
  167. */
  168. function writeInt64(value, buffer) {
  169. if (value < MIN_EXACT_INT64 || value > MAX_EXACT_INT64) {
  170. throw new Error("Value out of range.");
  171. }
  172. if (value < 0) {
  173. value += BIT_64;
  174. }
  175. writeUInt64(value, buffer);
  176. }
  177. /**
  178. * Writes a little-endian 64-bit unsigned int into a buffer.
  179. */
  180. function writeUInt64(value, buffer) {
  181. if (value < 0 || value > MAX_EXACT_UINT64) {
  182. throw new Error("Value out of range.");
  183. }
  184. var lowWord = value % BIT_32;
  185. var highWord = Math.floor(value / BIT_32);
  186. buffer.writeUInt32LE(lowWord, 0);
  187. buffer.writeUInt32LE(highWord, 4);
  188. }