c16rtomb.c revision 1.7 1 1.7 rillig /* $NetBSD: c16rtomb.c,v 1.7 2024/08/21 18:36:11 rillig 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 * c16rtomb(s, c16, ps)
31 1.1 riastrad *
32 1.1 riastrad * Encode the Unicode UTF-16 code unit c16, which may be surrogate
33 1.1 riastrad * code point, into the multibyte buffer s under the current
34 1.1 riastrad * locale, using multibyte encoding state ps.
35 1.1 riastrad *
36 1.1 riastrad * If c16 is a high surrogate, no output will be produced, but c16
37 1.1 riastrad * will be remembered; this must be followed by another call
38 1.1 riastrad * passing the trailing low surrogate.
39 1.1 riastrad *
40 1.1 riastrad * If c16 is a low surrogate, it must have been preceded by a call
41 1.1 riastrad * with the leading high surrogate; at this point the combined
42 1.1 riastrad * scalar value will be produced as output.
43 1.1 riastrad *
44 1.1 riastrad * Return the number of bytes stored on success, or (size_t)-1 on
45 1.1 riastrad * error with errno set to EILSEQ.
46 1.1 riastrad *
47 1.1 riastrad * At most MB_CUR_MAX bytes will be stored.
48 1.1 riastrad *
49 1.1 riastrad * References:
50 1.1 riastrad *
51 1.1 riastrad * The Unicode Standard, Version 15.0 -- Core Specification, The
52 1.7 rillig * Unicode Consortium, Sec. 3.8 `Surrogates', p. 118.
53 1.1 riastrad * https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=144
54 1.1 riastrad * https://web.archive.org/web/20240718101254/https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=144
55 1.1 riastrad *
56 1.1 riastrad * The Unicode Standard, Version 15.0 -- Core Specification, The
57 1.1 riastrad * Unicode Consortium, Sec. 3.9 `Unicode Encoding Forms': UTF-16,
58 1.1 riastrad * p. 124.
59 1.1 riastrad * https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=150
60 1.1 riastrad * https://web.archive.org/web/20240718101254/https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=150
61 1.1 riastrad *
62 1.1 riastrad * P. Hoffman and F. Yergeau, `UTF-16, an encoding of ISO 10646',
63 1.1 riastrad * RFC 2781, Internet Engineering Task Force, February 2000,
64 1.1 riastrad * Sec. 2.2: `Decoding UTF-16'.
65 1.1 riastrad * https://datatracker.ietf.org/doc/html/rfc2781#section-2.2
66 1.1 riastrad */
67 1.1 riastrad
68 1.1 riastrad #include <sys/cdefs.h>
69 1.7 rillig __RCSID("$NetBSD: c16rtomb.c,v 1.7 2024/08/21 18:36:11 rillig Exp $");
70 1.3 riastrad
71 1.3 riastrad #include "namespace.h"
72 1.1 riastrad
73 1.1 riastrad #include <assert.h>
74 1.1 riastrad #include <errno.h>
75 1.1 riastrad #include <limits.h>
76 1.5 riastrad #include <locale.h>
77 1.2 riastrad #include <stdalign.h>
78 1.1 riastrad #include <stddef.h>
79 1.1 riastrad #include <uchar.h>
80 1.1 riastrad
81 1.1 riastrad #include "c32rtomb.h"
82 1.5 riastrad #include "setlocale_local.h"
83 1.1 riastrad
84 1.1 riastrad struct c16rtombstate {
85 1.1 riastrad char16_t surrogate;
86 1.1 riastrad mbstate_t mbs;
87 1.1 riastrad };
88 1.1 riastrad __CTASSERT(offsetof(struct c16rtombstate, mbs) <= sizeof(mbstate_t));
89 1.1 riastrad __CTASSERT(sizeof(struct c32rtombstate) <= sizeof(mbstate_t) -
90 1.1 riastrad offsetof(struct c16rtombstate, mbs));
91 1.2 riastrad __CTASSERT(alignof(struct c16rtombstate) <= alignof(mbstate_t));
92 1.1 riastrad
93 1.5 riastrad #ifdef __weak_alias
94 1.5 riastrad __weak_alias(c16rtomb_l,_c16rtomb_l)
95 1.5 riastrad #endif
96 1.5 riastrad
97 1.1 riastrad size_t
98 1.1 riastrad c16rtomb(char *restrict s, char16_t c16, mbstate_t *restrict ps)
99 1.1 riastrad {
100 1.5 riastrad
101 1.5 riastrad return c16rtomb_l(s, c16, ps, _current_locale());
102 1.5 riastrad }
103 1.5 riastrad
104 1.5 riastrad size_t
105 1.5 riastrad c16rtomb_l(char *restrict s, char16_t c16, mbstate_t *restrict ps,
106 1.5 riastrad locale_t loc)
107 1.5 riastrad {
108 1.1 riastrad static mbstate_t psbuf;
109 1.1 riastrad char buf[MB_LEN_MAX];
110 1.1 riastrad struct c16rtombstate *S;
111 1.1 riastrad char32_t c32;
112 1.1 riastrad
113 1.1 riastrad /*
114 1.1 riastrad * `If ps is a null pointer, each function uses its own
115 1.1 riastrad * internal mbstate_t object instead, which is initialized at
116 1.1 riastrad * program startup to the initial conversion state; the
117 1.1 riastrad * functions are not required to avoid data races with other
118 1.1 riastrad * calls to the same function in this case. The
119 1.1 riastrad * implementation behaves as if no library function calls
120 1.1 riastrad * these functions with a null pointer for ps.'
121 1.1 riastrad */
122 1.1 riastrad if (ps == NULL)
123 1.1 riastrad ps = &psbuf;
124 1.1 riastrad
125 1.1 riastrad /*
126 1.1 riastrad * `If s is a null pointer, the c16rtomb function is equivalent
127 1.1 riastrad * to the call
128 1.1 riastrad *
129 1.1 riastrad * c16rtomb(buf, L'\0', ps)
130 1.1 riastrad *
131 1.1 riastrad * where buf is an internal buffer.
132 1.1 riastrad */
133 1.1 riastrad if (s == NULL) {
134 1.1 riastrad s = buf;
135 1.1 riastrad c16 = L'\0';
136 1.1 riastrad }
137 1.1 riastrad
138 1.1 riastrad /*
139 1.1 riastrad * Open the private UTF-16 decoding state.
140 1.1 riastrad */
141 1.4 christos S = (struct c16rtombstate *)(void *)ps;
142 1.1 riastrad
143 1.1 riastrad /*
144 1.6 riastrad * Handle several cases:
145 1.1 riastrad *
146 1.6 riastrad * 1. c16 is null.
147 1.6 riastrad * 2. Pending high surrogate.
148 1.6 riastrad * 3. No pending high surrogate and c16 is a high surrogate.
149 1.6 riastrad * 4. No pending high surrogate and c16 is a low surrogate.
150 1.6 riastrad * 5. No pending high surrogate and c16 is a BMP scalar value.
151 1.6 riastrad */
152 1.6 riastrad if (c16 == L'\0') { /* 1. null */
153 1.6 riastrad /*
154 1.6 riastrad * `If c16 is a null wide character, a null byte is
155 1.6 riastrad * stored, preceded by any shift sequence needed to
156 1.6 riastrad * restore the initial shift state; the resulting
157 1.6 riastrad * state described is the initial conversion state.'
158 1.6 riastrad *
159 1.6 riastrad * So if c16 is null, discard any pending high
160 1.6 riastrad * surrogate -- there's nothing we can legitimately do
161 1.6 riastrad * with it -- and convert a null scalar value, which by
162 1.6 riastrad * definition of c32rtomb writes out any shift sequence
163 1.6 riastrad * reset followed by a null byte.
164 1.6 riastrad */
165 1.1 riastrad S->surrogate = 0;
166 1.6 riastrad c32 = 0;
167 1.6 riastrad } else if (S->surrogate != 0) { /* 2. pending high surrogate */
168 1.6 riastrad /*
169 1.6 riastrad * If the previous code unit was a high surrogate, the
170 1.6 riastrad * next code unit must be a low surrogate. Reject it
171 1.6 riastrad * if not; otherwise clear the high surrogate for next
172 1.6 riastrad * time and combine them to output a scalar value.
173 1.6 riastrad */
174 1.1 riastrad if (c16 < 0xdc00 || c16 > 0xdfff) {
175 1.1 riastrad errno = EILSEQ;
176 1.1 riastrad return (size_t)-1;
177 1.1 riastrad }
178 1.1 riastrad const char16_t w1 = S->surrogate;
179 1.1 riastrad const char16_t w2 = c16;
180 1.4 christos c32 = (char32_t)(
181 1.4 christos __SHIFTIN(__SHIFTOUT(w1, __BITS(9,0)), __BITS(19,10)) |
182 1.4 christos __SHIFTIN(__SHIFTOUT(w2, __BITS(9,0)), __BITS(9,0)));
183 1.1 riastrad c32 += 0x10000;
184 1.1 riastrad S->surrogate = 0;
185 1.6 riastrad } else if (c16 >= 0xd800 && c16 <= 0xdbff) { /* 3. high surrogate */
186 1.6 riastrad /*
187 1.6 riastrad * No pending high surrogate and this code unit is a
188 1.6 riastrad * high surrogate. Save it for next time, and output
189 1.6 riastrad * nothing -- we don't yet know what the next scalar
190 1.6 riastrad * value will be until we receive the low surrogate.
191 1.6 riastrad */
192 1.1 riastrad S->surrogate = c16;
193 1.1 riastrad return 0; /* produced nothing */
194 1.6 riastrad } else if (c16 >= 0xdc00 && c16 <= 0xdfff) { /* 4. low surrogate */
195 1.6 riastrad /*
196 1.6 riastrad * No pending high surrogate and this code unit is a
197 1.6 riastrad * low surrogate. That's invalid; fail with EILSEQ.
198 1.6 riastrad */
199 1.1 riastrad errno = EILSEQ;
200 1.1 riastrad return (size_t)-1;
201 1.6 riastrad } else { /* 5. not a surrogate */
202 1.6 riastrad /*
203 1.6 riastrad * Code unit is a scalar value in the BMP. Just output
204 1.6 riastrad * it as is.
205 1.6 riastrad */
206 1.1 riastrad c32 = c16;
207 1.1 riastrad }
208 1.1 riastrad
209 1.1 riastrad /*
210 1.1 riastrad * We have a scalar value. Output it.
211 1.1 riastrad */
212 1.5 riastrad return c32rtomb_l(s, c32, &S->mbs, loc);
213 1.1 riastrad }
214