Annotation of embedaddon/php/ext/mbstring/libmbfl/filters/mbfilter_euc_cn.c, revision 1.1.1.2
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_euc_cn.h"
36:
37: #include "unicode_table_cp936.h"
38:
39: static int mbfl_filt_ident_euccn(int c, mbfl_identify_filter *filter);
40:
41: static const unsigned char mblen_table_euccn[] = { /* 0xA1-0xFE */
42: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
43: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
44: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
45: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
46: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
47: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
48: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
49: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
50: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
51: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
52: 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
53: 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
54: 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
55: 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
56: 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
57: 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1
58: };
59:
60: static const char *mbfl_encoding_euc_cn_aliases[] = {"CN-GB", "EUC_CN", "eucCN", "x-euc-cn", "gb2312", NULL};
61:
62: const mbfl_encoding mbfl_encoding_euc_cn = {
63: mbfl_no_encoding_euc_cn,
64: "EUC-CN",
65: "CN-GB",
66: (const char *(*)[])&mbfl_encoding_euc_cn_aliases,
67: mblen_table_euccn,
68: MBFL_ENCTYPE_MBCS
69: };
70:
71: const struct mbfl_identify_vtbl vtbl_identify_euccn = {
72: mbfl_no_encoding_euc_cn,
73: mbfl_filt_ident_common_ctor,
74: mbfl_filt_ident_common_dtor,
75: mbfl_filt_ident_euccn
76: };
77:
78: const struct mbfl_convert_vtbl vtbl_euccn_wchar = {
79: mbfl_no_encoding_euc_cn,
80: mbfl_no_encoding_wchar,
81: mbfl_filt_conv_common_ctor,
82: mbfl_filt_conv_common_dtor,
83: mbfl_filt_conv_euccn_wchar,
84: mbfl_filt_conv_common_flush
85: };
86:
87: const struct mbfl_convert_vtbl vtbl_wchar_euccn = {
88: mbfl_no_encoding_wchar,
89: mbfl_no_encoding_euc_cn,
90: mbfl_filt_conv_common_ctor,
91: mbfl_filt_conv_common_dtor,
92: mbfl_filt_conv_wchar_euccn,
93: mbfl_filt_conv_common_flush
94: };
95:
96: #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
97:
98: /*
99: * EUC-CN => wchar
100: */
101: int
102: mbfl_filt_conv_euccn_wchar(int c, mbfl_convert_filter *filter)
103: {
104: int c1, w;
105:
106: switch (filter->status) {
107: case 0:
108: if (c >= 0 && c < 0x80) { /* latin */
109: CK((*filter->output_function)(c, filter->data));
110: } else if (c > 0xa0 && c < 0xff) { /* dbcs lead byte */
111: filter->status = 1;
112: filter->cache = c;
113: } else {
114: w = c & MBFL_WCSGROUP_MASK;
115: w |= MBFL_WCSGROUP_THROUGH;
116: CK((*filter->output_function)(w, filter->data));
117: }
118: break;
119:
120: case 1: /* dbcs second byte */
121: filter->status = 0;
122: c1 = filter->cache;
123: if (c1 > 0xa0 && c1 < 0xff && c > 0xa0 && c < 0xff) {
124: w = (c1 - 0x81)*192 + (c - 0x40);
125: if (w >= 0 && w < cp936_ucs_table_size) {
126: w = cp936_ucs_table[w];
127: } else {
128: w = 0;
129: }
130: if (w <= 0) {
131: w = (c1 << 8) | c;
132: w &= MBFL_WCSPLANE_MASK;
133: w |= MBFL_WCSPLANE_GB2312;
134: }
135: CK((*filter->output_function)(w, filter->data));
136: } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
137: CK((*filter->output_function)(c, filter->data));
138: } else {
139: w = (c1 << 8) | c;
140: w &= MBFL_WCSGROUP_MASK;
141: w |= MBFL_WCSGROUP_THROUGH;
142: CK((*filter->output_function)(w, filter->data));
143: }
144: break;
145:
146: default:
147: filter->status = 0;
148: break;
149: }
150:
151: return c;
152: }
153:
154: /*
155: * wchar => EUC-CN
156: */
157: int
158: mbfl_filt_conv_wchar_euccn(int c, mbfl_convert_filter *filter)
159: {
160: int c1, c2, s;
161:
162: s = 0;
163: if (c >= ucs_a1_cp936_table_min && c < ucs_a1_cp936_table_max) {
164: s = ucs_a1_cp936_table[c - ucs_a1_cp936_table_min];
165: } else if (c >= ucs_a2_cp936_table_min && c < ucs_a2_cp936_table_max) {
166: s = ucs_a2_cp936_table[c - ucs_a2_cp936_table_min];
167: } else if (c >= ucs_a3_cp936_table_min && c < ucs_a3_cp936_table_max) {
168: s = ucs_a3_cp936_table[c - ucs_a3_cp936_table_min];
169: } else if (c >= ucs_i_cp936_table_min && c < ucs_i_cp936_table_max) {
170: s = ucs_i_cp936_table[c - ucs_i_cp936_table_min];
171: } else if (c >= ucs_hff_cp936_table_min && c < ucs_hff_cp936_table_max) {
1.1.1.2 ! misho 172: if (c == 0xff04) {
! 173: s = 0xa1e7;
! 174: } else if (c == 0xff5e) {
! 175: s = 0xa1ab;
! 176: } else if (c >= 0xff01 && c <= 0xff5d) {
! 177: s = c - 0xff01 + 0xa3a1;
! 178: } else if (c >= 0xffe0 && c <= 0xffe5) {
! 179: s = ucs_hff_s_cp936_table[c-0xffe0];
! 180: }
1.1 misho 181: }
182: c1 = (s >> 8) & 0xff;
183: c2 = s & 0xff;
184:
185: if (c1 < 0xa1 || c2 < 0xa1) { /* exclude CP936 extension */
186: s = c;
187: }
188:
189: if (s <= 0) {
190: c1 = c & ~MBFL_WCSPLANE_MASK;
191: if (c1 == MBFL_WCSPLANE_GB2312) {
192: s = c & MBFL_WCSPLANE_MASK;
193: }
194: if (c == 0) {
195: s = 0;
196: } else if (s <= 0) {
197: s = -1;
198: }
199: }
200: if (s >= 0) {
201: if (s < 0x80) { /* latin */
202: CK((*filter->output_function)(s, filter->data));
203: } else {
204: CK((*filter->output_function)((s >> 8) & 0xff, filter->data));
205: CK((*filter->output_function)(s & 0xff, filter->data));
206: }
207: } else {
208: if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
209: CK(mbfl_filt_conv_illegal_output(c, filter));
210: }
211: }
212:
213: return c;
214: }
215:
216: static int mbfl_filt_ident_euccn(int c, mbfl_identify_filter *filter)
217: {
218: switch (filter->status) {
219: case 0: /* latin */
220: if (c >= 0 && c < 0x80) { /* ok */
221: ;
222: } else if (c > 0xa0 && c < 0xff) { /* DBCS lead byte */
223: filter->status = 1;
224: } else { /* bad */
225: filter->flag = 1;
226: }
227: break;
228:
229: case 1: /* got lead byte */
230: if (c < 0xa1 || c > 0xfe) { /* bad */
231: filter->flag = 1;
232: }
233: filter->status = 0;
234: break;
235:
236: default:
237: filter->status = 0;
238: break;
239: }
240:
241: return c;
242: }
243:
244:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>