Home | History | Annotate | Line # | Download | only in locale
mbrtoc8.c revision 1.3
      1 /*	$NetBSD: mbrtoc8.c,v 1.3 2024/08/17 20:08:13 christos 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  * mbrtoc16(&c16, s, n, ps)
     31  *
     32  *	Decode a Unicode scalar value from up to n bytes out of the
     33  *	multibyte string s, using multibyte encoding state ps, and
     34  *	store the next code unit in the UTF-8 representation of that
     35  *	scalar value at c8.
     36  *
     37  *	If the UTF-8 representation of that scalar value is multiple
     38  *	bytes long, mbrtoc8 will yield leading byte in one call that
     39  *	consumes input, and will yield the trailing bytes in subsequent
     40  *	calls without consuming any input and returning (size_t)-3
     41  *	instead.
     42  *
     43  *	Return the number of bytes consumed on success, or:
     44  *
     45  *	- 0 if the code unit is NUL, or
     46  *	- (size_t)-3 if a trailing byte was returned without consuming
     47  *	  any additional input, or
     48  *	- (size_t)-2 if the input is incomplete, or
     49  *	- (size_t)-1 on error with errno set to EILSEQ.
     50  *
     51  *	In the case of incomplete input, the decoding state so far
     52  *	after processing s[0], s[1], ..., s[n - 1] is saved in ps, so
     53  *	subsequent calls to mbrtoc8 will pick up n bytes later into
     54  *	the input stream.
     55  *
     56  * References:
     57  *
     58  *	The Unicode Standard, Version 15.0 -- Core Specification, The
     59  *	Unicode Consortium, Sec. 3.8 `Surrogates', p. 119.
     60  *	https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=144
     61  *	https://web.archive.org/web/20240718101254/https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=144
     62  *
     63  *	The Unicode Standard, Version 15.0 -- Core Specification, The
     64  *	Unicode Consortium, Sec. 3.9 `Unicode Encoding Forms': UTF-16,
     65  *	p. 124.
     66  *	https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=150
     67  *	https://web.archive.org/web/20240718101254/https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=150
     68  *
     69  *	F. Yergeau, `UTF-8, a transformation format of ISO 10646',
     70  *	RFC 3629, Internet Engineering Task Force, November 2003.
     71  *	https://datatracker.ietf.org/doc/html/rfc3629
     72  */
     73 
     74 #include <sys/cdefs.h>
     75 __RCSID("$NetBSD: mbrtoc8.c,v 1.3 2024/08/17 20:08:13 christos Exp $");
     76 
     77 #include "namespace.h"
     78 
     79 #include <assert.h>
     80 #include <errno.h>
     81 #include <stdalign.h>
     82 #include <stddef.h>
     83 #include <uchar.h>
     84 
     85 #include "mbrtoc32.h"
     86 
     87 struct mbrtoc8state {
     88 	char8_t		nleft;
     89 	char8_t		buf[3];
     90 	mbstate_t	mbs;
     91 };
     92 __CTASSERT(offsetof(struct mbrtoc8state, mbs) <= sizeof(mbstate_t));
     93 __CTASSERT(sizeof(struct mbrtoc32state) <= sizeof(mbstate_t) -
     94     offsetof(struct mbrtoc8state, mbs));
     95 __CTASSERT(alignof(struct mbrtoc8state) <= alignof(mbstate_t));
     96 
     97 size_t
     98 mbrtoc8(char8_t *restrict pc8, const char *restrict s, size_t n,
     99     mbstate_t *restrict ps)
    100 {
    101 	static mbstate_t psbuf;
    102 	struct mbrtoc8state *S;
    103 	char32_t c32;
    104 	size_t len;
    105 
    106 	/*
    107 	 * `If ps is a null pointer, each function uses its own
    108 	 *  internal mbstate_t object instead, which is initialized at
    109 	 *  program startup to the initial conversion state; the
    110 	 *  functions are not required to avoid data races with other
    111 	 *  calls to the same function in this case.  The
    112 	 *  implementation behaves as if no library function calls
    113 	 *  these functions with a null pointer for ps.'
    114 	 */
    115 	if (ps == NULL)
    116 		ps = &psbuf;
    117 
    118 	/*
    119 	 * `If s is a null pointer, the mbrtoc8 function is equivalent
    120 	 *  to the call:
    121 	 *
    122 	 *	mbrtoc8(NULL, "", 1, ps)
    123 	 *
    124 	 *  In this case, the values of the parameters pc8 and n are
    125 	 *  ignored.'
    126 	 */
    127 	if (s == NULL) {
    128 		pc8 = NULL;
    129 		s = "";
    130 		n = 1;
    131 	}
    132 
    133 	/*
    134 	 * Get the private conversion state.
    135 	 */
    136 	S = (struct mbrtoc8state *)(void *)ps;
    137 
    138 	/*
    139 	 * If there are pending trailing bytes, yield them and return
    140 	 * (size_t)-3 to indicate that no bytes of input were consumed.
    141 	 */
    142 	if (S->nleft) {
    143 		if (pc8)
    144 			*pc8 = S->buf[sizeof(S->buf) - S->nleft];
    145 		S->buf[sizeof(S->buf) - S->nleft] = 0; /* paranoia */
    146 		S->nleft--;
    147 		return (size_t)-3;
    148 	}
    149 
    150 	/*
    151 	 * Consume the next scalar value.  If no full scalar value can
    152 	 * be obtained, stop here.
    153 	 */
    154 	len = mbrtoc32(&c32, s, n, &S->mbs);
    155 	switch (len) {
    156 	case 0:			/* NUL */
    157 		if (pc8)
    158 			*pc8 = 0;
    159 		return 0;
    160 	case (size_t)-2:	/* still incomplete after n bytes */
    161 	case (size_t)-1:	/* error */
    162 		return len;
    163 	default:		/* consumed len bytes of input */
    164 		break;
    165 	}
    166 
    167 	/*
    168 	 * We consumed a scalar value from the input.
    169 	 *
    170 	 * Encode it as UTF-8, yield the leading byte, and buffer the
    171 	 * trailing bytes to yield later.
    172 	 *
    173 	 * Table 3-6: UTF-8 Bit Distribution
    174 	 * Table 3-7: Well-Formed UTF-8 Byte Sequences
    175 	 */
    176 	switch (c32) {
    177 	case 0x00 ... 0x7f:
    178 		if (pc8)
    179 			*pc8 = c32;
    180 		_DIAGASSERT(S->nleft == 0);
    181 		break;
    182 	case 0x0080 ... 0x07ff:
    183 		if (pc8)
    184 			*pc8 = (char8_t)(
    185 			    0xc0 | __SHIFTOUT(c32, __BITS(10,6)));
    186 		S->buf[2] = (char8_t)(
    187 		    0x80 | __SHIFTOUT(c32, __BITS(5,0)));
    188 		S->nleft = 1;
    189 		break;
    190 	case 0x0800 ... 0xffff:
    191 		if (pc8)
    192 			*pc8 = (char8_t)(
    193 			    0xe0 | __SHIFTOUT(c32, __BITS(15,12)));
    194 		S->buf[1] = (char8_t)(
    195 		    0x80 | __SHIFTOUT(c32, __BITS(11,6)));
    196 		S->buf[2] = (char8_t)(
    197 		    0x80 | __SHIFTOUT(c32, __BITS(5,0)));
    198 		S->nleft = 2;
    199 		break;
    200 	case 0x10000 ... 0x10ffff:
    201 		if (pc8)
    202 			*pc8 = (char8_t)(
    203 			    0xf0 | __SHIFTOUT(c32, __BITS(20,18)));
    204 		S->buf[0] = (char8_t)(
    205 		    0x80 | __SHIFTOUT(c32, __BITS(17,12)));
    206 		S->buf[1] = (char8_t)(
    207 		    0x80 | __SHIFTOUT(c32, __BITS(11,6)));
    208 		S->buf[2] = (char8_t)(
    209 		    0x80 | __SHIFTOUT(c32, __BITS(5,0)));
    210 		S->nleft = 3;
    211 		break;
    212 	default:
    213 		errno = EILSEQ;
    214 		return (size_t)-1;
    215 	}
    216 
    217 	/*
    218 	 * Return the number of bytes consumed from the input.
    219 	 */
    220 	return len;
    221 }
    222