dup_mb_to_wc.c 1014 B

1234567891011121314151617181920212223242526272829303132333435
  1. /*
  2. * dup_mb_to_wc: memory-allocating wrapper on mb_to_wc.
  3. *
  4. * Also dup_mb_to_wc_c: same but you already know the length of the
  5. * string, and you get told the length of the returned wide string.
  6. * (But it's still NUL-terminated, for convenience.)
  7. */
  8. #include "putty.h"
  9. #include "misc.h"
  10. wchar_t *dup_mb_to_wc_c(int codepage, int flags, const char *string,
  11. size_t inlen, size_t *outlen_p)
  12. {
  13. assert(inlen <= INT_MAX);
  14. size_t mult;
  15. for (mult = 1 ;; mult++) {
  16. wchar_t *ret = snewn(mult*inlen + 2, wchar_t);
  17. size_t outlen = mb_to_wc(codepage, flags, string, inlen, ret,
  18. mult*inlen + 1);
  19. if (outlen < mult*inlen+1) {
  20. if (outlen_p)
  21. *outlen_p = outlen;
  22. ret[outlen] = L'\0';
  23. return ret;
  24. }
  25. sfree(ret);
  26. }
  27. }
  28. wchar_t *dup_mb_to_wc(int codepage, int flags, const char *string)
  29. {
  30. return dup_mb_to_wc_c(codepage, flags, string, strlen(string), NULL);
  31. }