summaryrefslogtreecommitdiffstats
path: root/comm/third_party/asn1js/src/Null.ts
blob: 8c4c4c580d3f5acbedc272d8ad5263d63db20857 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { ViewWriter } from "./ViewWriter";
import { ValueBlock, ValueBlockJson } from "./ValueBlock";
import { BaseBlock, BaseBlockJson, BaseBlockParams } from "./BaseBlock";
import { typeStore } from "./TypeStore";

export type NullParams = BaseBlockParams;
export type NullJson = BaseBlockJson<ValueBlockJson>;

export class Null extends BaseBlock<ValueBlock, ValueBlockJson> {

  static {
    typeStore.Null = this;
  }

  public static override NAME = "NULL";

  constructor(parameters: NullParams = {}) {
    super(parameters, ValueBlock); // We will not have a call to "Null value block" because of specified FROM_BER and TO_BER functions

    this.idBlock.tagClass = 1; // UNIVERSAL
    this.idBlock.tagNumber = 5; // Null
  }

  public override fromBER(inputBuffer: ArrayBuffer | Uint8Array, inputOffset: number, inputLength: number): number {
    if (this.lenBlock.length > 0)
      this.warnings.push("Non-zero length of value block for Null type");

    if (!this.idBlock.error.length)
      this.blockLength += this.idBlock.blockLength;

    if (!this.lenBlock.error.length)
      this.blockLength += this.lenBlock.blockLength;

    this.blockLength += inputLength;

    if ((inputOffset + inputLength) > inputBuffer.byteLength) {
      this.error = "End of input reached before message was fully decoded (inconsistent offset and length values)";

      return -1;
    }

    return (inputOffset + inputLength);
  }

  public override toBER(sizeOnly?: boolean, writer?: ViewWriter): ArrayBuffer {
    const retBuf = new ArrayBuffer(2);

    if (!sizeOnly) {
      const retView = new Uint8Array(retBuf);
      retView[0] = 0x05;
      retView[1] = 0x00;
    }

    if (writer) {
      writer.write(retBuf);
    }

    return retBuf;
  }

  protected override onAsciiEncoding(): string {
    return `${(this.constructor as typeof Null).NAME}`;
  }

}