Merge commit 'b572c3a256e7bf1e4eecc8c36448c08093240a6a' into stable
[gnulib.git] / lib / unicodeio.c
1 /* Unicode character output to streams with locale dependent encoding.
2
3    Copyright (C) 2000-2003, 2006, 2008-2011 Free Software Foundation, Inc.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 /* Written by Bruno Haible <haible@clisp.cons.org>.  */
19
20 #include <config.h>
21
22 /* Specification.  */
23 #include "unicodeio.h"
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #if HAVE_ICONV
30 # include <iconv.h>
31 #endif
32
33 #include <error.h>
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
38
39 #include "localcharset.h"
40 #include "unistr.h"
41 #include "ignore-value.h"
42
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
55 /* Luckily, the encoding's name is platform independent.  */
56 #define UTF8_NAME "UTF-8"
57
58 /* Converts the Unicode character CODE to its multibyte representation
59    in the current locale and calls the SUCCESS callback on the resulting
60    byte sequence.  If an error occurs, invokes the FAILURE callback instead,
61    passing it CODE and an English error string.
62    Returns whatever the callback returned.
63    Assumes that the locale doesn't change between two calls.  */
64 long
65 unicode_to_mb (unsigned int code,
66                long (*success) (const char *buf, size_t buflen,
67                                 void *callback_arg),
68                long (*failure) (unsigned int code, const char *msg,
69                                 void *callback_arg),
70                void *callback_arg)
71 {
72   static int initialized;
73   static int is_utf8;
74 #if HAVE_ICONV
75   static iconv_t utf8_to_local;
76 #endif
77
78   char inbuf[6];
79   int count;
80
81   if (!initialized)
82     {
83       const char *charset = locale_charset ();
84
85       is_utf8 = !strcmp (charset, UTF8_NAME);
86 #if HAVE_ICONV
87       if (!is_utf8)
88         {
89           utf8_to_local = iconv_open (charset, UTF8_NAME);
90           if (utf8_to_local == (iconv_t)(-1))
91             /* For an unknown encoding, assume ASCII.  */
92             utf8_to_local = iconv_open ("ASCII", UTF8_NAME);
93         }
94 #endif
95       initialized = 1;
96     }
97
98   /* Test whether the utf8_to_local converter is available at all.  */
99   if (!is_utf8)
100     {
101 #if HAVE_ICONV
102       if (utf8_to_local == (iconv_t)(-1))
103         return failure (code, N_("iconv function not usable"), callback_arg);
104 #else
105       return failure (code, N_("iconv function not available"), callback_arg);
106 #endif
107     }
108
109   /* Convert the character to UTF-8.  */
110   count = u8_uctomb ((unsigned char *) inbuf, code, sizeof (inbuf));
111   if (count < 0)
112     return failure (code, N_("character out of range"), callback_arg);
113
114 #if HAVE_ICONV
115   if (!is_utf8)
116     {
117       char outbuf[25];
118       const char *inptr;
119       size_t inbytesleft;
120       char *outptr;
121       size_t outbytesleft;
122       size_t res;
123
124       inptr = inbuf;
125       inbytesleft = count;
126       outptr = outbuf;
127       outbytesleft = sizeof (outbuf);
128
129       /* Convert the character from UTF-8 to the locale's charset.  */
130       res = iconv (utf8_to_local,
131                    (ICONV_CONST char **)&inptr, &inbytesleft,
132                    &outptr, &outbytesleft);
133       if (inbytesleft > 0 || res == (size_t)(-1)
134           /* Irix iconv() inserts a NUL byte if it cannot convert. */
135 # if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
136           || (res > 0 && code != 0 && outptr - outbuf == 1 && *outbuf == '\0')
137 # endif
138          )
139         return failure (code, NULL, callback_arg);
140
141       /* Avoid glibc-2.1 bug and Solaris 7 bug.  */
142 # if defined _LIBICONV_VERSION \
143     || !(((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) \
144           && !defined __UCLIBC__) \
145          || defined __sun)
146
147       /* Get back to the initial shift state.  */
148       res = iconv (utf8_to_local, NULL, NULL, &outptr, &outbytesleft);
149       if (res == (size_t)(-1))
150         return failure (code, NULL, callback_arg);
151 # endif
152
153       return success (outbuf, outptr - outbuf, callback_arg);
154     }
155 #endif
156
157   /* At this point, is_utf8 is true, so no conversion is needed.  */
158   return success (inbuf, count, callback_arg);
159 }
160
161 /* Simple success callback that outputs the converted string.
162    The STREAM is passed as callback_arg.  */
163 long
164 fwrite_success_callback (const char *buf, size_t buflen, void *callback_arg)
165 {
166   FILE *stream = (FILE *) callback_arg;
167
168   /* The return value of fwrite can be ignored here, because under normal
169      conditions (STREAM is an open stream and not wide-character oriented)
170      when fwrite() returns a value != buflen it also sets STREAM's error
171      indicator.  */
172   ignore_value (fwrite (buf, 1, buflen, stream));
173   return 0;
174 }
175
176 /* Simple failure callback that displays an error and exits.  */
177 static long
178 exit_failure_callback (unsigned int code, const char *msg,
179                        void *callback_arg _GL_UNUSED)
180 {
181   if (msg == NULL)
182     error (1, 0, _("cannot convert U+%04X to local character set"), code);
183   else
184     error (1, 0, _("cannot convert U+%04X to local character set: %s"), code,
185            gettext (msg));
186   return -1;
187 }
188
189 /* Simple failure callback that displays a fallback representation in plain
190    ASCII, using the same notation as ISO C99 strings.  */
191 static long
192 fallback_failure_callback (unsigned int code,
193                            const char *msg _GL_UNUSED,
194                            void *callback_arg)
195 {
196   FILE *stream = (FILE *) callback_arg;
197
198   if (code < 0x10000)
199     fprintf (stream, "\\u%04X", code);
200   else
201     fprintf (stream, "\\U%08X", code);
202   return -1;
203 }
204
205 /* Outputs the Unicode character CODE to the output stream STREAM.
206    Upon failure, exit if exit_on_error is true, otherwise output a fallback
207    notation.  */
208 void
209 print_unicode_char (FILE *stream, unsigned int code, int exit_on_error)
210 {
211   unicode_to_mb (code, fwrite_success_callback,
212                  exit_on_error
213                  ? exit_failure_callback
214                  : fallback_failure_callback,
215                  stream);
216 }