mbrtoc8.c revision 1.1 1 1.1 riastrad /* $NetBSD: mbrtoc8.c,v 1.1 2024/08/15 21:19:45 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.1 riastrad __RCSID("$NetBSD: mbrtoc8.c,v 1.1 2024/08/15 21:19:45 riastradh Exp $");
76 1.1 riastrad
77 1.1 riastrad #include <assert.h>
78 1.1 riastrad #include <errno.h>
79 1.1 riastrad #include <stdalign.h>
80 1.1 riastrad #include <stddef.h>
81 1.1 riastrad #include <uchar.h>
82 1.1 riastrad
83 1.1 riastrad #include "mbrtoc32.h"
84 1.1 riastrad
85 1.1 riastrad struct mbrtoc8state {
86 1.1 riastrad char8_t nleft;
87 1.1 riastrad char8_t buf[3];
88 1.1 riastrad mbstate_t mbs;
89 1.1 riastrad };
90 1.1 riastrad __CTASSERT(offsetof(struct mbrtoc8state, mbs) <= sizeof(mbstate_t));
91 1.1 riastrad __CTASSERT(sizeof(struct mbrtoc32state) <= sizeof(mbstate_t) -
92 1.1 riastrad offsetof(struct mbrtoc8state, mbs));
93 1.1 riastrad __CTASSERT(alignof(struct mbrtoc8state) <= alignof(mbstate_t));
94 1.1 riastrad
95 1.1 riastrad size_t
96 1.1 riastrad mbrtoc8(char8_t *restrict pc8, const char *restrict s, size_t n,
97 1.1 riastrad mbstate_t *restrict ps)
98 1.1 riastrad {
99 1.1 riastrad static mbstate_t psbuf;
100 1.1 riastrad struct mbrtoc8state *S;
101 1.1 riastrad char32_t c32;
102 1.1 riastrad size_t len;
103 1.1 riastrad
104 1.1 riastrad /*
105 1.1 riastrad * `If ps is a null pointer, each function uses its own
106 1.1 riastrad * internal mbstate_t object instead, which is initialized at
107 1.1 riastrad * program startup to the initial conversion state; the
108 1.1 riastrad * functions are not required to avoid data races with other
109 1.1 riastrad * calls to the same function in this case. The
110 1.1 riastrad * implementation behaves as if no library function calls
111 1.1 riastrad * these functions with a null pointer for ps.'
112 1.1 riastrad */
113 1.1 riastrad if (ps == NULL)
114 1.1 riastrad ps = &psbuf;
115 1.1 riastrad
116 1.1 riastrad /*
117 1.1 riastrad * `If s is a null pointer, the mbrtoc8 function is equivalent
118 1.1 riastrad * to the call:
119 1.1 riastrad *
120 1.1 riastrad * mbrtoc8(NULL, "", 1, ps)
121 1.1 riastrad *
122 1.1 riastrad * In this case, the values of the parameters pc8 and n are
123 1.1 riastrad * ignored.'
124 1.1 riastrad */
125 1.1 riastrad if (s == NULL) {
126 1.1 riastrad pc8 = NULL;
127 1.1 riastrad s = "";
128 1.1 riastrad n = 1;
129 1.1 riastrad }
130 1.1 riastrad
131 1.1 riastrad /*
132 1.1 riastrad * Get the private conversion state.
133 1.1 riastrad */
134 1.1 riastrad S = (struct mbrtoc8state *)ps;
135 1.1 riastrad
136 1.1 riastrad /*
137 1.1 riastrad * If there are pending trailing bytes, yield them and return
138 1.1 riastrad * (size_t)-3 to indicate that no bytes of input were consumed.
139 1.1 riastrad */
140 1.1 riastrad if (S->nleft) {
141 1.1 riastrad if (pc8)
142 1.1 riastrad *pc8 = S->buf[sizeof(S->buf) - S->nleft];
143 1.1 riastrad S->buf[sizeof(S->buf) - S->nleft] = 0; /* paranoia */
144 1.1 riastrad S->nleft--;
145 1.1 riastrad return (size_t)-3;
146 1.1 riastrad }
147 1.1 riastrad
148 1.1 riastrad /*
149 1.1 riastrad * Consume the next scalar value. If no full scalar value can
150 1.1 riastrad * be obtained, stop here.
151 1.1 riastrad */
152 1.1 riastrad len = mbrtoc32(&c32, s, n, &S->mbs);
153 1.1 riastrad switch (len) {
154 1.1 riastrad case 0: /* NUL */
155 1.1 riastrad if (pc8)
156 1.1 riastrad *pc8 = 0;
157 1.1 riastrad return 0;
158 1.1 riastrad case (size_t)-2: /* still incomplete after n bytes */
159 1.1 riastrad case (size_t)-1: /* error */
160 1.1 riastrad return len;
161 1.1 riastrad default: /* consumed len bytes of input */
162 1.1 riastrad break;
163 1.1 riastrad }
164 1.1 riastrad
165 1.1 riastrad /*
166 1.1 riastrad * We consumed a scalar value from the input.
167 1.1 riastrad *
168 1.1 riastrad * Encode it as UTF-8, yield the leading byte, and buffer the
169 1.1 riastrad * trailing bytes to yield later.
170 1.1 riastrad *
171 1.1 riastrad * Table 3-6: UTF-8 Bit Distribution
172 1.1 riastrad * Table 3-7: Well-Formed UTF-8 Byte Sequences
173 1.1 riastrad */
174 1.1 riastrad switch (c32) {
175 1.1 riastrad case 0x00 ... 0x7f:
176 1.1 riastrad if (pc8)
177 1.1 riastrad *pc8 = c32;
178 1.1 riastrad _DIAGASSERT(S->nleft == 0);
179 1.1 riastrad break;
180 1.1 riastrad case 0x0080 ... 0x07ff:
181 1.1 riastrad if (pc8)
182 1.1 riastrad *pc8 = 0xc0 | __SHIFTOUT(c32, __BITS(10,6));
183 1.1 riastrad S->buf[2] = 0x80 | __SHIFTOUT(c32, __BITS(5,0));
184 1.1 riastrad S->nleft = 1;
185 1.1 riastrad break;
186 1.1 riastrad case 0x0800 ... 0xffff:
187 1.1 riastrad if (pc8)
188 1.1 riastrad *pc8 = 0xe0 | __SHIFTOUT(c32, __BITS(15,12));
189 1.1 riastrad S->buf[1] = 0x80 | __SHIFTOUT(c32, __BITS(11,6));
190 1.1 riastrad S->buf[2] = 0x80 | __SHIFTOUT(c32, __BITS(5,0));
191 1.1 riastrad S->nleft = 2;
192 1.1 riastrad break;
193 1.1 riastrad case 0x10000 ... 0x10ffff:
194 1.1 riastrad if (pc8)
195 1.1 riastrad *pc8 = 0xf0 | __SHIFTOUT(c32, __BITS(20,18));
196 1.1 riastrad S->buf[0] = 0x80 | __SHIFTOUT(c32, __BITS(17,12));
197 1.1 riastrad S->buf[1] = 0x80 | __SHIFTOUT(c32, __BITS(11,6));
198 1.1 riastrad S->buf[2] = 0x80 | __SHIFTOUT(c32, __BITS(5,0));
199 1.1 riastrad S->nleft = 3;
200 1.1 riastrad break;
201 1.1 riastrad default:
202 1.1 riastrad errno = EILSEQ;
203 1.1 riastrad return (size_t)-1;
204 1.1 riastrad }
205 1.1 riastrad
206 1.1 riastrad /*
207 1.1 riastrad * Return the number of bytes consumed from the input.
208 1.1 riastrad */
209 1.1 riastrad return len;
210 1.1 riastrad }
211