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_uhc.h"
36 #include "unicode_table_uhc.h"
37 
38 static int mbfl_filt_ident_uhc(int c, mbfl_identify_filter *filter);
39 
40 static const unsigned char mblen_table_uhc[] = { /* 0x81-0xFE */
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, 2,
55   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
56   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1
57 };
58 
59 static const char *mbfl_encoding_uhc_aliases[] = {"CP949", NULL};
60 
61 const mbfl_encoding mbfl_encoding_uhc = {
62 	mbfl_no_encoding_uhc,
63 	"UHC",
64 	"UHC",
65 	(const char *(*)[])&mbfl_encoding_uhc_aliases,
66 	mblen_table_uhc,
67 	MBFL_ENCTYPE_MBCS
68 };
69 
70 const struct mbfl_identify_vtbl vtbl_identify_uhc = {
71 	mbfl_no_encoding_uhc,
72 	mbfl_filt_ident_common_ctor,
73 	mbfl_filt_ident_common_dtor,
74 	mbfl_filt_ident_uhc
75 };
76 
77 const struct mbfl_convert_vtbl vtbl_uhc_wchar = {
78 	mbfl_no_encoding_uhc,
79 	mbfl_no_encoding_wchar,
80 	mbfl_filt_conv_common_ctor,
81 	mbfl_filt_conv_common_dtor,
82 	mbfl_filt_conv_uhc_wchar,
83 	mbfl_filt_conv_common_flush
84 };
85 
86 const struct mbfl_convert_vtbl vtbl_wchar_uhc = {
87 	mbfl_no_encoding_wchar,
88 	mbfl_no_encoding_uhc,
89 	mbfl_filt_conv_common_ctor,
90 	mbfl_filt_conv_common_dtor,
91 	mbfl_filt_conv_wchar_uhc,
92 	mbfl_filt_conv_common_flush
93 };
94 
95 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
96 
97 /*
98  * UHC => wchar
99  */
100 int
mbfl_filt_conv_uhc_wchar(int c,mbfl_convert_filter * filter)101 mbfl_filt_conv_uhc_wchar(int c, mbfl_convert_filter *filter)
102 {
103 	int c1, w = 0, flag = 0;
104 
105 	switch (filter->status) {
106 	case 0:
107 		if (c >= 0 && c < 0x80) {	/* latin */
108 			CK((*filter->output_function)(c, filter->data));
109 		} else if (c > 0x80 && c < 0xff && c != 0xc9) {	/* dbcs lead byte */
110 			filter->status = 1;
111 			filter->cache = c;
112 		} else {
113 			w = c & MBFL_WCSGROUP_MASK;
114 			w |= MBFL_WCSGROUP_THROUGH;
115 			CK((*filter->output_function)(w, filter->data));
116 		}
117 		break;
118 
119 	case 1:		/* dbcs second byte */
120 		filter->status = 0;
121 		c1 = filter->cache;
122 
123 		if ( c1 >= 0x81 && c1 <= 0xa0){
124 			w = (c1 - 0x81)*190 + (c - 0x41);
125 			if (w >= 0 && w < uhc1_ucs_table_size) {
126 				flag = 1;
127 				w = uhc1_ucs_table[w];
128 			} else {
129 				w = 0;
130 			}
131 		} else if ( c1 >= 0xa1 && c1 <= 0xc6){
132 			w = (c1 - 0xa1)*190 + (c - 0x41);
133 			if (w >= 0 && w < uhc2_ucs_table_size) {
134 				flag = 2;
135 				w = uhc2_ucs_table[w];
136 			} else {
137 				w = 0;
138 			}
139 		} else if ( c1 >= 0xc7 && c1 <= 0xfe){
140 			w = (c1 - 0xc7)*94 + (c - 0xa1);
141 			if (w >= 0 && w < uhc3_ucs_table_size) {
142 				flag = 3;
143 				w = uhc3_ucs_table[w];
144 			} else {
145 				w = 0;
146 			}
147 		}
148 		if (flag > 0){
149 			if (w <= 0) {
150 				w = (c1 << 8) | c;
151 				w &= MBFL_WCSPLANE_MASK;
152 				w |= MBFL_WCSPLANE_UHC;
153 			}
154 			CK((*filter->output_function)(w, filter->data));
155 		} else {
156 			if ((c >= 0 && c < 0x21) || c == 0x7f) {		/* CTLs */
157 				CK((*filter->output_function)(c, filter->data));
158 			} else {
159 				w = (c1 << 8) | c;
160 				w &= MBFL_WCSGROUP_MASK;
161 				w |= MBFL_WCSGROUP_THROUGH;
162 				CK((*filter->output_function)(w, filter->data));
163 			}
164 		}
165 		break;
166 
167 	default:
168 		filter->status = 0;
169 		break;
170 	}
171 
172 	return c;
173 }
174 
175 /*
176  * wchar => UHC
177  */
178 int
mbfl_filt_conv_wchar_uhc(int c,mbfl_convert_filter * filter)179 mbfl_filt_conv_wchar_uhc(int c, mbfl_convert_filter *filter)
180 {
181 	int c1, s;
182 
183 	s = 0;
184 	if (c >= ucs_a1_uhc_table_min && c < ucs_a1_uhc_table_max) {
185 		s = ucs_a1_uhc_table[c - ucs_a1_uhc_table_min];
186 	} else if (c >= ucs_a2_uhc_table_min && c < ucs_a2_uhc_table_max) {
187 		s = ucs_a2_uhc_table[c - ucs_a2_uhc_table_min];
188 	} else if (c >= ucs_a3_uhc_table_min && c < ucs_a3_uhc_table_max) {
189 		s = ucs_a3_uhc_table[c - ucs_a3_uhc_table_min];
190 	} else if (c >= ucs_i_uhc_table_min && c < ucs_i_uhc_table_max) {
191 		s = ucs_i_uhc_table[c - ucs_i_uhc_table_min];
192 	} else if (c >= ucs_s_uhc_table_min && c < ucs_s_uhc_table_max) {
193 		s = ucs_s_uhc_table[c - ucs_s_uhc_table_min];
194 	} else if (c >= ucs_r1_uhc_table_min && c < ucs_r1_uhc_table_max) {
195 		s = ucs_r1_uhc_table[c - ucs_r1_uhc_table_min];
196 	} else if (c >= ucs_r2_uhc_table_min && c < ucs_r2_uhc_table_max) {
197 		s = ucs_r2_uhc_table[c - ucs_r2_uhc_table_min];
198 	}
199 	if (s <= 0) {
200 		c1 = c & ~MBFL_WCSPLANE_MASK;
201 		if (c1 == MBFL_WCSPLANE_UHC) {
202 			s = c & MBFL_WCSPLANE_MASK;
203 		}
204 		if (c == 0) {
205 			s = 0;
206 		} else if (s <= 0) {
207 			s = -1;
208 		}
209 	}
210 	if (s >= 0) {
211 		if (s < 0x80) {	/* latin */
212 			CK((*filter->output_function)(s, filter->data));
213 		} else {
214 			CK((*filter->output_function)((s >> 8) & 0xff, filter->data));
215 			CK((*filter->output_function)(s & 0xff, filter->data));
216 		}
217 	} else {
218 		if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
219 			CK(mbfl_filt_conv_illegal_output(c, filter));
220 		}
221 	}
222 
223 	return c;
224 }
225 
mbfl_filt_ident_uhc(int c,mbfl_identify_filter * filter)226 static int mbfl_filt_ident_uhc(int c, mbfl_identify_filter *filter)
227 {
228 	switch (filter->status) {
229 	case 0: /* latin */
230 		if (c >= 0 && c < 0x80) { /* ok */
231 			;
232 		} else if (c >= 0x81 && c <= 0xa0) {	/* dbcs first char */
233 		    filter->status= 1;
234 		} else if (c >= 0xa1 && c <= 0xc6) {	/* dbcs first char */
235 		    filter->status= 2;
236 		} else if (c >= 0xc7 && c <= 0xfe) {	/* dbcs first char */
237 		    filter->status= 3;
238 		} else { /* bad */
239 			filter->flag = 1;
240 		}
241 
242 	case 1:
243 	case 2:
244 		if (c < 0x41 || (c > 0x5a && c < 0x61)
245 			|| (c > 0x7a && c < 0x81) || c > 0xfe) {	/* bad */
246 		    filter->flag = 1;
247 		}
248 		filter->status = 0;
249 		break;
250 
251 	case 3:
252 		if (c < 0xa1 || c > 0xfe) {	/* bad */
253 		    filter->flag = 1;
254 		}
255 		filter->status = 0;
256 		break;
257 
258 	default:
259 		filter->status = 0;
260 		break;
261 	}
262 
263 	return c;
264 }
265 
266 
267