1 1.8 rillig /* $NetBSD: mbrtoc8.c,v 1.8 2024/08/21 18:36:11 rillig 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.5 riastrad * mbrtoc8(&c8, 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-8 representation of that 35 1.1 riastrad * scalar value at c8. 36 1.1 riastrad * 37 1.1 riastrad * If the UTF-8 representation of that scalar value is multiple 38 1.7 rillig * bytes long, mbrtoc8 will yield the leading byte in one call that 39 1.1 riastrad * consumes input, and will yield the trailing bytes in subsequent 40 1.1 riastrad * calls without consuming any input and returning (size_t)-3 41 1.1 riastrad * 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 a trailing byte was returned without consuming 47 1.1 riastrad * 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 mbrtoc8 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.8 rillig * Unicode Consortium, Sec. 3.8 `Surrogates', p. 118. 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.7 rillig * Unicode Consortium, Sec. 3.9 `Unicode Encoding Forms': UTF-8, 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 * F. Yergeau, `UTF-8, a transformation format of ISO 10646', 70 1.1 riastrad * RFC 3629, Internet Engineering Task Force, November 2003. 71 1.1 riastrad * https://datatracker.ietf.org/doc/html/rfc3629 72 1.1 riastrad */ 73 1.1 riastrad 74 1.1 riastrad #include <sys/cdefs.h> 75 1.8 rillig __RCSID("$NetBSD: mbrtoc8.c,v 1.8 2024/08/21 18:36:11 rillig Exp $"); 76 1.2 riastrad 77 1.2 riastrad #include "namespace.h" 78 1.1 riastrad 79 1.1 riastrad #include <assert.h> 80 1.1 riastrad #include <errno.h> 81 1.4 riastrad #include <locale.h> 82 1.1 riastrad #include <stdalign.h> 83 1.1 riastrad #include <stddef.h> 84 1.1 riastrad #include <uchar.h> 85 1.1 riastrad 86 1.1 riastrad #include "mbrtoc32.h" 87 1.4 riastrad #include "setlocale_local.h" 88 1.1 riastrad 89 1.1 riastrad struct mbrtoc8state { 90 1.1 riastrad char8_t nleft; 91 1.1 riastrad char8_t buf[3]; 92 1.1 riastrad mbstate_t mbs; 93 1.1 riastrad }; 94 1.1 riastrad __CTASSERT(offsetof(struct mbrtoc8state, mbs) <= sizeof(mbstate_t)); 95 1.1 riastrad __CTASSERT(sizeof(struct mbrtoc32state) <= sizeof(mbstate_t) - 96 1.1 riastrad offsetof(struct mbrtoc8state, mbs)); 97 1.1 riastrad __CTASSERT(alignof(struct mbrtoc8state) <= alignof(mbstate_t)); 98 1.1 riastrad 99 1.4 riastrad #ifdef __weak_alias 100 1.4 riastrad __weak_alias(mbrtoc8_l,_mbrtoc8_l) 101 1.4 riastrad #endif 102 1.4 riastrad 103 1.1 riastrad size_t 104 1.1 riastrad mbrtoc8(char8_t *restrict pc8, const char *restrict s, size_t n, 105 1.1 riastrad mbstate_t *restrict ps) 106 1.1 riastrad { 107 1.4 riastrad 108 1.4 riastrad return mbrtoc8_l(pc8, s, n, ps, _current_locale()); 109 1.4 riastrad } 110 1.4 riastrad 111 1.4 riastrad size_t 112 1.4 riastrad mbrtoc8_l(char8_t *restrict pc8, const char *restrict s, size_t n, 113 1.4 riastrad mbstate_t *restrict ps, locale_t restrict loc) 114 1.4 riastrad { 115 1.1 riastrad static mbstate_t psbuf; 116 1.1 riastrad struct mbrtoc8state *S; 117 1.1 riastrad char32_t c32; 118 1.1 riastrad size_t len; 119 1.1 riastrad 120 1.1 riastrad /* 121 1.1 riastrad * `If ps is a null pointer, each function uses its own 122 1.1 riastrad * internal mbstate_t object instead, which is initialized at 123 1.1 riastrad * program startup to the initial conversion state; the 124 1.1 riastrad * functions are not required to avoid data races with other 125 1.1 riastrad * calls to the same function in this case. The 126 1.1 riastrad * implementation behaves as if no library function calls 127 1.1 riastrad * these functions with a null pointer for ps.' 128 1.1 riastrad */ 129 1.1 riastrad if (ps == NULL) 130 1.1 riastrad ps = &psbuf; 131 1.1 riastrad 132 1.1 riastrad /* 133 1.1 riastrad * `If s is a null pointer, the mbrtoc8 function is equivalent 134 1.1 riastrad * to the call: 135 1.1 riastrad * 136 1.1 riastrad * mbrtoc8(NULL, "", 1, ps) 137 1.1 riastrad * 138 1.1 riastrad * In this case, the values of the parameters pc8 and n are 139 1.1 riastrad * ignored.' 140 1.1 riastrad */ 141 1.1 riastrad if (s == NULL) { 142 1.1 riastrad pc8 = NULL; 143 1.1 riastrad s = ""; 144 1.1 riastrad n = 1; 145 1.1 riastrad } 146 1.1 riastrad 147 1.1 riastrad /* 148 1.1 riastrad * Get the private conversion state. 149 1.1 riastrad */ 150 1.3 christos S = (struct mbrtoc8state *)(void *)ps; 151 1.1 riastrad 152 1.1 riastrad /* 153 1.1 riastrad * If there are pending trailing bytes, yield them and return 154 1.1 riastrad * (size_t)-3 to indicate that no bytes of input were consumed. 155 1.1 riastrad */ 156 1.1 riastrad if (S->nleft) { 157 1.1 riastrad if (pc8) 158 1.1 riastrad *pc8 = S->buf[sizeof(S->buf) - S->nleft]; 159 1.1 riastrad S->buf[sizeof(S->buf) - S->nleft] = 0; /* paranoia */ 160 1.1 riastrad S->nleft--; 161 1.1 riastrad return (size_t)-3; 162 1.1 riastrad } 163 1.1 riastrad 164 1.1 riastrad /* 165 1.1 riastrad * Consume the next scalar value. If no full scalar value can 166 1.1 riastrad * be obtained, stop here. 167 1.1 riastrad */ 168 1.4 riastrad len = mbrtoc32_l(&c32, s, n, &S->mbs, loc); 169 1.1 riastrad switch (len) { 170 1.1 riastrad case 0: /* NUL */ 171 1.1 riastrad if (pc8) 172 1.1 riastrad *pc8 = 0; 173 1.1 riastrad return 0; 174 1.1 riastrad case (size_t)-2: /* still incomplete after n bytes */ 175 1.1 riastrad case (size_t)-1: /* error */ 176 1.1 riastrad return len; 177 1.1 riastrad default: /* consumed len bytes of input */ 178 1.1 riastrad break; 179 1.1 riastrad } 180 1.1 riastrad 181 1.1 riastrad /* 182 1.1 riastrad * We consumed a scalar value from the input. 183 1.1 riastrad * 184 1.1 riastrad * Encode it as UTF-8, yield the leading byte, and buffer the 185 1.1 riastrad * trailing bytes to yield later. 186 1.1 riastrad * 187 1.1 riastrad * Table 3-6: UTF-8 Bit Distribution 188 1.1 riastrad * Table 3-7: Well-Formed UTF-8 Byte Sequences 189 1.1 riastrad */ 190 1.1 riastrad switch (c32) { 191 1.1 riastrad case 0x00 ... 0x7f: 192 1.1 riastrad if (pc8) 193 1.1 riastrad *pc8 = c32; 194 1.1 riastrad _DIAGASSERT(S->nleft == 0); 195 1.1 riastrad break; 196 1.1 riastrad case 0x0080 ... 0x07ff: 197 1.1 riastrad if (pc8) 198 1.6 rillig *pc8 = 0xc0 | __SHIFTOUT(c32, __BITS(10,6)); 199 1.6 rillig S->buf[2] = 0x80 | __SHIFTOUT(c32, __BITS(5,0)); 200 1.1 riastrad S->nleft = 1; 201 1.1 riastrad break; 202 1.1 riastrad case 0x0800 ... 0xffff: 203 1.1 riastrad if (pc8) 204 1.6 rillig *pc8 = 0xe0 | __SHIFTOUT(c32, __BITS(15,12)); 205 1.6 rillig S->buf[1] = 0x80 | __SHIFTOUT(c32, __BITS(11,6)); 206 1.6 rillig S->buf[2] = 0x80 | __SHIFTOUT(c32, __BITS(5,0)); 207 1.1 riastrad S->nleft = 2; 208 1.1 riastrad break; 209 1.1 riastrad case 0x10000 ... 0x10ffff: 210 1.1 riastrad if (pc8) 211 1.6 rillig *pc8 = 0xf0 | __SHIFTOUT(c32, __BITS(20,18)); 212 1.6 rillig S->buf[0] = 0x80 | __SHIFTOUT(c32, __BITS(17,12)); 213 1.6 rillig S->buf[1] = 0x80 | __SHIFTOUT(c32, __BITS(11,6)); 214 1.6 rillig S->buf[2] = 0x80 | __SHIFTOUT(c32, __BITS(5,0)); 215 1.1 riastrad S->nleft = 3; 216 1.1 riastrad break; 217 1.1 riastrad default: 218 1.1 riastrad errno = EILSEQ; 219 1.1 riastrad return (size_t)-1; 220 1.1 riastrad } 221 1.1 riastrad 222 1.1 riastrad /* 223 1.1 riastrad * Return the number of bytes consumed from the input. 224 1.1 riastrad */ 225 1.1 riastrad return len; 226 1.1 riastrad } 227