| OLD | NEW |
| 1 /* | 1 /* |
| 2 * This code was written by Rich Felker in 2010; no copyright is claimed. | 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 | 3 * This code is in the public domain. Attribution is appreciated but |
| 4 * unnecessary. | 4 * unnecessary. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <wchar.h> | 8 #include <wchar.h> |
| 9 #include <errno.h> | 9 #include <errno.h> |
| 10 #include "internal.h" | 10 #include "internal.h" |
| 11 | 11 |
| 12 int mbtowc(wchar_t *restrict wc, const char *restrict src, size_t n) | 12 int mbtowc(wchar_t* restrict wc, const char* restrict src, size_t n) { |
| 13 { | 13 unsigned c; |
| 14 » unsigned c; | 14 const unsigned char* s = (const void*)src; |
| 15 » const unsigned char *s = (const void *)src; | 15 wchar_t dummy; |
| 16 » wchar_t dummy; | |
| 17 | 16 |
| 18 » if (!s) return 0; | 17 if (!s) |
| 19 » if (!n) goto ilseq; | 18 return 0; |
| 20 » if (!wc) wc = &dummy; | 19 if (!n) |
| 20 goto ilseq; |
| 21 if (!wc) |
| 22 wc = &dummy; |
| 21 | 23 |
| 22 » if (*s < 0x80) return !!(*wc = *s); | 24 if (*s < 0x80) |
| 23 » if (MB_CUR_MAX==1) return (*wc = CODEUNIT(*s)), 1; | 25 return !!(*wc = *s); |
| 24 » if (*s-SA > SB-SA) goto ilseq; | 26 if (MB_CUR_MAX == 1) |
| 25 » c = bittab[*s++-SA]; | 27 return (*wc = CODEUNIT(*s)), 1; |
| 28 if (*s - SA > SB - SA) |
| 29 goto ilseq; |
| 30 c = bittab[*s++ - SA]; |
| 26 | 31 |
| 27 » /* Avoid excessive checks against n: If shifting the state n-1 | 32 /* Avoid excessive checks against n: If shifting the state n-1 |
| 28 » * times does not clear the high bit, then the value of n is | 33 * times does not clear the high bit, then the value of n is |
| 29 » * insufficient to read a character */ | 34 * insufficient to read a character */ |
| 30 » if (n<4 && ((c<<(6*n-6)) & (1U<<31))) goto ilseq; | 35 if (n < 4 && ((c << (6 * n - 6)) & (1U << 31))) |
| 36 goto ilseq; |
| 31 | 37 |
| 32 » if (OOB(c,*s)) goto ilseq; | 38 if (OOB(c, *s)) |
| 33 » c = c<<6 | *s++-0x80; | 39 goto ilseq; |
| 34 » if (!(c&(1U<<31))) { | 40 c = c << 6 | *s++ - 0x80; |
| 35 » » *wc = c; | 41 if (!(c & (1U << 31))) { |
| 36 » » return 2; | 42 *wc = c; |
| 37 » } | 43 return 2; |
| 44 } |
| 38 | 45 |
| 39 » if (*s-0x80u >= 0x40) goto ilseq; | 46 if (*s - 0x80u >= 0x40) |
| 40 » c = c<<6 | *s++-0x80; | 47 goto ilseq; |
| 41 » if (!(c&(1U<<31))) { | 48 c = c << 6 | *s++ - 0x80; |
| 42 » » *wc = c; | 49 if (!(c & (1U << 31))) { |
| 43 » » return 3; | 50 *wc = c; |
| 44 » } | 51 return 3; |
| 52 } |
| 45 | 53 |
| 46 » if (*s-0x80u >= 0x40) goto ilseq; | 54 if (*s - 0x80u >= 0x40) |
| 47 » *wc = c<<6 | *s++-0x80; | 55 goto ilseq; |
| 48 » return 4; | 56 *wc = c << 6 | *s++ - 0x80; |
| 57 return 4; |
| 49 | 58 |
| 50 ilseq: | 59 ilseq: |
| 51 » errno = EILSEQ; | 60 errno = EILSEQ; |
| 52 » return -1; | 61 return -1; |
| 53 } | 62 } |
| OLD | NEW |