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_iso2022_kr.h"
36 #include "unicode_table_uhc.h"
37
38 static int mbfl_filt_ident_2022kr(int c, mbfl_identify_filter *filter);
39
40 const mbfl_encoding mbfl_encoding_2022kr = {
41 mbfl_no_encoding_2022kr,
42 "ISO-2022-KR",
43 "ISO-2022-KR",
44 NULL,
45 NULL,
46 MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_SHFTCODE | MBFL_ENCTYPE_GL_UNSAFE,
47 &vtbl_2022kr_wchar,
48 &vtbl_wchar_2022kr
49 };
50
51 const struct mbfl_identify_vtbl vtbl_identify_2022kr = {
52 mbfl_no_encoding_2022kr,
53 mbfl_filt_ident_common_ctor,
54 mbfl_filt_ident_common_dtor,
55 mbfl_filt_ident_2022kr
56 };
57
58 const struct mbfl_convert_vtbl vtbl_wchar_2022kr = {
59 mbfl_no_encoding_wchar,
60 mbfl_no_encoding_2022kr,
61 mbfl_filt_conv_common_ctor,
62 mbfl_filt_conv_common_dtor,
63 mbfl_filt_conv_wchar_2022kr,
64 mbfl_filt_conv_any_2022kr_flush
65 };
66
67 const struct mbfl_convert_vtbl vtbl_2022kr_wchar = {
68 mbfl_no_encoding_2022kr,
69 mbfl_no_encoding_wchar,
70 mbfl_filt_conv_common_ctor,
71 mbfl_filt_conv_common_dtor,
72 mbfl_filt_conv_2022kr_wchar,
73 mbfl_filt_conv_common_flush
74 };
75
76 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
77
78 /*
79 * ISO-2022-KR => wchar
80 */
81 int
mbfl_filt_conv_2022kr_wchar(int c,mbfl_convert_filter * filter)82 mbfl_filt_conv_2022kr_wchar(int c, mbfl_convert_filter *filter)
83 {
84 int c1, w, flag;
85
86 retry:
87 switch (filter->status & 0xf) {
88 /* case 0x00: ASCII */
89 /* case 0x10: KSC5601 */
90 case 0:
91 if (c == 0x1b) { /* ESC */
92 filter->status += 2;
93 } else if (c == 0x0f) { /* SI (ASCII) */
94 filter->status &= ~0xff;
95 } else if (c == 0x0e) { /* SO (KSC5601) */
96 filter->status |= 0x10;
97 } else if ((filter->status & 0x10) != 0 && c > 0x20 && c < 0x7f) {
98 /* KSC5601 lead byte */
99 filter->cache = c;
100 filter->status += 1;
101 } else if ((filter->status & 0x10) == 0 && c >= 0 && c < 0x80) {
102 /* latin, CTLs */
103 CK((*filter->output_function)(c, filter->data));
104 } else {
105 w = c & MBFL_WCSGROUP_MASK;
106 w |= MBFL_WCSGROUP_THROUGH;
107 CK((*filter->output_function)(w, filter->data));
108 }
109 break;
110
111 case 1: /* dbcs second byte */
112 filter->status &= ~0xf;
113 c1 = filter->cache;
114 flag = 0;
115 if (c1 > 0x20 && c1 < 0x47) {
116 flag = 1;
117 } else if (c1 >= 0x47 && c1 <= 0x7e && c1 != 0x49) {
118 flag = 2;
119 }
120 if (flag > 0 && c > 0x20 && c < 0x7f) {
121 if (flag == 1){
122 w = (c1 - 0x21)*190 + (c - 0x41) + 0x80;
123 if (w >= 0 && w < uhc2_ucs_table_size) {
124 w = uhc2_ucs_table[w];
125 } else {
126 w = 0;
127 }
128 } else {
129 w = (c1 - 0x47)*94 + (c - 0x21);
130 if (w >= 0 && w < uhc3_ucs_table_size) {
131 w = uhc3_ucs_table[w];
132 } else {
133 w = 0;
134 }
135 }
136
137 if (w <= 0) {
138 w = (c1 << 8) | c;
139 w &= MBFL_WCSPLANE_MASK;
140 w |= MBFL_WCSPLANE_KSC5601;
141 }
142 CK((*filter->output_function)(w, filter->data));
143 } else if (c == 0x1b) { /* ESC */
144 filter->status++;
145 } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
146 CK((*filter->output_function)(c, filter->data));
147 } else {
148 w = (c1 << 8) | c;
149 w &= MBFL_WCSGROUP_MASK;
150 w |= MBFL_WCSGROUP_THROUGH;
151 CK((*filter->output_function)(w, filter->data));
152 }
153 break;
154
155 case 2: /* ESC */
156 if (c == 0x24) { /* '$' */
157 filter->status++;
158 } else {
159 filter->status &= ~0xf;
160 CK((*filter->output_function)(0x1b, filter->data));
161 goto retry;
162 }
163 break;
164 case 3: /* ESC $ */
165 if (c == 0x29) { /* ')' */
166 filter->status++;
167 } else {
168 filter->status &= ~0xf;
169 CK((*filter->output_function)(0x1b, filter->data));
170 CK((*filter->output_function)(0x24, filter->data));
171 goto retry;
172 }
173 break;
174 case 4: /* ESC $ ) */
175 if (c == 0x43) { /* 'C' */
176 filter->status &= ~0xf;
177 filter->status |= 0x100;
178 } else {
179 filter->status &= ~0xf;
180 CK((*filter->output_function)(0x1b, filter->data));
181 CK((*filter->output_function)(0x24, filter->data));
182 CK((*filter->output_function)(0x29, filter->data));
183 goto retry;
184 }
185 break;
186 default:
187 filter->status = 0;
188 break;
189 }
190
191 return c;
192 }
193
194 /*
195 * wchar => ISO-2022-KR
196 */
197 int
mbfl_filt_conv_wchar_2022kr(int c,mbfl_convert_filter * filter)198 mbfl_filt_conv_wchar_2022kr(int c, mbfl_convert_filter *filter)
199 {
200 int c1, c2, s;
201
202 s = 0;
203
204 if (c >= ucs_a1_uhc_table_min && c < ucs_a1_uhc_table_max) {
205 s = ucs_a1_uhc_table[c - ucs_a1_uhc_table_min];
206 } else if (c >= ucs_a2_uhc_table_min && c < ucs_a2_uhc_table_max) {
207 s = ucs_a2_uhc_table[c - ucs_a2_uhc_table_min];
208 } else if (c >= ucs_a3_uhc_table_min && c < ucs_a3_uhc_table_max) {
209 s = ucs_a3_uhc_table[c - ucs_a3_uhc_table_min];
210 } else if (c >= ucs_i_uhc_table_min && c < ucs_i_uhc_table_max) {
211 s = ucs_i_uhc_table[c - ucs_i_uhc_table_min];
212 } else if (c >= ucs_s_uhc_table_min && c < ucs_s_uhc_table_max) {
213 s = ucs_s_uhc_table[c - ucs_s_uhc_table_min];
214 } else if (c >= ucs_r1_uhc_table_min && c < ucs_r1_uhc_table_max) {
215 s = ucs_r1_uhc_table[c - ucs_r1_uhc_table_min];
216 } else if (c >= ucs_r2_uhc_table_min && c < ucs_r2_uhc_table_max) {
217 s = ucs_r2_uhc_table[c - ucs_r2_uhc_table_min];
218 }
219
220 c1 = (s >> 8) & 0xff;
221 c2 = s & 0xff;
222 /* exclude UHC extension area */
223 if (c1 < 0xa1 || c2 < 0xa1){
224 s = c;
225 }
226 if (s & 0x8000) {
227 s -= 0x8080;
228 }
229
230 if (s <= 0) {
231 c1 = c & ~MBFL_WCSPLANE_MASK;
232 if (c1 == MBFL_WCSPLANE_KSC5601) {
233 s = c & MBFL_WCSPLANE_MASK;
234 }
235 if (c == 0) {
236 s = 0;
237 } else if (s <= 0) {
238 s = -1;
239 }
240 } else if ((s >= 0x80 && s < 0x2121) || (s > 0x8080)) {
241 s = -1;
242 }
243 if (s >= 0) {
244 if (s < 0x80 && s > 0) { /* ASCII */
245 if ((filter->status & 0x10) != 0) {
246 CK((*filter->output_function)(0x0f, filter->data)); /* SI */
247 filter->status &= ~0x10;
248 }
249 CK((*filter->output_function)(s, filter->data));
250 } else {
251 if ( (filter->status & 0x100) == 0) {
252 CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
253 CK((*filter->output_function)(0x24, filter->data)); /* '$' */
254 CK((*filter->output_function)(0x29, filter->data)); /* ')' */
255 CK((*filter->output_function)(0x43, filter->data)); /* 'C' */
256 filter->status |= 0x100;
257 }
258 if ((filter->status & 0x10) == 0) {
259 CK((*filter->output_function)(0x0e, filter->data)); /* SO */
260 filter->status |= 0x10;
261 }
262 CK((*filter->output_function)((s >> 8) & 0xff, filter->data));
263 CK((*filter->output_function)(s & 0xff, filter->data));
264 }
265 } else {
266 CK(mbfl_filt_conv_illegal_output(c, filter));
267 }
268
269 return c;
270 }
271
272 int
mbfl_filt_conv_any_2022kr_flush(mbfl_convert_filter * filter)273 mbfl_filt_conv_any_2022kr_flush(mbfl_convert_filter *filter)
274 {
275 /* back to ascii */
276 if ((filter->status & 0xff00) != 0) {
277 CK((*filter->output_function)(0x0f, filter->data)); /* SI */
278 }
279
280 filter->status &= 0xff;
281
282 if (filter->flush_function != NULL) {
283 return (*filter->flush_function)(filter->data);
284 }
285
286 return 0;
287 }
288
mbfl_filt_ident_2022kr(int c,mbfl_identify_filter * filter)289 static int mbfl_filt_ident_2022kr(int c, mbfl_identify_filter *filter)
290 {
291 retry:
292 switch (filter->status & 0xf) {
293 /* case 0x00: ASCII */
294 /* case 0x10: KSC5601 mode */
295 /* case 0x20: KSC5601 DBCS */
296 /* case 0x40: KSC5601 SBCS */
297 case 0:
298 if (!(filter->status & 0x10)) {
299 if (c == 0x1b)
300 filter->status += 2;
301 } else if (filter->status == 0x20 && c > 0x20 && c < 0x7f) { /* kanji first char */
302 filter->status += 1;
303 } else if (c >= 0 && c < 0x80) { /* latin, CTLs */
304 ;
305 } else {
306 filter->flag = 1; /* bad */
307 }
308 break;
309
310 /* case 0x21: KSC5601 second char */
311 case 1:
312 filter->status &= ~0xf;
313 if (c < 0x21 || c > 0x7e) { /* bad */
314 filter->flag = 1;
315 }
316 break;
317
318 /* ESC */
319 case 2:
320 if (c == 0x24) { /* '$' */
321 filter->status++;
322 } else {
323 filter->flag = 1; /* bad */
324 filter->status &= ~0xf;
325 goto retry;
326 }
327 break;
328
329 /* ESC $ */
330 case 3:
331 if (c == 0x29) { /* ')' */
332 filter->status++;
333 } else {
334 filter->flag = 1; /* bad */
335 filter->status &= ~0xf;
336 goto retry;
337 }
338 break;
339
340 /* ESC $) */
341 case 5:
342 if (c == 0x43) { /* 'C' */
343 filter->status = 0x10;
344 } else {
345 filter->flag = 1; /* bad */
346 filter->status &= ~0xf;
347 goto retry;
348 }
349 break;
350
351 default:
352 filter->status = 0;
353 break;
354 }
355
356 return c;
357 }
358