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_kr.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_kr.h"
36 #include "unicode_table_uhc.h"
37
38 static int mbfl_filt_ident_euckr(int c, mbfl_identify_filter *filter);
39
40 static const unsigned char mblen_table_euckr[] = { /* 0xA1-0xFE */
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, 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, 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, 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, 1
57 };
58
59 static const char *mbfl_encoding_euc_kr_aliases[] = {"EUC_KR", "eucKR", "x-euc-kr", NULL};
60
61 const mbfl_encoding mbfl_encoding_euc_kr = {
62 mbfl_no_encoding_euc_kr,
63 "EUC-KR",
64 "EUC-KR",
65 (const char *(*)[])&mbfl_encoding_euc_kr_aliases,
66 mblen_table_euckr,
67 MBFL_ENCTYPE_MBCS,
68 &vtbl_euckr_wchar,
69 &vtbl_wchar_euckr
70 };
71
72 const struct mbfl_identify_vtbl vtbl_identify_euckr = {
73 mbfl_no_encoding_euc_kr,
74 mbfl_filt_ident_common_ctor,
75 mbfl_filt_ident_common_dtor,
76 mbfl_filt_ident_euckr
77 };
78
79 const struct mbfl_convert_vtbl vtbl_euckr_wchar = {
80 mbfl_no_encoding_euc_kr,
81 mbfl_no_encoding_wchar,
82 mbfl_filt_conv_common_ctor,
83 mbfl_filt_conv_common_dtor,
84 mbfl_filt_conv_euckr_wchar,
85 mbfl_filt_conv_common_flush
86 };
87
88 const struct mbfl_convert_vtbl vtbl_wchar_euckr = {
89 mbfl_no_encoding_wchar,
90 mbfl_no_encoding_euc_kr,
91 mbfl_filt_conv_common_ctor,
92 mbfl_filt_conv_common_dtor,
93 mbfl_filt_conv_wchar_euckr,
94 mbfl_filt_conv_common_flush
95 };
96
97
98 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
99
100 /*
101 * EUC-KR => wchar
102 */
103 int
mbfl_filt_conv_euckr_wchar(int c,mbfl_convert_filter * filter)104 mbfl_filt_conv_euckr_wchar(int c, mbfl_convert_filter *filter)
105 {
106 int c1, w, flag;
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 && c != 0xc9) { /* 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 flag = 0;
126 if (c1 >= 0xa1 && c1 <= 0xc6) {
127 flag = 1;
128 } else if (c1 >= 0xc7 && c1 <= 0xfe && c1 != 0xc9) {
129 flag = 2;
130 }
131 if (flag > 0 && c >= 0xa1 && c <= 0xfe) {
132 if (flag == 1){ /* 1st: 0xa1..0xc6, 2nd: 0x41..0x7a, 0x81..0xfe */
133 w = (c1 - 0xa1)*190 + (c - 0x41);
134 if (w >= 0 && w < uhc2_ucs_table_size) {
135 w = uhc2_ucs_table[w];
136 } else {
137 w = 0;
138 }
139 } else { /* 1st: 0xc7..0xc8,0xca..0xfe, 2nd: 0xa1..0xfe */
140 w = (c1 - 0xc7)*94 + (c - 0xa1);
141 if (w >= 0 && w < uhc3_ucs_table_size) {
142 w = uhc3_ucs_table[w];
143 } else {
144 w = 0;
145 }
146 }
147
148 if (w <= 0) {
149 w = (c1 << 8) | c;
150 w &= MBFL_WCSPLANE_MASK;
151 w |= MBFL_WCSPLANE_KSC5601;
152 }
153 CK((*filter->output_function)(w, filter->data));
154 } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
155 CK((*filter->output_function)(c, filter->data));
156 } else {
157 w = (c1 << 8) | c;
158 w &= MBFL_WCSGROUP_MASK;
159 w |= MBFL_WCSGROUP_THROUGH;
160 CK((*filter->output_function)(w, filter->data));
161 }
162 break;
163
164 default:
165 filter->status = 0;
166 break;
167 }
168
169 return c;
170 }
171
172 /*
173 * wchar => EUC-KR
174 */
175 int
mbfl_filt_conv_wchar_euckr(int c,mbfl_convert_filter * filter)176 mbfl_filt_conv_wchar_euckr(int c, mbfl_convert_filter *filter)
177 {
178 int c1, c2, s;
179
180 s = 0;
181
182 if (c >= ucs_a1_uhc_table_min && c < ucs_a1_uhc_table_max) {
183 s = ucs_a1_uhc_table[c - ucs_a1_uhc_table_min];
184 } else if (c >= ucs_a2_uhc_table_min && c < ucs_a2_uhc_table_max) {
185 s = ucs_a2_uhc_table[c - ucs_a2_uhc_table_min];
186 } else if (c >= ucs_a3_uhc_table_min && c < ucs_a3_uhc_table_max) {
187 s = ucs_a3_uhc_table[c - ucs_a3_uhc_table_min];
188 } else if (c >= ucs_i_uhc_table_min && c < ucs_i_uhc_table_max) {
189 s = ucs_i_uhc_table[c - ucs_i_uhc_table_min];
190 } else if (c >= ucs_s_uhc_table_min && c < ucs_s_uhc_table_max) {
191 s = ucs_s_uhc_table[c - ucs_s_uhc_table_min];
192 } else if (c >= ucs_r1_uhc_table_min && c < ucs_r1_uhc_table_max) {
193 s = ucs_r1_uhc_table[c - ucs_r1_uhc_table_min];
194 } else if (c >= ucs_r2_uhc_table_min && c < ucs_r2_uhc_table_max) {
195 s = ucs_r2_uhc_table[c - ucs_r2_uhc_table_min];
196 }
197
198 c1 = (s >> 8) & 0xff;
199 c2 = s & 0xff;
200 /* exclude UHC extension area */
201 if (c1 < 0xa1 || c2 < 0xa1){
202 s = c;
203 }
204
205 if (s <= 0) {
206 c1 = c & ~MBFL_WCSPLANE_MASK;
207 if (c1 == MBFL_WCSPLANE_KSC5601) {
208 s = c & MBFL_WCSPLANE_MASK;
209 }
210 if (c == 0) {
211 s = 0;
212 } else if (s <= 0) {
213 s = -1;
214 }
215 }
216 if (s >= 0) {
217 if (s < 0x80) { /* latin */
218 CK((*filter->output_function)(s, filter->data));
219 } else {
220 CK((*filter->output_function)((s >> 8) & 0xff, filter->data));
221 CK((*filter->output_function)(s & 0xff, filter->data));
222 }
223 } else {
224 CK(mbfl_filt_conv_illegal_output(c, filter));
225 }
226
227 return c;
228 }
229
mbfl_filt_ident_euckr(int c,mbfl_identify_filter * filter)230 static int mbfl_filt_ident_euckr(int c, mbfl_identify_filter *filter)
231 {
232 switch (filter->status) {
233 case 0: /* latin */
234 if (c >= 0 && c < 0x80) { /* ok */
235 ;
236 } else if (c > 0xa0 && c < 0xff) { /* DBCS lead byte */
237 filter->status = 1;
238 } else { /* bad */
239 filter->flag = 1;
240 }
241 break;
242
243 case 1: /* got lead byte */
244 if (c < 0xa1 || c > 0xfe) { /* bad */
245 filter->flag = 1;
246 }
247 filter->status = 0;
248 break;
249
250 default:
251 filter->status = 0;
252 break;
253 }
254
255 return c;
256 }
257