leb.js 9.5 KB

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