1 /*-
2 * Copyright (c) 2008
3 * Thorsten Glaser <tg@mirbsd.org>
4 *
5 * Provided that these terms and disclaimer and all copyright notices
6 * are retained or reproduced in an accompanying document, permission
7 * is granted to deal in this work without restriction, including un-
8 * limited rights to use, publicly perform, distribute, sell, modify,
9 * merge, give away, or sublicence.
10 *
11 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
12 * the utmost extent permitted by applicable law, neither express nor
13 * implied; without malicious intent or gross negligence. In no event
14 * may a licensor, author or contributor be held liable for indirect,
15 * direct, other damage, loss, or other issues arising in any way out
16 * of dealing in the work, even if advised of the possibility of such
17 * damage or existence of a defect, except proven that it results out
18 * of said person's immediate fault when using the work as intended.
19 *
20 * The author reserves the right to steward the OPTU encoding forms.
21 */
22
23 #include <errno.h>
24 #include <wchar.h>
25
26 __RCSID("$MirOS: src/lib/libc/i18n/mbrtowc.c,v 1.17 2014/02/09 22:35:52 tg Exp $");
27
28 size_t
mbrtowc(wchar_t * dst,const char * src,size_t len,mbstate_t * ps)29 mbrtowc(wchar_t *dst, const char *src, size_t len, mbstate_t *ps)
30 {
31 static mbstate_t internal_mbstate = { 0, 0 };
32 wchar_t wc;
33 size_t rv;
34
35 if (__predict_false(ps == NULL))
36 ps = &internal_mbstate;
37 if (__predict_false(dst == NULL))
38 dst = &wc;
39
40 if (__predict_false(src == NULL)) {
41 ps->count = 0;
42 return (0);
43 }
44
45 rv = optu8to16(dst, src, len, ps);
46 if (*dst == 0)
47 return (0);
48 if (rv == 0) {
49 errno = EILSEQ;
50 return ((size_t)-1);
51 }
52 return (rv);
53 }
54