unicodeio.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #if HAVE_ICONV
  32. # include <iconv.h>
  33. #endif
  34. /* Some systems, like SunOS 4, don't have EILSEQ. On these systems,
  35. define EILSEQ to some value other than EINVAL, because our invokers
  36. may want to distinguish EINVAL from EILSEQ. */
  37. #ifndef EILSEQ
  38. # define EILSEQ ENOENT
  39. #endif
  40. #ifndef ENOTSUP
  41. # define ENOTSUP EINVAL
  42. #endif
  43. #if HAVE_LANGINFO_CODESET && ! USE_INCLUDED_LIBINTL
  44. # include <langinfo.h>
  45. #endif
  46. #include "unicodeio.h"
  47. /* When we pass a Unicode character to iconv(), we must pass it in a
  48. suitable encoding. The standardized Unicode encodings are
  49. UTF-8, UCS-2, UCS-4, UTF-16, UTF-16BE, UTF-16LE, UTF-7.
  50. UCS-2 supports only characters up to \U0000FFFF.
  51. UTF-16 and variants support only characters up to \U0010FFFF.
  52. UTF-7 is way too complex and not supported by glibc-2.1.
  53. UCS-4 specification leaves doubts about endianness and byte order
  54. mark. glibc currently interprets it as big endian without byte order
  55. mark, but this is not backed by an RFC.
  56. So we use UTF-8. It supports characters up to \U7FFFFFFF and is
  57. unambiguously defined. */
  58. /* Stores the UTF-8 representation of the Unicode character wc in r[0..5].
  59. Returns the number of bytes stored, or -1 if wc is out of range. */
  60. static int
  61. utf8_wctomb (unsigned char *r, unsigned int wc)
  62. {
  63. int count;
  64. if (wc < 0x80)
  65. count = 1;
  66. else if (wc < 0x800)
  67. count = 2;
  68. else if (wc < 0x10000)
  69. count = 3;
  70. else if (wc < 0x200000)
  71. count = 4;
  72. else if (wc < 0x4000000)
  73. count = 5;
  74. else if (wc <= 0x7fffffff)
  75. count = 6;
  76. else
  77. return -1;
  78. switch (count)
  79. {
  80. /* Note: code falls through cases! */
  81. case 6: r[5] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x4000000;
  82. case 5: r[4] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x200000;
  83. case 4: r[3] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x10000;
  84. case 3: r[2] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x800;
  85. case 2: r[1] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0xc0;
  86. case 1: r[0] = wc;
  87. }
  88. return count;
  89. }
  90. /* Luckily, the encoding's name is platform independent. */
  91. #define UTF8_NAME "UTF-8"
  92. /* Converts the Unicode character CODE to its multibyte representation
  93. in the current locale and calls SUCCESS on the resulting byte
  94. sequence. If an error occurs, invoke FAILURE instead,
  95. passing it CODE with errno set appropriately.
  96. Assumes that the locale doesn't change between two calls.
  97. Return whatever the SUCCESS or FAILURE returns. */
  98. int
  99. unicode_to_mb (unsigned int code,
  100. int (*success) PARAMS((const char *buf, size_t buflen,
  101. void *callback_arg)),
  102. int (*failure) PARAMS((unsigned int code,
  103. void *callback_arg)),
  104. void *callback_arg)
  105. {
  106. static int initialized;
  107. static int is_utf8;
  108. #if HAVE_ICONV
  109. static iconv_t utf8_to_local;
  110. #endif
  111. char inbuf[6];
  112. int count;
  113. if (!initialized)
  114. {
  115. const char *charset;
  116. #if USE_INCLUDED_LIBINTL
  117. extern const char *locale_charset PARAMS ((void));
  118. charset = locale_charset ();
  119. #else
  120. # if HAVE_LANGINFO_CODESET
  121. charset = nl_langinfo (CODESET);
  122. # else
  123. charset = "";
  124. # endif
  125. #endif
  126. is_utf8 = !strcmp (charset, UTF8_NAME);
  127. #if HAVE_ICONV
  128. if (!is_utf8)
  129. {
  130. utf8_to_local = iconv_open (charset, UTF8_NAME);
  131. if (utf8_to_local == (iconv_t)(-1))
  132. {
  133. /* For an unknown encoding, assume ASCII. */
  134. utf8_to_local = iconv_open ("ASCII", UTF8_NAME);
  135. if (utf8_to_local == (iconv_t)(-1))
  136. return failure (code, callback_arg);
  137. }
  138. }
  139. #endif
  140. initialized = 1;
  141. }
  142. /* Convert the character to UTF-8. */
  143. count = utf8_wctomb ((unsigned char *) inbuf, code);
  144. if (count < 0)
  145. {
  146. errno = EILSEQ;
  147. return failure (code, callback_arg);
  148. }
  149. if (is_utf8)
  150. {
  151. return success (inbuf, count, callback_arg);
  152. }
  153. else
  154. {
  155. #if HAVE_ICONV
  156. char outbuf[25];
  157. const char *inptr;
  158. size_t inbytesleft;
  159. char *outptr;
  160. size_t outbytesleft;
  161. size_t res;
  162. inptr = inbuf;
  163. inbytesleft = count;
  164. outptr = outbuf;
  165. outbytesleft = sizeof (outbuf);
  166. /* Convert the character from UTF-8 to the locale's charset. */
  167. res = iconv (utf8_to_local,
  168. (ICONV_CONST char **)&inptr, &inbytesleft,
  169. &outptr, &outbytesleft);
  170. if (inbytesleft > 0 || res == (size_t)(-1)
  171. /* Irix iconv() inserts a NUL byte if it cannot convert. */
  172. # if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
  173. || (res > 0 && code != 0 && outptr - outbuf == 1 && *outbuf == '\0')
  174. # endif
  175. )
  176. {
  177. if (res != (size_t)(-1))
  178. errno = EILSEQ;
  179. return failure (code, callback_arg);
  180. }
  181. /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
  182. # if defined _LIBICONV_VERSION \
  183. || !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
  184. /* Get back to the initial shift state. */
  185. res = iconv (utf8_to_local, NULL, NULL, &outptr, &outbytesleft);
  186. if (res == (size_t)(-1))
  187. return failure (code, callback_arg);
  188. # endif
  189. return success (outbuf, outptr - outbuf, callback_arg);
  190. #else
  191. errno = ENOTSUP;
  192. return failure (code, callback_arg);
  193. #endif
  194. }
  195. }
  196. /* Simple success callback that outputs the converted string.
  197. The STREAM is passed as callback_arg. */
  198. int
  199. print_unicode_success (const char *buf, size_t buflen, void *callback_arg)
  200. {
  201. FILE *stream = (FILE *) callback_arg;
  202. return fwrite (buf, 1, buflen, stream) == 0 ? -1 : 0;
  203. }
  204. /* Simple failure callback that prints an ASCII representation, using
  205. the same notation as C99 strings. */
  206. int
  207. print_unicode_failure (unsigned int code, void *callback_arg)
  208. {
  209. int e = errno;
  210. FILE *stream = callback_arg;
  211. fprintf (stream, code < 0x10000 ? "\\u%04X" : "\\U%08X", code);
  212. errno = e;
  213. return -1;
  214. }
  215. /* Outputs the Unicode character CODE to the output stream STREAM.
  216. Returns zero if successful, -1 (setting errno) otherwise.
  217. Assumes that the locale doesn't change between two calls. */
  218. int
  219. print_unicode_char (FILE *stream, unsigned int code)
  220. {
  221. return unicode_to_mb (code, print_unicode_success, print_unicode_failure,
  222. stream);
  223. }