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