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