]>
Commit | Line | Data |
---|---|---|
a793fa46 JM |
1 | /** |
2 | * | |
3 | * Base64 encode / decode with array versions | |
4 | * | |
5 | * Based on Base64 from: | |
6 | * http://www.webtoolkit.info/ | |
7 | * | |
8 | **/ | |
9 | ||
10 | var Base64 = { | |
11 | ||
12 | // private property | |
13 | _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", | |
14 | ||
15 | // public method for encoding | |
16 | encode : function (input) { | |
17 | var output = ""; | |
18 | var chr1, chr2, chr3, enc1, enc2, enc3, enc4; | |
19 | var i = 0; | |
20 | ||
21 | input = Base64._utf8_encode(input); | |
22 | ||
23 | while (i < input.length) { | |
24 | ||
25 | chr1 = input.charCodeAt(i++); | |
26 | chr2 = input.charCodeAt(i++); | |
27 | chr3 = input.charCodeAt(i++); | |
28 | ||
29 | enc1 = chr1 >> 2; | |
30 | enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); | |
31 | enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); | |
32 | enc4 = chr3 & 63; | |
33 | ||
34 | if (isNaN(chr2)) { | |
35 | enc3 = enc4 = 64; | |
36 | } else if (isNaN(chr3)) { | |
37 | enc4 = 64; | |
38 | } | |
39 | ||
40 | output = output + | |
41 | this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + | |
42 | this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4); | |
43 | ||
44 | } | |
45 | ||
46 | return output; | |
47 | }, | |
48 | ||
49 | // public method for decoding to a string | |
50 | decode : function (input) { | |
51 | var output = ""; | |
52 | var chr1, chr2, chr3; | |
53 | var enc1, enc2, enc3, enc4; | |
54 | var i = 0; | |
55 | ||
56 | input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); | |
57 | ||
58 | while (i < input.length) { | |
59 | ||
60 | enc1 = this._keyStr.indexOf(input.charAt(i++)); | |
61 | enc2 = this._keyStr.indexOf(input.charAt(i++)); | |
62 | enc3 = this._keyStr.indexOf(input.charAt(i++)); | |
63 | enc4 = this._keyStr.indexOf(input.charAt(i++)); | |
64 | ||
65 | chr1 = (enc1 << 2) | (enc2 >> 4); | |
66 | chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); | |
67 | chr3 = ((enc3 & 3) << 6) | enc4; | |
68 | ||
69 | output = output + String.fromCharCode(chr1); | |
70 | ||
71 | if (enc3 != 64) { | |
72 | output = output + String.fromCharCode(chr2); | |
73 | } | |
74 | if (enc4 != 64) { | |
75 | output = output + String.fromCharCode(chr3); | |
76 | } | |
77 | ||
78 | } | |
79 | }, | |
80 | ||
81 | // public method for encoding an array | |
82 | encode_array : function (input) { | |
83 | var output = ""; | |
84 | var chr1, chr2, chr3, enc1, enc2, enc3, enc4; | |
85 | var i = 0; | |
86 | ||
87 | while (i < input.length) { | |
88 | ||
89 | chr1 = input[i++]; | |
90 | chr2 = input[i++]; | |
91 | chr3 = input[i++]; | |
92 | ||
93 | enc1 = chr1 >> 2; | |
94 | enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); | |
95 | enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); | |
96 | enc4 = chr3 & 63; | |
97 | ||
98 | if (isNaN(chr2)) { | |
99 | enc3 = enc4 = 64; | |
100 | } else if (isNaN(chr3)) { | |
101 | enc4 = 64; | |
102 | } | |
103 | ||
104 | output = output + | |
105 | this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + | |
106 | this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4); | |
107 | ||
108 | } | |
109 | ||
110 | return output; | |
111 | }, | |
112 | ||
113 | // public method for decoding to an array | |
114 | decode_array : function (input) { | |
115 | var output = []; | |
116 | var chr1, chr2, chr3; | |
117 | var enc1, enc2, enc3, enc4; | |
118 | var i = 0; | |
119 | ||
120 | input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); | |
121 | ||
122 | while (i < input.length) { | |
123 | ||
124 | enc1 = this._keyStr.indexOf(input.charAt(i++)); | |
125 | enc2 = this._keyStr.indexOf(input.charAt(i++)); | |
126 | enc3 = this._keyStr.indexOf(input.charAt(i++)); | |
127 | enc4 = this._keyStr.indexOf(input.charAt(i++)); | |
128 | ||
129 | chr1 = (enc1 << 2) | (enc2 >> 4); | |
130 | chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); | |
131 | chr3 = ((enc3 & 3) << 6) | enc4; | |
132 | ||
133 | output.push(chr1); | |
134 | ||
135 | if (enc3 != 64) { | |
136 | output.push(chr2); | |
137 | } | |
138 | if (enc4 != 64) { | |
139 | output.push(chr3); | |
140 | } | |
141 | ||
142 | } | |
143 | ||
144 | return output; | |
145 | ||
146 | }, | |
147 | ||
148 | // private method for UTF-8 encoding | |
149 | _utf8_encode : function (string) { | |
150 | string = string.replace(/\r\n/g,"\n"); | |
151 | var utftext = ""; | |
152 | ||
153 | for (var n = 0; n < string.length; n++) { | |
154 | ||
155 | var c = string.charCodeAt(n); | |
156 | ||
157 | if (c < 128) { | |
158 | utftext += String.fromCharCode(c); | |
159 | } | |
160 | else if((c > 127) && (c < 2048)) { | |
161 | utftext += String.fromCharCode((c >> 6) | 192); | |
162 | utftext += String.fromCharCode((c & 63) | 128); | |
163 | } | |
164 | else { | |
165 | utftext += String.fromCharCode((c >> 12) | 224); | |
166 | utftext += String.fromCharCode(((c >> 6) & 63) | 128); | |
167 | utftext += String.fromCharCode((c & 63) | 128); | |
168 | } | |
169 | ||
170 | } | |
171 | ||
172 | return utftext; | |
173 | }, | |
174 | ||
175 | // private method for UTF-8 decoding | |
176 | _utf8_decode : function (utftext) { | |
177 | var string = ""; | |
178 | var i = 0; | |
179 | var c = c1 = c2 = 0; | |
180 | ||
181 | while ( i < utftext.length ) { | |
182 | ||
183 | c = utftext.charCodeAt(i); | |
184 | ||
185 | if (c < 128) { | |
186 | string += String.fromCharCode(c); | |
187 | i++; | |
188 | } | |
189 | else if((c > 191) && (c < 224)) { | |
190 | c2 = utftext.charCodeAt(i+1); | |
191 | string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); | |
192 | i += 2; | |
193 | } | |
194 | else { | |
195 | c2 = utftext.charCodeAt(i+1); | |
196 | c3 = utftext.charCodeAt(i+2); | |
197 | string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); | |
198 | i += 3; | |
199 | } | |
200 | ||
201 | } | |
202 | ||
203 | return string; | |
204 | } | |
205 | ||
206 | } |