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