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_cp51932.h"
62 #include "filters/mbfilter_jis.h"
63 #include "filters/mbfilter_iso2022_jp_ms.h"
64 #include "filters/mbfilter_euc_jp.h"
65 #include "filters/mbfilter_euc_jp_win.h"
66 #include "filters/mbfilter_ascii.h"
67 #include "filters/mbfilter_koi8r.h"
68 #include "filters/mbfilter_koi8u.h"
69 #include "filters/mbfilter_cp866.h"
70 #include "filters/mbfilter_cp932.h"
71 #include "filters/mbfilter_cp936.h"
72 #include "filters/mbfilter_cp1251.h"
73 #include "filters/mbfilter_cp1252.h"
74 #include "filters/mbfilter_cp1254.h"
75 #include "filters/mbfilter_cp5022x.h"
76 #include "filters/mbfilter_iso8859_1.h"
77 #include "filters/mbfilter_iso8859_2.h"
78 #include "filters/mbfilter_iso8859_3.h"
79 #include "filters/mbfilter_iso8859_4.h"
80 #include "filters/mbfilter_iso8859_5.h"
81 #include "filters/mbfilter_iso8859_6.h"
82 #include "filters/mbfilter_iso8859_7.h"
83 #include "filters/mbfilter_iso8859_8.h"
84 #include "filters/mbfilter_iso8859_9.h"
85 #include "filters/mbfilter_iso8859_10.h"
86 #include "filters/mbfilter_iso8859_13.h"
87 #include "filters/mbfilter_iso8859_14.h"
88 #include "filters/mbfilter_iso8859_15.h"
89 #include "filters/mbfilter_iso8859_16.h"
90 #include "filters/mbfilter_base64.h"
91 #include "filters/mbfilter_qprint.h"
92 #include "filters/mbfilter_uuencode.h"
93 #include "filters/mbfilter_7bit.h"
94 #include "filters/mbfilter_utf7.h"
95 #include "filters/mbfilter_utf7imap.h"
96 #include "filters/mbfilter_utf8.h"
97 #include "filters/mbfilter_utf16.h"
98 #include "filters/mbfilter_utf32.h"
99 #include "filters/mbfilter_byte2.h"
100 #include "filters/mbfilter_byte4.h"
101 #include "filters/mbfilter_ucs4.h"
102 #include "filters/mbfilter_ucs2.h"
103 #include "filters/mbfilter_htmlent.h"
104 #include "filters/mbfilter_armscii8.h"
105 #include "filters/mbfilter_cp850.h"
106
107 #ifndef HAVE_STRCASECMP
108 #ifdef HAVE_STRICMP
109 #define strcasecmp stricmp
110 #endif
111 #endif
112
113
114 static const char *mbfl_encoding_auto_aliases[] = {"unknown", NULL};
115
116 static const mbfl_encoding mbfl_encoding_auto = {
117 mbfl_no_encoding_auto,
118 "auto",
119 NULL,
120 (const char *(*)[])&mbfl_encoding_auto_aliases,
121 NULL,
122 0
123 };
124
125 static const mbfl_encoding *mbfl_encoding_ptr_list[] = {
126 &mbfl_encoding_pass,
127 &mbfl_encoding_auto,
128 &mbfl_encoding_wchar,
129 &mbfl_encoding_byte2be,
130 &mbfl_encoding_byte2le,
131 &mbfl_encoding_byte4be,
132 &mbfl_encoding_byte4le,
133 &mbfl_encoding_base64,
134 &mbfl_encoding_uuencode,
135 &mbfl_encoding_html_ent,
136 &mbfl_encoding_qprint,
137 &mbfl_encoding_7bit,
138 &mbfl_encoding_8bit,
139 &mbfl_encoding_ucs4,
140 &mbfl_encoding_ucs4be,
141 &mbfl_encoding_ucs4le,
142 &mbfl_encoding_ucs2,
143 &mbfl_encoding_ucs2be,
144 &mbfl_encoding_ucs2le,
145 &mbfl_encoding_utf32,
146 &mbfl_encoding_utf32be,
147 &mbfl_encoding_utf32le,
148 &mbfl_encoding_utf16,
149 &mbfl_encoding_utf16be,
150 &mbfl_encoding_utf16le,
151 &mbfl_encoding_utf8,
152 &mbfl_encoding_utf7,
153 &mbfl_encoding_utf7imap,
154 &mbfl_encoding_ascii,
155 &mbfl_encoding_euc_jp,
156 &mbfl_encoding_sjis,
157 &mbfl_encoding_eucjp_win,
158 &mbfl_encoding_sjis_open,
159 &mbfl_encoding_cp932,
160 &mbfl_encoding_cp51932,
161 &mbfl_encoding_jis,
162 &mbfl_encoding_2022jp,
163 &mbfl_encoding_2022jpms,
164 &mbfl_encoding_cp1252,
165 &mbfl_encoding_cp1254,
166 &mbfl_encoding_8859_1,
167 &mbfl_encoding_8859_2,
168 &mbfl_encoding_8859_3,
169 &mbfl_encoding_8859_4,
170 &mbfl_encoding_8859_5,
171 &mbfl_encoding_8859_6,
172 &mbfl_encoding_8859_7,
173 &mbfl_encoding_8859_8,
174 &mbfl_encoding_8859_9,
175 &mbfl_encoding_8859_10,
176 &mbfl_encoding_8859_13,
177 &mbfl_encoding_8859_14,
178 &mbfl_encoding_8859_15,
179 &mbfl_encoding_8859_16,
180 &mbfl_encoding_euc_cn,
181 &mbfl_encoding_cp936,
182 &mbfl_encoding_hz,
183 &mbfl_encoding_euc_tw,
184 &mbfl_encoding_big5,
185 &mbfl_encoding_euc_kr,
186 &mbfl_encoding_uhc,
187 &mbfl_encoding_2022kr,
188 &mbfl_encoding_cp1251,
189 &mbfl_encoding_cp866,
190 &mbfl_encoding_koi8r,
191 &mbfl_encoding_koi8u,
192 &mbfl_encoding_armscii8,
193 &mbfl_encoding_cp850,
194 &mbfl_encoding_jis_ms,
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
308 int
mbfl_is_support_encoding(const char * name)309 mbfl_is_support_encoding(const char *name)
310 {
311 const mbfl_encoding *encoding;
312
313 encoding = mbfl_name2encoding(name);
314 if (encoding == NULL) {
315 return 0;
316 } else {
317 return 1;
318 }
319 }
320