Skip to content

Instantly share code, notes, and snippets.

@JavaScript-Packer
Last active September 30, 2015 15:39
Show Gist options
  • Select an option

  • Save JavaScript-Packer/fe5c7357de460fcab5f2 to your computer and use it in GitHub Desktop.

Select an option

Save JavaScript-Packer/fe5c7357de460fcab5f2 to your computer and use it in GitHub Desktop.
If I recode base64 encoded data with my modded base64 encoder (you will have to include small 288 byte decode function with data to unpack) that uses spaces as one of the characters, I can now achieve decent compression with server side gzip of average 2% (few times over 10%) compared to 1% gained (usually gets bigger) before with embedded bzip …
function fake_btoa(a) {
var c, d, e, f, g, h, i, j, o, b = "WHAK.com/PaCker BDEFGIJLMNOQR\
STUVXYZbdfghijlnpqstuvwxyz+01234567=", k = 0, l = 0, m = "", n = [];
if (!a) return a;
do c = a.charCodeAt(k++), d = a.charCodeAt(k++), e = a.charCodeAt(k++), j = c << 16 | d << 8 | e,
f = 63 & j >> 18, g = 63 & j >> 12, h = 63 & j >> 6, i = 63 & j, n[l++] = b.charAt(f) + b.charAt(g) + b.charAt(h) + b.charAt(i); while (k < a.length);
return m = n.join(""), o = a.length % 3, (o ? m.slice(0, o - 3) :m) + "===".slice(o || 3);
}
function fake_atob(a) {
var b, c, d, e = {}, f = 0, g = 0, h = "", i = String.fromCharCode, j = a.length;
for (b = 0; 64 > b; b++) e["WHAK.com/PaCker BDEFGIJLMNOQR\
STUVXYZbdfghijlnpqstuvwxyz+01234567".charAt(b)] = b;
for (c = 0; j > c; c++) for (b = e[a.charAt(c)], f = (f << 6) + b, g += 6; g >= 8; ) ((d = 255 & f >>> (g -= 8)) || j - 2 > c) && (h += i(d));
return h;
}
/*
// Minified to 288 bytes:
function fake_atob(a){var b,c,d,e={},f=0,g=0,h="",i=String.fromCharCode,
j=a.length;for(b=0;64>b;b++)e["WHAK.com/PaCker BDEFGIJLMNOQR\
STUVXYZbdfghijlnpqstuvwxyz+01234567".charAt(b)]=b;for(c=0;j>c;c++)for(b=e[a.charAt(c)],f=(f<<6)+b,g+=6;g>=8;)((d=255&f>>>(g-=8))||j-2>c)&&(h+=i(d));return h}
*/
@JavaScript-Packer

Copy link
Copy Markdown
Author

Need to experiment more, find where the space makes most impact, maybe add in tabs too, have too see...

@JavaScript-Packer

Copy link
Copy Markdown
Author

Wonder how many times now I can recompress things before they start getting bigger. My 1.44MB Holy Bible app was originally 6MB, minified JavaScript/Jquery (down to 5.3 MB), WHAK packed HTML (brought to about 4MB), compressed to bzip, then gzipped. Gzip alone only achieved 2.3MB and bzip alone only got 1,8MB. Here is my 1.44 (fits on floppy) portable Bible app: http://holybibleverse.master.com/texis/master/search/ and now I might be able to recode it with my little hack and get it down another 10% or more!

@JavaScript-Packer

Copy link
Copy Markdown
Author

Maybe randomizing the character map will help me find a "sweet spot"...

@JavaScript-Packer

Copy link
Copy Markdown
Author

Let web user use his/her own custom string for character map using only available characers so they may make vanity like mine

@JavaScript-Packer

Copy link
Copy Markdown
Author

Function to pack pure JavaScript code in WHAK64 encoding:

function fakerJS(maker){return 'function com(a){var b\
,c,d,e={},f=0,g=0,h="",i=String.fromCharCode,j=a.leng\
th;for(b=0;64>b;b++)e["WHAK.com/PaCker BDEFGIJLMNOQRS\
TUVXYZbdfghijlnpqstuvwxyz+01234567".charAt(b)]=b;for(\
c=0;j>c;c++)for(b=e[a.charAt(c)],f=(f<<6)+b,g+=6;g>=8\
;)((d=255&f>>>(g-=8))||j-2>c)&&(h+=i(d));return h}\nv\
ar WHAK=com("'+fake_btoa(maker)+'");eval(WHAK);'}

@JavaScript-Packer

Copy link
Copy Markdown
Author

Here is a combo of atob/btoa/fake64 and even eval JavaScript packer demo code:

var REAL='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',FAKE='WHAK.com/PaCker BDEFGIJLMNOQRSTUVXYZbdfghijlnpqstuvwxyz+01234567=',part1='function WHAK_com(a){var b,c,d,e={},f=0,g=0,h="",i=String.fromCharCode,j=a.length;for(b=0;64>b;b++)e["',
part2='".charAt(b)]=b;for(c=0;j>c;c++)for(b=e[a.charAt(c)],f=(f<<6)+b,g+=6;g>=8;)((d=255&f>>>(g-=8))||j-2>c)&&(h+=i(d));return h}\n';

document.write('<div id="whak"></div>');

function put(x){document.getElementById('whak').innerHTML+=x}

function txt(x,id){x='<p><textarea id="'+id+'" style="width:728px;height:150px" onmouseover="this.select();">'+x+'</textarea></p>';put(x)}

txt('','input');
txt('','output');

function but(func){var x='<input type="button" value="'+func+'" onclick="var tmp='+func+'(document.getElementById(\'input\').value);document.getElementById(\'output\').value=tmp;"> ';put(x)}

but('b2a');
but('a2b');
but('fake64');
but('unfake64');
but('base64_to_fake64');
but('fake64_to_base64');
but('pack_fake64');
but('pack_base64');


function b2a(a){var c,d,e,f,g,h,i,j,o,b=REAL,k=0,l=0,m="",n=[];if(!a)return a;do c=a.charCodeAt(k++),d=a.charCodeAt(k++),e=a.charCodeAt(k++),j=c<<16|d<<8|e,f=63&j>>18,g=63&j>>12,h=63&j>>6,i=63&j,n[l++]=b.charAt(f)+b.charAt(g)+b.charAt(h)+b.charAt(i);while(k<a.length);return m=n.join(""),o=a.length%3,(o?m.slice(0,o-3):m)+"===".slice(o||3)}

function a2b(a){var b,c,d,e={},f=0,g=0,h="",i=String.fromCharCode,j=a.length;for(b=0;64>b;b++)e[REAL.charAt(b)]=b;for(c=0;j>c;c++)for(b=e[a.charAt(c)],f=(f<<6)+b,g+=6;g>=8;)((d=255&f>>>(g-=8))||j-2>c)&&(h+=i(d));return h}

function fake64(a){var c,d,e,f,g,h,i,j,o,b=FAKE,k=0,l=0,m="",n=[];if(!a)return a;do c=a.charCodeAt(k++),d=a.charCodeAt(k++),e=a.charCodeAt(k++),j=c<<16|d<<8|e,f=63&j>>18,g=63&j>>12,h=63&j>>6,i=63&j,n[l++]=b.charAt(f)+b.charAt(g)+b.charAt(h)+b.charAt(i);while(k<a.length);return m=n.join(""),o=a.length%3,(o?m.slice(0,o-3):m)+"===".slice(o||3)}

function unfake64(a){var b,c,d,e={},f=0,g=0,h="",i=String.fromCharCode,j=a.length;for(b=0;64>b;b++)e[FAKE.charAt(b)]=b;for(c=0;j>c;c++)for(b=e[a.charAt(c)],f=(f<<6)+b,g+=6;g>=8;)((d=255&f>>>(g-=8))||j-2>c)&&(h+=i(d));return h}

function base64_to_fake64(x){return fake64(a2b(x))}

function fake64_to_base64(x){return b2a(unfake64(x))}


function pack_fake64(x){var part3='eval(WHAK_com("'+fake64(x)+'"));';return part1+FAKE+part2+part3}
function pack_base64(x){var part3='eval(WHAK_com("'+fake64(x)+'"));';return part1+REAL+part2+part3}

@JavaScript-Packer

Copy link
Copy Markdown
Author

Above code minified:

var REAL='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',FAKE='WHAK.com/PaCker BDEFGIJLMNOQRSTUVXYZbdfghijlnpqstuvwxyz+01234567=',part1='function WHAK_com(a){var b,c,d,e={},f=0,g=0,h="",i=String.fromCharCode,j=a.length;for(b=0;64>b;b++)e["', part2='".charAt(b)]=b;for(c=0;j>c;c++)for(b=e[a.charAt(c)],f=(f<<6)+b,g+=6;g>=8;)((d=255&f>>>(g-=8))||j-2>c)&&(h+=i(d));return h}\n'; document.write('<div id="whak"></div>'); function put(x){document.getElementById('whak').innerHTML+=x} function txt(x,id){x='<p><textarea id="'+id+'" style="width:728px;height:150px" onmouseover="this.select();">'+x+'</textarea></p>';put(x)} txt('','input'); txt('','output'); function but(func){var x='<input type="button" value="'+func+'" onclick="var tmp='+func+'(document.getElementById(\'input\').value);document.getElementById(\'output\').value=tmp;"> ';put(x)} but('b2a'); but('a2b'); but('fake64'); but('unfake64'); but('base64_to_fake64'); but('fake64_to_base64'); but('pack_fake64'); but('pack_base64'); function b2a(a){var c,d,e,f,g,h,i,j,o,b=REAL,k=0,l=0,m="",n=[];if(!a)return a;do c=a.charCodeAt(k++),d=a.charCodeAt(k++),e=a.charCodeAt(k++),j=c<<16|d<<8|e,f=63&j>>18,g=63&j>>12,h=63&j>>6,i=63&j,n[l++]=b.charAt(f)+b.charAt(g)+b.charAt(h)+b.charAt(i);while(k<a.length);return m=n.join(""),o=a.length%3,(o?m.slice(0,o-3):m)+"===".slice(o||3)} function a2b(a){var b,c,d,e={},f=0,g=0,h="",i=String.fromCharCode,j=a.length;for(b=0;64>b;b++)e[REAL.charAt(b)]=b;for(c=0;j>c;c++)for(b=e[a.charAt(c)],f=(f<<6)+b,g+=6;g>=8;)((d=255&f>>>(g-=8))||j-2>c)&&(h+=i(d));return h} function fake64(a){var c,d,e,f,g,h,i,j,o,b=FAKE,k=0,l=0,m="",n=[];if(!a)return a;do c=a.charCodeAt(k++),d=a.charCodeAt(k++),e=a.charCodeAt(k++),j=c<<16|d<<8|e,f=63&j>>18,g=63&j>>12,h=63&j>>6,i=63&j,n[l++]=b.charAt(f)+b.charAt(g)+b.charAt(h)+b.charAt(i);while(k<a.length);return m=n.join(""),o=a.length%3,(o?m.slice(0,o-3):m)+"===".slice(o||3)} function unfake64(a){var b,c,d,e={},f=0,g=0,h="",i=String.fromCharCode,j=a.length;for(b=0;64>b;b++)e[FAKE.charAt(b)]=b;for(c=0;j>c;c++)for(b=e[a.charAt(c)],f=(f<<6)+b,g+=6;g>=8;)((d=255&f>>>(g-=8))||j-2>c)&&(h+=i(d));return h} function base64_to_fake64(x){return fake64(a2b(x))} function fake64_to_base64(x){return b2a(unfake64(x))} function pack_fake64(x){var part3='eval(WHAK_com("'+fake64(x)+'"));';return part1+FAKE+part2+part3} function pack_base64(x){var part3='eval(WHAK_com("'+fake64(x)+'"));';return part1+REAL+part2+part3}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment