#include <locale.h>
#include <wchar.h>
#include <xlocale.h>
#include <uchar.h>
#include "mblocal.h"
#include "unicode.h"
#include <sys/debug.h>
CTASSERT(sizeof (_CHAR16State) <= sizeof (mbstate_t));
static mbstate_t mbrtoc16_state;
size_t
mbrtoc16(char16_t *restrict pc16, const char *restrict str, size_t len,
mbstate_t *restrict ps)
{
wchar_t wc;
size_t ret;
char16_t out;
_CHAR16State *c16s;
if (ps == NULL) {
ps = &mbrtoc16_state;
}
if (str == NULL) {
pc16 = NULL;
str = "";
len = 1;
}
c16s = (_CHAR16State *)ps;
if (c16s->c16_surrogate != 0) {
if (pc16 != NULL) {
*pc16 = c16s->c16_surrogate;
}
c16s->c16_surrogate = 0;
return ((size_t)-3);
}
ret = mbrtowc_l(&wc, str, len, ps, uselocale(NULL));
if ((ssize_t)ret < 0) {
return (ret);
}
if (wc >= UNICODE_SUP_START) {
wc -= UNICODE_SUP_START;
c16s->c16_surrogate = UNICODE_SUR_LOWER | UNICODE_SUR_LMASK(wc);
out = UNICODE_SUR_UPPER | UNICODE_SUR_UMASK(wc);
} else {
out = (char16_t)wc;
}
if (pc16 != NULL) {
*pc16 = out;
}
return (ret);
}