• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PeculiarVentures / ASN1.js / 14130271268

28 Mar 2025 01:41PM UTC coverage: 77.767% (-0.03%) from 77.8%
14130271268

push

github

web-flow
Merge pull request #114 from PeculiarVentures/update

Update dependencies and migrate testing framework to Jest

386 of 652 branches covered (59.2%)

102 of 113 new or added lines in 55 files covered. (90.27%)

16 existing lines in 7 files now uncovered.

1581 of 2033 relevant lines covered (77.77%)

5777.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.72
/src/internals/LocalIntegerValueBlock.ts
1
import * as pvutils from "pvutils";
6✔
2
import {
6✔
3
  HexBlockJson, HexBlockParams, HexBlock,
4
} from "../HexBlock";
5
import { IDerConvertible } from "../types";
6
import {
6✔
7
  ValueBlock, ValueBlockJson, ValueBlockParams,
8
} from "../ValueBlock";
9
import { powers2, digitsString } from "./constants";
6✔
10

11
function viewAdd(first: Uint8Array, second: Uint8Array): Uint8Array {
12
  // #region Initial variables
13
  const c = new Uint8Array([0]);
2,338✔
14

15
  const firstView = new Uint8Array(first);
2,338✔
16
  const secondView = new Uint8Array(second);
2,338✔
17

18
  let firstViewCopy = firstView.slice(0);
2,338✔
19
  const firstViewCopyLength = firstViewCopy.length - 1;
2,338✔
20
  const secondViewCopy = secondView.slice(0);
2,338✔
21
  const secondViewCopyLength = secondViewCopy.length - 1;
2,338✔
22

23
  let value = 0;
2,338✔
24

25
  const max = (secondViewCopyLength < firstViewCopyLength) ? firstViewCopyLength : secondViewCopyLength;
2,338!
26

27
  let counter = 0;
2,338✔
28
  // #endregion
29
  for (let i = max; i >= 0; i--, counter++) {
2,338✔
30
    switch (true) {
1,425,232✔
31
      case (counter < secondViewCopy.length):
32
        value = firstViewCopy[firstViewCopyLength - counter] + secondViewCopy[secondViewCopyLength - counter] + c[0];
642,102✔
33
        break;
642,102✔
34
      default:
35
        value = firstViewCopy[firstViewCopyLength - counter] + c[0];
783,130✔
36
    }
37

38
    c[0] = value / 10;
1,425,232✔
39

40
    switch (true) {
1,425,232!
41
      case (counter >= firstViewCopy.length):
42
        firstViewCopy = pvutils.utilConcatView(new Uint8Array([value % 10]), firstViewCopy);
×
43
        break;
×
44
      default:
45
        firstViewCopy[firstViewCopyLength - counter] = value % 10;
1,425,232✔
46
    }
47
  }
48

49
  if (c[0] > 0)
2,338!
50
    firstViewCopy = pvutils.utilConcatView(c, firstViewCopy);
×
51

52
  return firstViewCopy;
2,338✔
53
}
54

55
function power2(n: number): Uint8Array {
56
  if (n >= powers2.length) {
2,344✔
57
    for (let p = powers2.length; p <= n; p++) {
2,118✔
58
      const c = new Uint8Array([0]);
4,222✔
59
      let digits = (powers2[p - 1]).slice(0);
4,222✔
60

61
      for (let i = (digits.length - 1); i >= 0; i--) {
4,222✔
62
        const newValue = new Uint8Array([(digits[i] << 1) + c[0]]);
1,264,090✔
63
        c[0] = newValue[0] / 10;
1,264,090✔
64
        digits[i] = newValue[0] % 10;
1,264,090✔
65
      }
66

67
      if (c[0] > 0)
4,222✔
68
        digits = pvutils.utilConcatView(c, digits);
1,270✔
69

70
      powers2.push(digits);
4,222✔
71
    }
72
  }
73

74
  return powers2[n];
2,344✔
75
}
76

77
function viewSub(first: Uint8Array, second: Uint8Array): Uint8Array {
78
  // #region Initial variables
79
  let b = 0;
6✔
80

81
  const firstView = new Uint8Array(first);
6✔
82
  const secondView = new Uint8Array(second);
6✔
83

84
  const firstViewCopy = firstView.slice(0);
6✔
85
  const firstViewCopyLength = firstViewCopy.length - 1;
6✔
86
  const secondViewCopy = secondView.slice(0);
6✔
87
  const secondViewCopyLength = secondViewCopy.length - 1;
6✔
88

89
  let value;
90

91
  let counter = 0;
6✔
92
  // #endregion
93
  for (let i = secondViewCopyLength; i >= 0; i--, counter++) {
6✔
94
    value = firstViewCopy[firstViewCopyLength - counter] - secondViewCopy[secondViewCopyLength - counter] - b;
126✔
95

96
    switch (true) {
126✔
97
      case (value < 0):
98
        b = 1;
60✔
99
        firstViewCopy[firstViewCopyLength - counter] = value + 10;
60✔
100
        break;
60✔
101
      default:
102
        b = 0;
66✔
103
        firstViewCopy[firstViewCopyLength - counter] = value;
66✔
104
    }
105
  }
106

107
  if (b > 0) {
6!
108
    for (let i = (firstViewCopyLength - secondViewCopyLength + 1); i >= 0; i--, counter++) {
×
109
      value = firstViewCopy[firstViewCopyLength - counter] - b;
×
110

111
      if (value < 0) {
×
112
        b = 1;
×
113
        firstViewCopy[firstViewCopyLength - counter] = value + 10;
×
114
      } else {
UNCOV
115
        b = 0;
×
116
        firstViewCopy[firstViewCopyLength - counter] = value;
×
117
        break;
×
118
      }
119
    }
120
  }
121

122
  return firstViewCopy.slice();
6✔
123
}
124

125
export interface ILocalIntegerValueBlock {
126
  value: number;
127
}
128

129
export interface LocalIntegerValueBlockParams extends
130
  HexBlockParams, ValueBlockParams, Partial<ILocalIntegerValueBlock> { }
131

132
export interface LocalIntegerValueBlockJson extends HexBlockJson, ValueBlockJson {
133
  valueDec: number;
134
}
135

136
export class LocalIntegerValueBlock extends HexBlock(ValueBlock) implements IDerConvertible {
6✔
137
  protected setValueHex(): void {
138
    if (this.valueHexView.length >= 4) {
38✔
139
      this.warnings.push("Too big Integer for decoding, hex only");
24✔
140
      this.isHexOnly = true;
24✔
141
      this._valueDec = 0;
24✔
142
    } else {
143
      this.isHexOnly = false;
14✔
144

145
      if (this.valueHexView.length > 0) {
14✔
146
        this._valueDec = pvutils.utilDecodeTC.call(this);
14✔
147
      }
148
    }
149
  }
150

151
  public static override NAME = "IntegerValueBlock";
6✔
152

153
  static {
6✔
154
    Object.defineProperty(this.prototype, "valueHex", {
6✔
155
      set: function (this: LocalIntegerValueBlock, v: ArrayBuffer) {
156
        this.valueHexView = new Uint8Array(v);
×
157

158
        this.setValueHex();
×
159
      },
160
      get: function (this: LocalIntegerValueBlock) {
161
        return this.valueHexView.slice().buffer;
152✔
162
      },
163
    });
164
  }
165

166
  private _valueDec = 0;
46✔
167

168
  constructor({
×
169
    value,
170
    ...parameters
171
  }: LocalIntegerValueBlockParams = {}) {
172
    super(parameters);
46✔
173

174
    if (parameters.valueHex) {
46✔
175
      this.setValueHex();
22✔
176
    }
177

178
    if (value !== undefined) {
46✔
179
      this.valueDec = value;
2✔
180
    }
181
  }
182

183
  public set valueDec(v: number) {
184
    this._valueDec = v;
2✔
185

186
    this.isHexOnly = false;
2✔
187
    this.valueHexView = new Uint8Array(pvutils.utilEncodeTC(v));
2✔
188
  }
189

190
  public get valueDec(): number {
191
    return this._valueDec;
8✔
192
  }
193

194
  public fromDER(inputBuffer: ArrayBuffer, inputOffset: number, inputLength: number, expectedLength = 0): number {
×
195
    const offset = this.fromBER(inputBuffer, inputOffset, inputLength);
×
196
    if (offset === -1)
×
197
      return offset;
×
198

199
    const view = this.valueHexView;
×
200

201
    if ((view[0] === 0x00) && ((view[1] & 0x80) !== 0)) {
×
202
      this.valueHexView = view.subarray(1);
×
203
    } else {
UNCOV
204
      if (expectedLength !== 0) {
×
205
        if (view.length < expectedLength) {
×
206
          if ((expectedLength - view.length) > 1)
×
207
            expectedLength = view.length + 1;
×
208

209
          this.valueHexView = view.subarray(expectedLength - view.length);
×
210
        }
211
      }
212
    }
213

214
    return offset;
×
215
  }
216

217
  public toDER(sizeOnly = false): ArrayBuffer {
4✔
218
    const view = this.valueHexView;
4✔
219

220
    switch (true) {
4!
221
      case ((view[0] & 0x80) !== 0):
222
        {
223
          const updatedView = new Uint8Array(this.valueHexView.length + 1);
2✔
224

225
          updatedView[0] = 0x00;
2✔
226
          updatedView.set(view, 1);
2✔
227

228
          this.valueHexView = updatedView;
2✔
229
        }
230
        break;
2✔
231
      case ((view[0] === 0x00) && ((view[1] & 0x80) === 0)):
2!
232
        {
233
          this.valueHexView = this.valueHexView.subarray(1);
×
234
        }
235
        break;
×
236
      default:
237
    }
238

239
    return this.toBER(sizeOnly);
4✔
240
  }
241

242
  public override fromBER(inputBuffer: ArrayBuffer, inputOffset: number, inputLength: number): number {
243
    const resultOffset = super.fromBER(inputBuffer, inputOffset, inputLength);
22✔
244
    if (resultOffset === -1) {
22✔
245
      return resultOffset;
6✔
246
    }
247

248
    this.setValueHex();
16✔
249

250
    return resultOffset;
16✔
251
  }
252

253
  public override toBER(sizeOnly?: boolean): ArrayBuffer {
254
    return sizeOnly
28!
255
      ? new ArrayBuffer(this.valueHexView.length)
256
      : this.valueHexView.slice().buffer;
257
  }
258

259
  public override toJSON(): LocalIntegerValueBlockJson {
260
    return {
×
261
      ...super.toJSON(),
262
      valueDec: this.valueDec,
263
    };
264
  }
265

266
  public override toString(): string {
267
    // #region Initial variables
268
    const firstBit = (this.valueHexView.length * 8) - 1;
28✔
269

270
    let digits = new Uint8Array((this.valueHexView.length * 8) / 3);
28✔
271
    let bitNumber = 0;
28✔
272
    let currentByte;
273

274
    const asn1View = this.valueHexView;
28✔
275

276
    let result = "";
28✔
277

278
    let flag = false;
28✔
279
    // #endregion
280
    // #region Calculate number
281
    for (let byteNumber = (asn1View.byteLength - 1); byteNumber >= 0; byteNumber--) {
28✔
282
      currentByte = asn1View[byteNumber];
650✔
283

284
      for (let i = 0; i < 8; i++) {
650✔
285
        if ((currentByte & 1) === 1) {
5,200✔
286
          switch (bitNumber) {
2,344✔
287
            case firstBit:
288
              digits = viewSub(power2(bitNumber), digits);
6✔
289
              result = "-";
6✔
290
              break;
6✔
291
            default:
292
              digits = viewAdd(digits, power2(bitNumber));
2,338✔
293
          }
294
        }
295

296
        bitNumber++;
5,200✔
297
        currentByte >>= 1;
5,200✔
298
      }
299
    }
300
    // #endregion
301
    // #region Print number
302
    for (let i = 0; i < digits.length; i++) {
28✔
303
      if (digits[i])
1,716✔
304
        flag = true;
1,396✔
305

306
      if (flag)
1,716✔
307
        result += digitsString.charAt(digits[i]);
1,522✔
308
    }
309

310
    if (flag === false)
28!
311
      result += digitsString.charAt(0);
×
312
    // #endregion
313

314
    return result;
28✔
315
  }
316
}
317

318
export interface LocalIntegerValueBlock {
319
  /**
320
   * @deprecated since version 3.0.0
321
   */
322
  // @ts-ignore
323
  valueBeforeDecode: ArrayBuffer;
324
  /**
325
   * Binary data in ArrayBuffer representation
326
   *
327
   * @deprecated since version 3.0.0
328
   */
329
  // @ts-ignore
330
  valueHex: ArrayBuffer;
331
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc