summaryrefslogtreecommitdiffstats
path: root/comm/third_party/asn1js/src/internals/LocalRelativeObjectIdentifierValueBlock.ts
blob: 13237f50ecbfbaf3870e591c1bf1fd908a5c8f07 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { ViewWriter } from "../ViewWriter";
import { ValueBlock, ValueBlockJson, ValueBlockParams } from "../ValueBlock";
import { EMPTY_BUFFER, EMPTY_STRING } from "./constants";
import * as utils from "./utils";
import { LocalRelativeSidValueBlockJson, LocalRelativeSidValueBlock } from "./LocalRelativeSidValueBlock";
import { IStringConvertible } from "../types";

export interface ILocalRelativeObjectIdentifierValueBlock {
  value: string;
}

export interface LocalRelativeObjectIdentifierValueBlockParams extends ValueBlockParams, Partial<ILocalRelativeObjectIdentifierValueBlock> { }

export interface LocalRelativeObjectIdentifierValueBlockJson extends ValueBlockJson, ILocalRelativeObjectIdentifierValueBlock {
  sidArray: LocalRelativeSidValueBlockJson[];
}

export class LocalRelativeObjectIdentifierValueBlock extends ValueBlock implements IStringConvertible {

  public static override NAME = "RelativeObjectIdentifierValueBlock";

  public value: LocalRelativeSidValueBlock[] = [];

  constructor({
    value = EMPTY_STRING,
    ...parameters
  }: LocalRelativeObjectIdentifierValueBlockParams = {}) {
    super(parameters);

    if (value) {
      this.fromString(value);
    }
  }

  public override fromBER(inputBuffer: ArrayBuffer | Uint8Array, inputOffset: number, inputLength: number): number {
    let resultOffset = inputOffset;

    while (inputLength > 0) {
      const sidBlock = new LocalRelativeSidValueBlock();
      resultOffset = sidBlock.fromBER(inputBuffer, resultOffset, inputLength);
      if (resultOffset === -1) {
        this.blockLength = 0;
        this.error = sidBlock.error;

        return resultOffset;
      }

      this.blockLength += sidBlock.blockLength;
      inputLength -= sidBlock.blockLength;

      this.value.push(sidBlock);
    }

    return resultOffset;
  }

  public override toBER(sizeOnly?: boolean, writer?: ViewWriter): ArrayBuffer {
    const retBuffers: ArrayBuffer[] = [];

    for (let i = 0; i < this.value.length; i++) {
      const valueBuf = this.value[i].toBER(sizeOnly);
      if (valueBuf.byteLength === 0) {
        this.error = this.value[i].error;

        return EMPTY_BUFFER;
      }

      retBuffers.push(valueBuf);
    }

    return utils.concat(retBuffers);
  }

  public fromString(string: string): boolean {
    this.value = []; // Clear existing SID values

    let pos1 = 0;
    let pos2 = 0;

    let sid = "";

    do {
      pos2 = string.indexOf(".", pos1);
      if (pos2 === -1)
        sid = string.substring(pos1);

      else
        sid = string.substring(pos1, pos2);

      pos1 = pos2 + 1;

      const sidBlock = new LocalRelativeSidValueBlock();
      sidBlock.valueDec = parseInt(sid, 10);
      if (isNaN(sidBlock.valueDec))
        return true;

      this.value.push(sidBlock);

    } while (pos2 !== -1);

    return true;
  }

  public override toString(): string {
    let result = "";
    let isHexOnly = false;

    for (let i = 0; i < this.value.length; i++) {
      isHexOnly = this.value[i].isHexOnly;

      let sidStr = this.value[i].toString();

      if (i !== 0)
        result = `${result}.`;

      if (isHexOnly) {
        sidStr = `{${sidStr}}`;
        result += sidStr;
      }
      else
        result += sidStr;
    }

    return result;
  }

  public override toJSON(): LocalRelativeObjectIdentifierValueBlockJson {
    const object: LocalRelativeObjectIdentifierValueBlockJson = {
      ...super.toJSON(),
      value: this.toString(),
      sidArray: [],
    };

    for (let i = 0; i < this.value.length; i++)
      object.sidArray.push(this.value[i].toJSON());

    return object;
  }

}