UnicodeArrayNode.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import * as escodegen from 'escodegen';
  2. import { INode } from '../../interfaces/nodes/INode';
  3. import { TBlockScopeNode } from "../../types/TBlockScopeNode";
  4. import { AppendState } from '../../enums/AppendState';
  5. import { NodeType } from "../../enums/NodeType";
  6. import { Node } from '../Node';
  7. import { NodeUtils } from "../../NodeUtils";
  8. import { Utils } from '../../Utils';
  9. import {IVariableDeclarationNode} from "../../interfaces/nodes/IVariableDeclarationNode";
  10. export class UnicodeArrayNode extends Node {
  11. /**
  12. * @type {number}
  13. */
  14. public static UNICODE_ARRAY_RANDOM_LENGTH: number = 4;
  15. /**
  16. * @type {AppendState}
  17. */
  18. protected appendState: AppendState = AppendState.AfterObfuscation;
  19. /**
  20. * @type {string[]}
  21. */
  22. private unicodeArray: string[] = [];
  23. /**
  24. * @type {string}
  25. */
  26. private unicodeArrayName: string;
  27. /**
  28. * @type {number}
  29. */
  30. private unicodeArrayRotateValue: number;
  31. /**
  32. * @param unicodeArrayName
  33. * @param unicodeArrayRotateValue
  34. */
  35. constructor (unicodeArrayName: string, unicodeArrayRotateValue: number = 0) {
  36. super();
  37. this.unicodeArrayName = unicodeArrayName;
  38. this.unicodeArrayRotateValue = unicodeArrayRotateValue;
  39. this.node = this.getNodeStructure();
  40. }
  41. /**
  42. * @param blockScopeNode
  43. */
  44. public appendNode (blockScopeNode: TBlockScopeNode): void {
  45. NodeUtils.prependNode(blockScopeNode.body, this.getNode());
  46. }
  47. /**
  48. * @returns {string}
  49. */
  50. public getNodeIdentifier (): string {
  51. return this.unicodeArrayName;
  52. }
  53. /**
  54. * @returns {string[]}
  55. */
  56. public getNodeData (): string[] {
  57. return this.unicodeArray;
  58. }
  59. /**
  60. * @returns {INode}
  61. */
  62. public getNode (): INode {
  63. if (!this.unicodeArray.length) {
  64. return;
  65. }
  66. Utils.arrayRotate <string> (this.unicodeArray, this.unicodeArrayRotateValue);
  67. this.updateNode();
  68. return super.getNode();
  69. }
  70. /**
  71. * @returns {INode}
  72. */
  73. protected getNodeStructure (): IVariableDeclarationNode {
  74. return {
  75. 'type': NodeType.VariableDeclaration,
  76. 'declarations': [
  77. {
  78. 'type': NodeType.VariableDeclarator,
  79. 'id': {
  80. 'type': NodeType.Identifier,
  81. 'name': this.unicodeArrayName
  82. },
  83. 'init': {
  84. 'type': NodeType.ArrayExpression,
  85. 'elements': this.unicodeArray.map((value: string) => {
  86. return {
  87. 'type': NodeType.Literal,
  88. 'value': value,
  89. 'raw': `'${value}'`,
  90. 'x-verbatim-property': {
  91. 'content' : value,
  92. precedence: escodegen.Precedence.Primary
  93. }
  94. };
  95. })
  96. }
  97. }
  98. ],
  99. 'kind': 'var'
  100. };
  101. }
  102. }