mbsnrtowcs.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. size_t mbsnrtowcs(wchar_t *restrict wcs, const char **restrict src, size_t n, size_t wn, mbstate_t *restrict st)
  8. {
  9. size_t l, cnt=0, n2;
  10. wchar_t *ws, wbuf[256];
  11. const char *s = *src;
  12. if (!wcs) ws = wbuf, wn = sizeof wbuf / sizeof *wbuf;
  13. else ws = wcs;
  14. /* making sure output buffer size is at most n/4 will ensure
  15. * that mbsrtowcs never reads more than n input bytes. thus
  16. * we can use mbsrtowcs as long as it's practical.. */
  17. while ( s && wn && ( (n2=n/4)>=wn || n2>32 ) ) {
  18. if (n2>=wn) n2=wn;
  19. n -= n2;
  20. l = mbsrtowcs(ws, &s, n2, st);
  21. if (!(l+1)) {
  22. cnt = l;
  23. wn = 0;
  24. break;
  25. }
  26. if (ws != wbuf) {
  27. ws += l;
  28. wn -= l;
  29. }
  30. cnt += l;
  31. }
  32. if (s) while (wn && n) {
  33. l = mbrtowc(ws, s, n, st);
  34. if (l+2<=2) {
  35. if (!(l+1)) {
  36. cnt = l;
  37. break;
  38. }
  39. if (!l) {
  40. s = 0;
  41. break;
  42. }
  43. /* have to roll back partial character */
  44. *(unsigned *)st = 0;
  45. break;
  46. }
  47. s += l; n -= l;
  48. /* safe - this loop runs fewer than sizeof(wbuf)/8 times */
  49. ws++; wn--;
  50. cnt++;
  51. }
  52. if (wcs) *src = s;
  53. return cnt;
  54. }