mbrtoc16.c revision 1.2 1 1.2 riastrad /* $NetBSD: mbrtoc16.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 * mbrtoc16(&c16, s, n, ps)
31 1.1 riastrad *
32 1.1 riastrad * Decode a Unicode scalar value from up to n bytes out of the
33 1.1 riastrad * multibyte string s, using multibyte encoding state ps, and
34 1.1 riastrad * store the next code unit in the UTF-16 representation of that
35 1.1 riastrad * scalar value at c16.
36 1.1 riastrad *
37 1.1 riastrad * If the next scalar value in s is outside the Basic Multilingual
38 1.1 riastrad * Plane, mbrtoc16 will yield the high surrogate code point in one
39 1.1 riastrad * call that consumes input, and will yield the low surrogate code
40 1.1 riastrad * point in the next call without consuming any input and
41 1.1 riastrad * returning (size_t)-3 instead.
42 1.1 riastrad *
43 1.1 riastrad * Return the number of bytes consumed on success, or:
44 1.1 riastrad *
45 1.1 riastrad * - 0 if the code unit is NUL, or
46 1.1 riastrad * - (size_t)-3 if the trailing low surrogate of a surrogate pair
47 1.1 riastrad * was returned without consuming any additional input, or
48 1.1 riastrad * - (size_t)-2 if the input is incomplete, or
49 1.1 riastrad * - (size_t)-1 on error with errno set to EILSEQ.
50 1.1 riastrad *
51 1.1 riastrad * In the case of incomplete input, the decoding state so far
52 1.1 riastrad * after processing s[0], s[1], ..., s[n - 1] is saved in ps, so
53 1.1 riastrad * subsequent calls to mbrtoc16 will pick up n bytes later into
54 1.1 riastrad * the input stream.
55 1.1 riastrad *
56 1.1 riastrad * References:
57 1.1 riastrad *
58 1.1 riastrad * The Unicode Standard, Version 15.0 -- Core Specification, The
59 1.1 riastrad * Unicode Consortium, Sec. 3.8 `Surrogates', p. 119.
60 1.1 riastrad * https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=144
61 1.1 riastrad * https://web.archive.org/web/20240718101254/https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=144
62 1.1 riastrad *
63 1.1 riastrad * The Unicode Standard, Version 15.0 -- Core Specification, The
64 1.1 riastrad * Unicode Consortium, Sec. 3.9 `Unicode Encoding Forms': UTF-16,
65 1.1 riastrad * p. 124.
66 1.1 riastrad * https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=150
67 1.1 riastrad * https://web.archive.org/web/20240718101254/https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=150
68 1.1 riastrad *
69 1.1 riastrad * P. Hoffman and F. Yergeau, `UTF-16, an encoding of ISO 10646',
70 1.1 riastrad * RFC 2781, Internet Engineering Task Force, February 2000,
71 1.1 riastrad * Sec. 2.1: `Encoding UTF-16'.
72 1.1 riastrad * https://datatracker.ietf.org/doc/html/rfc2781#section-2.1
73 1.1 riastrad */
74 1.1 riastrad
75 1.1 riastrad #include <sys/cdefs.h>
76 1.2 riastrad __RCSID("$NetBSD: mbrtoc16.c,v 1.2 2024/08/15 15:46:40 riastradh Exp $");
77 1.1 riastrad
78 1.1 riastrad #include <assert.h>
79 1.1 riastrad #include <errno.h>
80 1.2 riastrad #include <stdalign.h>
81 1.1 riastrad #include <stddef.h>
82 1.1 riastrad #include <uchar.h>
83 1.1 riastrad
84 1.1 riastrad #include "mbrtoc32.h"
85 1.1 riastrad
86 1.1 riastrad struct mbrtoc16state {
87 1.1 riastrad char16_t surrogate;
88 1.1 riastrad mbstate_t mbs;
89 1.1 riastrad };
90 1.1 riastrad __CTASSERT(offsetof(struct mbrtoc16state, mbs) <= sizeof(mbstate_t));
91 1.1 riastrad __CTASSERT(sizeof(struct mbrtoc32state) <= sizeof(mbstate_t) -
92 1.1 riastrad offsetof(struct mbrtoc16state, mbs));
93 1.2 riastrad __CTASSERT(alignof(struct mbrtoc16state) <= alignof(mbstate_t));
94 1.1 riastrad
95 1.1 riastrad size_t
96 1.1 riastrad mbrtoc16(char16_t *restrict pc16, const char *restrict s, size_t n,
97 1.1 riastrad mbstate_t *restrict ps)
98 1.1 riastrad {
99 1.1 riastrad static mbstate_t psbuf;
100 1.1 riastrad struct mbrtoc16state *S;
101 1.1 riastrad char32_t c32;
102 1.1 riastrad size_t len;
103 1.1 riastrad
104 1.1 riastrad /*
105 1.1 riastrad * `If ps is a null pointer, each function uses its own
106 1.1 riastrad * internal mbstate_t object instead, which is initialized at
107 1.1 riastrad * program startup to the initial conversion state; the
108 1.1 riastrad * functions are not required to avoid data races with other
109 1.1 riastrad * calls to the same function in this case. The
110 1.1 riastrad * implementation behaves as if no library function calls
111 1.1 riastrad * these functions with a null pointer for ps.'
112 1.1 riastrad */
113 1.1 riastrad if (ps == NULL)
114 1.1 riastrad ps = &psbuf;
115 1.1 riastrad
116 1.1 riastrad /*
117 1.1 riastrad * `If s is a null pointer, the mbrtoc16 function is equivalent
118 1.1 riastrad * to the call:
119 1.1 riastrad *
120 1.1 riastrad * mbrtoc16(NULL, "", 1, ps)
121 1.1 riastrad *
122 1.1 riastrad * In this case, the values of the parameters pc16 and n are
123 1.1 riastrad * ignored.'
124 1.1 riastrad */
125 1.1 riastrad if (s == NULL) {
126 1.1 riastrad pc16 = NULL;
127 1.1 riastrad s = "";
128 1.1 riastrad n = 1;
129 1.1 riastrad }
130 1.1 riastrad
131 1.1 riastrad /*
132 1.1 riastrad * Get the private conversion state.
133 1.1 riastrad */
134 1.1 riastrad S = (struct mbrtoc16state *)ps;
135 1.1 riastrad
136 1.1 riastrad /*
137 1.1 riastrad * If there is a pending surrogate, stash it and consume no
138 1.1 riastrad * bytes of the input, returning (size_t)-3 to indicate that no
139 1.1 riastrad * bytes of input were consumed.
140 1.1 riastrad */
141 1.1 riastrad if (S->surrogate >= 0xdc00 && S->surrogate <= 0xdfff) {
142 1.1 riastrad if (pc16)
143 1.1 riastrad *pc16 = S->surrogate;
144 1.1 riastrad S->surrogate = 0;
145 1.1 riastrad return (size_t)-3;
146 1.1 riastrad }
147 1.1 riastrad
148 1.1 riastrad /*
149 1.1 riastrad * Consume the next scalar value. If no full scalar value can
150 1.1 riastrad * be obtained, stop here.
151 1.1 riastrad */
152 1.1 riastrad len = mbrtoc32(&c32, s, n, &S->mbs);
153 1.1 riastrad switch (len) {
154 1.1 riastrad case 0: /* NUL */
155 1.1 riastrad if (pc16)
156 1.1 riastrad *pc16 = 0;
157 1.1 riastrad return 0;
158 1.1 riastrad case (size_t)-2: /* still incomplete after n bytes */
159 1.1 riastrad case (size_t)-1: /* error */
160 1.1 riastrad return len;
161 1.1 riastrad default: /* consumed len bytes of input */
162 1.1 riastrad break;
163 1.1 riastrad }
164 1.1 riastrad
165 1.1 riastrad /*
166 1.1 riastrad * We consumed a scalar value from the input.
167 1.1 riastrad *
168 1.1 riastrad * If it's inside the Basic Multilingual Plane (16-bit scalar
169 1.1 riastrad * values), return it.
170 1.1 riastrad *
171 1.1 riastrad * If it's outside the Basic Multilingual Plane, split it into
172 1.1 riastrad * high and low surrogate code points, return the high, and
173 1.1 riastrad * save the low.
174 1.1 riastrad */
175 1.1 riastrad if (c32 <= 0xffff) {
176 1.1 riastrad if (pc16)
177 1.1 riastrad *pc16 = c32;
178 1.1 riastrad _DIAGASSERT(S->surrogate == 0);
179 1.1 riastrad } else {
180 1.1 riastrad c32 -= 0x10000;
181 1.1 riastrad const char16_t w1 = 0xd800 | __SHIFTOUT(c32, __BITS(19,10));
182 1.1 riastrad const char16_t w2 = 0xdc00 | __SHIFTOUT(c32, __BITS(9,0));
183 1.1 riastrad if (pc16)
184 1.1 riastrad *pc16 = w1;
185 1.1 riastrad S->surrogate = w2;
186 1.1 riastrad _DIAGASSERT(S->surrogate != 0);
187 1.1 riastrad }
188 1.1 riastrad
189 1.1 riastrad /*
190 1.1 riastrad * Return the number of bytes consumed from the input.
191 1.1 riastrad */
192 1.1 riastrad return len;
193 1.1 riastrad }
194