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 20 Dec 2002. The file
27  * mbfilter.c is included in this package .
28  *
29  */
30 
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 
35 #include <stddef.h>
36 
37 #include "mbfl_encoding.h"
38 #include "mbfl_allocators.h"
39 #include "mbfl_filter_output.h"
40 #include "mbfilter_pass.h"
41 #include "mbfilter_8bit.h"
42 #include "mbfilter_wchar.h"
43 
44 #include "filters/mbfilter_euc_cn.h"
45 #include "filters/mbfilter_hz.h"
46 #include "filters/mbfilter_euc_tw.h"
47 #include "filters/mbfilter_big5.h"
48 #include "filters/mbfilter_uhc.h"
49 #include "filters/mbfilter_euc_kr.h"
50 #include "filters/mbfilter_iso2022_kr.h"
51 #include "filters/mbfilter_sjis.h"
52 #include "filters/mbfilter_sjis_open.h"
53 #include "filters/mbfilter_sjis_2004.h"
54 #include "filters/mbfilter_sjis_mobile.h"
55 #include "filters/mbfilter_sjis_mac.h"
56 #include "filters/mbfilter_cp51932.h"
57 #include "filters/mbfilter_jis.h"
58 #include "filters/mbfilter_iso2022_jp_ms.h"
59 #include "filters/mbfilter_iso2022jp_2004.h"
60 #include "filters/mbfilter_iso2022jp_mobile.h"
61 #include "filters/mbfilter_euc_jp.h"
62 #include "filters/mbfilter_euc_jp_2004.h"
63 #include "filters/mbfilter_euc_jp_win.h"
64 #include "filters/mbfilter_gb18030.h"
65 #include "filters/mbfilter_ascii.h"
66 #include "filters/mbfilter_koi8r.h"
67 #include "filters/mbfilter_koi8u.h"
68 #include "filters/mbfilter_cp866.h"
69 #include "filters/mbfilter_cp932.h"
70 #include "filters/mbfilter_cp936.h"
71 #include "filters/mbfilter_cp1251.h"
72 #include "filters/mbfilter_cp1252.h"
73 #include "filters/mbfilter_cp1254.h"
74 #include "filters/mbfilter_cp5022x.h"
75 #include "filters/mbfilter_iso8859_1.h"
76 #include "filters/mbfilter_iso8859_2.h"
77 #include "filters/mbfilter_iso8859_3.h"
78 #include "filters/mbfilter_iso8859_4.h"
79 #include "filters/mbfilter_iso8859_5.h"
80 #include "filters/mbfilter_iso8859_6.h"
81 #include "filters/mbfilter_iso8859_7.h"
82 #include "filters/mbfilter_iso8859_8.h"
83 #include "filters/mbfilter_iso8859_9.h"
84 #include "filters/mbfilter_iso8859_10.h"
85 #include "filters/mbfilter_iso8859_13.h"
86 #include "filters/mbfilter_iso8859_14.h"
87 #include "filters/mbfilter_iso8859_15.h"
88 #include "filters/mbfilter_base64.h"
89 #include "filters/mbfilter_qprint.h"
90 #include "filters/mbfilter_uuencode.h"
91 #include "filters/mbfilter_7bit.h"
92 #include "filters/mbfilter_utf7.h"
93 #include "filters/mbfilter_utf7imap.h"
94 #include "filters/mbfilter_utf8.h"
95 #include "filters/mbfilter_utf8_mobile.h"
96 #include "filters/mbfilter_utf16.h"
97 #include "filters/mbfilter_utf32.h"
98 #include "filters/mbfilter_byte2.h"
99 #include "filters/mbfilter_byte4.h"
100 #include "filters/mbfilter_ucs4.h"
101 #include "filters/mbfilter_ucs2.h"
102 #include "filters/mbfilter_htmlent.h"
103 #include "filters/mbfilter_armscii8.h"
104 #include "filters/mbfilter_cp850.h"
105 
106 /* hex character table "0123456789ABCDEF" */
107 static char mbfl_hexchar_table[] = {
108 	0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,0x45,0x46
109 };
110 
111 static const struct mbfl_convert_vtbl *mbfl_special_filter_list[] = {
112 	&vtbl_8bit_b64,
113 	&vtbl_b64_8bit,
114 	&vtbl_uuencode_8bit,
115 	&vtbl_8bit_qprint,
116 	&vtbl_qprint_8bit,
117 	&vtbl_8bit_7bit,
118 	&vtbl_7bit_8bit,
119 	&vtbl_pass,
120 	NULL
121 };
122 
123 static int
mbfl_convert_filter_common_init(mbfl_convert_filter * filter,const mbfl_encoding * from,const mbfl_encoding * to,const struct mbfl_convert_vtbl * vtbl,int (* output_function)(int,void *),int (* flush_function)(void *),void * data)124 mbfl_convert_filter_common_init(
125 	mbfl_convert_filter *filter,
126 	const mbfl_encoding *from,
127 	const mbfl_encoding *to,
128 	const struct mbfl_convert_vtbl *vtbl,
129     int (*output_function)(int, void* ),
130     int (*flush_function)(void*),
131     void* data)
132 {
133 	/* encoding structure */
134 	filter->from = from;
135 	filter->to = to;
136 
137 	if (output_function != NULL) {
138 		filter->output_function = output_function;
139 	} else {
140 		filter->output_function = mbfl_filter_output_null;
141 	}
142 
143 	filter->flush_function = flush_function;
144 	filter->data = data;
145 	filter->illegal_mode = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;
146 	filter->illegal_substchar = 0x3f;		/* '?' */
147 	filter->num_illegalchar = 0;
148 	filter->filter_ctor = vtbl->filter_ctor;
149 	filter->filter_dtor = vtbl->filter_dtor;
150 	filter->filter_function = vtbl->filter_function;
151 	filter->filter_flush = vtbl->filter_flush;
152 	filter->filter_copy = vtbl->filter_copy;
153 
154 	(*filter->filter_ctor)(filter);
155 
156 	return 0;
157 }
158 
159 
160 mbfl_convert_filter *
mbfl_convert_filter_new(const mbfl_encoding * from,const mbfl_encoding * to,int (* output_function)(int,void *),int (* flush_function)(void *),void * data)161 mbfl_convert_filter_new(
162     const mbfl_encoding *from,
163     const mbfl_encoding *to,
164     int (*output_function)(int, void* ),
165     int (*flush_function)(void*),
166     void* data)
167 {
168 	mbfl_convert_filter * filter;
169 	const struct mbfl_convert_vtbl *vtbl;
170 
171 	vtbl = mbfl_convert_filter_get_vtbl(from, to);
172 	if (vtbl == NULL) {
173 		return NULL;
174 	}
175 
176 	/* allocate */
177 	filter = (mbfl_convert_filter *)mbfl_malloc(sizeof(mbfl_convert_filter));
178 	if (filter == NULL) {
179 		return NULL;
180 	}
181 
182 	if (mbfl_convert_filter_common_init(filter, from, to, vtbl,
183 			output_function, flush_function, data)) {
184 		mbfl_free(filter);
185 		return NULL;
186 	}
187 
188 	return filter;
189 }
190 
191 mbfl_convert_filter *
mbfl_convert_filter_new2(const struct mbfl_convert_vtbl * vtbl,int (* output_function)(int,void *),int (* flush_function)(void *),void * data)192 mbfl_convert_filter_new2(
193 	const struct mbfl_convert_vtbl *vtbl,
194     int (*output_function)(int, void* ),
195     int (*flush_function)(void*),
196     void* data)
197 {
198 	mbfl_convert_filter * filter;
199 	const mbfl_encoding *from_encoding, *to_encoding;
200 
201 	if (vtbl == NULL) {
202 		vtbl = &vtbl_pass;
203 	}
204 
205 	from_encoding = mbfl_no2encoding(vtbl->from);
206 	to_encoding = mbfl_no2encoding(vtbl->to);
207 
208 	/* allocate */
209 	filter = (mbfl_convert_filter *)mbfl_malloc(sizeof(mbfl_convert_filter));
210 	if (filter == NULL) {
211 		return NULL;
212 	}
213 
214 	if (mbfl_convert_filter_common_init(filter, from_encoding, to_encoding, vtbl,
215 			output_function, flush_function, data)) {
216 		mbfl_free(filter);
217 		return NULL;
218 	}
219 
220 	return filter;
221 }
222 
223 void
mbfl_convert_filter_delete(mbfl_convert_filter * filter)224 mbfl_convert_filter_delete(mbfl_convert_filter *filter)
225 {
226 	if (filter) {
227 		(*filter->filter_dtor)(filter);
228 		mbfl_free((void*)filter);
229 	}
230 }
231 
232 int
mbfl_convert_filter_feed(int c,mbfl_convert_filter * filter)233 mbfl_convert_filter_feed(int c, mbfl_convert_filter *filter)
234 {
235 	return (*filter->filter_function)(c, filter);
236 }
237 
238 int
mbfl_convert_filter_feed_string(mbfl_convert_filter * filter,const unsigned char * p,size_t len)239 mbfl_convert_filter_feed_string(mbfl_convert_filter *filter, const unsigned char *p, size_t len) {
240 	while (len > 0) {
241 		if ((*filter->filter_function)(*p++, filter) < 0) {
242 			return -1;
243 		}
244 		len--;
245 	}
246 	return 0;
247 }
248 
249 int
mbfl_convert_filter_flush(mbfl_convert_filter * filter)250 mbfl_convert_filter_flush(mbfl_convert_filter *filter)
251 {
252 	(*filter->filter_flush)(filter);
253 	return (filter->flush_function ? (*filter->flush_function)(filter->data) : 0);
254 }
255 
mbfl_convert_filter_reset(mbfl_convert_filter * filter,const mbfl_encoding * from,const mbfl_encoding * to)256 void mbfl_convert_filter_reset(mbfl_convert_filter *filter,
257 	    const mbfl_encoding *from, const mbfl_encoding *to)
258 {
259 	const struct mbfl_convert_vtbl *vtbl;
260 
261 	/* destruct old filter */
262 	(*filter->filter_dtor)(filter);
263 
264 	vtbl = mbfl_convert_filter_get_vtbl(from, to);
265 
266 	if (vtbl == NULL) {
267 		vtbl = &vtbl_pass;
268 	}
269 
270 	mbfl_convert_filter_common_init(filter, from, to, vtbl,
271 			filter->output_function, filter->flush_function, filter->data);
272 }
273 
274 void
mbfl_convert_filter_copy(mbfl_convert_filter * src,mbfl_convert_filter * dest)275 mbfl_convert_filter_copy(
276     mbfl_convert_filter *src,
277     mbfl_convert_filter *dest)
278 {
279 	if (src->filter_copy != NULL) {
280 		src->filter_copy(src, dest);
281 		return;
282 	}
283 
284 	*dest = *src;
285 }
286 
mbfl_convert_filter_devcat(mbfl_convert_filter * filter,mbfl_memory_device * src)287 int mbfl_convert_filter_devcat(mbfl_convert_filter *filter, mbfl_memory_device *src)
288 {
289 	size_t n;
290 	unsigned char *p;
291 
292 	p = src->buffer;
293 	n = src->pos;
294 	while (n > 0) {
295 		if ((*filter->filter_function)(*p++, filter) < 0) {
296 			return -1;
297 		}
298 		n--;
299 	}
300 
301 	return 0;
302 }
303 
mbfl_convert_filter_strcat(mbfl_convert_filter * filter,const unsigned char * p)304 int mbfl_convert_filter_strcat(mbfl_convert_filter *filter, const unsigned char *p)
305 {
306 	int c;
307 
308 	while ((c = *p++) != '\0') {
309 		if ((*filter->filter_function)(c, filter) < 0) {
310 			return -1;
311 		}
312 	}
313 
314 	return 0;
315 }
316 
317 /* illegal character output function for conv-filter */
318 int
mbfl_filt_conv_illegal_output(int c,mbfl_convert_filter * filter)319 mbfl_filt_conv_illegal_output(int c, mbfl_convert_filter *filter)
320 {
321 	int mode_backup, substchar_backup, ret, n, m, r;
322 
323 	ret = 0;
324 
325 	mode_backup = filter->illegal_mode;
326 	substchar_backup = filter->illegal_substchar;
327 
328 	/* The used substitution character may not be supported by the target character encoding.
329 	 * If that happens, first try to use "?" instead and if that also fails, silently drop the
330 	 * character. */
331 	if (filter->illegal_mode == MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR
332 			&& filter->illegal_substchar != 0x3f) {
333 		filter->illegal_substchar = 0x3f;
334 	} else {
335 		filter->illegal_mode = MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE;
336 	}
337 
338 	switch (mode_backup) {
339 	case MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR:
340 		ret = (*filter->filter_function)(substchar_backup, filter);
341 		break;
342 	case MBFL_OUTPUTFILTER_ILLEGAL_MODE_LONG:
343 		if (c >= 0) {
344 			if (c < MBFL_WCSGROUP_UCS4MAX) {	/* unicode */
345 				ret = mbfl_convert_filter_strcat(filter, (const unsigned char *)"U+");
346 			} else {
347 				if (c < MBFL_WCSGROUP_WCHARMAX) {
348 					m = c & ~MBFL_WCSPLANE_MASK;
349 					switch (m) {
350 					case MBFL_WCSPLANE_JIS0208:
351 						ret = mbfl_convert_filter_strcat(filter, (const unsigned char *)"JIS+");
352 						break;
353 					case MBFL_WCSPLANE_JIS0212:
354 						ret = mbfl_convert_filter_strcat(filter, (const unsigned char *)"JIS2+");
355 						break;
356 					case MBFL_WCSPLANE_JIS0213:
357 						ret = mbfl_convert_filter_strcat(filter, (const unsigned char *)"JIS3+");
358 						break;
359 					case MBFL_WCSPLANE_WINCP932:
360 						ret = mbfl_convert_filter_strcat(filter, (const unsigned char *)"W932+");
361 						break;
362 					case MBFL_WCSPLANE_GB18030:
363 						ret = mbfl_convert_filter_strcat(filter, (const unsigned char *)"GB+");
364 						break;
365 					case MBFL_WCSPLANE_8859_1:
366 						ret = mbfl_convert_filter_strcat(filter, (const unsigned char *)"I8859_1+");
367 						break;
368 					default:
369 						ret = mbfl_convert_filter_strcat(filter, (const unsigned char *)"?+");
370 						break;
371 					}
372 					c &= MBFL_WCSPLANE_MASK;
373 				} else {
374 					ret = mbfl_convert_filter_strcat(filter, (const unsigned char *)"BAD+");
375 					c &= MBFL_WCSGROUP_MASK;
376 				}
377 			}
378 			if (ret >= 0) {
379 				m = 0;
380 				r = 28;
381 				while (r >= 0) {
382 					n = (c >> r) & 0xf;
383 					if (n || m) {
384 						m = 1;
385 						ret = (*filter->filter_function)(mbfl_hexchar_table[n], filter);
386 						if (ret < 0) {
387 							break;
388 						}
389 					}
390 					r -= 4;
391 				}
392 				if (m == 0 && ret >= 0) {
393 					ret = (*filter->filter_function)(mbfl_hexchar_table[0], filter);
394 				}
395 			}
396 		}
397 		break;
398 	case MBFL_OUTPUTFILTER_ILLEGAL_MODE_ENTITY:
399 		if (c >= 0) {
400 			if (c < MBFL_WCSGROUP_UCS4MAX) {	/* unicode */
401 				ret = mbfl_convert_filter_strcat(filter, (const unsigned char *)"&#x");
402 				if (ret < 0)
403 					break;
404 
405 				m = 0;
406 				r = 28;
407 				while (r >= 0) {
408 					n = (c >> r) & 0xf;
409 					if (n || m) {
410 						m = 1;
411 						ret = (*filter->filter_function)(mbfl_hexchar_table[n], filter);
412 						if (ret < 0) {
413 							break;
414 						}
415 					}
416 					r -= 4;
417 				}
418 				if (ret < 0) {
419 					break;
420 				}
421 				if (m == 0) {
422 					ret = (*filter->filter_function)(mbfl_hexchar_table[0], filter);
423 				}
424 				ret = mbfl_convert_filter_strcat(filter, (const unsigned char *)";");
425 			} else {
426 				ret = (*filter->filter_function)(substchar_backup, filter);
427 			}
428 		}
429 		break;
430 	case MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE:
431 	default:
432 		break;
433 	}
434 
435 	filter->illegal_mode = mode_backup;
436 	filter->illegal_substchar = substchar_backup;
437 	filter->num_illegalchar++;
438 
439 	return ret;
440 }
441 
mbfl_convert_filter_get_vtbl(const mbfl_encoding * from,const mbfl_encoding * to)442 const struct mbfl_convert_vtbl * mbfl_convert_filter_get_vtbl(
443 		const mbfl_encoding *from, const mbfl_encoding *to)
444 {
445 	if (to->no_encoding == mbfl_no_encoding_base64 ||
446 	    to->no_encoding == mbfl_no_encoding_qprint ||
447 	    to->no_encoding == mbfl_no_encoding_7bit) {
448 		from = &mbfl_encoding_8bit;
449 	} else if (from->no_encoding == mbfl_no_encoding_base64 ||
450 			   from->no_encoding == mbfl_no_encoding_qprint ||
451 			   from->no_encoding == mbfl_no_encoding_uuencode) {
452 		to = &mbfl_encoding_8bit;
453 	}
454 
455 	if (to == from && (to == &mbfl_encoding_wchar || to == &mbfl_encoding_8bit)) {
456 		return &vtbl_pass;
457 	}
458 
459 	if (to->no_encoding == mbfl_no_encoding_wchar) {
460 		return from->input_filter;
461 	} else if (from->no_encoding == mbfl_no_encoding_wchar) {
462 		return to->output_filter;
463 	} else {
464 		int i = 0;
465 		const struct mbfl_convert_vtbl *vtbl;
466 		while ((vtbl = mbfl_special_filter_list[i++]) != NULL){
467 			if (vtbl->from == from->no_encoding && vtbl->to == to->no_encoding) {
468 				return vtbl;
469 			}
470 		}
471 		return NULL;
472 	}
473 }
474 
475 /*
476  * commonly used constructor and destructor
477  */
mbfl_filt_conv_common_ctor(mbfl_convert_filter * filter)478 void mbfl_filt_conv_common_ctor(mbfl_convert_filter *filter)
479 {
480 	filter->status = 0;
481 	filter->cache = 0;
482 }
483 
mbfl_filt_conv_common_flush(mbfl_convert_filter * filter)484 int mbfl_filt_conv_common_flush(mbfl_convert_filter *filter)
485 {
486 	filter->status = 0;
487 	filter->cache = 0;
488 
489 	if (filter->flush_function != NULL) {
490 		(*filter->flush_function)(filter->data);
491 	}
492 	return 0;
493 }
494 
mbfl_filt_conv_common_dtor(mbfl_convert_filter * filter)495 void mbfl_filt_conv_common_dtor(mbfl_convert_filter *filter)
496 {
497 	filter->status = 0;
498 	filter->cache = 0;
499 }
500