]> Dogcows Code - chaz/tar/blob - lib/unicodeio.c
(EILSEQ): Include <time.h> and <wchar.h> if <errno.h> does not define
[chaz/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
41 /* Define EILSEQ and ENOTSUP as portably as possible. Some
42 nonstandard systems, like SunOS 4, don't have EILSEQ. Others, like
43 BSD/OS 4.1, define it in <wchar.h>. Callers that use EILSEQ and/or
44 ENOTSUP and that want to be portable to these nonstandard systems
45 should mimic the following includes and defines. */
46
47 /* BSD/OS 4.1 wchar.h defines EILSEQ, but it requires FILE (defined in
48 <stdio.h>, included above) and struct tm (defined in <time.h>) to
49 be declared. */
50 #if HAVE_WCHAR_H && ! defined EILSEQ
51 # include <time.h>
52 # include <wchar.h>
53 #endif
54
55 /* Do not define EILSEQ to be EINVAL, since callers may want to
56 distinguish EINVAL and EILSEQ. */
57 #ifndef EILSEQ
58 # define EILSEQ ENOENT
59 #endif
60 #ifndef ENOTSUP
61 # define ENOTSUP EINVAL
62 #endif
63
64 #if HAVE_ICONV
65 # include <iconv.h>
66 #endif
67
68 #if HAVE_LANGINFO_CODESET && ! USE_INCLUDED_LIBINTL
69 # include <langinfo.h>
70 #endif
71
72 #include "unicodeio.h"
73
74 /* When we pass a Unicode character to iconv(), we must pass it in a
75 suitable encoding. The standardized Unicode encodings are
76 UTF-8, UCS-2, UCS-4, UTF-16, UTF-16BE, UTF-16LE, UTF-7.
77 UCS-2 supports only characters up to \U0000FFFF.
78 UTF-16 and variants support only characters up to \U0010FFFF.
79 UTF-7 is way too complex and not supported by glibc-2.1.
80 UCS-4 specification leaves doubts about endianness and byte order
81 mark. glibc currently interprets it as big endian without byte order
82 mark, but this is not backed by an RFC.
83 So we use UTF-8. It supports characters up to \U7FFFFFFF and is
84 unambiguously defined. */
85
86 /* Stores the UTF-8 representation of the Unicode character wc in r[0..5].
87 Returns the number of bytes stored, or -1 if wc is out of range. */
88 static int
89 utf8_wctomb (unsigned char *r, unsigned int wc)
90 {
91 int count;
92
93 if (wc < 0x80)
94 count = 1;
95 else if (wc < 0x800)
96 count = 2;
97 else if (wc < 0x10000)
98 count = 3;
99 else if (wc < 0x200000)
100 count = 4;
101 else if (wc < 0x4000000)
102 count = 5;
103 else if (wc <= 0x7fffffff)
104 count = 6;
105 else
106 return -1;
107
108 switch (count)
109 {
110 /* Note: code falls through cases! */
111 case 6: r[5] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x4000000;
112 case 5: r[4] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x200000;
113 case 4: r[3] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x10000;
114 case 3: r[2] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x800;
115 case 2: r[1] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0xc0;
116 case 1: r[0] = wc;
117 }
118
119 return count;
120 }
121
122 /* Luckily, the encoding's name is platform independent. */
123 #define UTF8_NAME "UTF-8"
124
125 /* Converts the Unicode character CODE to its multibyte representation
126 in the current locale and calls SUCCESS on the resulting byte
127 sequence. If an error occurs, invoke FAILURE instead,
128 passing it CODE with errno set appropriately.
129 Assumes that the locale doesn't change between two calls.
130 Return whatever the SUCCESS or FAILURE returns. */
131 int
132 unicode_to_mb (unsigned int code,
133 int (*success) PARAMS((const char *buf, size_t buflen,
134 void *callback_arg)),
135 int (*failure) PARAMS((unsigned int code,
136 void *callback_arg)),
137 void *callback_arg)
138 {
139 static int initialized;
140 static int is_utf8;
141 #if HAVE_ICONV
142 static iconv_t utf8_to_local;
143 #endif
144
145 char inbuf[6];
146 int count;
147
148 if (!initialized)
149 {
150 const char *charset;
151
152 #if USE_INCLUDED_LIBINTL
153 extern const char *locale_charset PARAMS ((void));
154 charset = locale_charset ();
155 #else
156 # if HAVE_LANGINFO_CODESET
157 charset = nl_langinfo (CODESET);
158 # else
159 charset = "";
160 # endif
161 #endif
162
163 is_utf8 = !strcmp (charset, UTF8_NAME);
164 #if HAVE_ICONV
165 if (!is_utf8)
166 {
167 utf8_to_local = iconv_open (charset, UTF8_NAME);
168 if (utf8_to_local == (iconv_t)(-1))
169 {
170 /* For an unknown encoding, assume ASCII. */
171 utf8_to_local = iconv_open ("ASCII", UTF8_NAME);
172 if (utf8_to_local == (iconv_t)(-1))
173 return failure (code, callback_arg);
174 }
175 }
176 #endif
177 initialized = 1;
178 }
179
180 /* Convert the character to UTF-8. */
181 count = utf8_wctomb ((unsigned char *) inbuf, code);
182 if (count < 0)
183 {
184 errno = EILSEQ;
185 return failure (code, callback_arg);
186 }
187
188 if (is_utf8)
189 {
190 return success (inbuf, count, callback_arg);
191 }
192 else
193 {
194 #if HAVE_ICONV
195 char outbuf[25];
196 const char *inptr;
197 size_t inbytesleft;
198 char *outptr;
199 size_t outbytesleft;
200 size_t res;
201
202 inptr = inbuf;
203 inbytesleft = count;
204 outptr = outbuf;
205 outbytesleft = sizeof (outbuf);
206
207 /* Convert the character from UTF-8 to the locale's charset. */
208 res = iconv (utf8_to_local,
209 (ICONV_CONST char **)&inptr, &inbytesleft,
210 &outptr, &outbytesleft);
211 if (inbytesleft > 0 || res == (size_t)(-1)
212 /* Irix iconv() inserts a NUL byte if it cannot convert. */
213 # if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
214 || (res > 0 && code != 0 && outptr - outbuf == 1 && *outbuf == '\0')
215 # endif
216 )
217 {
218 if (res != (size_t)(-1))
219 errno = EILSEQ;
220 return failure (code, callback_arg);
221 }
222
223 /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
224 # if defined _LIBICONV_VERSION \
225 || !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
226
227 /* Get back to the initial shift state. */
228 res = iconv (utf8_to_local, NULL, NULL, &outptr, &outbytesleft);
229 if (res == (size_t)(-1))
230 return failure (code, callback_arg);
231 # endif
232
233 return success (outbuf, outptr - outbuf, callback_arg);
234 #else
235 errno = ENOTSUP;
236 return failure (code, callback_arg);
237 #endif
238 }
239 }
240
241 /* Simple success callback that outputs the converted string.
242 The STREAM is passed as callback_arg. */
243 int
244 print_unicode_success (const char *buf, size_t buflen, void *callback_arg)
245 {
246 FILE *stream = (FILE *) callback_arg;
247
248 return fwrite (buf, 1, buflen, stream) == 0 ? -1 : 0;
249 }
250
251 /* Simple failure callback that prints an ASCII representation, using
252 the same notation as C99 strings. */
253 int
254 print_unicode_failure (unsigned int code, void *callback_arg)
255 {
256 int e = errno;
257 FILE *stream = callback_arg;
258
259 fprintf (stream, code < 0x10000 ? "\\u%04X" : "\\U%08X", code);
260 errno = e;
261 return -1;
262 }
263
264 /* Outputs the Unicode character CODE to the output stream STREAM.
265 Returns zero if successful, -1 (setting errno) otherwise.
266 Assumes that the locale doesn't change between two calls. */
267 int
268 print_unicode_char (FILE *stream, unsigned int code)
269 {
270 return unicode_to_mb (code, print_unicode_success, print_unicode_failure,
271 stream);
272 }
This page took 0.044801 seconds and 5 git commands to generate.