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