source: trip-planner-front/node_modules/@webassemblyjs/leb128/lib/leb.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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