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