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_sjis.h"
36 
37 #define UNICODE_TABLE_CP932_DEF
38 #define UNICODE_TABLE_JIS_DEF
39 
40 #include "unicode_table_cp932_ext.h"
41 #include "unicode_table_jis.h"
42 
43 int mbfl_filt_ident_sjis(int c, mbfl_identify_filter *filter);
44 
45 const unsigned char mblen_table_sjis[] = { /* 0x80-0x9f,0xE0-0xFF */
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, 1, 1,
53   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
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, 2,
56   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
57   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
58   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
59   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
60   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
61   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
62 };
63 
64 static const char *mbfl_encoding_sjis_aliases[] = {"x-sjis", "SHIFT-JIS", NULL};
65 
66 const mbfl_encoding mbfl_encoding_sjis = {
67 	mbfl_no_encoding_sjis,
68 	"SJIS",
69 	"Shift_JIS",
70 	(const char *(*)[])&mbfl_encoding_sjis_aliases,
71 	mblen_table_sjis,
72 	MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_GL_UNSAFE,
73 	&vtbl_sjis_wchar,
74 	&vtbl_wchar_sjis
75 };
76 
77 const struct mbfl_identify_vtbl vtbl_identify_sjis = {
78 	mbfl_no_encoding_sjis,
79 	mbfl_filt_ident_common_ctor,
80 	mbfl_filt_ident_common_dtor,
81 	mbfl_filt_ident_sjis
82 };
83 
84 const struct mbfl_convert_vtbl vtbl_sjis_wchar = {
85 	mbfl_no_encoding_sjis,
86 	mbfl_no_encoding_wchar,
87 	mbfl_filt_conv_common_ctor,
88 	mbfl_filt_conv_common_dtor,
89 	mbfl_filt_conv_sjis_wchar,
90 	mbfl_filt_conv_common_flush
91 };
92 
93 const struct mbfl_convert_vtbl vtbl_wchar_sjis = {
94 	mbfl_no_encoding_wchar,
95 	mbfl_no_encoding_sjis,
96 	mbfl_filt_conv_common_ctor,
97 	mbfl_filt_conv_common_dtor,
98 	mbfl_filt_conv_wchar_sjis,
99 	mbfl_filt_conv_common_flush
100 };
101 
102 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
103 
104 #define SJIS_ENCODE(c1,c2,s1,s2)	\
105 		do {						\
106 			s1 = c1;				\
107 			s1--;					\
108 			s1 >>= 1;				\
109 			if ((c1) < 0x5f) {		\
110 				s1 += 0x71;			\
111 			} else {				\
112 				s1 += 0xb1;			\
113 			}						\
114 			s2 = c2;				\
115 			if ((c1) & 1) {			\
116 				if ((c2) < 0x60) {	\
117 					s2--;			\
118 				}					\
119 				s2 += 0x20;			\
120 			} else {				\
121 				s2 += 0x7e;			\
122 			}						\
123 		} while (0)
124 
125 #define SJIS_DECODE(c1,c2,s1,s2)	\
126 		do {						\
127 			s1 = c1;				\
128 			if (s1 < 0xa0) {		\
129 				s1 -= 0x81;			\
130 			} else {				\
131 				s1 -= 0xc1;			\
132 			}						\
133 			s1 <<= 1;				\
134 			s1 += 0x21;				\
135 			s2 = c2;				\
136 			if (s2 < 0x9f) {		\
137 				if (s2 < 0x7f) {	\
138 					s2++;			\
139 				}					\
140 				s2 -= 0x20;			\
141 			} else {				\
142 				s1++;				\
143 				s2 -= 0x7e;			\
144 			}						\
145 		} while (0)
146 
147 
148 /*
149  * SJIS => wchar
150  */
151 int
mbfl_filt_conv_sjis_wchar(int c,mbfl_convert_filter * filter)152 mbfl_filt_conv_sjis_wchar(int c, mbfl_convert_filter *filter)
153 {
154 	int c1, s1, s2, w;
155 
156 	switch (filter->status) {
157 	case 0:
158 		if (c >= 0 && c < 0x80) {	/* latin */
159 			CK((*filter->output_function)(c, filter->data));
160 		} else if (c > 0xa0 && c < 0xe0) {	/* kana */
161 			CK((*filter->output_function)(0xfec0 + c, filter->data));
162 		} else if (c > 0x80 && c < 0xfd && c != 0xa0) {	/* kanji first char */
163 			filter->status = 1;
164 			filter->cache = c;
165 		} else {
166 			w = c & MBFL_WCSGROUP_MASK;
167 			w |= MBFL_WCSGROUP_THROUGH;
168 			CK((*filter->output_function)(w, filter->data));
169 		}
170 		break;
171 
172 	case 1:		/* kanji second char */
173 		filter->status = 0;
174 		c1 = filter->cache;
175 		if (c >= 0x40 && c <= 0xfc && c != 0x7f) {
176 			SJIS_DECODE(c1, c, s1, s2);
177 			w = (s1 - 0x21)*94 + s2 - 0x21;
178 			if (w >= 0 && w < jisx0208_ucs_table_size) {
179 				w = jisx0208_ucs_table[w];
180 			} else {
181 				w = 0;
182 			}
183 			if (w <= 0) {
184 				if (s1 < 0x7f && s2 < 0x7f) {
185 					w = (s1 << 8) | s2;
186 					w &= MBFL_WCSPLANE_MASK;
187 					w |= MBFL_WCSPLANE_JIS0208;
188 				} else {
189 					w = (c1 << 8) | c;
190 					w &= MBFL_WCSGROUP_MASK;
191 					w |= MBFL_WCSGROUP_THROUGH;
192 				}
193 			}
194 			CK((*filter->output_function)(w, filter->data));
195 		} else if ((c >= 0 && c < 0x21) || c == 0x7f) {		/* CTLs */
196 			CK((*filter->output_function)(c, filter->data));
197 		} else {
198 			w = (c1 << 8) | c;
199 			w &= MBFL_WCSGROUP_MASK;
200 			w |= MBFL_WCSGROUP_THROUGH;
201 			CK((*filter->output_function)(w, filter->data));
202 		}
203 		break;
204 
205 	default:
206 		filter->status = 0;
207 		break;
208 	}
209 
210 	return c;
211 }
212 
213 /*
214  * wchar => SJIS
215  */
216 int
mbfl_filt_conv_wchar_sjis(int c,mbfl_convert_filter * filter)217 mbfl_filt_conv_wchar_sjis(int c, mbfl_convert_filter *filter)
218 {
219 	int c1, c2, s1, s2;
220 
221 	s1 = 0;
222 	if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
223 		s1 = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
224 	} else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
225 		s1 = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
226 	} else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
227 		s1 = ucs_i_jis_table[c - ucs_i_jis_table_min];
228 	} else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
229 		s1 = ucs_r_jis_table[c - ucs_r_jis_table_min];
230 	}
231 	if (s1 <= 0) {
232 		c1 = c & ~MBFL_WCSPLANE_MASK;
233 		if (c1 == MBFL_WCSPLANE_JIS0208) {
234 			s1 = c & MBFL_WCSPLANE_MASK;
235 		} else if (c == 0xa5) {		/* YEN SIGN */
236 			s1 = 0x216f;	/* FULLWIDTH YEN SIGN */
237 		} else if (c == 0x203e) {	/* OVER LINE */
238 			s1 = 0x2131;	/* FULLWIDTH MACRON */
239 		} else if (c == 0xff3c) {	/* FULLWIDTH REVERSE SOLIDUS */
240 			s1 = 0x2140;
241 		} else if (c == 0xff5e) {	/* FULLWIDTH TILDE */
242 			s1 = 0x2141;
243 		} else if (c == 0x2225) {	/* PARALLEL TO */
244 			s1 = 0x2142;
245 		} else if (c == 0xff0d) {	/* FULLWIDTH HYPHEN-MINUS */
246 			s1 = 0x215d;
247 		} else if (c == 0xffe0) {	/* FULLWIDTH CENT SIGN */
248 			s1 = 0x2171;
249 		} else if (c == 0xffe1) {	/* FULLWIDTH POUND SIGN */
250 			s1 = 0x2172;
251 		} else if (c == 0xffe2) {	/* FULLWIDTH NOT SIGN */
252 			s1 = 0x224c;
253 		}
254 		if (c == 0) {
255 			s1 = 0;
256 		} else if (s1 <= 0) {
257 			s1 = -1;
258 		}
259 	} else if (s1 >= 0x8080) {
260 		s1 = -1;
261 	}
262 	if (s1 >= 0) {
263 		if (s1 < 0x100) { /* latin or kana */
264 			CK((*filter->output_function)(s1, filter->data));
265 		} else { /* kanji */
266 			c1 = (s1 >> 8) & 0xff;
267 			c2 = s1 & 0xff;
268 			SJIS_ENCODE(c1, c2, s1, s2);
269 			CK((*filter->output_function)(s1, filter->data));
270 			CK((*filter->output_function)(s2, filter->data));
271 		}
272 	} else {
273 		if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
274 			CK(mbfl_filt_conv_illegal_output(c, filter));
275 		}
276 	}
277 
278 	return c;
279 }
280 
mbfl_filt_ident_sjis(int c,mbfl_identify_filter * filter)281 int mbfl_filt_ident_sjis(int c, mbfl_identify_filter *filter)
282 {
283 	if (filter->status) {		/* kanji second char */
284 		if (c < 0x40 || c > 0xfc || c == 0x7f) {	/* bad */
285 		    filter->flag = 1;
286 		}
287 		filter->status = 0;
288 	} else if (c >= 0 && c < 0x80) {	/* latin  ok */
289 		;
290 	} else if (c > 0xa0 && c < 0xe0) {	/* kana  ok */
291 		;
292 	} else if (c > 0x80 && c < 0xf0 && c != 0xa0) {	/* kanji first char */
293 		filter->status = 1;
294 	} else {							/* bad */
295 		filter->flag = 1;
296 	}
297 
298 	return c;
299 }
300