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