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_conv_euckr_wchar_flush(mbfl_convert_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 	mbfl_encoding_euc_kr_aliases,
62 	mblen_table_euckr,
63 	0,
64 	&vtbl_euckr_wchar,
65 	&vtbl_wchar_euckr
66 };
67 
68 const struct mbfl_convert_vtbl vtbl_euckr_wchar = {
69 	mbfl_no_encoding_euc_kr,
70 	mbfl_no_encoding_wchar,
71 	mbfl_filt_conv_common_ctor,
72 	NULL,
73 	mbfl_filt_conv_euckr_wchar,
74 	mbfl_filt_conv_euckr_wchar_flush,
75 	NULL,
76 };
77 
78 const struct mbfl_convert_vtbl vtbl_wchar_euckr = {
79 	mbfl_no_encoding_wchar,
80 	mbfl_no_encoding_euc_kr,
81 	mbfl_filt_conv_common_ctor,
82 	NULL,
83 	mbfl_filt_conv_wchar_euckr,
84 	mbfl_filt_conv_common_flush,
85 	NULL,
86 };
87 
88 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
89 
mbfl_filt_conv_euckr_wchar(int c,mbfl_convert_filter * filter)90 int mbfl_filt_conv_euckr_wchar(int c, mbfl_convert_filter *filter)
91 {
92 	int c1, w, flag;
93 
94 	switch (filter->status) {
95 	case 0:
96 		if (c >= 0 && c < 0x80) { /* latin */
97 			CK((*filter->output_function)(c, filter->data));
98 		} else if (((c >= 0xA1 && c <= 0xAC) || (c >= 0xB0 && c <= 0xFD)) && c != 0xC9) { /* dbcs lead byte */
99 			filter->status = 1;
100 			filter->cache = c;
101 		} else {
102 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
103 		}
104 		break;
105 
106 	case 1: /* dbcs second byte */
107 		filter->status = 0;
108 		c1 = filter->cache;
109 		flag = 0;
110 		if (c1 >= 0xa1 && c1 <= 0xc6) {
111 			flag = 1;
112 		} else if (c1 >= 0xc7 && c1 <= 0xfe && c1 != 0xc9) {
113 			flag = 2;
114 		}
115 		if (flag > 0 && c >= 0xa1 && c <= 0xfe) {
116 			if (flag == 1) { /* 1st: 0xa1..0xc6, 2nd: 0x41..0x7a, 0x81..0xfe */
117 				w = (c1 - 0xa1)*190 + (c - 0x41);
118 				if (w >= 0 && w < uhc2_ucs_table_size) {
119 					w = uhc2_ucs_table[w];
120 				} else {
121 					w = 0;
122 				}
123 			} else { /* 1st: 0xc7..0xc8,0xca..0xfe, 2nd: 0xa1..0xfe */
124 				w = (c1 - 0xc7)*94 + (c - 0xa1);
125 				if (w >= 0 && w < uhc3_ucs_table_size) {
126 					w = uhc3_ucs_table[w];
127 				} else {
128 					w = 0;
129 				}
130 			}
131 
132 			if (w <= 0) {
133 				w = MBFL_BAD_INPUT;
134 			}
135 			CK((*filter->output_function)(w, filter->data));
136 		} else {
137 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
138 		}
139 		break;
140 
141 	default:
142 		filter->status = 0;
143 		break;
144 	}
145 
146 	return 0;
147 }
148 
mbfl_filt_conv_wchar_euckr(int c,mbfl_convert_filter * filter)149 int mbfl_filt_conv_wchar_euckr(int c, mbfl_convert_filter *filter)
150 {
151 	int s = 0;
152 
153 	if (c >= ucs_a1_uhc_table_min && c < ucs_a1_uhc_table_max) {
154 		s = ucs_a1_uhc_table[c - ucs_a1_uhc_table_min];
155 	} else if (c >= ucs_a2_uhc_table_min && c < ucs_a2_uhc_table_max) {
156 		s = ucs_a2_uhc_table[c - ucs_a2_uhc_table_min];
157 	} else if (c >= ucs_a3_uhc_table_min && c < ucs_a3_uhc_table_max) {
158 		s = ucs_a3_uhc_table[c - ucs_a3_uhc_table_min];
159 	} else if (c >= ucs_i_uhc_table_min && c < ucs_i_uhc_table_max) {
160 		s = ucs_i_uhc_table[c - ucs_i_uhc_table_min];
161 	} else if (c >= ucs_s_uhc_table_min && c < ucs_s_uhc_table_max) {
162 		s = ucs_s_uhc_table[c - ucs_s_uhc_table_min];
163 	} else if (c >= ucs_r1_uhc_table_min && c < ucs_r1_uhc_table_max) {
164 		s = ucs_r1_uhc_table[c - ucs_r1_uhc_table_min];
165 	} else if (c >= ucs_r2_uhc_table_min && c < ucs_r2_uhc_table_max) {
166 		s = ucs_r2_uhc_table[c - ucs_r2_uhc_table_min];
167 	}
168 
169 	/* exclude UHC extension area (although we are using the UHC conversion tables) */
170 	if (((s >> 8) & 0xFF) < 0xA1 || (s & 0xFF) < 0xA1) {
171 		s = 0;
172 	}
173 
174 	if (s <= 0) {
175 		if (c < 0x80) {
176 			s = c;
177 		} else {
178 			s = -1;
179 		}
180 	}
181 
182 	if (s >= 0) {
183 		if (s < 0x80) { /* latin */
184 			CK((*filter->output_function)(s, filter->data));
185 		} else {
186 			CK((*filter->output_function)((s >> 8) & 0xff, filter->data));
187 			CK((*filter->output_function)(s & 0xff, filter->data));
188 		}
189 	} else {
190 		CK(mbfl_filt_conv_illegal_output(c, filter));
191 	}
192 
193 	return 0;
194 }
195 
mbfl_filt_conv_euckr_wchar_flush(mbfl_convert_filter * filter)196 static int mbfl_filt_conv_euckr_wchar_flush(mbfl_convert_filter *filter)
197 {
198 	if (filter->status == 1) {
199 		/* 2-byte character was truncated */
200 		CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
201 	}
202 
203 	if (filter->flush_function) {
204 		(*filter->flush_function)(filter->data);
205 	}
206 
207 	return 0;
208 }
209