UNPKG

@wildboar/pkcs

Version:
150 lines (149 loc) 4.56 kB
/* eslint-disable */ import { ASN1TagClass as _TagClass, } from "@wildboar/asn1"; import * as $ from "@wildboar/asn1/functional"; /** * @summary Path * @description * * ### ASN.1 Definition: * * ```asn1 * Path ::= SEQUENCE { * path OCTET STRING, * index INTEGER (0..pkcs15-ub-index) OPTIONAL, * length [0] INTEGER (0..pkcs15-ub-index) OPTIONAL * }( WITH COMPONENTS {..., index PRESENT, length PRESENT} | WITH COMPONENTS {..., index ABSENT, length ABSENT}) * ``` * */ export class Path { path; index; length; constructor( /** * @summary `path`. * @public * @readonly */ path, /** * @summary `index`. * @public * @readonly */ index, /** * @summary `length`. * @public * @readonly */ length) { this.path = path; this.index = index; this.length = length; } /** * @summary Restructures an object into a Path * @description * * This takes an `object` and converts it to a `Path`. * * @public * @static * @method * @param {Object} _o An object having all of the keys and values of a `Path`. * @returns {Path} */ static _from_object(_o) { return new Path(_o.path, _o.index, _o.length); } } /** * @summary The Leading Root Component Types of Path * @description * * This is an array of `ComponentSpec`s that define how to decode the leading root component type list of a SET or SEQUENCE. * * @constant */ export const _root_component_type_list_1_spec_for_Path = [ new $.ComponentSpec("path", false, $.hasTag(_TagClass.universal, 4)), new $.ComponentSpec("index", true, $.hasTag(_TagClass.universal, 2)), new $.ComponentSpec("length", true, $.hasTag(_TagClass.context, 0)), ]; /** * @summary The Trailing Root Component Types of Path * @description * * This is an array of `ComponentSpec`s that define how to decode the trailing root component type list of a SET or SEQUENCE. * * @constant */ export const _root_component_type_list_2_spec_for_Path = []; /** * @summary The Extension Addition Component Types of Path * @description * * This is an array of `ComponentSpec`s that define how to decode the extension addition component type list of a SET or SEQUENCE. * * @constant */ export const _extension_additions_list_spec_for_Path = []; let _cached_decoder_for_Path = null; /** * @summary Decodes an ASN.1 element into a(n) Path * @function * @param {_Element} el The element being decoded. * @returns {Path} The decoded data structure. */ export function _decode_Path(el) { if (!_cached_decoder_for_Path) { _cached_decoder_for_Path = function (el) { let path; let index; let length; const callbacks = { path: (_el) => { path = $._decodeOctetString(_el); }, index: (_el) => { index = $._decodeInteger(_el); }, length: (_el) => { length = $._decode_implicit(() => $._decodeInteger)(_el); }, }; $._parse_sequence(el, callbacks, _root_component_type_list_1_spec_for_Path, _extension_additions_list_spec_for_Path, _root_component_type_list_2_spec_for_Path, undefined); return new Path(path, index, length); }; } return _cached_decoder_for_Path(el); } let _cached_encoder_for_Path = null; /** * @summary Encodes a(n) Path into an ASN.1 Element. * @function * @param value The element being encoded. * @param elGetter A function that can be used to get new ASN.1 elements. * @returns {_Element} The Path, encoded as an ASN.1 Element. */ export function _encode_Path(value, elGetter) { if (!_cached_encoder_for_Path) { _cached_encoder_for_Path = function (value) { return $._encodeSequence([] .concat([ /* REQUIRED */ $._encodeOctetString(value.path, $.BER), /* IF_ABSENT */ value.index === undefined ? undefined : $._encodeInteger(value.index, $.BER), /* IF_ABSENT */ value.length === undefined ? undefined : $._encode_implicit(_TagClass.context, 0, () => $._encodeInteger, $.BER)(value.length, $.BER), ]) .filter((c) => !!c), $.BER); }; } return _cached_encoder_for_Path(value, elGetter); } /* eslint-enable */