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