xref: /netbsd-src/lib/libc/locale/mbrtoc32.c (revision a280a6b73464811ef5df020e753edd09fddfad22)
1*a280a6b7Sriastradh /*	$NetBSD: mbrtoc32.c,v 1.9 2024/08/20 17:43:24 riastradh Exp $	*/
22cbd152aSriastradh 
32cbd152aSriastradh /*-
42cbd152aSriastradh  * Copyright (c) 2024 The NetBSD Foundation, Inc.
52cbd152aSriastradh  * All rights reserved.
62cbd152aSriastradh  *
72cbd152aSriastradh  * Redistribution and use in source and binary forms, with or without
82cbd152aSriastradh  * modification, are permitted provided that the following conditions
92cbd152aSriastradh  * are met:
102cbd152aSriastradh  * 1. Redistributions of source code must retain the above copyright
112cbd152aSriastradh  *    notice, this list of conditions and the following disclaimer.
122cbd152aSriastradh  * 2. Redistributions in binary form must reproduce the above copyright
132cbd152aSriastradh  *    notice, this list of conditions and the following disclaimer in the
142cbd152aSriastradh  *    documentation and/or other materials provided with the distribution.
152cbd152aSriastradh  *
162cbd152aSriastradh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
172cbd152aSriastradh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
182cbd152aSriastradh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
192cbd152aSriastradh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
202cbd152aSriastradh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
212cbd152aSriastradh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
222cbd152aSriastradh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
232cbd152aSriastradh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
242cbd152aSriastradh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
252cbd152aSriastradh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
262cbd152aSriastradh  * POSSIBILITY OF SUCH DAMAGE.
272cbd152aSriastradh  */
282cbd152aSriastradh 
292cbd152aSriastradh /*
302cbd152aSriastradh  * mbrtoc32(&c32, s, n, ps)
312cbd152aSriastradh  *
322cbd152aSriastradh  *	Decode a Unicode UTF-32 code unit from up to n bytes out of the
332cbd152aSriastradh  *	multibyte string s, and store it at c32, using multibyte
342cbd152aSriastradh  *	encoding state ps.  A UTF-32 code unit is also a Unicode scalar
352cbd152aSriastradh  *	value, which is any Unicode code point except a surrogate.
362cbd152aSriastradh  *
372cbd152aSriastradh  *	Return the number of bytes consumed on success, or 0 if the
382cbd152aSriastradh  *	code unit is NUL, or (size_t)-2 if the input is incomplete, or
392cbd152aSriastradh  *	(size_t)-1 on error with errno set to EILSEQ.
402cbd152aSriastradh  *
412cbd152aSriastradh  *	In the case of incomplete input, the decoding state so far
422cbd152aSriastradh  *	after processing s[0], s[1], ..., s[n - 1] is saved in ps, so
432cbd152aSriastradh  *	subsequent calls to mbrtoc32 will pick up n bytes later into
442cbd152aSriastradh  *	the input stream.
452cbd152aSriastradh  *
462cbd152aSriastradh  * References:
472cbd152aSriastradh  *
482cbd152aSriastradh  *	The Unicode Standard, Version 15.0 -- Core Specification, The
49c9acb578Srillig  *	Unicode Consortium, Sec. 3.8 `Surrogates', p. 118.
50c9acb578Srillig  *	https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=144
51c9acb578Srillig  *	https://web.archive.org/web/20240718101254/https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=144
522cbd152aSriastradh  */
532cbd152aSriastradh 
542cbd152aSriastradh #include <sys/cdefs.h>
55*a280a6b7Sriastradh __RCSID("$NetBSD: mbrtoc32.c,v 1.9 2024/08/20 17:43:24 riastradh Exp $");
56cd14a503Sriastradh 
57cd14a503Sriastradh #include "namespace.h"
582cbd152aSriastradh 
592cbd152aSriastradh #include <sys/param.h>		/* MIN */
602cbd152aSriastradh #include <sys/types.h>		/* broken citrus_*.h */
612cbd152aSriastradh #include <sys/queue.h>		/* broken citrus_*.h */
622cbd152aSriastradh 
632cbd152aSriastradh #include <assert.h>
642cbd152aSriastradh #include <errno.h>
652cbd152aSriastradh #include <langinfo.h>
662cbd152aSriastradh #include <limits.h>
674651a634Sriastradh #include <locale.h>
682cbd152aSriastradh #include <paths.h>
69b36f256cSriastradh #include <stdalign.h>
702cbd152aSriastradh #include <stddef.h>
712cbd152aSriastradh #include <stdlib.h>
722cbd152aSriastradh #include <string.h>
732cbd152aSriastradh #include <uchar.h>
742cbd152aSriastradh #include <wchar.h>
752cbd152aSriastradh 
762cbd152aSriastradh #include "citrus_types.h"	/* broken citrus_iconv.h */
772cbd152aSriastradh #include "citrus_module.h"	/* broken citrus_iconv.h */
782cbd152aSriastradh #include "citrus_hash.h"	/* broken citrus_iconv.h */
792cbd152aSriastradh #include "citrus_iconv.h"
804651a634Sriastradh #include "setlocale_local.h"
812cbd152aSriastradh 
822cbd152aSriastradh #include "mbrtoc32.h"
832cbd152aSriastradh 
842cbd152aSriastradh __CTASSERT(sizeof(struct mbrtoc32state) <= sizeof(mbstate_t));
85b36f256cSriastradh __CTASSERT(alignof(struct mbrtoc32state) <= alignof(mbstate_t));
862cbd152aSriastradh 
87cd14a503Sriastradh #ifdef __weak_alias
88cd14a503Sriastradh __weak_alias(mbrtoc32,_mbrtoc32)
894651a634Sriastradh __weak_alias(mbrtoc32_l,_mbrtoc32_l)
90cd14a503Sriastradh #endif
91cd14a503Sriastradh 
922cbd152aSriastradh size_t
932cbd152aSriastradh mbrtoc32(char32_t *restrict pc32, const char *restrict s, size_t n,
942cbd152aSriastradh     mbstate_t *restrict ps)
952cbd152aSriastradh {
964651a634Sriastradh 
974651a634Sriastradh 	return mbrtoc32_l(pc32, s, n, ps, _current_locale());
984651a634Sriastradh }
994651a634Sriastradh 
1004651a634Sriastradh size_t
1014651a634Sriastradh mbrtoc32_l(char32_t *restrict pc32, const char *restrict s, size_t n,
1024651a634Sriastradh     mbstate_t *restrict ps, locale_t restrict loc)
1034651a634Sriastradh {
1042cbd152aSriastradh 	static mbstate_t psbuf;
1052cbd152aSriastradh 	struct _citrus_iconv *iconv = NULL;
106926b03a6Sriastradh 	wchar_t wc;
107926b03a6Sriastradh 	mbstate_t wcrtombstate = {0};
108926b03a6Sriastradh 	char mb[MB_LEN_MAX];
109*a280a6b7Sriastradh 	size_t mb_len;
110926b03a6Sriastradh 	char utf32le[MB_LEN_MAX];
111926b03a6Sriastradh 	const char *src;
112926b03a6Sriastradh 	char *dst;
113926b03a6Sriastradh 	size_t srcleft, dstleft, inval;
1142cbd152aSriastradh 	char32_t c32;
115926b03a6Sriastradh 	size_t len;
1162cbd152aSriastradh 	int error, errno_save;
1172cbd152aSriastradh 
1182cbd152aSriastradh 	/*
1192cbd152aSriastradh 	 * Save errno in case _citrus_iconv_* clobbers it.
1202cbd152aSriastradh 	 */
1212cbd152aSriastradh 	errno_save = errno;
1222cbd152aSriastradh 
1232cbd152aSriastradh 	/*
1242cbd152aSriastradh 	 * `If ps is a null pointer, each function uses its own
1252cbd152aSriastradh 	 *  internal mbstate_t object instead, which is initialized at
1262cbd152aSriastradh 	 *  program startup to the initial conversion state; the
1272cbd152aSriastradh 	 *  functions are not required to avoid data races with other
1282cbd152aSriastradh 	 *  calls to the same function in this case.  The
1292cbd152aSriastradh 	 *  implementation behaves as if no library function calls
1302cbd152aSriastradh 	 *  these functions with a null pointer for ps.'
1312cbd152aSriastradh 	 */
1322cbd152aSriastradh 	if (ps == NULL)
1332cbd152aSriastradh 		ps = &psbuf;
1342cbd152aSriastradh 
1352cbd152aSriastradh 	/*
1362cbd152aSriastradh 	 * `If s is a null pointer, the mbrtoc32 function is equivalent
1372cbd152aSriastradh 	 *  to the call:
1382cbd152aSriastradh 	 *
1392cbd152aSriastradh 	 *	mbrtoc32(NULL, "", 1, ps)
1402cbd152aSriastradh 	 *
1412cbd152aSriastradh 	 *  In this case, the values of the parameters pc32 and n are
1422cbd152aSriastradh 	 *  ignored.'
1432cbd152aSriastradh 	 */
1442cbd152aSriastradh 	if (s == NULL) {
1452cbd152aSriastradh 		pc32 = NULL;
1462cbd152aSriastradh 		s = "";
1472cbd152aSriastradh 		n = 1;
1482cbd152aSriastradh 	}
1492cbd152aSriastradh 
1502cbd152aSriastradh 	/*
1512cbd152aSriastradh 	 * If input length is zero, the result is always incomplete by
1522cbd152aSriastradh 	 * definition.  Don't bother with iconv -- we'd have to
1532cbd152aSriastradh 	 * disentangle truncated outputs.
1542cbd152aSriastradh 	 */
1552cbd152aSriastradh 	if (n == 0) {
1562cbd152aSriastradh 		len = (size_t)-2;
1572cbd152aSriastradh 		goto out;
1582cbd152aSriastradh 	}
1592cbd152aSriastradh 
1602cbd152aSriastradh 	/*
1612cbd152aSriastradh 	 * Open an iconv handle to convert locale-dependent multibyte
1622cbd152aSriastradh 	 * input to UTF-32LE.
1632cbd152aSriastradh 	 */
1642cbd152aSriastradh 	if ((error = _citrus_iconv_open(&iconv, _PATH_ICONV,
1654651a634Sriastradh 		    nl_langinfo_l(CODESET, loc), "utf-32le")) != 0) {
1662cbd152aSriastradh 		errno = EIO; /* XXX? */
1672cbd152aSriastradh 		len = (size_t)-1;
1682cbd152aSriastradh 		goto out;
1692cbd152aSriastradh 	}
1702cbd152aSriastradh 
1712cbd152aSriastradh 	/*
172926b03a6Sriastradh 	 * Consume the next locale-dependent wide character.  If no
173926b03a6Sriastradh 	 * wide character can be obtained, stop here.
1742cbd152aSriastradh 	 */
175926b03a6Sriastradh 	len = mbrtowc_l(&wc, s, n, ps, loc);
176926b03a6Sriastradh 	switch (len) {
177926b03a6Sriastradh 	case 0:			/* NUL */
178926b03a6Sriastradh 		if (pc32)
179926b03a6Sriastradh 			*pc32 = 0;
180926b03a6Sriastradh 		goto out;
181926b03a6Sriastradh 	case (size_t)-2:	/* still incomplete after n bytes */
182926b03a6Sriastradh 	case (size_t)-1:	/* error */
183926b03a6Sriastradh 		goto out;
184926b03a6Sriastradh 	default:		/* consumed len bytes of input */
185cc0f56dbSriastradh 		break;
186926b03a6Sriastradh 	}
187926b03a6Sriastradh 
188926b03a6Sriastradh 	/*
189926b03a6Sriastradh 	 * We consumed a wide character from the input.  Convert it to
190926b03a6Sriastradh 	 * a multibyte sequence _in the initial conversion state_, so
191926b03a6Sriastradh 	 * we can pass that through iconv to get a Unicode scalar
192926b03a6Sriastradh 	 * value.
193926b03a6Sriastradh 	 */
194*a280a6b7Sriastradh 	if ((mb_len = wcrtomb_l(mb, wc, &wcrtombstate, loc)) == (size_t)-1) {
195926b03a6Sriastradh 		len = (size_t)-1;
196926b03a6Sriastradh 		goto out;
197926b03a6Sriastradh 	}
198926b03a6Sriastradh 
199926b03a6Sriastradh 	/*
200926b03a6Sriastradh 	 * Convert the multibyte sequence to UTF-16LE.
201926b03a6Sriastradh 	 */
202926b03a6Sriastradh 	src = mb;
203*a280a6b7Sriastradh 	srcleft = mb_len;
204926b03a6Sriastradh 	dst = utf32le;
205926b03a6Sriastradh 	dstleft = sizeof(utf32le);
206926b03a6Sriastradh 	error = _citrus_iconv_convert(iconv, &src, &srcleft, &dst, &dstleft,
207926b03a6Sriastradh 	    _CITRUS_ICONV_F_HIDE_INVALID, &inval);
208926b03a6Sriastradh 	if (error) {
2092cbd152aSriastradh 		errno = error;
2102cbd152aSriastradh 		len = (size_t)-1;
2112cbd152aSriastradh 		goto out;
2122cbd152aSriastradh 	}
2132cbd152aSriastradh 
2142cbd152aSriastradh 	/*
215926b03a6Sriastradh 	 * Successfully converted the multibyte sequence to UTF-16LE,
216926b03a6Sriastradh 	 * which should produce exactly one UTF-32 code unit, encoded
217926b03a6Sriastradh 	 * in little-endian, representing a code point.  Get the code
2182cbd152aSriastradh 	 * point.
2192cbd152aSriastradh 	 */
220926b03a6Sriastradh 	c32 = le32dec(utf32le);
2212cbd152aSriastradh 
2222cbd152aSriastradh 	/*
2232cbd152aSriastradh 	 * Reject surrogate code points.  We only deal in scalar
2242cbd152aSriastradh 	 * values.
2252cbd152aSriastradh 	 *
2262cbd152aSriastradh 	 * XXX Is this necessary?  Won't iconv take care of it for us?
2272cbd152aSriastradh 	 */
2282cbd152aSriastradh 	if (c32 >= 0xd800 && c32 <= 0xdfff) {
2292cbd152aSriastradh 		errno = EILSEQ;
2302cbd152aSriastradh 		len = (size_t)-1;
2312cbd152aSriastradh 		goto out;
2322cbd152aSriastradh 	}
2332cbd152aSriastradh 
2342cbd152aSriastradh 	/*
2352cbd152aSriastradh 	 * Non-surrogate code point -- scalar value.  Yield it.
2362cbd152aSriastradh 	 */
2372cbd152aSriastradh 	if (pc32)
2382cbd152aSriastradh 		*pc32 = c32;
2392cbd152aSriastradh 
2402cbd152aSriastradh 	/*
2412cbd152aSriastradh 	 * If we got the null scalar value, return zero length, as the
2422cbd152aSriastradh 	 * contract requires.
2432cbd152aSriastradh 	 */
2442cbd152aSriastradh 	if (c32 == 0)
2452cbd152aSriastradh 		len = 0;
2462cbd152aSriastradh 
2472cbd152aSriastradh 	/*
2482cbd152aSriastradh 	 * Make sure we preserve errno on success.
2492cbd152aSriastradh 	 */
2502cbd152aSriastradh 	errno = errno_save;
2512cbd152aSriastradh 
252926b03a6Sriastradh out:	errno_save = errno;
2532cbd152aSriastradh 	_citrus_iconv_close(iconv);
2542cbd152aSriastradh 	errno = errno_save;
2552cbd152aSriastradh 	return len;
2562cbd152aSriastradh }
257