Home | History | Annotate | Line # | Download | only in locale
c32rtomb.c revision 1.3
      1 /*	$NetBSD: c32rtomb.c,v 1.3 2024/08/17 21:24:53 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  * c32rtomb(s, c32, ps)
     31  *
     32  *	Encode the Unicode UTF-32 code unit c32, which must not be a
     33  *	surrogate code point, into the multibyte buffer s under the
     34  *	current locale, using multibyte encoding state ps.  A UTF-32
     35  *	code unit is also a Unicode scalar value, which is any Unicode
     36  *	code point except a surrogate.
     37  *
     38  *	Return the number of bytes stored on success, or (size_t)-1 on
     39  *	error with errno set to EILSEQ.
     40  *
     41  *	At most MB_CUR_MAX bytes will be stored.
     42  *
     43  * References:
     44  *
     45  *	The Unicode Standard, Version 15.0 -- Core Specification, The
     46  *	Unicode Consortium, Sec. 3.8 `Surrogates', p. 119.
     47  *	https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf
     48  *	https://web.archive.org/web/20240718101254/https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf
     49  */
     50 
     51 #include <sys/cdefs.h>
     52 __RCSID("$NetBSD: c32rtomb.c,v 1.3 2024/08/17 21:24:53 riastradh Exp $");
     53 
     54 #include "namespace.h"
     55 
     56 #include <sys/types.h>		/* broken citrus_*.h */
     57 #include <sys/queue.h>		/* broken citrus_*.h */
     58 
     59 #include <assert.h>
     60 #include <errno.h>
     61 #include <langinfo.h>
     62 #include <limits.h>
     63 #include <locale.h>
     64 #include <paths.h>
     65 #include <stddef.h>
     66 #include <stdlib.h>
     67 #include <uchar.h>
     68 #include <wchar.h>
     69 
     70 #include "citrus_types.h"	/* broken citrus_iconv.h */
     71 #include "citrus_module.h"	/* broken citrus_iconv.h */
     72 #include "citrus_hash.h"	/* broken citrus_iconv.h */
     73 #include "citrus_iconv.h"
     74 #include "setlocale_local.h"
     75 
     76 #ifdef __weak_alias
     77 __weak_alias(c32rtomb,_c32rtomb)
     78 __weak_alias(c32rtomb_l,_c32rtomb_l)
     79 #endif
     80 
     81 size_t
     82 c32rtomb(char *restrict s, char32_t c32, mbstate_t *restrict ps)
     83 {
     84 
     85 	return c32rtomb_l(s, c32, ps, _current_locale());
     86 }
     87 
     88 size_t
     89 c32rtomb_l(char *restrict s, char32_t c32, mbstate_t *restrict ps,
     90     locale_t loc)
     91 {
     92 	char buf[MB_LEN_MAX];
     93 	struct _citrus_iconv *iconv = NULL;
     94 	char srcbuf[4];
     95 	const char *src;
     96 	char *dst;
     97 	size_t srcleft, dstleft, inval, len;
     98 	int error, errno_save;
     99 
    100 	/*
    101 	 * Save errno in case _citrus_iconv_* clobbers it.
    102 	 */
    103 	errno_save = errno;
    104 
    105 	/*
    106 	 * `If s is a null pointer, the c32rtomb function is equivalent
    107 	 *  to the call
    108 	 *
    109 	 *	c32rtomb(buf, L'\0', ps)
    110 	 *
    111 	 *  where buf is an internal buffer.'
    112 	 */
    113 	if (s == NULL) {
    114 		s = buf;
    115 		c32 = L'\0';
    116 	}
    117 
    118 	/*
    119 	 * Reject surrogates.
    120 	 */
    121 	if (c32 >= 0xd800 && c32 <= 0xdfff) {
    122 		errno = EILSEQ;
    123 		len = (size_t)-1;
    124 		goto out;
    125 	}
    126 
    127 	/*
    128 	 * Open an iconv handle to convert UTF-32LE to locale-dependent
    129 	 * multibyte output.
    130 	 */
    131 	if ((error = _citrus_iconv_open(&iconv, _PATH_ICONV, "utf-32le",
    132 		    nl_langinfo_l(CODESET, loc))) != 0) {
    133 		errno = EIO; /* XXX? */
    134 		len = (size_t)-1;
    135 		goto out;
    136 	}
    137 
    138 	/*
    139 	 * Convert from UTF-32LE in our buffer.
    140 	 */
    141 	le32enc(srcbuf, c32);
    142 	src = srcbuf;
    143 	srcleft = sizeof(srcbuf);
    144 	dst = s;
    145 	dstleft = MB_CUR_MAX;
    146 	error = _citrus_iconv_convert(iconv,
    147 	    &src, &srcleft,
    148 	    &dst, &dstleft,
    149 	    _CITRUS_ICONV_F_HIDE_INVALID, &inval);
    150 	if (error) {		/* can't be incomplete, must be error */
    151 		errno = error;
    152 		len = (size_t)-1;
    153 		goto out;
    154 	}
    155 	_DIAGASSERT(srcleft == 0);
    156 	_DIAGASSERT(dstleft <= MB_CUR_MAX);
    157 
    158 	/*
    159 	 * If we didn't produce any output, that means the scalar value
    160 	 * c32 can't be encoded in the current locale, so treat it as
    161 	 * EILSEQ.
    162 	 */
    163 	len = MB_CUR_MAX - dstleft;
    164 	if (len == 0) {
    165 		errno = EILSEQ;
    166 		len = (size_t)-1;
    167 		goto out;
    168 	}
    169 
    170 	/*
    171 	 * Make sure we preserve errno on success.
    172 	 */
    173 	errno = errno_save;
    174 
    175 out:	errno_save = errno;
    176 	_citrus_iconv_close(iconv);
    177 	errno = errno_save;
    178 	return len;
    179 }
    180