Home | History | Annotate | Line # | Download | only in locale
mbrtoc32.c revision 1.6
      1 /*	$NetBSD: mbrtoc32.c,v 1.6 2024/08/17 21:24:54 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  * mbrtoc32(&c32, s, n, ps)
     31  *
     32  *	Decode a Unicode UTF-32 code unit from up to n bytes out of the
     33  *	multibyte string s, and store it at c32, using multibyte
     34  *	encoding state ps.  A UTF-32 code unit is also a Unicode scalar
     35  *	value, which is any Unicode code point except a surrogate.
     36  *
     37  *	Return the number of bytes consumed on success, or 0 if the
     38  *	code unit is NUL, or (size_t)-2 if the input is incomplete, or
     39  *	(size_t)-1 on error with errno set to EILSEQ.
     40  *
     41  *	In the case of incomplete input, the decoding state so far
     42  *	after processing s[0], s[1], ..., s[n - 1] is saved in ps, so
     43  *	subsequent calls to mbrtoc32 will pick up n bytes later into
     44  *	the input stream.
     45  *
     46  * References:
     47  *
     48  *	The Unicode Standard, Version 15.0 -- Core Specification, The
     49  *	Unicode Consortium, Sec. 3.8 `Surrogates', p. 119.
     50  *	https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf
     51  *	https://web.archive.org/web/20240718101254/https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf
     52  */
     53 
     54 #include <sys/cdefs.h>
     55 __RCSID("$NetBSD: mbrtoc32.c,v 1.6 2024/08/17 21:24:54 riastradh Exp $");
     56 
     57 #include "namespace.h"
     58 
     59 #include <sys/param.h>		/* MIN */
     60 #include <sys/types.h>		/* broken citrus_*.h */
     61 #include <sys/queue.h>		/* broken citrus_*.h */
     62 
     63 #include <assert.h>
     64 #include <errno.h>
     65 #include <langinfo.h>
     66 #include <limits.h>
     67 #include <locale.h>
     68 #include <paths.h>
     69 #include <stdalign.h>
     70 #include <stddef.h>
     71 #include <stdlib.h>
     72 #include <string.h>
     73 #include <uchar.h>
     74 #include <wchar.h>
     75 
     76 #include "citrus_types.h"	/* broken citrus_iconv.h */
     77 #include "citrus_module.h"	/* broken citrus_iconv.h */
     78 #include "citrus_hash.h"	/* broken citrus_iconv.h */
     79 #include "citrus_iconv.h"
     80 #include "setlocale_local.h"
     81 
     82 #include "mbrtoc32.h"
     83 
     84 __CTASSERT(sizeof(struct mbrtoc32state) <= sizeof(mbstate_t));
     85 __CTASSERT(alignof(struct mbrtoc32state) <= alignof(mbstate_t));
     86 
     87 #ifdef __weak_alias
     88 __weak_alias(mbrtoc32,_mbrtoc32)
     89 __weak_alias(mbrtoc32_l,_mbrtoc32_l)
     90 #endif
     91 
     92 size_t
     93 mbrtoc32(char32_t *restrict pc32, const char *restrict s, size_t n,
     94     mbstate_t *restrict ps)
     95 {
     96 
     97 	return mbrtoc32_l(pc32, s, n, ps, _current_locale());
     98 }
     99 
    100 size_t
    101 mbrtoc32_l(char32_t *restrict pc32, const char *restrict s, size_t n,
    102     mbstate_t *restrict ps, locale_t restrict loc)
    103 {
    104 	static mbstate_t psbuf;
    105 	struct mbrtoc32state *S;
    106 	struct _citrus_iconv *iconv = NULL;
    107 	size_t len;
    108 	char32_t c32;
    109 	int error, errno_save;
    110 
    111 	/*
    112 	 * Save errno in case _citrus_iconv_* clobbers it.
    113 	 */
    114 	errno_save = errno;
    115 
    116 	/*
    117 	 * `If ps is a null pointer, each function uses its own
    118 	 *  internal mbstate_t object instead, which is initialized at
    119 	 *  program startup to the initial conversion state; the
    120 	 *  functions are not required to avoid data races with other
    121 	 *  calls to the same function in this case.  The
    122 	 *  implementation behaves as if no library function calls
    123 	 *  these functions with a null pointer for ps.'
    124 	 */
    125 	if (ps == NULL)
    126 		ps = &psbuf;
    127 
    128 	/*
    129 	 * `If s is a null pointer, the mbrtoc32 function is equivalent
    130 	 *  to the call:
    131 	 *
    132 	 *	mbrtoc32(NULL, "", 1, ps)
    133 	 *
    134 	 *  In this case, the values of the parameters pc32 and n are
    135 	 *  ignored.'
    136 	 */
    137 	if (s == NULL) {
    138 		pc32 = NULL;
    139 		s = "";
    140 		n = 1;
    141 	}
    142 
    143 	/*
    144 	 * Get the private conversion state.
    145 	 */
    146 	S = (struct mbrtoc32state *)(void *)ps;
    147 
    148 	/*
    149 	 * If input length is zero, the result is always incomplete by
    150 	 * definition.  Don't bother with iconv -- we'd have to
    151 	 * disentangle truncated outputs.
    152 	 */
    153 	if (n == 0) {
    154 		len = (size_t)-2;
    155 		goto out;
    156 	}
    157 
    158 	/*
    159 	 * Reset the destination buffer if this is the initial state.
    160 	 */
    161 	if (S->dstleft == 0)
    162 		S->dstleft = sizeof(S->dstbuf);
    163 
    164 	/*
    165 	 * Open an iconv handle to convert locale-dependent multibyte
    166 	 * input to UTF-32LE.
    167 	 */
    168 	if ((error = _citrus_iconv_open(&iconv, _PATH_ICONV,
    169 		    nl_langinfo_l(CODESET, loc), "utf-32le")) != 0) {
    170 		errno = EIO; /* XXX? */
    171 		len = (size_t)-1;
    172 		goto out;
    173 	}
    174 
    175 	/*
    176 	 * Try to iconv a minimal prefix.  If we succeed, set len to
    177 	 * the length consumed and goto ok.
    178 	 */
    179 	for (len = 0; len < MIN(n, sizeof(S->srcbuf) - S->nsrc);) {
    180 		const char *src = S->srcbuf;
    181 		size_t srcleft;
    182 		char *dst = S->dstbuf + sizeof(S->dstbuf) - S->dstleft;
    183 		size_t inval;
    184 
    185 		S->srcbuf[S->nsrc++] = s[len++];
    186 		srcleft = S->nsrc;
    187 
    188 		error = _citrus_iconv_convert(iconv,
    189 		    &src, &srcleft,
    190 		    &dst, &S->dstleft,
    191 		    _CITRUS_ICONV_F_HIDE_INVALID, &inval);
    192 		if (error != EINVAL) {
    193 			if (error == 0)
    194 				break;
    195 			errno = error;
    196 			len = (size_t)-1;
    197 			goto out;
    198 		}
    199 	}
    200 
    201 	/*
    202 	 * If it is still incomplete after trying the whole input
    203 	 * buffer, return (size_t)-2 and let the caller try again.
    204 	 */
    205 	if (error) {
    206 		len = (size_t)-2;
    207 		goto out;
    208 	}
    209 
    210 	/*
    211 	 * Successfully converted a minimal byte sequence, which should
    212 	 * produce exactly one UTF-32 code unit, encoded in
    213 	 * little-endian, representing a code point.  Get the code
    214 	 * point.
    215 	 */
    216 	c32 = le32dec(S->dstbuf);
    217 
    218 	/*
    219 	 * Reject surrogate code points.  We only deal in scalar
    220 	 * values.
    221 	 *
    222 	 * XXX Is this necessary?  Won't iconv take care of it for us?
    223 	 */
    224 	if (c32 >= 0xd800 && c32 <= 0xdfff) {
    225 		errno = EILSEQ;
    226 		len = (size_t)-1;
    227 		goto out;
    228 	}
    229 
    230 	/*
    231 	 * Non-surrogate code point -- scalar value.  Yield it.
    232 	 */
    233 	if (pc32)
    234 		*pc32 = c32;
    235 
    236 	/*
    237 	 * If we got the null scalar value, return zero length, as the
    238 	 * contract requires.
    239 	 */
    240 	if (c32 == 0)
    241 		len = 0;
    242 
    243 	/*
    244 	 * Make sure we preserve errno on success.
    245 	 */
    246 	errno = errno_save;
    247 
    248 out:	if (len != (size_t)-2) {
    249 		S->nsrc = 0;
    250 		memset(S, 0, sizeof(*S)); /* paranoia */
    251 	}
    252 	errno_save = errno;
    253 	_citrus_iconv_close(iconv);
    254 	errno = errno_save;
    255 	return len;
    256 }
    257