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 #ifdef HAVE_STRING_H
40 #include <string.h>
41 #endif
42 
43 #ifdef HAVE_STRINGS_H
44 #include <strings.h>
45 #endif
46 
47 #include "mbfl_encoding.h"
48 #include "mbfilter_pass.h"
49 #include "mbfilter_8bit.h"
50 #include "mbfilter_wchar.h"
51 
52 #include "filters/mbfilter_euc_cn.h"
53 #include "filters/mbfilter_hz.h"
54 #include "filters/mbfilter_euc_tw.h"
55 #include "filters/mbfilter_big5.h"
56 #include "filters/mbfilter_uhc.h"
57 #include "filters/mbfilter_euc_kr.h"
58 #include "filters/mbfilter_iso2022_kr.h"
59 #include "filters/mbfilter_sjis.h"
60 #include "filters/mbfilter_sjis_open.h"
61 #include "filters/mbfilter_sjis_mobile.h"
62 #include "filters/mbfilter_sjis_mac.h"
63 #include "filters/mbfilter_sjis_2004.h"
64 #include "filters/mbfilter_cp51932.h"
65 #include "filters/mbfilter_jis.h"
66 #include "filters/mbfilter_iso2022_jp_ms.h"
67 #include "filters/mbfilter_iso2022jp_2004.h"
68 #include "filters/mbfilter_iso2022jp_mobile.h"
69 #include "filters/mbfilter_euc_jp.h"
70 #include "filters/mbfilter_euc_jp_win.h"
71 #include "filters/mbfilter_euc_jp_2004.h"
72 #include "filters/mbfilter_gb18030.h"
73 #include "filters/mbfilter_ascii.h"
74 #include "filters/mbfilter_koi8r.h"
75 #include "filters/mbfilter_koi8u.h"
76 #include "filters/mbfilter_cp866.h"
77 #include "filters/mbfilter_cp932.h"
78 #include "filters/mbfilter_cp936.h"
79 #include "filters/mbfilter_cp1251.h"
80 #include "filters/mbfilter_cp1252.h"
81 #include "filters/mbfilter_cp1254.h"
82 #include "filters/mbfilter_cp5022x.h"
83 #include "filters/mbfilter_iso8859_1.h"
84 #include "filters/mbfilter_iso8859_2.h"
85 #include "filters/mbfilter_iso8859_3.h"
86 #include "filters/mbfilter_iso8859_4.h"
87 #include "filters/mbfilter_iso8859_5.h"
88 #include "filters/mbfilter_iso8859_6.h"
89 #include "filters/mbfilter_iso8859_7.h"
90 #include "filters/mbfilter_iso8859_8.h"
91 #include "filters/mbfilter_iso8859_9.h"
92 #include "filters/mbfilter_iso8859_10.h"
93 #include "filters/mbfilter_iso8859_13.h"
94 #include "filters/mbfilter_iso8859_14.h"
95 #include "filters/mbfilter_iso8859_15.h"
96 #include "filters/mbfilter_iso8859_16.h"
97 #include "filters/mbfilter_base64.h"
98 #include "filters/mbfilter_qprint.h"
99 #include "filters/mbfilter_uuencode.h"
100 #include "filters/mbfilter_7bit.h"
101 #include "filters/mbfilter_utf7.h"
102 #include "filters/mbfilter_utf7imap.h"
103 #include "filters/mbfilter_utf8.h"
104 #include "filters/mbfilter_utf8_mobile.h"
105 #include "filters/mbfilter_utf16.h"
106 #include "filters/mbfilter_utf32.h"
107 #include "filters/mbfilter_byte2.h"
108 #include "filters/mbfilter_byte4.h"
109 #include "filters/mbfilter_ucs4.h"
110 #include "filters/mbfilter_ucs2.h"
111 #include "filters/mbfilter_htmlent.h"
112 #include "filters/mbfilter_armscii8.h"
113 #include "filters/mbfilter_cp850.h"
114 
115 #ifndef HAVE_STRCASECMP
116 #ifdef HAVE_STRICMP
117 #define strcasecmp stricmp
118 #endif
119 #endif
120 
121 
122 static const mbfl_encoding *mbfl_encoding_ptr_list[] = {
123 	&mbfl_encoding_pass,
124 	&mbfl_encoding_wchar,
125 	&mbfl_encoding_byte2be,
126 	&mbfl_encoding_byte2le,
127 	&mbfl_encoding_byte4be,
128 	&mbfl_encoding_byte4le,
129 	&mbfl_encoding_base64,
130 	&mbfl_encoding_uuencode,
131 	&mbfl_encoding_html_ent,
132 	&mbfl_encoding_qprint,
133 	&mbfl_encoding_7bit,
134 	&mbfl_encoding_8bit,
135 	&mbfl_encoding_ucs4,
136 	&mbfl_encoding_ucs4be,
137 	&mbfl_encoding_ucs4le,
138 	&mbfl_encoding_ucs2,
139 	&mbfl_encoding_ucs2be,
140 	&mbfl_encoding_ucs2le,
141 	&mbfl_encoding_utf32,
142 	&mbfl_encoding_utf32be,
143 	&mbfl_encoding_utf32le,
144 	&mbfl_encoding_utf16,
145 	&mbfl_encoding_utf16be,
146 	&mbfl_encoding_utf16le,
147 	&mbfl_encoding_utf8,
148 	&mbfl_encoding_utf7,
149 	&mbfl_encoding_utf7imap,
150 	&mbfl_encoding_ascii,
151 	&mbfl_encoding_euc_jp,
152 	&mbfl_encoding_sjis,
153 	&mbfl_encoding_eucjp_win,
154 	&mbfl_encoding_eucjp2004,
155 	&mbfl_encoding_sjis_open,
156  	&mbfl_encoding_sjis_docomo,
157  	&mbfl_encoding_sjis_kddi,
158  	&mbfl_encoding_sjis_sb,
159  	&mbfl_encoding_sjis_mac,
160 	&mbfl_encoding_sjis2004,
161 	&mbfl_encoding_utf8_docomo,
162 	&mbfl_encoding_utf8_kddi_a,
163 	&mbfl_encoding_utf8_kddi_b,
164 	&mbfl_encoding_utf8_sb,
165 	&mbfl_encoding_cp932,
166 	&mbfl_encoding_cp51932,
167 	&mbfl_encoding_jis,
168 	&mbfl_encoding_2022jp,
169 	&mbfl_encoding_2022jpms,
170 	&mbfl_encoding_gb18030,
171 	&mbfl_encoding_cp1252,
172 	&mbfl_encoding_cp1254,
173 	&mbfl_encoding_8859_1,
174 	&mbfl_encoding_8859_2,
175 	&mbfl_encoding_8859_3,
176 	&mbfl_encoding_8859_4,
177 	&mbfl_encoding_8859_5,
178 	&mbfl_encoding_8859_6,
179 	&mbfl_encoding_8859_7,
180 	&mbfl_encoding_8859_8,
181 	&mbfl_encoding_8859_9,
182 	&mbfl_encoding_8859_10,
183 	&mbfl_encoding_8859_13,
184 	&mbfl_encoding_8859_14,
185 	&mbfl_encoding_8859_15,
186 	&mbfl_encoding_8859_16,
187 	&mbfl_encoding_euc_cn,
188 	&mbfl_encoding_cp936,
189 	&mbfl_encoding_hz,
190 	&mbfl_encoding_euc_tw,
191 	&mbfl_encoding_big5,
192 	&mbfl_encoding_cp950,
193 	&mbfl_encoding_euc_kr,
194 	&mbfl_encoding_uhc,
195 	&mbfl_encoding_2022kr,
196 	&mbfl_encoding_cp1251,
197 	&mbfl_encoding_cp866,
198 	&mbfl_encoding_koi8r,
199 	&mbfl_encoding_koi8u,
200 	&mbfl_encoding_armscii8,
201 	&mbfl_encoding_cp850,
202 	&mbfl_encoding_jis_ms,
203 	&mbfl_encoding_2022jp_2004,
204 	&mbfl_encoding_2022jp_kddi,
205 	&mbfl_encoding_cp50220,
206 	&mbfl_encoding_cp50220raw,
207 	&mbfl_encoding_cp50221,
208 	&mbfl_encoding_cp50222,
209 	NULL
210 };
211 
212 /* encoding resolver */
213 const mbfl_encoding *
mbfl_name2encoding(const char * name)214 mbfl_name2encoding(const char *name)
215 {
216 	const mbfl_encoding *encoding;
217 	int i, j;
218 
219 	if (name == NULL) {
220 		return NULL;
221 	}
222 
223  	i = 0;
224  	while ((encoding = mbfl_encoding_ptr_list[i++]) != NULL){
225 		if (strcasecmp(encoding->name, name) == 0) {
226 			return encoding;
227 		}
228 	}
229 
230  	/* serch MIME charset name */
231  	i = 0;
232  	while ((encoding = mbfl_encoding_ptr_list[i++]) != NULL) {
233 		if (encoding->mime_name != NULL) {
234 			if (strcasecmp(encoding->mime_name, name) == 0) {
235 				return encoding;
236 			}
237 		}
238 	}
239 
240  	/* serch aliases */
241  	i = 0;
242  	while ((encoding = mbfl_encoding_ptr_list[i++]) != NULL) {
243 		if (encoding->aliases != NULL) {
244  			j = 0;
245  			while ((*encoding->aliases)[j] != NULL) {
246 				if (strcasecmp((*encoding->aliases)[j], name) == 0) {
247 					return encoding;
248 				}
249 				j++;
250 			}
251 		}
252 	}
253 
254 	return NULL;
255 }
256 
257 const mbfl_encoding *
mbfl_no2encoding(enum mbfl_no_encoding no_encoding)258 mbfl_no2encoding(enum mbfl_no_encoding no_encoding)
259 {
260 	const mbfl_encoding *encoding;
261 	int i;
262 
263 	i = 0;
264 	while ((encoding = mbfl_encoding_ptr_list[i++]) != NULL){
265 		if (encoding->no_encoding == no_encoding) {
266 			return encoding;
267 		}
268 	}
269 
270 	return NULL;
271 }
272 
273 enum mbfl_no_encoding
mbfl_name2no_encoding(const char * name)274 mbfl_name2no_encoding(const char *name)
275 {
276 	const mbfl_encoding *encoding;
277 
278 	encoding = mbfl_name2encoding(name);
279 	if (encoding == NULL) {
280 		return mbfl_no_encoding_invalid;
281 	} else {
282 		return encoding->no_encoding;
283 	}
284 }
285 
286 const char *
mbfl_no_encoding2name(enum mbfl_no_encoding no_encoding)287 mbfl_no_encoding2name(enum mbfl_no_encoding no_encoding)
288 {
289 	const mbfl_encoding *encoding;
290 
291 	encoding = mbfl_no2encoding(no_encoding);
292 	if (encoding == NULL) {
293 		return "";
294 	} else {
295 		return encoding->name;
296 	}
297 }
298 
299 const mbfl_encoding **
mbfl_get_supported_encodings(void)300 mbfl_get_supported_encodings(void)
301 {
302 	return mbfl_encoding_ptr_list;
303 }
304 
305 const char *
mbfl_no2preferred_mime_name(enum mbfl_no_encoding no_encoding)306 mbfl_no2preferred_mime_name(enum mbfl_no_encoding no_encoding)
307 {
308 	const mbfl_encoding *encoding;
309 
310 	encoding = mbfl_no2encoding(no_encoding);
311 	if (encoding != NULL && encoding->mime_name != NULL && encoding->mime_name[0] != '\0') {
312 		return encoding->mime_name;
313 	} else {
314 		return NULL;
315 	}
316 }
317 
318 int
mbfl_is_support_encoding(const char * name)319 mbfl_is_support_encoding(const char *name)
320 {
321 	const mbfl_encoding *encoding;
322 
323 	encoding = mbfl_name2encoding(name);
324 	if (encoding == NULL) {
325 		return 0;
326 	} else {
327 		return 1;
328 	}
329 }
330