Debian Codegraph @

lib/mbrtowc-impl.h

raw
1 /* Convert multibyte character to wide character.
2 Copyright (C) 1999-2002, 2005-2026 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 /* Written by Bruno Haible <[email protected]>, 2008. */
18
19 /* This file contains the body of the mbrtowc and mbrtoc32 functions,
20 when GNULIB_defined_mbstate_t is defined. */
21
22 char *pstate = (char *)ps;
23
24 if (s == NULL)
25 {
26 pwc = NULL;
27 s = "";
28 n = 1;
29 }
30
31 if (n == 0)
32 return (size_t)(-2);
33
34 /* Here n > 0. */
35
36 if (pstate == NULL)
37 pstate = internal_state;
38
39 {
40 size_t nstate = pstate[0];
41 char buf[4];
42 const char *p;
43 size_t m;
44
45 switch (nstate)
46 {
47 case 0:
48 p = s;
49 m = n;
50 break;
51 case 3:
52 buf[2] = pstate[3];
53 FALLTHROUGH;
54 case 2:
55 buf[1] = pstate[2];
56 FALLTHROUGH;
57 case 1:
58 buf[0] = pstate[1];
59 p = buf;
60 m = nstate;
61 buf[m++] = s[0];
62 if (n >= 2 && m < 4)
63 {
64 buf[m++] = s[1];
65 if (n >= 3 && m < 4)
66 buf[m++] = s[2];
67 }
68 break;
69 default:
70 errno = EINVAL;
71 return (size_t)(-1);
72 }
73
74 /* Here m > 0. */
75
76 enc_t enc = locale_encoding_classification ();
77 int res;
78
79 if (enc == enc_utf8) /* UTF-8 */
80 {
81 /* Achieve
82 - multi-thread safety and
83 - the ability to produce wide character values > WCHAR_MAX
84 by not calling mbtowc() at all. */
85 #include "mbrtowc-impl-utf8.h"
86 }
87 else
88 {
89 /* The hidden internal state of mbtowc would make this function not
90 multi-thread safe. Achieve multi-thread safety through a lock. */
91 {
92 wchar_t wc;
93 res = mbtowc_with_lock (&wc, p, m);
94
95 if (res >= 0)
96 {
97 if ((wc == 0) != (res == 0))
98 abort ();
99 if (pwc != NULL)
100 *pwc = wc;
101 goto success;
102 }
103 }
104
105 /* mbtowc does not distinguish between invalid and incomplete multibyte
106 sequences. But mbrtowc needs to make this distinction.
107 There are two possible approaches:
108 - Use iconv() and its return value.
109 - Use built-in knowledge about the possible encodings.
110 Given the low quality of implementation of iconv() on the systems
111 that lack mbrtowc(), we use the second approach.
112 The possible encodings are:
113 - 8-bit encodings,
114 - EUC-JP, EUC-KR, GB2312, EUC-TW, BIG5, GB18030, SJIS,
115 - UTF-8 (already handled above).
116 Use specialized code for each. */
117 if (m >= 4 || m >= MB_CUR_MAX)
118 goto invalid;
119 /* Here MB_CUR_MAX > 1 and 0 < m < 4. */
120 switch (enc)
121 {
122 /* As a reference for this code, you can use the GNU libiconv
123 implementation. Look for uses of the RET_TOOFEW macro. */
124
125 case enc_eucjp: /* EUC-JP */
126 {
127 if (m == 1)
128 {
129 unsigned char c = (unsigned char) p[0];
130
131 if ((c >= 0xa1 && c < 0xff) || c == 0x8e || c == 0x8f)
132 goto incomplete;
133 }
134 if (m == 2)
135 {
136 unsigned char c = (unsigned char) p[0];
137
138 if (c == 0x8f)
139 {
140 unsigned char c2 = (unsigned char) p[1];
141
142 if (c2 >= 0xa1 && c2 < 0xff)
143 goto incomplete;
144 }
145 }
146 goto invalid;
147 }
148
149 case enc_94: /* EUC-KR, GB2312, BIG5 */
150 {
151 if (m == 1)
152 {
153 unsigned char c = (unsigned char) p[0];
154
155 if (c >= 0xa1 && c < 0xff)
156 goto incomplete;
157 }
158 goto invalid;
159 }
160
161 case enc_euctw: /* EUC-TW */
162 {
163 if (m == 1)
164 {
165 unsigned char c = (unsigned char) p[0];
166
167 if ((c >= 0xa1 && c < 0xff) || c == 0x8e)
168 goto incomplete;
169 }
170 else /* m == 2 || m == 3 */
171 {
172 unsigned char c = (unsigned char) p[0];
173
174 if (c == 0x8e)
175 goto incomplete;
176 }
177 goto invalid;
178 }
179
180 case enc_gb18030: /* GB18030 */
181 {
182 if (m == 1)
183 {
184 unsigned char c = (unsigned char) p[0];
185
186 if ((c >= 0x90 && c <= 0xe3) || (c >= 0xf8 && c <= 0xfe))
187 goto incomplete;
188 }
189 else /* m == 2 || m == 3 */
190 {
191 unsigned char c = (unsigned char) p[0];
192
193 if (c >= 0x90 && c <= 0xe3)
194 {
195 unsigned char c2 = (unsigned char) p[1];
196
197 if (c2 >= 0x30 && c2 <= 0x39)
198 {
199 if (m == 2)
200 goto incomplete;
201 else /* m == 3 */
202 {
203 unsigned char c3 = (unsigned char) p[2];
204
205 if (c3 >= 0x81 && c3 <= 0xfe)
206 goto incomplete;
207 }
208 }
209 }
210 }
211 goto invalid;
212 }
213
214 case enc_sjis: /* SJIS */
215 {
216 if (m == 1)
217 {
218 unsigned char c = (unsigned char) p[0];
219
220 if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xea)
221 || (c >= 0xf0 && c <= 0xf9))
222 goto incomplete;
223 }
224 goto invalid;
225 }
226
227 default:
228 /* An unknown multibyte encoding. */
229 goto incomplete;
230 }
231 }
232
233 success:
234 /* res >= 0 is the corrected return value of
235 mbtowc_with_lock (&wc, p, m). */
236 if (nstate >= (res > 0 ? res : 1))
237 abort ();
238 res -= nstate;
239 pstate[0] = 0;
240 return res;
241
242 incomplete:
243 {
244 size_t k = nstate;
245 /* Here 0 <= k < m < 4. */
246 pstate[++k] = s[0];
247 if (k < m)
248 {
249 pstate[++k] = s[1];
250 if (k < m)
251 pstate[++k] = s[2];
252 }
253 if (k != m)
254 abort ();
255 }
256 pstate[0] = m;
257 return (size_t)(-2);
258
259 invalid:
260 errno = EILSEQ;
261 /* The conversion state is undefined, says POSIX. */
262 return (size_t)(-1);
263 }