@ac-kit/format-varint 0.2.7 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +48 -0
- package/README.md +10 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -1
- package/dist/leb128/_leb128.d.ts +22 -0
- package/dist/leb128/_leb128.d.ts.map +1 -0
- package/dist/leb128/_leb128.js +48 -0
- package/dist/leb128/_leb128.js.map +1 -0
- package/dist/leb128/decode-leb128.d.ts +40 -0
- package/dist/leb128/decode-leb128.d.ts.map +1 -0
- package/dist/leb128/decode-leb128.js +61 -0
- package/dist/leb128/decode-leb128.js.map +1 -0
- package/dist/leb128/encode-big-sleb128.d.ts +10 -0
- package/dist/leb128/encode-big-sleb128.d.ts.map +1 -0
- package/dist/leb128/encode-big-sleb128.js +25 -0
- package/dist/leb128/encode-big-sleb128.js.map +1 -0
- package/dist/leb128/encode-big-uleb128.d.ts +11 -0
- package/dist/leb128/encode-big-uleb128.d.ts.map +1 -0
- package/dist/leb128/encode-big-uleb128.js +29 -0
- package/dist/leb128/encode-big-uleb128.js.map +1 -0
- package/dist/leb128/encode-sleb128.d.ts +14 -0
- package/dist/leb128/encode-sleb128.d.ts.map +1 -0
- package/dist/leb128/encode-sleb128.js +20 -0
- package/dist/leb128/encode-sleb128.js.map +1 -0
- package/dist/leb128/encode-uleb128.d.ts +17 -0
- package/dist/leb128/encode-uleb128.d.ts.map +1 -0
- package/dist/leb128/encode-uleb128.js +23 -0
- package/dist/leb128/encode-uleb128.js.map +1 -0
- package/dist/leb128/leb128-codecs.d.ts +23 -0
- package/dist/leb128/leb128-codecs.d.ts.map +1 -0
- package/dist/leb128/leb128-codecs.js +53 -0
- package/dist/leb128/leb128-codecs.js.map +1 -0
- package/dist/leb128/leb128-read-options.d.ts +17 -0
- package/dist/leb128/leb128-read-options.d.ts.map +1 -0
- package/dist/leb128/leb128-read-options.js +2 -0
- package/dist/leb128/leb128-read-options.js.map +1 -0
- package/dist/leb128/read-big-sleb128.d.ts +20 -0
- package/dist/leb128/read-big-sleb128.d.ts.map +1 -0
- package/dist/leb128/read-big-sleb128.js +44 -0
- package/dist/leb128/read-big-sleb128.js.map +1 -0
- package/dist/leb128/read-big-uleb128.d.ts +19 -0
- package/dist/leb128/read-big-uleb128.d.ts.map +1 -0
- package/dist/leb128/read-big-uleb128.js +39 -0
- package/dist/leb128/read-big-uleb128.js.map +1 -0
- package/dist/leb128/read-sleb128.d.ts +26 -0
- package/dist/leb128/read-sleb128.d.ts.map +1 -0
- package/dist/leb128/read-sleb128.js +68 -0
- package/dist/leb128/read-sleb128.js.map +1 -0
- package/dist/leb128/read-uleb128.d.ts +25 -0
- package/dist/leb128/read-uleb128.d.ts.map +1 -0
- package/dist/leb128/read-uleb128.js +52 -0
- package/dist/leb128/read-uleb128.js.map +1 -0
- package/dist/leb128/sleb128-byte-length.d.ts +11 -0
- package/dist/leb128/sleb128-byte-length.d.ts.map +1 -0
- package/dist/leb128/sleb128-byte-length.js +25 -0
- package/dist/leb128/sleb128-byte-length.js.map +1 -0
- package/dist/leb128/uleb128-byte-length.d.ts +11 -0
- package/dist/leb128/uleb128-byte-length.d.ts.map +1 -0
- package/dist/leb128/uleb128-byte-length.js +24 -0
- package/dist/leb128/uleb128-byte-length.js.map +1 -0
- package/dist/leb128/write-sleb128.d.ts +13 -0
- package/dist/leb128/write-sleb128.d.ts.map +1 -0
- package/dist/leb128/write-sleb128.js +31 -0
- package/dist/leb128/write-sleb128.js.map +1 -0
- package/dist/leb128/write-uleb128.d.ts +13 -0
- package/dist/leb128/write-uleb128.d.ts.map +1 -0
- package/dist/leb128/write-uleb128.js +30 -0
- package/dist/leb128/write-uleb128.js.map +1 -0
- package/package.json +5 -4
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ByteReader } from "@ac-kit/core";
|
|
2
|
+
import type { Leb128ReadOptions } from "./leb128-read-options.js";
|
|
3
|
+
/**
|
|
4
|
+
* Reads one signed LEB128 from `reader` as a `bigint`, advancing it past the
|
|
5
|
+
* bytes consumed — the unbounded counterpart of {@link readSleb128}, for
|
|
6
|
+
* WebAssembly's `s64` and DWARF values beyond the safe integers. Prefer
|
|
7
|
+
* {@link readSleb128} where the value fits a `number`; `bigint` arithmetic
|
|
8
|
+
* allocates.
|
|
9
|
+
*
|
|
10
|
+
* Time complexity: O(n) in the bytes read.
|
|
11
|
+
*
|
|
12
|
+
* @returns The value.
|
|
13
|
+
* @throws {VarintIncompleteError} When the bytes run out while a continuation
|
|
14
|
+
* bit is still set.
|
|
15
|
+
* @throws {VarintMalformedError} When the encoding is overlong and `canonical`
|
|
16
|
+
* is set, or longer than `maxByteLength`.
|
|
17
|
+
* @throws {RangeError} When `maxByteLength` is not a positive integer.
|
|
18
|
+
*/
|
|
19
|
+
export declare function readBigSleb128(reader: ByteReader, options?: Leb128ReadOptions): bigint;
|
|
20
|
+
//# sourceMappingURL=read-big-sleb128.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read-big-sleb128.d.ts","sourceRoot":"","sources":["../../src/leb128/read-big-sleb128.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAW/C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAElE;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,cAAc,CAC7B,MAAM,EAAE,UAAU,EAClB,OAAO,CAAC,EAAE,iBAAiB,GACzB,MAAM,CA0BR"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { PAYLOAD_BITS, PAYLOAD_MASK } from "../_base128.js";
|
|
2
|
+
import { isLastByte_, isOverlongSigned_, maxByteLength_, overlong_, readGroupByte_, SIGN_BIT_, } from "./_leb128.js";
|
|
3
|
+
/**
|
|
4
|
+
* Reads one signed LEB128 from `reader` as a `bigint`, advancing it past the
|
|
5
|
+
* bytes consumed — the unbounded counterpart of {@link readSleb128}, for
|
|
6
|
+
* WebAssembly's `s64` and DWARF values beyond the safe integers. Prefer
|
|
7
|
+
* {@link readSleb128} where the value fits a `number`; `bigint` arithmetic
|
|
8
|
+
* allocates.
|
|
9
|
+
*
|
|
10
|
+
* Time complexity: O(n) in the bytes read.
|
|
11
|
+
*
|
|
12
|
+
* @returns The value.
|
|
13
|
+
* @throws {VarintIncompleteError} When the bytes run out while a continuation
|
|
14
|
+
* bit is still set.
|
|
15
|
+
* @throws {VarintMalformedError} When the encoding is overlong and `canonical`
|
|
16
|
+
* is set, or longer than `maxByteLength`.
|
|
17
|
+
* @throws {RangeError} When `maxByteLength` is not a positive integer.
|
|
18
|
+
*/
|
|
19
|
+
export function readBigSleb128(reader, options) {
|
|
20
|
+
const caller = "readBigSleb128";
|
|
21
|
+
const maxByteLength = maxByteLength_(caller, options);
|
|
22
|
+
let value = 0n;
|
|
23
|
+
let shift = 0n;
|
|
24
|
+
let count = 0;
|
|
25
|
+
let previous = 0;
|
|
26
|
+
let byte = 0;
|
|
27
|
+
do {
|
|
28
|
+
previous = byte;
|
|
29
|
+
byte = readGroupByte_(caller, reader, count, maxByteLength);
|
|
30
|
+
const group = byte & PAYLOAD_MASK;
|
|
31
|
+
if (group !== 0) {
|
|
32
|
+
value |= BigInt(group) << shift;
|
|
33
|
+
}
|
|
34
|
+
shift += PAYLOAD_BITS;
|
|
35
|
+
count++;
|
|
36
|
+
} while (!isLastByte_(byte));
|
|
37
|
+
if (options?.canonical === true &&
|
|
38
|
+
count > 1 &&
|
|
39
|
+
isOverlongSigned_(byte, previous)) {
|
|
40
|
+
throw overlong_(caller);
|
|
41
|
+
}
|
|
42
|
+
return (byte & SIGN_BIT_) === 0 ? value : value - (1n << shift);
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=read-big-sleb128.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read-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C,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,EAAE,IAAI,KAAK,CAAC,CAAC;AACjE,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ByteReader } from "@ac-kit/core";
|
|
2
|
+
import type { Leb128ReadOptions } from "./leb128-read-options.js";
|
|
3
|
+
/**
|
|
4
|
+
* Reads one unsigned LEB128 from `reader` as a `bigint`, advancing it past the
|
|
5
|
+
* bytes consumed — the unbounded counterpart of {@link readUleb128}, for
|
|
6
|
+
* WebAssembly's `u64` and DWARF values beyond 2⁵³. Prefer {@link readUleb128}
|
|
7
|
+
* where the value fits a `number`; `bigint` arithmetic allocates.
|
|
8
|
+
*
|
|
9
|
+
* Time complexity: O(n) in the bytes read.
|
|
10
|
+
*
|
|
11
|
+
* @returns The value, zero or greater.
|
|
12
|
+
* @throws {VarintIncompleteError} When the bytes run out while a continuation
|
|
13
|
+
* bit is still set.
|
|
14
|
+
* @throws {VarintMalformedError} When the encoding is overlong and `canonical`
|
|
15
|
+
* is set, or longer than `maxByteLength`.
|
|
16
|
+
* @throws {RangeError} When `maxByteLength` is not a positive integer.
|
|
17
|
+
*/
|
|
18
|
+
export declare function readBigUleb128(reader: ByteReader, options?: Leb128ReadOptions): bigint;
|
|
19
|
+
//# sourceMappingURL=read-big-uleb128.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read-big-uleb128.d.ts","sourceRoot":"","sources":["../../src/leb128/read-big-uleb128.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAS/C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAElE;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,cAAc,CAC7B,MAAM,EAAE,UAAU,EAClB,OAAO,CAAC,EAAE,iBAAiB,GACzB,MAAM,CAoBR"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { PAYLOAD_BITS, PAYLOAD_MASK } from "../_base128.js";
|
|
2
|
+
import { isLastByte_, maxByteLength_, overlong_, readGroupByte_, } from "./_leb128.js";
|
|
3
|
+
/**
|
|
4
|
+
* Reads one unsigned LEB128 from `reader` as a `bigint`, advancing it past the
|
|
5
|
+
* bytes consumed — the unbounded counterpart of {@link readUleb128}, for
|
|
6
|
+
* WebAssembly's `u64` and DWARF values beyond 2⁵³. Prefer {@link readUleb128}
|
|
7
|
+
* where the value fits a `number`; `bigint` arithmetic allocates.
|
|
8
|
+
*
|
|
9
|
+
* Time complexity: O(n) in the bytes read.
|
|
10
|
+
*
|
|
11
|
+
* @returns The value, zero or greater.
|
|
12
|
+
* @throws {VarintIncompleteError} When the bytes run out while a continuation
|
|
13
|
+
* bit is still set.
|
|
14
|
+
* @throws {VarintMalformedError} When the encoding is overlong and `canonical`
|
|
15
|
+
* is set, or longer than `maxByteLength`.
|
|
16
|
+
* @throws {RangeError} When `maxByteLength` is not a positive integer.
|
|
17
|
+
*/
|
|
18
|
+
export function readBigUleb128(reader, options) {
|
|
19
|
+
const caller = "readBigUleb128";
|
|
20
|
+
const maxByteLength = maxByteLength_(caller, options);
|
|
21
|
+
let value = 0n;
|
|
22
|
+
let shift = 0n;
|
|
23
|
+
let count = 0;
|
|
24
|
+
let byte;
|
|
25
|
+
do {
|
|
26
|
+
byte = readGroupByte_(caller, reader, count, maxByteLength);
|
|
27
|
+
const group = byte & PAYLOAD_MASK;
|
|
28
|
+
if (group !== 0) {
|
|
29
|
+
value |= BigInt(group) << shift;
|
|
30
|
+
}
|
|
31
|
+
shift += PAYLOAD_BITS;
|
|
32
|
+
count++;
|
|
33
|
+
} while (!isLastByte_(byte));
|
|
34
|
+
if (options?.canonical === true && count > 1 && byte === 0) {
|
|
35
|
+
throw overlong_(caller);
|
|
36
|
+
}
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=read-big-uleb128.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read-big-uleb128.js","sourceRoot":"","sources":["../../src/leb128/read-big-uleb128.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC5D,OAAO,EACN,WAAW,EACX,cAAc,EACd,SAAS,EACT,cAAc,GACd,MAAM,cAAc,CAAC;AAGtB;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,cAAc,CAC7B,MAAkB,EAClB,OAA2B;IAE3B,MAAM,MAAM,GAAG,gBAAgB,CAAC;IAChC,MAAM,aAAa,GAAG,cAAc,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACtD,IAAI,KAAK,GAAG,EAAE,CAAC;IACf,IAAI,KAAK,GAAG,EAAE,CAAC;IACf,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,IAAI,IAAY,CAAC;IACjB,GAAG,CAAC;QACH,IAAI,GAAG,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,aAAa,CAAC,CAAC;QAC5D,MAAM,KAAK,GAAG,IAAI,GAAG,YAAY,CAAC;QAClC,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;YACjB,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC;QACjC,CAAC;QACD,KAAK,IAAI,YAAY,CAAC;QACtB,KAAK,EAAE,CAAC;IACT,CAAC,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE;IAC7B,IAAI,OAAO,EAAE,SAAS,KAAK,IAAI,IAAI,KAAK,GAAG,CAAC,IAAI,IAAI,KAAK,CAAC,EAAE,CAAC;QAC5D,MAAM,SAAS,CAAC,MAAM,CAAC,CAAC;IACzB,CAAC;IACD,OAAO,KAAK,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ByteReader } from "@ac-kit/core";
|
|
2
|
+
import type { Leb128ReadOptions } from "./leb128-read-options.js";
|
|
3
|
+
/**
|
|
4
|
+
* Reads one signed LEB128 from `reader` — two's complement, base 128, least
|
|
5
|
+
* significant group first, the sign in bit 6 of the final group — advancing it
|
|
6
|
+
* past the bytes consumed. The encoding of DWARF's `SLEB128` and WebAssembly's
|
|
7
|
+
* `sN`. Protocol Buffers' `sint` fields are not this: they zigzag-map to an
|
|
8
|
+
* unsigned value read with {@link readUleb128}.
|
|
9
|
+
*
|
|
10
|
+
* Groups that only extend the sign are read unless `canonical` is set; they
|
|
11
|
+
* count against `maxByteLength`. Allocates nothing.
|
|
12
|
+
*
|
|
13
|
+
* Time complexity: O(n) in the bytes read.
|
|
14
|
+
*
|
|
15
|
+
* @returns The value, from `-Number.MAX_SAFE_INTEGER` to
|
|
16
|
+
* `Number.MAX_SAFE_INTEGER`.
|
|
17
|
+
* @throws {VarintIncompleteError} When the bytes run out while a continuation
|
|
18
|
+
* bit is still set.
|
|
19
|
+
* @throws {VarintMalformedError} When the encoding is overlong and `canonical`
|
|
20
|
+
* is set, or longer than `maxByteLength`.
|
|
21
|
+
* @throws {RangeError} When the value is outside the safe integers; it is
|
|
22
|
+
* well-formed, and {@link readBigSleb128} reads it. Also when `maxByteLength`
|
|
23
|
+
* is not a positive integer.
|
|
24
|
+
*/
|
|
25
|
+
export declare function readSleb128(reader: ByteReader, options?: Leb128ReadOptions): number;
|
|
26
|
+
//# sourceMappingURL=read-sleb128.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read-sleb128.d.ts","sourceRoot":"","sources":["../../src/leb128/read-sleb128.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAY/C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAElE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,WAAW,CAC1B,MAAM,EAAE,UAAU,EAClB,OAAO,CAAC,EAAE,iBAAiB,GACzB,MAAM,CAuCR"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { PAYLOAD_MASK, RADIX } from "../_base128.js";
|
|
2
|
+
import { EIGHTH_GROUP_WEIGHT_, isLastByte_, isOverlongSigned_, maxByteLength_, overlong_, readGroupByte_, SIGN_BIT_, } from "./_leb128.js";
|
|
3
|
+
/**
|
|
4
|
+
* Reads one signed LEB128 from `reader` — two's complement, base 128, least
|
|
5
|
+
* significant group first, the sign in bit 6 of the final group — advancing it
|
|
6
|
+
* past the bytes consumed. The encoding of DWARF's `SLEB128` and WebAssembly's
|
|
7
|
+
* `sN`. Protocol Buffers' `sint` fields are not this: they zigzag-map to an
|
|
8
|
+
* unsigned value read with {@link readUleb128}.
|
|
9
|
+
*
|
|
10
|
+
* Groups that only extend the sign are read unless `canonical` is set; they
|
|
11
|
+
* count against `maxByteLength`. Allocates nothing.
|
|
12
|
+
*
|
|
13
|
+
* Time complexity: O(n) in the bytes read.
|
|
14
|
+
*
|
|
15
|
+
* @returns The value, from `-Number.MAX_SAFE_INTEGER` to
|
|
16
|
+
* `Number.MAX_SAFE_INTEGER`.
|
|
17
|
+
* @throws {VarintIncompleteError} When the bytes run out while a continuation
|
|
18
|
+
* bit is still set.
|
|
19
|
+
* @throws {VarintMalformedError} When the encoding is overlong and `canonical`
|
|
20
|
+
* is set, or longer than `maxByteLength`.
|
|
21
|
+
* @throws {RangeError} When the value is outside the safe integers; it is
|
|
22
|
+
* well-formed, and {@link readBigSleb128} reads it. Also when `maxByteLength`
|
|
23
|
+
* is not a positive integer.
|
|
24
|
+
*/
|
|
25
|
+
export function readSleb128(reader, options) {
|
|
26
|
+
const caller = "readSleb128";
|
|
27
|
+
const maxByteLength = maxByteLength_(caller, options);
|
|
28
|
+
let low = 0;
|
|
29
|
+
let weight = 1;
|
|
30
|
+
let top = 0;
|
|
31
|
+
let count = 0;
|
|
32
|
+
let previous = 0;
|
|
33
|
+
let byte = 0;
|
|
34
|
+
do {
|
|
35
|
+
previous = byte;
|
|
36
|
+
byte = readGroupByte_(caller, reader, count, maxByteLength);
|
|
37
|
+
const group = byte & PAYLOAD_MASK;
|
|
38
|
+
if (count < 7) {
|
|
39
|
+
low += group * weight;
|
|
40
|
+
weight *= RADIX;
|
|
41
|
+
}
|
|
42
|
+
else if (count === 7) {
|
|
43
|
+
top = group;
|
|
44
|
+
}
|
|
45
|
+
else if (group !== ((top & SIGN_BIT_) === 0 ? 0 : PAYLOAD_MASK)) {
|
|
46
|
+
throw outOfRange_(caller);
|
|
47
|
+
}
|
|
48
|
+
count++;
|
|
49
|
+
} while (!isLastByte_(byte));
|
|
50
|
+
if (options?.canonical === true &&
|
|
51
|
+
count > 1 &&
|
|
52
|
+
isOverlongSigned_(byte, previous & PAYLOAD_MASK)) {
|
|
53
|
+
throw overlong_(caller);
|
|
54
|
+
}
|
|
55
|
+
if (count <= 7) {
|
|
56
|
+
return (byte & SIGN_BIT_) === 0 ? low : low - weight;
|
|
57
|
+
}
|
|
58
|
+
// The eighth group as a signed seven-bit number; later groups only extend it.
|
|
59
|
+
const high = (top & SIGN_BIT_) === 0 ? top : top - RADIX;
|
|
60
|
+
if (high > 15 || high < -16 || (high === -16 && low === 0)) {
|
|
61
|
+
throw outOfRange_(caller);
|
|
62
|
+
}
|
|
63
|
+
return high * EIGHTH_GROUP_WEIGHT_ + low;
|
|
64
|
+
}
|
|
65
|
+
function outOfRange_(caller) {
|
|
66
|
+
return new RangeError(`${caller}: value is outside the safe integers; use readBigSleb128 for a wider value`);
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=read-sleb128.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read-sleb128.js","sourceRoot":"","sources":["../../src/leb128/read-sleb128.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EACN,oBAAoB,EACpB,WAAW,EACX,iBAAiB,EACjB,cAAc,EACd,SAAS,EACT,cAAc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C,CAAC;QACF,MAAM,SAAS,CAAC,MAAM,CAAC,CAAC;IACzB,CAAC;IACD,IAAI,KAAK,IAAI,CAAC,EAAE,CAAC;QAChB,OAAO,CAAC,IAAI,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,MAAM,CAAC;IACtD,CAAC;IACD,8EAA8E;IAC9E,MAAM,IAAI,GAAG,CAAC,GAAG,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC;IACzD,IAAI,IAAI,GAAG,EAAE,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,CAAC,IAAI,KAAK,CAAC,EAAE,IAAI,GAAG,KAAK,CAAC,CAAC,EAAE,CAAC;QAC5D,MAAM,WAAW,CAAC,MAAM,CAAC,CAAC;IAC3B,CAAC;IACD,OAAO,IAAI,GAAG,oBAAoB,GAAG,GAAG,CAAC;AAC1C,CAAC;AAED,SAAS,WAAW,CAAC,MAAc;IAClC,OAAO,IAAI,UAAU,CACpB,GAAG,MAAM,4EAA4E,CACrF,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { ByteReader } from "@ac-kit/core";
|
|
2
|
+
import type { Leb128ReadOptions } from "./leb128-read-options.js";
|
|
3
|
+
/**
|
|
4
|
+
* Reads one unsigned LEB128 from `reader` — base 128, least significant group
|
|
5
|
+
* first, the continuation bit set on every byte but the last — advancing it
|
|
6
|
+
* past the bytes consumed. The encoding of DWARF's `ULEB128`, WebAssembly's
|
|
7
|
+
* `uN` and Protocol Buffers' varints.
|
|
8
|
+
*
|
|
9
|
+
* Padding groups of zero after the value are read unless `canonical` is set,
|
|
10
|
+
* since a fixed-width writer produces them; they count against `maxByteLength`.
|
|
11
|
+
* Allocates nothing.
|
|
12
|
+
*
|
|
13
|
+
* Time complexity: O(n) in the bytes read.
|
|
14
|
+
*
|
|
15
|
+
* @returns The value, from 0 to `Number.MAX_SAFE_INTEGER`.
|
|
16
|
+
* @throws {VarintIncompleteError} When the bytes run out while a continuation
|
|
17
|
+
* bit is still set.
|
|
18
|
+
* @throws {VarintMalformedError} When the encoding is overlong and `canonical`
|
|
19
|
+
* is set, or longer than `maxByteLength`.
|
|
20
|
+
* @throws {RangeError} When the value is too large for a `number`; it is
|
|
21
|
+
* well-formed, and {@link readBigUleb128} reads it. Also when `maxByteLength`
|
|
22
|
+
* is not a positive integer.
|
|
23
|
+
*/
|
|
24
|
+
export declare function readUleb128(reader: ByteReader, options?: Leb128ReadOptions): number;
|
|
25
|
+
//# sourceMappingURL=read-uleb128.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read-uleb128.d.ts","sourceRoot":"","sources":["../../src/leb128/read-uleb128.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAU/C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAElE;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,WAAW,CAC1B,MAAM,EAAE,UAAU,EAClB,OAAO,CAAC,EAAE,iBAAiB,GACzB,MAAM,CA2BR"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { PAYLOAD_MASK, RADIX } from "../_base128.js";
|
|
2
|
+
import { EIGHTH_GROUP_WEIGHT_, isLastByte_, maxByteLength_, overlong_, readGroupByte_, } from "./_leb128.js";
|
|
3
|
+
/**
|
|
4
|
+
* Reads one unsigned LEB128 from `reader` — base 128, least significant group
|
|
5
|
+
* first, the continuation bit set on every byte but the last — advancing it
|
|
6
|
+
* past the bytes consumed. The encoding of DWARF's `ULEB128`, WebAssembly's
|
|
7
|
+
* `uN` and Protocol Buffers' varints.
|
|
8
|
+
*
|
|
9
|
+
* Padding groups of zero after the value are read unless `canonical` is set,
|
|
10
|
+
* since a fixed-width writer produces them; they count against `maxByteLength`.
|
|
11
|
+
* Allocates nothing.
|
|
12
|
+
*
|
|
13
|
+
* Time complexity: O(n) in the bytes read.
|
|
14
|
+
*
|
|
15
|
+
* @returns The value, from 0 to `Number.MAX_SAFE_INTEGER`.
|
|
16
|
+
* @throws {VarintIncompleteError} When the bytes run out while a continuation
|
|
17
|
+
* bit is still set.
|
|
18
|
+
* @throws {VarintMalformedError} When the encoding is overlong and `canonical`
|
|
19
|
+
* is set, or longer than `maxByteLength`.
|
|
20
|
+
* @throws {RangeError} When the value is too large for a `number`; it is
|
|
21
|
+
* well-formed, and {@link readBigUleb128} reads it. Also when `maxByteLength`
|
|
22
|
+
* is not a positive integer.
|
|
23
|
+
*/
|
|
24
|
+
export function readUleb128(reader, options) {
|
|
25
|
+
const caller = "readUleb128";
|
|
26
|
+
const maxByteLength = maxByteLength_(caller, options);
|
|
27
|
+
let low = 0;
|
|
28
|
+
let weight = 1;
|
|
29
|
+
let high = 0;
|
|
30
|
+
let count = 0;
|
|
31
|
+
let byte;
|
|
32
|
+
do {
|
|
33
|
+
byte = readGroupByte_(caller, reader, count, maxByteLength);
|
|
34
|
+
const group = byte & PAYLOAD_MASK;
|
|
35
|
+
if (count < 7) {
|
|
36
|
+
low += group * weight;
|
|
37
|
+
weight *= RADIX;
|
|
38
|
+
}
|
|
39
|
+
else if ((count === 7 && group > 15) || (count > 7 && group !== 0)) {
|
|
40
|
+
throw new RangeError(`${caller}: value exceeds Number.MAX_SAFE_INTEGER; use readBigUleb128 for a wider value`);
|
|
41
|
+
}
|
|
42
|
+
else if (count === 7) {
|
|
43
|
+
high = group;
|
|
44
|
+
}
|
|
45
|
+
count++;
|
|
46
|
+
} while (!isLastByte_(byte));
|
|
47
|
+
if (options?.canonical === true && count > 1 && byte === 0) {
|
|
48
|
+
throw overlong_(caller);
|
|
49
|
+
}
|
|
50
|
+
return high * EIGHTH_GROUP_WEIGHT_ + low;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=read-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CAAC,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE;IAC7B,IAAI,OAAO,EAAE,SAAS,KAAK,IAAI,IAAI,KAAK,GAAG,CAAC,IAAI,IAAI,KAAK,CAAC,EAAE,CAAC;QAC5D,MAAM,SAAS,CAAC,MAAM,CAAC,CAAC;IACzB,CAAC;IACD,OAAO,IAAI,GAAG,oBAAoB,GAAG,GAAG,CAAC;AAC1C,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How many bytes {@link encodeSleb128} produces for `value`: the fewest that
|
|
3
|
+
* hold it with its sign in bit 6 of the final group, at least 1. Values from
|
|
4
|
+
* -64 to 63 take one byte.
|
|
5
|
+
*
|
|
6
|
+
* Time complexity: O(log₁₂₈ |value|), at most 8 iterations.
|
|
7
|
+
*
|
|
8
|
+
* @throws {RangeError} When `value` is fractional or not a safe integer.
|
|
9
|
+
*/
|
|
10
|
+
export declare function sleb128ByteLength(value: number): number;
|
|
11
|
+
//# sourceMappingURL=sleb128-byte-length.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sleb128-byte-length.d.ts","sourceRoot":"","sources":["../../src/leb128/sleb128-byte-length.ts"],"names":[],"mappings":"AAIA;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAcvD"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { describeValue } from "@ac-kit/core";
|
|
2
|
+
import { RADIX } from "../_base128.js";
|
|
3
|
+
/**
|
|
4
|
+
* How many bytes {@link encodeSleb128} produces for `value`: the fewest that
|
|
5
|
+
* hold it with its sign in bit 6 of the final group, at least 1. Values from
|
|
6
|
+
* -64 to 63 take one byte.
|
|
7
|
+
*
|
|
8
|
+
* Time complexity: O(log₁₂₈ |value|), at most 8 iterations.
|
|
9
|
+
*
|
|
10
|
+
* @throws {RangeError} When `value` is fractional or not a safe integer.
|
|
11
|
+
*/
|
|
12
|
+
export function sleb128ByteLength(value) {
|
|
13
|
+
if (!Number.isSafeInteger(value)) {
|
|
14
|
+
throw new RangeError(`sleb128ByteLength: value must be a safe integer, got ${describeValue(value)}`);
|
|
15
|
+
}
|
|
16
|
+
// A group of seven bits holds -64 to 63; each further one multiplies the range by 128.
|
|
17
|
+
let length = 1;
|
|
18
|
+
let limit = RADIX / 2;
|
|
19
|
+
while (value >= limit || value < -limit) {
|
|
20
|
+
limit *= RADIX;
|
|
21
|
+
length++;
|
|
22
|
+
}
|
|
23
|
+
return length;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=sleb128-byte-length.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sleb128-byte-length.js","sourceRoot":"","sources":["../../src/leb128/sleb128-byte-length.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAEvC;;;;;;;;GAQG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAa;IAC9C,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC;QAClC,MAAM,IAAI,UAAU,CACnB,wDAAwD,aAAa,CAAC,KAAK,CAAC,EAAE,CAC9E,CAAC;IACH,CAAC;IACD,uFAAuF;IACvF,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,IAAI,KAAK,GAAG,KAAK,GAAG,CAAC,CAAC;IACtB,OAAO,KAAK,IAAI,KAAK,IAAI,KAAK,GAAG,CAAC,KAAK,EAAE,CAAC;QACzC,KAAK,IAAI,KAAK,CAAC;QACf,MAAM,EAAE,CAAC;IACV,CAAC;IACD,OAAO,MAAM,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How many bytes {@link encodeUleb128} produces for `value`: the fewest the
|
|
3
|
+
* value needs, at least 1.
|
|
4
|
+
*
|
|
5
|
+
* Time complexity: O(log₁₂₈ value), at most 8 iterations.
|
|
6
|
+
*
|
|
7
|
+
* @throws {RangeError} When `value` is negative, fractional, or beyond
|
|
8
|
+
* `Number.MAX_SAFE_INTEGER`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function uleb128ByteLength(value: number): number;
|
|
11
|
+
//# sourceMappingURL=uleb128-byte-length.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uleb128-byte-length.d.ts","sourceRoot":"","sources":["../../src/leb128/uleb128-byte-length.ts"],"names":[],"mappings":"AAIA;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAavD"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { describeValue } from "@ac-kit/core";
|
|
2
|
+
import { RADIX } from "../_base128.js";
|
|
3
|
+
/**
|
|
4
|
+
* How many bytes {@link encodeUleb128} produces for `value`: the fewest the
|
|
5
|
+
* value needs, at least 1.
|
|
6
|
+
*
|
|
7
|
+
* Time complexity: O(log₁₂₈ value), at most 8 iterations.
|
|
8
|
+
*
|
|
9
|
+
* @throws {RangeError} When `value` is negative, fractional, or beyond
|
|
10
|
+
* `Number.MAX_SAFE_INTEGER`.
|
|
11
|
+
*/
|
|
12
|
+
export function uleb128ByteLength(value) {
|
|
13
|
+
if (!Number.isSafeInteger(value) || value < 0) {
|
|
14
|
+
throw new RangeError(`uleb128ByteLength: value must be a non-negative safe integer, got ${describeValue(value)}`);
|
|
15
|
+
}
|
|
16
|
+
let length = 1;
|
|
17
|
+
let remaining = value;
|
|
18
|
+
while (remaining >= RADIX) {
|
|
19
|
+
remaining = Math.floor(remaining / RADIX);
|
|
20
|
+
length++;
|
|
21
|
+
}
|
|
22
|
+
return length;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=uleb128-byte-length.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uleb128-byte-length.js","sourceRoot":"","sources":["../../src/leb128/uleb128-byte-length.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAEvC;;;;;;;;GAQG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAa;IAC9C,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QAC/C,MAAM,IAAI,UAAU,CACnB,qEAAqE,aAAa,CAAC,KAAK,CAAC,EAAE,CAC3F,CAAC;IACH,CAAC;IACD,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,IAAI,SAAS,GAAG,KAAK,CAAC;IACtB,OAAO,SAAS,IAAI,KAAK,EAAE,CAAC;QAC3B,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,GAAG,KAAK,CAAC,CAAC;QAC1C,MAAM,EAAE,CAAC;IACV,CAAC;IACD,OAAO,MAAM,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writes `value` into `target` at `offset` as a signed LEB128, least
|
|
3
|
+
* significant group first, in two's complement — the in-place counterpart of
|
|
4
|
+
* {@link encodeSleb128}.
|
|
5
|
+
*
|
|
6
|
+
* @param target - Destination, which must hold {@link sleb128ByteLength} bytes
|
|
7
|
+
* from `offset`.
|
|
8
|
+
* @returns The offset just past the bytes written.
|
|
9
|
+
* @throws {RangeError} When `value` is not a safe integer, or `target` is too
|
|
10
|
+
* short.
|
|
11
|
+
*/
|
|
12
|
+
export declare function writeSleb128(target: Uint8Array, offset: number, value: number): number;
|
|
13
|
+
//# sourceMappingURL=write-sleb128.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"write-sleb128.d.ts","sourceRoot":"","sources":["../../src/leb128/write-sleb128.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAC3B,MAAM,EAAE,UAAU,EAClB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,GACX,MAAM,CAoBR"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { CONTINUATION_BIT, RADIX } from "../_base128.js";
|
|
2
|
+
import { sleb128ByteLength } from "./sleb128-byte-length.js";
|
|
3
|
+
/**
|
|
4
|
+
* Writes `value` into `target` at `offset` as a signed LEB128, least
|
|
5
|
+
* significant group first, in two's complement — the in-place counterpart of
|
|
6
|
+
* {@link encodeSleb128}.
|
|
7
|
+
*
|
|
8
|
+
* @param target - Destination, which must hold {@link sleb128ByteLength} bytes
|
|
9
|
+
* from `offset`.
|
|
10
|
+
* @returns The offset just past the bytes written.
|
|
11
|
+
* @throws {RangeError} When `value` is not a safe integer, or `target` is too
|
|
12
|
+
* short.
|
|
13
|
+
*/
|
|
14
|
+
export function writeSleb128(target, offset, value) {
|
|
15
|
+
const length = sleb128ByteLength(value);
|
|
16
|
+
if (!Number.isSafeInteger(offset) ||
|
|
17
|
+
offset < 0 ||
|
|
18
|
+
offset + length > target.length) {
|
|
19
|
+
throw new RangeError(`writeSleb128: ${length} bytes at offset ${offset} do not fit in ${target.length}`);
|
|
20
|
+
}
|
|
21
|
+
let remaining = value;
|
|
22
|
+
for (let index = 0; index < length; index++) {
|
|
23
|
+
// The low seven bits in two's complement; the subtraction keeps the rest exact.
|
|
24
|
+
const group = ((remaining % RADIX) + RADIX) % RADIX;
|
|
25
|
+
remaining = (remaining - group) / RADIX;
|
|
26
|
+
target[offset + index] =
|
|
27
|
+
index === length - 1 ? group : group | CONTINUATION_BIT;
|
|
28
|
+
}
|
|
29
|
+
return offset + length;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=write-sleb128.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"write-sleb128.js","sourceRoot":"","sources":["../../src/leb128/write-sleb128.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAE7D;;;;;;;;;;GAUG;AACH,MAAM,UAAU,YAAY,CAC3B,MAAkB,EAClB,MAAc,EACd,KAAa;IAEb,MAAM,MAAM,GAAG,iBAAiB,CAAC,KAAK,CAAC,CAAC;IACxC,IACC,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC;QAC7B,MAAM,GAAG,CAAC;QACV,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC,MAAM,EAC9B,CAAC;QACF,MAAM,IAAI,UAAU,CACnB,iBAAiB,MAAM,oBAAoB,MAAM,kBAAkB,MAAM,CAAC,MAAM,EAAE,CAClF,CAAC;IACH,CAAC;IACD,IAAI,SAAS,GAAG,KAAK,CAAC;IACtB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QAC7C,gFAAgF;QAChF,MAAM,KAAK,GAAG,CAAC,CAAC,SAAS,GAAG,KAAK,CAAC,GAAG,KAAK,CAAC,GAAG,KAAK,CAAC;QACpD,SAAS,GAAG,CAAC,SAAS,GAAG,KAAK,CAAC,GAAG,KAAK,CAAC;QACxC,MAAM,CAAC,MAAM,GAAG,KAAK,CAAC;YACrB,KAAK,KAAK,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,GAAG,gBAAgB,CAAC;IAC1D,CAAC;IACD,OAAO,MAAM,GAAG,MAAM,CAAC;AACxB,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writes `value` into `target` at `offset` as an unsigned LEB128, least
|
|
3
|
+
* significant group first — the in-place counterpart of {@link encodeUleb128},
|
|
4
|
+
* for a caller assembling several values into one buffer.
|
|
5
|
+
*
|
|
6
|
+
* @param target - Destination, which must hold {@link uleb128ByteLength} bytes
|
|
7
|
+
* from `offset`.
|
|
8
|
+
* @returns The offset just past the bytes written.
|
|
9
|
+
* @throws {RangeError} When `value` is not a non-negative safe integer, or
|
|
10
|
+
* `target` is too short.
|
|
11
|
+
*/
|
|
12
|
+
export declare function writeUleb128(target: Uint8Array, offset: number, value: number): number;
|
|
13
|
+
//# sourceMappingURL=write-uleb128.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"write-uleb128.d.ts","sourceRoot":"","sources":["../../src/leb128/write-uleb128.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAC3B,MAAM,EAAE,UAAU,EAClB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,GACX,MAAM,CAmBR"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { CONTINUATION_BIT, RADIX } from "../_base128.js";
|
|
2
|
+
import { uleb128ByteLength } from "./uleb128-byte-length.js";
|
|
3
|
+
/**
|
|
4
|
+
* Writes `value` into `target` at `offset` as an unsigned LEB128, least
|
|
5
|
+
* significant group first — the in-place counterpart of {@link encodeUleb128},
|
|
6
|
+
* for a caller assembling several values into one buffer.
|
|
7
|
+
*
|
|
8
|
+
* @param target - Destination, which must hold {@link uleb128ByteLength} bytes
|
|
9
|
+
* from `offset`.
|
|
10
|
+
* @returns The offset just past the bytes written.
|
|
11
|
+
* @throws {RangeError} When `value` is not a non-negative safe integer, or
|
|
12
|
+
* `target` is too short.
|
|
13
|
+
*/
|
|
14
|
+
export function writeUleb128(target, offset, value) {
|
|
15
|
+
const length = uleb128ByteLength(value);
|
|
16
|
+
if (!Number.isSafeInteger(offset) ||
|
|
17
|
+
offset < 0 ||
|
|
18
|
+
offset + length > target.length) {
|
|
19
|
+
throw new RangeError(`writeUleb128: ${length} bytes at offset ${offset} do not fit in ${target.length}`);
|
|
20
|
+
}
|
|
21
|
+
let remaining = value;
|
|
22
|
+
for (let index = 0; index < length; index++) {
|
|
23
|
+
const group = remaining % RADIX;
|
|
24
|
+
remaining = Math.floor(remaining / RADIX);
|
|
25
|
+
target[offset + index] =
|
|
26
|
+
index === length - 1 ? group : group | CONTINUATION_BIT;
|
|
27
|
+
}
|
|
28
|
+
return offset + length;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=write-uleb128.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"write-uleb128.js","sourceRoot":"","sources":["../../src/leb128/write-uleb128.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAE7D;;;;;;;;;;GAUG;AACH,MAAM,UAAU,YAAY,CAC3B,MAAkB,EAClB,MAAc,EACd,KAAa;IAEb,MAAM,MAAM,GAAG,iBAAiB,CAAC,KAAK,CAAC,CAAC;IACxC,IACC,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC;QAC7B,MAAM,GAAG,CAAC;QACV,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC,MAAM,EAC9B,CAAC;QACF,MAAM,IAAI,UAAU,CACnB,iBAAiB,MAAM,oBAAoB,MAAM,kBAAkB,MAAM,CAAC,MAAM,EAAE,CAClF,CAAC;IACH,CAAC;IACD,IAAI,SAAS,GAAG,KAAK,CAAC;IACtB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QAC7C,MAAM,KAAK,GAAG,SAAS,GAAG,KAAK,CAAC;QAChC,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,GAAG,KAAK,CAAC,CAAC;QAC1C,MAAM,CAAC,MAAM,GAAG,KAAK,CAAC;YACrB,KAAK,KAAK,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,GAAG,gBAAgB,CAAC;IAC1D,CAAC;IACD,OAAO,MAAM,GAAG,MAAM,CAAC;AACxB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ac-kit/format-varint",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Variable-length integer encodings: LEB128, VLQ and the other self-delimiting integer formats.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"encoding",
|
|
@@ -45,13 +45,14 @@
|
|
|
45
45
|
"prepack": "yarn build"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@ac-kit/core": "^0.
|
|
49
|
-
"@ac-kit/format-core": "^0.
|
|
48
|
+
"@ac-kit/core": "^0.14.0",
|
|
49
|
+
"@ac-kit/format-core": "^0.5.1"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
+
"@ac-kit/math-random": "0.4.2",
|
|
52
53
|
"@ac-kit/tsconfig": "0.1.0",
|
|
53
54
|
"@ac-kit/typedoc-config": "0.1.0",
|
|
54
|
-
"@ac-kit/vitest-config": "0.
|
|
55
|
+
"@ac-kit/vitest-config": "0.3.0",
|
|
55
56
|
"@types/node": "^26.5.1",
|
|
56
57
|
"@typescript/native": "typescript@7.0.2",
|
|
57
58
|
"rimraf": "6.1.4",
|