asn1-ts
Version:
ASN.1 encoding and decoding, including BER, CER, and DER.
55 lines (54 loc) • 2.08 kB
JavaScript
import packBits from "../utils/packBits.mjs";
export default class External {
constructor(directReference, indirectReference, dataValueDescriptor, encoding) {
this.directReference = directReference;
this.indirectReference = indirectReference;
this.dataValueDescriptor = dataValueDescriptor;
this.encoding = encoding;
}
toString() {
let ret = "EXTERNAL { ";
if (this.directReference) {
ret += `directReference ${this.directReference.toString()} `;
}
if (this.indirectReference) {
ret += `indirectReference ${this.indirectReference.toString()} `;
}
if (this.dataValueDescriptor) {
ret += `dataValueDescriptor "${this.dataValueDescriptor}"`;
}
if (this.encoding instanceof Uint8Array) {
ret += `octet-aligned ${Array.from(this.encoding).map((byte) => byte.toString(16)).join("")} `;
}
else if (this.encoding instanceof Uint8ClampedArray) {
ret += `arbitrary ${this.encoding.toString()} `;
}
else {
ret += `single-ASN1-type ${this.encoding.toString()} `;
}
ret += "}";
return ret;
}
toJSON() {
return {
directReference: this.directReference,
indirectReference: this.indirectReference,
dataValueDescriptor: this.dataValueDescriptor,
encoding: (() => {
if (this.encoding instanceof Uint8Array) {
return Array.from(this.encoding).map((byte) => byte.toString(16)).join("");
}
else if (this.encoding instanceof Uint8ClampedArray) {
const bits = this.encoding;
return {
length: bits.length,
value: Array.from(packBits(bits)).map((byte) => byte.toString(16)).join(""),
};
}
else {
return this.encoding.toJSON();
}
})(),
};
}
}