Debian Codegraph @

lib/mbrtowc.c

raw
1 /* Convert multibyte character to wide character.
2 Copyright (C) 1999-2002, 2005-2026 Free Software Foundation, Inc.
3 Written by Bruno Haible <[email protected]>, 2008.
4
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 This file 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20 /* Specification. */
21 #include <wchar.h>
22
23 #if GNULIB_defined_mbstate_t
24 /* Implement mbrtowc() on top of mbtowc() for the non-UTF-8 locales
25 and directly for the UTF-8 locales. */
26
27 # include <errno.h>
28 # include <stdint.h>
29 # include <stdlib.h>
30
31 # if AVOID_ANY_THREADS
32
33 /* The option '--disable-threads' explicitly requests no locking. */
34
35 # elif defined _WIN32 && !defined __CYGWIN__
36
37 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
38 # include <windows.h>
39
40 # elif HAVE_PTHREAD_API
41
42 # include <pthread.h>
43 # if HAVE_THREADS_H && HAVE_WEAK_SYMBOLS
44 # include <threads.h>
45 # pragma weak thrd_exit
46 # define c11_threads_in_use() (thrd_exit != NULL)
47 # else
48 # define c11_threads_in_use() 0
49 # endif
50
51 # elif HAVE_THREADS_H
52
53 # include <threads.h>
54
55 # endif
56
57 # include "attribute.h"
58 # include "lc-charset-dispatch.h"
59 # include "mbtowc-lock.h"
60
61 static_assert (sizeof (mbstate_t) >= 4);
62 static char internal_state[4];
63
64 size_t
65 mbrtowc (wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
66 {
67 # define FITS_IN_CHAR_TYPE(wc) ((wc) <= WCHAR_MAX)
68 # include "mbrtowc-impl.h"
69 }
70
71 #else
72 /* Override the system's mbrtowc() function. */
73
74 # if MBRTOWC_IN_C_LOCALE_MAYBE_EILSEQ
75 # include "hard-locale.h"
76 # include <locale.h>
77 # endif
78
79 # undef mbrtowc
80
81 size_t
82 rpl_mbrtowc (wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
83 {
84 # if MBRTOWC_RETVAL_BUG || MBRTOWC_EMPTY_INPUT_BUG
85 if (s == NULL)
86 {
87 pwc = NULL;
88 s = "";
89 n = 1;
90 }
91 # endif
92
93 # if MBRTOWC_EMPTY_INPUT_BUG
94 if (n == 0)
95 return (size_t) -2;
96 # endif
97
98 wchar_t wc;
99 if (! pwc)
100 pwc = &wc;
101
102 # if MBRTOWC_RETVAL_BUG
103 {
104 static mbstate_t internal_state;
105
106 /* Override mbrtowc's internal state. We cannot call mbsinit() on the
107 hidden internal state, but we can call it on our variable. */
108 if (ps == NULL)
109 ps = &internal_state;
110
111 if (!mbsinit (ps))
112 {
113 /* Parse the rest of the multibyte character byte for byte. */
114 size_t count = 0;
115 for (; n > 0; s++, n--)
116 {
117 size_t ret = mbrtowc (&wc, s, 1, ps);
118
119 if (ret == (size_t)(-1))
120 return (size_t)(-1);
121 count++;
122 if (ret != (size_t)(-2))
123 {
124 /* The multibyte character has been completed. */
125 *pwc = wc;
126 return (wc == 0 ? 0 : count);
127 }
128 }
129 return (size_t)(-2);
130 }
131 }
132 # endif
133
134 size_t ret;
135 # if MBRTOWC_STORES_INCOMPLETE_BUG
136 ret = mbrtowc (&wc, s, n, ps);
137 if (ret < (size_t) -2 && pwc != NULL)
138 *pwc = wc;
139 # else
140 ret = mbrtowc (pwc, s, n, ps);
141 # endif
142
143 # if MBRTOWC_NUL_RETVAL_BUG
144 if (ret < (size_t) -2 && !*pwc)
145 return 0;
146 # endif
147
148 # if MBRTOWC_IN_C_LOCALE_MAYBE_EILSEQ
149 if ((size_t) -2 <= ret && n != 0 && ! hard_locale (LC_CTYPE))
150 {
151 unsigned char uc = *s;
152 *pwc = uc;
153 return 1;
154 }
155 # endif
156
157 return ret;
158 }
159
160 #endif