2 * This code was written by Rich Felker in 2010; no copyright is claimed.
3 * This code is in the public domain. Attribution is appreciated but
12 int mbtowc(wchar_t *restrict wc
, const char *restrict src
, size_t n
)
15 const unsigned char *s
= (const void *)src
;
22 if (*s
< 0x80) return !!(*wc
= *s
);
23 if (MB_CUR_MAX
==1) return (*wc
= CODEUNIT(*s
)), 1;
24 if (*s
-SA
> SB
-SA
) goto ilseq
;
27 /* Avoid excessive checks against n: If shifting the state n-1
28 * times does not clear the high bit, then the value of n is
29 * insufficient to read a character */
30 if (n
<4 && ((c
<<(6*n
-6)) & (1U<<31))) goto ilseq
;
32 if (OOB(c
,*s
)) goto ilseq
;
39 if (*s
-0x80u
>= 0x40) goto ilseq
;
46 if (*s
-0x80u
>= 0x40) goto ilseq
;
47 *wc
= c
<<6 | *s
++-0x80;