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