mbrtoc32.c revision 1.5 1 1.5 christos /* $NetBSD: mbrtoc32.c,v 1.5 2024/08/17 20:08:13 christos 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.1 riastrad * Unicode Consortium, Sec. 3.8 `Surrogates', p. 119.
50 1.1 riastrad * https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf
51 1.1 riastrad * https://web.archive.org/web/20240718101254/https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf
52 1.1 riastrad */
53 1.1 riastrad
54 1.1 riastrad #include <sys/cdefs.h>
55 1.5 christos __RCSID("$NetBSD: mbrtoc32.c,v 1.5 2024/08/17 20:08:13 christos 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.1 riastrad #include <paths.h>
68 1.2 riastrad #include <stdalign.h>
69 1.1 riastrad #include <stddef.h>
70 1.1 riastrad #include <stdlib.h>
71 1.1 riastrad #include <string.h>
72 1.1 riastrad #include <uchar.h>
73 1.1 riastrad #include <wchar.h>
74 1.1 riastrad
75 1.1 riastrad #include "citrus_types.h" /* broken citrus_iconv.h */
76 1.1 riastrad #include "citrus_module.h" /* broken citrus_iconv.h */
77 1.1 riastrad #include "citrus_hash.h" /* broken citrus_iconv.h */
78 1.1 riastrad #include "citrus_iconv.h"
79 1.1 riastrad
80 1.1 riastrad #include "mbrtoc32.h"
81 1.1 riastrad
82 1.1 riastrad __CTASSERT(sizeof(struct mbrtoc32state) <= sizeof(mbstate_t));
83 1.2 riastrad __CTASSERT(alignof(struct mbrtoc32state) <= alignof(mbstate_t));
84 1.1 riastrad
85 1.3 riastrad #ifdef __weak_alias
86 1.3 riastrad __weak_alias(mbrtoc32,_mbrtoc32)
87 1.3 riastrad #endif
88 1.3 riastrad
89 1.1 riastrad size_t
90 1.1 riastrad mbrtoc32(char32_t *restrict pc32, const char *restrict s, size_t n,
91 1.1 riastrad mbstate_t *restrict ps)
92 1.1 riastrad {
93 1.1 riastrad static mbstate_t psbuf;
94 1.1 riastrad struct mbrtoc32state *S;
95 1.1 riastrad struct _citrus_iconv *iconv = NULL;
96 1.1 riastrad size_t len;
97 1.1 riastrad char32_t c32;
98 1.1 riastrad int error, errno_save;
99 1.1 riastrad
100 1.1 riastrad /*
101 1.1 riastrad * Save errno in case _citrus_iconv_* clobbers it.
102 1.1 riastrad */
103 1.1 riastrad errno_save = errno;
104 1.1 riastrad
105 1.1 riastrad /*
106 1.1 riastrad * `If ps is a null pointer, each function uses its own
107 1.1 riastrad * internal mbstate_t object instead, which is initialized at
108 1.1 riastrad * program startup to the initial conversion state; the
109 1.1 riastrad * functions are not required to avoid data races with other
110 1.1 riastrad * calls to the same function in this case. The
111 1.1 riastrad * implementation behaves as if no library function calls
112 1.1 riastrad * these functions with a null pointer for ps.'
113 1.1 riastrad */
114 1.1 riastrad if (ps == NULL)
115 1.1 riastrad ps = &psbuf;
116 1.1 riastrad
117 1.1 riastrad /*
118 1.1 riastrad * `If s is a null pointer, the mbrtoc32 function is equivalent
119 1.1 riastrad * to the call:
120 1.1 riastrad *
121 1.1 riastrad * mbrtoc32(NULL, "", 1, ps)
122 1.1 riastrad *
123 1.1 riastrad * In this case, the values of the parameters pc32 and n are
124 1.1 riastrad * ignored.'
125 1.1 riastrad */
126 1.1 riastrad if (s == NULL) {
127 1.1 riastrad pc32 = NULL;
128 1.1 riastrad s = "";
129 1.1 riastrad n = 1;
130 1.1 riastrad }
131 1.1 riastrad
132 1.1 riastrad /*
133 1.1 riastrad * Get the private conversion state.
134 1.1 riastrad */
135 1.5 christos S = (struct mbrtoc32state *)(void *)ps;
136 1.1 riastrad
137 1.1 riastrad /*
138 1.1 riastrad * If input length is zero, the result is always incomplete by
139 1.1 riastrad * definition. Don't bother with iconv -- we'd have to
140 1.1 riastrad * disentangle truncated outputs.
141 1.1 riastrad */
142 1.1 riastrad if (n == 0) {
143 1.1 riastrad len = (size_t)-2;
144 1.1 riastrad goto out;
145 1.1 riastrad }
146 1.1 riastrad
147 1.1 riastrad /*
148 1.1 riastrad * Reset the destination buffer if this is the initial state.
149 1.1 riastrad */
150 1.1 riastrad if (S->dstleft == 0)
151 1.1 riastrad S->dstleft = sizeof(S->dstbuf);
152 1.1 riastrad
153 1.1 riastrad /*
154 1.1 riastrad * Open an iconv handle to convert locale-dependent multibyte
155 1.1 riastrad * input to UTF-32LE.
156 1.1 riastrad */
157 1.1 riastrad if ((error = _citrus_iconv_open(&iconv, _PATH_ICONV,
158 1.1 riastrad nl_langinfo(CODESET), "utf-32le")) != 0) {
159 1.1 riastrad errno = EIO; /* XXX? */
160 1.1 riastrad len = (size_t)-1;
161 1.1 riastrad goto out;
162 1.1 riastrad }
163 1.1 riastrad
164 1.1 riastrad /*
165 1.1 riastrad * Try to iconv a minimal prefix. If we succeed, set len to
166 1.1 riastrad * the length consumed and goto ok.
167 1.1 riastrad */
168 1.1 riastrad for (len = 0; len < MIN(n, sizeof(S->srcbuf) - S->nsrc);) {
169 1.1 riastrad const char *src = S->srcbuf;
170 1.1 riastrad size_t srcleft;
171 1.1 riastrad char *dst = S->dstbuf + sizeof(S->dstbuf) - S->dstleft;
172 1.1 riastrad size_t inval;
173 1.1 riastrad
174 1.1 riastrad S->srcbuf[S->nsrc++] = s[len++];
175 1.1 riastrad srcleft = S->nsrc;
176 1.1 riastrad
177 1.1 riastrad error = _citrus_iconv_convert(iconv,
178 1.1 riastrad &src, &srcleft,
179 1.1 riastrad &dst, &S->dstleft,
180 1.1 riastrad _CITRUS_ICONV_F_HIDE_INVALID, &inval);
181 1.1 riastrad if (error != EINVAL) {
182 1.1 riastrad if (error == 0)
183 1.4 riastrad break;
184 1.1 riastrad errno = error;
185 1.1 riastrad len = (size_t)-1;
186 1.1 riastrad goto out;
187 1.1 riastrad }
188 1.1 riastrad }
189 1.1 riastrad
190 1.1 riastrad /*
191 1.4 riastrad * If it is still incomplete after trying the whole input
192 1.4 riastrad * buffer, return (size_t)-2 and let the caller try again.
193 1.1 riastrad */
194 1.4 riastrad if (error) {
195 1.4 riastrad len = (size_t)-2;
196 1.4 riastrad goto out;
197 1.4 riastrad }
198 1.1 riastrad
199 1.4 riastrad /*
200 1.1 riastrad * Successfully converted a minimal byte sequence, which should
201 1.1 riastrad * produce exactly one UTF-32 code unit, encoded in
202 1.1 riastrad * little-endian, representing a code point. Get the code
203 1.1 riastrad * point.
204 1.1 riastrad */
205 1.1 riastrad c32 = le32dec(S->dstbuf);
206 1.1 riastrad
207 1.1 riastrad /*
208 1.1 riastrad * Reject surrogate code points. We only deal in scalar
209 1.1 riastrad * values.
210 1.1 riastrad *
211 1.1 riastrad * XXX Is this necessary? Won't iconv take care of it for us?
212 1.1 riastrad */
213 1.1 riastrad if (c32 >= 0xd800 && c32 <= 0xdfff) {
214 1.1 riastrad errno = EILSEQ;
215 1.1 riastrad len = (size_t)-1;
216 1.1 riastrad goto out;
217 1.1 riastrad }
218 1.1 riastrad
219 1.1 riastrad /*
220 1.1 riastrad * Non-surrogate code point -- scalar value. Yield it.
221 1.1 riastrad */
222 1.1 riastrad if (pc32)
223 1.1 riastrad *pc32 = c32;
224 1.1 riastrad
225 1.1 riastrad /*
226 1.1 riastrad * If we got the null scalar value, return zero length, as the
227 1.1 riastrad * contract requires.
228 1.1 riastrad */
229 1.1 riastrad if (c32 == 0)
230 1.1 riastrad len = 0;
231 1.1 riastrad
232 1.1 riastrad /*
233 1.1 riastrad * Make sure we preserve errno on success.
234 1.1 riastrad */
235 1.1 riastrad errno = errno_save;
236 1.1 riastrad
237 1.1 riastrad out: if (len != (size_t)-2) {
238 1.1 riastrad S->nsrc = 0;
239 1.1 riastrad memset(S, 0, sizeof(*S)); /* paranoia */
240 1.1 riastrad }
241 1.1 riastrad errno_save = errno;
242 1.1 riastrad _citrus_iconv_close(iconv);
243 1.1 riastrad errno = errno_save;
244 1.1 riastrad return len;
245 1.1 riastrad }
246