Skip to content

Commit

Permalink
Optimize wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
hazzik committed Sep 2, 2020
1 parent 312b10b commit 3d68105
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
31 changes: 28 additions & 3 deletions jsfuck.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@
}
}

function escapeSequenceForReplace(c) {
return escapeSequence(c).replace('\\', 't');
}

function encode(input, wrapWithEval, runInParentScope){
var output = [];

Expand All @@ -254,8 +258,24 @@
}
unmappped = unmappped.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
unmappped = new RegExp('[^' + unmappped + ']','g');
var hasUnmappedCharacters = unmappped.test(input);
if (hasUnmappedCharacters) {
var unmappedCharactersCount = (input.match(unmappped) || []).length;
if (unmappedCharactersCount > 1) {
// Without this optimization one unmapped caracter has encoded length
// of about 3600 characters. Every additional unmapped character adds
// 2000 to the total length. For example, the lenght of `~` is 3605,
// `~~` is 5600, and `~~~` is 7595.
//
// The loader with replace has encoded length of about 5300 characters
// and every additional character adds 100 to the total length.
// In the same example the length of `~~` becomes 5371 and `~~~` -- 5463.
//
// So, when we have more than one unmapped character we want to encode whole input
// except select characters (that have encoded length less than about 70)
// into an escape sequence.
//
// NOTE: `t` should be escaped!
input = input.replace(/[^0123456789.adefilnrsuN]/g, escapeSequenceForReplace);
} else if (unmappedCharactersCount > 0) {
//Because we will wrap the input into a string we need to escape Backslash
// and Double quote characters (we do not need to worry about other characters
// because they are not mapped explicitly).
Expand Down Expand Up @@ -295,7 +315,12 @@
output += "+[]";
}

if (hasUnmappedCharacters) {
if (unmappedCharactersCount > 1) {
// replace `t` with `\\`
output = "(" + output + ")[" + encode("split") + "](" + encode ("t") + ")[" + encode("join") +"](" + encode("\\") + ")";
}

if (unmappedCharactersCount > 0) {
output = "[][" + encode("flat") + "]"+
"[" + encode("constructor") + "]" +
"(" + encode("return\"") + "+" + output + "+" + encode("\"") + ")()";
Expand Down
5 changes: 5 additions & 0 deletions test/jsfuck_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ createTest('NaNtrue');
createTest('trueNaN');
createTest('undefinedNaN');
createTest('~\\"');
createTest('t~');
createTest('~t');
createTest('[(~t~)]');
createTest('~0123456789 abcdefghijklmnopqrstuvwxyz()+.~');
createTest('~0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ()+.~');

for(var i=MIN; i<MAX ;i++) {
createTest(String.fromCharCode(i));
Expand Down

0 comments on commit 3d68105

Please sign in to comment.