decode_utf8_to_wide_string.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. * Decode a string of UTF-8 to a wchar_t string.
  3. */
  4. #include "misc.h"
  5. wchar_t *decode_utf8_to_wide_string(const char *s)
  6. {
  7. wchar_t *ws = NULL;
  8. size_t wlen = 0, wsize = 0;
  9. BinarySource src[1];
  10. BinarySource_BARE_INIT_PL(src, ptrlen_from_asciz(s));
  11. while (get_avail(src) > 0) {
  12. /*
  13. * decode_utf8_to_wchar might emit up to 2 wchar_t if wchar_t
  14. * is 16 bits (because of UTF-16 surrogates), but will emit at
  15. * most one if wchar_t is 32-bit
  16. */
  17. sgrowarrayn(ws, wsize, wlen, 1 + (sizeof(wchar_t) < 4));
  18. /* We ignore 'err': if it is set, then the character decode
  19. * function will have emitted U+FFFD REPLACEMENT CHARACTER,
  20. * which is what we'd have done in response anyway. */
  21. { // WINSCP
  22. DecodeUTF8Failure err;
  23. wlen += decode_utf8_to_wchar(src, ws + wlen, &err);
  24. } // WINSCP
  25. }
  26. /* Reallocate to the final size and append the trailing NUL */
  27. ws = sresize(ws, wlen + 1, wchar_t);
  28. ws[wlen] = L'\0';
  29. return ws;
  30. }