corrade-nucleus-nucleons – Blame information for rev 20

Subversion Repositories:
Rev:
Rev Author Line No. Line
20 office 1 //
2 // Unpacker for Dean Edward's p.a.c.k.e.r, a part of javascript beautifier
3 // written by Einar Lielmanis <einar@jsbeautifier.org>
4 //
5 // Coincidentally, it can defeat a couple of other eval-based compressors.
6 //
7 // usage:
8 //
9 // if (P_A_C_K_E_R.detect(some_string)) {
10 // var unpacked = P_A_C_K_E_R.unpack(some_string);
11 // }
12 //
13 //
14  
15 var P_A_C_K_E_R = {
16 detect: function(str) {
17 return (P_A_C_K_E_R.get_chunks(str).length > 0);
18 },
19  
20 get_chunks: function(str) {
21 var chunks = str.match(/eval\(\(?function\(.*?(,0,\{\}\)\)|split\('\|'\)\)\))($|\n)/g);
22 return chunks ? chunks : [];
23 },
24  
25 unpack: function(str) {
26 var chunks = P_A_C_K_E_R.get_chunks(str),
27 chunk;
28 for (var i = 0; i < chunks.length; i++) {
29 chunk = chunks[i].replace(/\n$/, '');
30 str = str.split(chunk).join(P_A_C_K_E_R.unpack_chunk(chunk));
31 }
32 return str;
33 },
34  
35 unpack_chunk: function(str) {
36 var unpacked_source = '';
37 var __eval = eval;
38 if (P_A_C_K_E_R.detect(str)) {
39 try {
40 eval = function(s) { // jshint ignore:line
41 unpacked_source += s;
42 return unpacked_source;
43 }; // jshint ignore:line
44 __eval(str);
45 if (typeof unpacked_source === 'string' && unpacked_source) {
46 str = unpacked_source;
47 }
48 } catch (e) {
49 // well, it failed. we'll just return the original, instead of crashing on user.
50 }
51 }
52 eval = __eval; // jshint ignore:line
53 return str;
54 },
55  
56 run_tests: function(sanity_test) {
57 var t = sanity_test || new SanityTest();
58  
59 var pk1 = "eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('0 2=1',3,3,'var||a'.split('|'),0,{}))";
60 var unpk1 = 'var a=1';
61 var pk2 = "eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('0 2=1',3,3,'foo||b'.split('|'),0,{}))";
62 var unpk2 = 'foo b=1';
63 var pk_broken = "eval(function(p,a,c,k,e,r){BORKBORK;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('0 2=1',3,3,'var||a'.split('|'),0,{}))";
64 var pk3 = "eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('0 2=1{}))',3,3,'var||a'.split('|'),0,{}))";
65 var unpk3 = 'var a=1{}))';
66  
67 t.test_function(P_A_C_K_E_R.detect, "P_A_C_K_E_R.detect");
68 t.expect('', false);
69 t.expect('var a = b', false);
70 t.test_function(P_A_C_K_E_R.unpack, "P_A_C_K_E_R.unpack");
71 t.expect(pk_broken, pk_broken);
72 t.expect(pk1, unpk1);
73 t.expect(pk2, unpk2);
74 t.expect(pk3, unpk3);
75  
76 var filler = '\nfiller\n';
77 t.expect(filler + pk1 + "\n" + pk_broken + filler + pk2 + filler, filler + unpk1 + "\n" + pk_broken + filler + unpk2 + filler);
78  
79 return t;
80 }
81  
82  
83 };