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 (!ws) return strlen((const char *)s);
33 *src = (const void *)s;
47 if (*s-1u < 0x7f && (uintptr_t)s%4 == 0) {
48 while (!(( *(uint32_t*)s | *(uint32_t*)s-0x01010101) & 0x80808080)) {
58 if (*s-SA > SB-SA) break;
61 if (OOB(c,*s)) { s--; break; }
64 if (*s-0x80u >= 0x40) { s-=2; break; }
67 if (*s-0x80u >= 0x40) { s-=3; break; }
75 *src = (const void *)s;
78 if (*s-1u < 0x7f && (uintptr_t)s%4 == 0) {
79 while (wn>=5 && !(( *(uint32_t*)s | *(uint32_t*)s-0x01010101) & 0x80808080)) {
92 if (*s-SA > SB-SA) break;
95 if (OOB(c,*s)) { s--; break; }
96 c = (c<<6) | *s++-0x80;
98 if (*s-0x80u >= 0x40) { s-=2; break; }
99 c = (c<<6) | *s++-0x80;
101 if (*s-0x80u >= 0x40) { s-=3; break; }
102 c = (c<<6) | *s++-0x80;
118 if (ws) *src = (const void *)s;