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
| | #include <stdlib.h>
#include <wchar.h>
#include <uchar.h>
#include <errno.h>
#include "internal.h"
__attribute__((__noinline__))
static size_t mbrtoc8_slow(unsigned char *__restrict pc8, const char *__restrict src, size_t n, mbstate_t *__restrict st)
{
static unsigned internal_state;
wchar_t wc;
unsigned* pending = st ? (void*)st : &internal_state;
// The high bit is set if there is still missing input. If it
// is not set and the value is not zero, a previous call has
// stacked the missing output bytes withing the state.
if ((-*pending) > INT_MIN) {
if (pc8) *pc8 = *pending;
(*pending) >>= 8;
return -3;
}
// mbrtowc has return values -2, -1, 0, 1, ..., 4.
size_t res = mbrtowc(&wc, src, n, (void*)pending);
if (res <= 4) {
_Alignas(unsigned) unsigned char s[8] = { 0 };
// Write the result bytes to s over the word boundary
// as we need it. Our wcrtomb implementation ignores
// the state variable, there will be no errors and the
// return value can be ignored.
wcrtomb((void*)(s+3), wc, 0);
// Depending on the endianess this maybe a single load
// instruction. We want to be sure that the bytes are
// in this order, and that the high-order byte is 0.
*pending = (0u|s[4])<<000 | (0u|s[5])<<010 | (0u|s[6])<<020 | (0u|s[7])<<030;
if (pc8) *pc8 = s[3];
}
return res;
}
static size_t __mbrtoc8(unsigned char *__restrict pc8, const char *__restrict src, size_t n, mbstate_t *__restrict st)
{
unsigned char *s = (void*)src;
if (st && !*(unsigned*)st && (s[0] < 0x80u)) {
if (pc8) *pc8 = s[0];
return s[0]>0;
}
return mbrtoc8_slow(pc8, src, n, st);
}
weak_alias(__mbrtoc8, mbrtoc8);
|