mbrtoc32.c revision 1.9 1 1.9 riastrad /* $NetBSD: mbrtoc32.c,v 1.9 2024/08/20 17:43:24 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2024 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * Redistribution and use in source and binary forms, with or without
8 1.1 riastrad * modification, are permitted provided that the following conditions
9 1.1 riastrad * are met:
10 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
11 1.1 riastrad * notice, this list of conditions and the following disclaimer.
12 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
14 1.1 riastrad * documentation and/or other materials provided with the distribution.
15 1.1 riastrad *
16 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
27 1.1 riastrad */
28 1.1 riastrad
29 1.1 riastrad /*
30 1.1 riastrad * mbrtoc32(&c32, s, n, ps)
31 1.1 riastrad *
32 1.1 riastrad * Decode a Unicode UTF-32 code unit from up to n bytes out of the
33 1.1 riastrad * multibyte string s, and store it at c32, using multibyte
34 1.1 riastrad * encoding state ps. A UTF-32 code unit is also a Unicode scalar
35 1.1 riastrad * value, which is any Unicode code point except a surrogate.
36 1.1 riastrad *
37 1.1 riastrad * Return the number of bytes consumed on success, or 0 if the
38 1.1 riastrad * code unit is NUL, or (size_t)-2 if the input is incomplete, or
39 1.1 riastrad * (size_t)-1 on error with errno set to EILSEQ.
40 1.1 riastrad *
41 1.1 riastrad * In the case of incomplete input, the decoding state so far
42 1.1 riastrad * after processing s[0], s[1], ..., s[n - 1] is saved in ps, so
43 1.1 riastrad * subsequent calls to mbrtoc32 will pick up n bytes later into
44 1.1 riastrad * the input stream.
45 1.1 riastrad *
46 1.1 riastrad * References:
47 1.1 riastrad *
48 1.1 riastrad * The Unicode Standard, Version 15.0 -- Core Specification, The
49 1.7 rillig * Unicode Consortium, Sec. 3.8 `Surrogates', p. 118.
50 1.7 rillig * https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=144
51 1.7 rillig * https://web.archive.org/web/20240718101254/https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=144
52 1.1 riastrad */
53 1.1 riastrad
54 1.1 riastrad #include <sys/cdefs.h>
55 1.9 riastrad __RCSID("$NetBSD: mbrtoc32.c,v 1.9 2024/08/20 17:43:24 riastradh Exp $");
56 1.3 riastrad
57 1.3 riastrad #include "namespace.h"
58 1.1 riastrad
59 1.1 riastrad #include <sys/param.h> /* MIN */
60 1.1 riastrad #include <sys/types.h> /* broken citrus_*.h */
61 1.1 riastrad #include <sys/queue.h> /* broken citrus_*.h */
62 1.1 riastrad
63 1.1 riastrad #include <assert.h>
64 1.1 riastrad #include <errno.h>
65 1.1 riastrad #include <langinfo.h>
66 1.1 riastrad #include <limits.h>
67 1.6 riastrad #include <locale.h>
68 1.1 riastrad #include <paths.h>
69 1.2 riastrad #include <stdalign.h>
70 1.1 riastrad #include <stddef.h>
71 1.1 riastrad #include <stdlib.h>
72 1.1 riastrad #include <string.h>
73 1.1 riastrad #include <uchar.h>
74 1.1 riastrad #include <wchar.h>
75 1.1 riastrad
76 1.1 riastrad #include "citrus_types.h" /* broken citrus_iconv.h */
77 1.1 riastrad #include "citrus_module.h" /* broken citrus_iconv.h */
78 1.1 riastrad #include "citrus_hash.h" /* broken citrus_iconv.h */
79 1.1 riastrad #include "citrus_iconv.h"
80 1.6 riastrad #include "setlocale_local.h"
81 1.1 riastrad
82 1.1 riastrad #include "mbrtoc32.h"
83 1.1 riastrad
84 1.1 riastrad __CTASSERT(sizeof(struct mbrtoc32state) <= sizeof(mbstate_t));
85 1.2 riastrad __CTASSERT(alignof(struct mbrtoc32state) <= alignof(mbstate_t));
86 1.1 riastrad
87 1.3 riastrad #ifdef __weak_alias
88 1.3 riastrad __weak_alias(mbrtoc32,_mbrtoc32)
89 1.6 riastrad __weak_alias(mbrtoc32_l,_mbrtoc32_l)
90 1.3 riastrad #endif
91 1.3 riastrad
92 1.1 riastrad size_t
93 1.1 riastrad mbrtoc32(char32_t *restrict pc32, const char *restrict s, size_t n,
94 1.1 riastrad mbstate_t *restrict ps)
95 1.1 riastrad {
96 1.6 riastrad
97 1.6 riastrad return mbrtoc32_l(pc32, s, n, ps, _current_locale());
98 1.6 riastrad }
99 1.6 riastrad
100 1.6 riastrad size_t
101 1.6 riastrad mbrtoc32_l(char32_t *restrict pc32, const char *restrict s, size_t n,
102 1.6 riastrad mbstate_t *restrict ps, locale_t restrict loc)
103 1.6 riastrad {
104 1.1 riastrad static mbstate_t psbuf;
105 1.1 riastrad struct _citrus_iconv *iconv = NULL;
106 1.8 riastrad wchar_t wc;
107 1.8 riastrad mbstate_t wcrtombstate = {0};
108 1.8 riastrad char mb[MB_LEN_MAX];
109 1.9 riastrad size_t mb_len;
110 1.8 riastrad char utf32le[MB_LEN_MAX];
111 1.8 riastrad const char *src;
112 1.8 riastrad char *dst;
113 1.8 riastrad size_t srcleft, dstleft, inval;
114 1.8 riastrad char32_t c32;
115 1.1 riastrad size_t len;
116 1.1 riastrad int error, errno_save;
117 1.1 riastrad
118 1.1 riastrad /*
119 1.1 riastrad * Save errno in case _citrus_iconv_* clobbers it.
120 1.1 riastrad */
121 1.1 riastrad errno_save = errno;
122 1.1 riastrad
123 1.1 riastrad /*
124 1.1 riastrad * `If ps is a null pointer, each function uses its own
125 1.1 riastrad * internal mbstate_t object instead, which is initialized at
126 1.1 riastrad * program startup to the initial conversion state; the
127 1.1 riastrad * functions are not required to avoid data races with other
128 1.1 riastrad * calls to the same function in this case. The
129 1.1 riastrad * implementation behaves as if no library function calls
130 1.1 riastrad * these functions with a null pointer for ps.'
131 1.1 riastrad */
132 1.1 riastrad if (ps == NULL)
133 1.1 riastrad ps = &psbuf;
134 1.1 riastrad
135 1.1 riastrad /*
136 1.1 riastrad * `If s is a null pointer, the mbrtoc32 function is equivalent
137 1.1 riastrad * to the call:
138 1.1 riastrad *
139 1.1 riastrad * mbrtoc32(NULL, "", 1, ps)
140 1.1 riastrad *
141 1.1 riastrad * In this case, the values of the parameters pc32 and n are
142 1.1 riastrad * ignored.'
143 1.1 riastrad */
144 1.1 riastrad if (s == NULL) {
145 1.1 riastrad pc32 = NULL;
146 1.1 riastrad s = "";
147 1.1 riastrad n = 1;
148 1.1 riastrad }
149 1.1 riastrad
150 1.1 riastrad /*
151 1.1 riastrad * If input length is zero, the result is always incomplete by
152 1.1 riastrad * definition. Don't bother with iconv -- we'd have to
153 1.1 riastrad * disentangle truncated outputs.
154 1.1 riastrad */
155 1.1 riastrad if (n == 0) {
156 1.1 riastrad len = (size_t)-2;
157 1.1 riastrad goto out;
158 1.1 riastrad }
159 1.1 riastrad
160 1.1 riastrad /*
161 1.1 riastrad * Open an iconv handle to convert locale-dependent multibyte
162 1.1 riastrad * input to UTF-32LE.
163 1.1 riastrad */
164 1.1 riastrad if ((error = _citrus_iconv_open(&iconv, _PATH_ICONV,
165 1.6 riastrad nl_langinfo_l(CODESET, loc), "utf-32le")) != 0) {
166 1.1 riastrad errno = EIO; /* XXX? */
167 1.1 riastrad len = (size_t)-1;
168 1.1 riastrad goto out;
169 1.1 riastrad }
170 1.1 riastrad
171 1.1 riastrad /*
172 1.8 riastrad * Consume the next locale-dependent wide character. If no
173 1.8 riastrad * wide character can be obtained, stop here.
174 1.8 riastrad */
175 1.8 riastrad len = mbrtowc_l(&wc, s, n, ps, loc);
176 1.8 riastrad switch (len) {
177 1.8 riastrad case 0: /* NUL */
178 1.8 riastrad if (pc32)
179 1.8 riastrad *pc32 = 0;
180 1.8 riastrad goto out;
181 1.8 riastrad case (size_t)-2: /* still incomplete after n bytes */
182 1.8 riastrad case (size_t)-1: /* error */
183 1.8 riastrad goto out;
184 1.8 riastrad default: /* consumed len bytes of input */
185 1.8 riastrad break;
186 1.8 riastrad }
187 1.8 riastrad
188 1.8 riastrad /*
189 1.8 riastrad * We consumed a wide character from the input. Convert it to
190 1.8 riastrad * a multibyte sequence _in the initial conversion state_, so
191 1.8 riastrad * we can pass that through iconv to get a Unicode scalar
192 1.8 riastrad * value.
193 1.1 riastrad */
194 1.9 riastrad if ((mb_len = wcrtomb_l(mb, wc, &wcrtombstate, loc)) == (size_t)-1) {
195 1.8 riastrad len = (size_t)-1;
196 1.8 riastrad goto out;
197 1.1 riastrad }
198 1.1 riastrad
199 1.1 riastrad /*
200 1.8 riastrad * Convert the multibyte sequence to UTF-16LE.
201 1.1 riastrad */
202 1.8 riastrad src = mb;
203 1.9 riastrad srcleft = mb_len;
204 1.8 riastrad dst = utf32le;
205 1.8 riastrad dstleft = sizeof(utf32le);
206 1.8 riastrad error = _citrus_iconv_convert(iconv, &src, &srcleft, &dst, &dstleft,
207 1.8 riastrad _CITRUS_ICONV_F_HIDE_INVALID, &inval);
208 1.4 riastrad if (error) {
209 1.8 riastrad errno = error;
210 1.8 riastrad len = (size_t)-1;
211 1.4 riastrad goto out;
212 1.4 riastrad }
213 1.1 riastrad
214 1.4 riastrad /*
215 1.8 riastrad * Successfully converted the multibyte sequence to UTF-16LE,
216 1.8 riastrad * which should produce exactly one UTF-32 code unit, encoded
217 1.8 riastrad * in little-endian, representing a code point. Get the code
218 1.1 riastrad * point.
219 1.1 riastrad */
220 1.8 riastrad c32 = le32dec(utf32le);
221 1.1 riastrad
222 1.1 riastrad /*
223 1.1 riastrad * Reject surrogate code points. We only deal in scalar
224 1.1 riastrad * values.
225 1.1 riastrad *
226 1.1 riastrad * XXX Is this necessary? Won't iconv take care of it for us?
227 1.1 riastrad */
228 1.1 riastrad if (c32 >= 0xd800 && c32 <= 0xdfff) {
229 1.1 riastrad errno = EILSEQ;
230 1.1 riastrad len = (size_t)-1;
231 1.1 riastrad goto out;
232 1.1 riastrad }
233 1.1 riastrad
234 1.1 riastrad /*
235 1.1 riastrad * Non-surrogate code point -- scalar value. Yield it.
236 1.1 riastrad */
237 1.1 riastrad if (pc32)
238 1.1 riastrad *pc32 = c32;
239 1.1 riastrad
240 1.1 riastrad /*
241 1.1 riastrad * If we got the null scalar value, return zero length, as the
242 1.1 riastrad * contract requires.
243 1.1 riastrad */
244 1.1 riastrad if (c32 == 0)
245 1.1 riastrad len = 0;
246 1.1 riastrad
247 1.1 riastrad /*
248 1.1 riastrad * Make sure we preserve errno on success.
249 1.1 riastrad */
250 1.1 riastrad errno = errno_save;
251 1.1 riastrad
252 1.8 riastrad out: errno_save = errno;
253 1.1 riastrad _citrus_iconv_close(iconv);
254 1.1 riastrad errno = errno_save;
255 1.1 riastrad return len;
256 1.1 riastrad }
257