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