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_cn.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_cn.h"
36
37 #include "unicode_table_cp936.h"
38
39 static int mbfl_filt_ident_euccn(int c, mbfl_identify_filter *filter);
40
41 static const unsigned char mblen_table_euccn[] = { /* 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, 1, 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 static const char *mbfl_encoding_euc_cn_aliases[] = {"CN-GB", "EUC_CN", "eucCN", "x-euc-cn", "gb2312", NULL};
61
62 const mbfl_encoding mbfl_encoding_euc_cn = {
63 mbfl_no_encoding_euc_cn,
64 "EUC-CN",
65 "CN-GB",
66 (const char *(*)[])&mbfl_encoding_euc_cn_aliases,
67 mblen_table_euccn,
68 MBFL_ENCTYPE_MBCS,
69 &vtbl_euccn_wchar,
70 &vtbl_wchar_euccn
71 };
72
73 const struct mbfl_identify_vtbl vtbl_identify_euccn = {
74 mbfl_no_encoding_euc_cn,
75 mbfl_filt_ident_common_ctor,
76 mbfl_filt_ident_common_dtor,
77 mbfl_filt_ident_euccn
78 };
79
80 const struct mbfl_convert_vtbl vtbl_euccn_wchar = {
81 mbfl_no_encoding_euc_cn,
82 mbfl_no_encoding_wchar,
83 mbfl_filt_conv_common_ctor,
84 mbfl_filt_conv_common_dtor,
85 mbfl_filt_conv_euccn_wchar,
86 mbfl_filt_conv_common_flush
87 };
88
89 const struct mbfl_convert_vtbl vtbl_wchar_euccn = {
90 mbfl_no_encoding_wchar,
91 mbfl_no_encoding_euc_cn,
92 mbfl_filt_conv_common_ctor,
93 mbfl_filt_conv_common_dtor,
94 mbfl_filt_conv_wchar_euccn,
95 mbfl_filt_conv_common_flush
96 };
97
98 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
99
100 /*
101 * EUC-CN => wchar
102 */
103 int
mbfl_filt_conv_euccn_wchar(int c,mbfl_convert_filter * filter)104 mbfl_filt_conv_euccn_wchar(int c, mbfl_convert_filter *filter)
105 {
106 int c1, w;
107
108 switch (filter->status) {
109 case 0:
110 if (c >= 0 && c < 0x80) { /* latin */
111 CK((*filter->output_function)(c, filter->data));
112 } else if (c > 0xa0 && c < 0xff) { /* dbcs lead byte */
113 filter->status = 1;
114 filter->cache = c;
115 } else {
116 w = c & MBFL_WCSGROUP_MASK;
117 w |= MBFL_WCSGROUP_THROUGH;
118 CK((*filter->output_function)(w, filter->data));
119 }
120 break;
121
122 case 1: /* dbcs second byte */
123 filter->status = 0;
124 c1 = filter->cache;
125 if (c1 > 0xa0 && c1 < 0xff && c > 0xa0 && c < 0xff) {
126 w = (c1 - 0x81)*192 + (c - 0x40);
127 if (w >= 0 && w < cp936_ucs_table_size) {
128 w = cp936_ucs_table[w];
129 } else {
130 w = 0;
131 }
132 if (w <= 0) {
133 w = (c1 << 8) | c;
134 w &= MBFL_WCSPLANE_MASK;
135 w |= MBFL_WCSPLANE_GB2312;
136 }
137 CK((*filter->output_function)(w, filter->data));
138 } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
139 CK((*filter->output_function)(c, filter->data));
140 } else {
141 w = (c1 << 8) | c;
142 w &= MBFL_WCSGROUP_MASK;
143 w |= MBFL_WCSGROUP_THROUGH;
144 CK((*filter->output_function)(w, filter->data));
145 }
146 break;
147
148 default:
149 filter->status = 0;
150 break;
151 }
152
153 return c;
154 }
155
156 /*
157 * wchar => EUC-CN
158 */
159 int
mbfl_filt_conv_wchar_euccn(int c,mbfl_convert_filter * filter)160 mbfl_filt_conv_wchar_euccn(int c, mbfl_convert_filter *filter)
161 {
162 int c1, c2, s;
163
164 s = 0;
165 if (c >= ucs_a1_cp936_table_min && c < ucs_a1_cp936_table_max) {
166 s = ucs_a1_cp936_table[c - ucs_a1_cp936_table_min];
167 } else if (c >= ucs_a2_cp936_table_min && c < ucs_a2_cp936_table_max) {
168 s = ucs_a2_cp936_table[c - ucs_a2_cp936_table_min];
169 } else if (c >= ucs_a3_cp936_table_min && c < ucs_a3_cp936_table_max) {
170 s = ucs_a3_cp936_table[c - ucs_a3_cp936_table_min];
171 } else if (c >= ucs_i_cp936_table_min && c < ucs_i_cp936_table_max) {
172 s = ucs_i_cp936_table[c - ucs_i_cp936_table_min];
173 } else if (c >= ucs_hff_cp936_table_min && c < ucs_hff_cp936_table_max) {
174 if (c == 0xff04) {
175 s = 0xa1e7;
176 } else if (c == 0xff5e) {
177 s = 0xa1ab;
178 } else if (c >= 0xff01 && c <= 0xff5d) {
179 s = c - 0xff01 + 0xa3a1;
180 } else if (c >= 0xffe0 && c <= 0xffe5) {
181 s = ucs_hff_s_cp936_table[c-0xffe0];
182 }
183 }
184 c1 = (s >> 8) & 0xff;
185 c2 = s & 0xff;
186
187 if (c1 < 0xa1 || c2 < 0xa1) { /* exclude CP936 extension */
188 s = c;
189 }
190
191 if (s <= 0) {
192 c1 = c & ~MBFL_WCSPLANE_MASK;
193 if (c1 == MBFL_WCSPLANE_GB2312) {
194 s = c & MBFL_WCSPLANE_MASK;
195 }
196 if (c == 0) {
197 s = 0;
198 } else if (s <= 0) {
199 s = -1;
200 }
201 }
202 if (s >= 0) {
203 if (s < 0x80) { /* latin */
204 CK((*filter->output_function)(s, filter->data));
205 } else {
206 CK((*filter->output_function)((s >> 8) & 0xff, filter->data));
207 CK((*filter->output_function)(s & 0xff, filter->data));
208 }
209 } else {
210 CK(mbfl_filt_conv_illegal_output(c, filter));
211 }
212
213 return c;
214 }
215
mbfl_filt_ident_euccn(int c,mbfl_identify_filter * filter)216 static int mbfl_filt_ident_euccn(int c, mbfl_identify_filter *filter)
217 {
218 switch (filter->status) {
219 case 0: /* latin */
220 if (c >= 0 && c < 0x80) { /* ok */
221 ;
222 } else if (c > 0xa0 && c < 0xff) { /* DBCS lead byte */
223 filter->status = 1;
224 } else { /* bad */
225 filter->flag = 1;
226 }
227 break;
228
229 case 1: /* got lead byte */
230 if (c < 0xa1 || c > 0xfe) { /* bad */
231 filter->flag = 1;
232 }
233 filter->status = 0;
234 break;
235
236 default:
237 filter->status = 0;
238 break;
239 }
240
241 return c;
242 }
243