summaryrefslogtreecommitdiffstats
path: root/comm/third_party/asn1js/src/HexBlock.ts
blob: cca34da5deb253c7c583e486892c0b73d86af7cd (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
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import * as pvtsutils from "pvtsutils";
import { IBerConvertible } from "./types";
import { EMPTY_BUFFER, EMPTY_VIEW } from "./internals/constants";
import { LocalBaseBlockConstructor } from "./internals/LocalBaseBlock";
import { checkBufferParams } from "./internals/utils";

export interface IHexBlock {
  isHexOnly: boolean;
  valueHex: pvtsutils.BufferSource;
}

export interface HexBlockJson extends Omit<IHexBlock, "valueHex"> {
  valueHex: string;
}

export type HexBlockParams = Partial<IHexBlock>;
/**
 * Class used as a base block for all remaining ASN.1 classes
 */
export function HexBlock<T extends LocalBaseBlockConstructor>(BaseClass: T) {
  return class Some extends BaseClass implements IHexBlock, IBerConvertible {

    public static override NAME = "hexBlock";

    public isHexOnly: boolean;
    /**
     * Binary data in ArrayBuffer representation
     *
     * @deprecated since version 3.0.0
     */
    public get valueHex(): ArrayBuffer {
      return this.valueHexView.slice().buffer;
    }
    /**
     * Binary data in ArrayBuffer representation
     *
     * @deprecated since version 3.0.0
     */
    public set valueHex(value: ArrayBuffer) {
      this.valueHexView = new Uint8Array(value);
    }
    /**
     * Binary data in Uint8Array representation
     *
     * @since 3.0.0
     */
    public valueHexView: Uint8Array;

    constructor(...args: any[]) {
      super(...args);

      const params: HexBlockParams = args[0] || {};
      this.isHexOnly = params.isHexOnly ?? false;
      this.valueHexView = params.valueHex ? pvtsutils.BufferSourceConverter.toUint8Array(params.valueHex) : EMPTY_VIEW;
    }

    public fromBER(inputBuffer: ArrayBuffer | Uint8Array, inputOffset: number, inputLength: number): number {
      // Basic check for parameters
      const view = inputBuffer instanceof ArrayBuffer ? new Uint8Array(inputBuffer) : inputBuffer;
      if (!checkBufferParams(this, view, inputOffset, inputLength)) {
        return -1;
      }

      const endLength = inputOffset + inputLength;

      // Initial checks
      this.valueHexView = view.subarray(inputOffset, endLength);
      if (!this.valueHexView.length) {
        this.warnings.push("Zero buffer length");

        return inputOffset;
      }

      this.blockLength = inputLength;

      return endLength;
    }

    public toBER(sizeOnly = false): ArrayBuffer {
      if (!this.isHexOnly) {
        this.error = "Flag 'isHexOnly' is not set, abort";

        return EMPTY_BUFFER;
      }

      if (sizeOnly) {
        return new ArrayBuffer(this.valueHexView.byteLength);
      }

      // Don't copy data if View is not offset
      return (this.valueHexView.byteLength === this.valueHexView.buffer.byteLength)
        ? this.valueHexView.buffer
        : this.valueHexView.slice().buffer;
    }

    /**
     * Returns a JSON representation of an object
     * @returns JSON object
     */
    public override toJSON() {
      return {
        ...super.toJSON(),
        isHexOnly: this.isHexOnly,
        valueHex: pvtsutils.Convert.ToHex(this.valueHexView),
      };
    }

  };
}