blob: 3c1343aea81f82d30dd055a5406b5ef63267ffc7 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
/*
* This code was written by Rich Felker in 2010; no copyright is claimed.
* This code is in the public domain. Attribution is appreciated but
* unnecessary.
*/
#include <stdint.h>
#include <wchar.h>
#include <errno.h>
#include "internal.h"
size_t mbsrtowcs(wchar_t *restrict ws, const char **restrict src, size_t wn, mbstate_t *restrict st)
{
const unsigned char *s = (const void *)*src;
size_t wn0 = wn;
unsigned c = 0;
if (st && (c = *(unsigned *)st)) {
if (ws) {
*(unsigned *)st = 0;
goto resume;
} else {
goto resume0;
}
}
if (!ws) for (;;) {
if (*s-1u < 0x7f && (uintptr_t)s%4 == 0) {
while (!(( *(uint32_t*)s | *(uint32_t*)s-0x01010101) & 0x80808080)) {
s += 4;
wn -= 4;
}
}
if (*s-1u < 0x7f) {
s++;
wn--;
continue;
}
if (*s-SA > SB-SA) break;
c = bittab[*s++-SA];
resume0:
if (OOB(c,*s)) { s--; break; }
s++;
if (c&(1U<<25)) {
if (*s-0x80u >= 0x40) { s-=2; break; }
s++;
if (c&(1U<<19)) {
if (*s-0x80u >= 0x40) { s-=3; break; }
s++;
}
}
wn--;
c = 0;
} else for (;;) {
if (!wn) {
*src = (const void *)s;
return wn0;
}
if (*s-1u < 0x7f && (uintptr_t)s%4 == 0) {
while (wn>=5 && !(( *(uint32_t*)s | *(uint32_t*)s-0x01010101) & 0x80808080)) {
*ws++ = *s++;
*ws++ = *s++;
*ws++ = *s++;
*ws++ = *s++;
wn -= 4;
}
}
if (*s-1u < 0x7f) {
*ws++ = *s++;
wn--;
continue;
}
if (*s-SA > SB-SA) break;
c = bittab[*s++-SA];
resume:
if (OOB(c,*s)) { s--; break; }
c = (c<<6) | *s++-0x80;
if (c&(1U<<31)) {
if (*s-0x80u >= 0x40) { s-=2; break; }
c = (c<<6) | *s++-0x80;
if (c&(1U<<31)) {
if (*s-0x80u >= 0x40) { s-=3; break; }
c = (c<<6) | *s++-0x80;
}
}
*ws++ = c;
wn--;
c = 0;
}
if (!c && !*s) {
if (ws) {
*ws = 0;
*src = 0;
}
return wn0-wn;
}
errno = EILSEQ;
if (ws) *src = (const void *)s;
return -1;
}
|