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 #include "mbfilter.h"
31 #include "mbfilter_cp51932.h"
32 
33 #include "unicode_table_cp932_ext.h"
34 #include "unicode_table_jis.h"
35 #include "cp932_table.h"
36 
37 static int mbfl_filt_ident_cp51932(int c, mbfl_identify_filter *filter);
38 
39 static const unsigned char mblen_table_eucjp[] = { /* 0xA1-0xFE */
40   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
41   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
42   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
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, 2, 1,
49   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
50   1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
51   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
52   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
53   2, 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, 1
56 };
57 
58 
59 static const char *mbfl_encoding_cp51932_aliases[] = {"cp51932", NULL};
60 
61 const struct mbfl_identify_vtbl vtbl_identify_cp51932 = {
62 	mbfl_no_encoding_cp51932,
63 	mbfl_filt_ident_common_ctor,
64 	mbfl_filt_ident_cp51932
65 };
66 
67 const mbfl_encoding mbfl_encoding_cp51932 = {
68 	mbfl_no_encoding_cp51932,
69 	"CP51932",
70 	"CP51932",
71 	(const char *(*)[])&mbfl_encoding_cp51932_aliases,
72 	mblen_table_eucjp,
73 	MBFL_ENCTYPE_MBCS,
74 	&vtbl_cp51932_wchar,
75 	&vtbl_wchar_cp51932
76 };
77 
78 const struct mbfl_convert_vtbl vtbl_cp51932_wchar = {
79 	mbfl_no_encoding_cp51932,
80 	mbfl_no_encoding_wchar,
81 	mbfl_filt_conv_common_ctor,
82 	NULL,
83 	mbfl_filt_conv_cp51932_wchar,
84 	mbfl_filt_conv_common_flush,
85 	NULL,
86 };
87 
88 const struct mbfl_convert_vtbl vtbl_wchar_cp51932 = {
89 	mbfl_no_encoding_wchar,
90 	mbfl_no_encoding_cp51932,
91 	mbfl_filt_conv_common_ctor,
92 	NULL,
93 	mbfl_filt_conv_wchar_cp51932,
94 	mbfl_filt_conv_common_flush,
95 	NULL,
96 };
97 
98 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
99 
100 #define sjistoidx(c1, c2) \
101         (((c1) > 0x9f) \
102         ? (((c1) - 0xc1) * 188 + (c2) - (((c2) > 0x7e) ? 0x41 : 0x40)) \
103         : (((c1) - 0x81) * 188 + (c2) - (((c2) > 0x7e) ? 0x41 : 0x40)))
104 #define idxtoeuc1(c) (((c) / 94) + 0xa1)
105 #define idxtoeuc2(c) (((c) % 94) + 0xa1)
106 
107 /*
108  * cp51932 => wchar
109  */
110 int
mbfl_filt_conv_cp51932_wchar(int c,mbfl_convert_filter * filter)111 mbfl_filt_conv_cp51932_wchar(int c, mbfl_convert_filter *filter)
112 {
113 	int c1, s, w;
114 
115 	switch (filter->status) {
116 	case 0:
117 		if (c >= 0 && c < 0x80) {	/* latin */
118 			CK((*filter->output_function)(c, filter->data));
119 		} else if (c > 0xa0 && c < 0xff) {	/* CP932 first char */
120 			filter->status = 1;
121 			filter->cache = c;
122 		} else if (c == 0x8e) {	/* kana first char */
123 			filter->status = 2;
124 		} else {
125 			w = c & MBFL_WCSGROUP_MASK;
126 			w |= MBFL_WCSGROUP_THROUGH;
127 			CK((*filter->output_function)(w, filter->data));
128 		}
129 		break;
130 
131 	case 1:	/* got first half */
132 		filter->status = 0;
133 		c1 = filter->cache;
134 		if (c > 0xa0 && c < 0xff) {
135 			w = 0;
136 			s = (c1 - 0xa1)*94 + c - 0xa1;
137 			if (s <= 137) {
138 				if (s == 31) {
139 					w = 0xff3c;			/* FULLWIDTH REVERSE SOLIDUS */
140 				} else if (s == 32) {
141 					w = 0xff5e;			/* FULLWIDTH TILDE */
142 				} else if (s == 33) {
143 					w = 0x2225;			/* PARALLEL TO */
144 				} else if (s == 60) {
145 					w = 0xff0d;			/* FULLWIDTH HYPHEN-MINUS */
146 				} else if (s == 80) {
147 					w = 0xffe0;			/* FULLWIDTH CENT SIGN */
148 				} else if (s == 81) {
149 					w = 0xffe1;			/* FULLWIDTH POUND SIGN */
150 				} else if (s == 137) {
151 					w = 0xffe2;			/* FULLWIDTH NOT SIGN */
152 				}
153 			}
154 			if (w == 0) {
155 				if (s >= cp932ext1_ucs_table_min && s < cp932ext1_ucs_table_max) {		/* vendor ext1 (13ku) */
156 					w = cp932ext1_ucs_table[s - cp932ext1_ucs_table_min];
157 				} else if (s >= 0 && s < jisx0208_ucs_table_size) {		/* X 0208 */
158 					w = jisx0208_ucs_table[s];
159 				} else if (s >= cp932ext2_ucs_table_min && s < cp932ext2_ucs_table_max) {		/* vendor ext2 (89ku - 92ku) */
160 					w = cp932ext2_ucs_table[s - cp932ext2_ucs_table_min];
161 				}
162 			}
163 			if (w <= 0) {
164 				w = ((c1 & 0x7f) << 8) | (c & 0x7f);
165 				w &= MBFL_WCSPLANE_MASK;
166 				w |= MBFL_WCSPLANE_WINCP932;
167 			}
168 			CK((*filter->output_function)(w, filter->data));
169 		} else if ((c >= 0 && c < 0x21) || c == 0x7f) {		/* CTLs */
170 			CK((*filter->output_function)(c, filter->data));
171 		} else {
172 			w = (c1 << 8) | c;
173 			w &= MBFL_WCSGROUP_MASK;
174 			w |= MBFL_WCSGROUP_THROUGH;
175 			CK((*filter->output_function)(w, filter->data));
176 		}
177 		break;
178 
179 	case 2:	/* got 0x8e, X0201 kana */
180 		filter->status = 0;
181 		if (c > 0xa0 && c < 0xe0) {
182 			w = 0xfec0 + c;
183 			CK((*filter->output_function)(w, filter->data));
184 		} else if ((c >= 0 && c < 0x21) || c == 0x7f) {		/* CTLs */
185 			CK((*filter->output_function)(c, filter->data));
186 		} else {
187 			w = 0x8e00 | c;
188 			w &= MBFL_WCSGROUP_MASK;
189 			w |= MBFL_WCSGROUP_THROUGH;
190 			CK((*filter->output_function)(w, filter->data));
191 		}
192 		break;
193 
194 	default:
195 		filter->status = 0;
196 		break;
197 	}
198 
199 	return c;
200 }
201 
202 /*
203  * wchar => cp51932
204  */
205 int
mbfl_filt_conv_wchar_cp51932(int c,mbfl_convert_filter * filter)206 mbfl_filt_conv_wchar_cp51932(int c, mbfl_convert_filter *filter)
207 {
208 	int c1, c2, s1;
209 
210 	s1 = 0;
211 	if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
212 		s1 = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
213 	} else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
214 		s1 = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
215 	} else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
216 		s1 = ucs_i_jis_table[c - ucs_i_jis_table_min];
217 	} else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
218 		s1 = ucs_r_jis_table[c - ucs_r_jis_table_min];
219 	}
220 	if (s1 >= 0x8080) s1 = -1; /* we don't support JIS X0213 */
221 	if (s1 <= 0) {
222 		c1 = c & ~MBFL_WCSPLANE_MASK;
223 		if (c1 == MBFL_WCSPLANE_WINCP932) {
224 			s1 = c & MBFL_WCSPLANE_MASK;
225 			if (s1 >= ((85 + 0x20) << 8)) {	/* 85ku - 120ku */
226 				s1 = -1;
227 			}
228 		} else if (c1 == MBFL_WCSPLANE_JIS0208) {
229 			s1 = c & MBFL_WCSPLANE_MASK;
230 			if ((s1 >= ((85 + 0x20) << 8) &&  /* 85ku - 94ku */
231                              s1 <= ((88 + 0x20) << 8)) ||/* IBM extension */
232 			    (s1 >= ((93 + 0x20) << 8) && /* 89ku - 92ku */
233                              s1 <= ((94 + 0x20) << 8))) {
234 				s1 = -1;
235 			}
236 		} else if (c == 0xa5) {		/* YEN SIGN */
237 			s1 = 0x005c;			/* YEN SIGN */
238 		} else if (c == 0x203e) {	/* OVER LINE */
239 			s1 = 0x007e;			/* FULLWIDTH MACRON */
240 		} else if (c == 0xff3c) {	/* FULLWIDTH REVERSE SOLIDUS */
241 			s1 = 0x2140;
242 		} else if (c == 0xff5e) {	/* FULLWIDTH TILDE */
243 			s1 = 0x2141;
244 		} else if (c == 0x2225) {	/* PARALLEL TO */
245 			s1 = 0x2142;
246 		} else if (c == 0xff0d) {	/* FULLWIDTH HYPHEN-MINUS */
247 			s1 = 0x215d;
248 		} else if (c == 0xffe0) {	/* FULLWIDTH CENT SIGN */
249 			s1 = 0x2171;
250 		} else if (c == 0xffe1) {	/* FULLWIDTH POUND SIGN */
251 			s1 = 0x2172;
252 		} else if (c == 0xffe2) {	/* FULLWIDTH NOT SIGN */
253 			s1 = 0x224c;
254 		} else {
255 			s1 = -1;
256 			c1 = 0;
257 			c2 = cp932ext1_ucs_table_max - cp932ext1_ucs_table_min;
258 			while (c1 < c2) {		/* CP932 vendor ext1 (13ku) */
259 				if (c == cp932ext1_ucs_table[c1]) {
260 					s1 = ((c1/94 + 0x2d) << 8) + (c1%94 + 0x21);
261 					break;
262 				}
263 				c1++;
264 			}
265 			if (s1 < 0) {
266 				c1 = 0;
267 				c2 = cp932ext2_ucs_table_max - cp932ext2_ucs_table_min;
268 				while (c1 < c2) {		/* CP932 vendor ext3 (115ku - 119ku) */
269 					if (c == cp932ext2_ucs_table[c1]) {
270 					  s1 = ((c1/94 + 0x79) << 8) +(c1%94 + 0x21);
271 					  break;
272 					}
273 					c1++;
274 				}
275 			}
276 		}
277 		if (c == 0) {
278 			s1 = 0;
279 		} else if (s1 <= 0) {
280 			s1 = -1;
281 		}
282 	}
283 
284 	if (s1 >= 0) {
285 		if (s1 < 0x80) {	/* latin */
286 			CK((*filter->output_function)(s1, filter->data));
287 		} else if (s1 < 0x100) {	/* kana */
288 			CK((*filter->output_function)(0x8e, filter->data));
289 			CK((*filter->output_function)(s1, filter->data));
290 		} else if (s1 < 0x8080)  {	/* X 0208 */
291 			CK((*filter->output_function)(((s1 >> 8) & 0xff) | 0x80, filter->data));
292 			CK((*filter->output_function)((s1 & 0xff) | 0x80, filter->data));
293 		} else {
294 		    CK(mbfl_filt_conv_illegal_output(c, filter));
295 		}
296 	} else {
297 		CK(mbfl_filt_conv_illegal_output(c, filter));
298 	}
299 
300 	return c;
301 }
302 
mbfl_filt_ident_cp51932(int c,mbfl_identify_filter * filter)303 static int mbfl_filt_ident_cp51932(int c, mbfl_identify_filter *filter)
304 {
305 	switch (filter->status) {
306 	case  0:	/* latin */
307 		if (c >= 0 && c < 0x80) {	/* ok */
308 			;
309 		} else if (c > 0xa0 && c < 0xff) {	/* kanji first char */
310 			filter->status = 1;
311 		} else if (c == 0x8e) {				/* kana first char */
312 			filter->status = 2;
313 		} else {							/* bad */
314 			filter->flag = 1;
315 		}
316 		break;
317 
318 	case  1:	/* got first half */
319 		if (c < 0xa1 || c > 0xfe) {		/* bad */
320 			filter->flag = 1;
321 		}
322 		filter->status = 0;
323 		break;
324 
325 	case  2:	/* got 0x8e */
326 		if (c < 0xa1 || c > 0xdf) {		/* bad */
327 			filter->flag = 1;
328 		}
329 		filter->status = 0;
330 		break;
331 
332 	default:
333 		filter->status = 0;
334 		break;
335 	}
336 
337 	return c;
338 }
339