base64.js (3258B)
1 var Base64 = { 2 // private property 3 _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", 4 5 // public method for encoding 6 encode : function (input, needEncoding) { 7 var output = ""; 8 var chr1, chr2, chr3, enc1, enc2, enc3, enc4; 9 var i = 0; 10 11 if (needEncoding) { 12 input = Base64._utf8_encode(input); 13 } 14 15 while (i < input.length) { 16 chr1 = input.charCodeAt(i++); 17 chr2 = input.charCodeAt(i++); 18 chr3 = input.charCodeAt(i++); 19 20 enc1 = chr1 >> 2; 21 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); 22 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); 23 enc4 = chr3 & 63; 24 25 if (isNaN(chr2)) { 26 enc3 = enc4 = 64; 27 } else if (isNaN(chr3)) { 28 enc4 = 64; 29 } 30 31 output = output + 32 this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + 33 this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4); 34 } 35 36 return output; 37 }, 38 39 // public method for decoding 40 decode : function (input, needDecoding) { 41 var output = ""; 42 var chr1, chr2, chr3; 43 var enc1, enc2, enc3, enc4; 44 var i = 0; 45 46 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); 47 48 while (i < input.length) { 49 50 enc1 = this._keyStr.indexOf(input.charAt(i++)); 51 enc2 = this._keyStr.indexOf(input.charAt(i++)); 52 enc3 = this._keyStr.indexOf(input.charAt(i++)); 53 enc4 = this._keyStr.indexOf(input.charAt(i++)); 54 55 chr1 = (enc1 << 2) | (enc2 >> 4); 56 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); 57 chr3 = ((enc3 & 3) << 6) | enc4; 58 59 output = output + String.fromCharCode(chr1); 60 61 if (enc3 != 64) { 62 output = output + String.fromCharCode(chr2); 63 } 64 if (enc4 != 64) { 65 output = output + String.fromCharCode(chr3); 66 } 67 } 68 69 if (needDecoding) { 70 output = Base64._utf8_decode(output); 71 } 72 73 return output; 74 }, 75 76 // private method for UTF-8 encoding 77 _utf8_encode : function (string) { 78 string = string.replace(/\r\n/g,"\n"); 79 var utftext = ""; 80 for (var n = 0; n < string.length; n++) { 81 var c = string.charCodeAt(n); 82 if (c < 128) { 83 utftext += String.fromCharCode(c); 84 } 85 else if((c > 127) && (c < 2048)) { 86 utftext += String.fromCharCode((c >> 6) | 192); 87 utftext += String.fromCharCode((c & 63) | 128); 88 } 89 else { 90 utftext += String.fromCharCode((c >> 12) | 224); 91 utftext += String.fromCharCode(((c >> 6) & 63) | 128); 92 utftext += String.fromCharCode((c & 63) | 128); 93 } 94 } 95 96 return utftext; 97 }, 98 99 // private method for UTF-8 decoding 100 _utf8_decode : function (utftext) { 101 var string = ""; 102 var i = 0; 103 var c = c1 = c2 = 0; 104 105 while ( i < utftext.length ) { 106 c = utftext.charCodeAt(i); 107 if (c < 128) { 108 string += String.fromCharCode(c); 109 i++; 110 } 111 else if((c > 191) && (c < 224)) { 112 c2 = utftext.charCodeAt(i+1); 113 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); 114 i += 2; 115 } 116 else { 117 c2 = utftext.charCodeAt(i+1); 118 c3 = utftext.charCodeAt(i+2); 119 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); 120 i += 3; 121 } 122 } 123 return string; 124 } 125 };