1 /* Convert multibyte character to 32-bit wide character.
2 Copyright (C) 2020 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program 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 General Public License for more details.
14 You should have received a copy of the GNU 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 GNULIB_defined_mbstate_t /* AIX, IRIX */
30 /* Implement mbrtoc32() on top of mbtowc() for the non-UTF-8 locales
31 and directly for the UTF-8 locales. */
33 # if defined _WIN32 && !defined __CYGWIN__
35 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
38 # elif HAVE_PTHREAD_API
41 # if HAVE_THREADS_H && HAVE_WEAK_SYMBOLS
43 # pragma weak thrd_exit
44 # define c11_threads_in_use() (thrd_exit != NULL)
46 # define c11_threads_in_use() 0
56 # include "lc-charset-dispatch.h"
57 # include "mbtowc-lock.h"
59 verify (sizeof (mbstate_t) >= 4);
60 static char internal_state
[4];
63 mbrtoc32 (char32_t
*pwc
, const char *s
, size_t n
, mbstate_t *ps
)
65 # define FITS_IN_CHAR_TYPE(wc) 1
66 # include "mbrtowc-impl.h"
69 #else /* glibc, macOS, FreeBSD, NetBSD, OpenBSD, HP-UX, Solaris, Cygwin, mingw, MSVC, Minix, Android */
71 /* Implement mbrtoc32() based on the original mbrtoc32() or on mbrtowc(). */
75 # include "localcharset.h"
78 # if MBRTOC32_IN_C_LOCALE_MAYBE_EILSEQ
79 # include "hard-locale.h"
83 static mbstate_t internal_state
;
86 mbrtoc32 (char32_t
*pwc
, const char *s
, size_t n
, mbstate_t *ps
)
89 /* It's simpler to handle the case s == NULL upfront, than to worry about
90 this case later, before every test of pwc and n. */
98 # if MBRTOC32_EMPTY_INPUT_BUG || _GL_LARGE_CHAR32_T
104 ps
= &internal_state
;
106 # if HAVE_WORKING_MBRTOC32
107 /* mbrtoc32() may produce different values for wc than mbrtowc(). Therefore
110 # if defined _WIN32 && !defined __CYGWIN__
112 size_t ret
= mbrtoc32 (&wc
, s
, n
, ps
);
113 if (ret
< (size_t) -2 && pwc
!= NULL
)
116 size_t ret
= mbrtoc32 (pwc
, s
, n
, ps
);
119 # if MBRTOC32_IN_C_LOCALE_MAYBE_EILSEQ
120 if ((size_t) -2 <= ret
&& n
!= 0 && ! hard_locale (LC_CTYPE
))
123 *pwc
= (unsigned char) *s
;
130 # elif _GL_LARGE_CHAR32_T
132 /* Special-case all encodings that may produce wide character values
134 const char *encoding
= locale_charset ();
135 if (STREQ_OPT (encoding
, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0))
137 /* Special-case the UTF-8 encoding. Assume that the wide-character
138 encoding in a UTF-8 locale is UCS-2 or, equivalently, UTF-16. */
140 char *pstate
= (char *)ps
;
141 size_t nstate
= pstate
[0];
179 # define FITS_IN_CHAR_TYPE(wc) 1
180 # include "mbrtowc-impl-utf8.h"
184 if (nstate
>= (res
> 0 ? res
: 1))
187 /* Set *ps to the initial state. */
188 # if defined _WIN32 && !defined __CYGWIN__
189 /* Native Windows. */
190 /* MSVC defines 'mbstate_t' as an 8-byte struct; the first 4 bytes matter.
191 On mingw, 'mbstate_t' is sometimes defined as 'int', sometimes defined
192 as an 8-byte struct, of which the first 4 bytes matter. */
193 *(unsigned int *)pstate
= 0;
194 # elif defined __CYGWIN__
195 /* Cygwin defines 'mbstate_t' as an 8-byte struct; the first 4 bytes
206 /* Here 0 <= k < m < 4. */
222 /* The conversion state is undefined, says POSIX. */
228 size_t ret
= mbrtowc (&wc
, s
, n
, ps
);
229 if (ret
< (size_t) -2 && pwc
!= NULL
)
236 /* char32_t and wchar_t are equivalent. Use mbrtowc(). */
238 size_t ret
= mbrtowc (&wc
, s
, n
, ps
);
239 if (ret
< (size_t) -2 && pwc
!= NULL
)