diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /comm/third_party/asn1js/src/OctetString.ts | |
parent | Initial commit. (diff) | |
download | thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'comm/third_party/asn1js/src/OctetString.ts')
-rw-r--r-- | comm/third_party/asn1js/src/OctetString.ts | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/comm/third_party/asn1js/src/OctetString.ts b/comm/third_party/asn1js/src/OctetString.ts new file mode 100644 index 0000000000..df57e9bc6e --- /dev/null +++ b/comm/third_party/asn1js/src/OctetString.ts @@ -0,0 +1,103 @@ +import * as pvtsutils from "pvtsutils"; +import { BaseBlock, BaseBlockJson, BaseBlockParams } from "./BaseBlock"; +import { Constructed } from "./Constructed"; +import { LocalOctetStringValueBlockParams, LocalOctetStringValueBlock, LocalOctetStringValueBlockJson } from "./internals/LocalOctetStringValueBlock"; +import { OCTET_STRING_NAME } from "./internals/constants"; +import { localFromBER } from "./parser"; +import { typeStore } from "./TypeStore"; + +export interface OctetStringParams extends BaseBlockParams, LocalOctetStringValueBlockParams { } +export type OctetStringJson = BaseBlockJson<LocalOctetStringValueBlockJson>; + +export class OctetString extends BaseBlock<LocalOctetStringValueBlock, LocalOctetStringValueBlockJson> { + + static { + typeStore.OctetString = this; + } + + public static override NAME = OCTET_STRING_NAME; + + constructor({ + idBlock = {}, + lenBlock = {}, + ...parameters + }: OctetStringParams = {}) { + parameters.isConstructed ??= !!parameters.value?.length; + super({ + idBlock: { + isConstructed: parameters.isConstructed, + ...idBlock, + }, + lenBlock: { + ...lenBlock, + isIndefiniteForm: !!parameters.isIndefiniteForm, + }, + ...parameters, + }, LocalOctetStringValueBlock); + + this.idBlock.tagClass = 1; // UNIVERSAL + this.idBlock.tagNumber = 4; // OctetString + } + + public override fromBER(inputBuffer: ArrayBuffer | Uint8Array, inputOffset: number, inputLength: number): number { + this.valueBlock.isConstructed = this.idBlock.isConstructed; + this.valueBlock.isIndefiniteForm = this.lenBlock.isIndefiniteForm; + + // Ability to encode empty OCTET STRING + if (inputLength === 0) { + if (this.idBlock.error.length === 0) + this.blockLength += this.idBlock.blockLength; + + if (this.lenBlock.error.length === 0) + this.blockLength += this.lenBlock.blockLength; + + return inputOffset; + } + + if (!this.valueBlock.isConstructed) { + const view = inputBuffer instanceof ArrayBuffer ? new Uint8Array(inputBuffer) : inputBuffer; + const buf = view.subarray(inputOffset, inputOffset + inputLength); + try { + if (buf.byteLength) { + const asn = localFromBER(buf, 0, buf.byteLength); + if (asn.offset !== -1 && asn.offset === inputLength) { + this.valueBlock.value = [asn.result as OctetString]; + } + } + } catch (e) { + // nothing + } + } + + return super.fromBER(inputBuffer, inputOffset, inputLength); + } + + protected override onAsciiEncoding(): string { + if (this.valueBlock.isConstructed || (this.valueBlock.value && this.valueBlock.value.length)) { + return Constructed.prototype.onAsciiEncoding.call(this); + } + + return `${(this.constructor as typeof OctetString).NAME} : ${pvtsutils.Convert.ToHex(this.valueBlock.valueHexView)}`; + } + + /** + * Returns OctetString value. If OctetString is constructed, returns concatenated internal OctetString values + * @returns Array buffer + * @since 3.0.0 + */ + public getValue(): ArrayBuffer { + if (!this.idBlock.isConstructed) { + return this.valueBlock.valueHexView.slice().buffer; + } + + const array: ArrayBuffer[] = []; + for (const content of this.valueBlock.value) { + if (content instanceof OctetString) { + array.push(content.valueBlock.valueHexView); + } + } + + return pvtsutils.BufferSourceConverter.concat(array); + } + +} |