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
42 #include "filters/mbfilter_euc_cn.h"
43 #include "filters/mbfilter_hz.h"
44 #include "filters/mbfilter_euc_tw.h"
45 #include "filters/mbfilter_big5.h"
46 #include "filters/mbfilter_uhc.h"
47 #include "filters/mbfilter_euc_kr.h"
48 #include "filters/mbfilter_iso2022_kr.h"
49 #include "filters/mbfilter_sjis.h"
50 #include "filters/mbfilter_sjis_mobile.h"
51 #include "filters/mbfilter_sjis_mac.h"
52 #include "filters/mbfilter_sjis_2004.h"
53 #include "filters/mbfilter_cp51932.h"
54 #include "filters/mbfilter_jis.h"
55 #include "filters/mbfilter_iso2022_jp_ms.h"
56 #include "filters/mbfilter_iso2022jp_2004.h"
57 #include "filters/mbfilter_iso2022jp_mobile.h"
58 #include "filters/mbfilter_euc_jp.h"
59 #include "filters/mbfilter_euc_jp_win.h"
60 #include "filters/mbfilter_euc_jp_2004.h"
61 #include "filters/mbfilter_gb18030.h"
62 #include "filters/mbfilter_cp932.h"
63 #include "filters/mbfilter_cp936.h"
64 #include "filters/mbfilter_cp5022x.h"
65 #include "filters/mbfilter_base64.h"
66 #include "filters/mbfilter_qprint.h"
67 #include "filters/mbfilter_uuencode.h"
68 #include "filters/mbfilter_7bit.h"
69 #include "filters/mbfilter_utf7.h"
70 #include "filters/mbfilter_utf7imap.h"
71 #include "filters/mbfilter_utf8.h"
72 #include "filters/mbfilter_utf8_mobile.h"
73 #include "filters/mbfilter_utf16.h"
74 #include "filters/mbfilter_utf32.h"
75 #include "filters/mbfilter_ucs4.h"
76 #include "filters/mbfilter_ucs2.h"
77 #include "filters/mbfilter_htmlent.h"
78 #include "filters/mbfilter_singlebyte.h"
79
80 #ifndef HAVE_STRCASECMP
81 #ifdef HAVE_STRICMP
82 #define strcasecmp stricmp
83 #endif
84 #endif
85
86
87 static const mbfl_encoding *mbfl_encoding_ptr_list[] = {
88 &mbfl_encoding_base64,
89 &mbfl_encoding_uuencode,
90 &mbfl_encoding_html_ent,
91 &mbfl_encoding_qprint,
92 &mbfl_encoding_7bit,
93 &mbfl_encoding_8bit,
94 &mbfl_encoding_ucs4,
95 &mbfl_encoding_ucs4be,
96 &mbfl_encoding_ucs4le,
97 &mbfl_encoding_ucs2,
98 &mbfl_encoding_ucs2be,
99 &mbfl_encoding_ucs2le,
100 &mbfl_encoding_utf32,
101 &mbfl_encoding_utf32be,
102 &mbfl_encoding_utf32le,
103 &mbfl_encoding_utf16,
104 &mbfl_encoding_utf16be,
105 &mbfl_encoding_utf16le,
106 &mbfl_encoding_utf8,
107 &mbfl_encoding_utf7,
108 &mbfl_encoding_utf7imap,
109 &mbfl_encoding_ascii,
110 &mbfl_encoding_euc_jp,
111 &mbfl_encoding_sjis,
112 &mbfl_encoding_eucjp_win,
113 &mbfl_encoding_eucjp2004,
114 &mbfl_encoding_sjis_docomo,
115 &mbfl_encoding_sjis_kddi,
116 &mbfl_encoding_sjis_sb,
117 &mbfl_encoding_sjis_mac,
118 &mbfl_encoding_sjis2004,
119 &mbfl_encoding_utf8_docomo,
120 &mbfl_encoding_utf8_kddi_a,
121 &mbfl_encoding_utf8_kddi_b,
122 &mbfl_encoding_utf8_sb,
123 &mbfl_encoding_cp932,
124 &mbfl_encoding_sjiswin,
125 &mbfl_encoding_cp51932,
126 &mbfl_encoding_jis,
127 &mbfl_encoding_2022jp,
128 &mbfl_encoding_2022jpms,
129 &mbfl_encoding_gb18030,
130 &mbfl_encoding_cp1252,
131 &mbfl_encoding_cp1254,
132 &mbfl_encoding_8859_1,
133 &mbfl_encoding_8859_2,
134 &mbfl_encoding_8859_3,
135 &mbfl_encoding_8859_4,
136 &mbfl_encoding_8859_5,
137 &mbfl_encoding_8859_6,
138 &mbfl_encoding_8859_7,
139 &mbfl_encoding_8859_8,
140 &mbfl_encoding_8859_9,
141 &mbfl_encoding_8859_10,
142 &mbfl_encoding_8859_13,
143 &mbfl_encoding_8859_14,
144 &mbfl_encoding_8859_15,
145 &mbfl_encoding_8859_16,
146 &mbfl_encoding_euc_cn,
147 &mbfl_encoding_cp936,
148 &mbfl_encoding_hz,
149 &mbfl_encoding_euc_tw,
150 &mbfl_encoding_big5,
151 &mbfl_encoding_cp950,
152 &mbfl_encoding_euc_kr,
153 &mbfl_encoding_uhc,
154 &mbfl_encoding_2022kr,
155 &mbfl_encoding_cp1251,
156 &mbfl_encoding_cp866,
157 &mbfl_encoding_koi8r,
158 &mbfl_encoding_koi8u,
159 &mbfl_encoding_armscii8,
160 &mbfl_encoding_cp850,
161 &mbfl_encoding_2022jp_2004,
162 &mbfl_encoding_2022jp_kddi,
163 &mbfl_encoding_cp50220,
164 &mbfl_encoding_cp50221,
165 &mbfl_encoding_cp50222,
166 NULL
167 };
168
mbfl_name2encoding(const char * name)169 const mbfl_encoding *mbfl_name2encoding(const char *name)
170 {
171 const mbfl_encoding **encoding;
172
173 for (encoding = mbfl_encoding_ptr_list; *encoding; encoding++) {
174 if (strcasecmp((*encoding)->name, name) == 0) {
175 return *encoding;
176 }
177 }
178
179 /* search MIME charset name */
180 for (encoding = mbfl_encoding_ptr_list; *encoding; encoding++) {
181 if ((*encoding)->mime_name) {
182 if (strcasecmp((*encoding)->mime_name, name) == 0) {
183 return *encoding;
184 }
185 }
186 }
187
188 /* search aliases */
189 for (encoding = mbfl_encoding_ptr_list; *encoding; encoding++) {
190 if ((*encoding)->aliases) {
191 for (const char **alias = (*encoding)->aliases; *alias; alias++) {
192 if (strcasecmp(*alias, name) == 0) {
193 return *encoding;
194 }
195 }
196 }
197 }
198
199 return NULL;
200 }
201
mbfl_no2encoding(enum mbfl_no_encoding no_encoding)202 const mbfl_encoding *mbfl_no2encoding(enum mbfl_no_encoding no_encoding)
203 {
204 const mbfl_encoding **encoding;
205
206 for (encoding = mbfl_encoding_ptr_list; *encoding; encoding++) {
207 if ((*encoding)->no_encoding == no_encoding) {
208 return *encoding;
209 }
210 }
211
212 return NULL;
213 }
214
mbfl_name2no_encoding(const char * name)215 enum mbfl_no_encoding mbfl_name2no_encoding(const char *name)
216 {
217 const mbfl_encoding *encoding = mbfl_name2encoding(name);
218 return encoding ? encoding->no_encoding : mbfl_no_encoding_invalid;
219 }
220
mbfl_no_encoding2name(enum mbfl_no_encoding no_encoding)221 const char *mbfl_no_encoding2name(enum mbfl_no_encoding no_encoding)
222 {
223 const mbfl_encoding *encoding = mbfl_no2encoding(no_encoding);
224 return encoding ? encoding->name : "";
225 }
226
mbfl_get_supported_encodings(void)227 const mbfl_encoding **mbfl_get_supported_encodings(void)
228 {
229 return mbfl_encoding_ptr_list;
230 }
231
mbfl_no2preferred_mime_name(enum mbfl_no_encoding no_encoding)232 const char *mbfl_no2preferred_mime_name(enum mbfl_no_encoding no_encoding)
233 {
234 return mbfl_encoding_preferred_mime_name(mbfl_no2encoding(no_encoding));
235 }
236
mbfl_encoding_preferred_mime_name(const mbfl_encoding * encoding)237 const char *mbfl_encoding_preferred_mime_name(const mbfl_encoding *encoding)
238 {
239 if (encoding->mime_name && encoding->mime_name[0] != '\0') {
240 return encoding->mime_name;
241 }
242 return NULL;
243 }
244