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