4 * Base64 encode / decode
5 * http://www.webtoolkit.info/
12 _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
14 // public method for encoding
15 encode : function (input) {
17 var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
20 input = Base64._utf8_encode(input);
22 while (i < input.length) {
24 chr1 = input.charCodeAt(i++);
25 chr2 = input.charCodeAt(i++);
26 chr3 = input.charCodeAt(i++);
29 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
30 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
35 } else if (isNaN(chr3)) {
40 this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
41 this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
48 // public method for decoding
49 decode : function (input) {
52 var enc1, enc2, enc3, enc4;
55 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
57 while (i < input.length) {
59 enc1 = this._keyStr.indexOf(input.charAt(i++));
60 enc2 = this._keyStr.indexOf(input.charAt(i++));
61 enc3 = this._keyStr.indexOf(input.charAt(i++));
62 enc4 = this._keyStr.indexOf(input.charAt(i++));
64 chr1 = (enc1 << 2) | (enc2 >> 4);
65 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
66 chr3 = ((enc3 & 3) << 6) | enc4;
68 output = output + String.fromCharCode(chr1);
71 output = output + String.fromCharCode(chr2);
74 output = output + String.fromCharCode(chr3);
79 output = Base64._utf8_decode(output);
85 // private method for UTF-8 encoding
86 _utf8_encode : function (string) {
87 string = string.replace(/\r\n/g,"\n");
90 for (var n = 0; n < string.length; n++) {
92 var c = string.charCodeAt(n);
95 utftext += String.fromCharCode(c);
97 else if((c > 127) && (c < 2048)) {
98 utftext += String.fromCharCode((c >> 6) | 192);
99 utftext += String.fromCharCode((c & 63) | 128);
102 utftext += String.fromCharCode((c >> 12) | 224);
103 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
104 utftext += String.fromCharCode((c & 63) | 128);
112 // private method for UTF-8 decoding
113 _utf8_decode : function (utftext) {
118 while ( i < utftext.length ) {
120 c = utftext.charCodeAt(i);
123 string += String.fromCharCode(c);
126 else if((c > 191) && (c < 224)) {
127 c2 = utftext.charCodeAt(i+1);
128 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
132 c2 = utftext.charCodeAt(i+1);
133 c3 = utftext.charCodeAt(i+2);
134 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));