1 /* Unicode character output to streams with locale dependent encoding.
3 Copyright (C) 2000-2003, 2006, 2008-2024 Free Software Foundation, Inc.
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Bruno Haible <haible@clisp.cons.org>. */
23 #include "unicodeio.h"
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
39 #include "localcharset.h"
42 /* When we pass a Unicode character to iconv(), we must pass it in a
43 suitable encoding. The standardized Unicode encodings are
44 UTF-8, UCS-2, UCS-4, UTF-16, UTF-16BE, UTF-16LE, UTF-7.
45 UCS-2 supports only characters up to \U0000FFFF.
46 UTF-16 and variants support only characters up to \U0010FFFF.
47 UTF-7 is way too complex and not supported by glibc-2.1.
48 UCS-4 specification leaves doubts about endianness and byte order
49 mark. glibc currently interprets it as big endian without byte order
50 mark, but this is not backed by an RFC.
51 So we use UTF-8. It supports characters up to \U7FFFFFFF and is
52 unambiguously defined. */
54 /* Luckily, the encoding's name is platform independent. */
55 #define UTF8_NAME "UTF-8"
57 /* Converts the Unicode character CODE to its multibyte representation
58 in the current locale and calls the SUCCESS callback on the resulting
59 byte sequence. If an error occurs, invokes the FAILURE callback instead,
60 passing it CODE and an English error string.
61 Returns whatever the callback returned.
62 Assumes that the locale doesn't change between two calls. */
64 unicode_to_mb (unsigned int code
,
65 long (*success
) (const char *buf
, size_t buflen
,
67 long (*failure
) (unsigned int code
, const char *msg
,
71 static int initialized
;
74 static iconv_t utf8_to_local
;
82 const char *charset
= locale_charset ();
84 is_utf8
= !strcmp (charset
, UTF8_NAME
);
88 utf8_to_local
= iconv_open (charset
, UTF8_NAME
);
89 if (utf8_to_local
== (iconv_t
)(-1))
90 /* For an unknown encoding, assume ASCII. */
91 utf8_to_local
= iconv_open ("ASCII", UTF8_NAME
);
97 /* Test whether the utf8_to_local converter is available at all. */
101 if (utf8_to_local
== (iconv_t
)(-1))
102 return failure (code
, N_("iconv function not usable"), callback_arg
);
104 return failure (code
, N_("iconv function not available"), callback_arg
);
108 /* Convert the character to UTF-8. */
109 count
= u8_uctomb ((unsigned char *) inbuf
, code
, sizeof (inbuf
));
111 return failure (code
, N_("character out of range"), callback_arg
);
126 outbytesleft
= sizeof (outbuf
);
128 /* Convert the character from UTF-8 to the locale's charset. */
129 res
= iconv (utf8_to_local
,
130 (ICONV_CONST
char **)&inptr
, &inbytesleft
,
131 &outptr
, &outbytesleft
);
132 /* Analyze what iconv() actually did and distinguish replacements
133 that are OK (no need to invoke the FAILURE callback), such as
134 - replacing GREEK SMALL LETTER MU with MICRO SIGN, or
135 - replacing FULLWIDTH COLON with ':', or
136 - replacing a Unicode TAG character (U+E00xx) with an empty string,
137 from replacements that are worse than the FAILURE callback, such as
138 - replacing 'ç' with '?' (NetBSD, Solaris 11) or '*' (musl) or
140 if (inbytesleft
> 0 || res
== (size_t)(-1)
141 /* Irix iconv() inserts a NUL byte if it cannot convert. */
142 # if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
143 || (res
> 0 && code
!= 0 && outptr
- outbuf
== 1 && *outbuf
== '\0')
145 /* FreeBSD iconv(), NetBSD iconv(), and Solaris 11 iconv() insert
146 a '?' if they cannot convert. */
147 # if !defined _LIBICONV_VERSION || (_LIBICONV_VERSION == 0x10b && defined __APPLE__)
148 || (res
> 0 && outptr
- outbuf
== 1 && *outbuf
== '?')
150 /* musl libc iconv() inserts a '*' if it cannot convert. */
151 # if !defined _LIBICONV_VERSION && MUSL_LIBC
152 || (res
> 0 && outptr
- outbuf
== 1 && *outbuf
== '*')
155 return failure (code
, NULL
, callback_arg
);
157 /* Get back to the initial shift state. */
158 res
= iconv (utf8_to_local
, NULL
, NULL
, &outptr
, &outbytesleft
);
159 if (res
== (size_t)(-1))
160 return failure (code
, NULL
, callback_arg
);
162 return success (outbuf
, outptr
- outbuf
, callback_arg
);
166 /* At this point, is_utf8 is true, so no conversion is needed. */
167 return success (inbuf
, count
, callback_arg
);
170 /* Simple success callback that outputs the converted string.
171 The STREAM is passed as callback_arg. */
173 fwrite_success_callback (const char *buf
, size_t buflen
, void *callback_arg
)
175 FILE *stream
= (FILE *) callback_arg
;
177 /* The return value of fwrite can be ignored here, because under normal
178 conditions (STREAM is an open stream and not wide-character oriented)
179 when fwrite() returns a value != buflen it also sets STREAM's error
181 fwrite (buf
, 1, buflen
, stream
);
185 /* Simple failure callback that displays an error and exits. */
187 exit_failure_callback (unsigned int code
, const char *msg
,
188 _GL_UNUSED
void *callback_arg
)
191 error (1, 0, _("cannot convert U+%04X to local character set"), code
);
193 error (1, 0, _("cannot convert U+%04X to local character set: %s"), code
,
198 /* Simple failure callback that displays a fallback representation in plain
199 ASCII, using the same notation as ISO C99 strings. */
201 fallback_failure_callback (unsigned int code
,
202 _GL_UNUSED
const char *msg
,
205 FILE *stream
= (FILE *) callback_arg
;
208 fprintf (stream
, "\\u%04X", code
);
210 fprintf (stream
, "\\U%08X", code
);
214 /* Outputs the Unicode character CODE to the output stream STREAM.
215 Upon failure, exit if exit_on_error is true, otherwise output a fallback
218 print_unicode_char (FILE *stream
, unsigned int code
, int exit_on_error
)
220 /* Simplify this subset to avoid potential iconv() issues
221 for that range at least. */
222 if (32 <= code
&& code
< 128)
224 char code_char
= code
;
225 fwrite_success_callback (&code_char
, sizeof code_char
, stream
);
228 unicode_to_mb (code
, fwrite_success_callback
,
230 ? exit_failure_callback
231 : fallback_failure_callback
,