UNPKG

asn1-ts

Version:

ASN.1 encoding and decoding, including BER, CER, and DER.

38 lines (37 loc) 1.62 kB
import * as errors from "../errors.mjs"; export default function validateConstruction(elements, specification) { let i = 0; for (const spec of specification) { if ((i >= elements.length) || (spec.tagClass && spec.tagClass !== elements[i].tagClass) || (spec.construction && spec.construction !== elements[i].construction) || (spec.tagNumber && spec.tagNumber !== elements[i].tagNumber)) { if (!spec.optional) { throw new errors.ASN1ConstructionError(`Missing required element '${spec.name}'.`); } continue; } if (spec.choice && spec.choice.length > 0) { let matchingChoiceFound = false; for (let j = 0; j < spec.choice.length; j++) { const choice = spec.choice[j]; if ((!(choice.tagClass) || (choice.tagClass === elements[i].tagClass)) && (!(choice.construction) || (choice.construction === elements[i].construction)) && (!(choice.tagNumber) || (choice.tagNumber === elements[i].tagNumber))) { if (choice.callback) { choice.callback(elements[i]); } matchingChoiceFound = true; break; } } if (!matchingChoiceFound && !spec.optional) { throw new errors.ASN1ConstructionError(`Missing required CHOICE '${spec.name}'.`); } } if (spec.callback) { spec.callback(elements[i]); } i++; } }