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 int mbfl_filt_ident_eucjp(int c, mbfl_identify_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 (const char *(*)[])&mbfl_encoding_euc_jp_aliases,
64 mblen_table_eucjp,
65 MBFL_ENCTYPE_MBCS,
66 &vtbl_eucjp_wchar,
67 &vtbl_wchar_eucjp
68 };
69
70 const struct mbfl_identify_vtbl vtbl_identify_eucjp = {
71 mbfl_no_encoding_euc_jp,
72 mbfl_filt_ident_common_ctor,
73 mbfl_filt_ident_eucjp
74 };
75
76 const struct mbfl_convert_vtbl vtbl_eucjp_wchar = {
77 mbfl_no_encoding_euc_jp,
78 mbfl_no_encoding_wchar,
79 mbfl_filt_conv_common_ctor,
80 NULL,
81 mbfl_filt_conv_eucjp_wchar,
82 mbfl_filt_conv_common_flush,
83 NULL,
84 };
85
86 const struct mbfl_convert_vtbl vtbl_wchar_eucjp = {
87 mbfl_no_encoding_wchar,
88 mbfl_no_encoding_euc_jp,
89 mbfl_filt_conv_common_ctor,
90 NULL,
91 mbfl_filt_conv_wchar_eucjp,
92 mbfl_filt_conv_common_flush,
93 NULL,
94 };
95
96 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
97
98 /*
99 * EUC-JP => wchar
100 */
101 int
mbfl_filt_conv_eucjp_wchar(int c,mbfl_convert_filter * filter)102 mbfl_filt_conv_eucjp_wchar(int c, mbfl_convert_filter *filter)
103 {
104 int c1, s, w;
105
106 switch (filter->status) {
107 case 0:
108 if (c >= 0 && c < 0x80) { /* latin */
109 CK((*filter->output_function)(c, filter->data));
110 } else if (c > 0xa0 && c < 0xff) { /* X 0208 first char */
111 filter->status = 1;
112 filter->cache = c;
113 } else if (c == 0x8e) { /* kana first char */
114 filter->status = 2;
115 } else if (c == 0x8f) { /* X 0212 first char */
116 filter->status = 3;
117 } else {
118 w = c & MBFL_WCSGROUP_MASK;
119 w |= MBFL_WCSGROUP_THROUGH;
120 CK((*filter->output_function)(w, filter->data));
121 }
122 break;
123
124 case 1: /* got first half */
125 filter->status = 0;
126 c1 = filter->cache;
127 if (c > 0xa0 && c < 0xff) {
128 s = (c1 - 0xa1)*94 + c - 0xa1;
129 if (s >= 0 && s < jisx0208_ucs_table_size) {
130 w = jisx0208_ucs_table[s];
131 } else {
132 w = 0;
133 }
134 if (w <= 0) {
135 w = ((c1 & 0x7f) << 8) | (c & 0x7f);
136 w &= MBFL_WCSPLANE_MASK;
137 w |= MBFL_WCSPLANE_JIS0208;
138 }
139 CK((*filter->output_function)(w, filter->data));
140 } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
141 CK((*filter->output_function)(c, filter->data));
142 } else {
143 w = (c1 << 8) | c;
144 w &= MBFL_WCSGROUP_MASK;
145 w |= MBFL_WCSGROUP_THROUGH;
146 CK((*filter->output_function)(w, filter->data));
147 }
148 break;
149
150 case 2: /* got 0x8e */
151 filter->status = 0;
152 if (c > 0xa0 && c < 0xe0) {
153 w = 0xfec0 + c;
154 CK((*filter->output_function)(w, filter->data));
155 } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
156 CK((*filter->output_function)(c, filter->data));
157 } else {
158 w = 0x8e00 | c;
159 w &= MBFL_WCSGROUP_MASK;
160 w |= MBFL_WCSGROUP_THROUGH;
161 CK((*filter->output_function)(w, filter->data));
162 }
163 break;
164
165 case 3: /* got 0x8f, X 0212 first char */
166 if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
167 CK((*filter->output_function)(c, filter->data));
168 filter->status = 0;
169 } else {
170 filter->status++;
171 filter->cache = c;
172 }
173 break;
174 case 4: /* got 0x8f, X 0212 second char */
175 filter->status = 0;
176 c1 = filter->cache;
177 if (c1 > 0xa0 && c1 < 0xff && c > 0xa0 && c < 0xff) {
178 s = (c1 - 0xa1)*94 + c - 0xa1;
179 if (s >= 0 && s < jisx0212_ucs_table_size) {
180 w = jisx0212_ucs_table[s];
181 } else {
182 w = 0;
183 }
184 if (w <= 0) {
185 w = ((c1 & 0x7f) << 8) | (c & 0x7f);
186 w &= MBFL_WCSPLANE_MASK;
187 w |= MBFL_WCSPLANE_JIS0212;
188 }
189 CK((*filter->output_function)(w, filter->data));
190 } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
191 CK((*filter->output_function)(c, filter->data));
192 } else {
193 w = (c1 << 8) | c | 0x8f0000;
194 w &= MBFL_WCSGROUP_MASK;
195 w |= MBFL_WCSGROUP_THROUGH;
196 CK((*filter->output_function)(w, filter->data));
197 }
198 break;
199
200 default:
201 filter->status = 0;
202 break;
203 }
204
205 return c;
206 }
207
208 /*
209 * wchar => EUC-JP
210 */
211 int
mbfl_filt_conv_wchar_eucjp(int c,mbfl_convert_filter * filter)212 mbfl_filt_conv_wchar_eucjp(int c, mbfl_convert_filter *filter)
213 {
214 int c1, s;
215
216 s = 0;
217 if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
218 s = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
219 } else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
220 s = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
221 } else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
222 s = ucs_i_jis_table[c - ucs_i_jis_table_min];
223 } else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
224 s = ucs_r_jis_table[c - ucs_r_jis_table_min];
225 }
226 if (s <= 0) {
227 c1 = c & ~MBFL_WCSPLANE_MASK;
228 if (c1 == MBFL_WCSPLANE_JIS0208) {
229 s = c & MBFL_WCSPLANE_MASK;
230 } else if (c1 == MBFL_WCSPLANE_JIS0212) {
231 s = c & MBFL_WCSPLANE_MASK;
232 s |= 0x8080;
233 } else if (c == 0xff3c) { /* FULLWIDTH REVERSE SOLIDUS */
234 s = 0x2140;
235 } else if (c == 0xff5e) { /* FULLWIDTH TILDE */
236 s = 0x2141;
237 } else if (c == 0x2225) { /* PARALLEL TO */
238 s = 0x2142;
239 } else if (c == 0xff0d) { /* FULLWIDTH HYPHEN-MINUS */
240 s = 0x215d;
241 } else if (c == 0xffe0) { /* FULLWIDTH CENT SIGN */
242 s = 0x2171;
243 } else if (c == 0xffe1) { /* FULLWIDTH POUND SIGN */
244 s = 0x2172;
245 } else if (c == 0xffe2) { /* FULLWIDTH NOT SIGN */
246 s = 0x224c;
247 }
248 if (c == 0) {
249 s = 0;
250 } else if (s <= 0) {
251 s = -1;
252 }
253 }
254 if (s >= 0) {
255 if (s < 0x80) { /* latin */
256 CK((*filter->output_function)(s, filter->data));
257 } else if (s < 0x100) { /* kana */
258 CK((*filter->output_function)(0x8e, filter->data));
259 CK((*filter->output_function)(s, filter->data));
260 } else if (s < 0x8080) { /* X 0208 */
261 CK((*filter->output_function)(((s >> 8) & 0xff) | 0x80, filter->data));
262 CK((*filter->output_function)((s & 0xff) | 0x80, filter->data));
263 } else { /* X 0212 */
264 CK((*filter->output_function)(0x8f, filter->data));
265 CK((*filter->output_function)(((s >> 8) & 0xff) | 0x80, filter->data));
266 CK((*filter->output_function)((s & 0xff) | 0x80, filter->data));
267 }
268 } else {
269 CK(mbfl_filt_conv_illegal_output(c, filter));
270 }
271
272 return c;
273 }
274
mbfl_filt_ident_eucjp(int c,mbfl_identify_filter * filter)275 int mbfl_filt_ident_eucjp(int c, mbfl_identify_filter *filter)
276 {
277 switch (filter->status) {
278 case 0: /* latin */
279 if (c >= 0 && c < 0x80) { /* ok */
280 ;
281 } else if (c > 0xa0 && c < 0xff) { /* kanji first char */
282 filter->status = 1;
283 } else if (c == 0x8e) { /* kana first char */
284 filter->status = 2;
285 } else if (c == 0x8f) { /* X 0212 first char */
286 filter->status = 3;
287 } else { /* bad */
288 filter->flag = 1;
289 }
290 break;
291
292 case 1: /* got first half */
293 if (c < 0xa1 || c > 0xfe) { /* bad */
294 filter->flag = 1;
295 }
296 filter->status = 0;
297 break;
298
299 case 2: /* got 0x8e */
300 if (c < 0xa1 || c > 0xdf) { /* bad */
301 filter->flag = 1;
302 }
303 filter->status = 0;
304 break;
305
306 case 3: /* got 0x8f */
307 if (c < 0xa1 || c > 0xfe) { /* bad */
308 filter->flag = 1;
309 }
310 filter->status++;
311 break;
312 case 4: /* got 0x8f */
313 if (c < 0xa1 || c > 0xfe) { /* bad */
314 filter->flag = 1;
315 }
316 filter->status = 0;
317 break;
318
319 default:
320 filter->status = 0;
321 break;
322 }
323
324 return c;
325 }
326