summaryrefslogtreecommitdiffstats
path: root/comm/third_party/asn1js/src/internals/LocalBooleanValueBlock.ts
blob: a4cdb35deed83654be76031f19ca24af5d02a2a0 (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
/* eslint-disable @typescript-eslint/ban-ts-comment */
import * as pvtsutils from "pvtsutils";
import * as pvutils from "pvutils";
import { HexBlockJson, HexBlockParams, HexBlock } from "../HexBlock";
import { ValueBlock, ValueBlockJson, ValueBlockParams } from "../ValueBlock";
import { checkBufferParams } from "./utils";

export interface ILocalBooleanValueBlock {
  value: boolean;
}

export interface LocalBooleanValueBlockParams extends ValueBlockParams, HexBlockParams, Partial<ILocalBooleanValueBlock> { }

export interface LocalBooleanValueBlockJson extends ValueBlockJson, HexBlockJson, ILocalBooleanValueBlock { }

export class LocalBooleanValueBlock extends HexBlock(ValueBlock) implements ILocalBooleanValueBlock {

  public static override NAME = "BooleanValueBlock";

  public get value(): boolean {
    for (const octet of this.valueHexView) {
      if (octet > 0) {
        return true;
      }
    }

    return false;
  }

  public set value(value: boolean) {
    this.valueHexView[0] = value ? 0xFF : 0x00;
  }

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

    if (parameters.valueHex) {
      this.valueHexView = pvtsutils.BufferSourceConverter.toUint8Array(parameters.valueHex);
    } else {
      this.valueHexView = new Uint8Array(1);
    }

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

  public override fromBER(inputBuffer: ArrayBuffer | Uint8Array, inputOffset: number, inputLength: number): number {
    const inputView = pvtsutils.BufferSourceConverter.toUint8Array(inputBuffer);
    // Basic check for parameters
    if (!checkBufferParams(this, inputView, inputOffset, inputLength)) {
      return -1;
    }

    // Getting Uint8Array
    this.valueHexView = inputView.subarray(inputOffset, inputOffset + inputLength);

    if (inputLength > 1)
      this.warnings.push("Boolean value encoded in more then 1 octet");

    this.isHexOnly = true;
    pvutils.utilDecodeTC.call(this);
    this.blockLength = inputLength;

    return (inputOffset + inputLength);
  }

  public override toBER(): ArrayBuffer {
    return this.valueHexView.slice();
  }

  public override toJSON(): LocalBooleanValueBlockJson {
    return {
      ...super.toJSON(),
      value: this.value,
    };
  }
}

export interface LocalBooleanValueBlock {
  /**
   * @deprecated since version 3.0.0
   */
  // @ts-ignore
  valueBeforeDecode: ArrayBuffer;
  /**
   * Binary data in ArrayBuffer representation
   *
   * @deprecated since version 3.0.0
   */
  // @ts-ignore
  valueHex: ArrayBuffer;
}