c16rtomb.c revision 1.3 1 /* $NetBSD: c16rtomb.c,v 1.3 2024/08/15 22:22:35 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.3 2024/08/15 22:22:35 riastradh Exp $");
70
71 #include "namespace.h"
72
73 #include <assert.h>
74 #include <errno.h>
75 #include <limits.h>
76 #include <stdalign.h>
77 #include <stddef.h>
78 #include <uchar.h>
79
80 #include "c32rtomb.h"
81
82 struct c16rtombstate {
83 char16_t surrogate;
84 mbstate_t mbs;
85 };
86 __CTASSERT(offsetof(struct c16rtombstate, mbs) <= sizeof(mbstate_t));
87 __CTASSERT(sizeof(struct c32rtombstate) <= sizeof(mbstate_t) -
88 offsetof(struct c16rtombstate, mbs));
89 __CTASSERT(alignof(struct c16rtombstate) <= alignof(mbstate_t));
90
91 size_t
92 c16rtomb(char *restrict s, char16_t c16, mbstate_t *restrict ps)
93 {
94 static mbstate_t psbuf;
95 char buf[MB_LEN_MAX];
96 struct c16rtombstate *S;
97 char32_t c32;
98
99 /*
100 * `If ps is a null pointer, each function uses its own
101 * internal mbstate_t object instead, which is initialized at
102 * program startup to the initial conversion state; the
103 * functions are not required to avoid data races with other
104 * calls to the same function in this case. The
105 * implementation behaves as if no library function calls
106 * these functions with a null pointer for ps.'
107 */
108 if (ps == NULL)
109 ps = &psbuf;
110
111 /*
112 * `If s is a null pointer, the c16rtomb function is equivalent
113 * to the call
114 *
115 * c16rtomb(buf, L'\0', ps)
116 *
117 * where buf is an internal buffer.
118 */
119 if (s == NULL) {
120 s = buf;
121 c16 = L'\0';
122 }
123
124 /*
125 * Open the private UTF-16 decoding state.
126 */
127 S = (struct c16rtombstate *)ps;
128
129 #if 0
130 /*
131 * `If c16 is a null wide character, a null byte is stored,
132 * preceded by any shift sequence needed to restore the
133 * initial shift state; the resulting state described is the
134 * initial conversion state.'
135 *
136 * XXX But what else gets stored? Do we just discard any
137 * pending high surrogate, or do we convert it to something
138 * else, or what?
139 */
140 if (c16 == L'\0') {
141 S->surrogate = 0;
142 }
143 #endif
144
145 /*
146 * Check whether:
147 *
148 * 1. We had previously decoded a high surrogate.
149 * => Decode the low surrogate -- reject if it's not a low
150 * surrogate -- and combine them to output a scalar
151 * value; clear the high surrogate for next time.
152 * 2. This is a high surrogate.
153 * => Save it and wait for the low surrogate with no output.
154 * 3. This is a low surrogate.
155 * => Reject.
156 * 4. This is not a surrogate.
157 * => Output a scalar value.
158 */
159 if (S->surrogate != 0) { /* 1. pending surrogate pair */
160 if (c16 < 0xdc00 || c16 > 0xdfff) {
161 errno = EILSEQ;
162 return (size_t)-1;
163 }
164 const char16_t w1 = S->surrogate;
165 const char16_t w2 = c16;
166 c32 = __SHIFTIN(__SHIFTOUT(w1, __BITS(9,0)), __BITS(19,10)) |
167 __SHIFTIN(__SHIFTOUT(w2, __BITS(9,0)), __BITS(9,0));
168 c32 += 0x10000;
169 S->surrogate = 0;
170 } else if (c16 >= 0xd800 && c16 <= 0xdbff) { /* 2. high surrogate */
171 S->surrogate = c16;
172 return 0; /* produced nothing */
173 } else if (c16 >= 0xdc00 && c16 <= 0xdfff) { /* 3. low surrogate */
174 errno = EILSEQ;
175 return (size_t)-1;
176 } else { /* 4. not a surrogate */
177 c32 = c16;
178 }
179
180 /*
181 * We have a scalar value. Output it.
182 */
183 return c32rtomb(s, c32, &S->mbs);
184 }
185