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: Rui Hirokawa <hirokawa@php.net>
22 *
23 */
24 /*
25 * The source code included in this files was separated from mbfilter_tw.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_tw.h"
36
37 #include "unicode_table_cns11643.h"
38
39 static int mbfl_filt_ident_euctw(int c, mbfl_identify_filter *filter);
40
41 static const unsigned char mblen_table_euctw[] = { /* 0xA1-0xFE */
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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1,
51 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
52 1, 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, 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, 1
58 };
59
60
61 static const char *mbfl_encoding_euc_tw_aliases[] = {"EUC_TW", "eucTW", "x-euc-tw", NULL};
62
63 const mbfl_encoding mbfl_encoding_euc_tw = {
64 mbfl_no_encoding_euc_tw,
65 "EUC-TW",
66 "EUC-TW",
67 (const char *(*)[])&mbfl_encoding_euc_tw_aliases,
68 mblen_table_euctw,
69 MBFL_ENCTYPE_MBCS,
70 &vtbl_euctw_wchar,
71 &vtbl_wchar_euctw
72 };
73
74 const struct mbfl_identify_vtbl vtbl_identify_euctw = {
75 mbfl_no_encoding_euc_tw,
76 mbfl_filt_ident_common_ctor,
77 mbfl_filt_ident_common_dtor,
78 mbfl_filt_ident_euctw
79 };
80
81 const struct mbfl_convert_vtbl vtbl_euctw_wchar = {
82 mbfl_no_encoding_euc_tw,
83 mbfl_no_encoding_wchar,
84 mbfl_filt_conv_common_ctor,
85 mbfl_filt_conv_common_dtor,
86 mbfl_filt_conv_euctw_wchar,
87 mbfl_filt_conv_common_flush
88 };
89
90 const struct mbfl_convert_vtbl vtbl_wchar_euctw = {
91 mbfl_no_encoding_wchar,
92 mbfl_no_encoding_euc_tw,
93 mbfl_filt_conv_common_ctor,
94 mbfl_filt_conv_common_dtor,
95 mbfl_filt_conv_wchar_euctw,
96 mbfl_filt_conv_common_flush
97 };
98
99 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
100
101 /*
102 * EUC-TW => wchar
103 */
104 int
mbfl_filt_conv_euctw_wchar(int c,mbfl_convert_filter * filter)105 mbfl_filt_conv_euctw_wchar(int c, mbfl_convert_filter *filter)
106 {
107 int c1, s, w, plane;
108
109 switch (filter->status) {
110 case 0:
111 if (c >= 0 && c < 0x80) { /* latin */
112 CK((*filter->output_function)(c, filter->data));
113 } else if (c > 0xa0 && c < 0xff) { /* dbcs first byte */
114 filter->status = 1;
115 filter->cache = c;
116 } else if (c == 0x8e) { /* mbcs first byte */
117 filter->status = 2;
118 filter->cache = c;
119 } else {
120 w = c & MBFL_WCSGROUP_MASK;
121 w |= MBFL_WCSGROUP_THROUGH;
122 CK((*filter->output_function)(w, filter->data));
123 }
124 break;
125
126 case 1: /* mbcs second byte */
127 filter->status = 0;
128 c1 = filter->cache;
129 if (c > 0xa0 && c < 0xff) {
130 w = (c1 - 0xa1)*94 + (c - 0xa1);
131 if (w >= 0 && w < cns11643_1_ucs_table_size) {
132 w = cns11643_1_ucs_table[w];
133 } else {
134 w = 0;
135 }
136 if (w <= 0) {
137 w = (c1 << 8) | c;
138 w &= MBFL_WCSPLANE_MASK;
139 w |= MBFL_WCSPLANE_CNS11643;
140 }
141 CK((*filter->output_function)(w, filter->data));
142 } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
143 CK((*filter->output_function)(c, filter->data));
144 } else {
145 w = (c1 << 8) | c;
146 w &= MBFL_WCSGROUP_MASK;
147 w |= MBFL_WCSGROUP_THROUGH;
148 CK((*filter->output_function)(w, filter->data));
149 }
150 break;
151
152 case 2: /* got 0x8e, first char */
153 c1 = filter->cache;
154 if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
155 CK((*filter->output_function)(c, filter->data));
156 filter->status = 0;
157 } else if (c > 0xa0 && c < 0xaf) {
158 filter->status = 3;
159 filter->cache = c - 0xa1;
160 } else {
161 w = (c1 << 8) | c;
162 w &= MBFL_WCSGROUP_MASK;
163 w |= MBFL_WCSGROUP_THROUGH;
164 CK((*filter->output_function)(w, filter->data));
165 }
166 break;
167
168 case 3: /* got 0x8e, third char */
169 filter->status = 0;
170 c1 = filter->cache;
171 if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
172 CK((*filter->output_function)(c, filter->data));
173 filter->status = 0;
174 } else if (c > 0xa0 && c < 0xff) {
175 filter->status = 4;
176 filter->cache = (c1 << 8) + c - 0xa1;
177 } else {
178 w = (c1 << 8) | c;
179 w &= MBFL_WCSGROUP_MASK;
180 w |= MBFL_WCSGROUP_THROUGH;
181 CK((*filter->output_function)(w, filter->data));
182 }
183 break;
184
185 case 4: /* mbcs fourth char */
186 filter->status = 0;
187 c1 = filter->cache;
188 if (c1 >= 0x100 && c1 <= 0xdff && c > 0xa0 && c < 0xff) {
189 plane = (c1 & 0xf00) >> 8;
190 s = (c1 & 0xff)*94 + c - 0xa1;
191 w = 0;
192 if (s >= 0) {
193 if (plane == 1 && s < cns11643_2_ucs_table_size) {
194 w = cns11643_2_ucs_table[s];
195 }
196 if (plane == 13 && s < cns11643_14_ucs_table_size) {
197 w = cns11643_14_ucs_table[s];
198 }
199 }
200 if (w <= 0) {
201 w = ((c1 & 0x7f) << 8) | (c & 0x7f);
202 w &= MBFL_WCSPLANE_MASK;
203 w |= MBFL_WCSPLANE_CNS11643;
204 }
205 CK((*filter->output_function)(w, filter->data));
206 } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
207 CK((*filter->output_function)(c, filter->data));
208 } else {
209 w = (c1 << 8) | c | 0x8e0000;
210 w &= MBFL_WCSGROUP_MASK;
211 w |= MBFL_WCSGROUP_THROUGH;
212 CK((*filter->output_function)(w, filter->data));
213 }
214 break;
215
216 default:
217 filter->status = 0;
218 break;
219 }
220
221 return c;
222 }
223
224 /*
225 * wchar => EUC-TW
226 */
227 int
mbfl_filt_conv_wchar_euctw(int c,mbfl_convert_filter * filter)228 mbfl_filt_conv_wchar_euctw(int c, mbfl_convert_filter *filter)
229 {
230 int c1, s, plane;
231
232 s = 0;
233 if (c >= ucs_a1_cns11643_table_min && c < ucs_a1_cns11643_table_max) {
234 s = ucs_a1_cns11643_table[c - ucs_a1_cns11643_table_min];
235 } else if (c >= ucs_a2_cns11643_table_min && c < ucs_a2_cns11643_table_max) {
236 s = ucs_a2_cns11643_table[c - ucs_a2_cns11643_table_min];
237 } else if (c >= ucs_a3_cns11643_table_min && c < ucs_a3_cns11643_table_max) {
238 s = ucs_a3_cns11643_table[c - ucs_a3_cns11643_table_min];
239 } else if (c >= ucs_i_cns11643_table_min && c < ucs_i_cns11643_table_max) {
240 s = ucs_i_cns11643_table[c - ucs_i_cns11643_table_min];
241 } else if (c >= ucs_r_cns11643_table_min && c < ucs_r_cns11643_table_max) {
242 s = ucs_r_cns11643_table[c - ucs_r_cns11643_table_min];
243 }
244 if (s <= 0) {
245 c1 = c & ~MBFL_WCSPLANE_MASK;
246 if (c1 == MBFL_WCSPLANE_CNS11643) {
247 s = c & MBFL_WCSPLANE_MASK;
248 }
249 if (c == 0) {
250 s = 0;
251 } else if (s <= 0) {
252 s = -1;
253 }
254 }
255 if (s >= 0) {
256 plane = (s & 0x1f0000) >> 16;
257 if (plane <= 1){
258 if (s < 0x80) { /* latin */
259 CK((*filter->output_function)(s, filter->data));
260 } else {
261 s = (s & 0xffff) | 0x8080;
262 CK((*filter->output_function)((s >> 8) & 0xff, filter->data));
263 CK((*filter->output_function)(s & 0xff, filter->data));
264 }
265 } else {
266 s = (0x8ea00000 + (plane << 16)) | ((s & 0xffff) | 0x8080);
267 CK((*filter->output_function)(0x8e , filter->data));
268 CK((*filter->output_function)((s >> 16) & 0xff, filter->data));
269 CK((*filter->output_function)((s >> 8) & 0xff, filter->data));
270 CK((*filter->output_function)(s & 0xff, filter->data));
271 }
272 } else {
273 if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
274 CK(mbfl_filt_conv_illegal_output(c, filter));
275 }
276 }
277 return c;
278 }
279
mbfl_filt_ident_euctw(int c,mbfl_identify_filter * filter)280 static int mbfl_filt_ident_euctw(int c, mbfl_identify_filter *filter)
281 {
282 switch (filter->status) {
283 case 0: /* latin */
284 if (c >= 0 && c < 0x80) { /* ok */
285 ;
286 } else if (c > 0xa0 && c < 0xff) { /* DBCS lead byte */
287 filter->status = 1;
288 } else if (c == 0x8e) { /* DBCS lead byte */
289 filter->status = 2;
290 } else { /* bad */
291 filter->flag = 1;
292 }
293 break;
294
295 case 1: /* got lead byte */
296 if (c < 0xa1 || c > 0xfe) { /* bad */
297 filter->flag = 1;
298 }
299 filter->status = 0;
300 break;
301
302 case 2: /* got lead byte */
303 if (c >= 0xa1 && c < 0xaf) { /* ok */
304 filter->status = 3;
305 } else {
306 filter->flag = 1; /* bad */
307 }
308 break;
309
310 case 3: /* got lead byte */
311 if (c < 0xa1 || c > 0xfe) { /* bad */
312 filter->flag = 1;
313 }
314 filter->status = 4;
315 break;
316
317 case 4: /* got lead byte */
318 if (c < 0xa1 || c > 0xfe) { /* bad */
319 filter->flag = 1;
320 }
321 filter->status = 0;
322 break;
323
324 default:
325 filter->status = 0;
326 break;
327 }
328
329 return c;
330 }
331