1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 01:18:00 +00:00
putty-source/utils/decode_utf8_to_wide_string.c
Simon Tatham 392be3e494 New utility function: decode_utf8_to_wide_string.
We already had encode_wide_string_as_utf8, which treats the wide
string as UTF-16 or UTF-32 as appropriate to the size of wchar_t. I'm
about to need the inverse function, and was surprised that it didn't
already exist (even though enough component parts did to make it easy).
2023-05-29 15:08:49 +01:00

36 lines
1019 B
C

/*
* Decode a string of UTF-8 to a wchar_t string.
*/
#include "misc.h"
wchar_t *decode_utf8_to_wide_string(const char *s)
{
wchar_t *ws = NULL;
size_t wlen = 0, wsize = 0;
BinarySource src[1];
BinarySource_BARE_INIT_PL(src, ptrlen_from_asciz(s));
while (get_avail(src) > 0) {
/*
* decode_utf8_to_wchar might emit up to 2 wchar_t if wchar_t
* is 16 bits (because of UTF-16 surrogates), but will emit at
* most one if wchar_t is 32-bit
*/
sgrowarrayn(ws, wsize, wlen, 1 + (sizeof(wchar_t) < 4));
/* We ignore 'err': if it is set, then the character decode
* function will have emitted U+FFFD REPLACEMENT CHARACTER,
* which is what we'd have done in response anyway. */
DecodeUTF8Failure err;
wlen += decode_utf8_to_wchar(src, ws + wlen, &err);
}
/* Reallocate to the final size and append the trailing NUL */
ws = sresize(ws, wlen + 1, wchar_t);
ws[wlen] = L'\0';
return ws;
}