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