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_cn.c
26  * by moriyoshi koizumi <moriyoshi@php.net> on 4 dec 2002.
27  *
28  */
29 
30 #include "mbfilter.h"
31 #include "mbfilter_hz.h"
32 
33 #include "unicode_table_cp936.h"
34 
35 static int mbfl_filt_ident_hz(int c, mbfl_identify_filter *filter);
36 
37 const mbfl_encoding mbfl_encoding_hz = {
38 	mbfl_no_encoding_hz,
39 	"HZ",
40 	"HZ-GB-2312",
41 	NULL,
42 	NULL,
43 	MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_GL_UNSAFE,
44 	&vtbl_hz_wchar,
45 	&vtbl_wchar_hz
46 };
47 
48 const struct mbfl_identify_vtbl vtbl_identify_hz = {
49 	mbfl_no_encoding_hz,
50 	mbfl_filt_ident_common_ctor,
51 	mbfl_filt_ident_hz
52 };
53 
54 const struct mbfl_convert_vtbl vtbl_hz_wchar = {
55 	mbfl_no_encoding_hz,
56 	mbfl_no_encoding_wchar,
57 	mbfl_filt_conv_common_ctor,
58 	NULL,
59 	mbfl_filt_conv_hz_wchar,
60 	mbfl_filt_conv_common_flush,
61 	NULL,
62 };
63 
64 const struct mbfl_convert_vtbl vtbl_wchar_hz = {
65 	mbfl_no_encoding_wchar,
66 	mbfl_no_encoding_hz,
67 	mbfl_filt_conv_common_ctor,
68 	NULL,
69 	mbfl_filt_conv_wchar_hz,
70 	mbfl_filt_conv_any_hz_flush,
71 	NULL,
72 };
73 
74 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
75 
76 /*
77  * HZ => wchar
78  */
79 int
mbfl_filt_conv_hz_wchar(int c,mbfl_convert_filter * filter)80 mbfl_filt_conv_hz_wchar(int c, mbfl_convert_filter *filter)
81 {
82 	int c1, s, w;
83 
84 	switch (filter->status & 0xf) {
85 /*	case 0x00:	 ASCII */
86 /*	case 0x10:	 GB2312 */
87 	case 0:
88 		if (c == 0x7e) {
89 			filter->status += 2;
90 		} else if (filter->status == 0x10 && c > 0x20 && c < 0x7f) {	/* DBCS first char */
91 			filter->cache = c;
92 			filter->status += 1;
93 		} else if (c >= 0 && c < 0x80) {		/* latin, CTLs */
94 			CK((*filter->output_function)(c, filter->data));
95 		} else {
96 			w = c & MBFL_WCSGROUP_MASK;
97 			w |= MBFL_WCSGROUP_THROUGH;
98 			CK((*filter->output_function)(w, filter->data));
99 		}
100 		break;
101 
102 /*	case 0x11:	 GB2312 second char */
103 	case 1:
104 		filter->status &= ~0xf;
105 		c1 = filter->cache;
106 		if (c1 > 0x20 && c1 < 0x7f && c > 0x20 && c < 0x7f) {
107 			s = (c1 - 1)*192 + c + 0x40; /* GB2312 */
108 			if (s >= 0 && s < cp936_ucs_table_size) {
109 				w = cp936_ucs_table[s];
110 			} else {
111 				w = 0;
112 			}
113 			if (w <= 0) {
114 				w = (c1 << 8) | c;
115 				w &= MBFL_WCSPLANE_MASK;
116 				w |= MBFL_WCSPLANE_GB2312;
117 			}
118 			CK((*filter->output_function)(w, filter->data));
119 		} else if ((c >= 0 && c < 0x21) || c == 0x7f) {		/* CTLs */
120 			CK((*filter->output_function)(c, filter->data));
121 		} else {
122 			w = (c1 << 8) | c;
123 			w &= MBFL_WCSGROUP_MASK;
124 			w |= MBFL_WCSGROUP_THROUGH;
125 			CK((*filter->output_function)(w, filter->data));
126 		}
127 		break;
128 
129 	/* '~' */
130 	case 2:
131 		if (c == 0x7d) {		/* '}' */
132 			filter->status = 0x0;
133 		} else if (c == 0x7b) {		/* '{' */
134 			filter->status = 0x10;
135 		} else if (c == 0x7e) { /* '~' */
136 			filter->status = 0x0;
137 			CK((*filter->output_function)(0x007e, filter->data));
138 		}
139 		break;
140 
141 	default:
142 		filter->status = 0;
143 		break;
144 	}
145 
146 	return c;
147 }
148 
149 /*
150  * wchar => HZ
151  */
152 int
mbfl_filt_conv_wchar_hz(int c,mbfl_convert_filter * filter)153 mbfl_filt_conv_wchar_hz(int c, mbfl_convert_filter *filter)
154 {
155 	int s;
156 
157 	s = 0;
158 	if (c >= ucs_a1_cp936_table_min && c < ucs_a1_cp936_table_max) {
159 		s = ucs_a1_cp936_table[c - ucs_a1_cp936_table_min];
160 	} else if (c >= ucs_a2_cp936_table_min && c < ucs_a2_cp936_table_max) {
161 		s = ucs_a2_cp936_table[c - ucs_a2_cp936_table_min];
162 	} else if (c >= ucs_a3_cp936_table_min && c < ucs_a3_cp936_table_max) {
163 		s = ucs_a3_cp936_table[c - ucs_a3_cp936_table_min];
164 	} else if (c >= ucs_i_cp936_table_min && c < ucs_i_cp936_table_max) {
165 		s = ucs_i_cp936_table[c - ucs_i_cp936_table_min];
166 	} else if (c >= ucs_hff_cp936_table_min && c < ucs_hff_cp936_table_max) {
167 		if (c == 0xff04) {
168 			s = 0xa1e7;
169 		} else if (c == 0xff5e) {
170 			s = 0xa1ab;
171 		} else if (c >= 0xff01 && c <= 0xff5d) {
172 			s = c - 0xff01 + 0xa3a1;
173 		} else if (c >= 0xffe0 && c <= 0xffe5) {
174 			s = ucs_hff_s_cp936_table[c-0xffe0];
175 		}
176 	}
177 	if (s & 0x8000) {
178 		s -= 0x8080;
179 	}
180 
181 	if (s <= 0) {
182 		if (c == 0) {
183 			s = 0;
184 		} else if (s <= 0) {
185 			s = -1;
186 		}
187 	} else if ((s >= 0x80 && s < 0x2121) || (s > 0x8080)) {
188 		s = -1;
189 	}
190 	if (s >= 0) {
191 		if (s < 0x80) { /* ASCII */
192 			if ((filter->status & 0xff00) != 0) {
193 				CK((*filter->output_function)(0x7e, filter->data));		/* '~' */
194 				CK((*filter->output_function)(0x7d, filter->data));		/* '}' */
195 			}
196 			filter->status = 0;
197 			if (s == 0x7e){
198 				CK((*filter->output_function)(0x7e, filter->data));
199 			}
200 			CK((*filter->output_function)(s, filter->data));
201 		} else { /* GB 2312-80 */
202 			if ((filter->status & 0xff00) != 0x200) {
203 				CK((*filter->output_function)(0x7e, filter->data));		/* '~' */
204 				CK((*filter->output_function)(0x7b, filter->data));		/* '{' */
205 			}
206 			filter->status = 0x200;
207 			CK((*filter->output_function)((s >> 8) & 0x7f, filter->data));
208 			CK((*filter->output_function)(s & 0x7f, filter->data));
209 		}
210 	} else {
211 		CK(mbfl_filt_conv_illegal_output(c, filter));
212 	}
213 
214 	return c;
215 }
216 
217 int
mbfl_filt_conv_any_hz_flush(mbfl_convert_filter * filter)218 mbfl_filt_conv_any_hz_flush(mbfl_convert_filter *filter)
219 {
220 	/* back to latin */
221 	if ((filter->status & 0xff00) != 0) {
222 		CK((*filter->output_function)(0x7e, filter->data));		/* ~ */
223 		CK((*filter->output_function)(0x7d, filter->data));		/* '{' */
224 	}
225 	filter->status &= 0xff;
226 	return 0;
227 }
228 
mbfl_filt_ident_hz(int c,mbfl_identify_filter * filter)229 static int mbfl_filt_ident_hz(int c, mbfl_identify_filter *filter)
230 {
231 	switch (filter->status & 0xf) {
232 /*	case 0x00:	 ASCII */
233 /*	case 0x10:	 GB2312 */
234 	case 0:
235 		if (c == 0x7e) {
236 			filter->status += 2;
237 		} else if (filter->status == 0x10 && c > 0x20 && c < 0x7f) {		/* DBCS first char */
238 			filter->status += 1;
239 		} else if (c >= 0 && c < 0x80) {		/* latin, CTLs */
240 			;
241 		} else {
242 			filter->flag = 1;	/* bad */
243 		}
244 		break;
245 
246 /*	case 0x11:	 GB2312 second char */
247 	case 1:
248 		filter->status &= ~0xf;
249 		if (c < 0x21 || c > 0x7e) {		/* bad */
250 			filter->flag = 1;
251 		}
252 		break;
253 
254 	case 2:
255 		if (c == 0x7d) {		/* '}' */
256 			filter->status = 0;
257 		} else if (c == 0x7b) {		/* '{' */
258 			filter->status = 0x10;
259 		} else if (c == 0x7e) {		/* '~' */
260 			filter->status = 0;
261 		} else {
262 			filter->flag = 1;	/* bad */
263 			filter->status &= ~0xf;
264 		}
265 		break;
266 
267 	default:
268 		filter->status = 0;
269 		break;
270 	}
271 
272 	return c;
273 }
274