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