mbrtoc32.c revision 1.7 1 1.7 rillig /* $NetBSD: mbrtoc32.c,v 1.7 2024/08/18 20:06:05 rillig 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.7 rillig __RCSID("$NetBSD: mbrtoc32.c,v 1.7 2024/08/18 20:06:05 rillig 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 mbrtoc32state *S;
106 1.1 riastrad struct _citrus_iconv *iconv = NULL;
107 1.1 riastrad size_t len;
108 1.1 riastrad char32_t c32;
109 1.1 riastrad int error, errno_save;
110 1.1 riastrad
111 1.1 riastrad /*
112 1.1 riastrad * Save errno in case _citrus_iconv_* clobbers it.
113 1.1 riastrad */
114 1.1 riastrad errno_save = errno;
115 1.1 riastrad
116 1.1 riastrad /*
117 1.1 riastrad * `If ps is a null pointer, each function uses its own
118 1.1 riastrad * internal mbstate_t object instead, which is initialized at
119 1.1 riastrad * program startup to the initial conversion state; the
120 1.1 riastrad * functions are not required to avoid data races with other
121 1.1 riastrad * calls to the same function in this case. The
122 1.1 riastrad * implementation behaves as if no library function calls
123 1.1 riastrad * these functions with a null pointer for ps.'
124 1.1 riastrad */
125 1.1 riastrad if (ps == NULL)
126 1.1 riastrad ps = &psbuf;
127 1.1 riastrad
128 1.1 riastrad /*
129 1.1 riastrad * `If s is a null pointer, the mbrtoc32 function is equivalent
130 1.1 riastrad * to the call:
131 1.1 riastrad *
132 1.1 riastrad * mbrtoc32(NULL, "", 1, ps)
133 1.1 riastrad *
134 1.1 riastrad * In this case, the values of the parameters pc32 and n are
135 1.1 riastrad * ignored.'
136 1.1 riastrad */
137 1.1 riastrad if (s == NULL) {
138 1.1 riastrad pc32 = NULL;
139 1.1 riastrad s = "";
140 1.1 riastrad n = 1;
141 1.1 riastrad }
142 1.1 riastrad
143 1.1 riastrad /*
144 1.1 riastrad * Get the private conversion state.
145 1.1 riastrad */
146 1.5 christos S = (struct mbrtoc32state *)(void *)ps;
147 1.1 riastrad
148 1.1 riastrad /*
149 1.1 riastrad * If input length is zero, the result is always incomplete by
150 1.1 riastrad * definition. Don't bother with iconv -- we'd have to
151 1.1 riastrad * disentangle truncated outputs.
152 1.1 riastrad */
153 1.1 riastrad if (n == 0) {
154 1.1 riastrad len = (size_t)-2;
155 1.1 riastrad goto out;
156 1.1 riastrad }
157 1.1 riastrad
158 1.1 riastrad /*
159 1.1 riastrad * Reset the destination buffer if this is the initial state.
160 1.1 riastrad */
161 1.1 riastrad if (S->dstleft == 0)
162 1.1 riastrad S->dstleft = sizeof(S->dstbuf);
163 1.1 riastrad
164 1.1 riastrad /*
165 1.1 riastrad * Open an iconv handle to convert locale-dependent multibyte
166 1.1 riastrad * input to UTF-32LE.
167 1.1 riastrad */
168 1.1 riastrad if ((error = _citrus_iconv_open(&iconv, _PATH_ICONV,
169 1.6 riastrad nl_langinfo_l(CODESET, loc), "utf-32le")) != 0) {
170 1.1 riastrad errno = EIO; /* XXX? */
171 1.1 riastrad len = (size_t)-1;
172 1.1 riastrad goto out;
173 1.1 riastrad }
174 1.1 riastrad
175 1.1 riastrad /*
176 1.1 riastrad * Try to iconv a minimal prefix. If we succeed, set len to
177 1.1 riastrad * the length consumed and goto ok.
178 1.1 riastrad */
179 1.1 riastrad for (len = 0; len < MIN(n, sizeof(S->srcbuf) - S->nsrc);) {
180 1.1 riastrad const char *src = S->srcbuf;
181 1.1 riastrad size_t srcleft;
182 1.1 riastrad char *dst = S->dstbuf + sizeof(S->dstbuf) - S->dstleft;
183 1.1 riastrad size_t inval;
184 1.1 riastrad
185 1.1 riastrad S->srcbuf[S->nsrc++] = s[len++];
186 1.1 riastrad srcleft = S->nsrc;
187 1.1 riastrad
188 1.1 riastrad error = _citrus_iconv_convert(iconv,
189 1.1 riastrad &src, &srcleft,
190 1.1 riastrad &dst, &S->dstleft,
191 1.1 riastrad _CITRUS_ICONV_F_HIDE_INVALID, &inval);
192 1.1 riastrad if (error != EINVAL) {
193 1.1 riastrad if (error == 0)
194 1.4 riastrad break;
195 1.1 riastrad errno = error;
196 1.1 riastrad len = (size_t)-1;
197 1.1 riastrad goto out;
198 1.1 riastrad }
199 1.1 riastrad }
200 1.1 riastrad
201 1.1 riastrad /*
202 1.4 riastrad * If it is still incomplete after trying the whole input
203 1.4 riastrad * buffer, return (size_t)-2 and let the caller try again.
204 1.1 riastrad */
205 1.4 riastrad if (error) {
206 1.4 riastrad len = (size_t)-2;
207 1.4 riastrad goto out;
208 1.4 riastrad }
209 1.1 riastrad
210 1.4 riastrad /*
211 1.1 riastrad * Successfully converted a minimal byte sequence, which should
212 1.1 riastrad * produce exactly one UTF-32 code unit, encoded in
213 1.1 riastrad * little-endian, representing a code point. Get the code
214 1.1 riastrad * point.
215 1.1 riastrad */
216 1.1 riastrad c32 = le32dec(S->dstbuf);
217 1.1 riastrad
218 1.1 riastrad /*
219 1.1 riastrad * Reject surrogate code points. We only deal in scalar
220 1.1 riastrad * values.
221 1.1 riastrad *
222 1.1 riastrad * XXX Is this necessary? Won't iconv take care of it for us?
223 1.1 riastrad */
224 1.1 riastrad if (c32 >= 0xd800 && c32 <= 0xdfff) {
225 1.1 riastrad errno = EILSEQ;
226 1.1 riastrad len = (size_t)-1;
227 1.1 riastrad goto out;
228 1.1 riastrad }
229 1.1 riastrad
230 1.1 riastrad /*
231 1.1 riastrad * Non-surrogate code point -- scalar value. Yield it.
232 1.1 riastrad */
233 1.1 riastrad if (pc32)
234 1.1 riastrad *pc32 = c32;
235 1.1 riastrad
236 1.1 riastrad /*
237 1.1 riastrad * If we got the null scalar value, return zero length, as the
238 1.1 riastrad * contract requires.
239 1.1 riastrad */
240 1.1 riastrad if (c32 == 0)
241 1.1 riastrad len = 0;
242 1.1 riastrad
243 1.1 riastrad /*
244 1.1 riastrad * Make sure we preserve errno on success.
245 1.1 riastrad */
246 1.1 riastrad errno = errno_save;
247 1.1 riastrad
248 1.1 riastrad out: if (len != (size_t)-2) {
249 1.1 riastrad S->nsrc = 0;
250 1.1 riastrad memset(S, 0, sizeof(*S)); /* paranoia */
251 1.1 riastrad }
252 1.1 riastrad 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