| 1 |
/* |
| 2 |
|
| 3 |
The MIT License (MIT) |
| 4 |
|
| 5 |
Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors. |
| 6 |
|
| 7 |
Permission is hereby granted, free of charge, to any person |
| 8 |
obtaining a copy of this software and associated documentation files |
| 9 |
(the "Software"), to deal in the Software without restriction, |
| 10 |
including without limitation the rights to use, copy, modify, merge, |
| 11 |
publish, distribute, sublicense, and/or sell copies of the Software, |
| 12 |
and to permit persons to whom the Software is furnished to do so, |
| 13 |
subject to the following conditions: |
| 14 |
|
| 15 |
The above copyright notice and this permission notice shall be |
| 16 |
included in all copies or substantial portions of the Software. |
| 17 |
|
| 18 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 22 |
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 23 |
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 24 |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 |
SOFTWARE. |
| 26 |
*/ |
| 27 |
|
| 28 |
// |
| 29 |
// Unpacker for Dean Edward's p.a.c.k.e.r, a part of javascript beautifier |
| 30 |
// |
| 31 |
// Coincidentally, it can defeat a couple of other eval-based compressors. |
| 32 |
// |
| 33 |
// usage: |
| 34 |
// |
| 35 |
// if (P_A_C_K_E_R.detect(some_string)) { |
| 36 |
// var unpacked = P_A_C_K_E_R.unpack(some_string); |
| 37 |
// } |
| 38 |
// |
| 39 |
// |
| 40 |
|
| 41 |
/*jshint strict:false */ |
| 42 |
|
| 43 |
var P_A_C_K_E_R = { |
| 44 |
detect: function(str) { |
| 45 |
return (P_A_C_K_E_R.get_chunks(str).length > 0); |
| 46 |
}, |
| 47 |
|
| 48 |
get_chunks: function(str) { |
| 49 |
var chunks = str.match(/eval\(\(?function\(.*?(,0,\{\}\)\)|split\('\|'\)\)\))($|\n)/g); |
| 50 |
return chunks ? chunks : []; |
| 51 |
}, |
| 52 |
|
| 53 |
unpack: function(str) { |
| 54 |
var chunks = P_A_C_K_E_R.get_chunks(str), |
| 55 |
chunk; |
| 56 |
for (var i = 0; i < chunks.length; i++) { |
| 57 |
chunk = chunks[i].replace(/\n$/, ''); |
| 58 |
str = str.split(chunk).join(P_A_C_K_E_R.unpack_chunk(chunk)); |
| 59 |
} |
| 60 |
return str; |
| 61 |
}, |
| 62 |
|
| 63 |
unpack_chunk: function(str) { |
| 64 |
var unpacked_source = ''; |
| 65 |
var __eval = eval; |
| 66 |
if (P_A_C_K_E_R.detect(str)) { |
| 67 |
try { |
| 68 |
eval = function(s) { // jshint ignore:line |
| 69 |
unpacked_source += s; |
| 70 |
return unpacked_source; |
| 71 |
}; // jshint ignore:line |
| 72 |
__eval(str); |
| 73 |
if (typeof unpacked_source === 'string' && unpacked_source) { |
| 74 |
str = unpacked_source; |
| 75 |
} |
| 76 |
} catch (e) { |
| 77 |
// well, it failed. we'll just return the original, instead of crashing on user. |
| 78 |
} |
| 79 |
} |
| 80 |
eval = __eval; // jshint ignore:line |
| 81 |
return str; |
| 82 |
}, |
| 83 |
|
| 84 |
run_tests: function(sanity_test) { |
| 85 |
var t = sanity_test || new SanityTest(); |
| 86 |
|
| 87 |
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,{}))"; |
| 88 |
var unpk1 = 'var a=1'; |
| 89 |
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,{}))"; |
| 90 |
var unpk2 = 'foo b=1'; |
| 91 |
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,{}))"; |
| 92 |
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,{}))"; |
| 93 |
var unpk3 = 'var a=1{}))'; |
| 94 |
|
| 95 |
t.test_function(P_A_C_K_E_R.detect, "P_A_C_K_E_R.detect"); |
| 96 |
t.expect('', false); |
| 97 |
t.expect('var a = b', false); |
| 98 |
t.test_function(P_A_C_K_E_R.unpack, "P_A_C_K_E_R.unpack"); |
| 99 |
t.expect(pk_broken, pk_broken); |
| 100 |
t.expect(pk1, unpk1); |
| 101 |
t.expect(pk2, unpk2); |
| 102 |
t.expect(pk3, unpk3); |
| 103 |
t.expect("function test (){alert ('This is a test!')}; " + |
| 104 |
"eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String))" + |
| 105 |
"{while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function" + |
| 106 |
"(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp(" + |
| 107 |
"'\\b'+e(c)+'\\b','g'),k[c]);return p}('0 2=\\\'{Íâ–+›ï;ã†Ù¥#\\\'',3,3," + |
| 108 |
"'var||a'.split('|'),0,{}))", |
| 109 |
"function test (){alert ('This is a test!')}; var a='{Íâ–+›ï;ã†Ù¥#'"); |
| 110 |
|
| 111 |
|
| 112 |
var filler = '\nfiller\n'; |
| 113 |
t.expect(filler + pk1 + "\n" + pk_broken + filler + pk2 + filler, filler + unpk1 + "\n" + pk_broken + filler + unpk2 + filler); |
| 114 |
|
| 115 |
return t; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
}; |