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_euc_jp.h"
32 
33 #include "unicode_table_cp932_ext.h"
34 #include "unicode_table_jis.h"
35 
36 static int mbfl_filt_conv_eucjp_wchar_flush(mbfl_convert_filter *filter);
37 
38 const unsigned char mblen_table_eucjp[] = { /* 0xA1-0xFE */
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   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3,
48   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
49   1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
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   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
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, 1
55 };
56 
57 static const char *mbfl_encoding_euc_jp_aliases[] = {"EUC", "EUC_JP", "eucJP", "x-euc-jp", NULL};
58 
59 const mbfl_encoding mbfl_encoding_euc_jp = {
60 	mbfl_no_encoding_euc_jp,
61 	"EUC-JP",
62 	"EUC-JP",
63 	mbfl_encoding_euc_jp_aliases,
64 	mblen_table_eucjp,
65 	0,
66 	&vtbl_eucjp_wchar,
67 	&vtbl_wchar_eucjp,
68 	NULL
69 };
70 
71 const struct mbfl_convert_vtbl vtbl_eucjp_wchar = {
72 	mbfl_no_encoding_euc_jp,
73 	mbfl_no_encoding_wchar,
74 	mbfl_filt_conv_common_ctor,
75 	NULL,
76 	mbfl_filt_conv_eucjp_wchar,
77 	mbfl_filt_conv_eucjp_wchar_flush,
78 	NULL,
79 };
80 
81 const struct mbfl_convert_vtbl vtbl_wchar_eucjp = {
82 	mbfl_no_encoding_wchar,
83 	mbfl_no_encoding_euc_jp,
84 	mbfl_filt_conv_common_ctor,
85 	NULL,
86 	mbfl_filt_conv_wchar_eucjp,
87 	mbfl_filt_conv_common_flush,
88 	NULL,
89 };
90 
91 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
92 
93 /*
94  * EUC-JP => wchar
95  */
96 int
mbfl_filt_conv_eucjp_wchar(int c,mbfl_convert_filter * filter)97 mbfl_filt_conv_eucjp_wchar(int c, mbfl_convert_filter *filter)
98 {
99 	int c1, s, w = 0;
100 
101 	switch (filter->status) {
102 	case 0:
103 		if (c >= 0 && c < 0x80) {	/* latin */
104 			CK((*filter->output_function)(c, filter->data));
105 		} else if (c > 0xa0 && c < 0xff) {	/* X 0208 first char */
106 			filter->status = 1;
107 			filter->cache = c;
108 		} else if (c == 0x8e) {	/* kana first char */
109 			filter->status = 2;
110 		} else if (c == 0x8f) {	/* X 0212 first char */
111 			filter->status = 3;
112 		} else {
113 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
114 		}
115 		break;
116 
117 	case 1:	/* got first half */
118 		filter->status = 0;
119 		c1 = filter->cache;
120 		if (c > 0xa0 && c < 0xff) {
121 			s = (c1 - 0xa1)*94 + c - 0xa1;
122 			if (s >= 0 && s < jisx0208_ucs_table_size) {
123 				w = jisx0208_ucs_table[s];
124 				if (!w)
125 					w = MBFL_BAD_INPUT;
126 			} else {
127 				w = MBFL_BAD_INPUT;
128 			}
129 
130 			CK((*filter->output_function)(w, filter->data));
131 		} else {
132 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
133 		}
134 		break;
135 
136 	case 2:	/* got 0x8e */
137 		filter->status = 0;
138 		if (c > 0xa0 && c < 0xe0) {
139 			w = 0xfec0 + c;
140 			CK((*filter->output_function)(w, filter->data));
141 		} else {
142 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
143 		}
144 		break;
145 
146 	case 3: /* got 0x8f, JIS X 0212 first byte */
147 		filter->status++;
148 		filter->cache = c;
149 		break;
150 
151 	case 4: /* got 0x8f, JIS X 0212 second byte */
152 		filter->status = 0;
153 		c1 = filter->cache;
154 		if (c > 0xA0 && c < 0xFF && c1 > 0xA0 && c1 < 0xFF) {
155 			s = (c1 - 0xa1)*94 + c - 0xa1;
156 			if (s >= 0 && s < jisx0212_ucs_table_size) {
157 				w = jisx0212_ucs_table[s];
158 				if (!w)
159 					w = MBFL_BAD_INPUT;
160 			} else {
161 				w = MBFL_BAD_INPUT;
162 			}
163 
164 			CK((*filter->output_function)(w, filter->data));
165 		} else {
166 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
167 		}
168 		break;
169 
170 	default:
171 		filter->status = 0;
172 		break;
173 	}
174 
175 	return 0;
176 }
177 
mbfl_filt_conv_eucjp_wchar_flush(mbfl_convert_filter * filter)178 static int mbfl_filt_conv_eucjp_wchar_flush(mbfl_convert_filter *filter)
179 {
180 	if (filter->status) {
181 		(*filter->output_function)(MBFL_BAD_INPUT, filter->data);
182 		filter->status = 0;
183 	}
184 
185 	if (filter->flush_function) {
186 		(*filter->flush_function)(filter->data);
187 	}
188 
189 	return 0;
190 }
191 
192 /*
193  * wchar => EUC-JP
194  */
195 int
mbfl_filt_conv_wchar_eucjp(int c,mbfl_convert_filter * filter)196 mbfl_filt_conv_wchar_eucjp(int c, mbfl_convert_filter *filter)
197 {
198 	int s = 0;
199 
200 	if (c == 0xAF) { /* U+00AF is MACRON */
201 		s = 0xA2B4; /* Use JIS X 0212 overline */
202 	} else if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
203 		s = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
204 	} else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
205 		s = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
206 	} else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
207 		s = ucs_i_jis_table[c - ucs_i_jis_table_min];
208 	} else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
209 		s = ucs_r_jis_table[c - ucs_r_jis_table_min];
210 	}
211 	if (s <= 0) {
212 		if (c == 0xff3c) {	/* FULLWIDTH REVERSE SOLIDUS */
213 			s = 0x2140;
214 		} else if (c == 0xff5e) {	/* FULLWIDTH TILDE */
215 			s = 0x2141;
216 		} else if (c == 0x2225) {	/* PARALLEL TO */
217 			s = 0x2142;
218 		} else if (c == 0xff0d) {	/* FULLWIDTH HYPHEN-MINUS */
219 			s = 0x215d;
220 		} else if (c == 0xffe0) {	/* FULLWIDTH CENT SIGN */
221 			s = 0x2171;
222 		} else if (c == 0xffe1) {	/* FULLWIDTH POUND SIGN */
223 			s = 0x2172;
224 		} else if (c == 0xffe2) {	/* FULLWIDTH NOT SIGN */
225 			s = 0x224c;
226 		} else if (c == 0) {
227 			s = 0;
228 		} else {
229 			s = -1;
230 		}
231 	}
232 	if (s >= 0) {
233 		if (s < 0x80) {	/* latin */
234 			CK((*filter->output_function)(s, filter->data));
235 		} else if (s < 0x100) {	/* kana */
236 			CK((*filter->output_function)(0x8e, filter->data));
237 			CK((*filter->output_function)(s, filter->data));
238 		} else if (s < 0x8080)  {	/* X 0208 */
239 			CK((*filter->output_function)(((s >> 8) & 0xff) | 0x80, filter->data));
240 			CK((*filter->output_function)((s & 0xff) | 0x80, filter->data));
241 		} else {	/* X 0212 */
242 			CK((*filter->output_function)(0x8f, filter->data));
243 			CK((*filter->output_function)(((s >> 8) & 0xff) | 0x80, filter->data));
244 			CK((*filter->output_function)((s & 0xff) | 0x80, filter->data));
245 		}
246 	} else {
247 		CK(mbfl_filt_conv_illegal_output(c, filter));
248 	}
249 
250 	return 0;
251 }
252