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 #ifdef I_FAILED_TO_RTFM_RFC3629
46 if (*s++-0x80u >= 0x40) goto ilseq2;
47 c <<= 6; if (!(c&(1U<<31))) break;
48 if (*s++-0x80u >= 0x40) goto ilseq2;
49 c <<= 6; if (!(c&(1U<<31))) break;
51 if (*s++-0x80u >= 0x40) goto ilseq2;
52 c <<= 6; if (!(c&(1U<<31))) break;
53 if (*s++-0x80u >= 0x40) goto ilseq2;
61 while (((uintptr_t)s&3) && *s-1u<0x7f) {
65 while (wn>=4 && !(( *(uint32_t*)s | *(uint32_t*)s-0x01010101) & 0x80808080)) {
73 while (wn && *s-1u<0x7f) {
83 if (*s-SA >= SB-SA) goto ilseq;
88 if (OOB(c,*s)) goto ilseq;
89 c = (c<<6) | *s++-0x80;
90 if (!(c&(1U<<31))) break;
92 #ifdef I_FAILED_TO_RTFM_RFC3629
93 if (*s-0x80u >= 0x40) goto ilseq;
94 c = (c<<6) | *s++-0x80;
95 if (!(c&(1U<<31))) break;
97 if (*s-0x80u >= 0x40) goto ilseq;
98 c = (c<<6) | *s++-0x80;
99 if (!(c&(1U<<31))) break;
102 if (*s-0x80u >= 0x40) goto ilseq;
103 c = (c<<6) | *s++-0x80;
104 if (!(c&(1U<<31))) break;
106 if (*s-0x80u >= 0x40) goto ilseq;
107 c = (c<<6) | *s++-0x80;
110 *ws++ = c; wn--; c = 0;
112 *src = (const void *)s;
115 *src = (const void *)s;
117 /* enter permanently failing state */
118 *(unsigned *)st = FAILSTATE;