module-avb: format strings.c

This commit is contained in:
Nils Tonnaett 2026-05-13 21:22:54 -07:00 committed by Wim Taymans
parent ef77d995cd
commit c9ba3ced91

View file

@ -16,31 +16,53 @@ typedef enum {
ST_ERROR, ST_ERROR,
} UTF8_STATE; } UTF8_STATE;
int validate_utf8(uint8_t *str, size_t len) { int validate_utf8 (uint8_t *str, size_t len)
{
UTF8_STATE state = ST_START; UTF8_STATE state = ST_START;
for (int i = 0; i < len; ++i) { for (int i = 0; i < len; ++i)
switch (state) { {
switch (state)
{
case ST_START: case ST_START:
if (str[i] <= 0x7F) { if (str[i] <= 0x7F)
{
continue; continue;
} else if (str[i] >= 0xC2 && str[i] <= 0xDF) { }
else if (str[i] >= 0xC2 && str[i] <= 0xDF)
{
state = ST_A; state = ST_A;
} else if (str[i] >= 0xE1 && str[i] <= 0xEC) { }
else if (str[i] >= 0xE1 && str[i] <= 0xEC)
{
state = ST_B; state = ST_B;
} else if (str[i] >= 0xEE && str[i] <= 0xEF) { }
else if (str[i] >= 0xEE && str[i] <= 0xEF)
{
state = ST_B; state = ST_B;
} else if (str[i] == 0xE0) { }
else if (str[i] == 0xE0)
{
state = ST_C; state = ST_C;
} else if (str[i] == 0xED) { }
else if (str[i] == 0xED)
{
state = ST_D; state = ST_D;
} else if (str[i] >= 0xF1 && str[i] <= 0xF3) { }
else if (str[i] >= 0xF1 && str[i] <= 0xF3)
{
state = ST_E; state = ST_E;
} else if (str[i] == 0xF0) { }
else if (str[i] == 0xF0)
{
state = ST_F; state = ST_F;
} else if (str[i] >= 0xF4) { }
else if (str[i] >= 0xF4)
{
state = ST_G; state = ST_G;
} else { }
else
{
state = ST_ERROR; state = ST_ERROR;
} }
break; break;
@ -66,21 +88,26 @@ int validate_utf8(uint8_t *str, size_t len) {
state = (str[i] >= 0x80 && str[i] <= 0x8F) ? ST_B : ST_ERROR; state = (str[i] >= 0x80 && str[i] <= 0x8F) ? ST_B : ST_ERROR;
break; break;
} }
if (state == ST_ERROR) { if (state == ST_ERROR)
{
return -1; return -1;
} }
} }
if (state != ST_START) { if (state != ST_START)
{
return -1; return -1;
} else { }
else
{
return 0; return 0;
} }
} }
int check_zero_padding(uint8_t const *str, size_t len) int check_zero_padding (uint8_t const *str, size_t len)
{ {
size_t str_len = strnlen((char *)str, len); size_t str_len = strnlen ((char *)str, len);
/* String doesn't need to be null-terminated. Return success if there is no null in str */ /* String doesn't need to be null-terminated. Return success if there is no
* null in str */
if (str_len == len) if (str_len == len)
{ {
return 0; return 0;