Path: blob/main/projects/cookie-clicker/base64.js
1834 views
/**1*2* Base64 encode / decode3* http://www.webtoolkit.info/4*5**/67var Base64 = {89// private property10_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",1112// public method for encoding13encode : function (input) {14var output = "";15var chr1, chr2, chr3, enc1, enc2, enc3, enc4;16var i = 0;1718input = Base64._utf8_encode(input);1920while (i < input.length) {2122chr1 = input.charCodeAt(i++);23chr2 = input.charCodeAt(i++);24chr3 = input.charCodeAt(i++);2526enc1 = chr1 >> 2;27enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);28enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);29enc4 = chr3 & 63;3031if (isNaN(chr2)) {32enc3 = enc4 = 64;33} else if (isNaN(chr3)) {34enc4 = 64;35}3637output = output +38this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +39this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);4041}4243return output;44},4546// public method for decoding47decode : function (input) {48var output = "";49var chr1, chr2, chr3;50var enc1, enc2, enc3, enc4;51var i = 0;5253input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");5455while (i < input.length) {5657enc1 = this._keyStr.indexOf(input.charAt(i++));58enc2 = this._keyStr.indexOf(input.charAt(i++));59enc3 = this._keyStr.indexOf(input.charAt(i++));60enc4 = this._keyStr.indexOf(input.charAt(i++));6162chr1 = (enc1 << 2) | (enc2 >> 4);63chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);64chr3 = ((enc3 & 3) << 6) | enc4;6566output = output + String.fromCharCode(chr1);6768if (enc3 != 64) {69output = output + String.fromCharCode(chr2);70}71if (enc4 != 64) {72output = output + String.fromCharCode(chr3);73}7475}7677output = Base64._utf8_decode(output);7879return output;8081},8283// private method for UTF-8 encoding84_utf8_encode : function (string) {85string = string.replace(/\r\n/g,"\n");86var utftext = "";8788for (var n = 0; n < string.length; n++) {8990var c = string.charCodeAt(n);9192if (c < 128) {93utftext += String.fromCharCode(c);94}95else if((c > 127) && (c < 2048)) {96utftext += String.fromCharCode((c >> 6) | 192);97utftext += String.fromCharCode((c & 63) | 128);98}99else {100utftext += String.fromCharCode((c >> 12) | 224);101utftext += String.fromCharCode(((c >> 6) & 63) | 128);102utftext += String.fromCharCode((c & 63) | 128);103}104105}106107return utftext;108},109110// private method for UTF-8 decoding111_utf8_decode : function (utftext) {112var string = "";113var i = 0;114var c = c1 = c2 = 0;115116while ( i < utftext.length ) {117118c = utftext.charCodeAt(i);119120if (c < 128) {121string += String.fromCharCode(c);122i++;123}124else if((c > 191) && (c < 224)) {125c2 = utftext.charCodeAt(i+1);126string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));127i += 2;128}129else {130c2 = utftext.charCodeAt(i+1);131c3 = utftext.charCodeAt(i+2);132string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));133i += 3;134}135136}137138return string;139}140141}142143