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 : static int mbfl_filt_ident_eucjp(int c, mbfl_identify_filter *filter);
41 :
42 : static 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
103 : mbfl_filt_conv_eucjp_wchar(int c, mbfl_convert_filter *filter)
104 16282 : {
105 : int c1, s, w;
106 :
107 16282 : switch (filter->status) {
108 : case 0:
109 14591 : if (c >= 0 && c < 0x80) { /* latin */
110 4300 : CK((*filter->output_function)(c, filter->data));
111 11982 : } else if (c > 0xa0 && c < 0xff) { /* X 0208 first char */
112 5991 : filter->status = 1;
113 5991 : filter->cache = c;
114 0 : } else if (c == 0x8e) { /* kana first char */
115 0 : filter->status = 2;
116 0 : } else if (c == 0x8f) { /* X 0212 first char */
117 0 : filter->status = 3;
118 : } else {
119 0 : w = c & MBFL_WCSGROUP_MASK;
120 0 : w |= MBFL_WCSGROUP_THROUGH;
121 0 : CK((*filter->output_function)(w, filter->data));
122 : }
123 10291 : break;
124 :
125 : case 1: /* got first half */
126 5991 : filter->status = 0;
127 5991 : c1 = filter->cache;
128 11980 : if (c > 0xa0 && c < 0xff) {
129 5991 : s = (c1 - 0xa1)*94 + c - 0xa1;
130 11982 : if (s >= 0 && s < jisx0208_ucs_table_size) {
131 5991 : w = jisx0208_ucs_table[s];
132 : } else {
133 0 : w = 0;
134 : }
135 5991 : if (w <= 0) {
136 0 : w = ((c1 & 0x7f) << 8) | (c & 0x7f);
137 0 : w &= MBFL_WCSPLANE_MASK;
138 0 : w |= MBFL_WCSPLANE_JIS0208;
139 : }
140 5991 : CK((*filter->output_function)(w, filter->data));
141 0 : } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
142 0 : CK((*filter->output_function)(c, filter->data));
143 : } else {
144 0 : w = (c1 << 8) | c;
145 0 : w &= MBFL_WCSGROUP_MASK;
146 0 : w |= MBFL_WCSGROUP_THROUGH;
147 0 : CK((*filter->output_function)(w, filter->data));
148 : }
149 5989 : break;
150 :
151 : case 2: /* got 0x8e */
152 0 : filter->status = 0;
153 0 : if (c > 0xa0 && c < 0xe0) {
154 0 : w = 0xfec0 + c;
155 0 : CK((*filter->output_function)(w, filter->data));
156 0 : } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
157 0 : CK((*filter->output_function)(c, filter->data));
158 : } else {
159 0 : w = 0x8e00 | c;
160 0 : w &= MBFL_WCSGROUP_MASK;
161 0 : w |= MBFL_WCSGROUP_THROUGH;
162 0 : CK((*filter->output_function)(w, filter->data));
163 : }
164 0 : break;
165 :
166 : case 3: /* got 0x8f, X 0212 first char */
167 0 : if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
168 0 : CK((*filter->output_function)(c, filter->data));
169 0 : filter->status = 0;
170 : } else {
171 0 : filter->status++;
172 0 : filter->cache = c;
173 : }
174 0 : break;
175 : case 4: /* got 0x8f, X 0212 second char */
176 0 : filter->status = 0;
177 0 : c1 = filter->cache;
178 0 : if (c1 > 0xa0 && c1 < 0xff && c > 0xa0 && c < 0xff) {
179 0 : s = (c1 - 0xa1)*94 + c - 0xa1;
180 0 : if (s >= 0 && s < jisx0212_ucs_table_size) {
181 0 : w = jisx0212_ucs_table[s];
182 : } else {
183 0 : w = 0;
184 : }
185 0 : if (w <= 0) {
186 0 : w = ((c1 & 0x7f) << 8) | (c & 0x7f);
187 0 : w &= MBFL_WCSPLANE_MASK;
188 0 : w |= MBFL_WCSPLANE_JIS0212;
189 : }
190 0 : CK((*filter->output_function)(w, filter->data));
191 0 : } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
192 0 : CK((*filter->output_function)(c, filter->data));
193 : } else {
194 0 : w = (c1 << 8) | c | 0x8f0000;
195 0 : w &= MBFL_WCSGROUP_MASK;
196 0 : w |= MBFL_WCSGROUP_THROUGH;
197 0 : CK((*filter->output_function)(w, filter->data));
198 : }
199 0 : break;
200 :
201 : default:
202 0 : filter->status = 0;
203 : break;
204 : }
205 :
206 16280 : return c;
207 : }
208 :
209 : /*
210 : * wchar => EUC-JP
211 : */
212 : int
213 : mbfl_filt_conv_wchar_eucjp(int c, mbfl_convert_filter *filter)
214 3540 : {
215 : int c1, s;
216 :
217 3540 : s = 0;
218 4938 : if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
219 1398 : s = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
220 3288 : } else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
221 1146 : s = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
222 1726 : } else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
223 730 : s = ucs_i_jis_table[c - ucs_i_jis_table_min];
224 266 : } else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
225 266 : s = ucs_r_jis_table[c - ucs_r_jis_table_min];
226 : }
227 3540 : if (s <= 0) {
228 20 : c1 = c & ~MBFL_WCSPLANE_MASK;
229 20 : if (c1 == MBFL_WCSPLANE_JIS0208) {
230 0 : s = c & MBFL_WCSPLANE_MASK;
231 20 : } else if (c1 == MBFL_WCSPLANE_JIS0212) {
232 0 : s = c & MBFL_WCSPLANE_MASK;
233 0 : s |= 0x8080;
234 20 : } else if (c == 0xff3c) { /* FULLWIDTH REVERSE SOLIDUS */
235 0 : s = 0x2140;
236 20 : } else if (c == 0xff5e) { /* FULLWIDTH TILDE */
237 0 : s = 0x2141;
238 20 : } else if (c == 0x2225) { /* PARALLEL TO */
239 0 : s = 0x2142;
240 20 : } else if (c == 0xff0d) { /* FULLWIDTH HYPHEN-MINUS */
241 0 : s = 0x215d;
242 20 : } else if (c == 0xffe0) { /* FULLWIDTH CENT SIGN */
243 0 : s = 0x2171;
244 20 : } else if (c == 0xffe1) { /* FULLWIDTH POUND SIGN */
245 0 : s = 0x2172;
246 20 : } else if (c == 0xffe2) { /* FULLWIDTH NOT SIGN */
247 0 : s = 0x224c;
248 : }
249 20 : if (c == 0) {
250 0 : s = 0;
251 20 : } else if (s <= 0) {
252 20 : s = -1;
253 : }
254 : }
255 3540 : if (s >= 0) {
256 3520 : if (s < 0x80) { /* latin */
257 1328 : CK((*filter->output_function)(s, filter->data));
258 2192 : } else if (s < 0x100) { /* kana */
259 0 : CK((*filter->output_function)(0x8e, filter->data));
260 0 : CK((*filter->output_function)(s, filter->data));
261 2192 : } else if (s < 0x8080) { /* X 0208 */
262 2146 : CK((*filter->output_function)(((s >> 8) & 0xff) | 0x80, filter->data));
263 2146 : CK((*filter->output_function)((s & 0xff) | 0x80, filter->data));
264 : } else { /* X 0212 */
265 46 : CK((*filter->output_function)(0x8f, filter->data));
266 46 : CK((*filter->output_function)(((s >> 8) & 0xff) | 0x80, filter->data));
267 46 : CK((*filter->output_function)((s & 0xff) | 0x80, filter->data));
268 : }
269 : } else {
270 20 : if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
271 20 : CK(mbfl_filt_conv_illegal_output(c, filter));
272 : }
273 : }
274 :
275 3540 : return c;
276 : }
277 :
278 : static int mbfl_filt_ident_eucjp(int c, mbfl_identify_filter *filter)
279 281 : {
280 281 : switch (filter->status) {
281 : case 0: /* latin */
282 265 : if (c >= 0 && c < 0x80) { /* ok */
283 : ;
284 36 : } else if (c > 0xa0 && c < 0xff) { /* kanji first char */
285 17 : filter->status = 1;
286 2 : } else if (c == 0x8e) { /* kana first char */
287 0 : filter->status = 2;
288 2 : } else if (c == 0x8f) { /* X 0212 first char */
289 0 : filter->status = 3;
290 : } else { /* bad */
291 2 : filter->flag = 1;
292 : }
293 265 : break;
294 :
295 : case 1: /* got first half */
296 16 : if (c < 0xa1 || c > 0xfe) { /* bad */
297 0 : filter->flag = 1;
298 : }
299 16 : filter->status = 0;
300 16 : break;
301 :
302 : case 2: /* got 0x8e */
303 0 : if (c < 0xa1 || c > 0xdf) { /* bad */
304 0 : filter->flag = 1;
305 : }
306 0 : filter->status = 0;
307 0 : break;
308 :
309 : case 3: /* got 0x8f */
310 0 : if (c < 0xa1 || c > 0xfe) { /* bad */
311 0 : filter->flag = 1;
312 : }
313 0 : filter->status++;
314 0 : break;
315 : case 4: /* got 0x8f */
316 0 : if (c < 0xa1 || c > 0xfe) { /* bad */
317 0 : filter->flag = 1;
318 : }
319 0 : filter->status = 0;
320 0 : break;
321 :
322 : default:
323 0 : filter->status = 0;
324 : break;
325 : }
326 :
327 281 : return c;
328 : }
329 :
330 :
331 :
|