Files
sisungo 0cc87ce05e Initial commit
Signed-off-by: sisungo <[email protected]>
2026-11-20 10:24:59 +00:00

237 lines
7.1 KiB
C

/*
* libSemiOSCharsetUTF-16.so
* SemiOS charset library: UTF-16 (native-endian, with BOM)
*/
#include <stdint.h>
#include <stddef.h>
#define SEMIOS_EXPORT __attribute__((visibility("default")))
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define UTF16_NATIVE_BIG_ENDIAN 1
#elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define UTF16_NATIVE_BIG_ENDIAN 0
#else
#error "Unable to determine native byte order"
#endif
static uint32_t utf16_decode(const unsigned char *p, int big_endian) {
if (big_endian) {
return ((uint32_t)p[0] << 8) | (uint32_t)p[1];
}
return (uint32_t)p[0] | ((uint32_t)p[1] << 8);
}
static void utf16_encode(unsigned char *o, uint32_t u, int big_endian) {
if (big_endian) {
o[0] = (unsigned char)((u >> 8) & 0xFFu);
o[1] = (unsigned char)( u & 0xFFu);
} else {
o[0] = (unsigned char)( u & 0xFFu);
o[1] = (unsigned char)((u >> 8) & 0xFFu);
}
}
SEMIOS_EXPORT
int SemiOS_Charset_UTF_16_ToUTF8(
char **restrict in, size_t *restrict inb,
char **restrict out, size_t *restrict outb
) {
int big_endian = UTF16_NATIVE_BIG_ENDIAN;
/* Strip an optional BOM and let it select the byte order. */
if (*inb >= 2) {
const unsigned char *p = (const unsigned char *)*in;
uint32_t mark = utf16_decode(p, 1); /* BOMs are byte-swapped U+FEFF */
if (mark == 0xFEFFu) {
big_endian = 1;
*in += 2;
*inb -= 2;
} else if (mark == 0xFFFEu) {
big_endian = 0;
*in += 2;
*inb -= 2;
}
}
while (*inb >= 2) {
const unsigned char *p = (const unsigned char *)*in;
/* Decode 16-bit code unit in the selected byte order. */
uint32_t u = utf16_decode(p, big_endian);
uint32_t cp;
size_t consumed;
if (u >= 0xD800u && u <= 0xDBFFu) {
/* High surrogate: must be followed by a low surrogate. */
if (*inb < 4) {
return -1;
}
uint32_t lo = utf16_decode(p + 2, big_endian);
if (lo < 0xDC00u || lo > 0xDFFFu) {
return -2;
}
cp = 0x10000u
+ ((u - 0xD800u) << 10)
+ (lo - 0xDC00u);
consumed = 4;
} else if (u >= 0xDC00u && u <= 0xDFFFu) {
/* Unpaired low surrogate. */
return -2;
} else {
cp = u;
consumed = 2;
}
/* Determine UTF-8 length. */
size_t n;
if (cp < 0x80u) n = 1;
else if (cp < 0x800u) n = 2;
else if (cp < 0x10000u) n = 3;
else n = 4;
if (*outb < n) {
return -1;
}
/* Encode into UTF-8. */
unsigned char *o = (unsigned char *)*out;
if (n == 1) {
o[0] = (unsigned char)cp;
} else if (n == 2) {
o[0] = (unsigned char)(0xC0u | (cp >> 6));
o[1] = (unsigned char)(0x80u | (cp & 0x3Fu));
} else if (n == 3) {
o[0] = (unsigned char)(0xE0u | (cp >> 12));
o[1] = (unsigned char)(0x80u | ((cp >> 6) & 0x3Fu));
o[2] = (unsigned char)(0x80u | (cp & 0x3Fu));
} else {
o[0] = (unsigned char)(0xF0u | (cp >> 18));
o[1] = (unsigned char)(0x80u | ((cp >> 12) & 0x3Fu));
o[2] = (unsigned char)(0x80u | ((cp >> 6) & 0x3Fu));
o[3] = (unsigned char)(0x80u | (cp & 0x3Fu));
}
*in += consumed;
*inb -= consumed;
*out += n;
*outb -= n;
}
/* If any input remains, it is a partial 16-bit unit. */
return *inb == 0 ? 0 : -1;
}
SEMIOS_EXPORT
int SemiOS_Charset_UTF_16_FromUTF8(
char **restrict in, size_t *restrict inb,
char **restrict out, size_t *restrict outb
) {
int wrote_bom = 0;
while (*inb > 0) {
const unsigned char *p = (const unsigned char *)*in;
unsigned char b0 = p[0];
uint32_t cp;
size_t n;
if (b0 < 0x80u) {
/* ASCII */
cp = b0;
n = 1;
} else if ((b0 & 0xE0u) == 0xC0u) {
/* 2-byte sequence */
if (*inb < 2) return -1;
unsigned char b1 = p[1];
if ((b1 & 0xC0u) != 0x80u) return -2;
cp = ((uint32_t)(b0 & 0x1Fu) << 6)
| (uint32_t)(b1 & 0x3Fu);
if (cp < 0x80u) return -2; /* overlong */
if (cp >= 0xD800u && cp <= 0xDFFFu) return -2; /* surrogate */
n = 2;
} else if ((b0 & 0xF0u) == 0xE0u) {
/* 3-byte sequence */
if (*inb < 3) return -1;
unsigned char b1 = p[1];
unsigned char b2 = p[2];
if ((b1 & 0xC0u) != 0x80u || (b2 & 0xC0u) != 0x80u) return -2;
cp = ((uint32_t)(b0 & 0x0Fu) << 12)
| ((uint32_t)(b1 & 0x3Fu) << 6)
| (uint32_t)(b2 & 0x3Fu);
if (cp < 0x800u) return -2; /* overlong */
if (cp >= 0xD800u && cp <= 0xDFFFu) return -2; /* surrogate */
n = 3;
} else if ((b0 & 0xF8u) == 0xF0u) {
/* 4-byte sequence */
if (*inb < 4) return -1;
unsigned char b1 = p[1];
unsigned char b2 = p[2];
unsigned char b3 = p[3];
if ((b1 & 0xC0u) != 0x80u ||
(b2 & 0xC0u) != 0x80u ||
(b3 & 0xC0u) != 0x80u) return -2;
cp = ((uint32_t)(b0 & 0x07u) << 18)
| ((uint32_t)(b1 & 0x3Fu) << 12)
| ((uint32_t)(b2 & 0x3Fu) << 6)
| (uint32_t)(b3 & 0x3Fu);
if (cp < 0x10000u) return -2; /* overlong */
if (cp > 0x10FFFFu) return -2; /* out of range */
n = 4;
} else {
return -2; /* invalid lead */
}
size_t need = (wrote_bom ? 0 : 2) + (cp >= 0x10000u ? 4 : 2);
if (*outb < need) {
return -1;
}
unsigned char *o = (unsigned char *)*out;
/* Emit the native-endian BOM before the first character. */
if (!wrote_bom) {
utf16_encode(o, 0xFEFFu, UTF16_NATIVE_BIG_ENDIAN);
o += 2;
*out += 2;
*outb -= 2;
wrote_bom = 1;
}
if (cp >= 0x10000u) {
/* Supplementary code point: encode as a surrogate pair. */
uint32_t v = cp - 0x10000u;
uint32_t hi = 0xD800u + (v >> 10);
uint32_t lo = 0xDC00u + (v & 0x3FFu);
utf16_encode(o, hi, UTF16_NATIVE_BIG_ENDIAN);
utf16_encode(o + 2, lo, UTF16_NATIVE_BIG_ENDIAN);
*out += 4;
*outb -= 4;
} else {
/* BMP code point: encode as a single 16-bit unit. */
utf16_encode(o, cp, UTF16_NATIVE_BIG_ENDIAN);
*out += 2;
*outb -= 2;
}
*in += n;
*inb -= n;
}
return 0;
}