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