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 char *mbfl_encoding_auto_aliases[] = {"unknown", NULL};
123
124 static const mbfl_encoding mbfl_encoding_auto = {
125 mbfl_no_encoding_auto,
126 "auto",
127 NULL,
128 (const char *(*)[])&mbfl_encoding_auto_aliases,
129 NULL,
130 0
131 };
132
133 static const mbfl_encoding *mbfl_encoding_ptr_list[] = {
134 &mbfl_encoding_pass,
135 &mbfl_encoding_auto,
136 &mbfl_encoding_wchar,
137 &mbfl_encoding_byte2be,
138 &mbfl_encoding_byte2le,
139 &mbfl_encoding_byte4be,
140 &mbfl_encoding_byte4le,
141 &mbfl_encoding_base64,
142 &mbfl_encoding_uuencode,
143 &mbfl_encoding_html_ent,
144 &mbfl_encoding_qprint,
145 &mbfl_encoding_7bit,
146 &mbfl_encoding_8bit,
147 &mbfl_encoding_ucs4,
148 &mbfl_encoding_ucs4be,
149 &mbfl_encoding_ucs4le,
150 &mbfl_encoding_ucs2,
151 &mbfl_encoding_ucs2be,
152 &mbfl_encoding_ucs2le,
153 &mbfl_encoding_utf32,
154 &mbfl_encoding_utf32be,
155 &mbfl_encoding_utf32le,
156 &mbfl_encoding_utf16,
157 &mbfl_encoding_utf16be,
158 &mbfl_encoding_utf16le,
159 &mbfl_encoding_utf8,
160 &mbfl_encoding_utf7,
161 &mbfl_encoding_utf7imap,
162 &mbfl_encoding_ascii,
163 &mbfl_encoding_euc_jp,
164 &mbfl_encoding_sjis,
165 &mbfl_encoding_eucjp_win,
166 &mbfl_encoding_eucjp2004,
167 &mbfl_encoding_sjis_open,
168 &mbfl_encoding_sjis_docomo,
169 &mbfl_encoding_sjis_kddi,
170 &mbfl_encoding_sjis_sb,
171 &mbfl_encoding_sjis_mac,
172 &mbfl_encoding_sjis2004,
173 &mbfl_encoding_utf8_docomo,
174 &mbfl_encoding_utf8_kddi_a,
175 &mbfl_encoding_utf8_kddi_b,
176 &mbfl_encoding_utf8_sb,
177 &mbfl_encoding_cp932,
178 &mbfl_encoding_cp51932,
179 &mbfl_encoding_jis,
180 &mbfl_encoding_2022jp,
181 &mbfl_encoding_2022jpms,
182 &mbfl_encoding_gb18030,
183 &mbfl_encoding_cp1252,
184 &mbfl_encoding_cp1254,
185 &mbfl_encoding_8859_1,
186 &mbfl_encoding_8859_2,
187 &mbfl_encoding_8859_3,
188 &mbfl_encoding_8859_4,
189 &mbfl_encoding_8859_5,
190 &mbfl_encoding_8859_6,
191 &mbfl_encoding_8859_7,
192 &mbfl_encoding_8859_8,
193 &mbfl_encoding_8859_9,
194 &mbfl_encoding_8859_10,
195 &mbfl_encoding_8859_13,
196 &mbfl_encoding_8859_14,
197 &mbfl_encoding_8859_15,
198 &mbfl_encoding_8859_16,
199 &mbfl_encoding_euc_cn,
200 &mbfl_encoding_cp936,
201 &mbfl_encoding_hz,
202 &mbfl_encoding_euc_tw,
203 &mbfl_encoding_big5,
204 &mbfl_encoding_cp950,
205 &mbfl_encoding_euc_kr,
206 &mbfl_encoding_uhc,
207 &mbfl_encoding_2022kr,
208 &mbfl_encoding_cp1251,
209 &mbfl_encoding_cp866,
210 &mbfl_encoding_koi8r,
211 &mbfl_encoding_koi8u,
212 &mbfl_encoding_armscii8,
213 &mbfl_encoding_cp850,
214 &mbfl_encoding_jis_ms,
215 &mbfl_encoding_2022jp_2004,
216 &mbfl_encoding_2022jp_kddi,
217 &mbfl_encoding_cp50220,
218 &mbfl_encoding_cp50220raw,
219 &mbfl_encoding_cp50221,
220 &mbfl_encoding_cp50222,
221 NULL
222 };
223
224 /* encoding resolver */
225 const mbfl_encoding *
mbfl_name2encoding(const char * name)226 mbfl_name2encoding(const char *name)
227 {
228 const mbfl_encoding *encoding;
229 int i, j;
230
231 if (name == NULL) {
232 return NULL;
233 }
234
235 i = 0;
236 while ((encoding = mbfl_encoding_ptr_list[i++]) != NULL){
237 if (strcasecmp(encoding->name, name) == 0) {
238 return encoding;
239 }
240 }
241
242 /* serch MIME charset name */
243 i = 0;
244 while ((encoding = mbfl_encoding_ptr_list[i++]) != NULL) {
245 if (encoding->mime_name != NULL) {
246 if (strcasecmp(encoding->mime_name, name) == 0) {
247 return encoding;
248 }
249 }
250 }
251
252 /* serch aliases */
253 i = 0;
254 while ((encoding = mbfl_encoding_ptr_list[i++]) != NULL) {
255 if (encoding->aliases != NULL) {
256 j = 0;
257 while ((*encoding->aliases)[j] != NULL) {
258 if (strcasecmp((*encoding->aliases)[j], name) == 0) {
259 return encoding;
260 }
261 j++;
262 }
263 }
264 }
265
266 return NULL;
267 }
268
269 const mbfl_encoding *
mbfl_no2encoding(enum mbfl_no_encoding no_encoding)270 mbfl_no2encoding(enum mbfl_no_encoding no_encoding)
271 {
272 const mbfl_encoding *encoding;
273 int i;
274
275 i = 0;
276 while ((encoding = mbfl_encoding_ptr_list[i++]) != NULL){
277 if (encoding->no_encoding == no_encoding) {
278 return encoding;
279 }
280 }
281
282 return NULL;
283 }
284
285 enum mbfl_no_encoding
mbfl_name2no_encoding(const char * name)286 mbfl_name2no_encoding(const char *name)
287 {
288 const mbfl_encoding *encoding;
289
290 encoding = mbfl_name2encoding(name);
291 if (encoding == NULL) {
292 return mbfl_no_encoding_invalid;
293 } else {
294 return encoding->no_encoding;
295 }
296 }
297
298 const char *
mbfl_no_encoding2name(enum mbfl_no_encoding no_encoding)299 mbfl_no_encoding2name(enum mbfl_no_encoding no_encoding)
300 {
301 const mbfl_encoding *encoding;
302
303 encoding = mbfl_no2encoding(no_encoding);
304 if (encoding == NULL) {
305 return "";
306 } else {
307 return encoding->name;
308 }
309 }
310
311 const mbfl_encoding **
mbfl_get_supported_encodings(void)312 mbfl_get_supported_encodings(void)
313 {
314 return mbfl_encoding_ptr_list;
315 }
316
317 const char *
mbfl_no2preferred_mime_name(enum mbfl_no_encoding no_encoding)318 mbfl_no2preferred_mime_name(enum mbfl_no_encoding no_encoding)
319 {
320 const mbfl_encoding *encoding;
321
322 encoding = mbfl_no2encoding(no_encoding);
323 if (encoding != NULL && encoding->mime_name != NULL && encoding->mime_name[0] != '\0') {
324 return encoding->mime_name;
325 } else {
326 return NULL;
327 }
328 }
329
330 int
mbfl_is_support_encoding(const char * name)331 mbfl_is_support_encoding(const char *name)
332 {
333 const mbfl_encoding *encoding;
334
335 encoding = mbfl_name2encoding(name);
336 if (encoding == NULL) {
337 return 0;
338 } else {
339 return 1;
340 }
341 }
342