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