-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacker.js
389 lines (364 loc) · 9.13 KB
/
packer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
function ICommon(that) {
if (that != null) {
that.inherit = Common.prototype.inherit;
that.specialize = Common.prototype.specialize
}
return that
};
ICommon.specialize = function(p, c) {
if (!p) p = {};
if (!c) c = p.constructor;
if (c == {}.constructor) c = new Function("this.inherit()");
c.valueOf = new Function("return this");
c.valueOf.prototype = new this.valueOf;
c.valueOf.prototype.specialize(p);
c.prototype = new c.valueOf;
c.valueOf.prototype.constructor = c.prototype.constructor = c;
c.ancestor = this;
c.specialize = arguments.callee;
c.ancestorOf = this.ancestorOf;
return c
};
ICommon.valueOf = new Function("return this");
ICommon.valueOf.prototype = {
constructor: ICommon,
inherit: function() {
return arguments.callee.caller.ancestor.apply(this, arguments)
},
specialize: function(that) {
if (this == this.constructor.prototype && this.constructor.specialize) {
return this.constructor.valueOf.prototype.specialize(that)
}
for (var i in that) {
switch (i) {
case "constructor":
case "toString":
case "valueOf":
continue
}
if (typeof that[i] == "function" && that[i] != this[i]) {
that[i].ancestor = this[i]
}
this[i] = that[i]
}
if (that.toString != this.toString && that.toString != {}.toString) {
that.toString.ancestor = this.toString;
this.toString = that.toString
}
return this
}
};
function Common() {};
this.Common = ICommon.specialize({
constructor: Common,
toString: function() {
return "[common " + (this.constructor.className || "Object") + "]"
},
instanceOf: function(klass) {
return this.constructor == klass || klass.ancestorOf(this.constructor)
}
});
Common.className = "Common";
Common.ancestor = null;
Common.ancestorOf = function(klass) {
while (klass && klass.ancestor != this) klass = klass.ancestor;
return Boolean(klass)
};
Common.valueOf.ancestor = ICommon;
function ParseMaster() {
var E = 0,
R = 1,
L = 2;
var G = /\(/g,
S = /\$\d/,
I = /^\$\d+$/,
T = /(['"])\1\+(.*)\+\1\1$/,
ES = /\\./g,
Q = /'/,
DE = /\x01[^\x01]*\x01/g;
var self = this;
this.add = function(e, r) {
if (!r) r = "";
var l = (_14(String(e)).match(G) || "").length + 1;
if (S.test(r)) {
if (I.test(r)) {
r = parseInt(r.slice(1)) - 1
} else {
var i = l;
var q = Q.test(_14(r)) ? '"' : "'";
while (i) r = r.split("$" + i--).join(q + "+a[o+" + i + "]+" + q);
r = new Function("a,o", "return" + q + r.replace(T, "$1") + q)
}
}
_31(e || "/^$/", r, l)
};
this.exec = function(s) {
_3.length = 0;
return _28(_5(s, this.escapeChar).replace(new RegExp(_1, this.ignoreCase ? "gi" : "g"), _29), this.escapeChar).replace(DE, "")
};
this.reset = function() {
_1.length = 0
};
var _3 = [];
var _1 = [];
var _30 = function() {
return "(" + String(this[E]).slice(1, -1) + ")"
};
_1.toString = function() {
return this.join("|")
};
function _31() {
arguments.toString = _30;
_1[_1.length] = arguments
}
function _29() {
if (!arguments[0]) return "";
var i = 1,
j = 0,
p;
while (p = _1[j++]) {
if (arguments[i]) {
var r = p[R];
switch (typeof r) {
case "function":
return r(arguments, i);
case "number":
return arguments[r + i]
}
var d = (arguments[i].indexOf(self.escapeChar) == -1) ? "" : "\x01" + arguments[i] + "\x01";
return d + r
} else i += p[L]
}
};
function _5(s, e) {
return e ? s.replace(new RegExp("\\" + e + "(.)", "g"), function(m, c) {
_3[_3.length] = c;
return e
}) : s
};
function _28(s, e) {
var i = 0;
return e ? s.replace(new RegExp("\\" + e, "g"), function() {
return e + (_3[i++] || "")
}) : s
};
function _14(s) {
return s.replace(ES, "")
}
};
ParseMaster.prototype = {
constructor: ParseMaster,
ignoreCase: false,
escapeChar: ""
};
function pack(_7, _0, _2, _8) {
var I = "$1";
_7 += "\n";
_0 = Math.min(parseInt(_0), 95);
function _15(s) {
var i, p;
for (i = 0;
(p = _6[i]); i++) {
s = p(s)
}
return s
};
var _25 = function(p, a, c, k, e, d) {
while (c--)
if (k[c]) p = p.replace(new RegExp('\\b' + e(c) + '\\b', 'g'), k[c]);
return p
};
var _26 = function() {
if (!''.replace(/^/, String)) {
while (c--) d[e(c)] = k[c] || e(c);
k = [
function(e) {
return d[e]
}
];
e = function() {
return '\\w+'
};
c = 1
}
};
var _6 = [];
function _4(p) {
_6[_6.length] = p
};
function _18(s) {
var p = new ParseMaster;
p.escapeChar = "\\";
p.add(/'[^'\n\r]*'/, I);
p.add(/"[^"\n\r]*"/, I);
p.add(/\/\/[^\n\r]*[\n\r]/, " ");
p.add(/\/\*[^*]*\*+([^\/][^*]*\*+)*\//, " ");
p.add(/\s+(\/[^\/\n\r\*][^\/\n\r]*\/g?i?)/, "$2");
p.add(/[^\w\x24\/'"*)\?:]\/[^\/\n\r\*][^\/\n\r]*\/g?i?/, I);
if (_8) p.add(/;;;[^\n\r]+[\n\r]/);
p.add(/\(;;\)/, I);
p.add(/;+\s*([};])/, "$2");
s = p.exec(s);
p.add(/(\b|\x24)\s+(\b|\x24)/, "$2 $3");
p.add(/([+\-])\s+([+\-])/, "$2 $3");
p.add(/\s+/, "");
return p.exec(s)
};
function _17(s) {
var p = new ParseMaster;
p.add(/((\x24+)([a-zA-Z_]+))(\d*)/, function(m, o) {
var l = m[o + 2].length;
var s = l - Math.max(l - m[o + 3].length, 0);
return m[o + 1].substr(s, l) + m[o + 4]
});
var r = /\b_[A-Za-z\d]\w*/;
var k = _13(s, _9(r), _21);
var e = k.e;
p.add(r, function(m, o) {
return e[m[o]]
});
return p.exec(s)
};
function _16(s) {
if (_0 > 62) s = _20(s);
var p = new ParseMaster;
var e = _12(_0);
var r = (_0 > 62) ? /\w\w+/ : /\w+/;
k = _13(s, _9(r), e);
var e = k.e;
p.add(r, function(m, o) {
return e[m[o]]
});
return s && _27(p.exec(s), k)
};
function _13(s, r, e) {
var a = s.match(r);
var so = [];
var en = {};
var pr = {};
if (a) {
var u = [];
var p = {};
var v = {};
var c = {};
var i = a.length,
j = 0,
w;
do {
w = "$" + a[--i];
if (!c[w]) {
c[w] = 0;
u[j] = w;
p["$" + (v[j] = e(j))] = j++
}
c[w]++
} while (i);
i = u.length;
do {
w = u[--i];
if (p[w] != null) {
so[p[w]] = w.slice(1);
pr[p[w]] = true;
c[w] = 0
}
} while (i);
u.sort(function(m1, m2) {
return c[m2] - c[m1]
});
j = 0;
do {
if (so[i] == null) so[i] = u[j++].slice(1);
en[so[i]] = v[i]
} while (++i < u.length)
}
return {
s: so,
e: en,
p: pr
}
};
function _27(p, k) {
var E = _10("e\\(c\\)", "g");
p = "'" + _5(p) + "'";
var a = Math.min(k.s.length, _0) || 1;
var c = k.s.length;
for (var i in k.p) k.s[i] = "";
k = "'" + k.s.join("|") + "'.split('|')";
var e = _0 > 62 ? _11 : _12(a);
e = String(e).replace(/_0/g, "a").replace(/arguments\.callee/g, "e");
var i = "c" + (a > 10 ? ".toString(a)" : "");
if (_2) {
var d = _19(_26);
if (_0 > 62) d = d.replace(/\\\\w/g, "[\\xa1-\\xff]");
else if (a < 36) d = d.replace(E, i);
if (!c) d = d.replace(_10("(c)\\s*=\\s*1"), "$1=0")
}
var u = String(_25);
if (_2) {
u = u.replace(/\{/, "{" + d + ";")
}
u = u.replace(/"/g, "'");
if (_0 > 62) {
u = u.replace(/'\\\\b'\s*\+|\+\s*'\\\\b'/g, "")
}
if (a > 36 || _0 > 62 || _2) {
u = u.replace(/\{/, "{e=" + e + ";")
} else {
u = u.replace(E, i)
}
u = pack(u, 0, false, true);
var p = [p, a, c, k];
if (_2) {
p = p.concat(0, "{}")
}
return "eval(" + u + "(" + p + "))\n"
};
function _12(a) {
return a > 10 ? a > 36 ? a > 62 ? _11 : _22 : _23 : _24
};
var _24 = function(c) {
return c
};
var _23 = function(c) {
return c.toString(36)
};
var _22 = function(c) {
return (c < _0 ? '' : arguments.callee(parseInt(c / _0))) + ((c = c % _0) > 35 ? String.fromCharCode(c + 29) : c.toString(36))
};
var _11 = function(c) {
return (c < _0 ? '' : arguments.callee(c / _0)) + String.fromCharCode(c % _0 + 161)
};
var _21 = function(c) {
return "_" + c
};
function _5(s) {
return s.replace(/([\\'])/g, "\\$1")
};
function _20(s) {
return s.replace(/[\xa1-\xff]/g, function(m) {
return "\\x" + m.charCodeAt(0).toString(16)
})
};
function _10(s, f) {
return new RegExp(s.replace(/\$/g, "\\$"), f)
};
function _19(f) {
with(String(f)) return slice(indexOf("{") + 1, lastIndexOf("}"))
};
function _9(r) {
return new RegExp(String(r).slice(1, -1), "g")
};
_4(_18);
if (_8) _4(_17);
if (_0) _4(_16);
return _15(_7)
};
var encoding = 62;
var fastDecode = true;
var specialChars = false;
module.exports.minifyScript = function(script) {
return pack(script, encoding, fastDecode, specialChars);
};
module.exports.decodeScript = function(decodedScript) {
return eval("String" + decodedScript.slice(4));
}