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

212 lines
6.5 KiB
C

/*
* libSemiOSCharsetUTF-32.so
* SemiOS charset library: UTF-32 (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 UTF32_NATIVE_BIG_ENDIAN 1
#elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define UTF32_NATIVE_BIG_ENDIAN 0
#else
#error "Unable to determine native byte order"
#endif
static uint32_t utf32_decode(const unsigned char *p, int big_endian) {
if (big_endian) {
return ((uint32_t)p[0] << 24)
| ((uint32_t)p[1] << 16)
| ((uint32_t)p[2] << 8)
| (uint32_t)p[3];
}
return (uint32_t)p[0]
| ((uint32_t)p[1] << 8)
| ((uint32_t)p[2] << 16)
| ((uint32_t)p[3] << 24);
}
static void utf32_encode(unsigned char *o, uint32_t cp, int big_endian) {
if (big_endian) {
o[0] = (unsigned char)((cp >> 24) & 0xFFu);
o[1] = (unsigned char)((cp >> 16) & 0xFFu);
o[2] = (unsigned char)((cp >> 8) & 0xFFu);
o[3] = (unsigned char)( cp & 0xFFu);
} else {
o[0] = (unsigned char)( cp & 0xFFu);
o[1] = (unsigned char)((cp >> 8) & 0xFFu);
o[2] = (unsigned char)((cp >> 16) & 0xFFu);
o[3] = (unsigned char)((cp >> 24) & 0xFFu);
}
}
SEMIOS_EXPORT
int SemiOS_Charset_UTF_32_ToUTF8(
char **restrict in, size_t *restrict inb,
char **restrict out, size_t *restrict outb
) {
int big_endian = UTF32_NATIVE_BIG_ENDIAN;
/* Strip an optional BOM and let it select the byte order. */
if (*inb >= 4) {
const unsigned char *p = (const unsigned char *)*in;
uint32_t mark = utf32_decode(p, 1); /* BOMs are byte-swapped U+FEFF */
if (mark == 0x0000FEFFu) {
big_endian = 1;
*in += 4;
*inb -= 4;
} else if (mark == 0xFFFE0000u) {
big_endian = 0;
*in += 4;
*inb -= 4;
}
}
while (*inb >= 4) {
const unsigned char *p = (const unsigned char *)*in;
/* Decode 32-bit code unit in the selected byte order. */
uint32_t cp = utf32_decode(p, big_endian);
/* Reject surrogates and out-of-Unicode-range values. */
if (cp > 0x10FFFFu || (cp >= 0xD800u && cp <= 0xDFFFu)) {
return -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 += 4;
*inb -= 4;
*out += n;
*outb -= n;
}
/* If any input remains, it is a partial 32-bit unit. */
return *inb == 0 ? 0 : -1;
}
SEMIOS_EXPORT
int SemiOS_Charset_UTF_32_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 ? 4 : 8;
if (*outb < need) {
return -1;
}
unsigned char *o = (unsigned char *)*out;
/* Emit the native-endian BOM before the first character. */
if (!wrote_bom) {
utf32_encode(o, 0x0000FEFFu, UTF32_NATIVE_BIG_ENDIAN);
o += 4;
*out += 4;
*outb -= 4;
wrote_bom = 1;
}
/* Encode as native-endian 32-bit unit. */
utf32_encode(o, cp, UTF32_NATIVE_BIG_ENDIAN);
*in += n;
*inb -= n;
*out += 4;
*outb -= 4;
}
return 0;
}