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