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 size_t mbsrtowcs(wchar_t *restrict ws, const char **restrict src, size_t wn, mbstate_t *restrict st)
14 const unsigned char *s = (const void *)*src;
18 if (st && (c = *(unsigned *)st)) {
28 if (*s-1u < 0x7f && (uintptr_t)s%4 == 0) {
29 while (!(( *(uint32_t*)s | *(uint32_t*)s-0x01010101) & 0x80808080)) {
39 if (*s-SA > SB-SA) break;
42 if (OOB(c,*s)) { s--; break; }
45 if (*s-0x80u >= 0x40) { s-=2; break; }
48 if (*s-0x80u >= 0x40) { s-=3; break; }
56 *src = (const void *)s;
59 if (*s-1u < 0x7f && (uintptr_t)s%4 == 0) {
60 while (wn>=5 && !(( *(uint32_t*)s | *(uint32_t*)s-0x01010101) & 0x80808080)) {
73 if (*s-SA > SB-SA) break;
76 if (OOB(c,*s)) { s--; break; }
77 c = (c<<6) | *s++-0x80;
79 if (*s-0x80u >= 0x40) { s-=2; break; }
80 c = (c<<6) | *s++-0x80;
82 if (*s-0x80u >= 0x40) { s-=3; break; }
83 c = (c<<6) | *s++-0x80;
99 if (ws) *src = (const void *)s;