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