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 static int mbfl_filt_conv_sjis_wchar_flush(mbfl_convert_filter *filter);
40 
41 const unsigned char mblen_table_sjis[] = { /* 0x81-0x9F,0xE0-0xEF */
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   1, 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   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
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 	mbfl_encoding_sjis_aliases,
67 	mblen_table_sjis,
68 	MBFL_ENCTYPE_GL_UNSAFE,
69 	&vtbl_sjis_wchar,
70 	&vtbl_wchar_sjis,
71 	NULL
72 };
73 
74 const struct mbfl_convert_vtbl vtbl_sjis_wchar = {
75 	mbfl_no_encoding_sjis,
76 	mbfl_no_encoding_wchar,
77 	mbfl_filt_conv_common_ctor,
78 	NULL,
79 	mbfl_filt_conv_sjis_wchar,
80 	mbfl_filt_conv_sjis_wchar_flush,
81 	NULL
82 };
83 
84 const struct mbfl_convert_vtbl vtbl_wchar_sjis = {
85 	mbfl_no_encoding_wchar,
86 	mbfl_no_encoding_sjis,
87 	mbfl_filt_conv_common_ctor,
88 	NULL,
89 	mbfl_filt_conv_wchar_sjis,
90 	mbfl_filt_conv_common_flush,
91 	NULL
92 };
93 
94 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
95 
96 #define SJIS_ENCODE(c1,c2,s1,s2)	\
97 		do {						\
98 			s1 = c1;				\
99 			s1--;					\
100 			s1 >>= 1;				\
101 			if ((c1) < 0x5f) {		\
102 				s1 += 0x71;			\
103 			} else {				\
104 				s1 += 0xb1;			\
105 			}						\
106 			s2 = c2;				\
107 			if ((c1) & 1) {			\
108 				if ((c2) < 0x60) {	\
109 					s2--;			\
110 				}					\
111 				s2 += 0x20;			\
112 			} else {				\
113 				s2 += 0x7e;			\
114 			}						\
115 		} while (0)
116 
117 #define SJIS_DECODE(c1,c2,s1,s2)	\
118 		do {						\
119 			s1 = c1;				\
120 			if (s1 < 0xa0) {		\
121 				s1 -= 0x81;			\
122 			} else {				\
123 				s1 -= 0xc1;			\
124 			}						\
125 			s1 <<= 1;				\
126 			s1 += 0x21;				\
127 			s2 = c2;				\
128 			if (s2 < 0x9f) {		\
129 				if (s2 < 0x7f) {	\
130 					s2++;			\
131 				}					\
132 				s2 -= 0x20;			\
133 			} else {				\
134 				s1++;				\
135 				s2 -= 0x7e;			\
136 			}						\
137 		} while (0)
138 
mbfl_filt_conv_sjis_wchar(int c,mbfl_convert_filter * filter)139 int mbfl_filt_conv_sjis_wchar(int c, mbfl_convert_filter *filter)
140 {
141 	int s1, s2, w;
142 
143 	switch (filter->status) {
144 	case 0:
145 		if (c >= 0 && c < 0x80) { /* ASCII */
146 			CK((*filter->output_function)(c, filter->data));
147 		} else if (c > 0xA0 && c < 0xE0) { /* Kana */
148 			CK((*filter->output_function)(0xFEC0 + c, filter->data));
149 		} else if (c > 0x80 && c < 0xF0 && c != 0xA0) { /* Kanji, first byte */
150 			filter->status = 1;
151 			filter->cache = c;
152 		} else {
153 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
154 		}
155 		break;
156 
157 	case 1: /* Kanji, second byte */
158 		filter->status = 0;
159 		int c1 = filter->cache;
160 		if (c >= 0x40 && c <= 0xFC && c != 0x7F) {
161 			SJIS_DECODE(c1, c, s1, s2);
162 			w = (s1 - 0x21)*94 + s2 - 0x21;
163 			if (w >= 0 && w < jisx0208_ucs_table_size) {
164 				w = jisx0208_ucs_table[w];
165 				if (!w)
166 					w = MBFL_BAD_INPUT;
167 			} else {
168 				w = MBFL_BAD_INPUT;
169 			}
170 			CK((*filter->output_function)(w, filter->data));
171 		} else {
172 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
173 		}
174 	}
175 
176 	return 0;
177 }
178 
mbfl_filt_conv_sjis_wchar_flush(mbfl_convert_filter * filter)179 static int mbfl_filt_conv_sjis_wchar_flush(mbfl_convert_filter *filter)
180 {
181 	if (filter->status) {
182 		(*filter->output_function)(MBFL_BAD_INPUT, filter->data);
183 		filter->status = 0;
184 	}
185 
186 	if (filter->flush_function) {
187 		(*filter->flush_function)(filter->data);
188 	}
189 
190 	return 0;
191 }
192 
mbfl_filt_conv_wchar_sjis(int c,mbfl_convert_filter * filter)193 int mbfl_filt_conv_wchar_sjis(int c, mbfl_convert_filter *filter)
194 {
195 	int c1, c2, s1, s2;
196 
197 	s1 = 0;
198 	if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
199 		s1 = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
200 	} else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
201 		s1 = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
202 	} else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
203 		s1 = ucs_i_jis_table[c - ucs_i_jis_table_min];
204 	} else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
205 		s1 = ucs_r_jis_table[c - ucs_r_jis_table_min];
206 	}
207 	if (s1 <= 0) {
208 		if (c == 0xA5) { /* YEN SIGN */
209 			s1 = 0x216F; /* FULLWIDTH YEN SIGN */
210 		} else if (c == 0xAF || c == 0x203E) { /* U+00AF is MACRON, U+203E is OVERLINE */
211 			s1 = 0x2131; /* FULLWIDTH MACRON */
212 		} else if (c == 0xFF3C) { /* FULLWIDTH REVERSE SOLIDUS */
213 			s1 = 0x2140;
214 		} else if (c == 0xFF5E) { /* FULLWIDTH TILDE */
215 			s1 = 0x2141;
216 		} else if (c == 0x2225) { /* PARALLEL TO */
217 			s1 = 0x2142;
218 		} else if (c == 0xFF0D) { /* FULLWIDTH HYPHEN-MINUS */
219 			s1 = 0x215D;
220 		} else if (c == 0xFFE0) { /* FULLWIDTH CENT SIGN */
221 			s1 = 0x2171;
222 		} else if (c == 0xFFE1) { /* FULLWIDTH POUND SIGN */
223 			s1 = 0x2172;
224 		} else if (c == 0xFFE2) { /* FULLWIDTH NOT SIGN */
225 			s1 = 0x224C;
226 		} else if (c == 0) {
227 			s1 = 0;
228 		} else {
229 			s1 = -1;
230 		}
231 	} else if (s1 >= 0x8080) { /* JIS X 0212; not supported */
232 		s1 = -1;
233 	}
234 
235 	if (s1 >= 0) {
236 		if (s1 < 0x100) { /* Latin/Kana */
237 			CK((*filter->output_function)(s1, filter->data));
238 		} else { /* Kanji */
239 			c1 = (s1 >> 8) & 0xFF;
240 			c2 = s1 & 0xFF;
241 			SJIS_ENCODE(c1, c2, s1, s2);
242 			CK((*filter->output_function)(s1, filter->data));
243 			CK((*filter->output_function)(s2, filter->data));
244 		}
245 	} else {
246 		CK(mbfl_filt_conv_illegal_output(c, filter));
247 	}
248 
249 	return 0;
250 }
251