function charFromCharCode (charCode) {
return unescape('%' + charCode.toString(16));
}
function reverseBase64 () {
var r = new Object();
for (var i = 0; i < 64; i++) {
r[base64[i]] = i;
} return r;
}
var reversedBase64 = reverseBase64();
function decode (encStr) {
var charCodes = new Array();
var decStr = "";
/* charCodes contains the index values into the base64 array
for each character in the encoded string */
for (var i = 0; i < encStr.length; i++)
charCodes[i] = reversedBase64[encStr.charAt(i)];
for (var i = 0; i < encStr.length; i += 4) {
/* bits24 is 4 groups of 6-bit character indexes */
var bits24 = ( charCodes [i] & 0xFF ) << 18;
bits24 |= ( charCodes [i + 1] & 0xFF ) << 12;
bits24 |= ( charCodes [i + 2] & 0xFF ) << 6;
bits24 |= ( charCodes [i + 3] & 0xFF ) << 0;
/* grab the character for the first 8 bits by masking off the
last 16 bits and then shifting right */
decStr += charFromCharCode((bits24 & 0xFF0000) >> 16);
/* if the next characer is a pad character, there won't be a
charCode
for it; so charCodes[] will return false and the character
won't
be decoded. */
/* grab the character for the second 8 bits by masking off the
last 8 bits and then shifting right */
if (charCodes[i + 2]) // check for padding character =
decStr += charFromCharCode((bits24 & 0xFF00) >> 8);
/* grab the character for the last 8 bits */
if (charCodes[i + 3]) // check for padding character =
decStr += charFromCharCode((bits24 & 0xFF) >> 0);
} return decStr;
}
function encode (Str) {
var charCodes = new Array();
var encStr = "";
for (var i = 0; i < Str.length; i += 3) {
/* grab groups of three characters, 24 bits, then split into
4 groups of 6 bits, and use each group as an index into
base64[] to get the encoded character */
/* bits24 is 3 groups of 8-bit characters */
var bits24 = ( Str.charCodeAt(i) ) << 16;
var str = "spaghetti for the people!";
alert(encode(str))
...
/* Just couldn't let this slip by, one of these rare moments Mr. Honnen
proves human to the rest of us ;))
If for some reason one would decide to use the above "decode"
implementation instead of the NN4+ built-in "atob" method, he would
find it a no-go in NN4.05-. So, just for sake of it, provided below
goes a JS1.2 crossbrowser version using the same "base64s" string
scheme used in my "encode" implementation above. (should run slightly
faster than the previous "decode"-routine aswell).
*/