xref: /PHP-5.3/ext/mbstring/libmbfl/mbfl/mbfl_ident.c (revision ea539c8b)
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.c
26  * by Moriyoshi Koizumi <moriyoshi@php.net> on 20 Dec 2002. The file
27  * mbfilter.c is included in this package .
28  *
29  */
30 
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 
35 #ifdef HAVE_STDDEF_H
36 #include <stddef.h>
37 #endif
38 
39 #include "mbfl_ident.h"
40 #include "mbfl_allocators.h"
41 #include "mbfilter_pass.h"
42 #include "mbfilter_8bit.h"
43 #include "mbfilter_wchar.h"
44 
45 #include "filters/mbfilter_euc_cn.h"
46 #include "filters/mbfilter_hz.h"
47 #include "filters/mbfilter_euc_tw.h"
48 #include "filters/mbfilter_big5.h"
49 #include "filters/mbfilter_uhc.h"
50 #include "filters/mbfilter_euc_kr.h"
51 #include "filters/mbfilter_iso2022_kr.h"
52 #include "filters/mbfilter_sjis.h"
53 #include "filters/mbfilter_sjis_open.h"
54 #include "filters/mbfilter_jis.h"
55 #include "filters/mbfilter_iso2022_jp_ms.h"
56 #include "filters/mbfilter_euc_jp.h"
57 #include "filters/mbfilter_euc_jp_win.h"
58 #include "filters/mbfilter_ascii.h"
59 #include "filters/mbfilter_koi8r.h"
60 #include "filters/mbfilter_koi8u.h"
61 #include "filters/mbfilter_cp866.h"
62 #include "filters/mbfilter_cp932.h"
63 #include "filters/mbfilter_cp936.h"
64 #include "filters/mbfilter_cp1251.h"
65 #include "filters/mbfilter_cp1252.h"
66 #include "filters/mbfilter_cp1254.h"
67 #include "filters/mbfilter_cp51932.h"
68 #include "filters/mbfilter_cp5022x.h"
69 #include "filters/mbfilter_iso8859_1.h"
70 #include "filters/mbfilter_iso8859_2.h"
71 #include "filters/mbfilter_iso8859_3.h"
72 #include "filters/mbfilter_iso8859_4.h"
73 #include "filters/mbfilter_iso8859_5.h"
74 #include "filters/mbfilter_iso8859_6.h"
75 #include "filters/mbfilter_iso8859_7.h"
76 #include "filters/mbfilter_iso8859_8.h"
77 #include "filters/mbfilter_iso8859_9.h"
78 #include "filters/mbfilter_iso8859_10.h"
79 #include "filters/mbfilter_iso8859_13.h"
80 #include "filters/mbfilter_iso8859_14.h"
81 #include "filters/mbfilter_iso8859_15.h"
82 #include "filters/mbfilter_base64.h"
83 #include "filters/mbfilter_qprint.h"
84 #include "filters/mbfilter_uuencode.h"
85 #include "filters/mbfilter_7bit.h"
86 #include "filters/mbfilter_utf7.h"
87 #include "filters/mbfilter_utf7imap.h"
88 #include "filters/mbfilter_utf8.h"
89 #include "filters/mbfilter_utf16.h"
90 #include "filters/mbfilter_utf32.h"
91 #include "filters/mbfilter_byte2.h"
92 #include "filters/mbfilter_byte4.h"
93 #include "filters/mbfilter_ucs4.h"
94 #include "filters/mbfilter_ucs2.h"
95 #include "filters/mbfilter_htmlent.h"
96 #include "filters/mbfilter_armscii8.h"
97 #include "filters/mbfilter_cp850.h"
98 
99 static const struct mbfl_identify_vtbl vtbl_identify_false = {
100 	mbfl_no_encoding_pass,
101 	mbfl_filt_ident_false_ctor,
102 	mbfl_filt_ident_common_dtor,
103 	mbfl_filt_ident_false };
104 
105 
106 static const struct mbfl_identify_vtbl *mbfl_identify_filter_list[] = {
107 	&vtbl_identify_utf8,
108 	&vtbl_identify_utf7,
109 	&vtbl_identify_ascii,
110 	&vtbl_identify_eucjp,
111 	&vtbl_identify_sjis,
112 	&vtbl_identify_sjis_open,
113 	&vtbl_identify_eucjpwin,
114 	&vtbl_identify_cp932,
115 	&vtbl_identify_jis,
116 	&vtbl_identify_2022jp,
117 	&vtbl_identify_2022jpms,
118 	&vtbl_identify_cp51932,
119 	&vtbl_identify_euccn,
120 	&vtbl_identify_cp936,
121 	&vtbl_identify_hz,
122 	&vtbl_identify_euctw,
123 	&vtbl_identify_big5,
124 	&vtbl_identify_euckr,
125 	&vtbl_identify_uhc,
126 	&vtbl_identify_2022kr,
127 	&vtbl_identify_cp1251,
128 	&vtbl_identify_cp866,
129 	&vtbl_identify_koi8r,
130 	&vtbl_identify_koi8u,
131 	&vtbl_identify_cp1252,
132 	&vtbl_identify_cp1254,
133 	&vtbl_identify_8859_1,
134 	&vtbl_identify_8859_2,
135 	&vtbl_identify_8859_3,
136 	&vtbl_identify_8859_4,
137 	&vtbl_identify_8859_5,
138 	&vtbl_identify_8859_6,
139 	&vtbl_identify_8859_7,
140 	&vtbl_identify_8859_8,
141 	&vtbl_identify_8859_9,
142 	&vtbl_identify_8859_10,
143 	&vtbl_identify_8859_13,
144 	&vtbl_identify_8859_14,
145 	&vtbl_identify_8859_15,
146 	&vtbl_identify_armscii8,
147 	&vtbl_identify_cp850,
148 	&vtbl_identify_jis_ms,
149 	&vtbl_identify_cp50220,
150 	&vtbl_identify_cp50221,
151 	&vtbl_identify_cp50222,
152 	&vtbl_identify_false,
153 	NULL
154 };
155 
156 
157 
158 /*
159  * identify filter
160  */
mbfl_identify_filter_get_vtbl(enum mbfl_no_encoding encoding)161 const struct mbfl_identify_vtbl * mbfl_identify_filter_get_vtbl(enum mbfl_no_encoding encoding)
162 {
163 	const struct mbfl_identify_vtbl * vtbl;
164 	int i;
165 
166 	i = 0;
167 	while ((vtbl = mbfl_identify_filter_list[i++]) != NULL) {
168 		if (vtbl->encoding == encoding) {
169 			break;
170 		}
171 	}
172 
173 	return vtbl;
174 }
175 
mbfl_identify_filter_new(enum mbfl_no_encoding encoding)176 mbfl_identify_filter *mbfl_identify_filter_new(enum mbfl_no_encoding encoding)
177 {
178 	mbfl_identify_filter *filter;
179 
180 	/* allocate */
181 	filter = (mbfl_identify_filter *)mbfl_malloc(sizeof(mbfl_identify_filter));
182 	if (filter == NULL) {
183 		return NULL;
184 	}
185 
186 	if (mbfl_identify_filter_init(filter, encoding)) {
187 		mbfl_free(filter);
188 		return NULL;
189 	}
190 
191 	return filter;
192 }
193 
mbfl_identify_filter_init(mbfl_identify_filter * filter,enum mbfl_no_encoding encoding)194 int mbfl_identify_filter_init(mbfl_identify_filter *filter, enum mbfl_no_encoding encoding)
195 {
196 	const struct mbfl_identify_vtbl *vtbl;
197 
198 	/* encoding structure */
199 	filter->encoding = mbfl_no2encoding(encoding);
200 	if (filter->encoding == NULL) {
201 		filter->encoding = &mbfl_encoding_pass;
202 	}
203 
204 	filter->status = 0;
205 	filter->flag = 0;
206 	filter->score = 0;
207 
208 	/* setup the function table */
209 	vtbl = mbfl_identify_filter_get_vtbl(filter->encoding->no_encoding);
210 	if (vtbl == NULL) {
211 		vtbl = &vtbl_identify_false;
212 	}
213 	filter->filter_ctor = vtbl->filter_ctor;
214 	filter->filter_dtor = vtbl->filter_dtor;
215 	filter->filter_function = vtbl->filter_function;
216 
217 	/* constructor */
218 	(*filter->filter_ctor)(filter);
219 
220 	return 0;
221 }
222 
mbfl_identify_filter_delete(mbfl_identify_filter * filter)223 void mbfl_identify_filter_delete(mbfl_identify_filter *filter)
224 {
225 	if (filter == NULL) {
226 		return;
227 	}
228 
229 	mbfl_identify_filter_cleanup(filter);
230 	mbfl_free((void*)filter);
231 }
232 
mbfl_identify_filter_cleanup(mbfl_identify_filter * filter)233 void mbfl_identify_filter_cleanup(mbfl_identify_filter *filter)
234 {
235 	(*filter->filter_dtor)(filter);
236 }
237 
mbfl_filt_ident_common_ctor(mbfl_identify_filter * filter)238 void mbfl_filt_ident_common_ctor(mbfl_identify_filter *filter)
239 {
240 	filter->status = 0;
241 	filter->flag = 0;
242 }
243 
mbfl_filt_ident_common_dtor(mbfl_identify_filter * filter)244 void mbfl_filt_ident_common_dtor(mbfl_identify_filter *filter)
245 {
246 	filter->status = 0;
247 }
248 
mbfl_filt_ident_false(int c,mbfl_identify_filter * filter)249 int mbfl_filt_ident_false(int c, mbfl_identify_filter *filter)
250 {
251 	filter->flag = 1;	/* bad */
252 	return c;
253 }
254 
mbfl_filt_ident_false_ctor(mbfl_identify_filter * filter)255 void mbfl_filt_ident_false_ctor(mbfl_identify_filter *filter)
256 {
257 	filter->status = 0;
258 	filter->flag = 1;
259 }
260 
mbfl_filt_ident_true(int c,mbfl_identify_filter * filter)261 int mbfl_filt_ident_true(int c, mbfl_identify_filter *filter)
262 {
263 	return c;
264 }
265