Skip to content

Instantly share code, notes, and snippets.

@fundon
Created May 12, 2012 03:23
Show Gist options
  • Save fundon/2663918 to your computer and use it in GitHub Desktop.
Save fundon/2663918 to your computer and use it in GitHub Desktop.
base64 decoder

base64 decoder

A tweet-sized 64bit decoder inspired by this base64 encoder: https://gist.github.com/999166

It will fail on older IEs (Version 7 or older) unless the input String is converted to an Array using .split('').

Thanks to @jed for this great idea of 140byt.es and thanks to him and Kambfhase for help; thanks to @nikola for a hint saving 2 brackets! Thanks to LeverOne for his support fixing the last bugs regarding RFC2045 conformity and golfing the last bytes.

function(
d, // base64 data (in IE7 or older, use .split('') to get this working
b, // replacement map ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
c, // character and - ascii value buffer
u, // bit storage
r, // result
q, // bit counter
x // char counter
){
for (
// initialize result and counters
r = q = x = '';
// get next character
c = d[x++];
// character found in table? initialize bit storage and add its ascii value;
~c && (u = q%4 ? u*64+c : c,
// and if not first of each 4 characters, convert the first 8bits to one ascii character
q++ % 4) ? r += String.fromCharCode(255&u>>(-2*q&6)) : 0
)
// try to find character in table (0-63, not found => -1)
c = b.indexOf(c);
// return result
return r
}
function(d,b,c,u,r,q,x){for(r=q=x='';c=d[x++];~c&&(u=q%4?u*64+c:c,q++%4)?r+=String.fromCharCode(255&u>>(-2*q&6)):0)c=b.indexOf(c);return r}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright* (C) 2011 Alex Kloss <[email protected]>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
* As far as something complicated as Copyright applies to such an
simple code...
{
"name": "Base64Decoder",
"description": "Fully working Base64 decoder in 139bytes",
"keywords": [
"base64",
"decode",
"padding",
"rfc2045"
]
}
<!DOCTYPE html>
<title>Base64 decoder</title>
<script type="text/javascript">
(function(){
var f = function(d,b,c,u,r,q,x){for(r=q=x='';c=d[x++];~c&&(u=q%4?u*64+c:c,q++%4)?r+=String.fromCharCode(255&u>>(-2*q&6)):0)c=b.indexOf(c);return r}
, test = {
"Zg==" : "f"
,"Zm8=" : "fo"
,"Zm9v" : "foo"
,"Zm9vYg==" : "foob"
,"Zm9vYmE=" : "fooba"
,"Zm9vYmFy" : "foobar"
,"MTQwYnl0ZX\n MgcnVsZXMh" : "140bytes rules!"
}
, error = 0;
;
for( i in test ) {
var r = f(i, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
if( r != test[i] ) {
error++;
document.writeln( 'Expected &quot;'+test[i]+'&quot; for &quot;'+i+'&quot; but got &quot;'+ r + "&quot;<br>" );
}
}
if( error > 0) {
document.writeln( "<br>"+error+ " tests failed!<br>" );
} else {
document.writeln( "<br>Everything is fine!<br>" );
}
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment