UnicodeArrayRotateFunctionNode.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { INode } from "../../interfaces/nodes/INode";
  2. import { IOptions } from "../../interfaces/IOptions";
  3. import { TNodeWithBlockStatement } from "../../types/TNodeWithBlockStatement";
  4. import { AppendState } from "../../enums/AppendState";
  5. import { NO_CUSTOM_NODES_PRESET } from "../../preset-options/NoCustomNodesPreset";
  6. import { SelfDefendingTemplate } from "../../templates/custom-nodes/unicode-array-nodes/unicode-array-rotate-function-node/SelfDefendingTemplate";
  7. import { UnicodeArrayRotateFunctionTemplate } from "../../templates/custom-nodes/unicode-array-nodes/unicode-array-rotate-function-node/UnicodeArrayRotateFunctionTemplate";
  8. import { JavaScriptObfuscator } from "../../JavaScriptObfuscator";
  9. import { Node } from '../Node';
  10. import { NodeUtils } from "../../NodeUtils";
  11. import { Utils } from "../../Utils";
  12. export class UnicodeArrayRotateFunctionNode extends Node {
  13. /**
  14. * @type {AppendState}
  15. */
  16. protected appendState: AppendState = AppendState.AfterObfuscation;
  17. /**
  18. * @type {string[]}
  19. */
  20. private unicodeArray: string[];
  21. /**
  22. * @type {string}
  23. */
  24. private unicodeArrayName: string;
  25. /**
  26. * @param {number}
  27. */
  28. private unicodeArrayRotateValue: number;
  29. /**
  30. * @param unicodeArrayName
  31. * @param unicodeArray
  32. * @param unicodeArrayRotateValue
  33. * @param options
  34. */
  35. constructor (
  36. unicodeArrayName: string,
  37. unicodeArray: string[],
  38. unicodeArrayRotateValue: number,
  39. options: IOptions
  40. ) {
  41. super(options);
  42. this.unicodeArrayName = unicodeArrayName;
  43. this.unicodeArray = unicodeArray;
  44. this.unicodeArrayRotateValue = unicodeArrayRotateValue;
  45. }
  46. /**
  47. * @param blockScopeNode
  48. */
  49. public appendNode (blockScopeNode: TNodeWithBlockStatement): void {
  50. if (!this.unicodeArray.length) {
  51. return;
  52. }
  53. NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), 1);
  54. }
  55. /**
  56. * @returns {INode}
  57. */
  58. public getNode (): INode {
  59. return super.getNode();
  60. }
  61. /**
  62. * @returns {INode}
  63. */
  64. protected getNodeStructure (): INode {
  65. let code: string = '',
  66. timesName: string = Utils.getRandomVariableName(),
  67. whileFunctionName: string = Utils.getRandomVariableName();
  68. if (this.options.selfDefending) {
  69. code = JavaScriptObfuscator.obfuscate(
  70. SelfDefendingTemplate(whileFunctionName, timesName),
  71. NO_CUSTOM_NODES_PRESET
  72. ).getObfuscatedCode();
  73. } else {
  74. code = `${whileFunctionName}(++${timesName})`;
  75. }
  76. return NodeUtils.convertCodeToStructure(
  77. UnicodeArrayRotateFunctionTemplate(
  78. code,
  79. this.unicodeArrayName,
  80. this.unicodeArrayRotateValue,
  81. whileFunctionName,
  82. timesName
  83. )
  84. );
  85. }
  86. }