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