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 *restrict wc, const char *restrict src, size_t n, mbstate_t *restrict st)
16 static unsigned internal_state;
18 const unsigned char *s = (const void *)src;
21 if (!st) st = (void *)&internal_state;
27 } else if (!wc) wc = (void *)&wc;
31 if (*s < 0x80) return !!(*wc = *s);
32 if (*s-SA > SB-SA) goto ilseq;
33 c = bittab[*s++-SA]; n--;
37 if (OOB(c,*s)) goto ilseq;
39 c = c<<6 | *s++-0x80; n--;
46 if (*s-0x80u >= 0x40) goto ilseq;