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