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