1
0

mbtowc.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. #include <wchar.h>
  7. #include <errno.h>
  8. #include "internal.h"
  9. int mbtowc(wchar_t *restrict wc, const char *restrict src, size_t n)
  10. {
  11. unsigned c;
  12. const unsigned char *s = (const void *)src;
  13. wchar_t dummy;
  14. if (!s) return 0;
  15. if (!n) goto ilseq;
  16. if (!wc) wc = &dummy;
  17. if (*s < 0x80) return !!(*wc = *s);
  18. if (*s-SA > SB-SA) goto ilseq;
  19. c = bittab[*s++-SA];
  20. /* Avoid excessive checks against n: If shifting the state n-1
  21. * times does not clear the high bit, then the value of n is
  22. * insufficient to read a character */
  23. if (n<4 && ((c<<(6*n-6)) & (1U<<31))) goto ilseq;
  24. if (OOB(c,*s)) goto ilseq;
  25. c = c<<6 | *s++-0x80;
  26. if (!(c&(1U<<31))) {
  27. *wc = c;
  28. return 2;
  29. }
  30. if (*s-0x80u >= 0x40) goto ilseq;
  31. c = c<<6 | *s++-0x80;
  32. if (!(c&(1U<<31))) {
  33. *wc = c;
  34. return 3;
  35. }
  36. if (*s-0x80u >= 0x40) goto ilseq;
  37. *wc = c<<6 | *s++-0x80;
  38. return 4;
  39. ilseq:
  40. errno = EILSEQ;
  41. return -1;
  42. }