bufs.js 4.8 KB

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