unicodeio.c 6.7 KB

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