Branch
Hash :
26fd3545
Author :
Date :
2025-09-16T23:08:01
stringeq: prefer streq to strcmp in other modules * lib/acl-internal.c, lib/argmatch.c, lib/argp-help.c: * lib/argp-parse.c, lib/bcp47.c, lib/c32_get_mapping.c: * lib/c32_get_type_test.c, lib/concat-filename.c: * lib/cpu-supports.h, lib/dfa.c, lib/duplocale.c, lib/exclude.c: * lib/findprog-in.c, lib/findprog.c, lib/fopen.c, lib/freopen.c: * lib/fts.c, lib/gen-uni-tables.c, lib/getfilecon.c: * lib/getlogin.c, lib/getugroups.c, lib/hard-locale.c: * lib/hashcode-named-file.c, lib/hashkey-string.c: * lib/iconv_open.c, lib/idcache.c, lib/lc-charset-unicode.c: * lib/localcharset.c, lib/localename-environ.c: * lib/localename-unsafe.c, lib/mbs_endswith.c, lib/mountlist.c: * lib/newlocale.c, lib/nl_langinfo.c, lib/nproc.c, lib/open.c: * lib/os2-spawn.c, lib/parse-datetime.y, lib/popen.c: * lib/progreloc.c, lib/propername.c, lib/pt_chown.c, lib/putenv.c: * lib/qcopy-acl.c, lib/readutmp.c, lib/rpmatch.c: * lib/setlocale-fixes.c, lib/setlocale.c, lib/sig2str.c: * lib/spawni.c, lib/stat.c, lib/str_endswith.c, lib/streq.h: * lib/striconveha.c, lib/struniq.h, lib/supersede.c: * lib/time_rz.c, lib/unicodeio.c, lib/vasnprintf.c: * lib/vc-mtime.c, lib/wctrans-impl.h, lib/wctype-impl.h: Prefer streq to strcmp when either will do. Do not make this change to files shared with glibc. Do not make the change to test files, at least not for now. * lib/gen-uni-tables.c (streq): New static function, in same style. * modules/acl-permissions, modules/argp, modules/c32_get_mapping: * modules/c32_get_type_test, modules/concat-filename: * modules/cpu-supports, modules/dfa, modules/duplocale: * modules/exclude, modules/findprog, modules/findprog-in: * modules/fopen, modules/fopen-gnu, modules/freopen, modules/fts: * modules/getlocalename_l-simple, modules/getlocalename_l-unsafe: * modules/getlogin, modules/getugroups, modules/hard-locale: * modules/hashcode-named-file, modules/hashkey-string: * modules/iconv_open, modules/idcache, modules/localcharset: * modules/localename, modules/localename-unsafe: * modules/mbs_endswith, modules/mountlist, modules/newlocale: * modules/nl_langinfo, modules/nproc, modules/open: * modules/parse-datetime, modules/popen: * modules/posix_spawn-internal, modules/propername: * modules/pt_chown, modules/putenv-gnu, modules/qcopy-acl: * modules/rpmatch, modules/selinux-h, modules/setenv: * modules/setlocale, modules/setlocale-fixes, modules/sig2str: * modules/spawn-pipe, modules/stat, modules/str_endswith: * modules/streq, modules/striconveha, modules/supersede: * modules/time_rz, modules/uchar-h, modules/unicodeio: * modules/uniconv/base, modules/wctrans, modules/wctype: (Depends-on): Add stringeq.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
/* Conversion between the current locale's character encoding and Unicode.
Copyright (C) 2023-2025 Free Software Foundation, Inc.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Bruno Haible <bruno@clisp.org>, 2023. */
#include <config.h>
/* Specification. */
#include "lc-charset-unicode.h"
#if GL_CHAR32_T_IS_UNICODE && GL_CHAR32_T_VS_WCHAR_T_NEEDS_CONVERSION
/* We use iconv() to convert between a 'wchar_t' value and a Unicode code point.
For performance reasons, we don't allocate an iconv_t for each conversion,
but instead cache it for subsequent conversions. Since an iconv_t descriptor
can only be used in a single thread at a time, this cache must be
per-thread. */
# include <iconv.h>
# include <stdlib.h>
# include <string.h>
# include <wchar.h>
# include "localcharset.h"
# include "streq.h"
# include "glthread/once.h"
# include "glthread/tls.h"
# include "unistr.h"
/* Maximum length of encoding. Attained for "ISO-8859-15". */
# define MAX_ENCODING_LEN 11
struct converters
{
iconv_t cd_locale_to_utf8;
iconv_t cd_utf8_to_locale;
/* NUL-terminated encoding name. */
char encoding[MAX_ENCODING_LEN + 1];
};
static gl_tls_key_t converters_key; /* TLS key for a 'struct converters *' */
/* Frees a 'struct converters *', for example when a thread terminates. */
static void
free_converters (void *p)
{
if (p != NULL)
{
struct converters *conv = p;
iconv_close (conv->cd_locale_to_utf8);
iconv_close (conv->cd_utf8_to_locale);
free (conv);
}
}
static void
key_init (void)
{
gl_tls_key_init (converters_key, free_converters);
/* The per-thread initial value is NULL. */
}
/* Ensure that key_init is called once only. */
gl_once_define(static, key_init_once)
/* Returns the per-thread 'struct converters *' that contains converters for the
given encoding. Returns NULL upon failure. */
static struct converters *
get_converters (const char *encoding)
{
if (strlen (encoding) > MAX_ENCODING_LEN)
/* If this happens, increase MAX_ENCODING_LEN. */
return NULL;
gl_once (key_init_once, key_init);
struct converters *conv = gl_tls_get (converters_key);
if (conv == NULL)
{
conv = (struct converters *) malloc (sizeof (struct converters));
if (conv == NULL)
/* Out of memory. */
return NULL;
conv->cd_locale_to_utf8 = iconv_open ("UTF-8", encoding);
conv->cd_utf8_to_locale = iconv_open (encoding, "UTF-8");
if (conv->cd_locale_to_utf8 == (iconv_t)(-1)
|| conv->cd_utf8_to_locale == (iconv_t)(-1))
{
/* iconv does not support this encoding. */
if (conv->cd_locale_to_utf8 != (iconv_t)(-1))
iconv_close (conv->cd_locale_to_utf8);
if (conv->cd_utf8_to_locale != (iconv_t)(-1))
iconv_close (conv->cd_utf8_to_locale);
free (conv);
return NULL;
}
strcpy (conv->encoding, encoding);
gl_tls_set (converters_key, conv);
}
else if (!streq (conv->encoding, encoding))
{
/* The locale encoding of this thread changed. */
iconv_t new_cd_locale_to_utf8 = iconv_open ("UTF-8", encoding);
iconv_t new_cd_utf8_to_locale = iconv_open (encoding, "UTF-8");
if (new_cd_locale_to_utf8 == (iconv_t)(-1)
|| new_cd_utf8_to_locale == (iconv_t)(-1))
{
/* iconv does not support this encoding. */
if (new_cd_locale_to_utf8 != (iconv_t)(-1))
iconv_close (new_cd_locale_to_utf8);
if (new_cd_utf8_to_locale != (iconv_t)(-1))
iconv_close (new_cd_utf8_to_locale);
return NULL;
}
iconv_close (conv->cd_locale_to_utf8);
iconv_close (conv->cd_utf8_to_locale);
conv->cd_locale_to_utf8 = new_cd_locale_to_utf8;
conv->cd_utf8_to_locale = new_cd_utf8_to_locale;
strcpy (conv->encoding, encoding);
}
return conv;
}
char32_t
locale_encoding_to_unicode (wchar_t wc)
{
/* This function is like a simplified variant of u32_conv_from_encoding,
that uses a cached per-thread iconv_t instead of allocating an iconv_t
at each call. */
if (wc == 0)
/* Invalid argument. */
abort ();
const char *encoding = locale_charset ();
if (STREQ_OPT (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0))
/* Assume that if the locale encoding is UTF-8, the wchar_t encoding is
Unicode. */
return wc;
if (STREQ_OPT (encoding, "ASCII", 'A', 'S', 'C', 'I', 'I', 0, 0, 0, 0))
/* In the POSIX locale, avoid conversion errors. */
return wc;
struct converters *conv = get_converters (encoding);
if (conv == NULL)
return 0;
char mbbuf[64];
size_t mbcnt;
{
mbstate_t state;
mbszero (&state);
mbcnt = wcrtomb (mbbuf, wc, &state);
if (mbcnt > sizeof (mbbuf))
/* wcrtomb did not recognize the wide character wc. */
abort ();
}
char utf8buf[6];
size_t utf8cnt;
{
char *mbptr = mbbuf;
size_t mbsize = mbcnt;
char *utf8ptr = utf8buf;
size_t utf8size = sizeof (utf8buf);
size_t ret = iconv (conv->cd_locale_to_utf8,
(ICONV_CONST char **) &mbptr, &mbsize,
&utf8ptr, &utf8size);
if (ret == (size_t)(-1))
/* Conversion error. */
return 0;
if (mbsize != 0)
/* The input was not entirely converted. */
return 0;
utf8cnt = sizeof (utf8buf) - utf8size; /* = utf8ptr - utf8buf */
if (utf8cnt == 0)
/* The conversion produced no output. */
return 0;
}
ucs4_t uc;
if (u8_mbtouc (&uc, (const uint8_t *) utf8buf, utf8cnt) != utf8cnt)
/* iconv produced an invalid UTF-8 byte sequence. */
abort ();
return uc;
}
wchar_t
unicode_to_locale_encoding (char32_t uc)
{
if (uc == 0)
/* Invalid argument. */
abort ();
/* This function is like a simplified variant of u32_conv_to_encoding
that uses a cached per-thread iconv_t instead of allocating an iconv_t
at each call. */
const char *encoding = locale_charset ();
if (STREQ_OPT (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0))
/* Assume that if the locale encoding is UTF-8, the wchar_t encoding is
Unicode. */
return uc;
if (STREQ_OPT (encoding, "ASCII", 'A', 'S', 'C', 'I', 'I', 0, 0, 0, 0))
/* In the POSIX locale, avoid conversion errors. */
return uc;
struct converters *conv = get_converters (encoding);
if (conv == NULL)
return 0;
char utf8buf[6];
int utf8cnt = u8_uctomb ((uint8_t *) utf8buf, uc, sizeof (utf8buf));
if (utf8cnt < 0)
/* Out-of-range Unicode character. */
return 0;
char mbbuf[64];
size_t mbcnt;
{
char *utf8ptr = utf8buf;
size_t utf8size = utf8cnt;
char *mbptr = mbbuf;
size_t mbsize = sizeof (mbbuf);
size_t ret = iconv (conv->cd_utf8_to_locale,
(ICONV_CONST char **) &utf8ptr, &utf8size,
&mbptr, &mbsize);
if (ret == (size_t)(-1))
/* Conversion error. */
return 0;
if (utf8size != 0)
/* The input was not entirely converted. */
return 0;
mbcnt = sizeof (mbbuf) - mbsize; /* = mbptr - mbbuf */
if (mbcnt == 0)
/* The conversion produced no output. */
return 0;
}
wchar_t wc;
{
mbstate_t state;
mbszero (&state);
if (mbrtowc (&wc, mbbuf, mbcnt, &state) != mbcnt)
/* iconv produced an invalid multibyte sequence. */
return 0;
}
return wc;
}
#else
/* This declaration is solely to ensure that after preprocessing
this file is never empty. */
typedef int dummy;
#endif