unicodeio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. #ifndef EILSEQ
  32. # define EILSEQ EINVAL
  33. #endif
  34. #ifndef ENOTSUP
  35. # define ENOTSUP EINVAL
  36. #endif
  37. #if HAVE_ICONV
  38. # include <iconv.h>
  39. #endif
  40. #if ENABLE_NLS
  41. # include <libintl.h>
  42. # define _(Text) gettext (Text)
  43. #else
  44. # define _(Text) Text
  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. 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. {
  124. /* For an unknown encoding, assume ASCII. */
  125. utf8_to_local = iconv_open ("ASCII", UTF8_NAME);
  126. if (utf8_to_local == (iconv_t)(-1))
  127. return failure (code, callback_arg);
  128. }
  129. }
  130. #endif
  131. initialized = 1;
  132. }
  133. /* Convert the character to UTF-8. */
  134. count = utf8_wctomb ((unsigned char *) inbuf, code);
  135. if (count < 0)
  136. {
  137. errno = EILSEQ;
  138. return failure (code, callback_arg);
  139. }
  140. if (is_utf8)
  141. {
  142. return success (inbuf, count, callback_arg);
  143. }
  144. else
  145. {
  146. #if HAVE_ICONV
  147. char outbuf[25];
  148. const char *inptr;
  149. size_t inbytesleft;
  150. char *outptr;
  151. size_t outbytesleft;
  152. size_t res;
  153. inptr = inbuf;
  154. inbytesleft = count;
  155. outptr = outbuf;
  156. outbytesleft = sizeof (outbuf);
  157. /* Convert the character from UTF-8 to the locale's charset. */
  158. res = iconv (utf8_to_local,
  159. (ICONV_CONST char **)&inptr, &inbytesleft,
  160. &outptr, &outbytesleft);
  161. if (inbytesleft > 0 || res == (size_t)(-1)
  162. /* Irix iconv() inserts a NUL byte if it cannot convert. */
  163. # if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
  164. || (res > 0 && code != 0 && outptr - outbuf == 1 && *outbuf == '\0')
  165. # endif
  166. )
  167. {
  168. if (res != (size_t)(-1))
  169. errno = EILSEQ;
  170. return failure (code, callback_arg);
  171. }
  172. /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
  173. # if defined _LIBICONV_VERSION \
  174. || !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
  175. /* Get back to the initial shift state. */
  176. res = iconv (utf8_to_local, NULL, NULL, &outptr, &outbytesleft);
  177. if (res == (size_t)(-1))
  178. return failure (code, callback_arg);
  179. # endif
  180. return success (outbuf, outptr - outbuf, callback_arg);
  181. #else
  182. errno = ENOTSUP;
  183. return failure (code, callback_arg);
  184. #endif
  185. }
  186. }
  187. /* Simple success callback that outputs the converted string.
  188. The STREAM is passed as callback_arg. */
  189. int
  190. print_unicode_success (const char *buf, size_t buflen, void *callback_arg)
  191. {
  192. FILE *stream = (FILE *) callback_arg;
  193. return fwrite (buf, 1, buflen, stream) == 0 ? -1 : 0;
  194. }
  195. /* Simple failure callback that prints an ASCII representation, using
  196. the same notation as C99 strings. */
  197. int
  198. print_unicode_failure (unsigned int code, void *callback_arg)
  199. {
  200. int e = errno;
  201. FILE *stream = callback_arg;
  202. fprintf (stream, code < 0x10000 ? "\\u%04X" : "\\U%08X", code);
  203. errno = e;
  204. return -1;
  205. }
  206. /* Outputs the Unicode character CODE to the output stream STREAM.
  207. Returns zero if successful, -1 (setting errno) otherwise.
  208. Assumes that the locale doesn't change between two calls. */
  209. int
  210. print_unicode_char (FILE *stream, unsigned int code)
  211. {
  212. return unicode_to_mb (code, print_unicode_success, print_unicode_failure,
  213. stream);
  214. }