unicodeio.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 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. #ifdef HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #if HAVE_STDDEF_H
  20. # include <stddef.h>
  21. #endif
  22. #include <stdio.h>
  23. #if HAVE_STRING_H
  24. # include <string.h>
  25. #else
  26. # include <strings.h>
  27. #endif
  28. #include <errno.h>
  29. #ifndef errno
  30. extern int errno;
  31. #endif
  32. #if HAVE_ICONV
  33. # include <iconv.h>
  34. #endif
  35. #include <error.h>
  36. #if ENABLE_NLS
  37. # include <libintl.h>
  38. # define _(Text) gettext (Text)
  39. #else
  40. # define _(Text) Text
  41. #endif
  42. #include "unicodeio.h"
  43. /* When we pass a Unicode character to iconv(), we must pass it in a
  44. suitable encoding. The standardized Unicode encodings are
  45. UTF-8, UCS-2, UCS-4, UTF-16, UTF-16BE, UTF-16LE, UTF-7.
  46. UCS-2 supports only characters up to \U0000FFFF.
  47. UTF-16 and variants support only characters up to \U0010FFFF.
  48. UTF-7 is way too complex and not supported by glibc-2.1.
  49. UCS-4 specification leaves doubts about endianness and byte order
  50. mark. glibc currently interprets it as big endian without byte order
  51. mark, but this is not backed by an RFC.
  52. So we use UTF-8. It supports characters up to \U7FFFFFFF and is
  53. unambiguously defined. */
  54. /* Stores the UTF-8 representation of the Unicode character wc in r[0..5].
  55. Returns the number of bytes stored, or -1 if wc is out of range. */
  56. static int
  57. utf8_wctomb (unsigned char *r, unsigned int wc)
  58. {
  59. int count;
  60. if (wc < 0x80)
  61. count = 1;
  62. else if (wc < 0x800)
  63. count = 2;
  64. else if (wc < 0x10000)
  65. count = 3;
  66. else if (wc < 0x200000)
  67. count = 4;
  68. else if (wc < 0x4000000)
  69. count = 5;
  70. else if (wc <= 0x7fffffff)
  71. count = 6;
  72. else
  73. return -1;
  74. switch (count)
  75. {
  76. /* Note: code falls through cases! */
  77. case 6: r[5] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x4000000;
  78. case 5: r[4] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x200000;
  79. case 4: r[3] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x10000;
  80. case 3: r[2] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x800;
  81. case 2: r[1] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0xc0;
  82. case 1: r[0] = wc;
  83. }
  84. return count;
  85. }
  86. /* Luckily, the encoding's name is platform independent. */
  87. #define UTF8_NAME "UTF-8"
  88. /* Converts the Unicode character CODE to its multibyte representation
  89. in the current locale and calls the CALLBACK on the resulting byte
  90. sequence.
  91. Assumes that the locale doesn't change between two calls. */
  92. void
  93. unicode_to_mb (unsigned int code,
  94. void (*callback) PARAMS((const char *buf, size_t buflen,
  95. void *callback_arg)),
  96. void *callback_arg)
  97. {
  98. static int initialized;
  99. static int is_utf8;
  100. #if HAVE_ICONV
  101. static iconv_t utf8_to_local;
  102. #endif
  103. char inbuf[6];
  104. int count;
  105. if (!initialized)
  106. {
  107. extern const char *locale_charset PARAMS ((void));
  108. const char *charset = locale_charset ();
  109. is_utf8 = !strcmp (charset, UTF8_NAME);
  110. #if HAVE_ICONV
  111. if (!is_utf8)
  112. {
  113. utf8_to_local = iconv_open (charset, UTF8_NAME);
  114. if (utf8_to_local == (iconv_t)(-1))
  115. {
  116. /* For an unknown encoding, assume ASCII. */
  117. utf8_to_local = iconv_open ("ASCII", UTF8_NAME);
  118. if (utf8_to_local == (iconv_t)(-1))
  119. error (1, 0,
  120. _("cannot convert U+%04X to local character set: iconv function not usable"),
  121. code);
  122. }
  123. }
  124. #endif
  125. initialized = 1;
  126. }
  127. /* Convert the character to UTF-8. */
  128. count = utf8_wctomb ((unsigned char *) inbuf, code);
  129. if (count < 0)
  130. error (1, 0, _("U+%04X: character out of range"), code);
  131. if (is_utf8)
  132. {
  133. callback (inbuf, count, callback_arg);
  134. }
  135. else
  136. {
  137. #if HAVE_ICONV
  138. char outbuf[25];
  139. const char *inptr;
  140. size_t inbytesleft;
  141. char *outptr;
  142. size_t outbytesleft;
  143. size_t res;
  144. inptr = inbuf;
  145. inbytesleft = count;
  146. outptr = outbuf;
  147. outbytesleft = sizeof (outbuf);
  148. /* Convert the character from UTF-8 to the locale's charset. */
  149. res = iconv (utf8_to_local,
  150. (ICONV_CONST char **)&inptr, &inbytesleft,
  151. &outptr, &outbytesleft);
  152. if (inbytesleft > 0 || res == (size_t)(-1)
  153. /* Irix iconv() inserts a NUL byte if it cannot convert. */
  154. # if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
  155. || (res > 0 && code != 0 && outptr - outbuf == 1 && *outbuf == '\0')
  156. # endif
  157. )
  158. error (1, res == (size_t)(-1) ? errno : 0,
  159. _("cannot convert U+%04X to local character set"), code);
  160. /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
  161. # if defined _LIBICONV_VERSION \
  162. || !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
  163. /* Get back to the initial shift state. */
  164. res = iconv (utf8_to_local, NULL, NULL, &outptr, &outbytesleft);
  165. if (res == (size_t)(-1))
  166. error (1, errno, _("cannot convert U+%04X to local character set"),
  167. code);
  168. # endif
  169. callback (outbuf, outptr - outbuf, callback_arg);
  170. #else
  171. error (1, 0,
  172. _("cannot convert U+%04X to local character set: iconv function not available"),
  173. code);
  174. #endif
  175. }
  176. }
  177. /* Simple callback that outputs the converted string.
  178. The STREAM is passed as callback_arg. */
  179. static void
  180. fprintf_callback (const char *buf, size_t buflen, void *callback_arg)
  181. {
  182. FILE *stream = (FILE *) callback_arg;
  183. fwrite (buf, 1, buflen, stream);
  184. }
  185. /* Outputs the Unicode character CODE to the output stream STREAM.
  186. Assumes that the locale doesn't change between two calls. */
  187. void
  188. print_unicode_char (FILE *stream, unsigned int code)
  189. {
  190. unicode_to_mb (code, fprintf_callback, stream);
  191. }