StringArrayRotateFunctionNode.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import * as format from 'string-template';
  2. import { TNodeWithBlockStatement } from '../../types/TNodeWithBlockStatement';
  3. import { TObfuscationEvent } from '../../types/TObfuscationEvent';
  4. import { IOptions } from '../../interfaces/IOptions';
  5. import { IStorage } from '../../interfaces/IStorage';
  6. import { ObfuscationEvents } from '../../enums/ObfuscationEvents';
  7. import { NO_CUSTOM_NODES_PRESET } from '../../preset-options/NoCustomNodesPreset';
  8. import { SelfDefendingTemplate } from '../../templates/custom-nodes/string-array-nodes/string-array-rotate-function-node/SelfDefendingTemplate';
  9. import { StringArrayRotateFunctionTemplate } from '../../templates/custom-nodes/string-array-nodes/string-array-rotate-function-node/StringArrayRotateFunctionTemplate';
  10. import { AbstractCustomNode } from '../AbstractCustomNode';
  11. import { JavaScriptObfuscator } from '../../JavaScriptObfuscator';
  12. import { NodeAppender } from '../../node/NodeAppender';
  13. import { Utils } from '../../Utils';
  14. export class StringArrayRotateFunctionNode extends AbstractCustomNode {
  15. /**
  16. * @type {TObfuscationEvent}
  17. */
  18. protected readonly appendEvent: TObfuscationEvent = ObfuscationEvents.AfterObfuscation;
  19. /**
  20. * @type {IStorage <string>}
  21. */
  22. private stringArray: IStorage <string>;
  23. /**
  24. * @type {string}
  25. */
  26. private stringArrayName: string;
  27. /**
  28. * @param {number}
  29. */
  30. private stringArrayRotateValue: number;
  31. /**
  32. * @param stringArrayName
  33. * @param stringArray
  34. * @param stringArrayRotateValue
  35. * @param options
  36. */
  37. constructor (
  38. stringArrayName: string,
  39. stringArray: IStorage <string>,
  40. stringArrayRotateValue: number,
  41. options: IOptions
  42. ) {
  43. super(options);
  44. this.stringArrayName = stringArrayName;
  45. this.stringArray = stringArray;
  46. this.stringArrayRotateValue = stringArrayRotateValue;
  47. }
  48. /**
  49. * @param blockScopeNode
  50. */
  51. public appendNode (blockScopeNode: TNodeWithBlockStatement): void {
  52. if (!this.stringArray.getLength()) {
  53. return;
  54. }
  55. NodeAppender.insertNodeAtIndex(blockScopeNode, this.getNode(), 1);
  56. }
  57. /**
  58. * @returns {string}
  59. */
  60. public getCode (): string {
  61. let code: string = '',
  62. timesName: string = Utils.getRandomVariableName(),
  63. whileFunctionName: string = Utils.getRandomVariableName();
  64. if (this.options.selfDefending) {
  65. code = format(SelfDefendingTemplate(), {
  66. timesName,
  67. whileFunctionName
  68. });
  69. } else {
  70. code = `${whileFunctionName}(++${timesName})`;
  71. }
  72. return JavaScriptObfuscator.obfuscate(
  73. format(StringArrayRotateFunctionTemplate(), {
  74. code,
  75. timesName,
  76. stringArrayName: this.stringArrayName,
  77. stringArrayRotateValue: Utils.decToHex(this.stringArrayRotateValue),
  78. whileFunctionName
  79. }),
  80. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  81. seed: this.options.seed
  82. })
  83. ).getObfuscatedCode();
  84. }
  85. }