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