Home | History | Annotate | Line # | Download | only in locale
c32rtomb.c revision 1.2
      1 /*	$NetBSD: c32rtomb.c,v 1.2 2024/08/15 22:22:35 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.2 2024/08/15 22:22:35 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 <paths.h>
     64 #include <stddef.h>
     65 #include <stdlib.h>
     66 #include <uchar.h>
     67 #include <wchar.h>
     68 
     69 #include "citrus_types.h"	/* broken citrus_iconv.h */
     70 #include "citrus_module.h"	/* broken citrus_iconv.h */
     71 #include "citrus_hash.h"	/* broken citrus_iconv.h */
     72 #include "citrus_iconv.h"
     73 
     74 #ifdef __weak_alias
     75 __weak_alias(c32rtomb,_c32rtomb)
     76 #endif
     77 
     78 size_t
     79 c32rtomb(char *restrict s, char32_t c32, mbstate_t *restrict ps)
     80 {
     81 	char buf[MB_LEN_MAX];
     82 	struct _citrus_iconv *iconv = NULL;
     83 	char srcbuf[4];
     84 	const char *src;
     85 	char *dst;
     86 	size_t srcleft, dstleft, inval, len;
     87 	int error, errno_save;
     88 
     89 	/*
     90 	 * Save errno in case _citrus_iconv_* clobbers it.
     91 	 */
     92 	errno_save = errno;
     93 
     94 	/*
     95 	 * `If s is a null pointer, the c32rtomb function is equivalent
     96 	 *  to the call
     97 	 *
     98 	 *	c32rtomb(buf, L'\0', ps)
     99 	 *
    100 	 *  where buf is an internal buffer.'
    101 	 */
    102 	if (s == NULL) {
    103 		s = buf;
    104 		c32 = L'\0';
    105 	}
    106 
    107 	/*
    108 	 * Reject surrogates.
    109 	 */
    110 	if (c32 >= 0xd800 && c32 <= 0xdfff) {
    111 		errno = EILSEQ;
    112 		len = (size_t)-1;
    113 		goto out;
    114 	}
    115 
    116 	/*
    117 	 * Open an iconv handle to convert UTF-32LE to locale-dependent
    118 	 * multibyte output.
    119 	 */
    120 	if ((error = _citrus_iconv_open(&iconv, _PATH_ICONV, "utf-32le",
    121 		    nl_langinfo(CODESET))) != 0) {
    122 		errno = EIO; /* XXX? */
    123 		len = (size_t)-1;
    124 		goto out;
    125 	}
    126 
    127 	/*
    128 	 * Convert from UTF-32LE in our buffer.
    129 	 */
    130 	le32enc(srcbuf, c32);
    131 	src = srcbuf;
    132 	srcleft = sizeof(srcbuf);
    133 	dst = s;
    134 	dstleft = MB_CUR_MAX;
    135 	error = _citrus_iconv_convert(iconv,
    136 	    &src, &srcleft,
    137 	    &dst, &dstleft,
    138 	    _CITRUS_ICONV_F_HIDE_INVALID, &inval);
    139 	if (error) {		/* can't be incomplete, must be error */
    140 		errno = error;
    141 		len = (size_t)-1;
    142 		goto out;
    143 	}
    144 	_DIAGASSERT(srcleft == 0);
    145 	_DIAGASSERT(dstleft <= MB_CUR_MAX);
    146 
    147 	/*
    148 	 * If we didn't produce any output, that means the scalar value
    149 	 * c32 can't be encoded in the current locale, so treat it as
    150 	 * EILSEQ.
    151 	 */
    152 	len = MB_CUR_MAX - dstleft;
    153 	if (len == 0) {
    154 		errno = EILSEQ;
    155 		len = (size_t)-1;
    156 		goto out;
    157 	}
    158 
    159 	/*
    160 	 * Make sure we preserve errno on success.
    161 	 */
    162 	errno = errno_save;
    163 
    164 out:	errno_save = errno;
    165 	_citrus_iconv_close(iconv);
    166 	errno = errno_save;
    167 	return len;
    168 }
    169