MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / BER

Class BER

modules/crypt/etc/ber.js:38–480  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

36 */
37
38export default class BER {
39 #a;
40 #i = 0;
41
42 constructor(buffer) {
43 if (buffer instanceof ArrayBuffer)
44 this.#a = new Uint8Array(buffer)
45 else if (buffer instanceof Uint8Array)
46 this.#a = buffer;
47 else
48 this.#a = new Uint8Array(new ArrayBuffer(0, {maxByteLength: 16896}));
49 }
50 get readable() {
51 return this.#a.byteLength - this.#i;
52 }
53 getTag() {
54 return this.#a[this.#i++];
55 }
56 getLength() {
57 let length = this.#a[this.#i++];
58 if (length < 128)
59 return length;
60
61 length &= 0x7F;
62 if (!length)
63 return -1; // indefinite length
64
65 let result = 0;
66 while (length--)
67 result = (result << 8) | this.#a[this.#i++];
68
69 return result;
70 }
71 peek() {
72 return this.#a[this.#i];
73 }
74 skip(length) {
75 this.#i += length;
76 }
77 next() {
78 const i = this.#i;
79 this.skip(1);
80 this.skip(this.getLength());
81 return this.#a.subarray(i, this.#i)
82 }
83 getInteger() {
84 if (this.getTag() !== 2)
85 throw new Error("BER: not an integer");
86 const length = this.getLength();
87 const offset = this.#a.byteOffset + this.#i;
88 if (this.peek() & 0x80) {
89 const c = this.getChunk(length).slice(); // copy. cannot modify source data.
90 for (let i = 0; i < length; i++) // could use Logical.not
91 c[i] = ~c[i];
92 return -(BigInt.fromArrayBuffer(c.buffer) + 1n);
93 }
94 else {
95 this.skip(length)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected