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_ja.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_euc_jp.h"
36 
37 #include "unicode_table_cp932_ext.h"
38 #include "unicode_table_jis.h"
39 
40 int mbfl_filt_ident_eucjp(int c, mbfl_identify_filter *filter);
41 
42 const unsigned char mblen_table_eucjp[] = { /* 0xA1-0xFE */
43   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
44   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
45   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
46   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
47   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
48   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
49   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
50   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
51   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3,
52   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
53   1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
54   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
55   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
56   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
57   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
58   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1
59 };
60 
61 static const char *mbfl_encoding_euc_jp_aliases[] = {"EUC", "EUC_JP", "eucJP", "x-euc-jp", NULL};
62 
63 const mbfl_encoding mbfl_encoding_euc_jp = {
64 	mbfl_no_encoding_euc_jp,
65 	"EUC-JP",
66 	"EUC-JP",
67 	(const char *(*)[])&mbfl_encoding_euc_jp_aliases,
68 	mblen_table_eucjp,
69 	MBFL_ENCTYPE_MBCS,
70 	&vtbl_eucjp_wchar,
71 	&vtbl_wchar_eucjp
72 };
73 
74 const struct mbfl_identify_vtbl vtbl_identify_eucjp = {
75 	mbfl_no_encoding_euc_jp,
76 	mbfl_filt_ident_common_ctor,
77 	mbfl_filt_ident_common_dtor,
78 	mbfl_filt_ident_eucjp
79 };
80 
81 const struct mbfl_convert_vtbl vtbl_eucjp_wchar = {
82 	mbfl_no_encoding_euc_jp,
83 	mbfl_no_encoding_wchar,
84 	mbfl_filt_conv_common_ctor,
85 	mbfl_filt_conv_common_dtor,
86 	mbfl_filt_conv_eucjp_wchar,
87 	mbfl_filt_conv_common_flush
88 };
89 
90 const struct mbfl_convert_vtbl vtbl_wchar_eucjp = {
91 	mbfl_no_encoding_wchar,
92 	mbfl_no_encoding_euc_jp,
93 	mbfl_filt_conv_common_ctor,
94 	mbfl_filt_conv_common_dtor,
95 	mbfl_filt_conv_wchar_eucjp,
96 	mbfl_filt_conv_common_flush
97 };
98 
99 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
100 
101 /*
102  * EUC-JP => wchar
103  */
104 int
mbfl_filt_conv_eucjp_wchar(int c,mbfl_convert_filter * filter)105 mbfl_filt_conv_eucjp_wchar(int c, mbfl_convert_filter *filter)
106 {
107 	int c1, s, w;
108 
109 	switch (filter->status) {
110 	case 0:
111 		if (c >= 0 && c < 0x80) {	/* latin */
112 			CK((*filter->output_function)(c, filter->data));
113 		} else if (c > 0xa0 && c < 0xff) {	/* X 0208 first char */
114 			filter->status = 1;
115 			filter->cache = c;
116 		} else if (c == 0x8e) {	/* kana first char */
117 			filter->status = 2;
118 		} else if (c == 0x8f) {	/* X 0212 first char */
119 			filter->status = 3;
120 		} else {
121 			w = c & MBFL_WCSGROUP_MASK;
122 			w |= MBFL_WCSGROUP_THROUGH;
123 			CK((*filter->output_function)(w, filter->data));
124 		}
125 		break;
126 
127 	case 1:	/* got first half */
128 		filter->status = 0;
129 		c1 = filter->cache;
130 		if (c > 0xa0 && c < 0xff) {
131 			s = (c1 - 0xa1)*94 + c - 0xa1;
132 			if (s >= 0 && s < jisx0208_ucs_table_size) {
133 				w = jisx0208_ucs_table[s];
134 			} else {
135 				w = 0;
136 			}
137 			if (w <= 0) {
138 				w = ((c1 & 0x7f) << 8) | (c & 0x7f);
139 				w &= MBFL_WCSPLANE_MASK;
140 				w |= MBFL_WCSPLANE_JIS0208;
141 			}
142 			CK((*filter->output_function)(w, filter->data));
143 		} else if ((c >= 0 && c < 0x21) || c == 0x7f) {		/* CTLs */
144 			CK((*filter->output_function)(c, filter->data));
145 		} else {
146 			w = (c1 << 8) | c;
147 			w &= MBFL_WCSGROUP_MASK;
148 			w |= MBFL_WCSGROUP_THROUGH;
149 			CK((*filter->output_function)(w, filter->data));
150 		}
151 		break;
152 
153 	case 2:	/* got 0x8e */
154 		filter->status = 0;
155 		if (c > 0xa0 && c < 0xe0) {
156 			w = 0xfec0 + c;
157 			CK((*filter->output_function)(w, filter->data));
158 		} else if ((c >= 0 && c < 0x21) || c == 0x7f) {		/* CTLs */
159 			CK((*filter->output_function)(c, filter->data));
160 		} else {
161 			w = 0x8e00 | c;
162 			w &= MBFL_WCSGROUP_MASK;
163 			w |= MBFL_WCSGROUP_THROUGH;
164 			CK((*filter->output_function)(w, filter->data));
165 		}
166 		break;
167 
168 	case 3:	/* got 0x8f,  X 0212 first char */
169 		if ((c >= 0 && c < 0x21) || c == 0x7f) {		/* CTLs */
170 			CK((*filter->output_function)(c, filter->data));
171 			filter->status = 0;
172 		} else {
173 			filter->status++;
174 			filter->cache = c;
175 		}
176 		break;
177 	case 4:	/* got 0x8f,  X 0212 second char */
178 		filter->status = 0;
179 		c1 = filter->cache;
180 		if (c1 > 0xa0 && c1 < 0xff && c > 0xa0 && c < 0xff) {
181 			s = (c1 - 0xa1)*94 + c - 0xa1;
182 			if (s >= 0 && s < jisx0212_ucs_table_size) {
183 				w = jisx0212_ucs_table[s];
184 			} else {
185 				w = 0;
186 			}
187 			if (w <= 0) {
188 				w = ((c1 & 0x7f) << 8) | (c & 0x7f);
189 				w &= MBFL_WCSPLANE_MASK;
190 				w |= MBFL_WCSPLANE_JIS0212;
191 			}
192 			CK((*filter->output_function)(w, filter->data));
193 		} else if ((c >= 0 && c < 0x21) || c == 0x7f) {		/* CTLs */
194 			CK((*filter->output_function)(c, filter->data));
195 		} else {
196 			w = (c1 << 8) | c | 0x8f0000;
197 			w &= MBFL_WCSGROUP_MASK;
198 			w |= MBFL_WCSGROUP_THROUGH;
199 			CK((*filter->output_function)(w, filter->data));
200 		}
201 		break;
202 
203 	default:
204 		filter->status = 0;
205 		break;
206 	}
207 
208 	return c;
209 }
210 
211 /*
212  * wchar => EUC-JP
213  */
214 int
mbfl_filt_conv_wchar_eucjp(int c,mbfl_convert_filter * filter)215 mbfl_filt_conv_wchar_eucjp(int c, mbfl_convert_filter *filter)
216 {
217 	int c1, s;
218 
219 	s = 0;
220 	if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
221 		s = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
222 	} else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
223 		s = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
224 	} else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
225 		s = ucs_i_jis_table[c - ucs_i_jis_table_min];
226 	} else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
227 		s = ucs_r_jis_table[c - ucs_r_jis_table_min];
228 	}
229 	if (s <= 0) {
230 		c1 = c & ~MBFL_WCSPLANE_MASK;
231 		if (c1 == MBFL_WCSPLANE_JIS0208) {
232 			s = c & MBFL_WCSPLANE_MASK;
233 		} else if (c1 == MBFL_WCSPLANE_JIS0212) {
234 			s = c & MBFL_WCSPLANE_MASK;
235 			s |= 0x8080;
236 		} else if (c == 0xff3c) {	/* FULLWIDTH REVERSE SOLIDUS */
237 			s = 0x2140;
238 		} else if (c == 0xff5e) {	/* FULLWIDTH TILDE */
239 			s = 0x2141;
240 		} else if (c == 0x2225) {	/* PARALLEL TO */
241 			s = 0x2142;
242 		} else if (c == 0xff0d) {	/* FULLWIDTH HYPHEN-MINUS */
243 			s = 0x215d;
244 		} else if (c == 0xffe0) {	/* FULLWIDTH CENT SIGN */
245 			s = 0x2171;
246 		} else if (c == 0xffe1) {	/* FULLWIDTH POUND SIGN */
247 			s = 0x2172;
248 		} else if (c == 0xffe2) {	/* FULLWIDTH NOT SIGN */
249 			s = 0x224c;
250 		}
251 		if (c == 0) {
252 			s = 0;
253 		} else if (s <= 0) {
254 			s = -1;
255 		}
256 	}
257 	if (s >= 0) {
258 		if (s < 0x80) {	/* latin */
259 			CK((*filter->output_function)(s, filter->data));
260 		} else if (s < 0x100) {	/* kana */
261 			CK((*filter->output_function)(0x8e, filter->data));
262 			CK((*filter->output_function)(s, filter->data));
263 		} else if (s < 0x8080)  {	/* X 0208 */
264 			CK((*filter->output_function)(((s >> 8) & 0xff) | 0x80, filter->data));
265 			CK((*filter->output_function)((s & 0xff) | 0x80, filter->data));
266 		} else {	/* X 0212 */
267 			CK((*filter->output_function)(0x8f, filter->data));
268 			CK((*filter->output_function)(((s >> 8) & 0xff) | 0x80, filter->data));
269 			CK((*filter->output_function)((s & 0xff) | 0x80, filter->data));
270 		}
271 	} else {
272 		CK(mbfl_filt_conv_illegal_output(c, filter));
273 	}
274 
275 	return c;
276 }
277 
mbfl_filt_ident_eucjp(int c,mbfl_identify_filter * filter)278 int mbfl_filt_ident_eucjp(int c, mbfl_identify_filter *filter)
279 {
280 	switch (filter->status) {
281 	case  0:	/* latin */
282 		if (c >= 0 && c < 0x80) {	/* ok */
283 			;
284 		} else if (c > 0xa0 && c < 0xff) {	/* kanji first char */
285 			filter->status = 1;
286 		} else if (c == 0x8e) {				/* kana first char */
287 			filter->status = 2;
288 		} else if (c == 0x8f) {				/* X 0212 first char */
289 			filter->status = 3;
290 		} else {							/* bad */
291 			filter->flag = 1;
292 		}
293 		break;
294 
295 	case  1:	/* got first half */
296 		if (c < 0xa1 || c > 0xfe) {		/* bad */
297 			filter->flag = 1;
298 		}
299 		filter->status = 0;
300 		break;
301 
302 	case  2:	/* got 0x8e */
303 		if (c < 0xa1 || c > 0xdf) {		/* bad */
304 			filter->flag = 1;
305 		}
306 		filter->status = 0;
307 		break;
308 
309 	case  3:	/* got 0x8f */
310 		if (c < 0xa1 || c > 0xfe) {		/* bad */
311 			filter->flag = 1;
312 		}
313 		filter->status++;
314 		break;
315 	case  4:	/* got 0x8f */
316 		if (c < 0xa1 || c > 0xfe) {		/* bad */
317 			filter->flag = 1;
318 		}
319 		filter->status = 0;
320 		break;
321 
322 	default:
323 		filter->status = 0;
324 		break;
325 	}
326 
327 	return c;
328 }
329