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 static size_t mb_euckr_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state);
36 static void mb_wchar_to_euckr(uint32_t *in, size_t len, mb_convert_buf *buf, bool end);
37 
38 static const unsigned char mblen_table_euckr[] = { /* 0xA1-0xFE */
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, 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, 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, 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, 1
55 };
56 
57 static const char *mbfl_encoding_euc_kr_aliases[] = {"EUC_KR", "eucKR", "x-euc-kr", NULL};
58 
59 const mbfl_encoding mbfl_encoding_euc_kr = {
60 	mbfl_no_encoding_euc_kr,
61 	"EUC-KR",
62 	"EUC-KR",
63 	mbfl_encoding_euc_kr_aliases,
64 	mblen_table_euckr,
65 	0,
66 	&vtbl_euckr_wchar,
67 	&vtbl_wchar_euckr,
68 	mb_euckr_to_wchar,
69 	mb_wchar_to_euckr,
70 	NULL
71 };
72 
73 const struct mbfl_convert_vtbl vtbl_euckr_wchar = {
74 	mbfl_no_encoding_euc_kr,
75 	mbfl_no_encoding_wchar,
76 	mbfl_filt_conv_common_ctor,
77 	NULL,
78 	mbfl_filt_conv_euckr_wchar,
79 	mbfl_filt_conv_euckr_wchar_flush,
80 	NULL,
81 };
82 
83 const struct mbfl_convert_vtbl vtbl_wchar_euckr = {
84 	mbfl_no_encoding_wchar,
85 	mbfl_no_encoding_euc_kr,
86 	mbfl_filt_conv_common_ctor,
87 	NULL,
88 	mbfl_filt_conv_wchar_euckr,
89 	mbfl_filt_conv_common_flush,
90 	NULL,
91 };
92 
93 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
94 
mbfl_filt_conv_euckr_wchar(int c,mbfl_convert_filter * filter)95 int mbfl_filt_conv_euckr_wchar(int c, mbfl_convert_filter *filter)
96 {
97 	int c1, w, flag;
98 
99 	switch (filter->status) {
100 	case 0:
101 		if (c >= 0 && c < 0x80) { /* latin */
102 			CK((*filter->output_function)(c, filter->data));
103 		} else if (((c >= 0xA1 && c <= 0xAC) || (c >= 0xB0 && c <= 0xFD)) && c != 0xC9) { /* dbcs lead byte */
104 			filter->status = 1;
105 			filter->cache = c;
106 		} else {
107 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
108 		}
109 		break;
110 
111 	case 1: /* dbcs second byte */
112 		filter->status = 0;
113 		c1 = filter->cache;
114 		flag = 0;
115 		if (c1 >= 0xa1 && c1 <= 0xc6) {
116 			flag = 1;
117 		} else if (c1 >= 0xc7 && c1 <= 0xfe && c1 != 0xc9) {
118 			flag = 2;
119 		}
120 		if (flag > 0 && c >= 0xa1 && c <= 0xfe) {
121 			if (flag == 1) { /* 1st: 0xa1..0xc6, 2nd: 0x41..0x7a, 0x81..0xfe */
122 				w = (c1 - 0xa1)*190 + c - 0x41;
123 				ZEND_ASSERT(w < uhc2_ucs_table_size);
124 				w = uhc2_ucs_table[w];
125 			} else { /* 1st: 0xc7..0xc8,0xca..0xfe, 2nd: 0xa1..0xfe */
126 				w = (c1 - 0xc7)*94 + c - 0xa1;
127 				ZEND_ASSERT(w < uhc3_ucs_table_size);
128 				w = uhc3_ucs_table[w];
129 			}
130 
131 			if (w <= 0) {
132 				w = MBFL_BAD_INPUT;
133 			}
134 			CK((*filter->output_function)(w, filter->data));
135 		} else {
136 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
137 		}
138 		break;
139 
140 		EMPTY_SWITCH_DEFAULT_CASE();
141 	}
142 
143 	return 0;
144 }
145 
mbfl_filt_conv_wchar_euckr(int c,mbfl_convert_filter * filter)146 int mbfl_filt_conv_wchar_euckr(int c, mbfl_convert_filter *filter)
147 {
148 	int s = 0;
149 
150 	if (c >= ucs_a1_uhc_table_min && c < ucs_a1_uhc_table_max) {
151 		s = ucs_a1_uhc_table[c - ucs_a1_uhc_table_min];
152 	} else if (c >= ucs_a2_uhc_table_min && c < ucs_a2_uhc_table_max) {
153 		s = ucs_a2_uhc_table[c - ucs_a2_uhc_table_min];
154 	} else if (c >= ucs_a3_uhc_table_min && c < ucs_a3_uhc_table_max) {
155 		s = ucs_a3_uhc_table[c - ucs_a3_uhc_table_min];
156 	} else if (c >= ucs_i_uhc_table_min && c < ucs_i_uhc_table_max) {
157 		s = ucs_i_uhc_table[c - ucs_i_uhc_table_min];
158 	} else if (c >= ucs_s_uhc_table_min && c < ucs_s_uhc_table_max) {
159 		s = ucs_s_uhc_table[c - ucs_s_uhc_table_min];
160 	} else if (c >= ucs_r1_uhc_table_min && c < ucs_r1_uhc_table_max) {
161 		s = ucs_r1_uhc_table[c - ucs_r1_uhc_table_min];
162 	} else if (c >= ucs_r2_uhc_table_min && c < ucs_r2_uhc_table_max) {
163 		s = ucs_r2_uhc_table[c - ucs_r2_uhc_table_min];
164 	}
165 
166 	/* exclude UHC extension area (although we are using the UHC conversion tables) */
167 	if (((s >> 8) & 0xFF) < 0xA1 || (s & 0xFF) < 0xA1) {
168 		s = 0;
169 	}
170 
171 	if (s <= 0) {
172 		if (c < 0x80) {
173 			s = c;
174 		} else {
175 			s = -1;
176 		}
177 	}
178 
179 	if (s >= 0) {
180 		if (s < 0x80) { /* latin */
181 			CK((*filter->output_function)(s, filter->data));
182 		} else {
183 			CK((*filter->output_function)((s >> 8) & 0xff, filter->data));
184 			CK((*filter->output_function)(s & 0xff, filter->data));
185 		}
186 	} else {
187 		CK(mbfl_filt_conv_illegal_output(c, filter));
188 	}
189 
190 	return 0;
191 }
192 
mbfl_filt_conv_euckr_wchar_flush(mbfl_convert_filter * filter)193 static int mbfl_filt_conv_euckr_wchar_flush(mbfl_convert_filter *filter)
194 {
195 	if (filter->status == 1) {
196 		/* 2-byte character was truncated */
197 		filter->status = 0;
198 		CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
199 	}
200 
201 	if (filter->flush_function) {
202 		(*filter->flush_function)(filter->data);
203 	}
204 
205 	return 0;
206 }
207 
mb_euckr_to_wchar(unsigned char ** in,size_t * in_len,uint32_t * buf,size_t bufsize,unsigned int * state)208 static size_t mb_euckr_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
209 {
210 	unsigned char *p = *in, *e = p + *in_len;
211 	uint32_t *out = buf, *limit = buf + bufsize;
212 
213 	while (p < e && out < limit) {
214 		unsigned char c = *p++;
215 
216 		if (c < 0x80) {
217 			*out++ = c;
218 		} else if (((c >= 0xA1 && c <= 0xAC) || (c >= 0xB0 && c <= 0xFD)) && c != 0xC9 && p < e) {
219 			unsigned char c2 = *p++;
220 
221 			if (c >= 0xA1 && c <= 0xC6 && c2 >= 0xA1 && c2 <= 0xFE) {
222 				unsigned int w = (c - 0xA1)*190 + c2 - 0x41;
223 				ZEND_ASSERT(w < uhc2_ucs_table_size);
224 				w = uhc2_ucs_table[w];
225 				if (!w)
226 					w = MBFL_BAD_INPUT;
227 				*out++ = w;
228 			} else if (c >= 0xC7 && c <= 0xFE && c != 0xC9 && c2 >= 0xA1 && c2 <= 0xFE) {
229 				unsigned int w = (c - 0xC7)*94 + c2 - 0xA1;
230 				ZEND_ASSERT(w < uhc3_ucs_table_size);
231 				w = uhc3_ucs_table[w];
232 				if (!w)
233 					w = MBFL_BAD_INPUT;
234 				*out++ = w;
235 			} else {
236 				*out++ = MBFL_BAD_INPUT;
237 			}
238 		} else {
239 			*out++ = MBFL_BAD_INPUT;
240 		}
241 	}
242 
243 	*in_len = e - p;
244 	*in = p;
245 	return out - buf;
246 }
247 
mb_wchar_to_euckr(uint32_t * in,size_t len,mb_convert_buf * buf,bool end)248 static void mb_wchar_to_euckr(uint32_t *in, size_t len, mb_convert_buf *buf, bool end)
249 {
250 	unsigned char *out, *limit;
251 	MB_CONVERT_BUF_LOAD(buf, out, limit);
252 	MB_CONVERT_BUF_ENSURE(buf, out, limit, len);
253 
254 	while (len--) {
255 		uint32_t w = *in++;
256 		unsigned int s = 0;
257 
258 		if (w >= ucs_a1_uhc_table_min && w < ucs_a1_uhc_table_max) {
259 			s = ucs_a1_uhc_table[w - ucs_a1_uhc_table_min];
260 		} else if (w >= ucs_a2_uhc_table_min && w < ucs_a2_uhc_table_max) {
261 			s = ucs_a2_uhc_table[w - ucs_a2_uhc_table_min];
262 		} else if (w >= ucs_a3_uhc_table_min && w < ucs_a3_uhc_table_max) {
263 			s = ucs_a3_uhc_table[w - ucs_a3_uhc_table_min];
264 		} else if (w >= ucs_i_uhc_table_min && w < ucs_i_uhc_table_max) {
265 			s = ucs_i_uhc_table[w - ucs_i_uhc_table_min];
266 		} else if (w >= ucs_s_uhc_table_min && w < ucs_s_uhc_table_max) {
267 			s = ucs_s_uhc_table[w - ucs_s_uhc_table_min];
268 		} else if (w >= ucs_r1_uhc_table_min && w < ucs_r1_uhc_table_max) {
269 			s = ucs_r1_uhc_table[w - ucs_r1_uhc_table_min];
270 		} else if (w >= ucs_r2_uhc_table_min && w < ucs_r2_uhc_table_max) {
271 			s = ucs_r2_uhc_table[w - ucs_r2_uhc_table_min];
272 		}
273 
274 		/* Exclude UHC extension area (although we are using the UHC conversion tables) */
275 		if (((s >> 8) & 0xFF) < 0xA1 || (s & 0xFF) < 0xA1) {
276 			s = 0;
277 		}
278 
279 		if (!s) {
280 			if (w < 0x80) {
281 				out = mb_convert_buf_add(out, w);
282 			} else {
283 				MB_CONVERT_ERROR(buf, out, limit, w, mb_wchar_to_euckr);
284 				MB_CONVERT_BUF_ENSURE(buf, out, limit, len);
285 			}
286 		} else if (s < 0x80) {
287 			out = mb_convert_buf_add(out, s);
288 		} else {
289 			MB_CONVERT_BUF_ENSURE(buf, out, limit, len + 2);
290 			out = mb_convert_buf_add2(out, (s >> 8) & 0xFF, s & 0xFF);
291 		}
292 	}
293 
294 	MB_CONVERT_BUF_STORE(buf, out, limit);
295 }
296