summaryrefslogtreecommitdiffstats
path: root/comm/third_party/asn1js/src/internals/LocalLengthBlock.ts
blob: a4c7940b02fcb628c8de11324504644ea0187a53 (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
import * as pvtsutils from "pvtsutils";
import * as pvutils from "pvutils";
import { IBerConvertible } from "../types";
import { EMPTY_BUFFER } from "./constants";
import { LocalBaseBlock, LocalBaseBlockJson } from "./LocalBaseBlock";
import { checkBufferParams } from "./utils";

export interface ILocalLengthBlock {
  isIndefiniteForm: boolean;
  longFormUsed: boolean;
  length: number;
}

export interface LocalLengthBlockParams {
  lenBlock?: Partial<ILocalLengthBlock>;
}

export interface LocalLengthBlockJson extends LocalBaseBlockJson, ILocalLengthBlock {
  isIndefiniteForm: boolean;
  longFormUsed: boolean;
  length: number;
}

export class LocalLengthBlock extends LocalBaseBlock implements ILocalLengthBlock, IBerConvertible {

  public static override NAME = "lengthBlock";

  public isIndefiniteForm: boolean;
  public longFormUsed: boolean;
  public length: number;

  constructor({
    lenBlock = {},
  }: LocalLengthBlockParams = {}) {
    super();

    this.isIndefiniteForm = lenBlock.isIndefiniteForm ?? false;
    this.longFormUsed = lenBlock.longFormUsed ?? false;
    this.length = lenBlock.length ?? 0;
  }


  public fromBER(inputBuffer: ArrayBuffer | Uint8Array, inputOffset: number, inputLength: number): number {
    const view = pvtsutils.BufferSourceConverter.toUint8Array(inputBuffer);
    // Basic check for parameters
    if (!checkBufferParams(this, view, inputOffset, inputLength)) {
      return -1;
    }
    //#region Getting Uint8Array from ArrayBuffer
    const intBuffer = view.subarray(inputOffset, inputOffset + inputLength);
    //#endregion
    //#region Initial checks
    if (intBuffer.length === 0) {
      this.error = "Zero buffer length";

      return -1;
    }

    if (intBuffer[0] === 0xFF) {
      this.error = "Length block 0xFF is reserved by standard";

      return -1;
    }
    //#endregion
    //#region Check for length form type
    this.isIndefiniteForm = intBuffer[0] === 0x80;
    //#endregion
    //#region Stop working in case of indefinite length form
    if (this.isIndefiniteForm) {
      this.blockLength = 1;

      return (inputOffset + this.blockLength);
    }
    //#endregion
    //#region Check is long form of length encoding using
    this.longFormUsed = !!(intBuffer[0] & 0x80);
    //#endregion
    //#region Stop working in case of short form of length value
    if (this.longFormUsed === false) {
      this.length = (intBuffer[0]);
      this.blockLength = 1;

      return (inputOffset + this.blockLength);
    }
    //#endregion
    //#region Calculate length value in case of long form
    const count = intBuffer[0] & 0x7F;

    if (count > 8) // Too big length value
    {
      this.error = "Too big integer";

      return -1;
    }

    if ((count + 1) > intBuffer.length) {
      this.error = "End of input reached before message was fully decoded";

      return -1;
    }

    const lenOffset = inputOffset + 1;
    const lengthBufferView = view.subarray(lenOffset, lenOffset + count);

    if (lengthBufferView[count - 1] === 0x00)
      this.warnings.push("Needlessly long encoded length");

    this.length = pvutils.utilFromBase(lengthBufferView, 8);

    if (this.longFormUsed && (this.length <= 127))
      this.warnings.push("Unnecessary usage of long length form");

    this.blockLength = count + 1;
    //#endregion

    return (inputOffset + this.blockLength); // Return current offset in input buffer
  }

  public toBER(sizeOnly = false): ArrayBuffer {
    //#region Initial variables
    let retBuf: ArrayBuffer;
    let retView: Uint8Array;
    //#endregion
    if (this.length > 127)
      this.longFormUsed = true;

    if (this.isIndefiniteForm) {
      retBuf = new ArrayBuffer(1);

      if (sizeOnly === false) {
        retView = new Uint8Array(retBuf);
        retView[0] = 0x80;
      }

      return retBuf;
    }

    if (this.longFormUsed) {
      const encodedBuf = pvutils.utilToBase(this.length, 8);

      if (encodedBuf.byteLength > 127) {
        this.error = "Too big length";

        return (EMPTY_BUFFER);
      }

      retBuf = new ArrayBuffer(encodedBuf.byteLength + 1);

      if (sizeOnly)
        return retBuf;

      const encodedView = new Uint8Array(encodedBuf);
      retView = new Uint8Array(retBuf);

      retView[0] = encodedBuf.byteLength | 0x80;

      for (let i = 0; i < encodedBuf.byteLength; i++)
        retView[i + 1] = encodedView[i];

      return retBuf;
    }

    retBuf = new ArrayBuffer(1);

    if (sizeOnly === false) {
      retView = new Uint8Array(retBuf);

      retView[0] = this.length;
    }

    return retBuf;
  }

  public override toJSON(): LocalLengthBlockJson {
    return {
      ...super.toJSON(),
      isIndefiniteForm: this.isIndefiniteForm,
      longFormUsed: this.longFormUsed,
      length: this.length,
    };
  }
}