1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-25 09:12:24 +00:00
putty-source/utils/decode_utf8_to_wchar.c
Simon Tatham 69e217d23a Make decode_utf8() read from a BinarySource.
This enables it to handle data that isn't presented as a
NUL-terminated string.

In particular, the NUL byte can appear _within_ the string and be
correctly translated to the NUL wide character. So I've been able to
remove the awkwardness in the test rig of having to include the
terminating NUL in every test to ensure NUL has been tested, and
instead, insert a single explicit test for it.

Similarly to the previous commit, the simplification at the (one) call
site gives me a strong feeling of 'this is what the API should have
been all along'!
2022-11-09 19:21:02 +00:00

21 lines
498 B
C

/*
* Decode a single UTF-8 character to the platform's local wchar_t.
*/
#include "putty.h"
#include "misc.h"
size_t decode_utf8_to_wchar(BinarySource *src, wchar_t *out)
{
size_t outlen = 0;
unsigned wc = decode_utf8(src);
if (sizeof(wchar_t) > 2 || wc < 0x10000) {
out[outlen++] = wc;
} else {
unsigned wcoff = wc - 0x10000;
out[outlen++] = 0xD800 | (0x3FF & (wcoff >> 10));
out[outlen++] = 0xDC00 | (0x3FF & wcoff);
}
return outlen;
}