Mercurial > laserkard
diff awesome_js/Base64.js @ 37:021a9ab1ed5b laserkard
[svn r38] added echo.pl, a test program for the backend of the website
author | rlm |
---|---|
date | Sun, 24 Jan 2010 09:37:47 -0500 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/awesome_js/Base64.js Sun Jan 24 09:37:47 2010 -0500 1.3 @@ -0,0 +1,142 @@ 1.4 +/** 1.5 +* 1.6 +* Base64 encode / decode 1.7 +* http://www.webtoolkit.info/ 1.8 +* 1.9 +**/ 1.10 + 1.11 +var Base64 = { 1.12 + 1.13 + // private property 1.14 + _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", 1.15 + 1.16 + // public method for encoding 1.17 + encode : function (input) { 1.18 + var output = ""; 1.19 + var chr1, chr2, chr3, enc1, enc2, enc3, enc4; 1.20 + var i = 0; 1.21 + 1.22 + input = Base64._utf8_encode(input); 1.23 + 1.24 + while (i < input.length) { 1.25 + 1.26 + chr1 = input.charCodeAt(i++); 1.27 + chr2 = input.charCodeAt(i++); 1.28 + chr3 = input.charCodeAt(i++); 1.29 + 1.30 + enc1 = chr1 >> 2; 1.31 + enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); 1.32 + enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); 1.33 + enc4 = chr3 & 63; 1.34 + 1.35 + if (isNaN(chr2)) { 1.36 + enc3 = enc4 = 64; 1.37 + } else if (isNaN(chr3)) { 1.38 + enc4 = 64; 1.39 + } 1.40 + 1.41 + output = output + 1.42 + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + 1.43 + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4); 1.44 + 1.45 + } 1.46 + 1.47 + return output; 1.48 + }, 1.49 + 1.50 + // public method for decoding 1.51 + decode : function (input) { 1.52 + var output = ""; 1.53 + var chr1, chr2, chr3; 1.54 + var enc1, enc2, enc3, enc4; 1.55 + var i = 0; 1.56 + 1.57 + input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); 1.58 + 1.59 + while (i < input.length) { 1.60 + 1.61 + enc1 = this._keyStr.indexOf(input.charAt(i++)); 1.62 + enc2 = this._keyStr.indexOf(input.charAt(i++)); 1.63 + enc3 = this._keyStr.indexOf(input.charAt(i++)); 1.64 + enc4 = this._keyStr.indexOf(input.charAt(i++)); 1.65 + 1.66 + chr1 = (enc1 << 2) | (enc2 >> 4); 1.67 + chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); 1.68 + chr3 = ((enc3 & 3) << 6) | enc4; 1.69 + 1.70 + output = output + String.fromCharCode(chr1); 1.71 + 1.72 + if (enc3 != 64) { 1.73 + output = output + String.fromCharCode(chr2); 1.74 + } 1.75 + if (enc4 != 64) { 1.76 + output = output + String.fromCharCode(chr3); 1.77 + } 1.78 + 1.79 + } 1.80 + 1.81 + output = Base64._utf8_decode(output); 1.82 + 1.83 + return output; 1.84 + 1.85 + }, 1.86 + 1.87 + // private method for UTF-8 encoding 1.88 + _utf8_encode : function (string) { 1.89 + string = string.replace(/\r\n/g,"\n"); 1.90 + var utftext = ""; 1.91 + 1.92 + for (var n = 0; n < string.length; n++) { 1.93 + 1.94 + var c = string.charCodeAt(n); 1.95 + 1.96 + if (c < 128) { 1.97 + utftext += String.fromCharCode(c); 1.98 + } 1.99 + else if((c > 127) && (c < 2048)) { 1.100 + utftext += String.fromCharCode((c >> 6) | 192); 1.101 + utftext += String.fromCharCode((c & 63) | 128); 1.102 + } 1.103 + else { 1.104 + utftext += String.fromCharCode((c >> 12) | 224); 1.105 + utftext += String.fromCharCode(((c >> 6) & 63) | 128); 1.106 + utftext += String.fromCharCode((c & 63) | 128); 1.107 + } 1.108 + 1.109 + } 1.110 + 1.111 + return utftext; 1.112 + }, 1.113 + 1.114 + // private method for UTF-8 decoding 1.115 + _utf8_decode : function (utftext) { 1.116 + var string = ""; 1.117 + var i = 0; 1.118 + var c = c1 = c2 = 0; 1.119 + 1.120 + while ( i < utftext.length ) { 1.121 + 1.122 + c = utftext.charCodeAt(i); 1.123 + 1.124 + if (c < 128) { 1.125 + string += String.fromCharCode(c); 1.126 + i++; 1.127 + } 1.128 + else if((c > 191) && (c < 224)) { 1.129 + c2 = utftext.charCodeAt(i+1); 1.130 + string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); 1.131 + i += 2; 1.132 + } 1.133 + else { 1.134 + c2 = utftext.charCodeAt(i+1); 1.135 + c3 = utftext.charCodeAt(i+2); 1.136 + string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); 1.137 + i += 3; 1.138 + } 1.139 + 1.140 + } 1.141 + 1.142 + return string; 1.143 + } 1.144 + 1.145 +}