Skip to content

Instantly share code, notes, and snippets.

@nikhilm
Created April 15, 2012 17:00
Show Gist options
  • Save nikhilm/2393861 to your computer and use it in GitHub Desktop.
Save nikhilm/2393861 to your computer and use it in GitHub Desktop.
JSCrush 'reverse engineering'
segmentLengthBound = ... // B, originally set to s.length/2
counter = {} // o
bestSavings = 0 // M
maxRepetitions = 0 // N
bestSegment = 0 // e
longestSegmentLength = 0 // Z
segmentLength = 0 // t
while (segmentLength <= segmentLengthBound) {
start = 1;
while (i < s.length-segmentLength) {
j = start
segment = s.substr(j, segmentLength);
if (!counter[segment]) {
j = s.indexOf(segment, j + segmentLength)
if (~j) {
longestSegmentLength = segmentLength
counter[segment] = 1
while (~j) {
j = s.indexOf(segment, j + segmentLength)
counter[segment]++
}
}
}
++i
}
++segmentLength
}
segmentLengthBound = longestSegmentLength
for (segment in counter) {
segmentLength = encodeURI(i).replace(/%../g,'i').length // j
repetitions = counter[segment]
savings = repetitions * segmentLength - (repetitions + segmentLength + 1)
if (savings > bestSavings || savings == bestSavings && repetitions > maxRepetitions) {
bestSavings = savings
maxRepetitions = repetitions
bestSegment = segment
}
}
for(Y=0;$="WVPKGFEDA@5# "[Y++];)
with(_.split($))
_=join(pop());
eval(_)
b.innerHTML="Javascript ❤ source<br><textarea rows=12 cols=80></textarea><br><button>CRUSH</button> <b></b><br><textarea rows=12 cols=80></textarea>"+b.innerHTML;Q=[];for(i=127;--i;i-10&&i-13&&i-34&&i-39&&i-92&&Q.push(String.fromCharCode(i)));setTimeout("b.children[1].value=eval(b.children[9].innerHTML.replace(/eval\\(_\\)/,'_'));L()");b.children[3].onclick=L=function(s){i=s=b.children[1].value.replace(/([\r\n]|^)\s*\/\/.*|[\r\n]+\s*/g,'').replace(/\\/g,'\\\\'),B=s.length/2,m='';for(S=encodeURI(i).replace(/%../g,'i').length;;m=c+m){for(c=0,i=122;!c&&--i;!~s.indexOf(Q[i])&&(c=Q[i]));if(!c)break;for(o={},M=N=e=Z=t=0;++t<=B;)for(i=0;++i<s.length-t;)if(!o[x=s.substr(j=i,t)])if(~(j=s.indexOf(x,j+t)))for(Z=t,o[x]=1;~j;o[x]++)j=s.indexOf(x,j+t);B=Z;for(i in o){j=encodeURI(i).replace(/%../g,'i').length;if(j=(R=o[i])*j-R-j-1)if(j>M||j==M&&R>N)M=j,N=R,e=i}if(!e)break;s=s.split(e).join(c)+c+e}c=s.split('"').length<s.split("'").length?(B='"',/"/g):(B="'",/'/g);i=b.children[6].value='_='+B+s.replace(c,'\\'+B)+B+';for(Y=0;$='+B+m+B+'[Y++];)with(_.split($))_=join(pop());eval(_)';i=encodeURI(i).replace(/%../g,'i').length;b.children[4].innerHTML=S+'B to '+i+'B ('+(i=i-S)+'B, '+((i/S*1e4|0)/100)+'%)'}
_="b=\"Javascript ❤ sourceCRUSH</ <b></b>\"+b;Q=[]@127;Vi-1013343992Q.push(Strg.fromCharCode(i)setTimeout(\"1 =e(9]Ae(_)/,'_'L()\");3].onclick=L=function(s){D1 A(|^)s*//.*|+s*EAE,B=s/2,m'@S;m=c+m){c=0,122;!cV!~ Q)(c=Q!co={},M=N=e=Z=t=0;Ft<=B;)0;Fi<s-t;)!#=s.substr(Gi,t)])~())Z=t,#]=1;~j;#]F);B=Z@i  o){jG(R=o)*j-R-j-1)j>M||G=MR>N)M=j,N=R,e=i}!eDs e).Wc)+c+e}c=s '\"E<s \"'\")?(B\"',/\"/g):(B=\"'\",/'/g);6 _sc,'KB)'@Y=0;$m'[YF];)with(_ $))_=Wpop(e(_)';i4]=S5 to Ki5 (K(i-S)5, K((i/S*1e4|0)/100)+'%)'}=encodeURI(i)A%..iE;b.childPn[ rowD12 colD80></>.Pplace(.nerHTML.lengthfor(\\ .split( s.dexOf( ].ueif(i-G x,j+t)textaPa));val+B+/g,'button>)bPak;i=in<br><[rn]&&='[i]#o[x5+'B@;A/Ds=E')F++Gj=K'+PreV--i;Wjo(";for(Y=0;$="WVPKGFEDA@5# "[Y++];)with(_.split($))_=join(pop());eval(_)
b.innerHTML="Javascript ❤ source<br><textarea rows=12 cols=80></textarea><br><button>CRUSH</button> <b></b><br><textarea rows=12 cols=80></textarea>"+b.innerHTML;
Q=[];
for(i=127;--i;i-10&&i-13&&i-34&&i-39&&i-92&&Q.push(String.fromCharCode(i)))
;
setTimeout(function() {
b.children[1].value = b.children[9].innerHTML;
L()
});
b.children[3].onclick = L = function(s) {
i = s = b.children[1].value.replace(/([\r\n]|^)\s*\/\/.*|[\r\n]+\s*/g,'').replace(/\\/g,'\\\\');
B=s.length/2;
m='';
for(S=encodeURI(i).replace(/%../g,'i').length;;m=c+m) {
for (c=0,i=122;!c&&--i;!~s.indexOf(Q[i])&&(c=Q[i]))
;
if (!c)
break;
for (o={},M=N=e=Z=t=0; ++t<=B; )
for (i=0;++i<s.length-t;)
if (!o[x=s.substr(j=i,t)])
if (~(j=s.indexOf(x,j+t)))
for (Z=t,o[x]=1;~j;o[x]++)
j=s.indexOf(x,j+t);
B=Z;
for (i in o) {
j=encodeURI(i).replace(/%../g,'i').length;
if (j=(R=o[i])*j-R-j-1)
if(j>M||j==M&&R>N)
M=j,N=R,e=i
}
if (!e)
break;
s=s.split(e).join(c)+c+e
}
c = s.split('"').length < s.split("'").length ? (B='"',/"/g) : (B="'",/'/g);
i = b.children[6].value= '_=' + B + s.replace(c,'\\'+B) +B + ';for(Y=0;$=' + B + m + B + '[Y++];)with(_.split($))_=join(pop());eval(_)';
i = encodeURI(i).replace(/%../g,'i').length;
b.children[4].innerHTML = S + 'B to ' + i + 'B (' + (i=i-S) + 'B, ' + ((i/S*1e4|0)/100)+'%)'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment