fix non-static dummy function that slipped in with locale implementation
[musl] / src / multibyte / mbrtowc.c
1 /* 
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
4  * unnecessary.
5  */
6
7 #include <wchar.h>
8 #include <errno.h>
9 #include "internal.h"
10
11 size_t mbrtowc(wchar_t *restrict wc, const char *restrict src, size_t n, mbstate_t *restrict st)
12 {
13         static unsigned internal_state;
14         unsigned c;
15         const unsigned char *s = (const void *)src;
16         const unsigned N = n;
17         wchar_t dummy;
18
19         if (!st) st = (void *)&internal_state;
20         c = *(unsigned *)st;
21         
22         if (!s) {
23                 if (c) goto ilseq;
24                 return 0;
25         } else if (!wc) wc = &dummy;
26
27         if (!n) return -2;
28         if (!c) {
29                 if (*s < 0x80) return !!(*wc = *s);
30                 if (*s-SA > SB-SA) goto ilseq;
31                 c = bittab[*s++-SA]; n--;
32         }
33
34         if (n) {
35                 if (OOB(c,*s)) goto ilseq;
36 loop:
37                 c = c<<6 | *s++-0x80; n--;
38                 if (!(c&(1U<<31))) {
39                         *(unsigned *)st = 0;
40                         *wc = c;
41                         return N-n;
42                 }
43                 if (n) {
44                         if (*s-0x80u >= 0x40) goto ilseq;
45                         goto loop;
46                 }
47         }
48
49         *(unsigned *)st = c;
50         return -2;
51 ilseq:
52         *(unsigned *)st = 0;
53         errno = EILSEQ;
54         return -1;
55 }