UNPKG

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