js-confuser 1.7.2 → 2.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (263) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +6 -4
  2. package/.github/workflows/node.js.yml +1 -1
  3. package/CHANGELOG.md +105 -0
  4. package/Migration.md +57 -0
  5. package/README.md +23 -913
  6. package/dist/constants.js +69 -13
  7. package/dist/index.js +108 -152
  8. package/dist/obfuscator.js +316 -118
  9. package/dist/options.js +1 -109
  10. package/dist/order.js +30 -30
  11. package/dist/presets.js +47 -45
  12. package/dist/probability.js +25 -32
  13. package/dist/templates/bufferToStringTemplate.js +9 -0
  14. package/dist/templates/deadCodeTemplates.js +9 -0
  15. package/dist/templates/getGlobalTemplate.js +19 -0
  16. package/dist/templates/integrityTemplate.js +30 -0
  17. package/dist/templates/setFunctionLengthTemplate.js +9 -0
  18. package/dist/templates/stringCompressionTemplate.js +10 -0
  19. package/dist/templates/tamperProtectionTemplates.js +21 -0
  20. package/dist/templates/template.js +213 -93
  21. package/dist/transforms/astScrambler.js +100 -0
  22. package/dist/transforms/calculator.js +70 -127
  23. package/dist/transforms/controlFlowFlattening.js +1182 -0
  24. package/dist/transforms/deadCode.js +62 -577
  25. package/dist/transforms/dispatcher.js +300 -309
  26. package/dist/transforms/extraction/duplicateLiteralsRemoval.js +88 -189
  27. package/dist/transforms/extraction/objectExtraction.js +131 -215
  28. package/dist/transforms/finalizer.js +56 -59
  29. package/dist/transforms/flatten.js +275 -276
  30. package/dist/transforms/functionOutlining.js +230 -0
  31. package/dist/transforms/identifier/globalConcealing.js +217 -103
  32. package/dist/transforms/identifier/movedDeclarations.js +167 -91
  33. package/dist/transforms/identifier/renameVariables.js +240 -187
  34. package/dist/transforms/lock/integrity.js +61 -184
  35. package/dist/transforms/lock/lock.js +263 -303
  36. package/dist/transforms/minify.js +431 -436
  37. package/dist/transforms/opaquePredicates.js +65 -118
  38. package/dist/transforms/pack.js +160 -0
  39. package/dist/transforms/plugin.js +179 -0
  40. package/dist/transforms/preparation.js +263 -163
  41. package/dist/transforms/renameLabels.js +132 -56
  42. package/dist/transforms/rgf.js +142 -240
  43. package/dist/transforms/shuffle.js +52 -145
  44. package/dist/transforms/string/encoding.js +45 -173
  45. package/dist/transforms/string/stringCompression.js +81 -126
  46. package/dist/transforms/string/stringConcealing.js +189 -224
  47. package/dist/transforms/string/stringEncoding.js +32 -40
  48. package/dist/transforms/string/stringSplitting.js +54 -55
  49. package/dist/transforms/variableMasking.js +232 -0
  50. package/dist/utils/ControlObject.js +125 -0
  51. package/dist/utils/IntGen.js +46 -0
  52. package/dist/utils/NameGen.js +106 -0
  53. package/dist/utils/ast-utils.js +560 -0
  54. package/dist/utils/function-utils.js +56 -0
  55. package/dist/utils/gen-utils.js +48 -0
  56. package/dist/utils/node.js +77 -0
  57. package/dist/utils/object-utils.js +21 -0
  58. package/dist/utils/random-utils.js +91 -0
  59. package/dist/utils/static-utils.js +64 -0
  60. package/dist/validateOptions.js +122 -0
  61. package/index.d.ts +1 -17
  62. package/package.json +27 -22
  63. package/src/constants.ts +139 -77
  64. package/src/index.ts +70 -163
  65. package/src/obfuscationResult.ts +43 -0
  66. package/src/obfuscator.ts +328 -135
  67. package/src/options.ts +154 -623
  68. package/src/order.ts +14 -14
  69. package/src/presets.ts +39 -34
  70. package/src/probability.ts +21 -36
  71. package/src/templates/{bufferToString.ts → bufferToStringTemplate.ts} +5 -54
  72. package/src/templates/deadCodeTemplates.ts +1185 -0
  73. package/src/templates/getGlobalTemplate.ts +72 -0
  74. package/src/templates/integrityTemplate.ts +69 -0
  75. package/src/templates/setFunctionLengthTemplate.ts +11 -0
  76. package/src/templates/stringCompressionTemplate.ts +42 -0
  77. package/src/templates/tamperProtectionTemplates.ts +116 -0
  78. package/src/templates/template.ts +183 -92
  79. package/src/transforms/astScrambler.ts +99 -0
  80. package/src/transforms/calculator.ts +96 -224
  81. package/src/transforms/controlFlowFlattening.ts +1594 -0
  82. package/src/transforms/deadCode.ts +85 -628
  83. package/src/transforms/dispatcher.ts +431 -636
  84. package/src/transforms/extraction/duplicateLiteralsRemoval.ts +147 -299
  85. package/src/transforms/extraction/objectExtraction.ts +160 -333
  86. package/src/transforms/finalizer.ts +63 -64
  87. package/src/transforms/flatten.ts +439 -557
  88. package/src/transforms/functionOutlining.ts +225 -0
  89. package/src/transforms/identifier/globalConcealing.ts +261 -189
  90. package/src/transforms/identifier/movedDeclarations.ts +228 -142
  91. package/src/transforms/identifier/renameVariables.ts +252 -258
  92. package/src/transforms/lock/integrity.ts +84 -260
  93. package/src/transforms/lock/lock.ts +342 -491
  94. package/src/transforms/minify.ts +523 -663
  95. package/src/transforms/opaquePredicates.ts +90 -229
  96. package/src/transforms/pack.ts +195 -0
  97. package/src/transforms/plugin.ts +185 -0
  98. package/src/transforms/preparation.ts +337 -215
  99. package/src/transforms/renameLabels.ts +176 -77
  100. package/src/transforms/rgf.ts +293 -386
  101. package/src/transforms/shuffle.ts +80 -254
  102. package/src/transforms/string/encoding.ts +26 -129
  103. package/src/transforms/string/stringCompression.ts +118 -236
  104. package/src/transforms/string/stringConcealing.ts +255 -339
  105. package/src/transforms/string/stringEncoding.ts +28 -47
  106. package/src/transforms/string/stringSplitting.ts +61 -75
  107. package/src/transforms/variableMasking.ts +257 -0
  108. package/src/utils/ControlObject.ts +141 -0
  109. package/src/utils/IntGen.ts +33 -0
  110. package/src/utils/NameGen.ts +106 -0
  111. package/src/utils/ast-utils.ts +667 -0
  112. package/src/utils/function-utils.ts +50 -0
  113. package/src/utils/gen-utils.ts +48 -0
  114. package/src/utils/node.ts +78 -0
  115. package/src/utils/object-utils.ts +21 -0
  116. package/src/utils/random-utils.ts +79 -0
  117. package/src/utils/static-utils.ts +66 -0
  118. package/src/validateOptions.ts +256 -0
  119. package/tsconfig.json +13 -8
  120. package/babel.config.js +0 -12
  121. package/dev.js +0 -8
  122. package/dist/compiler.js +0 -34
  123. package/dist/parser.js +0 -59
  124. package/dist/precedence.js +0 -66
  125. package/dist/templates/bufferToString.js +0 -108
  126. package/dist/templates/crash.js +0 -59
  127. package/dist/templates/es5.js +0 -137
  128. package/dist/templates/functionLength.js +0 -34
  129. package/dist/templates/globals.js +0 -9
  130. package/dist/transforms/antiTooling.js +0 -88
  131. package/dist/transforms/controlFlowFlattening/controlFlowFlattening.js +0 -1281
  132. package/dist/transforms/controlFlowFlattening/expressionObfuscation.js +0 -131
  133. package/dist/transforms/es5/antiClass.js +0 -164
  134. package/dist/transforms/es5/antiDestructuring.js +0 -193
  135. package/dist/transforms/es5/antiES6Object.js +0 -185
  136. package/dist/transforms/es5/antiSpreadOperator.js +0 -35
  137. package/dist/transforms/es5/antiTemplate.js +0 -66
  138. package/dist/transforms/es5/es5.js +0 -123
  139. package/dist/transforms/extraction/classExtraction.js +0 -83
  140. package/dist/transforms/identifier/globalAnalysis.js +0 -70
  141. package/dist/transforms/identifier/variableAnalysis.js +0 -104
  142. package/dist/transforms/lock/antiDebug.js +0 -76
  143. package/dist/transforms/stack.js +0 -343
  144. package/dist/transforms/transform.js +0 -350
  145. package/dist/traverse.js +0 -110
  146. package/dist/util/compare.js +0 -145
  147. package/dist/util/gen.js +0 -564
  148. package/dist/util/guard.js +0 -9
  149. package/dist/util/identifiers.js +0 -355
  150. package/dist/util/insert.js +0 -362
  151. package/dist/util/math.js +0 -19
  152. package/dist/util/object.js +0 -40
  153. package/dist/util/random.js +0 -130
  154. package/dist/util/scope.js +0 -20
  155. package/docs/ControlFlowFlattening.md +0 -595
  156. package/docs/Countermeasures.md +0 -63
  157. package/docs/ES5.md +0 -197
  158. package/docs/Integrity.md +0 -75
  159. package/docs/RGF.md +0 -419
  160. package/samples/example.js +0 -15
  161. package/samples/high.js +0 -1
  162. package/samples/input.js +0 -3
  163. package/samples/javascriptobfuscator.com.js +0 -8
  164. package/samples/jscrambler_advanced.js +0 -1894
  165. package/samples/jscrambler_light.js +0 -1134
  166. package/samples/low.js +0 -1
  167. package/samples/medium.js +0 -1
  168. package/samples/obfuscator.io.js +0 -1686
  169. package/samples/preemptive.com.js +0 -16
  170. package/src/compiler.ts +0 -35
  171. package/src/parser.ts +0 -49
  172. package/src/precedence.ts +0 -61
  173. package/src/templates/crash.ts +0 -55
  174. package/src/templates/es5.ts +0 -131
  175. package/src/templates/functionLength.ts +0 -32
  176. package/src/templates/globals.ts +0 -3
  177. package/src/transforms/antiTooling.ts +0 -102
  178. package/src/transforms/controlFlowFlattening/controlFlowFlattening.ts +0 -2146
  179. package/src/transforms/controlFlowFlattening/expressionObfuscation.ts +0 -179
  180. package/src/transforms/es5/antiClass.ts +0 -272
  181. package/src/transforms/es5/antiDestructuring.ts +0 -294
  182. package/src/transforms/es5/antiES6Object.ts +0 -267
  183. package/src/transforms/es5/antiSpreadOperator.ts +0 -56
  184. package/src/transforms/es5/antiTemplate.ts +0 -98
  185. package/src/transforms/es5/es5.ts +0 -149
  186. package/src/transforms/extraction/classExtraction.ts +0 -168
  187. package/src/transforms/identifier/globalAnalysis.ts +0 -85
  188. package/src/transforms/identifier/variableAnalysis.ts +0 -118
  189. package/src/transforms/lock/antiDebug.ts +0 -112
  190. package/src/transforms/stack.ts +0 -551
  191. package/src/transforms/transform.ts +0 -453
  192. package/src/traverse.ts +0 -120
  193. package/src/types.ts +0 -131
  194. package/src/util/compare.ts +0 -181
  195. package/src/util/gen.ts +0 -651
  196. package/src/util/guard.ts +0 -7
  197. package/src/util/identifiers.ts +0 -494
  198. package/src/util/insert.ts +0 -419
  199. package/src/util/math.ts +0 -15
  200. package/src/util/object.ts +0 -39
  201. package/src/util/random.ts +0 -141
  202. package/src/util/scope.ts +0 -21
  203. package/test/code/Cash.src.js +0 -1011
  204. package/test/code/Cash.test.ts +0 -49
  205. package/test/code/Dynamic.src.js +0 -118
  206. package/test/code/Dynamic.test.ts +0 -49
  207. package/test/code/ES6.src.js +0 -235
  208. package/test/code/ES6.test.ts +0 -42
  209. package/test/code/NewFeatures.test.ts +0 -19
  210. package/test/code/StrictMode.src.js +0 -65
  211. package/test/code/StrictMode.test.js +0 -37
  212. package/test/compare.test.ts +0 -104
  213. package/test/index.test.ts +0 -249
  214. package/test/options.test.ts +0 -132
  215. package/test/presets.test.ts +0 -22
  216. package/test/probability.test.ts +0 -44
  217. package/test/templates/template.test.ts +0 -14
  218. package/test/transforms/antiTooling.test.ts +0 -52
  219. package/test/transforms/calculator.test.ts +0 -78
  220. package/test/transforms/controlFlowFlattening/controlFlowFlattening.test.ts +0 -1274
  221. package/test/transforms/controlFlowFlattening/expressionObfuscation.test.ts +0 -192
  222. package/test/transforms/deadCode.test.ts +0 -85
  223. package/test/transforms/dispatcher.test.ts +0 -457
  224. package/test/transforms/es5/antiClass.test.ts +0 -427
  225. package/test/transforms/es5/antiDestructuring.test.ts +0 -157
  226. package/test/transforms/es5/antiES6Object.test.ts +0 -245
  227. package/test/transforms/es5/antiTemplate.test.ts +0 -116
  228. package/test/transforms/es5/es5.test.ts +0 -110
  229. package/test/transforms/extraction/classExtraction.test.ts +0 -86
  230. package/test/transforms/extraction/duplicateLiteralsRemoval.test.ts +0 -200
  231. package/test/transforms/extraction/objectExtraction.test.ts +0 -491
  232. package/test/transforms/flatten.test.ts +0 -721
  233. package/test/transforms/hexadecimalNumbers.test.ts +0 -62
  234. package/test/transforms/identifier/globalConcealing.test.ts +0 -72
  235. package/test/transforms/identifier/movedDeclarations.test.ts +0 -275
  236. package/test/transforms/identifier/renameVariables.test.ts +0 -621
  237. package/test/transforms/lock/antiDebug.test.ts +0 -66
  238. package/test/transforms/lock/browserLock.test.ts +0 -129
  239. package/test/transforms/lock/countermeasures.test.ts +0 -100
  240. package/test/transforms/lock/integrity.test.ts +0 -161
  241. package/test/transforms/lock/lock.test.ts +0 -204
  242. package/test/transforms/lock/osLock.test.ts +0 -312
  243. package/test/transforms/lock/selfDefending.test.ts +0 -68
  244. package/test/transforms/minify.test.ts +0 -575
  245. package/test/transforms/opaquePredicates.test.ts +0 -43
  246. package/test/transforms/preparation.test.ts +0 -157
  247. package/test/transforms/renameLabels.test.ts +0 -95
  248. package/test/transforms/rgf.test.ts +0 -378
  249. package/test/transforms/shuffle.test.ts +0 -135
  250. package/test/transforms/stack.test.ts +0 -573
  251. package/test/transforms/string/stringCompression.test.ts +0 -120
  252. package/test/transforms/string/stringConcealing.test.ts +0 -299
  253. package/test/transforms/string/stringEncoding.test.ts +0 -95
  254. package/test/transforms/string/stringSplitting.test.ts +0 -135
  255. package/test/transforms/transform.test.ts +0 -66
  256. package/test/traverse.test.ts +0 -139
  257. package/test/util/compare.test.ts +0 -34
  258. package/test/util/gen.test.ts +0 -121
  259. package/test/util/identifiers.test.ts +0 -253
  260. package/test/util/insert.test.ts +0 -142
  261. package/test/util/math.test.ts +0 -5
  262. package/test/util/random.test.ts +0 -71
  263. /package/dist/{types.js → obfuscationResult.js} +0 -0
@@ -1,628 +1,85 @@
1
- import { ObfuscateOrder } from "../order";
2
- import { ComputeProbabilityMap } from "../probability";
3
- import Template from "../templates/template";
4
- import { isBlock } from "../traverse";
5
- import {
6
- Identifier,
7
- IfStatement,
8
- Literal,
9
- Node,
10
- VariableDeclaration,
11
- VariableDeclarator,
12
- } from "../util/gen";
13
- import { getBlockBody, isFunction, prepend } from "../util/insert";
14
- import { choice, getRandomInteger } from "../util/random";
15
- import Transform from "./transform";
16
-
17
- const templates = [
18
- Template(`
19
- function curCSS( elem, name, computed ) {
20
- var ret;
21
-
22
- computed = computed || getStyles( elem );
23
-
24
- if ( computed ) {
25
- ret = computed.getPropertyValue( name ) || computed[ name ];
26
-
27
- if ( ret === "" && !isAttached( elem ) ) {
28
- ret = redacted.style( elem, name );
29
- }
30
- }
31
-
32
- return ret !== undefined ?
33
-
34
- // Support: IE <=9 - 11+
35
- // IE returns zIndex value as an integer.
36
- ret + "" :
37
- ret;
38
- }`),
39
- Template(`
40
- function Example() {
41
- var state = redacted.useState(false);
42
- return x(
43
- ErrorBoundary,
44
- null,
45
- x(
46
- DisplayName,
47
- null,
48
- )
49
- );
50
- }`),
51
-
52
- Template(`
53
- const path = require('path');
54
- const { version } = require('../../package');
55
- const { version: dashboardPluginVersion } = require('@redacted/enterprise-plugin/package');
56
- const { version: componentsVersion } = require('@redacted/components/package');
57
- const { sdkVersion } = require('@redacted/enterprise-plugin');
58
- const isStandaloneExecutable = require('../utils/isStandaloneExecutable');
59
- const resolveLocalRedactedPath = require('./resolve-local-redacted-path');
60
-
61
- const redactedPath = path.resolve(__dirname, '../redacted.js');`),
62
-
63
- Template(`
64
- module.exports = async (resolveLocalRedactedPath = ()=>{throw new Error("No redacted path provided")}) => {
65
- const cliParams = new Set(process.argv.slice(2));
66
- if (!cliParams.has('--version')) {
67
- if (cliParams.size !== 1) return false;
68
- if (!cliParams.has('-v')) return false;
69
- }
70
-
71
- const installationModePostfix = await (async (isStandaloneExecutable, redactedPath) => {
72
- if (isStandaloneExecutable) return ' (standalone)';
73
- if (redactedPath === (await resolveLocalRedactedPath())) return ' (local)';
74
- return '';
75
- })();
76
-
77
- return true;
78
- };`),
79
- Template(`
80
- function setCookie(cname, cvalue, exdays) {
81
- var d = new Date();
82
- d.setTime(d.getTime() + (exdays*24*60*60*1000));
83
- var expires = "expires="+ d.toUTCString();
84
- document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
85
- }`),
86
-
87
- Template(`function getCookie(cname) {
88
- var name = cname + "=";
89
- var decodedCookie = decodeURIComponent(document.cookie);
90
- var ca = decodedCookie.split(';');
91
- for(var i = 0; i <ca.length; i++) {
92
- var c = ca[i];
93
- while (c.charAt(0) == ' ') {
94
- c = c.substring(1);
95
- }
96
- if (c.indexOf(name) == 0) {
97
- return c.substring(name.length, c.length);
98
- }
99
- }
100
- return "";
101
- }`),
102
-
103
- Template(`function getLocalStorageValue(key, cb){
104
- if ( typeof key !== "string" ) {
105
- throw new Error("Invalid data key provided (not type string)")
106
- }
107
- if ( !key ) {
108
- throw new Error("Invalid data key provided (empty string)")
109
- }
110
- var value = window.localStorage.getItem(key)
111
- try {
112
- value = JSON.parse(value)
113
- } catch ( e ) {
114
- cb(new Error("Serialization error for data '" + key + "': " + e.message))
115
- }
116
-
117
- cb(null, value)
118
- }`),
119
- Template(`
120
-
121
- var __ = "(c=ak(<~F$VU'9f)~><&85dBPL-module/from";
122
- var s = "q:function(){var ad=ad=>b(ad-29);if(!T.r[(typeof ab==ad(123)?";
123
- var g = "return U[c[c[d(-199)]-b(205)]]||V[ae(b(166))];case T.o[c[c[c[d(-199)]+d(-174)]-(c[b(119)]-(c[d(-199)]-163))]+ae(b(146))](0)==b(167)?d(-130):-d(-144)";
124
-
125
- __.match(s + g);
126
- `),
127
- Template(`
128
- function vec_pack(vec) {
129
- return vec[1] * 67108864 + (vec[0] < 0 ? 33554432 | vec[0] : vec[0]);
130
- }
131
-
132
- function vec_unpack(number) {
133
- switch (((number & 33554432) !== 0) * 1 + (number < 0) * 2) {
134
- case 0:
135
- return [number % 33554432, Math.trunc(number / 67108864)];
136
- case 1:
137
- return [
138
- (number % 33554432) - 33554432,
139
- Math.trunc(number / 67108864) + 1,
140
- ];
141
- case 2:
142
- return [
143
- (((number + 33554432) % 33554432) + 33554432) % 33554432,
144
- Math.round(number / 67108864),
145
- ];
146
- case 3:
147
- return [number % 33554432, Math.trunc(number / 67108864)];
148
- }
149
- }
150
-
151
- let a = vec_pack([2, 4]);
152
- let b = vec_pack([1, 2]);
153
-
154
- let c = a + b; // Vector addition
155
- let d = c - b; // Vector subtraction
156
- let e = d * 2; // Scalar multiplication
157
- let f = e / 2; // Scalar division
158
-
159
- console.log(vec_unpack(c)); // [3, 6]
160
- console.log(vec_unpack(d)); // [2, 4]
161
- console.log(vec_unpack(e)); // [4, 8]
162
- console.log(vec_unpack(f)); // [2, 4]
163
- `),
164
- Template(`
165
- function buildCharacterMap(str) {
166
- const characterMap = {};
167
-
168
- for (let char of str.replace(/[^\w]/g, "").toLowerCase())
169
- characterMap[char] = characterMap[char] + 1 || 1;
170
-
171
- return characterMap;
172
- }
173
-
174
- function isAnagrams(stringA, stringB) {
175
- const stringAMap = buildCharMap(stringA);
176
- const stringBMap = buildCharMap(stringB);
177
-
178
- for (let char in stringAMap) {
179
- if (stringAMap[char] !== stringBMap[char]) {
180
- return false;
181
- }
182
- }
183
-
184
- if (Object.keys(stringAMap).length !== Object.keys(stringBMap).length) {
185
- return false;
186
- }
187
-
188
- return true;
189
- }
190
-
191
- function isBalanced(root) {
192
- const height = getHeightBalanced(root);
193
- return height !== Infinity;
194
- }
195
-
196
- function getHeightBalanced(node) {
197
- if (!node) {
198
- return -1;
199
- }
200
-
201
- const leftTreeHeight = getHeightBalanced(node.left);
202
- const rightTreeHeight = getHeightBalanced(node.right);
203
-
204
- const heightDiff = Math.abs(leftTreeHeight - rightTreeHeight);
205
-
206
- if (
207
- leftTreeHeight === Infinity ||
208
- rightTreeHeight === Infinity ||
209
- heightDiff > 1
210
- ) {
211
- return Infinity;
212
- }
213
-
214
- const currentHeight = Math.max(leftTreeHeight, rightTreeHeight) + 1;
215
- return currentHeight;
216
- }
217
-
218
- window["__GLOBAL__HELPERS__"] = {
219
- buildCharacterMap,
220
- isAnagrams,
221
- isBalanced,
222
- getHeightBalanced,
223
- };
224
- `),
225
- Template(`
226
- function ListNode(){}
227
- var addTwoNumbers = function(l1, l2) {
228
- var carry = 0;
229
- var sum = 0;
230
- var head = new ListNode(0);
231
- var now = head;
232
- var a = l1;
233
- var b = l2;
234
- while (a !== null || b !== null) {
235
- sum = (a ? a.val : 0) + (b ? b.val : 0) + carry;
236
- carry = Math.floor(sum / 10);
237
- now.next = new ListNode(sum % 10);
238
- now = now.next;
239
- a = a ? a.next : null;
240
- b = b ? b.next : null;
241
- }
242
- if (carry) now.next = new ListNode(carry);
243
- return head.next;
244
- };
245
-
246
- console.log(addTwoNumbers)
247
- `),
248
- Template(`
249
- var threeSum = function(nums) {
250
- var len = nums.length;
251
- var res = [];
252
- var l = 0;
253
- var r = 0;
254
- nums.sort((a, b) => (a - b));
255
- for (var i = 0; i < len; i++) {
256
- if (i > 0 && nums[i] === nums[i - 1]) continue;
257
- l = i + 1;
258
- r = len - 1;
259
- while (l < r) {
260
- if (nums[i] + nums[l] + nums[r] < 0) {
261
- l++;
262
- } else if (nums[i] + nums[l] + nums[r] > 0) {
263
- r--;
264
- } else {
265
- res.push([nums[i], nums[l], nums[r]]);
266
- while (l < r && nums[l] === nums[l + 1]) l++;
267
- while (l < r && nums[r] === nums[r - 1]) r--;
268
- l++;
269
- r--;
270
- }
271
- }
272
- }
273
- return res;
274
- };
275
- console.log(threeSum)
276
- `),
277
- Template(`
278
- var combinationSum2 = function(candidates, target) {
279
- var res = [];
280
- var len = candidates.length;
281
- candidates.sort((a, b) => (a - b));
282
- dfs(res, [], 0, len, candidates, target);
283
- return res;
284
- };
285
-
286
- var dfs = function (res, stack, index, len, candidates, target) {
287
- var tmp = null;
288
- if (target < 0) return;
289
- if (target === 0) return res.push(stack);
290
- for (var i = index; i < len; i++) {
291
- if (candidates[i] > target) break;
292
- if (i > index && candidates[i] === candidates[i - 1]) continue;
293
- tmp = Array.from(stack);
294
- tmp.push(candidates[i]);
295
- dfs(res, tmp, i + 1, len, candidates, target - candidates[i]);
296
- }
297
- };
298
-
299
- console.log(combinationSum2);
300
- `),
301
- Template(`
302
- var isScramble = function(s1, s2) {
303
- return helper({}, s1, s2);
304
- };
305
-
306
- var helper = function (dp, s1, s2) {
307
- var map = {};
308
-
309
- if (dp[s1 + s2] !== undefined) return dp[s1 + s2];
310
- if (s1 === s2) return true;
311
-
312
- for (var j = 0; j < s1.length; j++) {
313
- if (map[s1[j]] === undefined) map[s1[j]] = 0;
314
- if (map[s2[j]] === undefined) map[s2[j]] = 0;
315
- map[s1[j]]++;
316
- map[s2[j]]--;
317
- }
318
-
319
- for (var key in map) {
320
- if (map[key] !== 0) {
321
- dp[s1 + s2] = false;
322
- return false;
323
- }
324
- }
325
-
326
- for (var i = 1; i < s1.length; i++) {
327
- if ((helper(dp, s1.substr(0, i), s2.substr(0, i))
328
- && helper(dp, s1.substr(i), s2.substr(i))) ||
329
- (helper(dp, s1.substr(0, i), s2.substr(s2.length - i))
330
- && helper(dp, s1.substr(i), s2.substr(0, s2.length - i)))) {
331
- dp[s1 + s2] = true;
332
- return true;
333
- }
334
- }
335
-
336
- dp[s1 + s2] = false;
337
- return false;
338
- };
339
-
340
- console.log(isScramble);
341
- `),
342
- Template(`
343
- var candy = function(ratings) {
344
- var len = ratings.length;
345
- var res = [];
346
- var sum = 0;
347
- for (var i = 0; i < len; i++) {
348
- res.push((i !== 0 && ratings[i] > ratings[i - 1]) ? (res[i - 1] + 1) : 1);
349
- }
350
- for (var j = len - 1; j >= 0; j--) {
351
- if (j !== len - 1 && ratings[j] > ratings[j + 1]) res[j] = Math.max(res[j], res[j + 1] + 1);
352
- sum += res[j];
353
- }
354
- return sum;
355
- };
356
-
357
- console.log(candy)
358
- `),
359
- Template(`
360
- var maxPoints = function(points) {
361
- var max = 0;
362
- var map = {};
363
- var localMax = 0;
364
- var samePoint = 0;
365
- var k = 0;
366
- var len = points.length;
367
- for (var i = 0; i < len; i++) {
368
- map = {};
369
- localMax = 0;
370
- samePoint = 1;
371
- for (var j = i + 1; j < len; j++) {
372
- if (points[i].x === points[j].x && points[i].y === points[j].y) {
373
- samePoint++;
374
- continue;
375
- }
376
- if (points[i].y === points[j].y) k = Number.MAX_SAFE_INTEGER;
377
- else k = (points[i].x - points[j].x) / (points[i].y - points[j].y);
378
- if (!map[k]) map[k] = 0;
379
- map[k]++;
380
- localMax = Math.max(localMax, map[k]);
381
- }
382
- localMax += samePoint;
383
- max = Math.max(max, localMax);
384
- }
385
- return max;
386
- };
387
-
388
- console.log(maxPoints)
389
- `),
390
- Template(`
391
- var maximumGap = function(nums) {
392
- var len = nums.length;
393
- if (len < 2) return 0;
394
-
395
- var max = Math.max(...nums);
396
- var min = Math.min(...nums);
397
- if (max === min) return 0;
398
-
399
- var minBuckets = Array(len - 1).fill(Number.MAX_SAFE_INTEGER);
400
- var maxBuckets = Array(len - 1).fill(Number.MIN_SAFE_INTEGER);
401
- var gap = Math.ceil((max - min) / (len - 1));
402
- var index = 0;
403
- for (var i = 0; i < len; i++) {
404
- if (nums[i] === min || nums[i] === max) continue;
405
- index = Math.floor((nums[i] - min) / gap);
406
- minBuckets[index] = Math.min(minBuckets[index], nums[i]);
407
- maxBuckets[index] = Math.max(maxBuckets[index], nums[i]);
408
- }
409
-
410
- var maxGap = Number.MIN_SAFE_INTEGER;
411
- var preVal = min;
412
- for (var j = 0; j < len - 1; j++) {
413
- if (minBuckets[j] === Number.MAX_SAFE_INTEGER && maxBuckets[j] === Number.MIN_SAFE_INTEGER) continue;
414
- maxGap = Math.max(maxGap, minBuckets[j] - preVal);
415
- preVal = maxBuckets[j];
416
- }
417
- maxGap = Math.max(maxGap, max - preVal);
418
-
419
- return maxGap;
420
- };
421
-
422
- console.log(maximumGap);
423
- `),
424
- Template(`
425
- var LRUCache = function(capacity) {
426
- this.capacity = capacity;
427
- this.length = 0;
428
- this.map = {};
429
- this.head = null;
430
- this.tail = null;
431
- };
432
-
433
- LRUCache.prototype.get = function(key) {
434
- var node = this.map[key];
435
- if (node) {
436
- this.remove(node);
437
- this.insert(node.key, node.val);
438
- return node.val;
439
- } else {
440
- return -1;
441
- }
442
- };
443
-
444
- LRUCache.prototype.put = function(key, value) {
445
- if (this.map[key]) {
446
- this.remove(this.map[key]);
447
- this.insert(key, value);
448
- } else {
449
- if (this.length === this.capacity) {
450
- this.remove(this.head);
451
- this.insert(key, value);
452
- } else {
453
- this.insert(key, value);
454
- this.length++;
455
- }
456
- }
457
- };
458
-
459
- /**
460
- * Your LRUCache object will be instantiated and called as such:
461
- * var obj = Object.create(LRUCache).createNew(capacity)
462
- * var param_1 = obj.get(key)
463
- * obj.put(key,value)
464
- */
465
-
466
- LRUCache.prototype.remove = function (node) {
467
- var prev = node.prev;
468
- var next = node.next;
469
- if (next) next.prev = prev;
470
- if (prev) prev.next = next;
471
- if (this.head === node) this.head = next;
472
- if (this.tail === node) this.tail = prev;
473
- delete this.map[node.key];
474
- };
475
-
476
- LRUCache.prototype.insert = function (key, val) {
477
- var node = new List(key, val);
478
- if (!this.tail) {
479
- this.tail = node;
480
- this.head = node;
481
- } else {
482
- this.tail.next = node;
483
- node.prev = this.tail;
484
- this.tail = node;
485
- }
486
- this.map[key] = node;
487
- };
488
-
489
- console.log(LRUCache);
490
- `),
491
- Template(`
492
- var isInterleave = function(s1, s2, s3) {
493
- var dp = {};
494
- if (s3.length !== s1.length + s2.length) return false;
495
- return helper(s1, s2, s3, 0, 0, 0, dp);
496
- };
497
-
498
- var helper = function (s1, s2, s3, i, j, k, dp) {
499
- var res = false;
500
-
501
- if (k >= s3.length) return true;
502
- if (dp['' + i + j + k] !== undefined) return dp['' + i + j + k];
503
-
504
- if (s3[k] === s1[i] && s3[k] === s2[j]) {
505
- res = helper(s1, s2, s3, i + 1, j, k + 1, dp) || helper(s1, s2, s3, i, j + 1, k + 1, dp);
506
- } else if (s3[k] === s1[i]) {
507
- res = helper(s1, s2, s3, i + 1, j, k + 1, dp);
508
- } else if (s3[k] === s2[j]) {
509
- res = helper(s1, s2, s3, i, j + 1, k + 1, dp);
510
- }
511
-
512
- dp['' + i + j + k] = res;
513
-
514
- return res;
515
- };
516
-
517
- console.log(isInterleave);
518
- `),
519
- Template(`
520
- var solveNQueens = function(n) {
521
- var res = [];
522
- if (n === 1 || n >= 4) dfs(res, [], n, 0);
523
- return res;
524
- };
525
-
526
- var dfs = function (res, points, n, index) {
527
- for (var i = index; i < n; i++) {
528
- if (points.length !== i) return;
529
- for (var j = 0; j < n; j++) {
530
- if (isValid(points, [i, j])) {
531
- points.push([i, j]);
532
- dfs(res, points, n, i + 1);
533
- if (points.length === n) res.push(buildRes(points));
534
- points.pop();
535
- }
536
- }
537
- }
538
- };
539
-
540
- var buildRes = function (points) {
541
- var res = [];
542
- var n = points.length;
543
- for (var i = 0; i < n; i++) {
544
- res[i] = '';
545
- for (var j = 0; j < n; j++) {
546
- res[i] += (points[i][1] === j ? 'Q' : '.');
547
- }
548
- }
549
- return res;
550
- };
551
-
552
- var isValid = function (oldPoints, newPoint) {
553
- var len = oldPoints.length;
554
- for (var i = 0; i < len; i++) {
555
- if (oldPoints[i][0] === newPoint[0] || oldPoints[i][1] === newPoint[1]) return false;
556
- if (Math.abs((oldPoints[i][0] - newPoint[0]) / (oldPoints[i][1] - newPoint[1])) === 1) return false;
557
- }
558
- return true;
559
- };
560
-
561
- console.log(solveNQueens);
562
- `),
563
- ];
564
-
565
- /**
566
- * Adds dead code to blocks.
567
- *
568
- * - Adds fake predicates.
569
- * - Adds fake code from various samples.
570
- */
571
- export default class DeadCode extends Transform {
572
- made: number;
573
-
574
- constructor(o) {
575
- super(o, ObfuscateOrder.DeadCode);
576
-
577
- this.made = 0;
578
- }
579
-
580
- match(object: Node, parents: Node[]) {
581
- return (
582
- isFunction(object) &&
583
- isBlock(object.body) &&
584
- !parents.find((x) => x.$dispatcherSkip)
585
- );
586
- }
587
-
588
- transform(object: Node, parents: Node[]) {
589
- if (!ComputeProbabilityMap(this.options.deadCode)) {
590
- return;
591
- }
592
-
593
- // Hard-coded limit of 100 Dead Code insertions
594
- this.made++;
595
- if (this.made > 100) {
596
- return;
597
- }
598
-
599
- return () => {
600
- var body = getBlockBody(object);
601
-
602
- // Do not place code before Import statements or 'use strict' directives
603
- var safeOffset = 0;
604
- for (var node of body) {
605
- if (node.type === "ImportDeclaration" || node.directive) safeOffset++;
606
- else break;
607
- }
608
-
609
- var index = getRandomInteger(safeOffset, body.length);
610
-
611
- var name = this.getPlaceholder();
612
- var variableDeclaration = VariableDeclaration(
613
- VariableDeclarator(name, Literal(false))
614
- );
615
-
616
- var template;
617
- do {
618
- template = choice(templates);
619
- } while (this.options.es5 && template.source.includes("async"));
620
-
621
- var ifStatement = IfStatement(Identifier(name), template.compile(), null);
622
-
623
- body.splice(index, 0, ifStatement);
624
-
625
- prepend(object, variableDeclaration);
626
- };
627
- }
628
- }
1
+ import { PluginArg, PluginObject } from "./plugin";
2
+ import { chance, choice } from "../utils/random-utils";
3
+ import { deadCodeTemplates } from "../templates/deadCodeTemplates";
4
+ import { computeProbabilityMap } from "../probability";
5
+ import { Order } from "../order";
6
+ import * as t from "@babel/types";
7
+ import Template from "../templates/template";
8
+ import { NameGen } from "../utils/NameGen";
9
+ import { prepend } from "../utils/ast-utils";
10
+
11
+ export default ({ Plugin }: PluginArg): PluginObject => {
12
+ const me = Plugin(Order.DeadCode, {
13
+ changeData: {
14
+ deadCode: 0,
15
+ },
16
+ });
17
+ let created = 0;
18
+
19
+ return {
20
+ visitor: {
21
+ Block: {
22
+ exit(blockPath) {
23
+ if (blockPath.find((p) => me.isSkipped(p))) return;
24
+
25
+ if (!computeProbabilityMap(me.options.deadCode)) {
26
+ return;
27
+ }
28
+
29
+ if (typeof me.options.deadCode !== "function") {
30
+ let suggestedMax = 25;
31
+ if (me.obfuscator.parentObfuscator) {
32
+ // RGF should contain less dead code
33
+ suggestedMax = 5;
34
+ }
35
+
36
+ if (created > suggestedMax && chance(created - suggestedMax))
37
+ return;
38
+ created++;
39
+ }
40
+
41
+ var template = choice(deadCodeTemplates);
42
+ var nodes = template.compile();
43
+
44
+ var containingFnName = me.getPlaceholder("dead_" + created);
45
+
46
+ var newPath = blockPath.unshiftContainer(
47
+ "body",
48
+ t.functionDeclaration(
49
+ t.identifier(containingFnName),
50
+ [],
51
+ t.blockStatement([...nodes])
52
+ )
53
+ );
54
+
55
+ // Overcomplicated way to get a random property name that doesn't exist on the Function
56
+ var randomProperty: string;
57
+ var nameGen = new NameGen("randomized");
58
+
59
+ function PrototypeCollision() {}
60
+ PrototypeCollision(); // Call it for code coverage :D
61
+
62
+ do {
63
+ randomProperty = nameGen.generate();
64
+ } while (
65
+ !randomProperty ||
66
+ PrototypeCollision[randomProperty] !== undefined
67
+ );
68
+
69
+ me.changeData.deadCode++;
70
+
71
+ prepend(
72
+ blockPath,
73
+ new Template(`
74
+ if("${randomProperty}" in ${containingFnName}) {
75
+ ${containingFnName}()
76
+ }
77
+ `).single()
78
+ );
79
+
80
+ me.skip(blockPath);
81
+ },
82
+ },
83
+ },
84
+ };
85
+ };