c16rtomb.c revision 1.5 1 /* $NetBSD: c16rtomb.c,v 1.5 2024/08/17 21:24:53 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. 119.
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.5 2024/08/17 21:24:53 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 #if 0
144 /*
145 * `If c16 is a null wide character, a null byte is stored,
146 * preceded by any shift sequence needed to restore the
147 * initial shift state; the resulting state described is the
148 * initial conversion state.'
149 *
150 * XXX But what else gets stored? Do we just discard any
151 * pending high surrogate, or do we convert it to something
152 * else, or what?
153 */
154 if (c16 == L'\0') {
155 S->surrogate = 0;
156 }
157 #endif
158
159 /*
160 * Check whether:
161 *
162 * 1. We had previously decoded a high surrogate.
163 * => Decode the low surrogate -- reject if it's not a low
164 * surrogate -- and combine them to output a scalar
165 * value; clear the high surrogate for next time.
166 * 2. This is a high surrogate.
167 * => Save it and wait for the low surrogate with no output.
168 * 3. This is a low surrogate.
169 * => Reject.
170 * 4. This is not a surrogate.
171 * => Output a scalar value.
172 */
173 if (S->surrogate != 0) { /* 1. pending surrogate pair */
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 = (char32_t)(
181 __SHIFTIN(__SHIFTOUT(w1, __BITS(9,0)), __BITS(19,10)) |
182 __SHIFTIN(__SHIFTOUT(w2, __BITS(9,0)), __BITS(9,0)));
183 c32 += 0x10000;
184 S->surrogate = 0;
185 } else if (c16 >= 0xd800 && c16 <= 0xdbff) { /* 2. high surrogate */
186 S->surrogate = c16;
187 return 0; /* produced nothing */
188 } else if (c16 >= 0xdc00 && c16 <= 0xdfff) { /* 3. low surrogate */
189 errno = EILSEQ;
190 return (size_t)-1;
191 } else { /* 4. not a surrogate */
192 c32 = c16;
193 }
194
195 /*
196 * We have a scalar value. Output it.
197 */
198 return c32rtomb_l(s, c32, &S->mbs, loc);
199 }
200