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