@ac-kit/format-asn1 0.2.2 → 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 +110 -0
- package/README.md +65 -7
- package/dist/_encoding/def-tags.d.ts +69 -0
- package/dist/_encoding/def-tags.d.ts.map +1 -0
- package/dist/_encoding/def-tags.js +239 -0
- package/dist/_encoding/def-tags.js.map +1 -0
- package/dist/_encoding/default-output.d.ts +9 -0
- package/dist/_encoding/default-output.d.ts.map +1 -0
- package/dist/_encoding/default-output.js +43 -0
- package/dist/_encoding/default-output.js.map +1 -0
- package/dist/_encoding/default-wire-value.d.ts +9 -0
- package/dist/_encoding/default-wire-value.d.ts.map +1 -0
- package/dist/_encoding/default-wire-value.js +56 -0
- package/dist/_encoding/default-wire-value.js.map +1 -0
- package/dist/_encoding/tag.d.ts +6 -1
- package/dist/_encoding/tag.d.ts.map +1 -1
- package/dist/_encoding/tag.js +10 -2
- package/dist/_encoding/tag.js.map +1 -1
- package/dist/_encoding/values.d.ts +70 -8
- package/dist/_encoding/values.d.ts.map +1 -1
- package/dist/_encoding/values.js +302 -72
- package/dist/_encoding/values.js.map +1 -1
- package/dist/ber/decode.d.ts.map +1 -1
- package/dist/ber/decode.js +25 -34
- package/dist/ber/decode.js.map +1 -1
- package/dist/ber/encode.js +1 -0
- package/dist/ber/encode.js.map +1 -1
- package/dist/cer/decode.d.ts.map +1 -1
- package/dist/cer/decode.js +26 -29
- package/dist/cer/decode.js.map +1 -1
- package/dist/cer/encode.d.ts +3 -0
- package/dist/cer/encode.d.ts.map +1 -1
- package/dist/cer/encode.js +52 -5
- package/dist/cer/encode.js.map +1 -1
- package/dist/cer.d.ts +1 -1
- package/dist/cer.d.ts.map +1 -1
- package/dist/cer.js +1 -1
- package/dist/cer.js.map +1 -1
- package/dist/der/decode.d.ts +57 -2
- package/dist/der/decode.d.ts.map +1 -1
- package/dist/der/decode.js +581 -139
- package/dist/der/decode.js.map +1 -1
- package/dist/der/default-encoding.d.ts +4 -0
- package/dist/der/default-encoding.d.ts.map +1 -0
- package/dist/der/default-encoding.js +25 -0
- package/dist/der/default-encoding.js.map +1 -0
- package/dist/der/encode.d.ts +20 -6
- package/dist/der/encode.d.ts.map +1 -1
- package/dist/der/encode.js +122 -115
- package/dist/der/encode.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/per/decode.d.ts.map +1 -1
- package/dist/per/decode.js +16 -6
- package/dist/per/decode.js.map +1 -1
- package/dist/per/encode.d.ts +3 -0
- package/dist/per/encode.d.ts.map +1 -1
- package/dist/per/encode.js +31 -13
- package/dist/per/encode.js.map +1 -1
- package/dist/per.d.ts +1 -1
- package/dist/per.d.ts.map +1 -1
- package/dist/per.js +1 -1
- package/dist/per.js.map +1 -1
- package/dist/schema/_lazy-resolution.d.ts +9 -0
- package/dist/schema/_lazy-resolution.d.ts.map +1 -0
- package/dist/schema/_lazy-resolution.js +15 -0
- package/dist/schema/_lazy-resolution.js.map +1 -0
- package/dist/schema/errors.d.ts +7 -0
- package/dist/schema/errors.d.ts.map +1 -0
- package/dist/schema/errors.js +10 -0
- package/dist/schema/errors.js.map +1 -0
- package/dist/schema/types/base.d.ts +1 -1
- package/dist/schema/types/base.d.ts.map +1 -1
- package/dist/schema/types/constructed/choice.d.ts +2 -2
- package/dist/schema/types/constructed/choice.d.ts.map +1 -1
- package/dist/schema/types/constructed/choice.js.map +1 -1
- package/dist/schema/types/constructed/component.d.ts +60 -18
- package/dist/schema/types/constructed/component.d.ts.map +1 -1
- package/dist/schema/types/constructed/component.js +15 -2
- package/dist/schema/types/constructed/component.js.map +1 -1
- package/dist/schema/types/constructed/sequence-of.d.ts +2 -2
- package/dist/schema/types/constructed/sequence-of.d.ts.map +1 -1
- package/dist/schema/types/constructed/sequence.d.ts +2 -2
- package/dist/schema/types/constructed/sequence.d.ts.map +1 -1
- package/dist/schema/types/constructed/sequence.js.map +1 -1
- package/dist/schema/types/constructed/set-of.d.ts +2 -2
- package/dist/schema/types/constructed/set-of.d.ts.map +1 -1
- package/dist/schema/types/constructed/set.d.ts +2 -2
- package/dist/schema/types/constructed/set.d.ts.map +1 -1
- package/dist/schema/types/constructed/set.js.map +1 -1
- package/dist/schema/types/primitives/lazy.d.ts +6 -6
- package/dist/schema/types/primitives/lazy.d.ts.map +1 -1
- package/dist/schema/types/primitives/lazy.js.map +1 -1
- package/dist/schema/validate.d.ts +4 -1
- package/dist/schema/validate.d.ts.map +1 -1
- package/dist/schema/validate.js +4 -1
- package/dist/schema/validate.js.map +1 -1
- package/dist/schema/values.d.ts +6 -0
- package/dist/schema/values.d.ts.map +1 -1
- package/dist/schema/walk.d.ts +38 -5
- package/dist/schema/walk.d.ts.map +1 -1
- package/dist/schema/walk.js +89 -52
- package/dist/schema/walk.js.map +1 -1
- package/package.json +6 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,115 @@
|
|
|
1
1
|
# @ac-kit/format-asn1
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 5152230: DER follows X.690 §10–11 in both directions.
|
|
8
|
+
|
|
9
|
+
**Breaking:** `derDecode` refuses input it used to accept, and `derEncode` emits
|
|
10
|
+
different bytes for some values (DEFAULT values omitted, times converted to UTC);
|
|
11
|
+
a decoded absent DEFAULT comes back filled in. Migration: verify signatures over
|
|
12
|
+
the original bytes you received, never over a re-encoding. For input from legacy
|
|
13
|
+
encoders that is BER but not DER (for example the RFC 8410 Ed25519 sample
|
|
14
|
+
certificate, which encodes `critical FALSE`), pass `false` as `derDecode`'s third
|
|
15
|
+
argument; malformed framing stays refused either way. UTCTime and GeneralizedTime
|
|
16
|
+
handling needs a `Temporal` global (Node.js 26, or a polyfill such as
|
|
17
|
+
`temporal-polyfill`).
|
|
18
|
+
|
|
19
|
+
`derEncode` leaves out a SEQUENCE or SET component equal to its known wire
|
|
20
|
+
DEFAULT (equality of DER encodings), converts UTCTime and GeneralizedTime to UTC
|
|
21
|
+
and writes them with `Z`, clears BIT STRING padding bits (on a copy, never in the
|
|
22
|
+
caller's buffer) and strips trailing zero bits of a BIT STRING with named bits,
|
|
23
|
+
sorts SET components with tag numbers of 31 and above correctly, and sets the
|
|
24
|
+
constructed bit right for an IMPLICIT tag over a `lazy`, tagged or CHOICE type
|
|
25
|
+
(the last encoded as EXPLICIT, X.680 §31.2.7). It throws `EncodingError` for a
|
|
26
|
+
date or time that does not exist or a leap second (validated with `Temporal`
|
|
27
|
+
before any offset conversion), a GeneralizedTime without a UTC offset or whose
|
|
28
|
+
UTC year does not fit four digits, a UTCTime outside 1950–2049 once in UTC, a BIT
|
|
29
|
+
STRING with more than 7 unused bits, and a tag number that is not a non-negative
|
|
30
|
+
safe integer. `cerEncode` also omits DEFAULT values and writes times in UTC;
|
|
31
|
+
`berEncode` validates time fields the same way.
|
|
32
|
+
|
|
33
|
+
`derDecode` recognises an absent OPTIONAL or DEFAULT component whose type is an
|
|
34
|
+
untagged CHOICE by the tags of all its alternatives, nested CHOICEs and
|
|
35
|
+
alternatives in extension addition groups included; CHOICE alternatives inside
|
|
36
|
+
extension addition groups decode and encode, and the inferred CHOICE value type
|
|
37
|
+
includes them (`extensionAdditionGroup` preserves literal members and accepts
|
|
38
|
+
dynamically built arrays). BER, CER, DER and PER return fresh DEFAULT values; PER
|
|
39
|
+
applies decode transforms to present values. Encoders take the original input
|
|
40
|
+
types through `lazy` and constructed members, including SEQUENCE and SET
|
|
41
|
+
extension groups. If a transform is reachable within a DEFAULT's schema, decoders
|
|
42
|
+
clone the declared output without filling nested DEFAULTs. DER/CER/PER retain
|
|
43
|
+
explicit values and strict DER accepts them unless
|
|
44
|
+
`.default(output, { wireValue: input })` supplies the wire default. These codecs
|
|
45
|
+
validate the wire value against the complete declared output on first use and
|
|
46
|
+
throw `SchemaDefinitionError` on mismatch. A numeric GeneralizedTime `fraction`
|
|
47
|
+
is written as its exact shortest decimal (`1e-21` included), and a binary REAL
|
|
48
|
+
truncated before its exponent length octet, exponent or mantissa is refused. It
|
|
49
|
+
decodes SET components by tag in any declared order, skips unknown extension
|
|
50
|
+
additions placed before the root components that follow a closing `...`, fills an
|
|
51
|
+
absent DEFAULT without sharing the schema's value (nested DEFAULTs filled when
|
|
52
|
+
DER can replay an inferred wire value), and returns byte values (OCTET STRING,
|
|
53
|
+
BIT STRING, ANY) as copies that never alias the input. GeneralizedTime values
|
|
54
|
+
carry the exact fraction digits in a new optional `fractionDigits` field, which
|
|
55
|
+
the encoders prefer over the `fraction` number (always below 1). UTF8String and
|
|
56
|
+
BMPString keep a leading U+FEFF; a BMPString or UniversalString that is not valid
|
|
57
|
+
UTF-16/UTF-32 is refused. INTEGER contents of any length decode and encode in
|
|
58
|
+
linear time. `berDecode` and `cerDecode` get the same CHOICE lookahead and BOM
|
|
59
|
+
handling; `berDecode` fills a trailing DEFAULT that is absent, and BER encodes and
|
|
60
|
+
decodes UTF8String as UTF-8 instead of Latin-1.
|
|
61
|
+
|
|
62
|
+
Stricter decoding. In strict mode (the default) `derDecode` refuses: trailing
|
|
63
|
+
bytes after the value, a length or nested length not in the fewest octets, BOOLEAN
|
|
64
|
+
TRUE other than `0xFF`, non-zero BIT STRING padding or trailing zero named bits, a
|
|
65
|
+
component encoded with its known wire DEFAULT, SET components or SET OF elements
|
|
66
|
+
out of order, UTCTime other than `YYMMDDHHMMSSZ` and GeneralizedTime other than
|
|
67
|
+
`YYYYMMDDHHMMSS[.f]Z`. In both modes it refuses malformed input it used to
|
|
68
|
+
misread: truncation, the indefinite length form (use `berDecode`), end-of-contents
|
|
69
|
+
octets (universal tag 0) as a value, a wrong primitive/constructed bit, a
|
|
70
|
+
constructed ANY, open-type or unknown-extension element whose contents are not
|
|
71
|
+
well-framed TLVs, bytes left inside an EXPLICIT tag or after the last component of
|
|
72
|
+
a non-extensible SEQUENCE, a BOOLEAN that is not one octet, an INTEGER that is
|
|
73
|
+
empty or has a redundant leading octet, a BIT STRING with more than 7 unused bits,
|
|
74
|
+
a non-empty NULL, dates that do not exist, leap seconds, and nesting deeper than
|
|
75
|
+
512 values (IMPLICIT chains included). Every such error is a `DecodingError`
|
|
76
|
+
carrying the absolute offset. Not yet checked: the REAL canonical form (§11.3),
|
|
77
|
+
GeneralString escapes (§11.4), the TIME family (§11.9) and restricted-string
|
|
78
|
+
repertoires.
|
|
79
|
+
|
|
80
|
+
`walkDef` is iterative, so deep schemas cannot overflow the stack; its callbacks,
|
|
81
|
+
control strings and paths are unchanged. It terminates on a recursive `lazy`
|
|
82
|
+
schema (a lazy def already being expanded by an ancestor is visited but not
|
|
83
|
+
expanded again) and accepts an optional `{ budget }` (a core `WorkBudget`) to cap
|
|
84
|
+
walks over schemas built from untrusted input.
|
|
85
|
+
|
|
86
|
+
### Patch Changes
|
|
87
|
+
|
|
88
|
+
- Updated dependencies [2a0fe74]
|
|
89
|
+
- Updated dependencies [5152230]
|
|
90
|
+
- Updated dependencies [5152230]
|
|
91
|
+
- Updated dependencies [2a0fe74]
|
|
92
|
+
- Updated dependencies [5152230]
|
|
93
|
+
- Updated dependencies [2a0fe74]
|
|
94
|
+
- Updated dependencies [5152230]
|
|
95
|
+
- Updated dependencies [5152230]
|
|
96
|
+
- Updated dependencies [2a0fe74]
|
|
97
|
+
- Updated dependencies [2a0fe74]
|
|
98
|
+
- Updated dependencies [5152230]
|
|
99
|
+
- Updated dependencies [5152230]
|
|
100
|
+
- Updated dependencies [5152230]
|
|
101
|
+
- Updated dependencies [5152230]
|
|
102
|
+
- Updated dependencies [5152230]
|
|
103
|
+
- Updated dependencies [5152230]
|
|
104
|
+
- Updated dependencies [5152230]
|
|
105
|
+
- Updated dependencies [5152230]
|
|
106
|
+
- Updated dependencies [5152230]
|
|
107
|
+
- @ac-kit/algo@0.4.0
|
|
108
|
+
- @ac-kit/core@0.10.0
|
|
109
|
+
- @ac-kit/format-core@0.4.0
|
|
110
|
+
- @ac-kit/format-varint@0.2.5
|
|
111
|
+
- @ac-kit/math-numbers@0.2.7
|
|
112
|
+
|
|
3
113
|
## 0.2.2
|
|
4
114
|
|
|
5
115
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -11,16 +11,27 @@ can run is `@ac-kit/format-asn1-compiler`. Most consumers decode bytes against a
|
|
|
11
11
|
schema they already have and need this package alone.
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
boolean,
|
|
16
|
+
component,
|
|
17
|
+
integer,
|
|
18
|
+
ref,
|
|
19
|
+
sequence,
|
|
20
|
+
utf8String,
|
|
21
|
+
} from "@ac-kit/format-asn1";
|
|
15
22
|
import { derDecode, derEncode } from "@ac-kit/format-asn1/der";
|
|
16
23
|
|
|
17
|
-
const Person =
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
24
|
+
const Person = ref(
|
|
25
|
+
sequence([
|
|
26
|
+
component("name", utf8String()),
|
|
27
|
+
component("age", integer()),
|
|
28
|
+
component("admin", boolean()).default(false),
|
|
29
|
+
] as const),
|
|
30
|
+
);
|
|
21
31
|
|
|
22
|
-
|
|
23
|
-
const
|
|
32
|
+
// `admin` equals its DEFAULT, so DER leaves it out (X.690 §11.5).
|
|
33
|
+
const bytes = derEncode(Person, { name: "Alice", age: 40n, admin: false });
|
|
34
|
+
const value = derDecode(Person, bytes); // { name: "Alice", age: 40n, admin: false }
|
|
24
35
|
```
|
|
25
36
|
|
|
26
37
|
Import the codec you need from its own subpath — `./ber`, `./cer`, `./der` or
|
|
@@ -29,5 +40,52 @@ canonical encoding certificates and signatures use; BER is its permissive
|
|
|
29
40
|
superset; CER is the streaming-friendly canonical form; PER is the compact,
|
|
30
41
|
schema-driven encoding used in telecoms protocols.
|
|
31
42
|
|
|
43
|
+
`derDecode` is strict by default. What strict mode certifies: the framing of
|
|
44
|
+
every element (definite lengths in the fewest octets, minimal tag forms, the
|
|
45
|
+
right primitive/constructed bit, nothing left over, no end-of-contents octets)
|
|
46
|
+
and the X.690 §11 contents rules of every typed value — BOOLEAN `0xFF`, minimal
|
|
47
|
+
INTEGER, BIT STRING padding and named-bit trailing zeros, no component encoded
|
|
48
|
+
with its known wire DEFAULT, SET and SET OF order, UTCTime and GeneralizedTime in UTC
|
|
49
|
+
with `Z`. Contents whose type the schema does not give (ANY, open types, unknown
|
|
50
|
+
extensions) are checked for framing only, since their contents rules depend on
|
|
51
|
+
a type the decoder does not know. Not yet checked: the REAL canonical form
|
|
52
|
+
(§11.3), GeneralString escape sequences (§11.4), the TIME family (§11.9) and
|
|
53
|
+
the character repertoires of restricted string types (X.680 §41). Anything else throws `DecodingError`. Pass `false` as the third
|
|
54
|
+
argument to accept the canonical-form violations from legacy encoders;
|
|
55
|
+
malformed framing is refused either way. Verify signatures over the original
|
|
56
|
+
bytes, never over a re-encoding.
|
|
57
|
+
|
|
58
|
+
`derEncode` converts times to UTC and omits known wire DEFAULTs. An
|
|
59
|
+
OPTIONAL component whose type is an untagged ANY has no tag to recognise it by,
|
|
60
|
+
so it is taken as present whenever an element remains: give it a tag or keep it
|
|
61
|
+
last in its SEQUENCE.
|
|
62
|
+
|
|
63
|
+
## DEFAULTs with decode transforms
|
|
64
|
+
|
|
65
|
+
`.transform(fn)` is decode-only: encoders still take the original input type.
|
|
66
|
+
Declare `.default(value)` in the **decoded output** type. BER, CER, DER, and PER
|
|
67
|
+
return fresh values for absent DEFAULT components. For a transformed component,
|
|
68
|
+
including one with a transform reachable anywhere in its schema, the decoder
|
|
69
|
+
clones the declared output as-is: it does not fill nested DEFAULT fields inside
|
|
70
|
+
that clone. Use structured-cloneable values, such as plain objects or typed
|
|
71
|
+
arrays.
|
|
72
|
+
|
|
73
|
+
Without a known wire default, DER, CER, and PER keep explicitly supplied values,
|
|
74
|
+
and strict DER does not reject them on DEFAULT grounds. This applies even when
|
|
75
|
+
the transform leaves the TypeScript type unchanged. To enable omission and strict
|
|
76
|
+
DER rejection, supply the corresponding encoder input as
|
|
77
|
+
`.default(value, { wireValue })`, for example
|
|
78
|
+
`.default({ flag: false }, { wireValue: false })` with
|
|
79
|
+
`boolean().transform((flag) => ({ flag }))`.
|
|
80
|
+
|
|
81
|
+
DER, CER, and PER validate the explicit wire value on first use. It must decode
|
|
82
|
+
to the complete declared output, including any nested fields the decoder fills;
|
|
83
|
+
otherwise they throw `SchemaDefinitionError`. Treat both values as immutable.
|
|
84
|
+
|
|
85
|
+
UTCTime and GeneralizedTime values are validated with `Temporal` (dates that
|
|
86
|
+
do not exist and leap seconds are refused): the runtime needs a `Temporal`
|
|
87
|
+
global — Node.js 26 has one; elsewhere load a polyfill such as
|
|
88
|
+
`temporal-polyfill`.
|
|
89
|
+
|
|
32
90
|
Portable: no Node.js built-ins, so it runs unchanged in browsers, Deno, Bun and
|
|
33
91
|
edge runtimes.
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { Tag } from "../schema/metadata.js";
|
|
2
|
+
import type { AnyAsn1TypeDef } from "../schema/types/any-def.js";
|
|
3
|
+
import type { Asn1TaggedTypeDef } from "../schema/types/base.js";
|
|
4
|
+
import type { Asn1ChoiceTypeDef } from "../schema/types/constructed/choice.js";
|
|
5
|
+
/**
|
|
6
|
+
* Strips the wrappers that never appear on the wire (`lazy`, `transform`, a
|
|
7
|
+
* parameterized-type instance) until a def that encodes itself is reached.
|
|
8
|
+
*
|
|
9
|
+
* Iterative with a seen-set, so a lazy getter that returns itself (directly or
|
|
10
|
+
* through other wrappers) is reported instead of looping forever.
|
|
11
|
+
*
|
|
12
|
+
* @param def - Any def.
|
|
13
|
+
* @param fail - Builds the codec's own error for a wrapper cycle.
|
|
14
|
+
* @param transforms - When given, receives the `transform` functions met, from
|
|
15
|
+
* the outermost to the innermost.
|
|
16
|
+
* @returns The first def that is not a wrapper.
|
|
17
|
+
* @throws What `fail` returns, when the wrappers form a cycle.
|
|
18
|
+
*/
|
|
19
|
+
export declare function unwrapDef(def: AnyAsn1TypeDef, fail: (message: string) => Error, transforms?: Array<(value: unknown) => unknown>): AnyAsn1TypeDef;
|
|
20
|
+
/**
|
|
21
|
+
* The tagging mode that actually applies on the wire: X.680 §31.2.7 treats an
|
|
22
|
+
* IMPLICIT tag on an untagged CHOICE, an open type or ANY as EXPLICIT, since
|
|
23
|
+
* their own tag is the only thing telling the alternatives apart.
|
|
24
|
+
*/
|
|
25
|
+
export declare function effectiveTaggingMode(def: Asn1TaggedTypeDef, fail: (message: string) => Error): "explicit" | "implicit";
|
|
26
|
+
/**
|
|
27
|
+
* Whether the outermost identifier octets of `def`'s encoding have the
|
|
28
|
+
* constructed bit set (X.690 §8.1.2.5), or `undefined` when it depends on the
|
|
29
|
+
* value (an untagged CHOICE or ANY).
|
|
30
|
+
*
|
|
31
|
+
* DER has no constructed form for a primitive type (§10.2), so this is exact
|
|
32
|
+
* for DER; BER and CER may also send strings constructed.
|
|
33
|
+
*/
|
|
34
|
+
export declare function isConstructedEncoding(def: AnyAsn1TypeDef, fail: (message: string) => Error): boolean | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* The alternatives of a CHOICE, the members of its extension addition groups
|
|
37
|
+
* included: what both first-tag discovery and decoding dispatch go through.
|
|
38
|
+
*/
|
|
39
|
+
export declare function choiceAlternatives(def: Asn1ChoiceTypeDef): Array<{
|
|
40
|
+
readonly name: string;
|
|
41
|
+
readonly type: AnyAsn1TypeDef;
|
|
42
|
+
}>;
|
|
43
|
+
/** Set of outer tags a def may start with; `undefined` stands for any tag. */
|
|
44
|
+
export type OuterTagSet = ReadonlySet<string> | undefined;
|
|
45
|
+
/** Key under which a tag is stored in an {@link OuterTagSet}. */
|
|
46
|
+
export declare function tagKey(tag: Tag): string;
|
|
47
|
+
/**
|
|
48
|
+
* Collects the outer tags `def`'s encoding may start with: one tag for a tagged
|
|
49
|
+
* or universal type, the union of the alternatives' sets for an untagged CHOICE
|
|
50
|
+
* (nested CHOICEs and extension addition groups included), and `undefined` —
|
|
51
|
+
* any tag — as soon as an ANY or open type is reachable, since those have no
|
|
52
|
+
* finite tag set.
|
|
53
|
+
*
|
|
54
|
+
* This is what decides whether an OPTIONAL or DEFAULT component is present
|
|
55
|
+
* (X.680 §25.6 requires the tags of consecutive optional components to be
|
|
56
|
+
* distinct, which is what makes one tag of lookahead enough).
|
|
57
|
+
*
|
|
58
|
+
* Iterative, and memoized per def: every CHOICE's set is computed once, nested
|
|
59
|
+
* ones included, and a recursive CHOICE reached through `lazy` terminates (a
|
|
60
|
+
* set that depends on a CHOICE still being computed is cached only once that
|
|
61
|
+
* CHOICE is done).
|
|
62
|
+
*
|
|
63
|
+
* Complexity: O(n + t·n) the first time for n CHOICE defs and t tags; O(1)
|
|
64
|
+
* afterwards.
|
|
65
|
+
*/
|
|
66
|
+
export declare function outerTagSet(def: AnyAsn1TypeDef, fail: (message: string) => Error): OuterTagSet;
|
|
67
|
+
/** Whether `tag` may start an encoding of a def whose outer set is `set`. */
|
|
68
|
+
export declare function outerTagSetHas(set: OuterTagSet, tag: Tag): boolean;
|
|
69
|
+
//# sourceMappingURL=def-tags.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"def-tags.d.ts","sourceRoot":"","sources":["../../src/_encoding/def-tags.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AACjE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,uCAAuC,CAAC;AAG/E;;;;;;;;;;;;;GAaG;AACH,wBAAgB,SAAS,CACxB,GAAG,EAAE,cAAc,EACnB,IAAI,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,KAAK,EAChC,UAAU,CAAC,EAAE,KAAK,CAAC,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC,GAC7C,cAAc,CAwBhB;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CACnC,GAAG,EAAE,iBAAiB,EACtB,IAAI,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,KAAK,GAC9B,UAAU,GAAG,UAAU,CAazB;AAED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CACpC,GAAG,EAAE,cAAc,EACnB,IAAI,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,KAAK,GAC9B,OAAO,GAAG,SAAS,CAerB;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CACjC,GAAG,EAAE,iBAAiB,GACpB,KAAK,CAAC;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAA;CAAE,CAAC,CAYjE;AAED,8EAA8E;AAC9E,MAAM,MAAM,WAAW,GAAG,WAAW,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC;AAI1D,iEAAiE;AACjE,wBAAgB,MAAM,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,CAEvC;AA+BD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,WAAW,CAC1B,GAAG,EAAE,cAAc,EACnB,IAAI,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,KAAK,GAC9B,WAAW,CA8Fb;AACD,6EAA6E;AAC7E,wBAAgB,cAAc,CAAC,GAAG,EAAE,WAAW,EAAE,GAAG,EAAE,GAAG,GAAG,OAAO,CAElE"}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { universalTagFor } from "../ber/tags.js";
|
|
2
|
+
import { resolveLazyDef } from "../schema/_lazy-resolution.js";
|
|
3
|
+
import { resolveInstance } from "../schema/types/parameterized/parameterized-type.js";
|
|
4
|
+
/**
|
|
5
|
+
* Strips the wrappers that never appear on the wire (`lazy`, `transform`, a
|
|
6
|
+
* parameterized-type instance) until a def that encodes itself is reached.
|
|
7
|
+
*
|
|
8
|
+
* Iterative with a seen-set, so a lazy getter that returns itself (directly or
|
|
9
|
+
* through other wrappers) is reported instead of looping forever.
|
|
10
|
+
*
|
|
11
|
+
* @param def - Any def.
|
|
12
|
+
* @param fail - Builds the codec's own error for a wrapper cycle.
|
|
13
|
+
* @param transforms - When given, receives the `transform` functions met, from
|
|
14
|
+
* the outermost to the innermost.
|
|
15
|
+
* @returns The first def that is not a wrapper.
|
|
16
|
+
* @throws What `fail` returns, when the wrappers form a cycle.
|
|
17
|
+
*/
|
|
18
|
+
export function unwrapDef(def, fail, transforms) {
|
|
19
|
+
let current = def;
|
|
20
|
+
let seen;
|
|
21
|
+
for (;;) {
|
|
22
|
+
let next;
|
|
23
|
+
if (current.kind === "lazy") {
|
|
24
|
+
next = resolveLazyDef(current);
|
|
25
|
+
}
|
|
26
|
+
else if (current.kind === "transform") {
|
|
27
|
+
if (transforms !== undefined && current.fn !== undefined) {
|
|
28
|
+
transforms.push(current.fn);
|
|
29
|
+
}
|
|
30
|
+
next = current.innerType;
|
|
31
|
+
}
|
|
32
|
+
else if (current.kind === "parameterizedTypeInstance") {
|
|
33
|
+
next = resolveInstance(current);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
return current;
|
|
37
|
+
}
|
|
38
|
+
seen ??= new Set();
|
|
39
|
+
if (seen.has(next)) {
|
|
40
|
+
throw fail("lazy/transform wrappers form a cycle with no encoding");
|
|
41
|
+
}
|
|
42
|
+
seen.add(current);
|
|
43
|
+
current = next;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The tagging mode that actually applies on the wire: X.680 §31.2.7 treats an
|
|
48
|
+
* IMPLICIT tag on an untagged CHOICE, an open type or ANY as EXPLICIT, since
|
|
49
|
+
* their own tag is the only thing telling the alternatives apart.
|
|
50
|
+
*/
|
|
51
|
+
export function effectiveTaggingMode(def, fail) {
|
|
52
|
+
if (def.mode === "explicit") {
|
|
53
|
+
return "explicit";
|
|
54
|
+
}
|
|
55
|
+
const inner = unwrapDef(def.innerType, fail);
|
|
56
|
+
if (inner.kind === "choice" ||
|
|
57
|
+
inner.kind === "any" ||
|
|
58
|
+
inner.kind === "openType") {
|
|
59
|
+
return "explicit";
|
|
60
|
+
}
|
|
61
|
+
return "implicit";
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Whether the outermost identifier octets of `def`'s encoding have the
|
|
65
|
+
* constructed bit set (X.690 §8.1.2.5), or `undefined` when it depends on the
|
|
66
|
+
* value (an untagged CHOICE or ANY).
|
|
67
|
+
*
|
|
68
|
+
* DER has no constructed form for a primitive type (§10.2), so this is exact
|
|
69
|
+
* for DER; BER and CER may also send strings constructed.
|
|
70
|
+
*/
|
|
71
|
+
export function isConstructedEncoding(def, fail) {
|
|
72
|
+
let current = unwrapDef(def, fail);
|
|
73
|
+
let seen;
|
|
74
|
+
while (current.kind === "tagged") {
|
|
75
|
+
if (effectiveTaggingMode(current, fail) === "explicit") {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
seen ??= new Set();
|
|
79
|
+
if (seen.has(current)) {
|
|
80
|
+
throw fail("IMPLICIT tags form a cycle with no encoding");
|
|
81
|
+
}
|
|
82
|
+
seen.add(current);
|
|
83
|
+
current = unwrapDef(current.innerType, fail);
|
|
84
|
+
}
|
|
85
|
+
return universalTagFor(current)?.constructed;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The alternatives of a CHOICE, the members of its extension addition groups
|
|
89
|
+
* included: what both first-tag discovery and decoding dispatch go through.
|
|
90
|
+
*/
|
|
91
|
+
export function choiceAlternatives(def) {
|
|
92
|
+
const alternatives = [];
|
|
93
|
+
for (const item of def.alternatives) {
|
|
94
|
+
if ("kind" in item) {
|
|
95
|
+
if (item.kind === "extensionAdditionGroup") {
|
|
96
|
+
alternatives.push(...item.components);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
alternatives.push(item);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return alternatives;
|
|
104
|
+
}
|
|
105
|
+
const outerTagSetCache = new WeakMap();
|
|
106
|
+
/** Key under which a tag is stored in an {@link OuterTagSet}. */
|
|
107
|
+
export function tagKey(tag) {
|
|
108
|
+
return `${tag.tagClass}:${tag.tagNumber}`;
|
|
109
|
+
}
|
|
110
|
+
/** The tag set of a def that is not a CHOICE, or `"choice"`. */
|
|
111
|
+
function leafTagSet(base) {
|
|
112
|
+
if (base.kind === "tagged") {
|
|
113
|
+
return new Set([tagKey(base.tag)]);
|
|
114
|
+
}
|
|
115
|
+
if (base.kind === "choice") {
|
|
116
|
+
return "choice";
|
|
117
|
+
}
|
|
118
|
+
const universal = universalTagFor(base);
|
|
119
|
+
return universal === undefined
|
|
120
|
+
? undefined
|
|
121
|
+
: new Set([
|
|
122
|
+
tagKey({ tagClass: "universal", tagNumber: universal.tagNumber }),
|
|
123
|
+
]);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Collects the outer tags `def`'s encoding may start with: one tag for a tagged
|
|
127
|
+
* or universal type, the union of the alternatives' sets for an untagged CHOICE
|
|
128
|
+
* (nested CHOICEs and extension addition groups included), and `undefined` —
|
|
129
|
+
* any tag — as soon as an ANY or open type is reachable, since those have no
|
|
130
|
+
* finite tag set.
|
|
131
|
+
*
|
|
132
|
+
* This is what decides whether an OPTIONAL or DEFAULT component is present
|
|
133
|
+
* (X.680 §25.6 requires the tags of consecutive optional components to be
|
|
134
|
+
* distinct, which is what makes one tag of lookahead enough).
|
|
135
|
+
*
|
|
136
|
+
* Iterative, and memoized per def: every CHOICE's set is computed once, nested
|
|
137
|
+
* ones included, and a recursive CHOICE reached through `lazy` terminates (a
|
|
138
|
+
* set that depends on a CHOICE still being computed is cached only once that
|
|
139
|
+
* CHOICE is done).
|
|
140
|
+
*
|
|
141
|
+
* Complexity: O(n + t·n) the first time for n CHOICE defs and t tags; O(1)
|
|
142
|
+
* afterwards.
|
|
143
|
+
*/
|
|
144
|
+
export function outerTagSet(def, fail) {
|
|
145
|
+
const cached = outerTagSetCache.get(def);
|
|
146
|
+
if (cached !== undefined) {
|
|
147
|
+
return cached.set;
|
|
148
|
+
}
|
|
149
|
+
const stack = [];
|
|
150
|
+
const inProgress = new Map();
|
|
151
|
+
// Results within this call, cached or not, with their low-link.
|
|
152
|
+
const finished = new Map();
|
|
153
|
+
let result;
|
|
154
|
+
/** Resolves `child` now, or pushes a frame and returns `undefined`. */
|
|
155
|
+
const enter = (child) => {
|
|
156
|
+
const known = outerTagSetCache.get(child);
|
|
157
|
+
if (known !== undefined) {
|
|
158
|
+
return { set: known.set, low: Infinity };
|
|
159
|
+
}
|
|
160
|
+
const base = unwrapDef(child, fail);
|
|
161
|
+
const knownBase = outerTagSetCache.get(base);
|
|
162
|
+
if (knownBase !== undefined) {
|
|
163
|
+
return { set: knownBase.set, low: Infinity };
|
|
164
|
+
}
|
|
165
|
+
const partial = finished.get(base);
|
|
166
|
+
if (partial !== undefined) {
|
|
167
|
+
return partial;
|
|
168
|
+
}
|
|
169
|
+
const depth = inProgress.get(base);
|
|
170
|
+
if (depth !== undefined) {
|
|
171
|
+
return { set: new Set(), low: depth };
|
|
172
|
+
}
|
|
173
|
+
const leaf = leafTagSet(base);
|
|
174
|
+
if (leaf !== "choice") {
|
|
175
|
+
outerTagSetCache.set(base, { set: leaf });
|
|
176
|
+
return { set: leaf, low: Infinity };
|
|
177
|
+
}
|
|
178
|
+
inProgress.set(base, stack.length);
|
|
179
|
+
stack.push({
|
|
180
|
+
def: child,
|
|
181
|
+
base,
|
|
182
|
+
depth: stack.length,
|
|
183
|
+
children: choiceAlternatives(base).map((alternative) => alternative.type),
|
|
184
|
+
next: 0,
|
|
185
|
+
tags: new Set(),
|
|
186
|
+
low: Infinity,
|
|
187
|
+
});
|
|
188
|
+
return undefined;
|
|
189
|
+
};
|
|
190
|
+
const merge = (frame, child) => {
|
|
191
|
+
frame.low = Math.min(frame.low, child.low);
|
|
192
|
+
if (child.set === undefined) {
|
|
193
|
+
frame.tags = undefined;
|
|
194
|
+
}
|
|
195
|
+
else if (frame.tags !== undefined) {
|
|
196
|
+
for (const tag of child.set)
|
|
197
|
+
frame.tags.add(tag);
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
result = enter(def);
|
|
201
|
+
while (stack.length > 0) {
|
|
202
|
+
const frame = stack[stack.length - 1];
|
|
203
|
+
if (frame.tags !== undefined && frame.next < frame.children.length) {
|
|
204
|
+
const child = enter(frame.children[frame.next++]);
|
|
205
|
+
if (child !== undefined)
|
|
206
|
+
merge(frame, child);
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
stack.pop();
|
|
210
|
+
inProgress.delete(frame.base);
|
|
211
|
+
const done = { set: frame.tags, low: frame.low };
|
|
212
|
+
if (frame.low >= frame.depth) {
|
|
213
|
+
outerTagSetCache.set(frame.base, { set: done.set });
|
|
214
|
+
outerTagSetCache.set(frame.def, { set: done.set });
|
|
215
|
+
done.low = Infinity;
|
|
216
|
+
// Partial sets waiting on this frame are stale now that it is done.
|
|
217
|
+
for (const [base, partial] of finished) {
|
|
218
|
+
if (partial.low >= frame.depth)
|
|
219
|
+
finished.delete(base);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
finished.set(frame.base, done);
|
|
224
|
+
}
|
|
225
|
+
if (stack.length === 0) {
|
|
226
|
+
result = done;
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
merge(stack[stack.length - 1], done);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
outerTagSetCache.set(def, { set: result.set });
|
|
233
|
+
return result.set;
|
|
234
|
+
}
|
|
235
|
+
/** Whether `tag` may start an encoding of a def whose outer set is `set`. */
|
|
236
|
+
export function outerTagSetHas(set, tag) {
|
|
237
|
+
return set === undefined || set.has(tagKey(tag));
|
|
238
|
+
}
|
|
239
|
+
//# sourceMappingURL=def-tags.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"def-tags.js","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CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACxB,MAAM,GAAG,IAAI,CAAC;QACf,CAAC;aAAM,CAAC;YACP,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAE,EAAE,IAAI,CAAC,CAAC;QACvC,CAAC;IACF,CAAC;IACD,gBAAgB,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,GAAG,EAAE,MAAO,CAAC,GAAG,EAAE,CAAC,CAAC;IAChD,OAAO,MAAO,CAAC,GAAG,CAAC;AACpB,CAAC;AACD,6EAA6E;AAC7E,MAAM,UAAU,cAAc,CAAC,GAAgB,EAAE,GAAQ;IACxD,OAAO,GAAG,KAAK,SAAS,IAAI,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;AAClD,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type CallableNoArgs } from "@ac-kit/core";
|
|
2
|
+
import type { Asn1ComponentDef } from "../schema/types/constructed/component.js";
|
|
3
|
+
/** Compare schema values structurally, including byte-string leaves. */
|
|
4
|
+
export declare function defaultValuesEqual(actual: unknown, expected: unknown): boolean;
|
|
5
|
+
/** Verify an explicitly supplied wire default against its decoded output. */
|
|
6
|
+
export declare function checkDefaultOutput(component: Asn1ComponentDef, roundTrip: CallableNoArgs<unknown>): void;
|
|
7
|
+
/** Fill an absent component without sharing its declared output object. */
|
|
8
|
+
export declare function freshDefault(component: Asn1ComponentDef): unknown;
|
|
9
|
+
//# sourceMappingURL=default-output.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"default-output.d.ts","sourceRoot":"","sources":["../../src/_encoding/default-output.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6B,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AAG9E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0CAA0C,CAAC;AAEjF,wEAAwE;AACxE,wBAAgB,kBAAkB,CACjC,MAAM,EAAE,OAAO,EACf,QAAQ,EAAE,OAAO,GACf,OAAO,CAMT;AAID,6EAA6E;AAC7E,wBAAgB,kBAAkB,CACjC,SAAS,EAAE,gBAAgB,EAC3B,SAAS,EAAE,cAAc,CAAC,OAAO,CAAC,GAChC,IAAI,CA4BN;AAED,2EAA2E;AAC3E,wBAAgB,YAAY,CAAC,SAAS,EAAE,gBAAgB,GAAG,OAAO,CAUjE"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { bytesIsEqual, isDeepEqual } from "@ac-kit/core";
|
|
2
|
+
import { SchemaDefinitionError } from "../schema/errors.js";
|
|
3
|
+
/** Compare schema values structurally, including byte-string leaves. */
|
|
4
|
+
export function defaultValuesEqual(actual, expected) {
|
|
5
|
+
return isDeepEqual(actual, expected, (left, right) => left instanceof Uint8Array && right instanceof Uint8Array
|
|
6
|
+
? bytesIsEqual(left, right)
|
|
7
|
+
: isDeepEqual(left, right, "sameValue"));
|
|
8
|
+
}
|
|
9
|
+
const checkingWireDefaults = new WeakSet();
|
|
10
|
+
/** Verify an explicitly supplied wire default against its decoded output. */
|
|
11
|
+
export function checkDefaultOutput(component, roundTrip) {
|
|
12
|
+
if (component.defaultWireValue === undefined)
|
|
13
|
+
return;
|
|
14
|
+
if (checkingWireDefaults.has(component)) {
|
|
15
|
+
throw new SchemaDefinitionError(component.name, `Recursive DEFAULT wireValue for component "${component.name}"`);
|
|
16
|
+
}
|
|
17
|
+
checkingWireDefaults.add(component);
|
|
18
|
+
let equal;
|
|
19
|
+
try {
|
|
20
|
+
equal = defaultValuesEqual(roundTrip(), component.defaultValue);
|
|
21
|
+
}
|
|
22
|
+
catch (cause) {
|
|
23
|
+
if (cause instanceof SchemaDefinitionError)
|
|
24
|
+
throw cause;
|
|
25
|
+
throw new SchemaDefinitionError(component.name, `DEFAULT wireValue for component "${component.name}" cannot round-trip`, { cause });
|
|
26
|
+
}
|
|
27
|
+
finally {
|
|
28
|
+
checkingWireDefaults.delete(component);
|
|
29
|
+
}
|
|
30
|
+
if (!equal) {
|
|
31
|
+
throw new SchemaDefinitionError(component.name, `DEFAULT wireValue for component "${component.name}" does not decode to its declared output`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/** Fill an absent component without sharing its declared output object. */
|
|
35
|
+
export function freshDefault(component) {
|
|
36
|
+
try {
|
|
37
|
+
return structuredClone(component.defaultValue);
|
|
38
|
+
}
|
|
39
|
+
catch (cause) {
|
|
40
|
+
throw new SchemaDefinitionError(component.name, `DEFAULT for component "${component.name}" is not structured-cloneable`, { cause });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=default-output.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"default-output.js","sourceRoot":"","sources":["../../src/_encoding/default-output.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAuB,MAAM,cAAc,CAAC;AAE9E,OAAO,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAG5D,wEAAwE;AACxE,MAAM,UAAU,kBAAkB,CACjC,MAAe,EACf,QAAiB;IAEjB,OAAO,WAAW,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CACpD,IAAI,YAAY,UAAU,IAAI,KAAK,YAAY,UAAU;QACxD,CAAC,CAAC,YAAY,CAAC,IAAI,EAAE,KAAK,CAAC;QAC3B,CAAC,CAAC,WAAW,CAAC,IAAI,EAAE,KAAK,EAAE,WAAW,CAAC,CACxC,CAAC;AACH,CAAC;AAED,MAAM,oBAAoB,GAAG,IAAI,OAAO,EAAoB,CAAC;AAE7D,6EAA6E;AAC7E,MAAM,UAAU,kBAAkB,CACjC,SAA2B,EAC3B,SAAkC;IAElC,IAAI,SAAS,CAAC,gBAAgB,KAAK,SAAS;QAAE,OAAO;IACrD,IAAI,oBAAoB,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC;QACzC,MAAM,IAAI,qBAAqB,CAC9B,SAAS,CAAC,IAAI,EACd,8CAA8C,SAAS,CAAC,IAAI,GAAG,CAC/D,CAAC;IACH,CAAC;IACD,oBAAoB,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IACpC,IAAI,KAAc,CAAC;IACnB,IAAI,CAAC;QACJ,KAAK,GAAG,kBAAkB,CAAC,SAAS,EAAE,EAAE,SAAS,CAAC,YAAY,CAAC,CAAC;IACjE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,IAAI,KAAK,YAAY,qBAAqB;YAAE,MAAM,KAAK,CAAC;QACxD,MAAM,IAAI,qBAAqB,CAC9B,SAAS,CAAC,IAAI,EACd,oCAAoC,SAAS,CAAC,IAAI,qBAAqB,EACvE,EAAE,KAAK,EAAE,CACT,CAAC;IACH,CAAC;YAAS,CAAC;QACV,oBAAoB,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACxC,CAAC;IACD,IAAI,CAAC,KAAK,EAAE,CAAC;QACZ,MAAM,IAAI,qBAAqB,CAC9B,SAAS,CAAC,IAAI,EACd,oCAAoC,SAAS,CAAC,IAAI,0CAA0C,CAC5F,CAAC;IACH,CAAC;AACF,CAAC;AAED,2EAA2E;AAC3E,MAAM,UAAU,YAAY,CAAC,SAA2B;IACvD,IAAI,CAAC;QACJ,OAAO,eAAe,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC;IAChD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,MAAM,IAAI,qBAAqB,CAC9B,SAAS,CAAC,IAAI,EACd,0BAA0B,SAAS,CAAC,IAAI,+BAA+B,EACvE,EAAE,KAAK,EAAE,CACT,CAAC;IACH,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Asn1ComponentDef } from "../schema/types/constructed/component.js";
|
|
2
|
+
/**
|
|
3
|
+
* The explicit encoder input, or the declared default if no decode transform is
|
|
4
|
+
* reachable. A decode-only transform may change the value even when it does not
|
|
5
|
+
* change the type; in that case the wire default cannot be inferred. Expands
|
|
6
|
+
* each reachable definition at most once (including lazy cycles).
|
|
7
|
+
*/
|
|
8
|
+
export declare function defaultWireValue(component: Asn1ComponentDef): unknown;
|
|
9
|
+
//# sourceMappingURL=default-wire-value.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"default-wire-value.d.ts","sourceRoot":"","sources":["../../src/_encoding/default-wire-value.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0CAA0C,CAAC;AAIjF;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,gBAAgB,GAAG,OAAO,CAgBrE"}
|