summaryrefslogtreecommitdiffstats
path: root/comm/third_party/asn1js/src/internals/LocalBaseBlock.ts
blob: c144c49d1086d4d7a1b3247e9ae5751d2c9825a8 (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
import * as pvtsutils from "pvtsutils";
import { EMPTY_STRING, EMPTY_VIEW } from "./constants";

export interface ILocalBaseBlock {
  blockLength: number;
  error: string;
  warnings: string[];
}

export interface LocalBaseBlockJson extends ILocalBaseBlock {
  blockName: string;
  valueBeforeDecode: string;
}

export interface LocalBaseBlockParams extends Partial<ILocalBaseBlock> {
  valueBeforeDecode?: pvtsutils.BufferSource;
}

export interface LocalBaseBlockConstructor<T extends LocalBaseBlock = LocalBaseBlock> {
  new(...args: any[]): T;
  prototype: T;
  NAME: string;
  blockName(): string;
}

/**
 * Class used as a base block for all remaining ASN.1 classes
 */
export class LocalBaseBlock implements ILocalBaseBlock {

  /**
   * Name of the block
   */
  public static NAME = "baseBlock";
  /**
   * Aux function, need to get a block name. Need to have it here for inheritance
   * @returns Returns name of the block
   */
  public static blockName(): string {
    return this.NAME;
  }

  public blockLength: number;
  public error: string;
  public warnings: string[];
  /**
   * @deprecated since version 3.0.0
   */
  public get valueBeforeDecode(): ArrayBuffer {
    return this.valueBeforeDecodeView.slice().buffer;
  }
  /**
   * @deprecated since version 3.0.0
   */
  public set valueBeforeDecode(value: ArrayBuffer) {
    this.valueBeforeDecodeView = new Uint8Array(value);
  }
  /**
   * @since 3.0.0
   */
  public valueBeforeDecodeView: Uint8Array;

  /**
   * Creates and initializes an object instance of that class
   * @param param0 Initialization parameters
   */
  constructor({
    blockLength = 0,
    error = EMPTY_STRING,
    warnings = [],
    valueBeforeDecode = EMPTY_VIEW,
  }: LocalBaseBlockParams = {}) {
    this.blockLength = blockLength;
    this.error = error;
    this.warnings = warnings;
    this.valueBeforeDecodeView = pvtsutils.BufferSourceConverter.toUint8Array(valueBeforeDecode);
  }

  /**
   * Returns a JSON representation of an object
   * @returns JSON object
   */
  public toJSON(): LocalBaseBlockJson {
    return {
      blockName: (this.constructor as typeof LocalBaseBlock).NAME,
      blockLength: this.blockLength,
      error: this.error,
      warnings: this.warnings,
      valueBeforeDecode: pvtsutils.Convert.ToHex(this.valueBeforeDecodeView),
    };
  }

}