UnicodeArrayRotateFunctionNode.ts 3.1 KB

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