Base64 decoder
116:
function(a,b,c,d,e){for(e='';~(c=c<<6|b.indexOf(a[d|=0]));d++%4?e+=String.fromCharCode(c>>(-d&3)*2&255):0);return e}
//added later
115:
function(a,b,c,d,e){for(e='';~(c=c<<6|b.indexOf(a[d|=0]));d++%4?e+=String.fromCharCode(c>>(d*6&6)&255):0);return e}
Notes:
1. Why not "search" ?
1) alert("+".search("+"));
2) ~(c=c<<6|b.search(a[d|=0])) <-- obviously endless loop if input data w/o "=", as we see in older versions here [
gist.github.com]
2. It's still the incorrect decoder.
Quote
http://tools.ietf.org/html/rfc2045
Any characters outside of the base64 alphabet are to be ignored in
base64-encoded data.
Join! :)
Correct decoder
130:
function(a,b,c,d,e,f){f=e='';for(d in a)~(d=b.indexOf(a[+d]))&&(c=d|c<<6,f++%4)?e+=String.fromCharCode(c>>(f*6&6)&255):0;return e}
Notes:
1. a[+d] - to exclude custom prototypic methods/properties.
2.
Quote
http://tools.ietf.org/html/rfc2045
Because it is used only for padding at the end of the data, the
occurrence of any "=" characters may be taken as evidence that the
end of the data has been reached (without truncation in transit).
In this implementation "=" character is not regarded as an end of the data. Therefore, input "YWJ=j" will be decoded as "abc".
Example:
alert(b64decode('YW %-) Jj','ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'))
To encode/decode UTF-8 string you can use this solution:
alert(decodeURI(escape(b64decode('0YLQtdGB0YI=','ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'))))
alert(b64encode(unescape(encodeURI('\u0442\u0435\u0441\u0442')),'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'))
[
ecmanaut.blogspot.com]
----------------------
~Veritas~
Edited 8 time(s). Last edit at 01/26/2012 07:09AM by LeverOne.