@ac-kit/format-varint 0.1.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/LICENSE +28 -0
- package/README.md +59 -0
- package/dist/_base128.d.ts +19 -0
- package/dist/_base128.d.ts.map +1 -0
- package/dist/_base128.js +19 -0
- package/dist/_base128.js.map +1 -0
- package/dist/errors.d.ts +24 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +33 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/vlq/big-vlq-decoder.d.ts +14 -0
- package/dist/vlq/big-vlq-decoder.d.ts.map +1 -0
- package/dist/vlq/big-vlq-decoder.js +34 -0
- package/dist/vlq/big-vlq-decoder.js.map +1 -0
- package/dist/vlq/big-vlq-encoder.d.ts +9 -0
- package/dist/vlq/big-vlq-encoder.d.ts.map +1 -0
- package/dist/vlq/big-vlq-encoder.js +11 -0
- package/dist/vlq/big-vlq-encoder.js.map +1 -0
- package/dist/vlq/decode-big-vlq.d.ts +12 -0
- package/dist/vlq/decode-big-vlq.d.ts.map +1 -0
- package/dist/vlq/decode-big-vlq.js +22 -0
- package/dist/vlq/decode-big-vlq.js.map +1 -0
- package/dist/vlq/decode-vlq.d.ts +15 -0
- package/dist/vlq/decode-vlq.d.ts.map +1 -0
- package/dist/vlq/decode-vlq.js +25 -0
- package/dist/vlq/decode-vlq.js.map +1 -0
- package/dist/vlq/encode-big-vlq.d.ts +18 -0
- package/dist/vlq/encode-big-vlq.d.ts.map +1 -0
- package/dist/vlq/encode-big-vlq.js +37 -0
- package/dist/vlq/encode-big-vlq.js.map +1 -0
- package/dist/vlq/encode-vlq.d.ts +18 -0
- package/dist/vlq/encode-vlq.d.ts.map +1 -0
- package/dist/vlq/encode-vlq.js +24 -0
- package/dist/vlq/encode-vlq.js.map +1 -0
- package/dist/vlq/read-big-vlq.d.ts +19 -0
- package/dist/vlq/read-big-vlq.d.ts.map +1 -0
- package/dist/vlq/read-big-vlq.js +37 -0
- package/dist/vlq/read-big-vlq.js.map +1 -0
- package/dist/vlq/read-vlq.d.ts +26 -0
- package/dist/vlq/read-vlq.d.ts.map +1 -0
- package/dist/vlq/read-vlq.js +47 -0
- package/dist/vlq/read-vlq.js.map +1 -0
- package/dist/vlq/vlq-byte-length.d.ts +12 -0
- package/dist/vlq/vlq-byte-length.d.ts.map +1 -0
- package/dist/vlq/vlq-byte-length.js +24 -0
- package/dist/vlq/vlq-byte-length.js.map +1 -0
- package/dist/vlq/vlq-decoder.d.ts +20 -0
- package/dist/vlq/vlq-decoder.d.ts.map +1 -0
- package/dist/vlq/vlq-decoder.js +40 -0
- package/dist/vlq/vlq-decoder.js.map +1 -0
- package/dist/vlq/vlq-encoder.d.ts +9 -0
- package/dist/vlq/vlq-encoder.d.ts.map +1 -0
- package/dist/vlq/vlq-encoder.js +11 -0
- package/dist/vlq/vlq-encoder.js.map +1 -0
- package/dist/vlq/write-vlq.d.ts +15 -0
- package/dist/vlq/write-vlq.d.ts.map +1 -0
- package/dist/vlq/write-vlq.js +32 -0
- package/dist/vlq/write-vlq.js.map +1 -0
- package/package.json +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2026 Anthony Champagne and contributors
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# @ac-kit/format-varint
|
|
2
|
+
|
|
3
|
+
Variable-length integer encodings — the family where an integer is split into
|
|
4
|
+
groups of bits and each byte carries a continuation flag, so a value delimits
|
|
5
|
+
itself and small values stay small.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { encodeVlq, readVlq, vlqDecoder } from "@ac-kit/format-varint";
|
|
9
|
+
import { ByteReader } from "@ac-kit/core";
|
|
10
|
+
|
|
11
|
+
encodeVlq(300); // Uint8Array [0x82, 0x2c]
|
|
12
|
+
readVlq(new ByteReader(bytes)); // reads one value, advances the cursor
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Which member you want
|
|
16
|
+
|
|
17
|
+
**VLQ** is base-128, most significant group first, with the continuation bit set
|
|
18
|
+
on every byte but the last. It is what ASN.1 X.690 uses for tag numbers (§8.1.2)
|
|
19
|
+
and OID arcs (§8.19.2), and what Standard MIDI Files use for delta times.
|
|
20
|
+
|
|
21
|
+
It is not the only member of the family, and the members are not
|
|
22
|
+
interchangeable: LEB128 puts the _least_ significant group first, and source
|
|
23
|
+
maps use a base-64 alphabet with the sign in the low bit. Every export here
|
|
24
|
+
names its member, so there is no ambiguous `encodeVarint`.
|
|
25
|
+
|
|
26
|
+
## Reading one value, or reading a stream
|
|
27
|
+
|
|
28
|
+
Two shapes, because the two situations are genuinely different:
|
|
29
|
+
|
|
30
|
+
- `readVlq(reader)` reads from a cursor and throws when the bytes run out. Use
|
|
31
|
+
it wherever an enclosing frame already bounds the value — an ASN.1 TLV's
|
|
32
|
+
contents, a file already in memory — because there "out of bytes" means the
|
|
33
|
+
frame lied, not that more is coming.
|
|
34
|
+
- `vlqDecoder` is a `Decoder` from `@ac-kit/format-core`. It reports a truncated
|
|
35
|
+
value as `incomplete` so a driver can wait for more input. Use it only where a
|
|
36
|
+
value really can straddle a chunk boundary; it allocates a `DecodeResult` per
|
|
37
|
+
value and is several times slower than the cursor form.
|
|
38
|
+
|
|
39
|
+
## `number` or `bigint`
|
|
40
|
+
|
|
41
|
+
`readVlq` and `encodeVlq` work in `number` and cover 0 to
|
|
42
|
+
`Number.MAX_SAFE_INTEGER`. `readBigVlq` and `encodeBigVlq` are the unbounded
|
|
43
|
+
counterparts.
|
|
44
|
+
|
|
45
|
+
The encoding itself has no ceiling — X.660 caps no OID arc — so a value too
|
|
46
|
+
large for a `number` is well-formed data, not a malformed encoding. `readVlq`
|
|
47
|
+
says so: it throws `RangeError` naming `readBigVlq`, and reserves
|
|
48
|
+
`VarintMalformedError` for what is actually wrong with the bytes. Prefer the
|
|
49
|
+
`number` forms; `bigint` arithmetic is markedly slower and allocates.
|
|
50
|
+
|
|
51
|
+
## Canonical form
|
|
52
|
+
|
|
53
|
+
An overlong encoding — a leading `0x80`, whose payload group is zero — is
|
|
54
|
+
rejected. X.690 §8.19.2 requires the fewest possible octets, and accepting the
|
|
55
|
+
padded form would let two different byte sequences mean the same number.
|
|
56
|
+
|
|
57
|
+
See the
|
|
58
|
+
[generated API docs](https://anthochamp.github.io/node-essentials/api/format-varint/)
|
|
59
|
+
for the full reference.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Set on every byte but the last of a sequence. */
|
|
2
|
+
export declare const CONTINUATION_BIT = 128;
|
|
3
|
+
/** The seven payload bits each byte carries. */
|
|
4
|
+
export declare const PAYLOAD_MASK = 127;
|
|
5
|
+
/** The radix: seven payload bits per byte. */
|
|
6
|
+
export declare const RADIX = 128;
|
|
7
|
+
/** Seven payload bits, as a shift distance for the `bigint` forms. */
|
|
8
|
+
export declare const PAYLOAD_BITS = 7n;
|
|
9
|
+
/** {@link PAYLOAD_MASK} for the `bigint` forms. */
|
|
10
|
+
export declare const PAYLOAD_MASK_BIG = 127n;
|
|
11
|
+
/** {@link RADIX} for the `bigint` forms. */
|
|
12
|
+
export declare const RADIX_BIG = 128n;
|
|
13
|
+
/**
|
|
14
|
+
* The largest value that can still be shifted left by seven bits and stay
|
|
15
|
+
* exactly representable: `value * 128 + 127` is `Number.MAX_SAFE_INTEGER` when
|
|
16
|
+
* `value` is this, so the check is exact rather than conservative.
|
|
17
|
+
*/
|
|
18
|
+
export declare const MAX_SAFE_PREFIX: number;
|
|
19
|
+
//# sourceMappingURL=_base128.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_base128.d.ts","sourceRoot":"","sources":["../src/_base128.ts"],"names":[],"mappings":"AAAA,oDAAoD;AACpD,eAAO,MAAM,gBAAgB,MAAO,CAAC;AAErC,gDAAgD;AAChD,eAAO,MAAM,YAAY,MAAO,CAAC;AAEjC,8CAA8C;AAC9C,eAAO,MAAM,KAAK,MAAM,CAAC;AAEzB,sEAAsE;AACtE,eAAO,MAAM,YAAY,KAAK,CAAC;AAE/B,mDAAmD;AACnD,eAAO,MAAM,gBAAgB,OAAQ,CAAC;AAEtC,4CAA4C;AAC5C,eAAO,MAAM,SAAS,OAAO,CAAC;AAE9B;;;;GAIG;AACH,eAAO,MAAM,eAAe,QAA8C,CAAC"}
|
package/dist/_base128.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Set on every byte but the last of a sequence. */
|
|
2
|
+
export const CONTINUATION_BIT = 0x80;
|
|
3
|
+
/** The seven payload bits each byte carries. */
|
|
4
|
+
export const PAYLOAD_MASK = 0x7f;
|
|
5
|
+
/** The radix: seven payload bits per byte. */
|
|
6
|
+
export const RADIX = 128;
|
|
7
|
+
/** Seven payload bits, as a shift distance for the `bigint` forms. */
|
|
8
|
+
export const PAYLOAD_BITS = 7n;
|
|
9
|
+
/** {@link PAYLOAD_MASK} for the `bigint` forms. */
|
|
10
|
+
export const PAYLOAD_MASK_BIG = 0x7fn;
|
|
11
|
+
/** {@link RADIX} for the `bigint` forms. */
|
|
12
|
+
export const RADIX_BIG = 128n;
|
|
13
|
+
/**
|
|
14
|
+
* The largest value that can still be shifted left by seven bits and stay
|
|
15
|
+
* exactly representable: `value * 128 + 127` is `Number.MAX_SAFE_INTEGER` when
|
|
16
|
+
* `value` is this, so the check is exact rather than conservative.
|
|
17
|
+
*/
|
|
18
|
+
export const MAX_SAFE_PREFIX = Math.floor(Number.MAX_SAFE_INTEGER / RADIX);
|
|
19
|
+
//# sourceMappingURL=_base128.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_base128.js","sourceRoot":"","sources":["../src/_base128.ts"],"names":[],"mappings":"AAAA,oDAAoD;AACpD,MAAM,CAAC,MAAM,gBAAgB,GAAG,IAAI,CAAC;AAErC,gDAAgD;AAChD,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,CAAC;AAEjC,8CAA8C;AAC9C,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,CAAC;AAEzB,sEAAsE;AACtE,MAAM,CAAC,MAAM,YAAY,GAAG,EAAE,CAAC;AAE/B,mDAAmD;AACnD,MAAM,CAAC,MAAM,gBAAgB,GAAG,KAAK,CAAC;AAEtC,4CAA4C;AAC5C,MAAM,CAAC,MAAM,SAAS,GAAG,IAAI,CAAC;AAE9B;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,gBAAgB,GAAG,KAAK,CAAC,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Base class for every failure this package reports. */
|
|
2
|
+
export declare class VarintError extends Error {
|
|
3
|
+
constructor(message: string);
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* The byte sequence ran out while a continuation bit was still set.
|
|
7
|
+
*
|
|
8
|
+
* Distinct from {@link VarintMalformedError} because it is not necessarily an
|
|
9
|
+
* error at all: a streaming decoder reading from a partially-filled buffer
|
|
10
|
+
* answers it by asking for more input, where a malformed sequence has no
|
|
11
|
+
* recovery.
|
|
12
|
+
*/
|
|
13
|
+
export declare class VarintIncompleteError extends VarintError {
|
|
14
|
+
constructor(message: string);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The byte sequence is well-formed as bytes but not a valid encoding — an
|
|
18
|
+
* overlong (non-minimal) form, a value beyond the representable range, or
|
|
19
|
+
* trailing bytes after a complete value.
|
|
20
|
+
*/
|
|
21
|
+
export declare class VarintMalformedError extends VarintError {
|
|
22
|
+
constructor(message: string);
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,yDAAyD;AACzD,qBAAa,WAAY,SAAQ,KAAK;IACrC,YAAY,OAAO,EAAE,MAAM,EAG1B;CACD;AAED;;;;;;;GAOG;AACH,qBAAa,qBAAsB,SAAQ,WAAW;IACrD,YAAY,OAAO,EAAE,MAAM,EAG1B;CACD;AAED;;;;GAIG;AACH,qBAAa,oBAAqB,SAAQ,WAAW;IACpD,YAAY,OAAO,EAAE,MAAM,EAG1B;CACD"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** Base class for every failure this package reports. */
|
|
2
|
+
export class VarintError extends Error {
|
|
3
|
+
constructor(message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "VarintError";
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The byte sequence ran out while a continuation bit was still set.
|
|
10
|
+
*
|
|
11
|
+
* Distinct from {@link VarintMalformedError} because it is not necessarily an
|
|
12
|
+
* error at all: a streaming decoder reading from a partially-filled buffer
|
|
13
|
+
* answers it by asking for more input, where a malformed sequence has no
|
|
14
|
+
* recovery.
|
|
15
|
+
*/
|
|
16
|
+
export class VarintIncompleteError extends VarintError {
|
|
17
|
+
constructor(message) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = "VarintIncompleteError";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The byte sequence is well-formed as bytes but not a valid encoding — an
|
|
24
|
+
* overlong (non-minimal) form, a value beyond the representable range, or
|
|
25
|
+
* trailing bytes after a complete value.
|
|
26
|
+
*/
|
|
27
|
+
export class VarintMalformedError extends VarintError {
|
|
28
|
+
constructor(message) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.name = "VarintMalformedError";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,yDAAyD;AACzD,MAAM,OAAO,WAAY,SAAQ,KAAK;IACrC,YAAY,OAAe;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;IAC3B,CAAC;CACD;AAED;;;;;;;GAOG;AACH,MAAM,OAAO,qBAAsB,SAAQ,WAAW;IACrD,YAAY,OAAe;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;IACrC,CAAC;CACD;AAED;;;;GAIG;AACH,MAAM,OAAO,oBAAqB,SAAQ,WAAW;IACpD,YAAY,OAAe;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;IACpC,CAAC;CACD"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export * from "./errors.js";
|
|
2
|
+
export * from "./vlq/big-vlq-decoder.js";
|
|
3
|
+
export * from "./vlq/big-vlq-encoder.js";
|
|
4
|
+
export * from "./vlq/decode-big-vlq.js";
|
|
5
|
+
export * from "./vlq/decode-vlq.js";
|
|
6
|
+
export * from "./vlq/encode-big-vlq.js";
|
|
7
|
+
export * from "./vlq/encode-vlq.js";
|
|
8
|
+
export * from "./vlq/read-big-vlq.js";
|
|
9
|
+
export * from "./vlq/read-vlq.js";
|
|
10
|
+
export * from "./vlq/vlq-byte-length.js";
|
|
11
|
+
export * from "./vlq/vlq-decoder.js";
|
|
12
|
+
export * from "./vlq/vlq-encoder.js";
|
|
13
|
+
export * from "./vlq/write-vlq.js";
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,0BAA0B,CAAC;AACzC,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,0BAA0B,CAAC;AACzC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export * from "./errors.js";
|
|
2
|
+
export * from "./vlq/big-vlq-decoder.js";
|
|
3
|
+
export * from "./vlq/big-vlq-encoder.js";
|
|
4
|
+
export * from "./vlq/decode-big-vlq.js";
|
|
5
|
+
export * from "./vlq/decode-vlq.js";
|
|
6
|
+
export * from "./vlq/encode-big-vlq.js";
|
|
7
|
+
export * from "./vlq/encode-vlq.js";
|
|
8
|
+
export * from "./vlq/read-big-vlq.js";
|
|
9
|
+
export * from "./vlq/read-vlq.js";
|
|
10
|
+
export * from "./vlq/vlq-byte-length.js";
|
|
11
|
+
export * from "./vlq/vlq-decoder.js";
|
|
12
|
+
export * from "./vlq/vlq-encoder.js";
|
|
13
|
+
export * from "./vlq/write-vlq.js";
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,0BAA0B,CAAC;AACzC,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,0BAA0B,CAAC;AACzC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type Decoder } from "@ac-kit/format-core";
|
|
2
|
+
/**
|
|
3
|
+
* A base-128 VLQ as a streaming {@link Decoder} producing `bigint` — the
|
|
4
|
+
* unbounded counterpart of {@link vlqDecoder}.
|
|
5
|
+
*
|
|
6
|
+
* Same framing behaviour: a VLQ delimits itself, so a truncated sequence is
|
|
7
|
+
* `incomplete` with no `needAtLeast` floor better than "one more byte", and an
|
|
8
|
+
* overlong encoding is `fatal`. Unlike {@link vlqDecoder} it has no range to
|
|
9
|
+
* exceed, so the only fatal outcome is a malformed sequence.
|
|
10
|
+
*
|
|
11
|
+
* Stateless, so one instance serves every caller and `reset` is unnecessary.
|
|
12
|
+
*/
|
|
13
|
+
export declare const bigVlqDecoder: Decoder<bigint>;
|
|
14
|
+
//# sourceMappingURL=big-vlq-decoder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"big-vlq-decoder.d.ts","sourceRoot":"","sources":["../../src/vlq/big-vlq-decoder.ts"],"names":[],"mappings":"AACA,OAAO,EAGN,KAAK,OAAO,EACZ,MAAM,qBAAqB,CAAC;AAK7B;;;;;;;;;;GAUG;AACH,eAAO,MAAM,aAAa,EAAE,OAAO,CAAC,MAAM,CAmBzC,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ByteReader } from "@ac-kit/core";
|
|
2
|
+
import { DECODE_INCOMPLETE, } from "@ac-kit/format-core";
|
|
3
|
+
import { VarintIncompleteError } from "../errors.js";
|
|
4
|
+
import { readBigVlq } from "./read-big-vlq.js";
|
|
5
|
+
/**
|
|
6
|
+
* A base-128 VLQ as a streaming {@link Decoder} producing `bigint` — the
|
|
7
|
+
* unbounded counterpart of {@link vlqDecoder}.
|
|
8
|
+
*
|
|
9
|
+
* Same framing behaviour: a VLQ delimits itself, so a truncated sequence is
|
|
10
|
+
* `incomplete` with no `needAtLeast` floor better than "one more byte", and an
|
|
11
|
+
* overlong encoding is `fatal`. Unlike {@link vlqDecoder} it has no range to
|
|
12
|
+
* exceed, so the only fatal outcome is a malformed sequence.
|
|
13
|
+
*
|
|
14
|
+
* Stateless, so one instance serves every caller and `reset` is unnecessary.
|
|
15
|
+
*/
|
|
16
|
+
export const bigVlqDecoder = {
|
|
17
|
+
decode(view) {
|
|
18
|
+
const reader = new ByteReader(view);
|
|
19
|
+
try {
|
|
20
|
+
const value = readBigVlq(reader);
|
|
21
|
+
return { status: "decoded", value, consumed: reader.position };
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
if (error instanceof VarintIncompleteError) {
|
|
25
|
+
return DECODE_INCOMPLETE;
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
status: "fatal",
|
|
29
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
//# sourceMappingURL=big-vlq-decoder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"big-vlq-decoder.js","sourceRoot":"","sources":["../../src/vlq/big-vlq-decoder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EACN,iBAAiB,GAGjB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAE/C;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,aAAa,GAAoB;IAC7C,MAAM,CAAC,IAAgB;QACtB,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC;QAEpC,IAAI,CAAC;YACJ,MAAM,KAAK,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC;YAEjC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,CAAC;QAChE,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,IAAI,KAAK,YAAY,qBAAqB,EAAE,CAAC;gBAC5C,OAAO,iBAAiB,CAAC;YAC1B,CAAC;YAED,OAAO;gBACN,MAAM,EAAE,OAAO;gBACf,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;aAChE,CAAC;QACH,CAAC;IACF,CAAC;CACD,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Encoder } from "@ac-kit/format-core";
|
|
2
|
+
/**
|
|
3
|
+
* A base-128 VLQ as a streaming {@link Encoder} taking `bigint`, the write half
|
|
4
|
+
* matching {@link bigVlqDecoder}.
|
|
5
|
+
*
|
|
6
|
+
* Stateless, so one instance serves every caller and `reset` is unnecessary.
|
|
7
|
+
*/
|
|
8
|
+
export declare const bigVlqEncoder: Encoder<bigint>;
|
|
9
|
+
//# sourceMappingURL=big-vlq-encoder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"big-vlq-encoder.d.ts","sourceRoot":"","sources":["../../src/vlq/big-vlq-encoder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAInD;;;;;GAKG;AACH,eAAO,MAAM,aAAa,EAAE,OAAO,CAAC,MAAM,CAEzC,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { encodeBigVlq } from "./encode-big-vlq.js";
|
|
2
|
+
/**
|
|
3
|
+
* A base-128 VLQ as a streaming {@link Encoder} taking `bigint`, the write half
|
|
4
|
+
* matching {@link bigVlqDecoder}.
|
|
5
|
+
*
|
|
6
|
+
* Stateless, so one instance serves every caller and `reset` is unnecessary.
|
|
7
|
+
*/
|
|
8
|
+
export const bigVlqEncoder = {
|
|
9
|
+
encode: encodeBigVlq,
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=big-vlq-encoder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"big-vlq-encoder.js","sourceRoot":"","sources":["../../src/vlq/big-vlq-encoder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,aAAa,GAAoB;IAC7C,MAAM,EAAE,YAAY;CACpB,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decodes a buffer holding exactly one base-128 VLQ as a `bigint` — the
|
|
3
|
+
* unbounded counterpart of {@link decodeVlq}.
|
|
4
|
+
*
|
|
5
|
+
* @param bytes - A buffer holding one complete encoding and nothing else.
|
|
6
|
+
* @returns The decoded value, zero or greater.
|
|
7
|
+
* @throws {VarintIncompleteError} When the encoding is truncated.
|
|
8
|
+
* @throws {VarintMalformedError} When the encoding is overlong, or followed by
|
|
9
|
+
* unread bytes.
|
|
10
|
+
*/
|
|
11
|
+
export declare function decodeBigVlq(bytes: Uint8Array): bigint;
|
|
12
|
+
//# sourceMappingURL=decode-big-vlq.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decode-big-vlq.d.ts","sourceRoot":"","sources":["../../src/vlq/decode-big-vlq.ts"],"names":[],"mappings":"AAKA;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAWtD"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ByteReader } from "@ac-kit/core";
|
|
2
|
+
import { VarintMalformedError } from "../errors.js";
|
|
3
|
+
import { readBigVlq } from "./read-big-vlq.js";
|
|
4
|
+
/**
|
|
5
|
+
* Decodes a buffer holding exactly one base-128 VLQ as a `bigint` — the
|
|
6
|
+
* unbounded counterpart of {@link decodeVlq}.
|
|
7
|
+
*
|
|
8
|
+
* @param bytes - A buffer holding one complete encoding and nothing else.
|
|
9
|
+
* @returns The decoded value, zero or greater.
|
|
10
|
+
* @throws {VarintIncompleteError} When the encoding is truncated.
|
|
11
|
+
* @throws {VarintMalformedError} When the encoding is overlong, or followed by
|
|
12
|
+
* unread bytes.
|
|
13
|
+
*/
|
|
14
|
+
export function decodeBigVlq(bytes) {
|
|
15
|
+
const reader = new ByteReader(bytes);
|
|
16
|
+
const value = readBigVlq(reader);
|
|
17
|
+
if (!reader.atEnd) {
|
|
18
|
+
throw new VarintMalformedError(`decodeBigVlq: ${reader.remaining} byte(s) left after a complete value`);
|
|
19
|
+
}
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=decode-big-vlq.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decode-big-vlq.js","sourceRoot":"","sources":["../../src/vlq/decode-big-vlq.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAE/C;;;;;;;;;GASG;AACH,MAAM,UAAU,YAAY,CAAC,KAAiB;IAC7C,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC;IACrC,MAAM,KAAK,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC;IAEjC,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QACnB,MAAM,IAAI,oBAAoB,CAC7B,iBAAiB,MAAM,CAAC,SAAS,sCAAsC,CACvE,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decodes a buffer holding exactly one base-128 VLQ.
|
|
3
|
+
*
|
|
4
|
+
* The one-shot counterpart of {@link readVlq}, for a caller that already knows
|
|
5
|
+
* the extent of the value. Decoding a run of values from one buffer is
|
|
6
|
+
* {@link readVlq}'s job — it does not re-scan and does not allocate per value.
|
|
7
|
+
*
|
|
8
|
+
* @param bytes - A buffer holding one complete encoding and nothing else.
|
|
9
|
+
* @returns The decoded value, from 0 to `Number.MAX_SAFE_INTEGER`.
|
|
10
|
+
* @throws {VarintIncompleteError} When the encoding is truncated.
|
|
11
|
+
* @throws {VarintMalformedError} When the encoding is overlong, out of range,
|
|
12
|
+
* or followed by unread bytes.
|
|
13
|
+
*/
|
|
14
|
+
export declare function decodeVlq(bytes: Uint8Array): number;
|
|
15
|
+
//# sourceMappingURL=decode-vlq.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decode-vlq.d.ts","sourceRoot":"","sources":["../../src/vlq/decode-vlq.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAWnD"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ByteReader } from "@ac-kit/core";
|
|
2
|
+
import { VarintMalformedError } from "../errors.js";
|
|
3
|
+
import { readVlq } from "./read-vlq.js";
|
|
4
|
+
/**
|
|
5
|
+
* Decodes a buffer holding exactly one base-128 VLQ.
|
|
6
|
+
*
|
|
7
|
+
* The one-shot counterpart of {@link readVlq}, for a caller that already knows
|
|
8
|
+
* the extent of the value. Decoding a run of values from one buffer is
|
|
9
|
+
* {@link readVlq}'s job — it does not re-scan and does not allocate per value.
|
|
10
|
+
*
|
|
11
|
+
* @param bytes - A buffer holding one complete encoding and nothing else.
|
|
12
|
+
* @returns The decoded value, from 0 to `Number.MAX_SAFE_INTEGER`.
|
|
13
|
+
* @throws {VarintIncompleteError} When the encoding is truncated.
|
|
14
|
+
* @throws {VarintMalformedError} When the encoding is overlong, out of range,
|
|
15
|
+
* or followed by unread bytes.
|
|
16
|
+
*/
|
|
17
|
+
export function decodeVlq(bytes) {
|
|
18
|
+
const reader = new ByteReader(bytes);
|
|
19
|
+
const value = readVlq(reader);
|
|
20
|
+
if (!reader.atEnd) {
|
|
21
|
+
throw new VarintMalformedError(`decodeVlq: ${reader.remaining} byte(s) left after a complete value`);
|
|
22
|
+
}
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=decode-vlq.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decode-vlq.js","sourceRoot":"","sources":["../../src/vlq/decode-vlq.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAExC;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,SAAS,CAAC,KAAiB;IAC1C,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC;IACrC,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAE9B,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QACnB,MAAM,IAAI,oBAAoB,CAC7B,cAAc,MAAM,CAAC,SAAS,sCAAsC,CACpE,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encodes a non-negative `bigint` as a base-128 VLQ — the unbounded counterpart
|
|
3
|
+
* of {@link encodeVlq}.
|
|
4
|
+
*
|
|
5
|
+
* X.660 puts no ceiling on an OID arc and X.690 §8.19.2 none on a
|
|
6
|
+
* subidentifier, so the encoding has a range `number` cannot cover. Reach for
|
|
7
|
+
* this only when a value may exceed `Number.MAX_SAFE_INTEGER`: every ASN.1 tag
|
|
8
|
+
* number and all but pathological arcs fit a `number`, and `bigint` arithmetic
|
|
9
|
+
* is markedly slower.
|
|
10
|
+
*
|
|
11
|
+
* Time complexity: O(log₁₂₈ value).
|
|
12
|
+
*
|
|
13
|
+
* @param value - A non-negative `bigint`.
|
|
14
|
+
* @returns A fresh buffer holding the encoding, never empty.
|
|
15
|
+
* @throws {RangeError} When `value` is negative.
|
|
16
|
+
*/
|
|
17
|
+
export declare function encodeBigVlq(value: bigint): Uint8Array<ArrayBuffer>;
|
|
18
|
+
//# sourceMappingURL=encode-big-vlq.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encode-big-vlq.d.ts","sourceRoot":"","sources":["../../src/vlq/encode-big-vlq.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAAC,WAAW,CAAC,CAwBnE"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { CONTINUATION_BIT, PAYLOAD_BITS, PAYLOAD_MASK_BIG, RADIX_BIG, } from "../_base128.js";
|
|
2
|
+
/**
|
|
3
|
+
* Encodes a non-negative `bigint` as a base-128 VLQ — the unbounded counterpart
|
|
4
|
+
* of {@link encodeVlq}.
|
|
5
|
+
*
|
|
6
|
+
* X.660 puts no ceiling on an OID arc and X.690 §8.19.2 none on a
|
|
7
|
+
* subidentifier, so the encoding has a range `number` cannot cover. Reach for
|
|
8
|
+
* this only when a value may exceed `Number.MAX_SAFE_INTEGER`: every ASN.1 tag
|
|
9
|
+
* number and all but pathological arcs fit a `number`, and `bigint` arithmetic
|
|
10
|
+
* is markedly slower.
|
|
11
|
+
*
|
|
12
|
+
* Time complexity: O(log₁₂₈ value).
|
|
13
|
+
*
|
|
14
|
+
* @param value - A non-negative `bigint`.
|
|
15
|
+
* @returns A fresh buffer holding the encoding, never empty.
|
|
16
|
+
* @throws {RangeError} When `value` is negative.
|
|
17
|
+
*/
|
|
18
|
+
export function encodeBigVlq(value) {
|
|
19
|
+
if (value < 0n) {
|
|
20
|
+
throw new RangeError(`encodeBigVlq: value must be non-negative, got ${value}`);
|
|
21
|
+
}
|
|
22
|
+
let length = 1;
|
|
23
|
+
for (let remaining = value; remaining >= RADIX_BIG; remaining /= RADIX_BIG) {
|
|
24
|
+
length++;
|
|
25
|
+
}
|
|
26
|
+
const out = new Uint8Array(length);
|
|
27
|
+
let remaining = value;
|
|
28
|
+
// Filled back to front: the least significant group is known first but
|
|
29
|
+
// belongs last, and only the final byte clears the continuation bit.
|
|
30
|
+
for (let index = length - 1; index >= 0; index--) {
|
|
31
|
+
const group = Number(remaining & PAYLOAD_MASK_BIG);
|
|
32
|
+
out[index] = index === length - 1 ? group : group | CONTINUATION_BIT;
|
|
33
|
+
remaining >>= PAYLOAD_BITS;
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=encode-big-vlq.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encode-big-vlq.js","sourceRoot":"","sources":["../../src/vlq/encode-big-vlq.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,gBAAgB,EAChB,YAAY,EACZ,gBAAgB,EAChB,SAAS,GACT,MAAM,gBAAgB,CAAC;AAExB;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,YAAY,CAAC,KAAa;IACzC,IAAI,KAAK,GAAG,EAAE,EAAE,CAAC;QAChB,MAAM,IAAI,UAAU,CACnB,iDAAiD,KAAK,EAAE,CACxD,CAAC;IACH,CAAC;IAED,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,KAAK,IAAI,SAAS,GAAG,KAAK,EAAE,SAAS,IAAI,SAAS,EAAE,SAAS,IAAI,SAAS,EAAE,CAAC;QAC5E,MAAM,EAAE,CAAC;IACV,CAAC;IAED,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IACnC,IAAI,SAAS,GAAG,KAAK,CAAC;IAEtB,uEAAuE;IACvE,qEAAqE;IACrE,KAAK,IAAI,KAAK,GAAG,MAAM,GAAG,CAAC,EAAE,KAAK,IAAI,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC;QAClD,MAAM,KAAK,GAAG,MAAM,CAAC,SAAS,GAAG,gBAAgB,CAAC,CAAC;QACnD,GAAG,CAAC,KAAK,CAAC,GAAG,KAAK,KAAK,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,GAAG,gBAAgB,CAAC;QACrE,SAAS,KAAK,YAAY,CAAC;IAC5B,CAAC;IAED,OAAO,GAAG,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encodes a non-negative integer as a base-128 VLQ, most significant group
|
|
3
|
+
* first, with the continuation bit set on every byte but the last.
|
|
4
|
+
*
|
|
5
|
+
* Used by ASN.1 X.690 for tag numbers (§8.1.2.4) and OID arcs (§8.19.2), and by
|
|
6
|
+
* Standard MIDI Files for delta times. DWARF, WebAssembly and Protocol Buffers
|
|
7
|
+
* use the little-endian LEB128 member of the same family instead, which this is
|
|
8
|
+
* not interchangeable with.
|
|
9
|
+
*
|
|
10
|
+
* Time complexity: O(log₁₂₈ value).
|
|
11
|
+
*
|
|
12
|
+
* @param value - A non-negative safe integer.
|
|
13
|
+
* @returns A fresh buffer holding the encoding, never empty.
|
|
14
|
+
* @throws {RangeError} When `value` is negative, fractional, or beyond
|
|
15
|
+
* `Number.MAX_SAFE_INTEGER`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function encodeVlq(value: number): Uint8Array<ArrayBuffer>;
|
|
18
|
+
//# sourceMappingURL=encode-vlq.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encode-vlq.d.ts","sourceRoot":"","sources":["../../src/vlq/encode-vlq.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAAC,WAAW,CAAC,CAKhE"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { vlqByteLength } from "./vlq-byte-length.js";
|
|
2
|
+
import { writeVlq } from "./write-vlq.js";
|
|
3
|
+
/**
|
|
4
|
+
* Encodes a non-negative integer as a base-128 VLQ, most significant group
|
|
5
|
+
* first, with the continuation bit set on every byte but the last.
|
|
6
|
+
*
|
|
7
|
+
* Used by ASN.1 X.690 for tag numbers (§8.1.2.4) and OID arcs (§8.19.2), and by
|
|
8
|
+
* Standard MIDI Files for delta times. DWARF, WebAssembly and Protocol Buffers
|
|
9
|
+
* use the little-endian LEB128 member of the same family instead, which this is
|
|
10
|
+
* not interchangeable with.
|
|
11
|
+
*
|
|
12
|
+
* Time complexity: O(log₁₂₈ value).
|
|
13
|
+
*
|
|
14
|
+
* @param value - A non-negative safe integer.
|
|
15
|
+
* @returns A fresh buffer holding the encoding, never empty.
|
|
16
|
+
* @throws {RangeError} When `value` is negative, fractional, or beyond
|
|
17
|
+
* `Number.MAX_SAFE_INTEGER`.
|
|
18
|
+
*/
|
|
19
|
+
export function encodeVlq(value) {
|
|
20
|
+
const out = new Uint8Array(vlqByteLength(value));
|
|
21
|
+
writeVlq(out, 0, value);
|
|
22
|
+
return out;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=encode-vlq.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encode-vlq.js","sourceRoot":"","sources":["../../src/vlq/encode-vlq.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,SAAS,CAAC,KAAa;IACtC,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC;IACjD,QAAQ,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC;IAExB,OAAO,GAAG,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ByteReader } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Reads one base-128 VLQ from `reader` as a `bigint`, advancing it past the
|
|
4
|
+
* bytes consumed — the unbounded counterpart of {@link readVlq}.
|
|
5
|
+
*
|
|
6
|
+
* Has no range limit to report: X.660 caps no OID arc, and a `bigint` holds
|
|
7
|
+
* whatever the bytes say. Prefer {@link readVlq} wherever the value is known to
|
|
8
|
+
* fit a `number`; `bigint` arithmetic is markedly slower and allocates.
|
|
9
|
+
*
|
|
10
|
+
* Time complexity: O(log₁₂₈ value).
|
|
11
|
+
*
|
|
12
|
+
* @param reader - Cursor positioned at the first byte of the encoding.
|
|
13
|
+
* @returns The decoded value, zero or greater.
|
|
14
|
+
* @throws {VarintIncompleteError} When the bytes run out while a continuation
|
|
15
|
+
* bit is still set.
|
|
16
|
+
* @throws {VarintMalformedError} When the encoding is overlong.
|
|
17
|
+
*/
|
|
18
|
+
export declare function readBigVlq(reader: ByteReader): bigint;
|
|
19
|
+
//# sourceMappingURL=read-big-vlq.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read-big-vlq.d.ts","sourceRoot":"","sources":["../../src/vlq/read-big-vlq.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAK/C;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CA2BrD"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { CONTINUATION_BIT, PAYLOAD_BITS, PAYLOAD_MASK } from "../_base128.js";
|
|
2
|
+
import { VarintIncompleteError, VarintMalformedError } from "../errors.js";
|
|
3
|
+
/**
|
|
4
|
+
* Reads one base-128 VLQ from `reader` as a `bigint`, advancing it past the
|
|
5
|
+
* bytes consumed — the unbounded counterpart of {@link readVlq}.
|
|
6
|
+
*
|
|
7
|
+
* Has no range limit to report: X.660 caps no OID arc, and a `bigint` holds
|
|
8
|
+
* whatever the bytes say. Prefer {@link readVlq} wherever the value is known to
|
|
9
|
+
* fit a `number`; `bigint` arithmetic is markedly slower and allocates.
|
|
10
|
+
*
|
|
11
|
+
* Time complexity: O(log₁₂₈ value).
|
|
12
|
+
*
|
|
13
|
+
* @param reader - Cursor positioned at the first byte of the encoding.
|
|
14
|
+
* @returns The decoded value, zero or greater.
|
|
15
|
+
* @throws {VarintIncompleteError} When the bytes run out while a continuation
|
|
16
|
+
* bit is still set.
|
|
17
|
+
* @throws {VarintMalformedError} When the encoding is overlong.
|
|
18
|
+
*/
|
|
19
|
+
export function readBigVlq(reader) {
|
|
20
|
+
if (reader.atEnd) {
|
|
21
|
+
throw new VarintIncompleteError("readBigVlq: no bytes to read");
|
|
22
|
+
}
|
|
23
|
+
let byte = reader.readByte();
|
|
24
|
+
if (byte === CONTINUATION_BIT) {
|
|
25
|
+
throw new VarintMalformedError("readBigVlq: overlong encoding, leading byte 0x80 has an empty payload");
|
|
26
|
+
}
|
|
27
|
+
let value = BigInt(byte & PAYLOAD_MASK);
|
|
28
|
+
while ((byte & CONTINUATION_BIT) !== 0) {
|
|
29
|
+
if (reader.atEnd) {
|
|
30
|
+
throw new VarintIncompleteError("readBigVlq: continuation bit set on the last available byte");
|
|
31
|
+
}
|
|
32
|
+
byte = reader.readByte();
|
|
33
|
+
value = (value << PAYLOAD_BITS) | BigInt(byte & PAYLOAD_MASK);
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=read-big-vlq.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read-big-vlq.js","sourceRoot":"","sources":["../../src/vlq/read-big-vlq.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9E,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAE3E;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,UAAU,CAAC,MAAkB;IAC5C,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;QAClB,MAAM,IAAI,qBAAqB,CAAC,8BAA8B,CAAC,CAAC;IACjE,CAAC;IAED,IAAI,IAAI,GAAG,MAAM,CAAC,QAAQ,EAAE,CAAC;IAE7B,IAAI,IAAI,KAAK,gBAAgB,EAAE,CAAC;QAC/B,MAAM,IAAI,oBAAoB,CAC7B,uEAAuE,CACvE,CAAC;IACH,CAAC;IAED,IAAI,KAAK,GAAG,MAAM,CAAC,IAAI,GAAG,YAAY,CAAC,CAAC;IAExC,OAAO,CAAC,IAAI,GAAG,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;QACxC,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;YAClB,MAAM,IAAI,qBAAqB,CAC9B,6DAA6D,CAC7D,CAAC;QACH,CAAC;QAED,IAAI,GAAG,MAAM,CAAC,QAAQ,EAAE,CAAC;QACzB,KAAK,GAAG,CAAC,KAAK,IAAI,YAAY,CAAC,GAAG,MAAM,CAAC,IAAI,GAAG,YAAY,CAAC,CAAC;IAC/D,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ByteReader } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Reads one base-128 VLQ from `reader`, advancing it past the bytes consumed.
|
|
4
|
+
*
|
|
5
|
+
* Allocates nothing: the cursor carries the position, so a caller decoding a
|
|
6
|
+
* run of values (an OID's arcs, a MIDI track's delta times) pays one
|
|
7
|
+
* `ByteReader` for the run rather than one result object per value.
|
|
8
|
+
*
|
|
9
|
+
* Rejects an overlong encoding — a leading `0x80` byte, whose payload group is
|
|
10
|
+
* zero — because X.690 §8.19.2 requires the fewest possible octets, and an
|
|
11
|
+
* accepted overlong form would let two byte sequences mean the same number.
|
|
12
|
+
*
|
|
13
|
+
* Time complexity: O(log₁₂₈ value).
|
|
14
|
+
*
|
|
15
|
+
* @param reader - Cursor positioned at the first byte of the encoding.
|
|
16
|
+
* @returns The decoded value, from 0 to `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.
|
|
20
|
+
* @throws {RangeError} When the value is too large for a `number`. X.660 puts
|
|
21
|
+
* no ceiling on an OID arc, so such an encoding is well-formed and
|
|
22
|
+
* {@link readBigVlq} reads it — the limit is this function's return type, not
|
|
23
|
+
* the data.
|
|
24
|
+
*/
|
|
25
|
+
export declare function readVlq(reader: ByteReader): number;
|
|
26
|
+
//# sourceMappingURL=read-vlq.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read-vlq.d.ts","sourceRoot":"","sources":["../../src/vlq/read-vlq.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAU/C;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAiClD"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { CONTINUATION_BIT, MAX_SAFE_PREFIX, PAYLOAD_MASK, RADIX, } from "../_base128.js";
|
|
2
|
+
import { VarintIncompleteError, VarintMalformedError } from "../errors.js";
|
|
3
|
+
/**
|
|
4
|
+
* Reads one base-128 VLQ from `reader`, advancing it past the bytes consumed.
|
|
5
|
+
*
|
|
6
|
+
* Allocates nothing: the cursor carries the position, so a caller decoding a
|
|
7
|
+
* run of values (an OID's arcs, a MIDI track's delta times) pays one
|
|
8
|
+
* `ByteReader` for the run rather than one result object per value.
|
|
9
|
+
*
|
|
10
|
+
* Rejects an overlong encoding — a leading `0x80` byte, whose payload group is
|
|
11
|
+
* zero — because X.690 §8.19.2 requires the fewest possible octets, and an
|
|
12
|
+
* accepted overlong form would let two byte sequences mean the same number.
|
|
13
|
+
*
|
|
14
|
+
* Time complexity: O(log₁₂₈ value).
|
|
15
|
+
*
|
|
16
|
+
* @param reader - Cursor positioned at the first byte of the encoding.
|
|
17
|
+
* @returns The decoded value, from 0 to `Number.MAX_SAFE_INTEGER`.
|
|
18
|
+
* @throws {VarintIncompleteError} When the bytes run out while a continuation
|
|
19
|
+
* bit is still set.
|
|
20
|
+
* @throws {VarintMalformedError} When the encoding is overlong.
|
|
21
|
+
* @throws {RangeError} When the value is too large for a `number`. X.660 puts
|
|
22
|
+
* no ceiling on an OID arc, so such an encoding is well-formed and
|
|
23
|
+
* {@link readBigVlq} reads it — the limit is this function's return type, not
|
|
24
|
+
* the data.
|
|
25
|
+
*/
|
|
26
|
+
export function readVlq(reader) {
|
|
27
|
+
if (reader.atEnd) {
|
|
28
|
+
throw new VarintIncompleteError("readVlq: no bytes to read");
|
|
29
|
+
}
|
|
30
|
+
let byte = reader.readByte();
|
|
31
|
+
if (byte === CONTINUATION_BIT) {
|
|
32
|
+
throw new VarintMalformedError("readVlq: overlong encoding, leading byte 0x80 has an empty payload");
|
|
33
|
+
}
|
|
34
|
+
let value = byte & PAYLOAD_MASK;
|
|
35
|
+
while ((byte & CONTINUATION_BIT) !== 0) {
|
|
36
|
+
if (reader.atEnd) {
|
|
37
|
+
throw new VarintIncompleteError("readVlq: continuation bit set on the last available byte");
|
|
38
|
+
}
|
|
39
|
+
if (value > MAX_SAFE_PREFIX) {
|
|
40
|
+
throw new RangeError("readVlq: value exceeds Number.MAX_SAFE_INTEGER; use readBigVlq for a wider value");
|
|
41
|
+
}
|
|
42
|
+
byte = reader.readByte();
|
|
43
|
+
value = value * RADIX + (byte & PAYLOAD_MASK);
|
|
44
|
+
}
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=read-vlq.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read-vlq.js","sourceRoot":"","sources":["../../src/vlq/read-vlq.ts"],"names":[],"mappings":"AAEA,OAAO,EACN,gBAAgB,EAChB,eAAe,EACf,YAAY,EACZ,KAAK,GACL,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAE3E;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,OAAO,CAAC,MAAkB;IACzC,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;QAClB,MAAM,IAAI,qBAAqB,CAAC,2BAA2B,CAAC,CAAC;IAC9D,CAAC;IAED,IAAI,IAAI,GAAG,MAAM,CAAC,QAAQ,EAAE,CAAC;IAE7B,IAAI,IAAI,KAAK,gBAAgB,EAAE,CAAC;QAC/B,MAAM,IAAI,oBAAoB,CAC7B,oEAAoE,CACpE,CAAC;IACH,CAAC;IAED,IAAI,KAAK,GAAG,IAAI,GAAG,YAAY,CAAC;IAEhC,OAAO,CAAC,IAAI,GAAG,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;QACxC,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;YAClB,MAAM,IAAI,qBAAqB,CAC9B,0DAA0D,CAC1D,CAAC;QACH,CAAC;QAED,IAAI,KAAK,GAAG,eAAe,EAAE,CAAC;YAC7B,MAAM,IAAI,UAAU,CACnB,kFAAkF,CAClF,CAAC;QACH,CAAC;QAED,IAAI,GAAG,MAAM,CAAC,QAAQ,EAAE,CAAC;QACzB,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,CAAC,IAAI,GAAG,YAAY,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How many bytes {@link encodeVlq} will produce for `value`.
|
|
3
|
+
*
|
|
4
|
+
* Time complexity: O(log₁₂₈ value) — at most 8 iterations for any safe integer.
|
|
5
|
+
*
|
|
6
|
+
* @param value - A non-negative safe integer.
|
|
7
|
+
* @returns The byte count, at least 1.
|
|
8
|
+
* @throws {RangeError} When `value` is negative, fractional, or beyond
|
|
9
|
+
* `Number.MAX_SAFE_INTEGER`.
|
|
10
|
+
*/
|
|
11
|
+
export declare function vlqByteLength(value: number): number;
|
|
12
|
+
//# sourceMappingURL=vlq-byte-length.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vlq-byte-length.d.ts","sourceRoot":"","sources":["../../src/vlq/vlq-byte-length.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAgBnD"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { RADIX } from "../_base128.js";
|
|
2
|
+
/**
|
|
3
|
+
* How many bytes {@link encodeVlq} will produce for `value`.
|
|
4
|
+
*
|
|
5
|
+
* Time complexity: O(log₁₂₈ value) — at most 8 iterations for any safe integer.
|
|
6
|
+
*
|
|
7
|
+
* @param value - A non-negative safe integer.
|
|
8
|
+
* @returns The byte count, at least 1.
|
|
9
|
+
* @throws {RangeError} When `value` is negative, fractional, or beyond
|
|
10
|
+
* `Number.MAX_SAFE_INTEGER`.
|
|
11
|
+
*/
|
|
12
|
+
export function vlqByteLength(value) {
|
|
13
|
+
if (!Number.isSafeInteger(value) || value < 0) {
|
|
14
|
+
throw new RangeError(`vlqByteLength: value must be a non-negative safe integer, got ${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=vlq-byte-length.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vlq-byte-length.js","sourceRoot":"","sources":["../../src/vlq/vlq-byte-length.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAEvC;;;;;;;;;GASG;AACH,MAAM,UAAU,aAAa,CAAC,KAAa;IAC1C,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QAC/C,MAAM,IAAI,UAAU,CACnB,iEAAiE,KAAK,EAAE,CACxE,CAAC;IACH,CAAC;IAED,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,IAAI,SAAS,GAAG,KAAK,CAAC;IAEtB,OAAO,SAAS,IAAI,KAAK,EAAE,CAAC;QAC3B,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,GAAG,KAAK,CAAC,CAAC;QAC1C,MAAM,EAAE,CAAC;IACV,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Decoder } from "@ac-kit/format-core";
|
|
2
|
+
/**
|
|
3
|
+
* A base-128 VLQ as a streaming {@link Decoder}.
|
|
4
|
+
*
|
|
5
|
+
* A VLQ delimits itself — the extent is only known once a byte without the
|
|
6
|
+
* continuation bit arrives — so a truncated sequence is `incomplete` rather
|
|
7
|
+
* than an error, and `needAtLeast` is deliberately omitted: a decoder that
|
|
8
|
+
* cannot see the end cannot compute a floor better than "one more byte".
|
|
9
|
+
*
|
|
10
|
+
* Stateless, so one instance serves every caller and `reset` is unnecessary.
|
|
11
|
+
*
|
|
12
|
+
* A malformed sequence is `fatal`: an overlong or out-of-range value means the
|
|
13
|
+
* byte stream is not what it claimed to be, and there is no length to
|
|
14
|
+
* resynchronise past. Callers reading a VLQ from inside an already-framed
|
|
15
|
+
* region — ASN.1 tag numbers and OID arcs, where an enclosing TLV bounds the
|
|
16
|
+
* value — want {@link readVlq} instead, where running out of bytes is a
|
|
17
|
+
* malformed frame rather than a request for more input.
|
|
18
|
+
*/
|
|
19
|
+
export declare const vlqDecoder: Decoder<number>;
|
|
20
|
+
//# sourceMappingURL=vlq-decoder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vlq-decoder.d.ts","sourceRoot":"","sources":["../../src/vlq/vlq-decoder.ts"],"names":[],"mappings":"AACA,OAAO,EAGN,KAAK,OAAO,EACZ,MAAM,qBAAqB,CAAC;AAK7B;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,UAAU,EAAE,OAAO,CAAC,MAAM,CAmBtC,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ByteReader } from "@ac-kit/core";
|
|
2
|
+
import { DECODE_INCOMPLETE, } from "@ac-kit/format-core";
|
|
3
|
+
import { VarintIncompleteError } from "../errors.js";
|
|
4
|
+
import { readVlq } from "./read-vlq.js";
|
|
5
|
+
/**
|
|
6
|
+
* A base-128 VLQ as a streaming {@link Decoder}.
|
|
7
|
+
*
|
|
8
|
+
* A VLQ delimits itself — the extent is only known once a byte without the
|
|
9
|
+
* continuation bit arrives — so a truncated sequence is `incomplete` rather
|
|
10
|
+
* than an error, and `needAtLeast` is deliberately omitted: a decoder that
|
|
11
|
+
* cannot see the end cannot compute a floor better than "one more byte".
|
|
12
|
+
*
|
|
13
|
+
* Stateless, so one instance serves every caller and `reset` is unnecessary.
|
|
14
|
+
*
|
|
15
|
+
* A malformed sequence is `fatal`: an overlong or out-of-range value means the
|
|
16
|
+
* byte stream is not what it claimed to be, and there is no length to
|
|
17
|
+
* resynchronise past. Callers reading a VLQ from inside an already-framed
|
|
18
|
+
* region — ASN.1 tag numbers and OID arcs, where an enclosing TLV bounds the
|
|
19
|
+
* value — want {@link readVlq} instead, where running out of bytes is a
|
|
20
|
+
* malformed frame rather than a request for more input.
|
|
21
|
+
*/
|
|
22
|
+
export const vlqDecoder = {
|
|
23
|
+
decode(view) {
|
|
24
|
+
const reader = new ByteReader(view);
|
|
25
|
+
try {
|
|
26
|
+
const value = readVlq(reader);
|
|
27
|
+
return { status: "decoded", value, consumed: reader.position };
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
if (error instanceof VarintIncompleteError) {
|
|
31
|
+
return DECODE_INCOMPLETE;
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
status: "fatal",
|
|
35
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
//# sourceMappingURL=vlq-decoder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vlq-decoder.js","sourceRoot":"","sources":["../../src/vlq/vlq-decoder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EACN,iBAAiB,GAGjB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAExC;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,MAAM,UAAU,GAAoB;IAC1C,MAAM,CAAC,IAAgB;QACtB,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC;QAEpC,IAAI,CAAC;YACJ,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;YAE9B,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,CAAC;QAChE,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,IAAI,KAAK,YAAY,qBAAqB,EAAE,CAAC;gBAC5C,OAAO,iBAAiB,CAAC;YAC1B,CAAC;YAED,OAAO;gBACN,MAAM,EAAE,OAAO;gBACf,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;aAChE,CAAC;QACH,CAAC;IACF,CAAC;CACD,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Encoder } from "@ac-kit/format-core";
|
|
2
|
+
/**
|
|
3
|
+
* A base-128 VLQ as a streaming {@link Encoder}, the write half matching
|
|
4
|
+
* {@link vlqDecoder}.
|
|
5
|
+
*
|
|
6
|
+
* Stateless, so one instance serves every caller and `reset` is unnecessary.
|
|
7
|
+
*/
|
|
8
|
+
export declare const vlqEncoder: Encoder<number>;
|
|
9
|
+
//# sourceMappingURL=vlq-encoder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vlq-encoder.d.ts","sourceRoot":"","sources":["../../src/vlq/vlq-encoder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAInD;;;;;GAKG;AACH,eAAO,MAAM,UAAU,EAAE,OAAO,CAAC,MAAM,CAEtC,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { encodeVlq } from "./encode-vlq.js";
|
|
2
|
+
/**
|
|
3
|
+
* A base-128 VLQ as a streaming {@link Encoder}, the write half matching
|
|
4
|
+
* {@link vlqDecoder}.
|
|
5
|
+
*
|
|
6
|
+
* Stateless, so one instance serves every caller and `reset` is unnecessary.
|
|
7
|
+
*/
|
|
8
|
+
export const vlqEncoder = {
|
|
9
|
+
encode: encodeVlq,
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=vlq-encoder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vlq-encoder.js","sourceRoot":"","sources":["../../src/vlq/vlq-encoder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C;;;;;GAKG;AACH,MAAM,CAAC,MAAM,UAAU,GAAoB;IAC1C,MAAM,EAAE,SAAS;CACjB,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writes `value` into `target` at `offset`, most significant group first.
|
|
3
|
+
*
|
|
4
|
+
* The in-place counterpart of {@link encodeVlq}, for a caller assembling several
|
|
5
|
+
* values into one buffer without allocating per value.
|
|
6
|
+
*
|
|
7
|
+
* @param target - Destination, which must hold {@link vlqByteLength} bytes from
|
|
8
|
+
* `offset`.
|
|
9
|
+
* @param offset - Where to start writing.
|
|
10
|
+
* @param value - A non-negative safe integer.
|
|
11
|
+
* @returns The offset just past the bytes written.
|
|
12
|
+
* @throws {RangeError} When `value` is out of range, or `target` is too short.
|
|
13
|
+
*/
|
|
14
|
+
export declare function writeVlq(target: Uint8Array, offset: number, value: number): number;
|
|
15
|
+
//# sourceMappingURL=write-vlq.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"write-vlq.d.ts","sourceRoot":"","sources":["../../src/vlq/write-vlq.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;GAYG;AACH,wBAAgB,QAAQ,CACvB,MAAM,EAAE,UAAU,EAClB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,GACX,MAAM,CAqBR"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { CONTINUATION_BIT, RADIX } from "../_base128.js";
|
|
2
|
+
import { vlqByteLength } from "./vlq-byte-length.js";
|
|
3
|
+
/**
|
|
4
|
+
* Writes `value` into `target` at `offset`, most significant group first.
|
|
5
|
+
*
|
|
6
|
+
* The in-place counterpart of {@link encodeVlq}, for a caller assembling several
|
|
7
|
+
* values into one buffer without allocating per value.
|
|
8
|
+
*
|
|
9
|
+
* @param target - Destination, which must hold {@link vlqByteLength} bytes from
|
|
10
|
+
* `offset`.
|
|
11
|
+
* @param offset - Where to start writing.
|
|
12
|
+
* @param value - A non-negative safe integer.
|
|
13
|
+
* @returns The offset just past the bytes written.
|
|
14
|
+
* @throws {RangeError} When `value` is out of range, or `target` is too short.
|
|
15
|
+
*/
|
|
16
|
+
export function writeVlq(target, offset, value) {
|
|
17
|
+
const length = vlqByteLength(value);
|
|
18
|
+
if (offset < 0 || offset + length > target.length) {
|
|
19
|
+
throw new RangeError(`writeVlq: ${length} bytes at offset ${offset} do not fit in ${target.length}`);
|
|
20
|
+
}
|
|
21
|
+
let remaining = value;
|
|
22
|
+
// Filled back to front: the least significant group is known first but
|
|
23
|
+
// belongs last, and only the final byte clears the continuation bit.
|
|
24
|
+
for (let index = length - 1; index >= 0; index--) {
|
|
25
|
+
const group = remaining % RADIX;
|
|
26
|
+
target[offset + index] =
|
|
27
|
+
index === length - 1 ? group : group | CONTINUATION_BIT;
|
|
28
|
+
remaining = Math.floor(remaining / RADIX);
|
|
29
|
+
}
|
|
30
|
+
return offset + length;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=write-vlq.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"write-vlq.js","sourceRoot":"","sources":["../../src/vlq/write-vlq.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAErD;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,QAAQ,CACvB,MAAkB,EAClB,MAAc,EACd,KAAa;IAEb,MAAM,MAAM,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;IAEpC,IAAI,MAAM,GAAG,CAAC,IAAI,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;QACnD,MAAM,IAAI,UAAU,CACnB,aAAa,MAAM,oBAAoB,MAAM,kBAAkB,MAAM,CAAC,MAAM,EAAE,CAC9E,CAAC;IACH,CAAC;IAED,IAAI,SAAS,GAAG,KAAK,CAAC;IAEtB,uEAAuE;IACvE,qEAAqE;IACrE,KAAK,IAAI,KAAK,GAAG,MAAM,GAAG,CAAC,EAAE,KAAK,IAAI,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC;QAClD,MAAM,KAAK,GAAG,SAAS,GAAG,KAAK,CAAC;QAChC,MAAM,CAAC,MAAM,GAAG,KAAK,CAAC;YACrB,KAAK,KAAK,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,GAAG,gBAAgB,CAAC;QACzD,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,GAAG,KAAK,CAAC,CAAC;IAC3C,CAAC;IAED,OAAO,MAAM,GAAG,MAAM,CAAC;AACxB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ac-kit/format-varint",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Variable-length integer encodings: LEB128, VLQ and the other self-delimiting integer formats.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"encoding",
|
|
7
|
+
"leb128",
|
|
8
|
+
"varint",
|
|
9
|
+
"vlq",
|
|
10
|
+
"zigzag"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://anthochamp.github.io/node-essentials/api/format-varint/",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/anthochamp/node-essentials/issues"
|
|
15
|
+
},
|
|
16
|
+
"license": "BSD-3-Clause",
|
|
17
|
+
"author": "Anthony Champagne <dev@anthonychampagne.fr>",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/anthochamp/node-essentials.git",
|
|
21
|
+
"directory": "packages/format-varint"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"main": "./dist/index.js",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"main": "./dist/index.js",
|
|
34
|
+
"exports": {
|
|
35
|
+
".": "./dist/index.js"
|
|
36
|
+
},
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"compile": "tsc -p ./tsconfig.build.json -outDir dist",
|
|
41
|
+
"compile:check": "tsc -noEmit -p ./tsconfig.json",
|
|
42
|
+
"clean": "rimraf dist",
|
|
43
|
+
"build": "yarn clean && yarn compile",
|
|
44
|
+
"test": "yarn run -T vitest run",
|
|
45
|
+
"prepack": "yarn build"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@ac-kit/core": "^0.1.0",
|
|
49
|
+
"@ac-kit/format-core": "^0.1.0"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@ac-kit/tsconfig": "0.1.0",
|
|
53
|
+
"@ac-kit/typedoc-config": "0.1.0",
|
|
54
|
+
"@ac-kit/vitest-config": "0.1.0",
|
|
55
|
+
"@types/node": "^26.5.1",
|
|
56
|
+
"@typescript/native": "typescript@7.0.2",
|
|
57
|
+
"rimraf": "6.1.3",
|
|
58
|
+
"typedoc": "0.28.20",
|
|
59
|
+
"typescript": "@typescript/typescript6@6.0.2"
|
|
60
|
+
},
|
|
61
|
+
"engines": {
|
|
62
|
+
"node": ">=24"
|
|
63
|
+
}
|
|
64
|
+
}
|