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