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