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 mbsrtowcs(wchar_t *restrict ws, const char **restrict src, size_t wn, mbstate_t *restrict st)
16 const unsigned char *s = (const void *)*src;
20 if (st && (c = *(unsigned *)st)) {
30 if (*s-1u < 0x7f && (uintptr_t)s%4 == 0) {
31 while (!(( *(uint32_t*)s | *(uint32_t*)s-0x01010101) & 0x80808080)) {
41 if (*s-SA > SB-SA) break;
44 if (OOB(c,*s)) { s--; break; }
47 if (*s-0x80u >= 0x40) { s-=2; break; }
50 if (*s-0x80u >= 0x40) { s-=3; break; }
58 *src = (const void *)s;
61 if (*s-1u < 0x7f && (uintptr_t)s%4 == 0) {
62 while (wn>=4 && !(( *(uint32_t*)s | *(uint32_t*)s-0x01010101) & 0x80808080)) {
75 if (*s-SA > SB-SA) break;
78 if (OOB(c,*s)) { s--; break; }
79 c = (c<<6) | *s++-0x80;
81 if (*s-0x80u >= 0x40) { s-=2; break; }
82 c = (c<<6) | *s++-0x80;
84 if (*s-0x80u >= 0x40) { s-=3; break; }
85 c = (c<<6) | *s++-0x80;
101 if (ws) *src = (const void *)s;