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.

leb.js 7.5KB

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