StringArrayRotateFunctionNode.ts 3.0 KB

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