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_sjis.h"
32 
33 #define UNICODE_TABLE_CP932_DEF
34 #define UNICODE_TABLE_JIS_DEF
35 
36 #include "unicode_table_cp932_ext.h"
37 #include "unicode_table_jis.h"
38 
39 int mbfl_filt_ident_sjis(int c, mbfl_identify_filter *filter);
40 
41 const unsigned char mblen_table_sjis[] = { /* 0x80-0x9f,0xE0-0xFF */
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, 1, 1,
49   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
50   2, 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   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
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   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 };
59 
60 static const char *mbfl_encoding_sjis_aliases[] = {"x-sjis", "SHIFT-JIS", NULL};
61 
62 const mbfl_encoding mbfl_encoding_sjis = {
63 	mbfl_no_encoding_sjis,
64 	"SJIS",
65 	"Shift_JIS",
66 	(const char *(*)[])&mbfl_encoding_sjis_aliases,
67 	mblen_table_sjis,
68 	MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_GL_UNSAFE,
69 	&vtbl_sjis_wchar,
70 	&vtbl_wchar_sjis
71 };
72 
73 const struct mbfl_identify_vtbl vtbl_identify_sjis = {
74 	mbfl_no_encoding_sjis,
75 	mbfl_filt_ident_common_ctor,
76 	mbfl_filt_ident_sjis
77 };
78 
79 const struct mbfl_convert_vtbl vtbl_sjis_wchar = {
80 	mbfl_no_encoding_sjis,
81 	mbfl_no_encoding_wchar,
82 	mbfl_filt_conv_common_ctor,
83 	NULL,
84 	mbfl_filt_conv_sjis_wchar,
85 	mbfl_filt_conv_common_flush,
86 	NULL,
87 };
88 
89 const struct mbfl_convert_vtbl vtbl_wchar_sjis = {
90 	mbfl_no_encoding_wchar,
91 	mbfl_no_encoding_sjis,
92 	mbfl_filt_conv_common_ctor,
93 	NULL,
94 	mbfl_filt_conv_wchar_sjis,
95 	mbfl_filt_conv_common_flush,
96 	NULL,
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 => wchar
147  */
148 int
mbfl_filt_conv_sjis_wchar(int c,mbfl_convert_filter * filter)149 mbfl_filt_conv_sjis_wchar(int c, mbfl_convert_filter *filter)
150 {
151 	int c1, 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 			SJIS_DECODE(c1, c, s1, s2);
174 			w = (s1 - 0x21)*94 + s2 - 0x21;
175 			if (w >= 0 && w < jisx0208_ucs_table_size) {
176 				w = jisx0208_ucs_table[w];
177 			} else {
178 				w = 0;
179 			}
180 			if (w <= 0) {
181 				if (s1 < 0x7f && s2 < 0x7f) {
182 					w = (s1 << 8) | s2;
183 					w &= MBFL_WCSPLANE_MASK;
184 					w |= MBFL_WCSPLANE_JIS0208;
185 				} else {
186 					w = (c1 << 8) | c;
187 					w &= MBFL_WCSGROUP_MASK;
188 					w |= MBFL_WCSGROUP_THROUGH;
189 				}
190 			}
191 			CK((*filter->output_function)(w, filter->data));
192 		} else if ((c >= 0 && c < 0x21) || c == 0x7f) {		/* CTLs */
193 			CK((*filter->output_function)(c, filter->data));
194 		} else {
195 			w = (c1 << 8) | c;
196 			w &= MBFL_WCSGROUP_MASK;
197 			w |= MBFL_WCSGROUP_THROUGH;
198 			CK((*filter->output_function)(w, filter->data));
199 		}
200 		break;
201 
202 	default:
203 		filter->status = 0;
204 		break;
205 	}
206 
207 	return c;
208 }
209 
210 /*
211  * wchar => SJIS
212  */
213 int
mbfl_filt_conv_wchar_sjis(int c,mbfl_convert_filter * filter)214 mbfl_filt_conv_wchar_sjis(int c, mbfl_convert_filter *filter)
215 {
216 	int c1, c2, s1, s2;
217 
218 	s1 = 0;
219 	if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
220 		s1 = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
221 	} else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
222 		s1 = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
223 	} else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
224 		s1 = ucs_i_jis_table[c - ucs_i_jis_table_min];
225 	} else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
226 		s1 = ucs_r_jis_table[c - ucs_r_jis_table_min];
227 	}
228 	if (s1 <= 0) {
229 		c1 = c & ~MBFL_WCSPLANE_MASK;
230 		if (c1 == MBFL_WCSPLANE_JIS0208) {
231 			s1 = c & MBFL_WCSPLANE_MASK;
232 		} else if (c == 0xa5) {		/* YEN SIGN */
233 			s1 = 0x216f;	/* FULLWIDTH YEN SIGN */
234 		} else if (c == 0x203e) {	/* OVER LINE */
235 			s1 = 0x2131;	/* FULLWIDTH MACRON */
236 		} else if (c == 0xff3c) {	/* FULLWIDTH REVERSE SOLIDUS */
237 			s1 = 0x2140;
238 		} else if (c == 0xff5e) {	/* FULLWIDTH TILDE */
239 			s1 = 0x2141;
240 		} else if (c == 0x2225) {	/* PARALLEL TO */
241 			s1 = 0x2142;
242 		} else if (c == 0xff0d) {	/* FULLWIDTH HYPHEN-MINUS */
243 			s1 = 0x215d;
244 		} else if (c == 0xffe0) {	/* FULLWIDTH CENT SIGN */
245 			s1 = 0x2171;
246 		} else if (c == 0xffe1) {	/* FULLWIDTH POUND SIGN */
247 			s1 = 0x2172;
248 		} else if (c == 0xffe2) {	/* FULLWIDTH NOT SIGN */
249 			s1 = 0x224c;
250 		}
251 		if (c == 0) {
252 			s1 = 0;
253 		} else if (s1 <= 0) {
254 			s1 = -1;
255 		}
256 	} else if (s1 >= 0x8080) {
257 		s1 = -1;
258 	}
259 	if (s1 >= 0) {
260 		if (s1 < 0x100) { /* latin or kana */
261 			CK((*filter->output_function)(s1, filter->data));
262 		} else { /* kanji */
263 			c1 = (s1 >> 8) & 0xff;
264 			c2 = s1 & 0xff;
265 			SJIS_ENCODE(c1, c2, s1, s2);
266 			CK((*filter->output_function)(s1, filter->data));
267 			CK((*filter->output_function)(s2, filter->data));
268 		}
269 	} else {
270 		CK(mbfl_filt_conv_illegal_output(c, filter));
271 	}
272 
273 	return c;
274 }
275 
mbfl_filt_ident_sjis(int c,mbfl_identify_filter * filter)276 int mbfl_filt_ident_sjis(int c, mbfl_identify_filter *filter)
277 {
278 	if (filter->status) {		/* kanji second char */
279 		if (c < 0x40 || c > 0xfc || c == 0x7f) {	/* bad */
280 		    filter->flag = 1;
281 		}
282 		filter->status = 0;
283 	} else if (c >= 0 && c < 0x80) {	/* latin  ok */
284 		;
285 	} else if (c > 0xa0 && c < 0xe0) {	/* kana  ok */
286 		;
287 	} else if (c > 0x80 && c < 0xf0 && c != 0xa0) {	/* kanji first char */
288 		filter->status = 1;
289 	} else {							/* bad */
290 		filter->flag = 1;
291 	}
292 
293 	return c;
294 }
295