summaryrefslogtreecommitdiffstats
path: root/comm/third_party/asn1js/src/Constructed.ts
blob: 8fe3bc502f15a0587089dc4d0797b43468bc80bf (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
import { BaseBlock, BaseBlockJson, BaseBlockParams } from "./BaseBlock";
import { LocalConstructedValueBlock, LocalConstructedValueBlockJson, LocalConstructedValueBlockParams } from "./internals/LocalConstructedValueBlock";
import { typeStore } from "./TypeStore";

export interface ConstructedParams extends BaseBlockParams, LocalConstructedValueBlockParams { }
export type ConstructedJson = BaseBlockJson<LocalConstructedValueBlockJson>;

export class Constructed extends BaseBlock<LocalConstructedValueBlock, LocalConstructedValueBlockJson> {

  static {
    typeStore.Constructed = this;
  }

  public static override NAME = "CONSTRUCTED";

  constructor(parameters: ConstructedParams = {}) {
    super(parameters, LocalConstructedValueBlock);

    this.idBlock.isConstructed = true;
  }

  public override fromBER(inputBuffer: ArrayBuffer | Uint8Array, inputOffset: number, inputLength: number): number {
    this.valueBlock.isIndefiniteForm = this.lenBlock.isIndefiniteForm;

    const resultOffset = this.valueBlock.fromBER(inputBuffer, inputOffset, (this.lenBlock.isIndefiniteForm) ? inputLength : this.lenBlock.length);
    if (resultOffset === -1) {
      this.error = this.valueBlock.error;

      return resultOffset;
    }

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

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

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

    return resultOffset;
  }

  /**
   * @internal
   */
  public override onAsciiEncoding(): string {
    const values = [];
    for (const value of this.valueBlock.value) {
      values.push(value.toString("ascii").split("\n").map(o => `  ${o}`).join("\n"));
    }
    const blockName = this.idBlock.tagClass === 3
      ? `[${this.idBlock.tagNumber}]`
      : (this.constructor as typeof Constructed).NAME;

    return values.length
      ? `${blockName} :\n${values.join("\n")}` // items
      : `${blockName} :`; // empty
  }

}