libunic/fromchar16_unicode.c

53 lines
1.2 KiB
C

/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "build.h"
const char16_t* fromchar16_unicode(unsigned int* uc, const char16_t* s) {
unsigned char n;
unsigned short* p;
unsigned int v;
*uc = n = 0;
p = (void*)s;
memcpy(&v, s, 4);
if (IS_LITTLE_ENDIAN) {
if ((v&0xfc00fc00) == 0xdc00d800) {
*uc = *(p++)&0x03ff;
*uc <<= 10;
*uc |= (*p&0x03ff);
*uc += 0x010000;
n = 2;
} else if ((v&0x0000f800) != 0x0000d800) {
*uc = *p;
n = 2;
}
} else {
if ((v&0xfc00fc00) == 0xd800dc00) {
*uc = *(p++)&0x03ff;
*uc <<= 10;
*uc |= (*p&0x03ff);
*uc += 0x010000;
n = 2;
} else if ((v&0xf8000000) != 0xd8000000) {
*uc = *p;
n = 1;
}
}
#ifndef UNICODE_CHARSIZE_CHECK_DISABLE
if (n == 1 && *uc >= 0xd800 && *uc <= 0xdfff)
return (void*)(size_t)(*uc = 0);
#endif
if (n) return s + n;
return (void*)0;
}