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