UnicodeArrayRotateFunctionNode.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import * as esprima from 'esprima';
  2. import { INode } from "../../interfaces/nodes/INode";
  3. import { BlockScopeNode } from "../../types/BlockScopeNode";
  4. import { AppendState } from "../../enums/AppendState";
  5. import { Node } from '../Node';
  6. import { NodeUtils } from "../../NodeUtils";
  7. import {Utils} from "../../Utils";
  8. import {Obfuscator} from "../../Obfuscator";
  9. export class UnicodeArrayRotateFunctionNode extends Node {
  10. /**
  11. * @type {AppendState}
  12. */
  13. protected appendState: AppendState = AppendState.AfterObfuscation;
  14. /**
  15. * @type {string[]}
  16. */
  17. private unicodeArray: string[];
  18. /**
  19. * @type {string}
  20. */
  21. private unicodeArrayName: string;
  22. /**
  23. * @param {number}
  24. */
  25. private unicodeArrayRotateValue: number;
  26. /**
  27. * @param unicodeArrayName
  28. * @param unicodeArray
  29. * @param unicodeArrayRotateValue
  30. */
  31. constructor (
  32. unicodeArrayName: string,
  33. unicodeArray: string[],
  34. unicodeArrayRotateValue
  35. ) {
  36. super();
  37. this.unicodeArrayName = unicodeArrayName;
  38. this.unicodeArray = unicodeArray;
  39. this.unicodeArrayRotateValue = unicodeArrayRotateValue;
  40. this.node = this.getNodeStructure();
  41. }
  42. /**
  43. * @param blockScopeNode
  44. */
  45. public appendNode (blockScopeNode: BlockScopeNode): void {
  46. NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), 1);
  47. }
  48. /**
  49. * @returns {INode}
  50. */
  51. public getNode (): INode {
  52. if (!this.unicodeArray.length) {
  53. return;
  54. }
  55. return super.getNode();
  56. }
  57. /**
  58. * @returns any
  59. */
  60. protected getNodeStructure (): any {
  61. return NodeUtils.getBlockScopeNodeByIndex(
  62. esprima.parse(`
  63. (function (array, times, reverse) {
  64. if (times < 0) {
  65. return;
  66. }
  67. var temp;
  68. while (times--) {
  69. if (!reverse) {
  70. temp = array.pop();
  71. array.unshift(temp);
  72. } else {
  73. temp = array.shift();
  74. array.push(temp);
  75. }
  76. }
  77. })(${this.unicodeArrayName}, ${this.unicodeArrayRotateValue}, true);
  78. `)
  79. );
  80. /*return new Obfuscator({
  81. rotateUnicodeArray: false
  82. }).obfuscateNode(
  83. NodeUtils.getBlockScopeNodeByIndex(
  84. esprima.parse(`
  85. (function (array, times, reverse) {
  86. if (times < 0) {
  87. return;
  88. }
  89. var temp;
  90. while (times--) {
  91. if (!reverse) {
  92. temp = array.pop();
  93. array.unshift(temp);
  94. } else {
  95. temp = array.shift();
  96. array.push(temp);
  97. }
  98. }
  99. })(${this.unicodeArrayName}, ${this.unicodeArrayRotateValue}, true);
  100. `)
  101. )
  102. );*/
  103. }
  104. }