bufs.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 Buffer(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. buffer.copy(newBuf);
  94. free(buffer);
  95. return newBuf;
  96. }
  97. /**
  98. * Reads an arbitrary signed int from a buffer.
  99. */
  100. export function readInt(buffer) {
  101. var length = buffer.length;
  102. var positive = buffer[length - 1] < 0x80;
  103. var result = positive ? 0 : -1;
  104. var lossy = false; // Note: We can't use bit manipulation here, since that stops
  105. // working if the result won't fit in a 32-bit int.
  106. if (length < 7) {
  107. // Common case which can't possibly be lossy (because the result has
  108. // no more than 48 bits, and loss only happens with 54 or more).
  109. for (var i = length - 1; i >= 0; i--) {
  110. result = result * 0x100 + buffer[i];
  111. }
  112. } else {
  113. for (var _i = length - 1; _i >= 0; _i--) {
  114. var one = buffer[_i];
  115. result *= 0x100;
  116. if (isLossyToAdd(result, one)) {
  117. lossy = true;
  118. }
  119. result += one;
  120. }
  121. }
  122. return {
  123. value: result,
  124. lossy: lossy
  125. };
  126. }
  127. /**
  128. * Reads an arbitrary unsigned int from a buffer.
  129. */
  130. export function readUInt(buffer) {
  131. var length = buffer.length;
  132. var result = 0;
  133. var lossy = false; // Note: See above in re bit manipulation.
  134. if (length < 7) {
  135. // Common case which can't possibly be lossy (see above).
  136. for (var i = length - 1; i >= 0; i--) {
  137. result = result * 0x100 + buffer[i];
  138. }
  139. } else {
  140. for (var _i2 = length - 1; _i2 >= 0; _i2--) {
  141. var one = buffer[_i2];
  142. result *= 0x100;
  143. if (isLossyToAdd(result, one)) {
  144. lossy = true;
  145. }
  146. result += one;
  147. }
  148. }
  149. return {
  150. value: result,
  151. lossy: lossy
  152. };
  153. }
  154. /**
  155. * Writes a little-endian 64-bit signed int into a buffer.
  156. */
  157. export function writeInt64(value, buffer) {
  158. if (value < MIN_EXACT_INT64 || value > MAX_EXACT_INT64) {
  159. throw new Error("Value out of range.");
  160. }
  161. if (value < 0) {
  162. value += BIT_64;
  163. }
  164. writeUInt64(value, buffer);
  165. }
  166. /**
  167. * Writes a little-endian 64-bit unsigned int into a buffer.
  168. */
  169. export function writeUInt64(value, buffer) {
  170. if (value < 0 || value > MAX_EXACT_UINT64) {
  171. throw new Error("Value out of range.");
  172. }
  173. var lowWord = value % BIT_32;
  174. var highWord = Math.floor(value / BIT_32);
  175. buffer.writeUInt32LE(lowWord, 0);
  176. buffer.writeUInt32LE(highWord, 4);
  177. }