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 *ws, const char **src, size_t wn, mbstate_t *st)
17 const unsigned char *s = (const void *)*src;
18 const wchar_t *wsorig = ws;
20 if (!st) st = (void *)&c, c = 0;
21 else c = *(unsigned *)st;
32 if (!ws) for (wn=0;;) {
34 while (((uintptr_t)s&3) && *s-1u<0x7f) s++, wn++;
35 while (!(( *(uint32_t*)s | *(uint32_t*)s-0x01010101) & 0x80808080)) s+=4, wn+=4;
36 while (*s-1u<0x7f) s++, wn++;
38 if (*s-SA >= SB-SA) goto ilseq2;
43 if (OOB(c,*s)) goto ilseq2; s++;
44 c <<= 6; if (!(c&(1U<<31))) break;
45 if (*s++-0x80u >= 0x40) goto ilseq2;
46 c <<= 6; if (!(c&(1U<<31))) break;
47 if (*s++-0x80u >= 0x40) goto ilseq2;
55 while (((uintptr_t)s&3) && *s-1u<0x7f) {
59 while (wn>=4 && !(( *(uint32_t*)s | *(uint32_t*)s-0x01010101) & 0x80808080)) {
67 while (wn && *s-1u<0x7f) {
77 if (*s-SA >= SB-SA) goto ilseq;
82 if (OOB(c,*s)) goto ilseq;
83 c = (c<<6) | *s++-0x80;
84 if (!(c&(1U<<31))) break;
86 if (*s-0x80u >= 0x40) goto ilseq;
87 c = (c<<6) | *s++-0x80;
88 if (!(c&(1U<<31))) break;
90 if (*s-0x80u >= 0x40) goto ilseq;
91 c = (c<<6) | *s++-0x80;
94 *ws++ = c; wn--; c = 0;
96 *src = (const void *)s;
99 *src = (const void *)s;
101 /* enter permanently failing state */
102 *(unsigned *)st = FAILSTATE;