functionalscript 0.9.1 → 0.9.3
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/README.md +3 -3
- package/bnf/data/test.f.d.ts +2 -0
- package/bnf/data/test.f.js +39 -2
- package/bnf/module.f.d.ts +6 -2
- package/bnf/module.f.js +10 -11
- package/cas/module.f.d.ts +14 -13
- package/cas/module.f.js +78 -60
- package/ci/module.f.d.ts +3 -2
- package/ci/module.f.js +6 -8
- package/ci/module.js +3 -3
- package/crypto/hmac/test.f.js +1 -1
- package/crypto/sign/test.f.js +1 -1
- package/dev/module.f.d.ts +1 -1
- package/dev/module.f.js +13 -6
- package/dev/tf/all.test.js +2 -2
- package/dev/tf/module.f.js +2 -2
- package/dev/version/module.f.d.ts +2 -11
- package/dev/version/module.f.js +17 -15
- package/dev/version/test.f.d.ts +3 -1
- package/dev/version/test.f.js +26 -11
- package/djs/parser-new/module.f.d.ts +3 -0
- package/djs/parser-new/module.f.js +149 -0
- package/djs/parser-new/test.f.d.ts +5 -0
- package/djs/parser-new/test.f.js +202 -0
- package/djs/serializer/module.f.js +6 -6
- package/fjs/module.f.d.ts +1 -1
- package/fjs/module.f.js +3 -2
- package/fsc/module.f.js +1 -1
- package/io/module.d.ts +4 -0
- package/io/module.f.d.ts +10 -4
- package/io/module.f.js +28 -0
- package/io/module.js +7 -1
- package/json/module.f.js +3 -3
- package/package.json +3 -2
- package/path/module.f.d.ts +1 -0
- package/path/module.f.js +6 -3
- package/text/utf16/module.f.js +1 -1
- package/text/utf8/module.f.d.ts +1 -1
- package/types/asn.1/module.f.d.ts +62 -0
- package/types/asn.1/module.f.js +276 -0
- package/types/asn.1/test.f.d.ts +44 -0
- package/types/asn.1/test.f.js +312 -0
- package/types/base128/module.f.d.ts +15 -0
- package/types/base128/module.f.js +38 -0
- package/types/base128/test.f.d.ts +2 -0
- package/types/base128/test.f.js +26 -0
- package/types/bit_vec/module.f.d.ts +15 -3
- package/types/bit_vec/module.f.js +22 -2
- package/types/bit_vec/test.f.d.ts +1 -0
- package/types/bit_vec/test.f.js +28 -6
- package/types/effect/mock/module.f.d.ts +5 -0
- package/types/effect/mock/module.f.js +14 -0
- package/types/effect/module.d.ts +2 -0
- package/types/effect/module.f.d.ts +23 -0
- package/types/effect/module.f.js +14 -0
- package/types/effect/module.js +11 -0
- package/types/effect/node/module.f.d.ts +56 -0
- package/types/effect/node/module.f.js +8 -0
- package/types/effect/node/test.f.d.ts +28 -0
- package/types/effect/node/test.f.js +277 -0
- package/types/effect/node/virtual/module.f.d.ts +16 -0
- package/types/effect/node/virtual/module.f.js +103 -0
- package/types/function/module.f.d.ts +1 -1
- package/types/function/module.f.js +1 -1
- package/types/function/test.f.js +1 -1
- package/types/list/module.f.js +4 -4
- package/website/module.d.ts +1 -0
- package/website/module.f.d.ts +3 -0
- package/website/module.f.js +9 -0
- package/website/module.js +3 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { bitLength, max } from "../bigint/module.f.js";
|
|
2
|
+
import { empty, isVec, length, listToVec, msb, msbCmp, uint, unpack, vec, vec8 } from "../bit_vec/module.f.js";
|
|
3
|
+
import { identity } from "../function/module.f.js";
|
|
4
|
+
import { encode as b128encode, decode as b128decode } from "../base128/module.f.js";
|
|
5
|
+
const pop = msb.popFront;
|
|
6
|
+
const pop8 = pop(8n);
|
|
7
|
+
const concat = listToVec(msb);
|
|
8
|
+
const classPcMask = 224n;
|
|
9
|
+
const tagNumberMask = 31n;
|
|
10
|
+
const parsedTagEncode = ([classPc, number]) => {
|
|
11
|
+
const [firstByteNumber, rest] = number < tagNumberMask
|
|
12
|
+
? [number, empty]
|
|
13
|
+
: [tagNumberMask, b128encode(number)];
|
|
14
|
+
return concat([vec8(classPc | firstByteNumber), rest]);
|
|
15
|
+
};
|
|
16
|
+
const parsedTagDecode = (v) => {
|
|
17
|
+
const [firstByte, rest] = pop8(v);
|
|
18
|
+
const classPc = (firstByte & classPcMask);
|
|
19
|
+
const firstByteNumber = firstByte & tagNumberMask;
|
|
20
|
+
const [number, rest1] = firstByteNumber < tagNumberMask
|
|
21
|
+
? [firstByteNumber, rest]
|
|
22
|
+
: b128decode(rest);
|
|
23
|
+
return [[classPc, number], rest1];
|
|
24
|
+
};
|
|
25
|
+
const tagEncode = (tag) => vec(max((bitLength(tag) + 7n) >> 3n)(1n) << 3n)(tag);
|
|
26
|
+
const tagDecode = (v) => {
|
|
27
|
+
const [parsedTag, rest] = parsedTagDecode(v);
|
|
28
|
+
return [uint(parsedTagEncode(parsedTag)), rest];
|
|
29
|
+
};
|
|
30
|
+
//
|
|
31
|
+
const eoc = 0x00n;
|
|
32
|
+
/** ASN.1 universal BOOLEAN tag. */
|
|
33
|
+
export const boolean = 0x01n;
|
|
34
|
+
/** ASN.1 universal INTEGER tag. */
|
|
35
|
+
export const integer = 0x02n;
|
|
36
|
+
const bitString = 0x03n;
|
|
37
|
+
/** ASN.1 universal OCTET STRING tag. */
|
|
38
|
+
export const octetString = 0x04n;
|
|
39
|
+
const null_ = 0x05;
|
|
40
|
+
/** ASN.1 universal OBJECT IDENTIFIER tag. */
|
|
41
|
+
export const objectIdentifier = 0x06n;
|
|
42
|
+
const objectDescriptor = 0x07;
|
|
43
|
+
const external = 0x08;
|
|
44
|
+
const real = 0x09;
|
|
45
|
+
const enumerated = 0x0A;
|
|
46
|
+
const embeddedPdv = 0x0B;
|
|
47
|
+
const utf8string = 0x0C;
|
|
48
|
+
const relativeOid = 0x0D;
|
|
49
|
+
const time = 0x0E;
|
|
50
|
+
const sequence = 0x10;
|
|
51
|
+
const set = 0x11;
|
|
52
|
+
const numericString = 0x12;
|
|
53
|
+
const printableString = 0x13;
|
|
54
|
+
const t61String = 0x14;
|
|
55
|
+
const videotexString = 0x15;
|
|
56
|
+
const ia5String = 0x16;
|
|
57
|
+
const utcTime = 0x17;
|
|
58
|
+
const generalizedTime = 0x18;
|
|
59
|
+
const graphicString = 0x19;
|
|
60
|
+
const visibleString = 0x1A;
|
|
61
|
+
const generalString = 0x1B;
|
|
62
|
+
const universalString = 0x1C;
|
|
63
|
+
const characterString = 0x1D;
|
|
64
|
+
const bmpString = 0x1E;
|
|
65
|
+
const date = 0x1F;
|
|
66
|
+
const timeOfDay = 0x20;
|
|
67
|
+
const dateTime = 0x21;
|
|
68
|
+
const duration = 0x22;
|
|
69
|
+
const oidIri = 0x23;
|
|
70
|
+
const relativeOidIri = 0x24;
|
|
71
|
+
const constructed = 0x20;
|
|
72
|
+
export const constructedSequence = 0x30n; // constructed | sequence
|
|
73
|
+
export const constructedSet = 0x31n; // constructed | set
|
|
74
|
+
const round8 = ({ length, uint }) => {
|
|
75
|
+
const byteLen = (length + 7n) >> 3n;
|
|
76
|
+
return { byteLen, v: vec(byteLen << 3n)(uint) };
|
|
77
|
+
};
|
|
78
|
+
const lenEncode = (uint) => {
|
|
79
|
+
if (uint < 0x80n) {
|
|
80
|
+
return vec8(uint);
|
|
81
|
+
}
|
|
82
|
+
const { byteLen, v } = round8({ length: bitLength(uint), uint });
|
|
83
|
+
return concat([vec8(0x80n | byteLen), v]);
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Decodes the length field of an ASN.1 TLV and returns the length in bits and the remaining input.
|
|
87
|
+
*
|
|
88
|
+
* @param v - The input bit vector starting with the length field.
|
|
89
|
+
* @returns A tuple containing the length in bits and the remaining input after the length field.
|
|
90
|
+
*/
|
|
91
|
+
const lenDecode = (v) => {
|
|
92
|
+
const firstAndRest = pop8(v);
|
|
93
|
+
const [first, rest1] = firstAndRest;
|
|
94
|
+
const [byteLen, rest2] = first < 0x80n ? firstAndRest : pop((first & 0x7fn) << 3n)(rest1);
|
|
95
|
+
return [byteLen << 3n, rest2];
|
|
96
|
+
};
|
|
97
|
+
/** Encodes a raw ASN.1 TLV tuple into a bit vector. */
|
|
98
|
+
export const encodeRaw = ([tag, value]) => {
|
|
99
|
+
const tagVec = tagEncode(tag);
|
|
100
|
+
const { byteLen, v } = round8(unpack(value));
|
|
101
|
+
return concat([tagVec, lenEncode(byteLen), v]);
|
|
102
|
+
};
|
|
103
|
+
/** Decodes a raw ASN.1 TLV tuple and returns the remaining input. */
|
|
104
|
+
export const decodeRaw = (v) => {
|
|
105
|
+
const [tag, v1] = tagDecode(v);
|
|
106
|
+
const [len, v2] = lenDecode(v1);
|
|
107
|
+
const [result, next] = pop(len)(v2);
|
|
108
|
+
return [[tag, vec(len)(result)], next];
|
|
109
|
+
};
|
|
110
|
+
// boolean
|
|
111
|
+
/** Encodes a JavaScript boolean as an ASN.1 BOOLEAN value. */
|
|
112
|
+
export const encodeBoolean = (b) => vec8(b ? 0xffn : 0x00n);
|
|
113
|
+
/** Decodes an ASN.1 BOOLEAN value. */
|
|
114
|
+
export const decodeBoolean = (v) => uint(v) !== 0n;
|
|
115
|
+
// integer (two's compliment)
|
|
116
|
+
/** Encodes a signed bigint using ASN.1 INTEGER two's complement representation. */
|
|
117
|
+
export const encodeInteger = (uint) => {
|
|
118
|
+
const offset = uint < 0n ? 1n : 0n;
|
|
119
|
+
return round8({ length: bitLength(uint + offset) + 1n, uint }).v;
|
|
120
|
+
};
|
|
121
|
+
/** Decodes an ASN.1 INTEGER encoded in two's complement. */
|
|
122
|
+
export const decodeInteger = (v) => {
|
|
123
|
+
const { length, uint } = unpack(v);
|
|
124
|
+
const sign = uint >> (length - 1n);
|
|
125
|
+
return sign === 0n ? uint : uint - (1n << length);
|
|
126
|
+
};
|
|
127
|
+
// octet string
|
|
128
|
+
/** Encodes an OCTET STRING value. */
|
|
129
|
+
export const encodeOctetString = (v) => v;
|
|
130
|
+
/** Decodes an OCTET STRING value. */
|
|
131
|
+
export const decodeOctetString = (v) => v;
|
|
132
|
+
/** Encodes an OBJECT IDENTIFIER value. */
|
|
133
|
+
export const encodeObjectIdentifier = (oid) => {
|
|
134
|
+
const [first, second, ...rest] = oid;
|
|
135
|
+
const firstByte = first * 40n + second;
|
|
136
|
+
return concat([vec8(firstByte), ...rest.map(b128encode)]);
|
|
137
|
+
};
|
|
138
|
+
/** Decodes an OBJECT IDENTIFIER value. */
|
|
139
|
+
export const decodeObjectIdentifier = (v) => {
|
|
140
|
+
const [firstByte, rest] = pop8(v);
|
|
141
|
+
const first = firstByte / 40n;
|
|
142
|
+
const second = firstByte % 40n;
|
|
143
|
+
let result = [first, second];
|
|
144
|
+
let tail = rest;
|
|
145
|
+
while (length(tail) > 0n) {
|
|
146
|
+
const [value, next] = b128decode(tail);
|
|
147
|
+
result = [...result, value];
|
|
148
|
+
tail = next;
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
};
|
|
152
|
+
const genericEncodeSequence = (map) => (...records) => concat(map(records.map(encode)));
|
|
153
|
+
/** Encodes a SEQUENCE payload from ordered records. */
|
|
154
|
+
export const encodeSequence = genericEncodeSequence(identity);
|
|
155
|
+
/** Decodes a SEQUENCE payload into records. */
|
|
156
|
+
export const decodeSequence = (v) => {
|
|
157
|
+
let result = [];
|
|
158
|
+
while (length(v) !== 0n) {
|
|
159
|
+
const [record, rest] = decode(v);
|
|
160
|
+
result = [...result, record];
|
|
161
|
+
v = rest;
|
|
162
|
+
}
|
|
163
|
+
return result;
|
|
164
|
+
};
|
|
165
|
+
/** Encodes a SET payload with canonical byte ordering. */
|
|
166
|
+
export const encodeSet = genericEncodeSequence(v => v.toSorted((a, b) => msbCmp(a)(b)));
|
|
167
|
+
/** Decodes a SET payload. */
|
|
168
|
+
export const decodeSet = decodeSequence;
|
|
169
|
+
// encode
|
|
170
|
+
const recordToRaw = ([tag, value]) => {
|
|
171
|
+
switch (tag) {
|
|
172
|
+
case boolean: return encodeBoolean(value);
|
|
173
|
+
case integer: return encodeInteger(value);
|
|
174
|
+
case octetString: return encodeOctetString(value);
|
|
175
|
+
case objectIdentifier: return encodeObjectIdentifier(value);
|
|
176
|
+
case constructedSequence: return encodeSequence(...value);
|
|
177
|
+
case constructedSet: return encodeSet(...value);
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
/** Encodes a supported ASN.1 record as TLV. */
|
|
181
|
+
export const encode = (record) => isVec(record) ? record : encodeRaw([record[0], recordToRaw(record)]);
|
|
182
|
+
// decode
|
|
183
|
+
const rawToRecord = (raw) => {
|
|
184
|
+
const [tag, value] = raw;
|
|
185
|
+
switch (tag) {
|
|
186
|
+
case boolean: return [boolean, decodeBoolean(value)];
|
|
187
|
+
case integer: return [integer, decodeInteger(value)];
|
|
188
|
+
case octetString: return [octetString, decodeOctetString(value)];
|
|
189
|
+
case objectIdentifier: return [objectIdentifier, decodeObjectIdentifier(value)];
|
|
190
|
+
case constructedSequence: return [constructedSequence, decodeSequence(value)];
|
|
191
|
+
case constructedSet: return [constructedSet, decodeSet(value)];
|
|
192
|
+
default: return encodeRaw(raw);
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
/** Decodes one supported ASN.1 record and returns the remaining input. */
|
|
196
|
+
export const decode = (v) => {
|
|
197
|
+
const [raw, rest] = decodeRaw(v);
|
|
198
|
+
return [rawToRecord(raw), rest];
|
|
199
|
+
};
|
|
200
|
+
/*
|
|
201
|
+
TimeStampReq ::= SEQUENCE {
|
|
202
|
+
version INTEGER { v1(1) }, // [x]
|
|
203
|
+
messageImprint MessageImprint,
|
|
204
|
+
reqPolicy TSAPolicyId OPTIONAL,
|
|
205
|
+
nonce INTEGER OPTIONAL, // [X]
|
|
206
|
+
certReq BOOLEAN DEFAULT FALSE, // [X]
|
|
207
|
+
extensions [0] IMPLICIT Extensions OPTIONAL // [X]
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
MessageImprint ::= SEQUENCE {
|
|
211
|
+
hashAlgorithm AlgorithmIdentifier,
|
|
212
|
+
hashedMessage OCTET STRING // [X]
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
TSAPolicyId ::= OBJECT IDENTIFIER // [X]
|
|
216
|
+
*/
|
|
217
|
+
/*
|
|
218
|
+
TimeStampResp ::= SEQUENCE {
|
|
219
|
+
status PKIStatusInfo,
|
|
220
|
+
timeStampToken TimeStampToken OPTIONAL
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
PKIStatusInfo ::= SEQUENCE {
|
|
224
|
+
status PKIStatus,
|
|
225
|
+
statusString PKIFreeText OPTIONAL,
|
|
226
|
+
failInfo PKIFailureInfo OPTIONAL
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
PKIStatus ::= INTEGER { // [X]
|
|
230
|
+
granted (0),
|
|
231
|
+
grantedWithMods (1),
|
|
232
|
+
rejection (2),
|
|
233
|
+
waiting (3),
|
|
234
|
+
revocationWarning (4),
|
|
235
|
+
revocationNotification (5)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
TimeStampToken ::= ContentInfo
|
|
239
|
+
|
|
240
|
+
ContentInfo ::= SEQUENCE {
|
|
241
|
+
contentType ContentType,
|
|
242
|
+
content [0] EXPLICIT ANY DEFINED BY contentType
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
ContentType ::= OBJECT IDENTIFIER
|
|
246
|
+
|
|
247
|
+
SignedData ::= SEQUENCE {
|
|
248
|
+
version CMSVersion,
|
|
249
|
+
digestAlgorithms SET OF DigestAlgorithmIdentifier, // [X]
|
|
250
|
+
encapContentInfo EncapsulatedContentInfo,
|
|
251
|
+
certificates [0] IMPLICIT CertificateSet OPTIONAL,
|
|
252
|
+
crls [1] IMPLICIT RevocationInfoChoices OPTIONAL,
|
|
253
|
+
signerInfos SET OF SignerInfo // [X]
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
EncapsulatedContentInfo ::= SEQUENCE {
|
|
257
|
+
eContentType ContentType,
|
|
258
|
+
eContent [0] EXPLICIT OCTET STRING OPTIONAL
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
TSTInfo ::= SEQUENCE {
|
|
262
|
+
version INTEGER { v1(1) },
|
|
263
|
+
policy TSAPolicyId,
|
|
264
|
+
messageImprint MessageImprint,
|
|
265
|
+
serialNumber INTEGER,
|
|
266
|
+
genTime GeneralizedTime,
|
|
267
|
+
accuracy Accuracy OPTIONAL,
|
|
268
|
+
ordering BOOLEAN DEFAULT FALSE,
|
|
269
|
+
nonce INTEGER OPTIONAL,
|
|
270
|
+
tsa [0] GeneralName OPTIONAL,
|
|
271
|
+
extensions [1] IMPLICIT Extensions OPTIONAL
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
Bits: 8 7 | 6 | 5 4 3 2 1
|
|
275
|
+
Class | P/C | Tag number
|
|
276
|
+
*/
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
encodeSmall: () => void;
|
|
3
|
+
encode7F: () => void;
|
|
4
|
+
encode80: () => void;
|
|
5
|
+
encodeFF: () => void;
|
|
6
|
+
encode103: () => void;
|
|
7
|
+
ed: ((() => void) | {
|
|
8
|
+
x80: () => void;
|
|
9
|
+
x100: () => void;
|
|
10
|
+
x1000: () => void;
|
|
11
|
+
x10000: () => void;
|
|
12
|
+
x20000: () => void;
|
|
13
|
+
x40000: () => void;
|
|
14
|
+
x80000: () => void;
|
|
15
|
+
xC0000: () => void;
|
|
16
|
+
xE0000: () => void;
|
|
17
|
+
xF0000: () => void;
|
|
18
|
+
xFFF00: () => void;
|
|
19
|
+
xFFF80: () => void;
|
|
20
|
+
xFFFC0: () => void;
|
|
21
|
+
xFFFD0: () => void;
|
|
22
|
+
})[];
|
|
23
|
+
integerValue: {
|
|
24
|
+
zero: () => void;
|
|
25
|
+
one: () => void;
|
|
26
|
+
minusOne: () => void;
|
|
27
|
+
x7F: () => void;
|
|
28
|
+
x80: () => void;
|
|
29
|
+
xFF: () => void;
|
|
30
|
+
nx7F: () => void;
|
|
31
|
+
nx80: () => void;
|
|
32
|
+
nx81: () => void;
|
|
33
|
+
nx7FFF: () => void;
|
|
34
|
+
nx8000: () => void;
|
|
35
|
+
nx8001: () => void;
|
|
36
|
+
};
|
|
37
|
+
encodeDecode: {
|
|
38
|
+
integer: () => void;
|
|
39
|
+
sequence: () => void;
|
|
40
|
+
set: () => void;
|
|
41
|
+
};
|
|
42
|
+
raw: (() => void)[];
|
|
43
|
+
};
|
|
44
|
+
export default _default;
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import { empty, length, listToVec, msb, uint, unpack, vec, vec8 } from "../bit_vec/module.f.js";
|
|
2
|
+
import { asBase } from "../nominal/module.f.js";
|
|
3
|
+
import { decodeRaw, decodeInteger, encodeRaw, encodeInteger, integer, encode, decode, constructedSequence, octetString, boolean, constructedSet } from "./module.f.js";
|
|
4
|
+
const { concat, popFront: pop } = msb;
|
|
5
|
+
const cat = listToVec(msb);
|
|
6
|
+
const pop8 = pop(8n);
|
|
7
|
+
const check = (tag, v, rest) => {
|
|
8
|
+
const s = encodeRaw([tag, v]);
|
|
9
|
+
const [[t0, v0], r] = decodeRaw(concat(s)(rest));
|
|
10
|
+
if (t0 !== tag) {
|
|
11
|
+
throw `t0: ${t0}`;
|
|
12
|
+
}
|
|
13
|
+
if (v0 !== v) {
|
|
14
|
+
throw `v0: ${asBase(v0)}`;
|
|
15
|
+
}
|
|
16
|
+
if (r !== rest) {
|
|
17
|
+
throw `r: ${asBase(r)}`;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const integerValueCheck = (i, v) => {
|
|
21
|
+
const v0 = encodeInteger(i);
|
|
22
|
+
if (v !== v0) {
|
|
23
|
+
throw `encode: ${asBase(v)}, ${asBase(v0)}`;
|
|
24
|
+
}
|
|
25
|
+
const i0 = decodeInteger(v);
|
|
26
|
+
if (i !== i0) {
|
|
27
|
+
throw [i, i0];
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const ch0 = (r, v, rest) => {
|
|
31
|
+
const [r0, rest0] = decode(concat(v)(rest));
|
|
32
|
+
if (rest0 !== rest) {
|
|
33
|
+
throw `rest: ${asBase(rest0)}`;
|
|
34
|
+
}
|
|
35
|
+
const v0 = encode(r);
|
|
36
|
+
const v1 = encode(r0);
|
|
37
|
+
if (v !== v0) {
|
|
38
|
+
throw `encode: ${asBase(v)}, ${asBase(v0)}`;
|
|
39
|
+
}
|
|
40
|
+
if (v !== v1) {
|
|
41
|
+
throw `encode: ${asBase(v)}, ${asBase(v1)}`;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const ch = (r, v) => {
|
|
45
|
+
ch0(r, v, empty);
|
|
46
|
+
ch0(r, v, vec8(0x23n));
|
|
47
|
+
ch0(r, v, vec(16n)(0x2345n));
|
|
48
|
+
};
|
|
49
|
+
export default {
|
|
50
|
+
encodeSmall: () => {
|
|
51
|
+
const v = vec8(0x13n);
|
|
52
|
+
const x = encodeRaw([integer, v]);
|
|
53
|
+
const lx = length(x);
|
|
54
|
+
if (lx !== 24n) {
|
|
55
|
+
throw lx;
|
|
56
|
+
}
|
|
57
|
+
const [tag, x1] = pop8(x);
|
|
58
|
+
if (tag !== BigInt(integer)) {
|
|
59
|
+
throw tag;
|
|
60
|
+
}
|
|
61
|
+
const [len, x2] = pop8(x1);
|
|
62
|
+
if (len !== 1n) {
|
|
63
|
+
throw len;
|
|
64
|
+
}
|
|
65
|
+
const { length: intLen, uint } = unpack(x2);
|
|
66
|
+
if (intLen !== 8n) {
|
|
67
|
+
throw intLen;
|
|
68
|
+
}
|
|
69
|
+
if (uint !== 0x13n) {
|
|
70
|
+
throw uint;
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
encode7F: () => {
|
|
74
|
+
const valueLen = 0x7fn << 3n;
|
|
75
|
+
const value = 0x1234n;
|
|
76
|
+
const v = vec(valueLen)(value);
|
|
77
|
+
const x = encodeRaw([integer, v]);
|
|
78
|
+
const lx = length(x);
|
|
79
|
+
if (lx !== 0x81n << 3n) {
|
|
80
|
+
throw lx;
|
|
81
|
+
}
|
|
82
|
+
const [tag, x1] = pop8(x);
|
|
83
|
+
if (tag !== BigInt(integer)) {
|
|
84
|
+
throw tag;
|
|
85
|
+
}
|
|
86
|
+
const [len, x2] = pop8(x1);
|
|
87
|
+
if (len !== 0x7fn) {
|
|
88
|
+
throw len;
|
|
89
|
+
}
|
|
90
|
+
const { length: intLen, uint } = unpack(x2);
|
|
91
|
+
if (intLen !== valueLen) {
|
|
92
|
+
throw intLen;
|
|
93
|
+
}
|
|
94
|
+
if (uint !== value) {
|
|
95
|
+
throw uint;
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
encode80: () => {
|
|
99
|
+
const valueLen = 0x80n << 3n;
|
|
100
|
+
const value = 0x123456n;
|
|
101
|
+
const v = vec(valueLen)(value);
|
|
102
|
+
const x = encodeRaw([integer, v]);
|
|
103
|
+
const lx = length(x);
|
|
104
|
+
if (lx !== 0x83n << 3n) {
|
|
105
|
+
throw lx;
|
|
106
|
+
}
|
|
107
|
+
const [tag, x1] = pop8(x);
|
|
108
|
+
if (tag !== BigInt(integer)) {
|
|
109
|
+
throw tag;
|
|
110
|
+
}
|
|
111
|
+
const [lenLen, x2] = pop8(x1);
|
|
112
|
+
if (lenLen !== 0x81n) {
|
|
113
|
+
throw lenLen;
|
|
114
|
+
}
|
|
115
|
+
const [len, x3] = pop8(x2);
|
|
116
|
+
if (len !== 0x80n) {
|
|
117
|
+
throw len;
|
|
118
|
+
}
|
|
119
|
+
const { length: intLen, uint } = unpack(x3);
|
|
120
|
+
if (intLen !== valueLen) {
|
|
121
|
+
throw intLen;
|
|
122
|
+
}
|
|
123
|
+
if (uint !== value) {
|
|
124
|
+
throw uint;
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
encodeFF: () => {
|
|
128
|
+
const valueLen = 0xffn << 3n;
|
|
129
|
+
const value = 0x123456n;
|
|
130
|
+
const v = vec(valueLen)(value);
|
|
131
|
+
const x = encodeRaw([integer, v]);
|
|
132
|
+
const lx = length(x);
|
|
133
|
+
if (lx !== valueLen + (3n << 3n)) {
|
|
134
|
+
throw `lx: ${lx}`;
|
|
135
|
+
}
|
|
136
|
+
const [tag, x1] = pop8(x);
|
|
137
|
+
if (tag !== BigInt(integer)) {
|
|
138
|
+
throw tag;
|
|
139
|
+
}
|
|
140
|
+
const [lenLen, x2] = pop8(x1);
|
|
141
|
+
if (lenLen !== 0x81n) {
|
|
142
|
+
throw lenLen;
|
|
143
|
+
}
|
|
144
|
+
const [len, x3] = pop8(x2);
|
|
145
|
+
if (len !== 0xffn) {
|
|
146
|
+
throw len;
|
|
147
|
+
}
|
|
148
|
+
const { length: intLen, uint } = unpack(x3);
|
|
149
|
+
if (intLen !== valueLen) {
|
|
150
|
+
throw intLen;
|
|
151
|
+
}
|
|
152
|
+
if (uint !== value) {
|
|
153
|
+
throw uint;
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
encode103: () => {
|
|
157
|
+
const valueLen = 0x103n << 3n;
|
|
158
|
+
const value = 0x123456n;
|
|
159
|
+
const v = vec(valueLen)(value);
|
|
160
|
+
const x = encodeRaw([integer, v]);
|
|
161
|
+
const lx = length(x);
|
|
162
|
+
if (lx !== valueLen + (4n << 3n)) {
|
|
163
|
+
throw `lx: ${lx}`;
|
|
164
|
+
}
|
|
165
|
+
const [tag, x1] = pop8(x);
|
|
166
|
+
if (tag !== BigInt(integer)) {
|
|
167
|
+
throw tag;
|
|
168
|
+
}
|
|
169
|
+
const [lenLen, x2] = pop8(x1);
|
|
170
|
+
if (lenLen !== 0x82n) {
|
|
171
|
+
throw lenLen;
|
|
172
|
+
}
|
|
173
|
+
const [len, x3] = pop(16n)(x2);
|
|
174
|
+
if (len !== 0x103n) {
|
|
175
|
+
throw len;
|
|
176
|
+
}
|
|
177
|
+
const { length: intLen, uint } = unpack(x3);
|
|
178
|
+
if (intLen !== valueLen) {
|
|
179
|
+
throw intLen;
|
|
180
|
+
}
|
|
181
|
+
if (uint !== value) {
|
|
182
|
+
throw uint;
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
ed: [
|
|
186
|
+
() => {
|
|
187
|
+
const tag = integer;
|
|
188
|
+
const v = vec(0x10n)(0x8234n);
|
|
189
|
+
if (asBase(v) !== 0x8234n) {
|
|
190
|
+
throw asBase(v).toString(16);
|
|
191
|
+
}
|
|
192
|
+
check(integer, v, empty);
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
x80: () => check(integer, vec(0x80n)(0x8234n), empty),
|
|
196
|
+
x100: () => check(integer, vec(0x100n)(0x8234n), vec8(0x23n)),
|
|
197
|
+
x1000: () => check(integer, vec(0x1000n)(0x8234n), vec8(0x23n)),
|
|
198
|
+
x10000: () => check(integer, vec(0x10000n)(0x8234n), vec8(0x23n)),
|
|
199
|
+
x20000: () => check(integer, vec(0x20000n)(0x8234n), vec8(0x23n)),
|
|
200
|
+
x40000: () => check(integer, vec(0x40000n)(0x8234n), empty),
|
|
201
|
+
x80000: () => check(integer, vec(0x80000n)(0x8234n), empty),
|
|
202
|
+
xC0000: () => check(integer, vec(0xc0000n)(0x8234n), empty),
|
|
203
|
+
xE0000: () => check(integer, vec(0xe0000n)(0x8234n), empty),
|
|
204
|
+
xF0000: () => check(integer, vec(0xf0000n)(0x8234n), empty),
|
|
205
|
+
xFFF00: () => check(integer, vec(0xfff00n)(0x8234n), empty),
|
|
206
|
+
xFFF80: () => check(integer, vec(0xfff80n)(0x8234n), empty),
|
|
207
|
+
xFFFC0: () => check(integer, vec(0xfffc0n)(0x8234n), empty),
|
|
208
|
+
xFFFD0: () => check(integer, vec(0xfffd0n)(0x8234n), empty),
|
|
209
|
+
//// fail on Bun because it has a smaller limit for BigInt
|
|
210
|
+
//xFFFD8: () => check(integer, vec(0xF_FFD8n)(0x8234n), empty),
|
|
211
|
+
//xFFFE0: () => check(integer, vec(0xF_FFE0n)(0x8234n), empty),
|
|
212
|
+
//e100000: () => check(integer, vec(0x10_0000n)(0x8234n), empty),
|
|
213
|
+
//x100000: () => check(integer, vec(0x10_0000n)(0x8234n), vec8(0x23n)),
|
|
214
|
+
//x1000000: () => check(integer, vec(0x100_0000n)(0x8234n), vec8(0x23n)),
|
|
215
|
+
//x10000000: () => check(integer, vec(0x1000_0000n)(0x8234n), vec8(0x23n)),
|
|
216
|
+
//x20000000: () => check(integer, vec(0x2000_0000n)(0x8234n), vec8(0x23n)),
|
|
217
|
+
//x30000000: () => check(integer, vec(0x3000_0000n)(0x8234n), vec8(0x23n)),
|
|
218
|
+
//x38000000: () => check(integer, vec(0x3800_0000n)(0x8234n), vec8(0x23n)),
|
|
219
|
+
//x3C000000: () => check(integer, vec(0x3800_0000n)(0x8234n), vec8(0x23n)),
|
|
220
|
+
//x3E000000: () => check(integer, vec(0x3E00_0000n)(0x8234n), vec8(0x23n)),
|
|
221
|
+
//x3F000000: () => check(integer, vec(0x3F00_0000n)(0x8234n), vec8(0x23n)),
|
|
222
|
+
//x3F800000: () => check(integer, vec(0x3F80_0000n)(0x8234n), vec8(0x23n)),
|
|
223
|
+
//x3FFFFFC0: () => check(integer, vec(0x3FFF_FFC0n)(0x8234n), vec8(0x23n)),
|
|
224
|
+
//e3FFFFFC8: () => check(integer, vec(0x3FFF_FFC8n)(0x8234n), empty),
|
|
225
|
+
//// fail on Node
|
|
226
|
+
//x3FFFFFC8: () => check(integer, vec(0x3FFF_FFC1n)(0x8234n), vec8(0x23n)),
|
|
227
|
+
//x40000000: () => check(integer, vec(0x4000_0000n)(0x8234n), vec8(0x23n)),
|
|
228
|
+
// check(integer, vec(0x1_0000_0000n)(0x8234n), vec8(0x23n))
|
|
229
|
+
},
|
|
230
|
+
],
|
|
231
|
+
integerValue: {
|
|
232
|
+
zero: () => integerValueCheck(0n, vec8(0n)),
|
|
233
|
+
one: () => integerValueCheck(1n, vec8(1n)),
|
|
234
|
+
minusOne: () => integerValueCheck(-1n, vec8(0xffn)),
|
|
235
|
+
x7F: () => integerValueCheck(0x7fn, vec8(0x7fn)),
|
|
236
|
+
x80: () => integerValueCheck(0x80n, vec(16n)(0x80n)),
|
|
237
|
+
xFF: () => integerValueCheck(0xffn, vec(16n)(0xffn)),
|
|
238
|
+
nx7F: () => integerValueCheck(-0x7fn, vec8(0x81n)),
|
|
239
|
+
nx80: () => integerValueCheck(-0x80n, vec8(0x80n)),
|
|
240
|
+
nx81: () => integerValueCheck(-0x81n, vec(16n)(0xff7fn)),
|
|
241
|
+
nx7FFF: () => integerValueCheck(-0x7fffn, vec(16n)(0x8001n)),
|
|
242
|
+
nx8000: () => integerValueCheck(-0x8000n, vec(16n)(0x8000n)),
|
|
243
|
+
nx8001: () => integerValueCheck(-0x8001n, vec(24n)(0xff7fffn)),
|
|
244
|
+
},
|
|
245
|
+
encodeDecode: {
|
|
246
|
+
integer: () => {
|
|
247
|
+
ch([integer, 0n], cat([vec8(BigInt(integer)), vec8(1n), vec8(0n)]));
|
|
248
|
+
ch([integer, 1n], cat([vec8(BigInt(integer)), vec8(1n), vec8(1n)]));
|
|
249
|
+
},
|
|
250
|
+
sequence: () => {
|
|
251
|
+
ch([constructedSequence, []], cat([vec8(BigInt(constructedSequence)), vec8(0n)]));
|
|
252
|
+
ch([constructedSequence, [[integer, 0n]]], cat([vec8(BigInt(constructedSequence)), vec8(3n), encode([integer, 0n])]));
|
|
253
|
+
ch([constructedSequence, [[integer, 1n], [integer, 2n]]], cat([
|
|
254
|
+
vec8(BigInt(constructedSequence)),
|
|
255
|
+
vec8(6n),
|
|
256
|
+
encode([integer, 1n]),
|
|
257
|
+
encode([integer, 2n])
|
|
258
|
+
]));
|
|
259
|
+
ch([constructedSequence, [[octetString, vec8(0x23n)], [boolean, true], [boolean, false]]], cat([
|
|
260
|
+
vec8(BigInt(constructedSequence)),
|
|
261
|
+
vec8(9n),
|
|
262
|
+
encode([octetString, vec8(0x23n)]),
|
|
263
|
+
encode([boolean, true]),
|
|
264
|
+
encode([boolean, false]),
|
|
265
|
+
]));
|
|
266
|
+
},
|
|
267
|
+
set: () => {
|
|
268
|
+
ch([constructedSet, [[integer, 2n], [integer, 1n]]], cat([
|
|
269
|
+
vec8(BigInt(constructedSet)),
|
|
270
|
+
vec8(6n),
|
|
271
|
+
encode([integer, 1n]),
|
|
272
|
+
encode([integer, 2n])
|
|
273
|
+
]));
|
|
274
|
+
},
|
|
275
|
+
},
|
|
276
|
+
raw: [
|
|
277
|
+
() => {
|
|
278
|
+
const e = encodeRaw([0x00n, vec8(0x23n)]);
|
|
279
|
+
if (e !== cat([vec8(0x00n), vec8(1n), vec8(0x23n)])) {
|
|
280
|
+
throw `encode: ${length(e)}: ${uint(e).toString(2)}`;
|
|
281
|
+
}
|
|
282
|
+
const [[tag, value], rest] = decodeRaw(e);
|
|
283
|
+
if (rest !== empty) {
|
|
284
|
+
throw `rest: ${asBase(rest)}`;
|
|
285
|
+
}
|
|
286
|
+
if (tag !== 0x00n) {
|
|
287
|
+
throw `tag: ${tag}`;
|
|
288
|
+
}
|
|
289
|
+
if (value !== vec8(0x23n)) {
|
|
290
|
+
throw `value: ${asBase(value)}`;
|
|
291
|
+
}
|
|
292
|
+
},
|
|
293
|
+
() => {
|
|
294
|
+
const e = encodeRaw([0x1f20n, vec(16n)(0x1234n)]);
|
|
295
|
+
if (e !== cat([vec8(0x1fn), vec8(0x20n), vec8(2n), vec(16n)(0x1234n)])) {
|
|
296
|
+
const l = length(e);
|
|
297
|
+
const u = uint(e);
|
|
298
|
+
throw `encode: ${l}: ${u.toString(16)}`;
|
|
299
|
+
}
|
|
300
|
+
const [[tag, value], rest] = decodeRaw(e);
|
|
301
|
+
if (rest !== empty) {
|
|
302
|
+
throw `rest: ${asBase(rest)}`;
|
|
303
|
+
}
|
|
304
|
+
if (tag !== 0x1f20n) {
|
|
305
|
+
throw `tag: ${tag}`;
|
|
306
|
+
}
|
|
307
|
+
if (value !== vec(16n)(0x1234n)) {
|
|
308
|
+
throw `value: ${asBase(value)}`;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
]
|
|
312
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type Vec } from '../bit_vec/module.f.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Encodes a bigint into an MSB Base128 vector.
|
|
4
|
+
*
|
|
5
|
+
* @param uint The bigint to encode.
|
|
6
|
+
* @returns The encoded MSB Base128 vector.
|
|
7
|
+
*/
|
|
8
|
+
export declare const encode: (uint: bigint) => Vec;
|
|
9
|
+
/**
|
|
10
|
+
* Decodes an MSB Base128 vector into a bigint.
|
|
11
|
+
*
|
|
12
|
+
* @param v The MSB Base128 vector to decode.
|
|
13
|
+
* @returns A tuple containing the decoded bigint and the remaining vector.
|
|
14
|
+
*/
|
|
15
|
+
export declare const decode: (v: Vec) => readonly [bigint, Vec];
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { vec8, msb, empty } from "../bit_vec/module.f.js";
|
|
2
|
+
const { concat, popFront } = msb;
|
|
3
|
+
const pop8 = popFront(8n);
|
|
4
|
+
/**
|
|
5
|
+
* Encodes a bigint into an MSB Base128 vector.
|
|
6
|
+
*
|
|
7
|
+
* @param uint The bigint to encode.
|
|
8
|
+
* @returns The encoded MSB Base128 vector.
|
|
9
|
+
*/
|
|
10
|
+
export const encode = (uint) => {
|
|
11
|
+
let result = empty;
|
|
12
|
+
while (true) {
|
|
13
|
+
const item = uint & 0x7fn;
|
|
14
|
+
const flag = result === empty ? 0n : 0x80n;
|
|
15
|
+
result = concat(vec8(flag | item))(result);
|
|
16
|
+
uint >>= 7n;
|
|
17
|
+
if (uint === 0n) {
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Decodes an MSB Base128 vector into a bigint.
|
|
24
|
+
*
|
|
25
|
+
* @param v The MSB Base128 vector to decode.
|
|
26
|
+
* @returns A tuple containing the decoded bigint and the remaining vector.
|
|
27
|
+
*/
|
|
28
|
+
export const decode = (v) => {
|
|
29
|
+
let result = 0n;
|
|
30
|
+
while (true) {
|
|
31
|
+
const [byte, rest] = pop8(v);
|
|
32
|
+
result = (result << 7n) | (byte & 0x7fn);
|
|
33
|
+
if (byte < 0x80n) {
|
|
34
|
+
return [result, rest];
|
|
35
|
+
}
|
|
36
|
+
v = rest;
|
|
37
|
+
}
|
|
38
|
+
};
|