encode_wide_string_as_utf8.c 571 B

123456789101112131415161718192021222324
  1. /*
  2. * Encode a string of wchar_t as UTF-8.
  3. */
  4. #include "putty.h"
  5. #include "misc.h"
  6. char *encode_wide_string_as_utf8(const wchar_t *ws)
  7. {
  8. strbuf *sb = strbuf_new();
  9. while (*ws) {
  10. unsigned long ch = *ws++;
  11. if (sizeof(wchar_t) == 2 && IS_HIGH_SURROGATE(ch) &&
  12. IS_LOW_SURROGATE(*ws)) {
  13. ch = FROM_SURROGATES(ch, *ws);
  14. ws++;
  15. } else if (IS_SURROGATE(ch)) {
  16. ch = 0xfffd; /* illegal UTF-16 -> REPLACEMENT CHARACTER */
  17. }
  18. put_utf8_char(sb, ch);
  19. }
  20. return strbuf_to_str(sb);
  21. }