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

PeculiarVentures / ASN1.js / 20070693751

09 Dec 2025 04:24PM UTC coverage: 77.789% (+0.02%) from 77.767%
20070693751

push

github

microshine
style: fix linter errors

559 of 940 branches covered (59.47%)

1 of 1 new or added line in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

1583 of 2035 relevant lines covered (77.79%)

11543.69 hits per line

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

95.45
/src/internals/LocalBitStringValueBlock.ts
1
import * as pvtsutils from "pvtsutils";
12✔
2
import { ViewWriter } from "../ViewWriter";
3
import {
12✔
4
  HexBlockJson, HexBlockParams, HexBlock,
5
} from "../HexBlock";
6
import { localFromBER } from "../parser";
12✔
7
import type { BitString } from "../BitString";
8
import { BIT_STRING_NAME, END_OF_CONTENT_NAME } from "./constants";
12✔
9
import {
12✔
10
  LocalConstructedValueBlockParams, LocalConstructedValueBlockJson, LocalConstructedValueBlock,
11
} from "./LocalConstructedValueBlock";
12
import { checkBufferParams } from "./utils";
12✔
13

14
export interface ILocalBitStringValueBlock {
15
  unusedBits: number;
16
  isConstructed: boolean;
17
}
18

19
export interface LocalBitStringValueBlockParams extends
20
  HexBlockParams, LocalConstructedValueBlockParams, Partial<ILocalBitStringValueBlock> {
21
  value?: BitString[];
22
}
23

24
export interface LocalBitStringValueBlockJson extends
25
  HexBlockJson, LocalConstructedValueBlockJson, ILocalBitStringValueBlock { }
26

27
export class LocalBitStringValueBlock extends
12✔
28
  HexBlock(LocalConstructedValueBlock) implements ILocalBitStringValueBlock {
29
  public static override NAME = "BitStringValueBlock";
12✔
30

31
  public unusedBits: number;
32
  public isConstructed: boolean;
33

34
  constructor({
×
35
    unusedBits = 0,
84✔
36
    isConstructed = false,
×
37
    ...parameters
38
  }: LocalBitStringValueBlockParams = {}) {
39
    super(parameters);
172✔
40

41
    this.unusedBits = unusedBits;
172✔
42
    this.isConstructed = isConstructed;
172✔
43
    this.blockLength = this.valueHexView.byteLength;
172✔
44
  }
45

46
  public override fromBER(inputBuffer: ArrayBuffer | Uint8Array, inputOffset: number, inputLength: number): number {
47
    // Ability to decode zero-length BitString value
48
    if (!inputLength) {
140✔
49
      return inputOffset;
12✔
50
    }
51

52
    let resultOffset = -1;
128✔
53

54
    // If the BIT STRING supposed to be a constructed value
55
    if (this.isConstructed) {
128✔
56
      resultOffset = LocalConstructedValueBlock.prototype.fromBER.call(this, inputBuffer, inputOffset, inputLength);
32✔
57
      if (resultOffset === -1)
32✔
58
        return resultOffset;
8✔
59

60
      for (const value of this.value) {
24✔
61
        const currentBlockName = (value.constructor as typeof LocalBitStringValueBlock).NAME;
48✔
62

63
        if (currentBlockName === END_OF_CONTENT_NAME) {
48✔
64
          if (this.isIndefiniteForm)
4!
65
            break;
×
66
          else {
67
            this.error = "EndOfContent is unexpected, BIT STRING may consists of BIT STRINGs only";
4✔
68

69
            return -1;
4✔
70
          }
71
        }
72

73
        if (currentBlockName !== BIT_STRING_NAME) {
44✔
74
          this.error = "BIT STRING may consists of BIT STRINGs only";
4✔
75

76
          return -1;
4✔
77
        }
78

79
        const valueBlock = value.valueBlock as unknown as LocalBitStringValueBlock;
40✔
80
        if ((this.unusedBits > 0) && (valueBlock.unusedBits > 0)) {
40✔
81
          this.error = "Using of \"unused bits\" inside constructive BIT STRING allowed for least one only";
4✔
82

83
          return -1;
4✔
84
        }
85

86
        this.unusedBits = valueBlock.unusedBits;
36✔
87
      }
88

89
      return resultOffset;
12✔
90
    }
91

92
    const inputView = pvtsutils.BufferSourceConverter.toUint8Array(inputBuffer);
96✔
93

94
    // If the BitString supposed to be a primitive value
95
    if (!checkBufferParams(this, inputView, inputOffset, inputLength)) {
96✔
96
      return -1;
4✔
97
    }
98

99
    const intBuffer = inputView.subarray(inputOffset, inputOffset + inputLength);
92✔
100

101
    this.unusedBits = intBuffer[0];
92✔
102

103
    if (this.unusedBits > 7) {
92✔
104
      this.error = "Unused bits for BitString must be in range 0-7";
16✔
105

106
      return -1;
16✔
107
    }
108

109
    if (!this.unusedBits) {
76✔
110
      const buf = intBuffer.subarray(1);
48✔
111
      try {
48✔
112
        if (buf.byteLength) {
48✔
113
          const asn = localFromBER(buf, 0, buf.byteLength);
44✔
114
          if (asn.offset !== -1 && asn.offset === (inputLength - 1)) {
44✔
115
            this.value = [asn.result as BitString];
4✔
116
          }
117
        }
118
      } catch {
119
        // nothing
120
      }
121
    }
122

123
    // Copy input buffer to internal buffer
124
    this.valueHexView = intBuffer.subarray(1);
76✔
125
    this.blockLength = intBuffer.length;
76✔
126

127
    return (inputOffset + inputLength);
76✔
128
  }
129

130
  public override toBER(sizeOnly?: boolean, writer?: ViewWriter): ArrayBuffer {
131
    if (this.isConstructed) {
32✔
132
      return LocalConstructedValueBlock.prototype.toBER.call(this, sizeOnly, writer);
8✔
133
    }
134

135
    if (sizeOnly) {
24!
136
      return new ArrayBuffer(this.valueHexView.byteLength + 1);
×
137
    }
138

139
    if (!this.valueHexView.byteLength) {
24✔
140
      const empty = new Uint8Array(1);
4✔
141
      empty[0] = 0; // unusedBits
4✔
142
      return empty.buffer;
4✔
143
    }
144

145
    const retView = new Uint8Array(this.valueHexView.length + 1);
20✔
146

147
    retView[0] = this.unusedBits;
20✔
148
    retView.set(this.valueHexView, 1);
20✔
149

150
    return retView.buffer;
20✔
151
  }
152

153
  public override toJSON(): LocalBitStringValueBlockJson {
UNCOV
154
    return {
×
155
      ...super.toJSON(),
156
      unusedBits: this.unusedBits,
157
      isConstructed: this.isConstructed,
158
    } as LocalBitStringValueBlockJson;
159
  }
160
}
161

162
export interface LocalBitStringValueBlock {
163
  /**
164
   * @deprecated since version 3.0.0
165
   */
166
  // @ts-ignore
167
  valueBeforeDecode: ArrayBuffer;
168
  /**
169
   * Binary data in ArrayBuffer representation
170
   *
171
   * @deprecated since version 3.0.0
172
   */
173
  // @ts-ignore
174
  valueHex: ArrayBuffer;
175
}
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