iconv: add aliases for GBK
[musl.git] / src / multibyte / c16rtomb.c
blob39ca3758fa16eb16d12f468046045f0f425e5d1c
1 #include <uchar.h>
2 #include <errno.h>
3 #include <wchar.h>
5 size_t c16rtomb(char *restrict s, char16_t c16, mbstate_t *restrict ps)
7 static unsigned internal_state;
8 if (!ps) ps = (void *)&internal_state;
9 unsigned *x = (unsigned *)ps;
10 wchar_t wc;
12 if (!s) {
13 if (*x) goto ilseq;
14 return 1;
17 if (!*x && c16 - 0xd800u < 0x400) {
18 *x = c16 - 0xd7c0 << 10;
19 return 0;
22 if (*x) {
23 if (c16 - 0xdc00u >= 0x400) goto ilseq;
24 else wc = *x + c16 - 0xdc00;
25 *x = 0;
26 } else {
27 wc = c16;
29 return wcrtomb(s, wc, 0);
31 ilseq:
32 *x = 0;
33 errno = EILSEQ;
34 return -1;