summaryrefslogtreecommitdiffstats
path: root/comm/third_party/asn1js/src/Integer.ts
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
commit6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /comm/third_party/asn1js/src/Integer.ts
parentInitial commit. (diff)
downloadthunderbird-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/Integer.ts')
-rw-r--r--comm/third_party/asn1js/src/Integer.ts103
1 files changed, 103 insertions, 0 deletions
diff --git a/comm/third_party/asn1js/src/Integer.ts b/comm/third_party/asn1js/src/Integer.ts
new file mode 100644
index 0000000000..fd3d1d9e70
--- /dev/null
+++ b/comm/third_party/asn1js/src/Integer.ts
@@ -0,0 +1,103 @@
+import * as pvtsutils from "pvtsutils";
+import { BaseBlock, BaseBlockJson, BaseBlockParams } from "./BaseBlock";
+import { LocalIntegerValueBlockParams, LocalIntegerValueBlock, LocalIntegerValueBlockJson } from "./internals/LocalIntegerValueBlock";
+import { assertBigInt } from "./internals/utils";
+import { typeStore } from "./TypeStore";
+import { ViewWriter } from "./ViewWriter";
+
+export interface IntegerParams extends BaseBlockParams, LocalIntegerValueBlockParams { }
+export type IntegerJson = BaseBlockJson<LocalIntegerValueBlockJson>;
+
+export class Integer extends BaseBlock<LocalIntegerValueBlock, LocalIntegerValueBlockJson> {
+
+ static {
+ typeStore.Integer = this;
+ }
+
+ public static override NAME = "INTEGER";
+
+ constructor(parameters: IntegerParams = {}) {
+ super(parameters, LocalIntegerValueBlock);
+
+ this.idBlock.tagClass = 1; // UNIVERSAL
+ this.idBlock.tagNumber = 2; // Integer
+ }
+
+ /**
+ * Converts Integer into BigInt
+ * @throws Throws Error if BigInt is not supported
+ * @since 3.0.0
+ */
+ public toBigInt(): bigint {
+ assertBigInt();
+
+ return BigInt(this.valueBlock.toString());
+ }
+
+ /**
+ * Creates Integer from BigInt value
+ * @param value BigInt value
+ * @returns ASN.1 Integer
+ * @throws Throws Error if BigInt is not supported
+ * @since 3.0.0
+ */
+ public static fromBigInt(value: number | string | bigint | boolean): Integer {
+ assertBigInt();
+
+ const bigIntValue = BigInt(value);
+ const writer = new ViewWriter();
+
+ const hex = bigIntValue.toString(16).replace(/^-/, "");
+ const view = new Uint8Array(pvtsutils.Convert.FromHex(hex));
+
+ if (bigIntValue < 0) {
+ // a negative number
+ const first = new Uint8Array(view.length + (view[0] & 0x80 ? 1 : 0));
+ first[0] |= 0x80;
+
+ const firstInt = BigInt(`0x${pvtsutils.Convert.ToHex(first)}`);
+ const secondInt = firstInt + bigIntValue;
+ const second = pvtsutils.BufferSourceConverter.toUint8Array(pvtsutils.Convert.FromHex(secondInt.toString(16)));
+ second[0] |= 0x80;
+
+ writer.write(second);
+ } else {
+ // a positive number
+ if (view[0] & 0x80) {
+ writer.write(new Uint8Array([0]));
+ }
+ writer.write(view);
+ }
+
+ const res = new Integer({
+ valueHex: writer.final(),
+ });
+
+ return res;
+ }
+
+ public convertToDER(): Integer {
+ const integer = new Integer({ valueHex: this.valueBlock.valueHexView });
+
+ integer.valueBlock.toDER();
+
+ return integer;
+ }
+
+ /**
+ * Convert current Integer value from DER to BER format
+ * @returns
+ */
+ public convertFromDER(): Integer {
+ return new Integer({
+ valueHex: this.valueBlock.valueHexView[0] === 0
+ ? this.valueBlock.valueHexView.subarray(1)
+ : this.valueBlock.valueHexView,
+ });
+ }
+
+ protected override onAsciiEncoding(): string {
+ return `${(this.constructor as typeof Integer).NAME} : ${this.valueBlock.toString()}`;
+ }
+
+}