X-Git-Url: https://pd.if.org/git/?p=pdclib;a=blobdiff_plain;f=opt%2Fbasecodecs%2F_PDCLIB_utf8.c;h=a4c19adb349a735c6715af6c66641eab61de66b3;hp=65f76e01470ba3c337382e15c322e847531ffd6d;hb=da0f3f353d417fed71f358a48d5d5394145e460d;hpb=0a419d48138f1411d6e3e50a367b9ece5a2cf893 diff --git a/opt/basecodecs/_PDCLIB_utf8.c b/opt/basecodecs/_PDCLIB_utf8.c index 65f76e0..a4c19ad 100644 --- a/opt/basecodecs/_PDCLIB_utf8.c +++ b/opt/basecodecs/_PDCLIB_utf8.c @@ -9,7 +9,7 @@ #include #include #include -#include <_PDCLIB_encoding.h> +#include "_PDCLIB_encoding.h" /* Use of the mbstate: * @@ -17,6 +17,9 @@ * _St32[1] is the character accumulated so far */ +static bool utf8_mbsinit( const mbstate_t *p_s ) +{ return p_s->_StUC[0] == 0; } + enum { DecStart = 0, @@ -52,15 +55,16 @@ end_conversion: \ _PDCLIB_UNDEFINED(accum); \ state = DecStart; \ } while(0) + #define CHECK_CONTINUATION \ do { if((c & 0xC0) != 0x80) return false; } while(0) static bool utf8toc32( - char32_t **restrict p_outbuf, - size_t *restrict p_outsz, - const char **restrict p_inbuf, - size_t *restrict p_insz, - mbstate_t *restrict p_s + char32_t *restrict *restrict p_outbuf, + size_t *restrict p_outsz, + const char *restrict *restrict p_inbuf, + size_t *restrict p_insz, + mbstate_t *restrict p_s ) { START_CONVERSION @@ -69,7 +73,7 @@ static bool utf8toc32( char32_t c32; switch(state) { case DecStart: - // 1 byte + // 1 byte if(c <= 0x7F) { OUT32(c); } else if(c <= 0xDF) { @@ -152,7 +156,7 @@ static bool utf8toc32( } (*p_inbuf)++; - (*p_insz)--; + (*p_insz)--; } END_CONVERSION; } @@ -165,16 +169,16 @@ enum { }; static bool c32toutf8( - char **restrict p_outbuf, - size_t *restrict p_outsz, - const char32_t **restrict p_inbuf, - size_t *restrict p_insz, - mbstate_t *restrict p_s + char *restrict *restrict p_outbuf, + size_t *restrict p_outsz, + const char32_t *restrict *restrict p_inbuf, + size_t *restrict p_insz, + mbstate_t *restrict p_s ) { START_CONVERSION while(*p_outsz) { - unsigned char outc; + unsigned char outc = 0; switch(state) { case Enc3R: outc = 0x80 | ((accum >> 12) & 0x3F); @@ -221,38 +225,40 @@ static bool c32toutf8( if(p_outbuf) { **p_outbuf = outc; - (*p_outbuf)++; + (*p_outbuf)++; } - (*p_outsz)--; + (*p_outsz)--; } END_CONVERSION; } -_PDCLIB_charcodec_t _PDCLIB_utf8_codec = { +const struct _PDCLIB_charcodec_t _PDCLIB_utf8_codec = { + .__mbsinit = utf8_mbsinit, .__mbstoc32s = utf8toc32, .__c32stombs = c32toutf8, + .__mb_max = 4, }; #endif #ifdef TEST -#include <_PDCLIB_test.h> +#include "_PDCLIB_test.h" int main( void ) { #ifndef REGTEST // Valid conversion & back - static const char* input = "abcde" "\xDF\xBF" "\xEF\xBF\xBF" + static const char* input = "abcde" "\xDF\xBF" "\xEF\xBF\xBF" "\xF4\x8F\xBF\xBF"; char32_t c32out[8]; - char32_t *c32ptr = &c32out[0]; - size_t c32rem = 8; - char *chrptr = (char*) &input[0]; - size_t chrrem = strlen(input); - mbstate_t mbs = { 0 }; + char32_t *c32ptr = &c32out[0]; + size_t c32rem = 8; + const char *chrptr = (char*) &input[0]; + size_t chrrem = strlen(input); + mbstate_t mbs = { 0 }; TESTCASE(utf8toc32(&c32ptr, &c32rem, &chrptr, &chrrem, &mbs)); TESTCASE(c32rem == 0);