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
14 size_t mbrtowc(wchar_t *wc, const char *src, size_t n, mbstate_t *st)
16 static unsigned internal_state;
18 const unsigned char *s = (const void *)src;
21 if (!st) st = (void *)&internal_state;
28 } else if (!wc) wc = (void *)&wc;
32 if (*s < 0x80) return !!(*wc = *s);
33 if (*s-SA > SB-SA) goto ilseq;
34 c = bittab[*s++-SA]; n--;
38 if (OOB(c,*s)) goto ilseq;
40 c = c<<6 | *s++-0x80; n--;
47 if (*s-0x80u >= 0x40) goto ilseq;
55 *(unsigned *)st = FAILSTATE;