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