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 #include "unicode_table_gb2312.h"
35 
36 static int mbfl_filt_conv_hz_wchar_flush(mbfl_convert_filter *filter);
37 
38 const mbfl_encoding mbfl_encoding_hz = {
39 	mbfl_no_encoding_hz,
40 	"HZ",
41 	"HZ-GB-2312",
42 	NULL,
43 	NULL,
44 	MBFL_ENCTYPE_GL_UNSAFE,
45 	&vtbl_hz_wchar,
46 	&vtbl_wchar_hz,
47 	NULL
48 };
49 
50 const struct mbfl_convert_vtbl vtbl_hz_wchar = {
51 	mbfl_no_encoding_hz,
52 	mbfl_no_encoding_wchar,
53 	mbfl_filt_conv_common_ctor,
54 	NULL,
55 	mbfl_filt_conv_hz_wchar,
56 	mbfl_filt_conv_hz_wchar_flush,
57 	NULL,
58 };
59 
60 const struct mbfl_convert_vtbl vtbl_wchar_hz = {
61 	mbfl_no_encoding_wchar,
62 	mbfl_no_encoding_hz,
63 	mbfl_filt_conv_common_ctor,
64 	NULL,
65 	mbfl_filt_conv_wchar_hz,
66 	mbfl_filt_conv_any_hz_flush,
67 	NULL,
68 };
69 
70 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
71 
mbfl_filt_conv_hz_wchar(int c,mbfl_convert_filter * filter)72 int mbfl_filt_conv_hz_wchar(int c, mbfl_convert_filter *filter)
73 {
74 	int c1, s, w;
75 
76 	switch (filter->status & 0xf) {
77 	/* case 0x00: ASCII */
78 	/* case 0x10: GB2312 */
79 	case 0:
80 		if (c == '~') {
81 			filter->status += 2;
82 		} else if (filter->status == 0x10 && ((c > 0x20 && c <= 0x29) || (c >= 0x30 && c <= 0x77))) {
83 			/* DBCS first char */
84 			filter->cache = c;
85 			filter->status += 1;
86 		} else if (filter->status == 0 && c >= 0 && c < 0x80) { /* latin, CTLs */
87 			CK((*filter->output_function)(c, filter->data));
88 		} else {
89 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
90 		}
91 		break;
92 
93 	/* case 0x11: GB2312 second char */
94 	case 1:
95 		filter->status &= ~0xf;
96 		c1 = filter->cache;
97 		if (c1 > 0x20 && c1 < 0x7F && c > 0x20 && c < 0x7F) {
98 			s = (c1 - 1)*192 + c + 0x40; /* GB2312 */
99 			if (s >= 0 && s < cp936_ucs_table_size) {
100 				if (s == 0x1864) {
101 					w = 0x30FB;
102 				} else if (s == 0x186A) {
103 					w = 0x2015;
104 				} else if (s == 0x186C) {
105 					w = 0x2225;
106 				} else if ((s >= 0x1920 && s <= 0x192A) || s == 0x1963 || (s >= 0x1C60 && s <= 0x1C7F) || (s >= 0x1DBB && s <= 0x1DC4)) {
107 					w = 0;
108 				} else {
109 					w = cp936_ucs_table[s];
110 				}
111 			} else {
112 				w = 0;
113 			}
114 
115 			if (w <= 0) {
116 				w = MBFL_BAD_INPUT;
117 			}
118 
119 			CK((*filter->output_function)(w, filter->data));
120 		} else {
121 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
122 		}
123 		break;
124 
125 	/* '~' */
126 	case 2:
127 		if (c == '}' && filter->status == 0x12) {
128 			filter->status = 0;
129 		} else if (c == '{' && filter->status == 2) {
130 			filter->status = 0x10;
131 		} else if (c == '~' && filter->status == 2) {
132 			CK((*filter->output_function)('~', filter->data));
133 		} else if (c == '\n') {
134 			/* "~\n" is a line continuation; no output is needed, nor should we shift modes */
135 			filter->status -= 2;
136 		} else {
137 			/* Invalid character after ~ */
138 			filter->status -= 2;
139 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
140 		}
141 		break;
142 
143 	default:
144 		filter->status = 0;
145 		break;
146 	}
147 
148 	return 0;
149 }
150 
mbfl_filt_conv_hz_wchar_flush(mbfl_convert_filter * filter)151 static int mbfl_filt_conv_hz_wchar_flush(mbfl_convert_filter *filter)
152 {
153 	if (filter->status == 0x11) {
154 		/* 2-byte character was truncated */
155 		CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
156 	}
157 
158 	filter->status = 0;
159 
160 	if (filter->flush_function) {
161 		(*filter->flush_function)(filter->data);
162 	}
163 
164 	return 0;
165 }
166 
mbfl_filt_conv_wchar_hz(int c,mbfl_convert_filter * filter)167 int mbfl_filt_conv_wchar_hz(int c, mbfl_convert_filter *filter)
168 {
169 	int s = 0;
170 
171 	if (c >= ucs_a1_cp936_table_min && c < ucs_a1_cp936_table_max) {
172 		if (c == 0xB7 || c == 0x144 || c == 0x148 || c == 0x251 || c == 0x261 || c == 0x2CA || c == 0x2CB || c == 0x2D9) {
173 			s = 0;
174 		} else {
175 			s = ucs_a1_cp936_table[c - ucs_a1_cp936_table_min];
176 		}
177 	} else if (c >= ucs_a2_cp936_table_min && c < ucs_a2_cp936_table_max) {
178 		if (c == 0x2015) {
179 			s = 0xA1AA;
180 		} else if (c == 0x2010 || c == 0x2013 || c == 0x2014 || c == 0x2016 || c == 0x2025 || c == 0x2035 ||
181 				c == 0x2105 || c == 0x2109 || c == 0x2121 || (c >= 0x2170 && c <= 0x2179) || (c >= 0x2196 && c <= 0x2199) ||
182 				c == 0x2215 || c == 0x221F || c == 0x2223 || c == 0x2252 || c == 0x2266 || c == 0x2267 || c == 0x2295 ||
183 				(c >= 0x2550 && c <= 0x2573) || c == 0x22BF || c == 0x2609 || (c >= 0x2581 && c <= 0x258F) ||
184 				(c >= 0x2593 && c <= 0x2595) || c == 0x25BC || c == 0x25BD || (c >= 0x25E2 && c <= 0x25E5)) {
185 			s = 0;
186 		} else {
187 			s = ucs_a2_cp936_table[c - ucs_a2_cp936_table_min];
188 		}
189 	} else if (c >= ucs_a3_cp936_table_min && c < ucs_a3_cp936_table_max) {
190 		if (c == 0x30FB) {
191 			s = 0xA1A4;
192 		} else if (c == 0x3006 || c == 0x3007 || c == 0x3012 || c == 0x3231 || c == 0x32A3 || c >= 0x3300 ||
193 				(c >= 0x3018 && c <= 0x3040) || (c >= 0x309B && c <= 0x309E) || (c >= 0x30FC && c <= 0x30FE)) {
194 			s = 0;
195 		} else {
196 			s = ucs_a3_cp936_table[c - ucs_a3_cp936_table_min];
197 		}
198 	} else if (c >= ucs_i_gb2312_table_min && c < ucs_i_gb2312_table_max) {
199 		s = ucs_i_gb2312_table[c - ucs_i_gb2312_table_min];
200 	} else if (c >= ucs_hff_cp936_table_min && c < ucs_hff_cp936_table_max) {
201 		if (c == 0xFF04) {
202 			s = 0xA1E7;
203 		} else if (c == 0xFF5E) {
204 			s = 0xA1AB;
205 		} else if (c >= 0xFF01 && c <= 0xFF5D) {
206 			s = c - 0xFF01 + 0xA3A1;
207 		} else if (c == 0xFFE0 || c == 0xFFE1 || c == 0xFFE3 || c == 0xFFE5) {
208 			s = ucs_hff_s_cp936_table[c - 0xFFE0];
209 		}
210 	}
211 
212 	if (s & 0x8000) {
213 		s -= 0x8080;
214 	}
215 
216 	if (s <= 0) {
217 		s = (c == 0) ? 0 : -1;
218 	} else if ((s >= 0x80 && s < 0x2121) || s > 0x8080) {
219 		s = -1;
220 	}
221 
222 	if (s >= 0) {
223 		if (s < 0x80) { /* ASCII */
224 			if ((filter->status & 0xff00) != 0) {
225 				CK((*filter->output_function)('~', filter->data));
226 				CK((*filter->output_function)('}', filter->data));
227 			}
228 			filter->status = 0;
229 			if (s == 0x7E) {
230 				CK((*filter->output_function)('~', filter->data));
231 			}
232 			CK((*filter->output_function)(s, filter->data));
233 		} else { /* GB 2312-80 */
234 			if ((filter->status & 0xFF00) != 0x200) {
235 				CK((*filter->output_function)('~', filter->data));
236 				CK((*filter->output_function)('{', filter->data));
237 			}
238 			filter->status = 0x200;
239 			CK((*filter->output_function)((s >> 8) & 0x7F, filter->data));
240 			CK((*filter->output_function)(s & 0x7F, filter->data));
241 		}
242 	} else {
243 		CK(mbfl_filt_conv_illegal_output(c, filter));
244 	}
245 
246 	return 0;
247 }
248 
mbfl_filt_conv_any_hz_flush(mbfl_convert_filter * filter)249 int mbfl_filt_conv_any_hz_flush(mbfl_convert_filter *filter)
250 {
251 	/* back to latin */
252 	if (filter->status & 0xFF00) {
253 		CK((*filter->output_function)('~', filter->data));
254 		CK((*filter->output_function)('}', filter->data));
255 	}
256 	filter->status = 0;
257 	return 0;
258 }
259