unicodeio.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* Unicode character output to streams with locale dependent encoding.
  2. Copyright (C) 2000-2002 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Library General Public License as published
  5. by 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 GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  14. USA. */
  15. /* Written by Bruno Haible <[email protected]>. */
  16. /* Note: This file requires the locale_charset() function. See in
  17. libiconv-1.7/libcharset/INTEGRATE for how to obtain it. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #if HAVE_STDDEF_H
  22. # include <stddef.h>
  23. #endif
  24. #include <stdio.h>
  25. #if HAVE_STRING_H
  26. # include <string.h>
  27. #else
  28. # include <strings.h>
  29. #endif
  30. #include <errno.h>
  31. #ifndef errno
  32. extern int errno;
  33. #endif
  34. #if HAVE_ICONV
  35. # include <iconv.h>
  36. #endif
  37. #include <error.h>
  38. #if ENABLE_NLS
  39. # include <libintl.h>
  40. #else
  41. # define gettext(Text) Text
  42. #endif
  43. #define _(Text) gettext (Text)
  44. #define N_(Text) Text
  45. /* Specification. */
  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 the SUCCESS callback on the resulting
  94. byte sequence. If an error occurs, invokes the FAILURE callback instead,
  95. passing it CODE and an English error string.
  96. Returns whatever the callback returned.
  97. Assumes that the locale doesn't change between two calls. */
  98. long
  99. unicode_to_mb (unsigned int code,
  100. long (*success) PARAMS ((const char *buf, size_t buflen,
  101. void *callback_arg)),
  102. long (*failure) PARAMS ((unsigned int code, const char *msg,
  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. extern const char *locale_charset PARAMS ((void));
  116. const char *charset = locale_charset ();
  117. is_utf8 = !strcmp (charset, UTF8_NAME);
  118. #if HAVE_ICONV
  119. if (!is_utf8)
  120. {
  121. utf8_to_local = iconv_open (charset, UTF8_NAME);
  122. if (utf8_to_local == (iconv_t)(-1))
  123. /* For an unknown encoding, assume ASCII. */
  124. utf8_to_local = iconv_open ("ASCII", UTF8_NAME);
  125. }
  126. #endif
  127. initialized = 1;
  128. }
  129. /* Test whether the utf8_to_local converter is available at all. */
  130. if (!is_utf8)
  131. {
  132. #if HAVE_ICONV
  133. if (utf8_to_local == (iconv_t)(-1))
  134. return failure (code, N_("iconv function not usable"), callback_arg);
  135. #else
  136. return failure (code, N_("iconv function not available"), callback_arg);
  137. #endif
  138. }
  139. /* Convert the character to UTF-8. */
  140. count = utf8_wctomb ((unsigned char *) inbuf, code);
  141. if (count < 0)
  142. return failure (code, N_("character out of range"), callback_arg);
  143. #if HAVE_ICONV
  144. if (!is_utf8)
  145. {
  146. char outbuf[25];
  147. const char *inptr;
  148. size_t inbytesleft;
  149. char *outptr;
  150. size_t outbytesleft;
  151. size_t res;
  152. inptr = inbuf;
  153. inbytesleft = count;
  154. outptr = outbuf;
  155. outbytesleft = sizeof (outbuf);
  156. /* Convert the character from UTF-8 to the locale's charset. */
  157. res = iconv (utf8_to_local,
  158. (ICONV_CONST char **)&inptr, &inbytesleft,
  159. &outptr, &outbytesleft);
  160. if (inbytesleft > 0 || res == (size_t)(-1)
  161. /* Irix iconv() inserts a NUL byte if it cannot convert. */
  162. # if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
  163. || (res > 0 && code != 0 && outptr - outbuf == 1 && *outbuf == '\0')
  164. # endif
  165. )
  166. return failure (code, NULL, callback_arg);
  167. /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
  168. # if defined _LIBICONV_VERSION \
  169. || !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
  170. /* Get back to the initial shift state. */
  171. res = iconv (utf8_to_local, NULL, NULL, &outptr, &outbytesleft);
  172. if (res == (size_t)(-1))
  173. return failure (code, NULL, callback_arg);
  174. # endif
  175. return success (outbuf, outptr - outbuf, callback_arg);
  176. }
  177. #endif
  178. /* At this point, is_utf8 is true, so no conversion is needed. */
  179. return success (inbuf, count, callback_arg);
  180. }
  181. /* Simple success callback that outputs the converted string.
  182. The STREAM is passed as callback_arg. */
  183. long
  184. fwrite_success_callback (const char *buf, size_t buflen, void *callback_arg)
  185. {
  186. FILE *stream = (FILE *) callback_arg;
  187. fwrite (buf, 1, buflen, stream);
  188. return 0;
  189. }
  190. /* Simple failure callback that displays an error and exits. */
  191. static long
  192. exit_failure_callback (unsigned int code, const char *msg, void *callback_arg)
  193. {
  194. if (msg == NULL)
  195. error (1, 0, _("cannot convert U+%04X to local character set"), code);
  196. else
  197. error (1, 0, _("cannot convert U+%04X to local character set: %s"), code,
  198. gettext (msg));
  199. return -1;
  200. }
  201. /* Simple failure callback that displays a fallback representation in plain
  202. ASCII, using the same notation as ISO C99 strings. */
  203. static long
  204. fallback_failure_callback (unsigned int code, const char *msg, void *callback_arg)
  205. {
  206. FILE *stream = (FILE *) callback_arg;
  207. if (code < 0x10000)
  208. fprintf (stream, "\\u%04X", code);
  209. else
  210. fprintf (stream, "\\U%08X", code);
  211. return -1;
  212. }
  213. /* Outputs the Unicode character CODE to the output stream STREAM.
  214. Upon failure, exit if exit_on_error is true, otherwise output a fallback
  215. notation. */
  216. void
  217. print_unicode_char (FILE *stream, unsigned int code, int exit_on_error)
  218. {
  219. unicode_to_mb (code, fwrite_success_callback,
  220. exit_on_error
  221. ? exit_failure_callback
  222. : fallback_failure_callback,
  223. stream);
  224. }