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 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 
34 #include "mbfilter.h"
35 #include "mbfilter_utf7.h"
36 
37 static int mbfl_filt_ident_utf7(int c, mbfl_identify_filter *filter);
38 
39 static const unsigned char mbfl_base64_table[] = {
40  /* 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', */
41    0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,
42  /* 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', */
43    0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,
44  /* 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', */
45    0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,
46  /* 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', */
47    0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,
48  /* '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0' */
49    0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x2b,0x2f,0x00
50 };
51 
52 static const char *mbfl_encoding_utf7_aliases[] = {"utf7", NULL};
53 
54 const mbfl_encoding mbfl_encoding_utf7 = {
55 	mbfl_no_encoding_utf7,
56 	"UTF-7",
57 	"UTF-7",
58 	(const char *(*)[])&mbfl_encoding_utf7_aliases,
59 	NULL,
60 	MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_SHFTCODE | MBFL_ENCTYPE_GL_UNSAFE,
61 	&vtbl_utf7_wchar,
62 	&vtbl_wchar_utf7
63 };
64 
65 const struct mbfl_identify_vtbl vtbl_identify_utf7 = {
66 	mbfl_no_encoding_utf7,
67 	mbfl_filt_ident_common_ctor,
68 	mbfl_filt_ident_common_dtor,
69 	mbfl_filt_ident_utf7
70 };
71 
72 const struct mbfl_convert_vtbl vtbl_utf7_wchar = {
73 	mbfl_no_encoding_utf7,
74 	mbfl_no_encoding_wchar,
75 	mbfl_filt_conv_common_ctor,
76 	mbfl_filt_conv_common_dtor,
77 	mbfl_filt_conv_utf7_wchar,
78 	mbfl_filt_conv_common_flush
79 };
80 
81 const struct mbfl_convert_vtbl vtbl_wchar_utf7 = {
82 	mbfl_no_encoding_wchar,
83 	mbfl_no_encoding_utf7,
84 	mbfl_filt_conv_common_ctor,
85 	mbfl_filt_conv_common_dtor,
86 	mbfl_filt_conv_wchar_utf7,
87 	mbfl_filt_conv_wchar_utf7_flush
88 };
89 
90 
91 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
92 
93 /*
94  * UTF-7 => wchar
95  */
mbfl_filt_conv_utf7_wchar(int c,mbfl_convert_filter * filter)96 int mbfl_filt_conv_utf7_wchar(int c, mbfl_convert_filter *filter)
97 {
98 	int s, n;
99 
100 	n = -1;
101 	if (filter->status != 0) {		/* Modified Base64 */
102 		if (c >= 0x41 && c <= 0x5a) {		/* A - Z */
103 			n = c - 65;
104 		} else if (c >= 0x61 && c <= 0x7a) {	/* a - z */
105 			n = c - 71;
106 		} else if (c >= 0x30 && c <= 0x39) {	/* 0 - 9 */
107 			n = c + 4;
108 		} else if (c == 0x2b) {			/* '+' */
109 			n = 62;
110 		} else if (c == 0x2f) {			/* '/' */
111 			n = 63;
112 		}
113 		if (n < 0 || n > 63) {
114 			if (c == 0x2d) {
115 				if (filter->status == 1) {		/* "+-" -> "+" */
116 					CK((*filter->output_function)(0x2b, filter->data));
117 				}
118 			} else if (c >= 0 && c < 0x80) {	/* ASCII exclude '-' */
119 				CK((*filter->output_function)(c, filter->data));
120 			} else {		/* illegal character */
121 				s = c & MBFL_WCSGROUP_MASK;
122 				s |= MBFL_WCSGROUP_THROUGH;
123 				CK((*filter->output_function)(s, filter->data));
124 			}
125 			filter->cache = 0;
126 			filter->status = 0;
127 			return c;
128 		}
129 	}
130 
131 	switch (filter->status) {
132 	/* directly encoded characters */
133 	case 0:
134 		if (c == 0x2b) {	/* '+'  shift character */
135 			filter->status = 1;
136 		} else if (c >= 0 && c < 0x80) {	/* ASCII */
137 			CK((*filter->output_function)(c, filter->data));
138 		} else {		/* illegal character */
139 			s = c & MBFL_WCSGROUP_MASK;
140 			s |= MBFL_WCSGROUP_THROUGH;
141 			CK((*filter->output_function)(s, filter->data));
142 		}
143 		break;
144 
145 	/* decode Modified Base64 */
146 	case 1:
147 	case 2:
148 		filter->cache |= n << 10;
149 		filter->status = 3;
150 		break;
151 	case 3:
152 		filter->cache |= n << 4;
153 		filter->status = 4;
154 		break;
155 	case 4:
156 		s = ((n >> 2) & 0xf) | (filter->cache & 0xffff);
157 		n = (n & 0x3) << 14;
158 		filter->status = 5;
159 		if (s >= 0xd800 && s < 0xdc00) {
160 			s = (((s & 0x3ff) << 16) + 0x400000) | n;
161 			filter->cache = s;
162 		} else if (s >= 0xdc00 && s < 0xe000) {
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 				s &= MBFL_WCSGROUP_MASK;
170 				s |= MBFL_WCSGROUP_THROUGH;
171 				CK((*filter->output_function)(s, filter->data));
172 			}
173 		} else {
174 			filter->cache = n;
175 			CK((*filter->output_function)(s, filter->data));
176 		}
177 		break;
178 
179 	case 5:
180 		filter->cache |= n << 8;
181 		filter->status = 6;
182 		break;
183 	case 6:
184 		filter->cache |= n << 2;
185 		filter->status = 7;
186 		break;
187 	case 7:
188 		s = ((n >> 4) & 0x3) | (filter->cache & 0xffff);
189 		n = (n & 0xf) << 12;
190 		filter->status = 8;
191 		if (s >= 0xd800 && s < 0xdc00) {
192 			s = (((s & 0x3ff) << 16) + 0x400000) | n;
193 			filter->cache = s;
194 		} else if (s >= 0xdc00 && s < 0xe000) {
195 			s &= 0x3ff;
196 			s |= (filter->cache & 0xfff0000) >> 6;
197 			filter->cache = n;
198 			if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
199 				CK((*filter->output_function)(s, filter->data));
200 			} else {		/* illegal character */
201 				s &= MBFL_WCSGROUP_MASK;
202 				s |= MBFL_WCSGROUP_THROUGH;
203 				CK((*filter->output_function)(s, filter->data));
204 			}
205 		} else {
206 			filter->cache = n;
207 			CK((*filter->output_function)(s, filter->data));
208 		}
209 		break;
210 
211 	case 8:
212 		filter->cache |= n << 6;
213 		filter->status = 9;
214 		break;
215 	case 9:
216 		s = n | (filter->cache & 0xffff);
217 		filter->status = 2;
218 		if (s >= 0xd800 && s < 0xdc00) {
219 			s = (((s & 0x3ff) << 16) + 0x400000);
220 			filter->cache = s;
221 		} else if (s >= 0xdc00 && s < 0xe000) {
222 			s &= 0x3ff;
223 			s |= (filter->cache & 0xfff0000) >> 6;
224 			filter->cache = 0;
225 			if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
226 				CK((*filter->output_function)(s, filter->data));
227 			} else {		/* illegal character */
228 				s &= MBFL_WCSGROUP_MASK;
229 				s |= MBFL_WCSGROUP_THROUGH;
230 				CK((*filter->output_function)(s, filter->data));
231 			}
232 		} else {
233 			filter->cache = 0;
234 			CK((*filter->output_function)(s, filter->data));
235 		}
236 		break;
237 
238 	default:
239 		filter->status = 0;
240 		break;
241 	}
242 
243 	return c;
244 }
245 
246 /*
247  * wchar => UTF-7
248  */
mbfl_filt_conv_wchar_utf7(int c,mbfl_convert_filter * filter)249 int mbfl_filt_conv_wchar_utf7(int c, mbfl_convert_filter *filter)
250 {
251 	int s, n;
252 
253 	n = 0;
254 	if (c >= 0 && c < 0x80) {	/* ASCII */
255 		if (c >= 0x41 && c <= 0x5a) {		/* A - Z */
256 			n = 1;
257 		} else if (c >= 0x61 && c <= 0x7a) {	/* a - z */
258 			n = 1;
259 		} else if (c >= 0x30 && c <= 0x39) {	/* 0 - 9 */
260 			n = 1;
261 		} else if (c == '\0') {			/* '\0' */
262 			n = 1;
263 		} else if (c == 0x2f) {			/* '/' */
264 			n = 1;
265 		} else if (c == 0x2d) {			/* '-' */
266 			n = 1;
267 		} else if (c == 0x20) {			/* SPACE */
268 			n = 2;
269 		} else if (c == 0x09) {			/* HTAB */
270 			n = 2;
271 		} else if (c == 0x0d) {			/* CR */
272 			n = 2;
273 		} else if (c == 0x0a) {			/* LF */
274 			n = 2;
275 		} else if (c == 0x27) {			/* "'" */
276 			n = 2;
277 		} else if (c == 0x28) {			/* '(' */
278 			n = 2;
279 		} else if (c == 0x29) {			/* ')' */
280 			n = 2;
281 		} else if (c == 0x2c) {			/* ',' */
282 			n = 2;
283 		} else if (c == 0x2e) {			/* '.' */
284 			n = 2;
285 		} else if (c == 0x3a) {			/* ':' */
286 			n = 2;
287 		} else if (c == 0x3f) {			/* '?' */
288 			n = 2;
289 		}
290 	} else if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
291 		;
292 	} else if (c >= MBFL_WCSPLANE_SUPMIN && c < MBFL_WCSPLANE_SUPMAX) {
293 		s = ((c >> 10) - 0x40) | 0xd800;
294 		CK((*filter->filter_function)(s, filter));
295 		s = (c & 0x3ff) | 0xdc00;
296 		CK((*filter->filter_function)(s, filter));
297 		return c;
298 	} else {
299 		if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
300 			CK(mbfl_filt_conv_illegal_output(c, filter));
301 		}
302 		return c;
303 	}
304 
305 	switch (filter->status) {
306 	case 0:
307 		if (n != 0) {	/* directly encode characters */
308 			CK((*filter->output_function)(c, filter->data));
309 		} else {	/* Modified Base64 */
310 			CK((*filter->output_function)(0x2b, filter->data));		/* '+' */
311 			filter->status++;
312 			filter->cache = c;
313 		}
314 		break;
315 
316 	/* encode Modified Base64 */
317 	case 1:
318 		s = filter->cache;
319 		CK((*filter->output_function)(mbfl_base64_table[(s >> 10) & 0x3f], filter->data));
320 		CK((*filter->output_function)(mbfl_base64_table[(s >> 4) & 0x3f], filter->data));
321 		if (n != 0) {
322 			CK((*filter->output_function)(mbfl_base64_table[(s << 2) & 0x3c], filter->data));
323 			if (n == 1) {
324 				CK((*filter->output_function)(0x2d, filter->data));		/* '-' */
325 			}
326 			CK((*filter->output_function)(c, filter->data));
327 			filter->status = 0;
328 		} else {
329 			filter->status++;
330 			filter->cache = ((s & 0xf) << 16) | c;
331 		}
332 		break;
333 
334 	case 2:
335 		s = filter->cache;
336 		CK((*filter->output_function)(mbfl_base64_table[(s >> 14) & 0x3f], filter->data));
337 		CK((*filter->output_function)(mbfl_base64_table[(s >> 8) & 0x3f], filter->data));
338 		CK((*filter->output_function)(mbfl_base64_table[(s >> 2) & 0x3f], filter->data));
339 		if (n != 0) {
340 			CK((*filter->output_function)(mbfl_base64_table[(s << 4) & 0x30], filter->data));
341 			if (n == 1) {
342 				CK((*filter->output_function)(0x2d, filter->data));		/* '-' */
343 			}
344 			CK((*filter->output_function)(c, filter->data));
345 			filter->status = 0;
346 		} else {
347 			filter->status++;
348 			filter->cache = ((s & 0x3) << 16) | c;
349 		}
350 		break;
351 
352 	case 3:
353 		s = filter->cache;
354 		CK((*filter->output_function)(mbfl_base64_table[(s >> 12) & 0x3f], filter->data));
355 		CK((*filter->output_function)(mbfl_base64_table[(s >> 6) & 0x3f], filter->data));
356 		CK((*filter->output_function)(mbfl_base64_table[s & 0x3f], filter->data));
357 		if (n != 0) {
358 			if (n == 1) {
359 				CK((*filter->output_function)(0x2d, filter->data));		/* '-' */
360 			}
361 			CK((*filter->output_function)(c, filter->data));
362 			filter->status = 0;
363 		} else {
364 			filter->status = 1;
365 			filter->cache = c;
366 		}
367 		break;
368 
369 	default:
370 		filter->status = 0;
371 		break;
372 	}
373 
374 	return c;
375 
376 }
377 
mbfl_filt_conv_wchar_utf7_flush(mbfl_convert_filter * filter)378 int mbfl_filt_conv_wchar_utf7_flush(mbfl_convert_filter *filter)
379 {
380 	int status, cache;
381 
382 	status = filter->status;
383 	cache = filter->cache;
384 	filter->status = 0;
385 	filter->cache = 0;
386 	/* flush fragments */
387 	switch (status) {
388 	case 1:
389 		CK((*filter->output_function)(mbfl_base64_table[(cache >> 10) & 0x3f], filter->data));
390 		CK((*filter->output_function)(mbfl_base64_table[(cache >> 4) & 0x3f], filter->data));
391 		CK((*filter->output_function)(mbfl_base64_table[(cache << 2) & 0x3c], filter->data));
392 		CK((*filter->output_function)(0x2d, filter->data));		/* '-' */
393 		break;
394 
395 	case 2:
396 		CK((*filter->output_function)(mbfl_base64_table[(cache >> 14) & 0x3f], filter->data));
397 		CK((*filter->output_function)(mbfl_base64_table[(cache >> 8) & 0x3f], filter->data));
398 		CK((*filter->output_function)(mbfl_base64_table[(cache >> 2) & 0x3f], filter->data));
399 		CK((*filter->output_function)(mbfl_base64_table[(cache << 4) & 0x30], filter->data));
400 		CK((*filter->output_function)(0x2d, filter->data));		/* '-' */
401 		break;
402 
403 	case 3:
404 		CK((*filter->output_function)(mbfl_base64_table[(cache >> 12) & 0x3f], filter->data));
405 		CK((*filter->output_function)(mbfl_base64_table[(cache >> 6) & 0x3f], filter->data));
406 		CK((*filter->output_function)(mbfl_base64_table[cache & 0x3f], filter->data));
407 		CK((*filter->output_function)(0x2d, filter->data));		/* '-' */
408 		break;
409 	}
410 
411 	if (filter->flush_function != NULL) {
412 		(*filter->flush_function)(filter->data);
413 	}
414 
415 	return 0;
416 }
417 
mbfl_filt_ident_utf7(int c,mbfl_identify_filter * filter)418 static int mbfl_filt_ident_utf7(int c, mbfl_identify_filter *filter)
419 {
420 	int n;
421 
422 	switch (filter->status) {
423 	/* directly encoded characters */
424 	case 0:
425 		if (c == 0x2b) {	/* '+'  shift character */
426 			filter->status++;
427 		} else if (c == 0x5c || c == 0x7e || c < 0 || c > 0x7f) {	/* illegal character */
428 			filter->flag = 1;	/* bad */
429 		}
430 		break;
431 
432 	/* Modified Base64 */
433 	case 1:
434 	case 2:
435 		n = 0;
436 		if (c >= 0x41 && c <= 0x5a) {		/* A - Z */
437 			n = 1;
438 		} else if (c >= 0x61 && c <= 0x7a) {	/* a - z */
439 			n = 1;
440 		} else if (c >= 0x30 && c <= 0x39) {	/* 0 - 9 */
441 			n = 1;
442 		} else if (c == 0x2b) {			/* '+' */
443 			n = 1;
444 		} else if (c == 0x2f) {			/* '/' */
445 			n = 1;
446 		}
447 		if (n <= 0) {
448 			if (filter->status == 1 && c != 0x2d) {
449 				filter->flag = 1;	/* bad */
450 			} else if (c < 0 || c > 0x7f) {
451 				filter->flag = 1;	/* bad */
452 			}
453 			filter->status = 0;
454 		} else {
455 			filter->status = 2;
456 		}
457 		break;
458 
459 	default:
460 		filter->status = 0;
461 		break;
462 	}
463 
464 	return c;
465 }
466