1 /*
2 * "streamable kanji code filter and converter"
3 * Copyright (c) 1998-2002 HappySize, Inc. All rights reserved.
4 *
5 * LICENSE NOTICES
6 *
7 * This file is part of "streamable kanji code filter and converter",
8 * which is distributed under the terms of GNU Lesser General Public
9 * License (version 2) as published by the Free Software Foundation.
10 *
11 * This software is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with "streamable kanji code filter and converter";
18 * if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 * Suite 330, Boston, MA 02111-1307 USA
20 *
21 * The author of this file:
22 *
23 */
24 /*
25 * The source code included in this files was separated from mbfilter.c
26 * by moriyoshi koizumi <moriyoshi@php.net> on 4 dec 2002.
27 *
28 */
29
30 #include "mbfilter.h"
31 #include "mbfilter_utf7.h"
32
33 static int mbfl_filt_conv_utf7_wchar_flush(mbfl_convert_filter *filter);
34
35 static const unsigned char mbfl_base64_table[] = {
36 /* 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', */
37 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,
38 /* 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', */
39 0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,
40 /* 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', */
41 0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,
42 /* 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', */
43 0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,
44 /* '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0' */
45 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x2b,0x2f,0x00
46 };
47
48 static const char *mbfl_encoding_utf7_aliases[] = {"utf7", NULL};
49
50 const mbfl_encoding mbfl_encoding_utf7 = {
51 mbfl_no_encoding_utf7,
52 "UTF-7",
53 "UTF-7",
54 mbfl_encoding_utf7_aliases,
55 NULL,
56 MBFL_ENCTYPE_GL_UNSAFE,
57 &vtbl_utf7_wchar,
58 &vtbl_wchar_utf7
59 };
60
61 const struct mbfl_convert_vtbl vtbl_utf7_wchar = {
62 mbfl_no_encoding_utf7,
63 mbfl_no_encoding_wchar,
64 mbfl_filt_conv_common_ctor,
65 NULL,
66 mbfl_filt_conv_utf7_wchar,
67 mbfl_filt_conv_utf7_wchar_flush,
68 NULL,
69 };
70
71 const struct mbfl_convert_vtbl vtbl_wchar_utf7 = {
72 mbfl_no_encoding_wchar,
73 mbfl_no_encoding_utf7,
74 mbfl_filt_conv_common_ctor,
75 NULL,
76 mbfl_filt_conv_wchar_utf7,
77 mbfl_filt_conv_wchar_utf7_flush,
78 NULL,
79 };
80
81
82 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
83
decode_base64_char(unsigned char c)84 static unsigned int decode_base64_char(unsigned char c)
85 {
86 if (c >= 'A' && c <= 'Z') {
87 return c - 65;
88 } else if (c >= 'a' && c <= 'z') {
89 return c - 71;
90 } else if (c >= '0' && c <= '9') {
91 return c + 4;
92 } else if (c == '+') {
93 return 62;
94 } else if (c == '/') {
95 return 63;
96 }
97 return -1;
98 }
99
mbfl_filt_conv_utf7_wchar(int c,mbfl_convert_filter * filter)100 int mbfl_filt_conv_utf7_wchar(int c, mbfl_convert_filter *filter)
101 {
102 int s, n = -1;
103
104 if (filter->status) { /* Modified Base64 */
105 n = decode_base64_char(c);
106 if (n < 0) {
107 if (filter->cache) {
108 /* Either we were expecting the 2nd half of a surrogate pair which
109 * never came, or else the last Base64 data was not padded with zeroes */
110 (*filter->output_function)(MBFL_BAD_INPUT, filter->data);
111 }
112 if (c == '-') {
113 if (filter->status == 1) { /* "+-" -> "+" */
114 CK((*filter->output_function)('+', filter->data));
115 }
116 } else if (c >= 0 && c < 0x80) { /* ASCII exclude '-' */
117 CK((*filter->output_function)(c, filter->data));
118 } else { /* illegal character */
119 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
120 }
121 filter->cache = filter->status = 0;
122 return 0;
123 }
124 }
125
126 switch (filter->status) {
127 /* directly encoded characters */
128 case 0:
129 if (c == '+') { /* '+' shift character */
130 filter->status = 1;
131 } else if (c >= 0 && c < 0x80) { /* ASCII */
132 CK((*filter->output_function)(c, filter->data));
133 } else { /* illegal character */
134 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
135 }
136 break;
137
138 /* decode Modified Base64 */
139 case 1:
140 case 2:
141 filter->cache |= n << 10;
142 filter->status = 3;
143 break;
144 case 3:
145 filter->cache |= n << 4;
146 filter->status = 4;
147 break;
148 case 4:
149 s = ((n >> 2) & 0xf) | (filter->cache & 0xffff);
150 n = (n & 0x3) << 14;
151 filter->status = 5;
152 if (s >= 0xd800 && s < 0xdc00) {
153 /* 1st part of surrogate pair */
154 if (filter->cache & 0xfff0000) {
155 /* We were waiting for the 2nd part of a surrogate pair */
156 (*filter->output_function)(MBFL_BAD_INPUT, filter->data);
157 }
158 s = (((s & 0x3ff) << 16) + 0x400000) | n;
159 filter->cache = s;
160 } else if (s >= 0xdc00 && s < 0xe000) {
161 /* 2nd part of surrogate pair */
162 if (filter->cache & 0xfff0000) {
163 s &= 0x3ff;
164 s |= (filter->cache & 0xfff0000) >> 6;
165 filter->cache = n;
166 if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
167 CK((*filter->output_function)(s, filter->data));
168 } else { /* illegal character */
169 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
170 }
171 } else {
172 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
173 filter->cache = n;
174 }
175 } else {
176 if (filter->cache & 0xfff0000) {
177 /* We were waiting for the 2nd part of a surrogate pair */
178 (*filter->output_function)(MBFL_BAD_INPUT, filter->data);
179 }
180 filter->cache = n;
181 CK((*filter->output_function)(s, filter->data));
182 }
183 break;
184
185 case 5:
186 filter->cache |= n << 8;
187 filter->status = 6;
188 break;
189 case 6:
190 filter->cache |= n << 2;
191 filter->status = 7;
192 break;
193 case 7:
194 s = ((n >> 4) & 0x3) | (filter->cache & 0xffff);
195 n = (n & 0xf) << 12;
196 filter->status = 8;
197 if (s >= 0xd800 && s < 0xdc00) {
198 if (filter->cache & 0xfff0000) {
199 /* We were waiting for the 2nd part of a surrogate pair */
200 (*filter->output_function)(MBFL_BAD_INPUT, filter->data);
201 }
202 s = (((s & 0x3ff) << 16) + 0x400000) | n;
203 filter->cache = s;
204 } else if (s >= 0xdc00 && s < 0xe000) {
205 /* 2nd part of surrogate pair */
206 if (filter->cache & 0xfff0000) {
207 s &= 0x3ff;
208 s |= (filter->cache & 0xfff0000) >> 6;
209 filter->cache = n;
210 if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
211 CK((*filter->output_function)(s, filter->data));
212 } else { /* illegal character */
213 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
214 }
215 } else {
216 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
217 filter->cache = n;
218 }
219 } else {
220 if (filter->cache & 0xfff0000) {
221 /* We were waiting for the 2nd part of a surrogate pair */
222 (*filter->output_function)(MBFL_BAD_INPUT, filter->data);
223 }
224 filter->cache = n;
225 CK((*filter->output_function)(s, filter->data));
226 }
227 break;
228
229 case 8:
230 filter->cache |= n << 6;
231 filter->status = 9;
232 break;
233 case 9:
234 s = n | (filter->cache & 0xffff);
235 filter->status = 2;
236 if (s >= 0xd800 && s < 0xdc00) {
237 if (filter->cache & 0xfff0000) {
238 /* We were waiting for the 2nd part of a surrogate pair */
239 (*filter->output_function)(MBFL_BAD_INPUT, filter->data);
240 }
241 s = (((s & 0x3ff) << 16) + 0x400000);
242 filter->cache = s;
243 } else if (s >= 0xdc00 && s < 0xe000) {
244 if (filter->cache & 0xfff0000) {
245 s &= 0x3ff;
246 s |= (filter->cache & 0xfff0000) >> 6;
247 filter->cache = 0;
248 if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
249 CK((*filter->output_function)(s, filter->data));
250 } else { /* illegal character */
251 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
252 }
253 } else {
254 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
255 filter->cache = 0;
256 }
257 } else {
258 if (filter->cache & 0xfff0000) {
259 /* We were waiting for the 2nd part of a surrogate pair */
260 (*filter->output_function)(MBFL_BAD_INPUT, filter->data);
261 }
262 filter->cache = 0;
263 CK((*filter->output_function)(s, filter->data));
264 }
265 break;
266
267 default:
268 filter->status = 0;
269 break;
270 }
271
272 return 0;
273 }
274
mbfl_filt_conv_utf7_wchar_flush(mbfl_convert_filter * filter)275 static int mbfl_filt_conv_utf7_wchar_flush(mbfl_convert_filter *filter)
276 {
277 if (filter->cache) {
278 /* Either we were expecting the 2nd half of a surrogate pair which
279 * never came, or else the last Base64 data was not padded with zeroes */
280 (*filter->output_function)(MBFL_BAD_INPUT, filter->data);
281 }
282
283 if (filter->flush_function) {
284 (*filter->flush_function)(filter->data);
285 }
286
287 return 0;
288 }
289
mbfl_filt_conv_wchar_utf7(int c,mbfl_convert_filter * filter)290 int mbfl_filt_conv_wchar_utf7(int c, mbfl_convert_filter *filter)
291 {
292 int s;
293
294 int n = 0;
295 if (c >= 0 && c < 0x80) { /* ASCII */
296 if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '\0' || c == '/' || c == '-') {
297 n = 1;
298 } else if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\'' || c == '(' || c == ')' || c == ',' || c == '.' || c == ':' || c == '?') {
299 n = 2;
300 }
301 } else if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
302 ;
303 } else if (c >= MBFL_WCSPLANE_SUPMIN && c < MBFL_WCSPLANE_SUPMAX) {
304 CK((*filter->filter_function)(((c >> 10) - 0x40) | 0xd800, filter));
305 CK((*filter->filter_function)((c & 0x3ff) | 0xdc00, filter));
306 return 0;
307 } else {
308 CK(mbfl_filt_conv_illegal_output(c, filter));
309 return 0;
310 }
311
312 switch (filter->status) {
313 case 0:
314 if (n != 0) { /* directly encode characters */
315 CK((*filter->output_function)(c, filter->data));
316 } else { /* Modified Base64 */
317 CK((*filter->output_function)('+', filter->data));
318 filter->status = 1;
319 filter->cache = c;
320 }
321 break;
322
323 /* encode Modified Base64 */
324 case 1:
325 s = filter->cache;
326 CK((*filter->output_function)(mbfl_base64_table[(s >> 10) & 0x3f], filter->data));
327 CK((*filter->output_function)(mbfl_base64_table[(s >> 4) & 0x3f], filter->data));
328 if (n != 0) {
329 CK((*filter->output_function)(mbfl_base64_table[(s << 2) & 0x3c], filter->data));
330 if (n == 1) {
331 CK((*filter->output_function)('-', filter->data));
332 }
333 CK((*filter->output_function)(c, filter->data));
334 filter->status = 0;
335 } else {
336 filter->status = 2;
337 filter->cache = ((s & 0xf) << 16) | c;
338 }
339 break;
340
341 case 2:
342 s = filter->cache;
343 CK((*filter->output_function)(mbfl_base64_table[(s >> 14) & 0x3f], filter->data));
344 CK((*filter->output_function)(mbfl_base64_table[(s >> 8) & 0x3f], filter->data));
345 CK((*filter->output_function)(mbfl_base64_table[(s >> 2) & 0x3f], filter->data));
346 if (n != 0) {
347 CK((*filter->output_function)(mbfl_base64_table[(s << 4) & 0x30], filter->data));
348 if (n == 1) {
349 CK((*filter->output_function)('-', filter->data));
350 }
351 CK((*filter->output_function)(c, filter->data));
352 filter->status = 0;
353 } else {
354 filter->status = 3;
355 filter->cache = ((s & 0x3) << 16) | c;
356 }
357 break;
358
359 case 3:
360 s = filter->cache;
361 CK((*filter->output_function)(mbfl_base64_table[(s >> 12) & 0x3f], filter->data));
362 CK((*filter->output_function)(mbfl_base64_table[(s >> 6) & 0x3f], filter->data));
363 CK((*filter->output_function)(mbfl_base64_table[s & 0x3f], filter->data));
364 if (n != 0) {
365 if (n == 1) {
366 CK((*filter->output_function)('-', filter->data));
367 }
368 CK((*filter->output_function)(c, filter->data));
369 filter->status = 0;
370 } else {
371 filter->status = 1;
372 filter->cache = c;
373 }
374 break;
375
376 default:
377 filter->status = 0;
378 break;
379 }
380
381 return 0;
382 }
383
mbfl_filt_conv_wchar_utf7_flush(mbfl_convert_filter * filter)384 int mbfl_filt_conv_wchar_utf7_flush(mbfl_convert_filter *filter)
385 {
386 int status = filter->status;
387 int cache = filter->cache;
388
389 /* flush fragments */
390 switch (status) {
391 case 1:
392 CK((*filter->output_function)(mbfl_base64_table[(cache >> 10) & 0x3f], filter->data));
393 CK((*filter->output_function)(mbfl_base64_table[(cache >> 4) & 0x3f], filter->data));
394 CK((*filter->output_function)(mbfl_base64_table[(cache << 2) & 0x3c], filter->data));
395 CK((*filter->output_function)('-', filter->data));
396 break;
397
398 case 2:
399 CK((*filter->output_function)(mbfl_base64_table[(cache >> 14) & 0x3f], filter->data));
400 CK((*filter->output_function)(mbfl_base64_table[(cache >> 8) & 0x3f], filter->data));
401 CK((*filter->output_function)(mbfl_base64_table[(cache >> 2) & 0x3f], filter->data));
402 CK((*filter->output_function)(mbfl_base64_table[(cache << 4) & 0x30], filter->data));
403 CK((*filter->output_function)('-', filter->data));
404 break;
405
406 case 3:
407 CK((*filter->output_function)(mbfl_base64_table[(cache >> 12) & 0x3f], filter->data));
408 CK((*filter->output_function)(mbfl_base64_table[(cache >> 6) & 0x3f], filter->data));
409 CK((*filter->output_function)(mbfl_base64_table[cache & 0x3f], filter->data));
410 CK((*filter->output_function)('-', filter->data));
411 break;
412 }
413
414 if (filter->flush_function) {
415 (*filter->flush_function)(filter->data);
416 }
417
418 return 0;
419 }
420