Home | History | Annotate | Line # | Download | only in locale
      1  1.7    rillig /*	$NetBSD: mbrtoc16.c,v 1.7 2024/08/18 20:06:05 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.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.7    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.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.7    rillig __RCSID("$NetBSD: mbrtoc16.c,v 1.7 2024/08/18 20:06:05 rillig Exp $");
     77  1.4  riastrad 
     78  1.4  riastrad #include "namespace.h"
     79  1.1  riastrad 
     80  1.1  riastrad #include <assert.h>
     81  1.1  riastrad #include <errno.h>
     82  1.6  riastrad #include <locale.h>
     83  1.2  riastrad #include <stdalign.h>
     84  1.1  riastrad #include <stddef.h>
     85  1.1  riastrad #include <uchar.h>
     86  1.1  riastrad 
     87  1.1  riastrad #include "mbrtoc32.h"
     88  1.6  riastrad #include "setlocale_local.h"
     89  1.1  riastrad 
     90  1.1  riastrad struct mbrtoc16state {
     91  1.1  riastrad 	char16_t	surrogate;
     92  1.1  riastrad 	mbstate_t	mbs;
     93  1.1  riastrad };
     94  1.1  riastrad __CTASSERT(offsetof(struct mbrtoc16state, mbs) <= sizeof(mbstate_t));
     95  1.1  riastrad __CTASSERT(sizeof(struct mbrtoc32state) <= sizeof(mbstate_t) -
     96  1.1  riastrad     offsetof(struct mbrtoc16state, mbs));
     97  1.2  riastrad __CTASSERT(alignof(struct mbrtoc16state) <= alignof(mbstate_t));
     98  1.1  riastrad 
     99  1.6  riastrad #ifdef __weak_alias
    100  1.6  riastrad __weak_alias(mbrtoc16_l,_mbrtoc16_l)
    101  1.6  riastrad #endif
    102  1.6  riastrad 
    103  1.1  riastrad size_t
    104  1.1  riastrad mbrtoc16(char16_t *restrict pc16, const char *restrict s, size_t n,
    105  1.1  riastrad     mbstate_t *restrict ps)
    106  1.1  riastrad {
    107  1.6  riastrad 
    108  1.6  riastrad 	return mbrtoc16_l(pc16, s, n, ps, _current_locale());
    109  1.6  riastrad }
    110  1.6  riastrad 
    111  1.6  riastrad size_t
    112  1.6  riastrad mbrtoc16_l(char16_t *restrict pc16, const char *restrict s, size_t n,
    113  1.6  riastrad     mbstate_t *restrict ps, locale_t restrict loc)
    114  1.6  riastrad {
    115  1.1  riastrad 	static mbstate_t psbuf;
    116  1.1  riastrad 	struct mbrtoc16state *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 mbrtoc16 function is equivalent
    134  1.1  riastrad 	 *  to the call:
    135  1.1  riastrad 	 *
    136  1.1  riastrad 	 *	mbrtoc16(NULL, "", 1, ps)
    137  1.1  riastrad 	 *
    138  1.1  riastrad 	 *  In this case, the values of the parameters pc16 and n are
    139  1.1  riastrad 	 *  ignored.'
    140  1.1  riastrad 	 */
    141  1.1  riastrad 	if (s == NULL) {
    142  1.1  riastrad 		pc16 = 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.5  christos 	S = (struct mbrtoc16state *)(void *)ps;
    151  1.1  riastrad 
    152  1.1  riastrad 	/*
    153  1.3  riastrad 	 * If there is a pending surrogate, yield it and consume no
    154  1.1  riastrad 	 * bytes of the input, returning (size_t)-3 to indicate that no
    155  1.1  riastrad 	 * bytes of input were consumed.
    156  1.1  riastrad 	 */
    157  1.3  riastrad 	if (S->surrogate != 0) {
    158  1.3  riastrad 		_DIAGASSERT(S->surrogate >= 0xdc00);
    159  1.3  riastrad 		_DIAGASSERT(S->surrogate <= 0xdfff);
    160  1.1  riastrad 		if (pc16)
    161  1.1  riastrad 			*pc16 = S->surrogate;
    162  1.1  riastrad 		S->surrogate = 0;
    163  1.1  riastrad 		return (size_t)-3;
    164  1.1  riastrad 	}
    165  1.1  riastrad 
    166  1.1  riastrad 	/*
    167  1.1  riastrad 	 * Consume the next scalar value.  If no full scalar value can
    168  1.1  riastrad 	 * be obtained, stop here.
    169  1.1  riastrad 	 */
    170  1.6  riastrad 	len = mbrtoc32_l(&c32, s, n, &S->mbs, loc);
    171  1.1  riastrad 	switch (len) {
    172  1.1  riastrad 	case 0:			/* NUL */
    173  1.1  riastrad 		if (pc16)
    174  1.1  riastrad 			*pc16 = 0;
    175  1.1  riastrad 		return 0;
    176  1.1  riastrad 	case (size_t)-2:	/* still incomplete after n bytes */
    177  1.1  riastrad 	case (size_t)-1:	/* error */
    178  1.1  riastrad 		return len;
    179  1.1  riastrad 	default:		/* consumed len bytes of input */
    180  1.1  riastrad 		break;
    181  1.1  riastrad 	}
    182  1.1  riastrad 
    183  1.1  riastrad 	/*
    184  1.1  riastrad 	 * We consumed a scalar value from the input.
    185  1.1  riastrad 	 *
    186  1.1  riastrad 	 * If it's inside the Basic Multilingual Plane (16-bit scalar
    187  1.1  riastrad 	 * values), return it.
    188  1.1  riastrad 	 *
    189  1.1  riastrad 	 * If it's outside the Basic Multilingual Plane, split it into
    190  1.1  riastrad 	 * high and low surrogate code points, return the high, and
    191  1.1  riastrad 	 * save the low.
    192  1.1  riastrad 	 */
    193  1.1  riastrad 	if (c32 <= 0xffff) {
    194  1.1  riastrad 		if (pc16)
    195  1.1  riastrad 			*pc16 = c32;
    196  1.1  riastrad 		_DIAGASSERT(S->surrogate == 0);
    197  1.1  riastrad 	} else {
    198  1.1  riastrad 		c32 -= 0x10000;
    199  1.7    rillig 		const char16_t w1 = 0xd800 | __SHIFTOUT(c32, __BITS(19,10));
    200  1.7    rillig 		const char16_t w2 = 0xdc00 | __SHIFTOUT(c32, __BITS(9,0));
    201  1.1  riastrad 		if (pc16)
    202  1.1  riastrad 			*pc16 = w1;
    203  1.1  riastrad 		S->surrogate = w2;
    204  1.1  riastrad 		_DIAGASSERT(S->surrogate != 0);
    205  1.3  riastrad 		_DIAGASSERT(S->surrogate >= 0xdc00);
    206  1.3  riastrad 		_DIAGASSERT(S->surrogate <= 0xdfff);
    207  1.1  riastrad 	}
    208  1.1  riastrad 
    209  1.1  riastrad 	/*
    210  1.1  riastrad 	 * Return the number of bytes consumed from the input.
    211  1.1  riastrad 	 */
    212  1.1  riastrad 	return len;
    213  1.1  riastrad }
    214