xref: /titanic_50/usr/src/lib/libc/port/locale/mbrtoc32.c (revision 3fc10f8cbc2fd5dd5cd13044edf9cb68a1ef422b)
1*3fc10f8cSRobert Mustacchi /*
2*3fc10f8cSRobert Mustacchi  * This file and its contents are supplied under the terms of the
3*3fc10f8cSRobert Mustacchi  * Common Development and Distribution License ("CDDL"), version 1.0.
4*3fc10f8cSRobert Mustacchi  * You may only use this file in accordance with the terms of version
5*3fc10f8cSRobert Mustacchi  * 1.0 of the CDDL.
6*3fc10f8cSRobert Mustacchi  *
7*3fc10f8cSRobert Mustacchi  * A full copy of the text of the CDDL should have accompanied this
8*3fc10f8cSRobert Mustacchi  * source.  A copy of the CDDL is also available via the Internet at
9*3fc10f8cSRobert Mustacchi  * http://www.illumos.org/license/CDDL.
10*3fc10f8cSRobert Mustacchi  */
11*3fc10f8cSRobert Mustacchi 
12*3fc10f8cSRobert Mustacchi /*
13*3fc10f8cSRobert Mustacchi  * Copyright 2020 Robert Mustacchi
14*3fc10f8cSRobert Mustacchi  */
15*3fc10f8cSRobert Mustacchi 
16*3fc10f8cSRobert Mustacchi /*
17*3fc10f8cSRobert Mustacchi  * C11 mbrtoc32(3C) support.
18*3fc10f8cSRobert Mustacchi  *
19*3fc10f8cSRobert Mustacchi  * The char32_t type is designed to represent UTF-32. Conveniently, the wchar_t
20*3fc10f8cSRobert Mustacchi  * is as well. In this case, we can just pass this directly to mbrtowc_l().
21*3fc10f8cSRobert Mustacchi  */
22*3fc10f8cSRobert Mustacchi 
23*3fc10f8cSRobert Mustacchi #include <locale.h>
24*3fc10f8cSRobert Mustacchi #include <wchar.h>
25*3fc10f8cSRobert Mustacchi #include <xlocale.h>
26*3fc10f8cSRobert Mustacchi #include <uchar.h>
27*3fc10f8cSRobert Mustacchi 
28*3fc10f8cSRobert Mustacchi static mbstate_t mbrtoc32_state;
29*3fc10f8cSRobert Mustacchi 
30*3fc10f8cSRobert Mustacchi size_t
mbrtoc32(char32_t * restrict pc32,const char * restrict str,size_t len,mbstate_t * restrict ps)31*3fc10f8cSRobert Mustacchi mbrtoc32(char32_t *restrict pc32, const char *restrict str, size_t len,
32*3fc10f8cSRobert Mustacchi     mbstate_t *restrict ps)
33*3fc10f8cSRobert Mustacchi {
34*3fc10f8cSRobert Mustacchi 	if (ps == NULL) {
35*3fc10f8cSRobert Mustacchi 		ps = &mbrtoc32_state;
36*3fc10f8cSRobert Mustacchi 	}
37*3fc10f8cSRobert Mustacchi 
38*3fc10f8cSRobert Mustacchi 	if (str == NULL) {
39*3fc10f8cSRobert Mustacchi 		pc32 = NULL;
40*3fc10f8cSRobert Mustacchi 		str = "";
41*3fc10f8cSRobert Mustacchi 		len = 1;
42*3fc10f8cSRobert Mustacchi 	}
43*3fc10f8cSRobert Mustacchi 
44*3fc10f8cSRobert Mustacchi 	return (mbrtowc_l((wchar_t *)pc32, str, len, ps,
45*3fc10f8cSRobert Mustacchi 	    uselocale((locale_t)0)));
46*3fc10f8cSRobert Mustacchi }
47