UnicodeArrayRotateFunctionNode.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. import { INode } from "../../interfaces/nodes/INode";
  2. import { BlockScopeNode } from "../../types/BlockScopeNode";
  3. import { AppendState } from "../../enums/AppendState";
  4. import { NodeType } from "../../enums/NodeType";
  5. import { Node } from '../Node';
  6. import { NodeUtils } from "../../NodeUtils";
  7. import { Utils } from '../../Utils';
  8. export class UnicodeArrayRotateFunctionNode extends Node {
  9. /**
  10. * @type {AppendState}
  11. */
  12. protected appendState: AppendState = AppendState.AfterObfuscation;
  13. /**
  14. * @type {string[]}
  15. */
  16. private unicodeArray: string[];
  17. /**
  18. * @type {string}
  19. */
  20. private unicodeArrayName: string;
  21. /**
  22. * @param {number}
  23. */
  24. private unicodeArrayRotateValue: number;
  25. /**
  26. * @param unicodeArrayName
  27. * @param unicodeArray
  28. * @param unicodeArrayRotateValue
  29. */
  30. constructor (
  31. unicodeArrayName: string,
  32. unicodeArray: string[],
  33. unicodeArrayRotateValue
  34. ) {
  35. super();
  36. this.unicodeArrayName = unicodeArrayName;
  37. this.unicodeArray = unicodeArray;
  38. this.unicodeArrayRotateValue = unicodeArrayRotateValue;
  39. this.node = this.getNodeStructure();
  40. }
  41. /**
  42. * @param blockScopeNode
  43. */
  44. public appendNode (blockScopeNode: BlockScopeNode): void {
  45. NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), 1);
  46. }
  47. /**
  48. * @returns {INode}
  49. */
  50. public getNode (): INode {
  51. if (!this.unicodeArray.length) {
  52. return;
  53. }
  54. return super.getNode();
  55. }
  56. /**
  57. * @returns any
  58. */
  59. protected getNodeStructure (): any {
  60. return {
  61. "type": NodeType.ExpressionStatement,
  62. "expression": {
  63. "type": NodeType.CallExpression,
  64. "callee": {
  65. 'type': NodeType.FunctionExpression,
  66. 'id': null,
  67. 'params': [
  68. {
  69. 'type': NodeType.Identifier,
  70. 'name': 'array'
  71. },
  72. {
  73. 'type': NodeType.Identifier,
  74. 'name': 'times'
  75. },
  76. {
  77. 'type': NodeType.Identifier,
  78. 'name': 'reverse'
  79. }
  80. ],
  81. 'defaults': [],
  82. 'body': {
  83. 'type': NodeType.BlockStatement,
  84. 'body': [
  85. {
  86. 'type': NodeType.IfStatement,
  87. 'test': {
  88. 'type': NodeType.BinaryExpression,
  89. 'operator': '<',
  90. 'left': {
  91. 'type': NodeType.Identifier,
  92. 'name': 'times'
  93. },
  94. 'right': {
  95. 'type': NodeType.Literal,
  96. 'value': 0,
  97. 'raw': '0'
  98. }
  99. },
  100. 'consequent': {
  101. 'type': NodeType.BlockStatement,
  102. 'body': [
  103. {
  104. 'type': NodeType.ReturnStatement,
  105. 'argument': null
  106. }
  107. ]
  108. },
  109. 'alternate': null
  110. },
  111. {
  112. 'type': NodeType.ExpressionStatement,
  113. 'expression': {
  114. 'type': NodeType.AssignmentExpression,
  115. 'operator': '=',
  116. 'left': {
  117. 'type': NodeType.Identifier,
  118. 'name': 'reverse'
  119. },
  120. 'right': {
  121. 'type': NodeType.LogicalExpression,
  122. 'operator': '||',
  123. 'left': {
  124. 'type': NodeType.Identifier,
  125. 'name': 'reverse'
  126. },
  127. 'right': {
  128. 'type': NodeType.Literal,
  129. 'value': false,
  130. 'raw': 'false'
  131. }
  132. }
  133. }
  134. },
  135. {
  136. 'type': NodeType.VariableDeclaration,
  137. 'declarations': [
  138. {
  139. 'type': NodeType.VariableDeclarator,
  140. 'id': {
  141. 'type': NodeType.Identifier,
  142. 'name': 'temp'
  143. },
  144. 'init': null
  145. }
  146. ],
  147. 'kind': 'var'
  148. },
  149. {
  150. 'type': NodeType.WhileStatement,
  151. 'test': {
  152. 'type': NodeType.UpdateExpression,
  153. 'operator': '--',
  154. 'argument': {
  155. 'type': NodeType.Identifier,
  156. 'name': 'times'
  157. },
  158. 'prefix': false
  159. },
  160. 'body': {
  161. 'type': NodeType.BlockStatement,
  162. 'body': [
  163. {
  164. 'type': NodeType.IfStatement,
  165. 'test': {
  166. 'type': NodeType.UnaryExpression,
  167. 'operator': '!',
  168. 'argument': {
  169. 'type': NodeType.Identifier,
  170. 'name': 'reverse'
  171. },
  172. 'prefix': true
  173. },
  174. 'consequent': {
  175. 'type': NodeType.BlockStatement,
  176. 'body': [
  177. {
  178. 'type': NodeType.ExpressionStatement,
  179. 'expression': {
  180. 'type': NodeType.AssignmentExpression,
  181. 'operator': '=',
  182. 'left': {
  183. 'type': NodeType.Identifier,
  184. 'name': 'temp'
  185. },
  186. 'right': {
  187. 'type': NodeType.CallExpression,
  188. 'callee': {
  189. 'type': NodeType.MemberExpression,
  190. 'computed': true,
  191. 'object': {
  192. 'type': NodeType.Identifier,
  193. 'name': 'array'
  194. },
  195. 'property': {
  196. 'type': NodeType.Literal,
  197. 'name': 'pop',
  198. 'x-verbatim-property': Utils.stringToUnicode('pop')
  199. }
  200. },
  201. 'arguments': []
  202. }
  203. }
  204. },
  205. {
  206. 'type': NodeType.ExpressionStatement,
  207. 'expression': {
  208. 'type': NodeType.CallExpression,
  209. 'callee': {
  210. 'type': NodeType.MemberExpression,
  211. 'computed': true,
  212. 'object': {
  213. 'type': NodeType.Identifier,
  214. 'name': 'array'
  215. },
  216. 'property': {
  217. 'type': NodeType.Literal,
  218. 'name': 'unshift',
  219. 'x-verbatim-property': Utils.stringToUnicode('unshift')
  220. }
  221. },
  222. 'arguments': [
  223. {
  224. 'type': NodeType.Identifier,
  225. 'name': 'temp'
  226. }
  227. ]
  228. }
  229. }
  230. ]
  231. },
  232. 'alternate': {
  233. 'type': NodeType.BlockStatement,
  234. 'body': [
  235. {
  236. 'type': NodeType.ExpressionStatement,
  237. 'expression': {
  238. 'type': NodeType.AssignmentExpression,
  239. 'operator': '=',
  240. 'left': {
  241. 'type': NodeType.Identifier,
  242. 'name': 'temp'
  243. },
  244. 'right': {
  245. 'type': NodeType.CallExpression,
  246. 'callee': {
  247. 'type': NodeType.MemberExpression,
  248. 'computed': true,
  249. 'object': {
  250. 'type': NodeType.Identifier,
  251. 'name': 'array'
  252. },
  253. 'property': {
  254. 'type': NodeType.Literal,
  255. 'name': 'shift',
  256. 'x-verbatim-property': Utils.stringToUnicode('shift')
  257. }
  258. },
  259. 'arguments': []
  260. }
  261. }
  262. },
  263. {
  264. 'type': NodeType.ExpressionStatement,
  265. 'expression': {
  266. 'type': NodeType.CallExpression,
  267. 'callee': {
  268. 'type': NodeType.MemberExpression,
  269. 'computed': true,
  270. 'object': {
  271. 'type': NodeType.Identifier,
  272. 'name': 'array'
  273. },
  274. 'property': {
  275. 'type': NodeType.Literal,
  276. 'name': 'push',
  277. 'x-verbatim-property': Utils.stringToUnicode('push')
  278. }
  279. },
  280. 'arguments': [
  281. {
  282. 'type': NodeType.Identifier,
  283. 'name': 'temp'
  284. }
  285. ]
  286. }
  287. }
  288. ]
  289. }
  290. }
  291. ]
  292. }
  293. }
  294. ]
  295. },
  296. 'generator': false,
  297. 'expression': false
  298. },
  299. "arguments": [
  300. {
  301. 'type': NodeType.Identifier,
  302. 'name': this.unicodeArrayName
  303. },
  304. {
  305. 'type': NodeType.Literal,
  306. 'value': this.unicodeArrayRotateValue,
  307. 'raw': `'${this.unicodeArrayRotateValue}'`
  308. },
  309. {
  310. 'type': NodeType.Literal,
  311. 'value': true,
  312. 'raw': 'true'
  313. }
  314. ]
  315. }
  316. };
  317. }
  318. }