Home | History | Annotate | Line # | Download | only in locale
c32rtomb.c revision 1.6.2.2
      1  1.6.2.2  martin /*	$NetBSD: c32rtomb.c,v 1.6.2.2 2024/10/14 17:20:18 martin Exp $	*/
      2  1.6.2.2  martin 
      3  1.6.2.2  martin /*-
      4  1.6.2.2  martin  * Copyright (c) 2024 The NetBSD Foundation, Inc.
      5  1.6.2.2  martin  * All rights reserved.
      6  1.6.2.2  martin  *
      7  1.6.2.2  martin  * Redistribution and use in source and binary forms, with or without
      8  1.6.2.2  martin  * modification, are permitted provided that the following conditions
      9  1.6.2.2  martin  * are met:
     10  1.6.2.2  martin  * 1. Redistributions of source code must retain the above copyright
     11  1.6.2.2  martin  *    notice, this list of conditions and the following disclaimer.
     12  1.6.2.2  martin  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.6.2.2  martin  *    notice, this list of conditions and the following disclaimer in the
     14  1.6.2.2  martin  *    documentation and/or other materials provided with the distribution.
     15  1.6.2.2  martin  *
     16  1.6.2.2  martin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.6.2.2  martin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.6.2.2  martin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.6.2.2  martin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.6.2.2  martin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.6.2.2  martin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.6.2.2  martin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.6.2.2  martin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.6.2.2  martin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.6.2.2  martin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.6.2.2  martin  * POSSIBILITY OF SUCH DAMAGE.
     27  1.6.2.2  martin  */
     28  1.6.2.2  martin 
     29  1.6.2.2  martin /*
     30  1.6.2.2  martin  * c32rtomb(s, c32, ps)
     31  1.6.2.2  martin  *
     32  1.6.2.2  martin  *	Encode the Unicode UTF-32 code unit c32, which must not be a
     33  1.6.2.2  martin  *	surrogate code point, into the multibyte buffer s under the
     34  1.6.2.2  martin  *	current locale, using multibyte encoding state ps.  A UTF-32
     35  1.6.2.2  martin  *	code unit is also a Unicode scalar value, which is any Unicode
     36  1.6.2.2  martin  *	code point except a surrogate.
     37  1.6.2.2  martin  *
     38  1.6.2.2  martin  *	Return the number of bytes stored on success, or (size_t)-1 on
     39  1.6.2.2  martin  *	error with errno set to EILSEQ.
     40  1.6.2.2  martin  *
     41  1.6.2.2  martin  *	At most MB_CUR_MAX bytes will be stored.
     42  1.6.2.2  martin  *
     43  1.6.2.2  martin  * References:
     44  1.6.2.2  martin  *
     45  1.6.2.2  martin  *	The Unicode Standard, Version 15.0 -- Core Specification, The
     46  1.6.2.2  martin  *	Unicode Consortium, Sec. 3.8 `Surrogates', p. 118.
     47  1.6.2.2  martin  *	https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=144
     48  1.6.2.2  martin  *	https://web.archive.org/web/20240718101254/https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf#page=144
     49  1.6.2.2  martin  */
     50  1.6.2.2  martin 
     51  1.6.2.2  martin #include <sys/cdefs.h>
     52  1.6.2.2  martin __RCSID("$NetBSD: c32rtomb.c,v 1.6.2.2 2024/10/14 17:20:18 martin Exp $");
     53  1.6.2.2  martin 
     54  1.6.2.2  martin #include "namespace.h"
     55  1.6.2.2  martin 
     56  1.6.2.2  martin #include <sys/types.h>		/* broken citrus_*.h */
     57  1.6.2.2  martin #include <sys/queue.h>		/* broken citrus_*.h */
     58  1.6.2.2  martin 
     59  1.6.2.2  martin #include <assert.h>
     60  1.6.2.2  martin #include <errno.h>
     61  1.6.2.2  martin #include <langinfo.h>
     62  1.6.2.2  martin #include <limits.h>
     63  1.6.2.2  martin #include <locale.h>
     64  1.6.2.2  martin #include <paths.h>
     65  1.6.2.2  martin #include <stddef.h>
     66  1.6.2.2  martin #include <stdlib.h>
     67  1.6.2.2  martin #include <uchar.h>
     68  1.6.2.2  martin #include <wchar.h>
     69  1.6.2.2  martin 
     70  1.6.2.2  martin #include "citrus_types.h"	/* broken citrus_iconv.h */
     71  1.6.2.2  martin #include "citrus_module.h"	/* broken citrus_iconv.h */
     72  1.6.2.2  martin #include "citrus_hash.h"	/* broken citrus_iconv.h */
     73  1.6.2.2  martin #include "citrus_iconv.h"
     74  1.6.2.2  martin #include "setlocale_local.h"
     75  1.6.2.2  martin 
     76  1.6.2.2  martin #ifdef __weak_alias
     77  1.6.2.2  martin __weak_alias(c32rtomb,_c32rtomb)
     78  1.6.2.2  martin __weak_alias(c32rtomb_l,_c32rtomb_l)
     79  1.6.2.2  martin #endif
     80  1.6.2.2  martin 
     81  1.6.2.2  martin size_t
     82  1.6.2.2  martin c32rtomb(char *restrict s, char32_t c32, mbstate_t *restrict ps)
     83  1.6.2.2  martin {
     84  1.6.2.2  martin 
     85  1.6.2.2  martin 	return c32rtomb_l(s, c32, ps, _current_locale());
     86  1.6.2.2  martin }
     87  1.6.2.2  martin 
     88  1.6.2.2  martin size_t
     89  1.6.2.2  martin c32rtomb_l(char *restrict s, char32_t c32, mbstate_t *restrict ps,
     90  1.6.2.2  martin     locale_t loc)
     91  1.6.2.2  martin {
     92  1.6.2.2  martin 	static mbstate_t psbuf;
     93  1.6.2.2  martin 	struct _citrus_iconv *iconv = NULL;
     94  1.6.2.2  martin 	char buf[2*MB_LEN_MAX];	/* [shift from init, wc] [shift to init] */
     95  1.6.2.2  martin 	char utf32le[4];
     96  1.6.2.2  martin 	const char *src;
     97  1.6.2.2  martin 	char *dst;
     98  1.6.2.2  martin 	size_t srcleft, dstleft, inval;
     99  1.6.2.2  martin 	mbstate_t mbrtowcstate = {0};
    100  1.6.2.2  martin 	wchar_t wc;
    101  1.6.2.2  martin 	size_t wc_len;
    102  1.6.2.2  martin 	size_t len;
    103  1.6.2.2  martin 	int error, errno_save;
    104  1.6.2.2  martin 
    105  1.6.2.2  martin 	/*
    106  1.6.2.2  martin 	 * Save errno in case _citrus_iconv_* clobbers it.
    107  1.6.2.2  martin 	 */
    108  1.6.2.2  martin 	errno_save = errno;
    109  1.6.2.2  martin 
    110  1.6.2.2  martin 	/*
    111  1.6.2.2  martin 	 * `If ps is a null pointer, each function uses its own
    112  1.6.2.2  martin 	 *  internal mbstate_t object instead, which is initialized at
    113  1.6.2.2  martin 	 *  program startup to the initial conversion state; the
    114  1.6.2.2  martin 	 *  functions are not required to avoid data races with other
    115  1.6.2.2  martin 	 *  calls to the same function in this case.  The
    116  1.6.2.2  martin 	 *  implementation behaves as if no library function calls
    117  1.6.2.2  martin 	 *  these functions with a null pointer for ps.'
    118  1.6.2.2  martin 	 */
    119  1.6.2.2  martin 	if (ps == NULL)
    120  1.6.2.2  martin 		ps = &psbuf;
    121  1.6.2.2  martin 
    122  1.6.2.2  martin 	/*
    123  1.6.2.2  martin 	 * `If s is a null pointer, the c32rtomb function is equivalent
    124  1.6.2.2  martin 	 *  to the call
    125  1.6.2.2  martin 	 *
    126  1.6.2.2  martin 	 *	c32rtomb(buf, L'\0', ps)
    127  1.6.2.2  martin 	 *
    128  1.6.2.2  martin 	 *  where buf is an internal buffer.'
    129  1.6.2.2  martin 	 */
    130  1.6.2.2  martin 	if (s == NULL) {
    131  1.6.2.2  martin 		s = buf;
    132  1.6.2.2  martin 		c32 = L'\0';
    133  1.6.2.2  martin 	}
    134  1.6.2.2  martin 
    135  1.6.2.2  martin 	/*
    136  1.6.2.2  martin 	 * Reject surrogate code points.  We only deal in scalar
    137  1.6.2.2  martin 	 * values.
    138  1.6.2.2  martin 	 *
    139  1.6.2.2  martin 	 * XXX Is this necessary?  Won't iconv take care of it for us?
    140  1.6.2.2  martin 	 */
    141  1.6.2.2  martin 	if (c32 >= 0xd800 && c32 <= 0xdfff) {
    142  1.6.2.2  martin 		errno = EILSEQ;
    143  1.6.2.2  martin 		len = (size_t)-1;
    144  1.6.2.2  martin 		goto out;
    145  1.6.2.2  martin 	}
    146  1.6.2.2  martin 
    147  1.6.2.2  martin 	/*
    148  1.6.2.2  martin 	 * Open an iconv handle to convert UTF-32LE to locale-dependent
    149  1.6.2.2  martin 	 * multibyte output.
    150  1.6.2.2  martin 	 */
    151  1.6.2.2  martin 	if ((error = _citrus_iconv_open(&iconv, _PATH_ICONV, "utf-32le",
    152  1.6.2.2  martin 		    nl_langinfo_l(CODESET, loc))) != 0) {
    153  1.6.2.2  martin 		errno = EIO; /* XXX? */
    154  1.6.2.2  martin 		len = (size_t)-1;
    155  1.6.2.2  martin 		goto out;
    156  1.6.2.2  martin 	}
    157  1.6.2.2  martin 
    158  1.6.2.2  martin 	/*
    159  1.6.2.2  martin 	 * Convert from UTF-32LE to a multibyte sequence.
    160  1.6.2.2  martin 	 */
    161  1.6.2.2  martin 	le32enc(utf32le, c32);
    162  1.6.2.2  martin 	src = utf32le;
    163  1.6.2.2  martin 	srcleft = sizeof(utf32le);
    164  1.6.2.2  martin 	dst = buf;
    165  1.6.2.2  martin 	dstleft = MB_CUR_MAX;
    166  1.6.2.2  martin 	error = _citrus_iconv_convert(iconv, &src, &srcleft, &dst, &dstleft,
    167  1.6.2.2  martin 	    _CITRUS_ICONV_F_HIDE_INVALID, &inval);
    168  1.6.2.2  martin 	if (error) {		/* can't be incomplete, must be error */
    169  1.6.2.2  martin 		errno = error;
    170  1.6.2.2  martin 		len = (size_t)-1;
    171  1.6.2.2  martin 		goto out;
    172  1.6.2.2  martin 	}
    173  1.6.2.2  martin 	_DIAGASSERT(srcleft == 0);
    174  1.6.2.2  martin 	_DIAGASSERT(dstleft <= MB_CUR_MAX);
    175  1.6.2.2  martin 
    176  1.6.2.2  martin 	/*
    177  1.6.2.2  martin 	 * If we didn't produce any output, that means the scalar value
    178  1.6.2.2  martin 	 * c32 can't be encoded in the current locale, so treat it as
    179  1.6.2.2  martin 	 * EILSEQ.
    180  1.6.2.2  martin 	 */
    181  1.6.2.2  martin 	len = MB_CUR_MAX - dstleft;
    182  1.6.2.2  martin 	if (len == 0) {
    183  1.6.2.2  martin 		errno = EILSEQ;
    184  1.6.2.2  martin 		len = (size_t)-1;
    185  1.6.2.2  martin 		goto out;
    186  1.6.2.2  martin 	}
    187  1.6.2.2  martin 
    188  1.6.2.2  martin 	/*
    189  1.6.2.2  martin 	 * Now get a wide character out of the buffer.  We don't care
    190  1.6.2.2  martin 	 * how much it consumes other than for a diagnostic assertion.
    191  1.6.2.2  martin 	 * It had better return exactly one wide character, because we
    192  1.6.2.2  martin 	 * are only allowed to encode one wide character's worth of
    193  1.6.2.2  martin 	 * multibyte output (possibly including a shift sequence).
    194  1.6.2.2  martin 	 *
    195  1.6.2.2  martin 	 * XXX What about combining characters?
    196  1.6.2.2  martin 	 */
    197  1.6.2.2  martin 	wc_len = mbrtowc_l(&wc, buf, len, &mbrtowcstate, loc);
    198  1.6.2.2  martin 	switch (wc_len) {
    199  1.6.2.2  martin 	case (size_t)-1:	/* error, with errno set */
    200  1.6.2.2  martin 		len = (size_t)-1;
    201  1.6.2.2  martin 		goto out;
    202  1.6.2.2  martin 	case 0:			/* decoded NUL */
    203  1.6.2.2  martin 		wc = 0;		/* paranoia */
    204  1.6.2.2  martin 		len = wc_len;
    205  1.6.2.2  martin 		break;
    206  1.6.2.2  martin 	default:		/* decoded wc */
    207  1.6.2.2  martin 		_DIAGASSERT(wc_len <= len);
    208  1.6.2.2  martin 	}
    209  1.6.2.2  martin 
    210  1.6.2.2  martin 	/*
    211  1.6.2.2  martin 	 * Now put the wide character out, using the caller's
    212  1.6.2.2  martin 	 * conversion state so that we don't output unnecessary shift
    213  1.6.2.2  martin 	 * sequences.
    214  1.6.2.2  martin 	 */
    215  1.6.2.2  martin 	len = wcrtomb_l(s, wc, ps, loc);
    216  1.6.2.2  martin 	if (len == (size_t)-1)	/* error, with errno set */
    217  1.6.2.2  martin 		goto out;
    218  1.6.2.2  martin 
    219  1.6.2.2  martin 	/*
    220  1.6.2.2  martin 	 * Make sure we preserve errno on success.
    221  1.6.2.2  martin 	 */
    222  1.6.2.2  martin 	errno = errno_save;
    223  1.6.2.2  martin 
    224  1.6.2.2  martin out:	errno_save = errno;
    225  1.6.2.2  martin 	_citrus_iconv_close(iconv);
    226  1.6.2.2  martin 	errno = errno_save;
    227  1.6.2.2  martin 	return len;
    228  1.6.2.2  martin }
    229