leb.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 = new Uint8Array(4);
  169. buf[0] = num & 0xff;
  170. buf[1] = num >> 8 & 0xff;
  171. buf[2] = num >> 16 & 0xff;
  172. buf[3] = num >> 24 & 0xff;
  173. var result = encodeIntBuffer(buf);
  174. return result;
  175. }
  176. function decodeInt32(encodedBuffer, index) {
  177. var result = decodeIntBuffer(encodedBuffer, index);
  178. var parsed = bufs.readInt(result.value);
  179. var value = parsed.value;
  180. bufs.free(result.value);
  181. if (value < MIN_INT32 || value > MAX_INT32) {
  182. throw new Error("integer too large");
  183. }
  184. return {
  185. value: value,
  186. nextIndex: result.nextIndex
  187. };
  188. }
  189. function encodeInt64(num) {
  190. var buf = bufs.alloc(8);
  191. bufs.writeInt64(num, buf);
  192. var result = encodeIntBuffer(buf);
  193. bufs.free(buf);
  194. return result;
  195. }
  196. function decodeInt64(encodedBuffer, index) {
  197. var result = decodeIntBuffer(encodedBuffer, index); // sign-extend if necessary
  198. var length = result.value.length;
  199. if (result.value[length - 1] >> 7) {
  200. result.value = bufs.resize(result.value, 8);
  201. result.value.fill(255, length);
  202. }
  203. var value = Long.fromBytesLE(result.value, false);
  204. bufs.free(result.value);
  205. return {
  206. value: value,
  207. nextIndex: result.nextIndex,
  208. lossy: false
  209. };
  210. }
  211. function encodeUIntBuffer(buffer) {
  212. return encodeBufferCommon(buffer, false);
  213. }
  214. function decodeUIntBuffer(encodedBuffer, index) {
  215. return decodeBufferCommon(encodedBuffer, index, false);
  216. }
  217. function encodeUInt32(num) {
  218. var buf = new Uint8Array(4);
  219. buf[0] = num & 0xff;
  220. buf[1] = num >> 8 & 0xff;
  221. buf[2] = num >> 16 & 0xff;
  222. buf[3] = num >> 24 & 0xff;
  223. var result = encodeUIntBuffer(buf);
  224. return result;
  225. }
  226. function decodeUInt32(encodedBuffer, index) {
  227. var result = decodeUIntBuffer(encodedBuffer, index);
  228. var parsed = bufs.readUInt(result.value);
  229. var value = parsed.value;
  230. bufs.free(result.value);
  231. if (value > MAX_UINT32) {
  232. throw new Error("integer too large");
  233. }
  234. return {
  235. value: value,
  236. nextIndex: result.nextIndex
  237. };
  238. }
  239. function encodeUInt64(num) {
  240. var buf = bufs.alloc(8);
  241. bufs.writeUInt64(num, buf);
  242. var result = encodeUIntBuffer(buf);
  243. bufs.free(buf);
  244. return result;
  245. }
  246. function decodeUInt64(encodedBuffer, index) {
  247. var result = decodeUIntBuffer(encodedBuffer, index);
  248. var value = Long.fromBytesLE(result.value, true);
  249. bufs.free(result.value);
  250. return {
  251. value: value,
  252. nextIndex: result.nextIndex,
  253. lossy: false
  254. };
  255. }
  256. export default {
  257. decodeInt32: decodeInt32,
  258. decodeInt64: decodeInt64,
  259. decodeIntBuffer: decodeIntBuffer,
  260. decodeUInt32: decodeUInt32,
  261. decodeUInt64: decodeUInt64,
  262. decodeUIntBuffer: decodeUIntBuffer,
  263. encodeInt32: encodeInt32,
  264. encodeInt64: encodeInt64,
  265. encodeIntBuffer: encodeIntBuffer,
  266. encodeUInt32: encodeUInt32,
  267. encodeUInt64: encodeUInt64,
  268. encodeUIntBuffer: encodeUIntBuffer
  269. };