*** empty log message ***
[gnulib.git] / lib / unicodeio.c
1 /* Unicode character output to streams with locale dependent encoding.
2
3    Copyright (C) 2000-2003 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 2, or (at your option)
8    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 along
16    with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* Written by Bruno Haible <haible@clisp.cons.org>.  */
20
21 /* Note: This file requires the locale_charset() function.  See in
22    libiconv-1.8/libcharset/INTEGRATE for how to obtain it.  */
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 /* Specification.  */
29 #include "unicodeio.h"
30
31 #include <stdio.h>
32 #include <string.h>
33
34 #include <errno.h>
35 #ifndef errno
36 extern int errno;
37 #endif
38
39 #if HAVE_ICONV
40 # include <iconv.h>
41 #endif
42
43 #include <error.h>
44
45 #include "gettext.h"
46 #define _(msgid) gettext (msgid)
47 #define N_(msgid) msgid
48
49 #include "localcharset.h"
50
51 /* When we pass a Unicode character to iconv(), we must pass it in a
52    suitable encoding. The standardized Unicode encodings are
53    UTF-8, UCS-2, UCS-4, UTF-16, UTF-16BE, UTF-16LE, UTF-7.
54    UCS-2 supports only characters up to \U0000FFFF.
55    UTF-16 and variants support only characters up to \U0010FFFF.
56    UTF-7 is way too complex and not supported by glibc-2.1.
57    UCS-4 specification leaves doubts about endianness and byte order
58    mark. glibc currently interprets it as big endian without byte order
59    mark, but this is not backed by an RFC.
60    So we use UTF-8. It supports characters up to \U7FFFFFFF and is
61    unambiguously defined.  */
62
63 /* Stores the UTF-8 representation of the Unicode character wc in r[0..5].
64    Returns the number of bytes stored, or -1 if wc is out of range.  */
65 static int
66 utf8_wctomb (unsigned char *r, unsigned int wc)
67 {
68   int count;
69
70   if (wc < 0x80)
71     count = 1;
72   else if (wc < 0x800)
73     count = 2;
74   else if (wc < 0x10000)
75     count = 3;
76   else if (wc < 0x200000)
77     count = 4;
78   else if (wc < 0x4000000)
79     count = 5;
80   else if (wc <= 0x7fffffff)
81     count = 6;
82   else
83     return -1;
84
85   switch (count)
86     {
87       /* Note: code falls through cases! */
88       case 6: r[5] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x4000000;
89       case 5: r[4] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x200000;
90       case 4: r[3] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x10000;
91       case 3: r[2] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x800;
92       case 2: r[1] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0xc0;
93       case 1: r[0] = wc;
94     }
95
96   return count;
97 }
98
99 /* Luckily, the encoding's name is platform independent.  */
100 #define UTF8_NAME "UTF-8"
101
102 /* Converts the Unicode character CODE to its multibyte representation
103    in the current locale and calls the SUCCESS callback on the resulting
104    byte sequence.  If an error occurs, invokes the FAILURE callback instead,
105    passing it CODE and an English error string.
106    Returns whatever the callback returned.
107    Assumes that the locale doesn't change between two calls.  */
108 long
109 unicode_to_mb (unsigned int code,
110                long (*success) (const char *buf, size_t buflen,
111                                 void *callback_arg),
112                long (*failure) (unsigned int code, const char *msg,
113                                 void *callback_arg),
114                void *callback_arg)
115 {
116   static int initialized;
117   static int is_utf8;
118 #if HAVE_ICONV
119   static iconv_t utf8_to_local;
120 #endif
121
122   char inbuf[6];
123   int count;
124
125   if (!initialized)
126     {
127       const char *charset = locale_charset ();
128
129       is_utf8 = !strcmp (charset, UTF8_NAME);
130 #if HAVE_ICONV
131       if (!is_utf8)
132         {
133           utf8_to_local = iconv_open (charset, UTF8_NAME);
134           if (utf8_to_local == (iconv_t)(-1))
135             /* For an unknown encoding, assume ASCII.  */
136             utf8_to_local = iconv_open ("ASCII", UTF8_NAME);
137         }
138 #endif
139       initialized = 1;
140     }
141
142   /* Test whether the utf8_to_local converter is available at all.  */
143   if (!is_utf8)
144     {
145 #if HAVE_ICONV
146       if (utf8_to_local == (iconv_t)(-1))
147         return failure (code, N_("iconv function not usable"), callback_arg);
148 #else
149       return failure (code, N_("iconv function not available"), callback_arg);
150 #endif
151     }
152
153   /* Convert the character to UTF-8.  */
154   count = utf8_wctomb ((unsigned char *) inbuf, code);
155   if (count < 0)
156     return failure (code, N_("character out of range"), callback_arg);
157
158 #if HAVE_ICONV
159   if (!is_utf8)
160     {
161       char outbuf[25];
162       const char *inptr;
163       size_t inbytesleft;
164       char *outptr;
165       size_t outbytesleft;
166       size_t res;
167
168       inptr = inbuf;
169       inbytesleft = count;
170       outptr = outbuf;
171       outbytesleft = sizeof (outbuf);
172
173       /* Convert the character from UTF-8 to the locale's charset.  */
174       res = iconv (utf8_to_local,
175                    (ICONV_CONST char **)&inptr, &inbytesleft,
176                    &outptr, &outbytesleft);
177       if (inbytesleft > 0 || res == (size_t)(-1)
178           /* Irix iconv() inserts a NUL byte if it cannot convert. */
179 # if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
180           || (res > 0 && code != 0 && outptr - outbuf == 1 && *outbuf == '\0')
181 # endif
182          )
183         return failure (code, NULL, callback_arg);
184
185       /* Avoid glibc-2.1 bug and Solaris 7 bug.  */
186 # if defined _LIBICONV_VERSION \
187     || !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
188
189       /* Get back to the initial shift state.  */
190       res = iconv (utf8_to_local, NULL, NULL, &outptr, &outbytesleft);
191       if (res == (size_t)(-1))
192         return failure (code, NULL, callback_arg);
193 # endif
194
195       return success (outbuf, outptr - outbuf, callback_arg);
196     }
197 #endif
198
199   /* At this point, is_utf8 is true, so no conversion is needed.  */
200   return success (inbuf, count, callback_arg);
201 }
202
203 /* Simple success callback that outputs the converted string.
204    The STREAM is passed as callback_arg.  */
205 long
206 fwrite_success_callback (const char *buf, size_t buflen, void *callback_arg)
207 {
208   FILE *stream = (FILE *) callback_arg;
209
210   fwrite (buf, 1, buflen, stream);
211   return 0;
212 }
213
214 /* Simple failure callback that displays an error and exits.  */
215 static long
216 exit_failure_callback (unsigned int code, const char *msg, void *callback_arg)
217 {
218   if (msg == NULL)
219     error (1, 0, _("cannot convert U+%04X to local character set"), code);
220   else
221     error (1, 0, _("cannot convert U+%04X to local character set: %s"), code,
222            gettext (msg));
223   return -1;
224 }
225
226 /* Simple failure callback that displays a fallback representation in plain
227    ASCII, using the same notation as ISO C99 strings.  */
228 static long
229 fallback_failure_callback (unsigned int code, const char *msg, void *callback_arg)
230 {
231   FILE *stream = (FILE *) callback_arg;
232
233   if (code < 0x10000)
234     fprintf (stream, "\\u%04X", code);
235   else
236     fprintf (stream, "\\U%08X", code);
237   return -1;
238 }
239
240 /* Outputs the Unicode character CODE to the output stream STREAM.
241    Upon failure, exit if exit_on_error is true, otherwise output a fallback
242    notation.  */
243 void
244 print_unicode_char (FILE *stream, unsigned int code, int exit_on_error)
245 {
246   unicode_to_mb (code, fwrite_success_callback,
247                  exit_on_error
248                  ? exit_failure_callback
249                  : fallback_failure_callback,
250                  stream);
251 }