summaryrefslogtreecommitdiffstats
path: root/comm/third_party/asn1js/src/internals/LocalObjectIdentifierValueBlock.ts
blob: a24b9e947a4b7fd4e6ff5ad0002b15f8ee9a6fd9 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { ValueBlock, ValueBlockJson, ValueBlockParams } from "../ValueBlock";
import { EMPTY_BUFFER, EMPTY_STRING } from "./constants";
import * as utils from "./utils";
import { LocalSidValueBlockJson, LocalSidValueBlock } from "./LocalSidValueBlock";
import { IStringConvertible } from "../types";


export interface ILocalObjectIdentifierValueBlock {
  value: string;
}

export interface LocalObjectIdentifierValueBlockParams extends ValueBlockParams, Partial<ILocalObjectIdentifierValueBlock> { }

export interface LocalObjectIdentifierValueBlockJson extends ValueBlockJson, ILocalObjectIdentifierValueBlock {
  sidArray: LocalSidValueBlockJson[];
}

export class LocalObjectIdentifierValueBlock extends ValueBlock implements IStringConvertible {

  public static override NAME = "ObjectIdentifierValueBlock";

  public value: LocalSidValueBlock[] = [];

  constructor({
    value = EMPTY_STRING,
    ...parameters
  }: LocalObjectIdentifierValueBlockParams = {}) {
    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 LocalSidValueBlock();
      resultOffset = sidBlock.fromBER(inputBuffer, resultOffset, inputLength);
      if (resultOffset === -1) {
        this.blockLength = 0;
        this.error = sidBlock.error;

        return resultOffset;
      }

      if (this.value.length === 0)
        sidBlock.isFirstSid = true;

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

      this.value.push(sidBlock);
    }

    return resultOffset;
  }
  public override toBER(sizeOnly?: boolean): 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): void {
    this.value = []; // Clear existing SID values

    let pos1 = 0;
    let pos2 = 0;

    let sid = "";

    let flag = false;

    // const sids = string.split(".");
    // for (const sid of sids) {

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

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

      pos1 = pos2 + 1;

      if (flag) {
        const sidBlock = this.value[0];

        let plus = 0;

        switch (sidBlock.valueDec) {
          case 0:
            break;
          case 1:
            plus = 40;
            break;
          case 2:
            plus = 80;
            break;
          default:
            this.value = []; // clear SID array

            return;
        }

        const parsedSID = parseInt(sid, 10);
        if (isNaN(parsedSID))
          return;

        sidBlock.valueDec = parsedSID + plus;

        flag = false;
      } else {
        const sidBlock = new LocalSidValueBlock();
        if ((sid as any) > Number.MAX_SAFE_INTEGER) { // TODO remove as any
          utils.assertBigInt();
          const sidValue = BigInt(sid);
          sidBlock.valueBigInt = sidValue;
        } else {
          sidBlock.valueDec = parseInt(sid, 10);
          if (isNaN(sidBlock.valueDec))
            return;
        }

        if (!this.value.length) {
          sidBlock.isFirstSid = true;
          flag = true;
        }

        this.value.push(sidBlock);
      }
    } while (pos2 !== -1);
  }

  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}}`;

        if (this.value[i].isFirstSid)
          result = `2.{${sidStr} - 80}`;

        else
          result += sidStr;
      }

      else
        result += sidStr;
    }

    return result;
  }

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

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

    return object;
  }

}