2e8ec97015d70153e67b5ba05e70ed95bf34be11
[musl] / src / multibyte / c16rtomb.c
1 #include <uchar.h>
2 #include <errno.h>
3 #include <wchar.h>
4
5 size_t c16rtomb(char *restrict s, char16_t c16, mbstate_t *restrict ps)
6 {
7         unsigned *x = (unsigned *)ps;
8         wchar_t wc;
9
10         if (!s) {
11                 if (*x) goto ilseq;
12                 return 1;
13         }
14
15         if (!*x && c16 - 0xd800u < 0x400) {
16                 *x = c16 - 0xd7c0 << 10;
17                 return 0;
18         }
19
20         if (*x) {
21                 if (c16 - 0xdc00u >= 0x400) goto ilseq;
22                 else wc = *x + c16 - 0xdc00;
23                 *x = 0;
24         } else {
25                 wc = c16;
26         }
27         return wcrtomb(s, wc, 0);
28
29 ilseq:
30         *x = 0;
31         errno = EILSEQ;
32         return -1;
33 }