1 /* Convert multibyte character to 32-bit wide character.
2 Copyright (C) 2020-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2020. */
24 #include "attribute.h"
29 #if GL_CHAR32_T_IS_UNICODE
30 # include "lc-charset-unicode.h"
33 #if GNULIB_defined_mbstate_t /* AIX, IRIX */
34 /* Implement mbrtoc32() on top of mbtowc() for the non-UTF-8 locales
35 and directly for the UTF-8 locales. */
37 /* Note: On AIX (64-bit) we can implement mbrtoc32 in two equivalent ways:
38 - in a way that parallels the override of mbrtowc; this is the code branch
40 - in a way that invokes the overridden mbrtowc; this would be the #else
42 They are equivalent. */
44 # if AVOID_ANY_THREADS
46 /* The option '--disable-threads' explicitly requests no locking. */
48 # elif defined _WIN32 && !defined __CYGWIN__
50 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
53 # elif HAVE_PTHREAD_API
56 # if HAVE_THREADS_H && HAVE_WEAK_SYMBOLS
58 # pragma weak thrd_exit
59 # define c11_threads_in_use() (thrd_exit != NULL)
61 # define c11_threads_in_use() 0
70 # include "lc-charset-dispatch.h"
71 # include "mbtowc-lock.h"
73 static_assert (sizeof (mbstate_t) >= 4);
74 static char internal_state
[4];
77 mbrtoc32 (char32_t
*pwc
, const char *s
, size_t n
, mbstate_t *ps
)
79 # define FITS_IN_CHAR_TYPE(wc) 1
80 # include "mbrtowc-impl.h"
83 #else /* glibc, macOS, FreeBSD, NetBSD, OpenBSD, HP-UX, Solaris, Cygwin, mingw, MSVC, Minix, Android */
85 /* Implement mbrtoc32() based on the original mbrtoc32() or on mbrtowc(). */
89 # include "localcharset.h"
92 # if MBRTOC32_IN_C_LOCALE_MAYBE_EILSEQ
93 # include "hard-locale.h"
97 static mbstate_t internal_state
;
100 mbrtoc32 (char32_t
*pwc
, const char *s
, size_t n
, mbstate_t *ps
)
103 /* It's simpler to handle the case s == NULL upfront, than to worry about
104 this case later, before every test of pwc and n. */
112 # if MBRTOC32_EMPTY_INPUT_BUG || _GL_SMALL_WCHAR_T
118 ps
= &internal_state
;
120 # if HAVE_WORKING_MBRTOC32 && HAVE_WORKING_C32RTOMB && !MBRTOC32_MULTIBYTE_LOCALE_BUG
121 /* mbrtoc32() may produce different values for wc than mbrtowc(). Therefore
124 # if defined _WIN32 && !defined __CYGWIN__
126 size_t ret
= mbrtoc32 (&wc
, s
, n
, ps
);
127 if (ret
< (size_t) -2 && pwc
!= NULL
)
130 size_t ret
= mbrtoc32 (pwc
, s
, n
, ps
);
133 # if GNULIB_MBRTOC32_REGULAR
134 /* Verify that mbrtoc32 is regular. */
135 if (ret
< (size_t) -3 && ! mbsinit (ps
))
136 /* This occurs on glibc 2.36. */
138 if (ret
== (size_t) -3)
142 # if MBRTOC32_IN_C_LOCALE_MAYBE_EILSEQ
143 if ((size_t) -2 <= ret
&& n
!= 0 && ! hard_locale (LC_CTYPE
))
146 *pwc
= (unsigned char) *s
;
153 # elif _GL_SMALL_WCHAR_T
155 /* Special-case all encodings that may produce wide character values
157 const char *encoding
= locale_charset ();
158 if (STREQ_OPT (encoding
, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0))
160 /* Special-case the UTF-8 encoding. Assume that the wide-character
161 encoding in a UTF-8 locale is UCS-2 or, equivalently, UTF-16. */
163 char *pstate
= (char *)ps
;
164 size_t nstate
= pstate
[0];
202 # define FITS_IN_CHAR_TYPE(wc) 1
203 # include "mbrtowc-impl-utf8.h"
207 if (nstate
>= (res
> 0 ? res
: 1))
210 /* Set *ps to an initial state. */
211 # if defined _WIN32 && !defined __CYGWIN__
212 /* Native Windows. */
213 /* MSVC defines 'mbstate_t' as an 8-byte struct; the first 4 bytes matter.
214 On mingw, 'mbstate_t' is sometimes defined as 'int', sometimes defined
215 as an 8-byte struct, of which the first 4 bytes matter. */
216 *(unsigned int *)pstate
= 0;
217 # elif defined __CYGWIN__
218 /* Cygwin defines 'mbstate_t' as an 8-byte struct; the first 4 bytes
229 /* Here 0 <= k < m < 4. */
245 /* The conversion state is undefined, says POSIX. */
251 size_t ret
= mbrtowc (&wc
, s
, n
, ps
);
252 if (ret
< (size_t) -2 && pwc
!= NULL
)
259 /* char32_t and wchar_t are equivalent. Use mbrtowc(). */
261 size_t ret
= mbrtowc (&wc
, s
, n
, ps
);
263 # if GNULIB_MBRTOC32_REGULAR
264 /* Ensure that mbrtoc32 is regular. */
265 if (ret
< (size_t) -2 && ! mbsinit (ps
))
266 /* This occurs on glibc 2.12. */
270 # if GL_CHAR32_T_IS_UNICODE && GL_CHAR32_T_VS_WCHAR_T_NEEDS_CONVERSION
271 if (ret
< (size_t) -2 && wc
!= 0)
273 wc
= locale_encoding_to_unicode (wc
);
281 if (ret
< (size_t) -2 && pwc
!= NULL
)