Skip to content

Commit

Permalink
fixed: Compressed code
Browse files Browse the repository at this point in the history
「return’a’」->「return$fog$0」
  • Loading branch information
zswang committed Sep 6, 2015
1 parent 9b609d8 commit cc26804
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 7 deletions.
17 changes: 13 additions & 4 deletions lib/jfogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* Javascript code obfuscator
* @author
* zswang (http://weibo.com/zswang)
* @version 0.0.9
* @date 2015-08-26
* @version 0.0.10
* @date 2015-09-07
*/
var esprima = require('esprima');
/**
Expand Down Expand Up @@ -141,8 +141,17 @@ function obfuscate(code, options) {
return b - a;
}).forEach(function (obj) {
if (obj.type === 'Literal') {
code = code.slice(0, obj.range[0]) + propertys[obj.$name] +
code.slice(obj.range[1]);
var begin = code.slice(0, obj.range[0]);
var end = code.slice(obj.range[1]);
code = begin;
if (/[^\s~!%^&*()_+\-={}\[\]|:";'<>,.?]$/.test(begin)) { // compress
code += ' ';
}
code += propertys[obj.$name];
if (/^[^\s~!%^&*()_+\-={}\[\]|:";'<>,.?]/.test(end)) {
code += ' ';
}
code += end;
}
else { // if (obj.type === 'MemberExpression') {
code = code.slice(0, obj.property.range[0]).replace(/\.\s*$/, '') +
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jfogs",
"version": "0.0.9",
"version": "0.0.10",
"description": "Javascript code obfuscator",
"main": "index.js",
"bin": {
Expand Down
13 changes: 11 additions & 2 deletions src/jfogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,17 @@ function obfuscate(code, options) {
return b - a;
}).forEach(function (obj) {
if (obj.type === 'Literal') {
code = code.slice(0, obj.range[0]) + propertys[obj.$name] +
code.slice(obj.range[1]);
var begin = code.slice(0, obj.range[0]);
var end = code.slice(obj.range[1]);
code = begin;
if (/[^\s~!%^&*()_+\-={}\[\]|:";'<>,.?]$/.test(begin)) { // compress
code += ' ';
}
code += propertys[obj.$name];
if (/^[^\s~!%^&*()_+\-={}\[\]|:";'<>,.?]/.test(end)) {
code += ' ';
}
code += end;
}
else { // if (obj.type === 'MemberExpression') {
code = code.slice(0, obj.property.range[0]).replace(/\.\s*$/, '') +
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ node cli test/fixtures/one.input.js -o test/fixtures/one.zero.output.js -t zero
node cli test/fixtures/use-strict.input.js -o test/fixtures/use-strict.output.js
node cli test/fixtures/use-strict.input.js -o test/fixtures/use-strict.reverse.output.js -t reverse
node cli test/fixtures/use-strict.input.js -o test/fixtures/use-strict.zero.output.js -t zero

node cli test/fixtures/min.input.js -o test/fixtures/min.output.js
node cli test/fixtures/min.input.js -o test/fixtures/min.reverse.output.js -t reverse
node cli test/fixtures/min.input.js -o test/fixtures/min.zero.output.js -t zero
1 change: 1 addition & 0 deletions test/fixtures/min.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(function(){return'length'in{};})();
10 changes: 10 additions & 0 deletions test/fixtures/min.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

(function ($fog$1, $fog$0) {

if ("‮" !== $fog$1) {
return;
}

(function(){return $fog$0 in{};})();
})("‮", "length");

24 changes: 24 additions & 0 deletions test/fixtures/min.reverse.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

(function ($fog$0, $fog$13, $fog$4, $fog$3, $fog$9, $fog$6, $fog$7, $fog$8, $fog$12, $fog$10, $fog$11) {

if ("‮" !== $fog$13) {
return;
}
var $fog$1 = arguments;
var $fog$2;

for ($fog$2 = $fog$9; $fog$2 < $fog$4; $fog$2++) {
if (typeof $fog$1[$fog$2] === $fog$6) {
$fog$1[$fog$2] = $fog$1[$fog$2][$fog$7]($fog$3)[$fog$8]()[$fog$12]($fog$3);
}
}

for ($fog$2 = $fog$9; $fog$2 < $fog$4 / $fog$11; $fog$2++) {
var $fog$5 = $fog$1[$fog$2];
$fog$1[$fog$2] = $fog$1[$fog$4 - $fog$2 - $fog$10];
$fog$1[$fog$4 - $fog$2 - $fog$10] = $fog$5;
}

(function(){return $fog$0 in{};})();
})("htgnel", "‮", 1, "", 0, "string", "split", "reverse", "join", 1, 2);

26 changes: 26 additions & 0 deletions test/fixtures/min.zero.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

(function ($fog$0, $fog$16, $fog$4, $fog$5, $fog$6, $fog$13, $fog$14, $fog$12, $fog$7, $fog$9, $fog$10, $fog$11, $fog$15) {

if ("‮" !== $fog$16) {
return;
}
var $fog$1 = arguments;
for (var $fog$2 = 0; $fog$2 < $fog$4; $fog$2++) {
if (typeof $fog$1[$fog$2] !== $fog$5) {
continue;
}
$fog$1[$fog$2] = $fog$1[$fog$2][$fog$6]($fog$13,
function (a) {
return {
'\u200c': $fog$9,
'\u200d': $fog$10
}[a];
}
).replace($fog$14, function (a) {
return $fog$12[$fog$7]($fog$15(a, $fog$11));
});
}

(function(){return $fog$0 in{};})();
})("‍‍‌‍‍‌‌‍‍‌‌‍‌‍‍‍‌‍‍‍‌‍‍‌‌‍‍‍‍‍‍‌‍‌‌‍‍‌‍‌‌‌", "‮", 1, "string", "replace", /./g, /.{7}/g, String, "fromCharCode", 0, 1, 2, parseInt);

0 comments on commit cc26804

Please sign in to comment.