mbrtoc8.c revision 1.2 1 1.2 riastrad /* $NetBSD: mbrtoc8.c,v 1.2 2024/08/15 22:23:17 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 * mbrtoc16(&c16, s, n, ps)
31 1.1 riastrad *
32 1.1 riastrad * Decode a Unicode scalar value from up to n bytes out of the
33 1.1 riastrad * multibyte string s, using multibyte encoding state ps, and
34 1.1 riastrad * store the next code unit in the UTF-8 representation of that
35 1.1 riastrad * scalar value at c8.
36 1.1 riastrad *
37 1.1 riastrad * If the UTF-8 representation of that scalar value is multiple
38 1.1 riastrad * bytes long, mbrtoc8 will yield leading byte in one call that
39 1.1 riastrad * consumes input, and will yield the trailing bytes in subsequent
40 1.1 riastrad * calls without consuming any input and returning (size_t)-3
41 1.1 riastrad * instead.
42 1.1 riastrad *
43 1.1 riastrad * Return the number of bytes consumed on success, or:
44 1.1 riastrad *
45 1.1 riastrad * - 0 if the code unit is NUL, or
46 1.1 riastrad * - (size_t)-3 if a trailing byte was returned without consuming
47 1.1 riastrad * any additional input, or
48 1.1 riastrad * - (size_t)-2 if the input is incomplete, or
49 1.1 riastrad * - (size_t)-1 on error with errno set to EILSEQ.
50 1.1 riastrad *
51 1.1 riastrad * In the case of incomplete input, the decoding state so far
52 1.1 riastrad * after processing s[0], s[1], ..., s[n - 1] is saved in ps, so
53 1.1 riastrad * subsequent calls to mbrtoc8 will pick up n bytes later into
54 1.1 riastrad * the input stream.
55 1.1 riastrad *
56 1.1 riastrad * References:
57 1.1 riastrad *
58 1.1 riastrad * The Unicode Standard, Version 15.0 -- Core Specification, The
59 1.1 riastrad * Unicode Consortium, Sec. 3.8 `Surrogates', p. 119.
60 1.1 riastrad * https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=144
61 1.1 riastrad * https://web.archive.org/web/20240718101254/https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=144
62 1.1 riastrad *
63 1.1 riastrad * The Unicode Standard, Version 15.0 -- Core Specification, The
64 1.1 riastrad * Unicode Consortium, Sec. 3.9 `Unicode Encoding Forms': UTF-16,
65 1.1 riastrad * p. 124.
66 1.1 riastrad * https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=150
67 1.1 riastrad * https://web.archive.org/web/20240718101254/https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=150
68 1.1 riastrad *
69 1.1 riastrad * F. Yergeau, `UTF-8, a transformation format of ISO 10646',
70 1.1 riastrad * RFC 3629, Internet Engineering Task Force, November 2003.
71 1.1 riastrad * https://datatracker.ietf.org/doc/html/rfc3629
72 1.1 riastrad */
73 1.1 riastrad
74 1.1 riastrad #include <sys/cdefs.h>
75 1.2 riastrad __RCSID("$NetBSD: mbrtoc8.c,v 1.2 2024/08/15 22:23:17 riastradh Exp $");
76 1.2 riastrad
77 1.2 riastrad #include "namespace.h"
78 1.1 riastrad
79 1.1 riastrad #include <assert.h>
80 1.1 riastrad #include <errno.h>
81 1.1 riastrad #include <stdalign.h>
82 1.1 riastrad #include <stddef.h>
83 1.1 riastrad #include <uchar.h>
84 1.1 riastrad
85 1.1 riastrad #include "mbrtoc32.h"
86 1.1 riastrad
87 1.1 riastrad struct mbrtoc8state {
88 1.1 riastrad char8_t nleft;
89 1.1 riastrad char8_t buf[3];
90 1.1 riastrad mbstate_t mbs;
91 1.1 riastrad };
92 1.1 riastrad __CTASSERT(offsetof(struct mbrtoc8state, mbs) <= sizeof(mbstate_t));
93 1.1 riastrad __CTASSERT(sizeof(struct mbrtoc32state) <= sizeof(mbstate_t) -
94 1.1 riastrad offsetof(struct mbrtoc8state, mbs));
95 1.1 riastrad __CTASSERT(alignof(struct mbrtoc8state) <= alignof(mbstate_t));
96 1.1 riastrad
97 1.1 riastrad size_t
98 1.1 riastrad mbrtoc8(char8_t *restrict pc8, const char *restrict s, size_t n,
99 1.1 riastrad mbstate_t *restrict ps)
100 1.1 riastrad {
101 1.1 riastrad static mbstate_t psbuf;
102 1.1 riastrad struct mbrtoc8state *S;
103 1.1 riastrad char32_t c32;
104 1.1 riastrad size_t len;
105 1.1 riastrad
106 1.1 riastrad /*
107 1.1 riastrad * `If ps is a null pointer, each function uses its own
108 1.1 riastrad * internal mbstate_t object instead, which is initialized at
109 1.1 riastrad * program startup to the initial conversion state; the
110 1.1 riastrad * functions are not required to avoid data races with other
111 1.1 riastrad * calls to the same function in this case. The
112 1.1 riastrad * implementation behaves as if no library function calls
113 1.1 riastrad * these functions with a null pointer for ps.'
114 1.1 riastrad */
115 1.1 riastrad if (ps == NULL)
116 1.1 riastrad ps = &psbuf;
117 1.1 riastrad
118 1.1 riastrad /*
119 1.1 riastrad * `If s is a null pointer, the mbrtoc8 function is equivalent
120 1.1 riastrad * to the call:
121 1.1 riastrad *
122 1.1 riastrad * mbrtoc8(NULL, "", 1, ps)
123 1.1 riastrad *
124 1.1 riastrad * In this case, the values of the parameters pc8 and n are
125 1.1 riastrad * ignored.'
126 1.1 riastrad */
127 1.1 riastrad if (s == NULL) {
128 1.1 riastrad pc8 = NULL;
129 1.1 riastrad s = "";
130 1.1 riastrad n = 1;
131 1.1 riastrad }
132 1.1 riastrad
133 1.1 riastrad /*
134 1.1 riastrad * Get the private conversion state.
135 1.1 riastrad */
136 1.1 riastrad S = (struct mbrtoc8state *)ps;
137 1.1 riastrad
138 1.1 riastrad /*
139 1.1 riastrad * If there are pending trailing bytes, yield them and return
140 1.1 riastrad * (size_t)-3 to indicate that no bytes of input were consumed.
141 1.1 riastrad */
142 1.1 riastrad if (S->nleft) {
143 1.1 riastrad if (pc8)
144 1.1 riastrad *pc8 = S->buf[sizeof(S->buf) - S->nleft];
145 1.1 riastrad S->buf[sizeof(S->buf) - S->nleft] = 0; /* paranoia */
146 1.1 riastrad S->nleft--;
147 1.1 riastrad return (size_t)-3;
148 1.1 riastrad }
149 1.1 riastrad
150 1.1 riastrad /*
151 1.1 riastrad * Consume the next scalar value. If no full scalar value can
152 1.1 riastrad * be obtained, stop here.
153 1.1 riastrad */
154 1.1 riastrad len = mbrtoc32(&c32, s, n, &S->mbs);
155 1.1 riastrad switch (len) {
156 1.1 riastrad case 0: /* NUL */
157 1.1 riastrad if (pc8)
158 1.1 riastrad *pc8 = 0;
159 1.1 riastrad return 0;
160 1.1 riastrad case (size_t)-2: /* still incomplete after n bytes */
161 1.1 riastrad case (size_t)-1: /* error */
162 1.1 riastrad return len;
163 1.1 riastrad default: /* consumed len bytes of input */
164 1.1 riastrad break;
165 1.1 riastrad }
166 1.1 riastrad
167 1.1 riastrad /*
168 1.1 riastrad * We consumed a scalar value from the input.
169 1.1 riastrad *
170 1.1 riastrad * Encode it as UTF-8, yield the leading byte, and buffer the
171 1.1 riastrad * trailing bytes to yield later.
172 1.1 riastrad *
173 1.1 riastrad * Table 3-6: UTF-8 Bit Distribution
174 1.1 riastrad * Table 3-7: Well-Formed UTF-8 Byte Sequences
175 1.1 riastrad */
176 1.1 riastrad switch (c32) {
177 1.1 riastrad case 0x00 ... 0x7f:
178 1.1 riastrad if (pc8)
179 1.1 riastrad *pc8 = c32;
180 1.1 riastrad _DIAGASSERT(S->nleft == 0);
181 1.1 riastrad break;
182 1.1 riastrad case 0x0080 ... 0x07ff:
183 1.1 riastrad if (pc8)
184 1.1 riastrad *pc8 = 0xc0 | __SHIFTOUT(c32, __BITS(10,6));
185 1.1 riastrad S->buf[2] = 0x80 | __SHIFTOUT(c32, __BITS(5,0));
186 1.1 riastrad S->nleft = 1;
187 1.1 riastrad break;
188 1.1 riastrad case 0x0800 ... 0xffff:
189 1.1 riastrad if (pc8)
190 1.1 riastrad *pc8 = 0xe0 | __SHIFTOUT(c32, __BITS(15,12));
191 1.1 riastrad S->buf[1] = 0x80 | __SHIFTOUT(c32, __BITS(11,6));
192 1.1 riastrad S->buf[2] = 0x80 | __SHIFTOUT(c32, __BITS(5,0));
193 1.1 riastrad S->nleft = 2;
194 1.1 riastrad break;
195 1.1 riastrad case 0x10000 ... 0x10ffff:
196 1.1 riastrad if (pc8)
197 1.1 riastrad *pc8 = 0xf0 | __SHIFTOUT(c32, __BITS(20,18));
198 1.1 riastrad S->buf[0] = 0x80 | __SHIFTOUT(c32, __BITS(17,12));
199 1.1 riastrad S->buf[1] = 0x80 | __SHIFTOUT(c32, __BITS(11,6));
200 1.1 riastrad S->buf[2] = 0x80 | __SHIFTOUT(c32, __BITS(5,0));
201 1.1 riastrad S->nleft = 3;
202 1.1 riastrad break;
203 1.1 riastrad default:
204 1.1 riastrad errno = EILSEQ;
205 1.1 riastrad return (size_t)-1;
206 1.1 riastrad }
207 1.1 riastrad
208 1.1 riastrad /*
209 1.1 riastrad * Return the number of bytes consumed from the input.
210 1.1 riastrad */
211 1.1 riastrad return len;
212 1.1 riastrad }
213