mbfl_encoding.c 8.3 KB

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