annotate awesome_js/Base64.js @ 85:da4f4f91f8aa laserkard

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