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