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