unicodeio.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* Unicode character output to streams with locale dependent encoding.
  2. Copyright (C) 2000, 2001 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. /* Written by Bruno Haible <[email protected]>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #if HAVE_STDDEF_H
  19. # include <stddef.h>
  20. #endif
  21. #include <stdio.h>
  22. #if HAVE_STRING_H
  23. # include <string.h>
  24. #else
  25. # include <strings.h>
  26. #endif
  27. #include <errno.h>
  28. #ifndef errno
  29. extern int errno;
  30. #endif
  31. /* Define EILSEQ and ENOTSUP as portably as possible. Some
  32. nonstandard systems, like SunOS 4, don't have EILSEQ. Others, like
  33. BSD/OS 4.1, define it in <wchar.h>. Callers that use EILSEQ and/or
  34. ENOTSUP and that want to be portable to these nonstandard systems
  35. should mimic the following includes and defines. */
  36. /* BSD/OS 4.1 wchar.h defines EILSEQ, but it requires FILE (defined in
  37. <stdio.h>, included above) and struct tm (defined in <time.h>) to
  38. be declared. */
  39. #if HAVE_WCHAR_H && ! defined EILSEQ
  40. # include <time.h>
  41. # include <wchar.h>
  42. #endif
  43. /* Do not define EILSEQ to be EINVAL, since callers may want to
  44. distinguish EINVAL and EILSEQ. */
  45. #ifndef EILSEQ
  46. # define EILSEQ ENOENT
  47. #endif
  48. #ifndef ENOTSUP
  49. # define ENOTSUP EINVAL
  50. #endif
  51. #if HAVE_ICONV
  52. # include <iconv.h>
  53. #endif
  54. #if HAVE_LANGINFO_CODESET && ! USE_INCLUDED_LIBINTL
  55. # include <langinfo.h>
  56. #endif
  57. #include "unicodeio.h"
  58. /* When we pass a Unicode character to iconv(), we must pass it in a
  59. suitable encoding. The standardized Unicode encodings are
  60. UTF-8, UCS-2, UCS-4, UTF-16, UTF-16BE, UTF-16LE, UTF-7.
  61. UCS-2 supports only characters up to \U0000FFFF.
  62. UTF-16 and variants support only characters up to \U0010FFFF.
  63. UTF-7 is way too complex and not supported by glibc-2.1.
  64. UCS-4 specification leaves doubts about endianness and byte order
  65. mark. glibc currently interprets it as big endian without byte order
  66. mark, but this is not backed by an RFC.
  67. So we use UTF-8. It supports characters up to \U7FFFFFFF and is
  68. unambiguously defined. */
  69. /* Stores the UTF-8 representation of the Unicode character wc in r[0..5].
  70. Returns the number of bytes stored, or -1 if wc is out of range. */
  71. static int
  72. utf8_wctomb (unsigned char *r, unsigned int wc)
  73. {
  74. int count;
  75. if (wc < 0x80)
  76. count = 1;
  77. else if (wc < 0x800)
  78. count = 2;
  79. else if (wc < 0x10000)
  80. count = 3;
  81. else if (wc < 0x200000)
  82. count = 4;
  83. else if (wc < 0x4000000)
  84. count = 5;
  85. else if (wc <= 0x7fffffff)
  86. count = 6;
  87. else
  88. return -1;
  89. switch (count)
  90. {
  91. /* Note: code falls through cases! */
  92. case 6: r[5] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x4000000;
  93. case 5: r[4] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x200000;
  94. case 4: r[3] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x10000;
  95. case 3: r[2] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x800;
  96. case 2: r[1] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0xc0;
  97. case 1: r[0] = wc;
  98. }
  99. return count;
  100. }
  101. /* Luckily, the encoding's name is platform independent. */
  102. #define UTF8_NAME "UTF-8"
  103. /* Converts the Unicode character CODE to its multibyte representation
  104. in the current locale and calls SUCCESS on the resulting byte
  105. sequence. If an error occurs, invoke FAILURE instead,
  106. passing it CODE with errno set appropriately.
  107. Assumes that the locale doesn't change between two calls.
  108. Return whatever the SUCCESS or FAILURE returns. */
  109. int
  110. unicode_to_mb (unsigned int code,
  111. int (*success) PARAMS((const char *buf, size_t buflen,
  112. void *callback_arg)),
  113. int (*failure) PARAMS((unsigned int code,
  114. void *callback_arg)),
  115. void *callback_arg)
  116. {
  117. static int initialized;
  118. static int is_utf8;
  119. #if HAVE_ICONV
  120. static iconv_t utf8_to_local;
  121. #endif
  122. char inbuf[6];
  123. int count;
  124. if (!initialized)
  125. {
  126. const char *charset;
  127. #if USE_INCLUDED_LIBINTL
  128. extern const char *locale_charset PARAMS ((void));
  129. charset = locale_charset ();
  130. #else
  131. # if HAVE_LANGINFO_CODESET
  132. charset = nl_langinfo (CODESET);
  133. # else
  134. charset = "";
  135. # endif
  136. #endif
  137. is_utf8 = !strcmp (charset, UTF8_NAME);
  138. #if HAVE_ICONV
  139. if (!is_utf8)
  140. {
  141. utf8_to_local = iconv_open (charset, UTF8_NAME);
  142. if (utf8_to_local == (iconv_t)(-1))
  143. {
  144. /* For an unknown encoding, assume ASCII. */
  145. utf8_to_local = iconv_open ("ASCII", UTF8_NAME);
  146. if (utf8_to_local == (iconv_t)(-1))
  147. return failure (code, callback_arg);
  148. }
  149. }
  150. #endif
  151. initialized = 1;
  152. }
  153. /* Convert the character to UTF-8. */
  154. count = utf8_wctomb ((unsigned char *) inbuf, code);
  155. if (count < 0)
  156. {
  157. errno = EILSEQ;
  158. return failure (code, callback_arg);
  159. }
  160. if (is_utf8)
  161. {
  162. return success (inbuf, count, callback_arg);
  163. }
  164. else
  165. {
  166. #if HAVE_ICONV
  167. char outbuf[25];
  168. const char *inptr;
  169. size_t inbytesleft;
  170. char *outptr;
  171. size_t outbytesleft;
  172. size_t res;
  173. inptr = inbuf;
  174. inbytesleft = count;
  175. outptr = outbuf;
  176. outbytesleft = sizeof (outbuf);
  177. /* Convert the character from UTF-8 to the locale's charset. */
  178. res = iconv (utf8_to_local,
  179. (ICONV_CONST char **)&inptr, &inbytesleft,
  180. &outptr, &outbytesleft);
  181. if (inbytesleft > 0 || res == (size_t)(-1)
  182. /* Irix iconv() inserts a NUL byte if it cannot convert. */
  183. # if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
  184. || (res > 0 && code != 0 && outptr - outbuf == 1 && *outbuf == '\0')
  185. # endif
  186. )
  187. {
  188. if (res != (size_t)(-1))
  189. errno = EILSEQ;
  190. return failure (code, callback_arg);
  191. }
  192. /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
  193. # if defined _LIBICONV_VERSION \
  194. || !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
  195. /* Get back to the initial shift state. */
  196. res = iconv (utf8_to_local, NULL, NULL, &outptr, &outbytesleft);
  197. if (res == (size_t)(-1))
  198. return failure (code, callback_arg);
  199. # endif
  200. return success (outbuf, outptr - outbuf, callback_arg);
  201. #else
  202. errno = ENOTSUP;
  203. return failure (code, callback_arg);
  204. #endif
  205. }
  206. }
  207. /* Simple success callback that outputs the converted string.
  208. The STREAM is passed as callback_arg. */
  209. int
  210. print_unicode_success (const char *buf, size_t buflen, void *callback_arg)
  211. {
  212. FILE *stream = (FILE *) callback_arg;
  213. return fwrite (buf, 1, buflen, stream) == 0 ? -1 : 0;
  214. }
  215. /* Simple failure callback that prints an ASCII representation, using
  216. the same notation as C99 strings. */
  217. int
  218. print_unicode_failure (unsigned int code, void *callback_arg)
  219. {
  220. int e = errno;
  221. FILE *stream = callback_arg;
  222. fprintf (stream, code < 0x10000 ? "\\u%04X" : "\\U%08X", code);
  223. errno = e;
  224. return -1;
  225. }
  226. /* Outputs the Unicode character CODE to the output stream STREAM.
  227. Returns zero if successful, -1 (setting errno) otherwise.
  228. Assumes that the locale doesn't change between two calls. */
  229. int
  230. print_unicode_char (FILE *stream, unsigned int code)
  231. {
  232. return unicode_to_mb (code, print_unicode_success, print_unicode_failure,
  233. stream);
  234. }