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