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