leb.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // Copyright 2012 The Obvious Corporation.
  2. /*
  3. * leb: LEB128 utilities.
  4. */
  5. /*
  6. * Modules used
  7. */
  8. "use strict";
  9. Object.defineProperty(exports, "__esModule", {
  10. value: true
  11. });
  12. exports.default = void 0;
  13. var _long = _interopRequireDefault(require("@xtuc/long"));
  14. var bits = _interopRequireWildcard(require("./bits"));
  15. var bufs = _interopRequireWildcard(require("./bufs"));
  16. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  17. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  18. /*
  19. * Module variables
  20. */
  21. /** The minimum possible 32-bit signed int. */
  22. var MIN_INT32 = -0x80000000;
  23. /** The maximum possible 32-bit signed int. */
  24. var MAX_INT32 = 0x7fffffff;
  25. /** The maximum possible 32-bit unsigned int. */
  26. var MAX_UINT32 = 0xffffffff;
  27. /** The minimum possible 64-bit signed int. */
  28. // const MIN_INT64 = -0x8000000000000000;
  29. /**
  30. * The maximum possible 64-bit signed int that is representable as a
  31. * JavaScript number.
  32. */
  33. // const MAX_INT64 = 0x7ffffffffffffc00;
  34. /**
  35. * The maximum possible 64-bit unsigned int that is representable as a
  36. * JavaScript number.
  37. */
  38. // const MAX_UINT64 = 0xfffffffffffff800;
  39. /*
  40. * Helper functions
  41. */
  42. /**
  43. * Determines the number of bits required to encode the number
  44. * represented in the given buffer as a signed value. The buffer is
  45. * taken to represent a signed number in little-endian form.
  46. *
  47. * The number of bits to encode is the (zero-based) bit number of the
  48. * highest-order non-sign-matching bit, plus two. For example:
  49. *
  50. * 11111011 01110101
  51. * high low
  52. *
  53. * The sign bit here is 1 (that is, it's a negative number). The highest
  54. * bit number that doesn't match the sign is bit #10 (where the lowest-order
  55. * bit is bit #0). So, we have to encode at least 12 bits total.
  56. *
  57. * As a special degenerate case, the numbers 0 and -1 each require just one bit.
  58. */
  59. function signedBitCount(buffer) {
  60. return bits.highOrder(bits.getSign(buffer) ^ 1, buffer) + 2;
  61. }
  62. /**
  63. * Determines the number of bits required to encode the number
  64. * represented in the given buffer as an unsigned value. The buffer is
  65. * taken to represent an unsigned number in little-endian form.
  66. *
  67. * The number of bits to encode is the (zero-based) bit number of the
  68. * highest-order 1 bit, plus one. For example:
  69. *
  70. * 00011000 01010011
  71. * high low
  72. *
  73. * The highest-order 1 bit here is bit #12 (where the lowest-order bit
  74. * is bit #0). So, we have to encode at least 13 bits total.
  75. *
  76. * As a special degenerate case, the number 0 requires 1 bit.
  77. */
  78. function unsignedBitCount(buffer) {
  79. var result = bits.highOrder(1, buffer) + 1;
  80. return result ? result : 1;
  81. }
  82. /**
  83. * Common encoder for both signed and unsigned ints. This takes a
  84. * bigint-ish buffer, returning an LEB128-encoded buffer.
  85. */
  86. function encodeBufferCommon(buffer, signed) {
  87. var signBit;
  88. var bitCount;
  89. if (signed) {
  90. signBit = bits.getSign(buffer);
  91. bitCount = signedBitCount(buffer);
  92. } else {
  93. signBit = 0;
  94. bitCount = unsignedBitCount(buffer);
  95. }
  96. var byteCount = Math.ceil(bitCount / 7);
  97. var result = bufs.alloc(byteCount);
  98. for (var i = 0; i < byteCount; i++) {
  99. var payload = bits.extract(buffer, i * 7, 7, signBit);
  100. result[i] = payload | 0x80;
  101. } // Mask off the top bit of the last byte, to indicate the end of the
  102. // encoding.
  103. result[byteCount - 1] &= 0x7f;
  104. return result;
  105. }
  106. /**
  107. * Gets the byte-length of the value encoded in the given buffer at
  108. * the given index.
  109. */
  110. function encodedLength(encodedBuffer, index) {
  111. var result = 0;
  112. while (encodedBuffer[index + result] >= 0x80) {
  113. result++;
  114. }
  115. result++; // to account for the last byte
  116. if (index + result > encodedBuffer.length) {// FIXME(sven): seems to cause false positives
  117. // throw new Error("integer representation too long");
  118. }
  119. return result;
  120. }
  121. /**
  122. * Common decoder for both signed and unsigned ints. This takes an
  123. * LEB128-encoded buffer, returning a bigint-ish buffer.
  124. */
  125. function decodeBufferCommon(encodedBuffer, index, signed) {
  126. index = index === undefined ? 0 : index;
  127. var length = encodedLength(encodedBuffer, index);
  128. var bitLength = length * 7;
  129. var byteLength = Math.ceil(bitLength / 8);
  130. var result = bufs.alloc(byteLength);
  131. var outIndex = 0;
  132. while (length > 0) {
  133. bits.inject(result, outIndex, 7, encodedBuffer[index]);
  134. outIndex += 7;
  135. index++;
  136. length--;
  137. }
  138. var signBit;
  139. var signByte;
  140. if (signed) {
  141. // Sign-extend the last byte.
  142. var lastByte = result[byteLength - 1];
  143. var endBit = outIndex % 8;
  144. if (endBit !== 0) {
  145. var shift = 32 - endBit; // 32 because JS bit ops work on 32-bit ints.
  146. lastByte = result[byteLength - 1] = lastByte << shift >> shift & 0xff;
  147. }
  148. signBit = lastByte >> 7;
  149. signByte = signBit * 0xff;
  150. } else {
  151. signBit = 0;
  152. signByte = 0;
  153. } // Slice off any superfluous bytes, that is, ones that add no meaningful
  154. // bits (because the value would be the same if they were removed).
  155. while (byteLength > 1 && result[byteLength - 1] === signByte && (!signed || result[byteLength - 2] >> 7 === signBit)) {
  156. byteLength--;
  157. }
  158. result = bufs.resize(result, byteLength);
  159. return {
  160. value: result,
  161. nextIndex: index
  162. };
  163. }
  164. /*
  165. * Exported bindings
  166. */
  167. function encodeIntBuffer(buffer) {
  168. return encodeBufferCommon(buffer, true);
  169. }
  170. function decodeIntBuffer(encodedBuffer, index) {
  171. return decodeBufferCommon(encodedBuffer, index, true);
  172. }
  173. function encodeInt32(num) {
  174. var buf = bufs.alloc(4);
  175. buf.writeInt32LE(num, 0);
  176. var result = encodeIntBuffer(buf);
  177. bufs.free(buf);
  178. return result;
  179. }
  180. function decodeInt32(encodedBuffer, index) {
  181. var result = decodeIntBuffer(encodedBuffer, index);
  182. var parsed = bufs.readInt(result.value);
  183. var value = parsed.value;
  184. bufs.free(result.value);
  185. if (value < MIN_INT32 || value > MAX_INT32) {
  186. throw new Error("integer too large");
  187. }
  188. return {
  189. value: value,
  190. nextIndex: result.nextIndex
  191. };
  192. }
  193. function encodeInt64(num) {
  194. var buf = bufs.alloc(8);
  195. bufs.writeInt64(num, buf);
  196. var result = encodeIntBuffer(buf);
  197. bufs.free(buf);
  198. return result;
  199. }
  200. function decodeInt64(encodedBuffer, index) {
  201. var result = decodeIntBuffer(encodedBuffer, index);
  202. var value = _long.default.fromBytesLE(result.value, false);
  203. bufs.free(result.value);
  204. return {
  205. value: value,
  206. nextIndex: result.nextIndex,
  207. lossy: false
  208. };
  209. }
  210. function encodeUIntBuffer(buffer) {
  211. return encodeBufferCommon(buffer, false);
  212. }
  213. function decodeUIntBuffer(encodedBuffer, index) {
  214. return decodeBufferCommon(encodedBuffer, index, false);
  215. }
  216. function encodeUInt32(num) {
  217. var buf = bufs.alloc(4);
  218. buf.writeUInt32LE(num, 0);
  219. var result = encodeUIntBuffer(buf);
  220. bufs.free(buf);
  221. return result;
  222. }
  223. function decodeUInt32(encodedBuffer, index) {
  224. var result = decodeUIntBuffer(encodedBuffer, index);
  225. var parsed = bufs.readUInt(result.value);
  226. var value = parsed.value;
  227. bufs.free(result.value);
  228. if (value > MAX_UINT32) {
  229. throw new Error("integer too large");
  230. }
  231. return {
  232. value: value,
  233. nextIndex: result.nextIndex
  234. };
  235. }
  236. function encodeUInt64(num) {
  237. var buf = bufs.alloc(8);
  238. bufs.writeUInt64(num, buf);
  239. var result = encodeUIntBuffer(buf);
  240. bufs.free(buf);
  241. return result;
  242. }
  243. function decodeUInt64(encodedBuffer, index) {
  244. var result = decodeUIntBuffer(encodedBuffer, index);
  245. var value = _long.default.fromBytesLE(result.value, true);
  246. bufs.free(result.value);
  247. return {
  248. value: value,
  249. nextIndex: result.nextIndex,
  250. lossy: false
  251. };
  252. }
  253. var _default = {
  254. decodeInt32: decodeInt32,
  255. decodeInt64: decodeInt64,
  256. decodeIntBuffer: decodeIntBuffer,
  257. decodeUInt32: decodeUInt32,
  258. decodeUInt64: decodeUInt64,
  259. decodeUIntBuffer: decodeUIntBuffer,
  260. encodeInt32: encodeInt32,
  261. encodeInt64: encodeInt64,
  262. encodeIntBuffer: encodeIntBuffer,
  263. encodeUInt32: encodeUInt32,
  264. encodeUInt64: encodeUInt64,
  265. encodeUIntBuffer: encodeUIntBuffer
  266. };
  267. exports.default = _default;