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

@ -5,82 +5,109 @@
#include "strings.h" #include "strings.h"
typedef enum { typedef enum {
ST_START, ST_START,
ST_A, ST_A,
ST_B, ST_B,
ST_C, ST_C,
ST_D, ST_D,
ST_E, ST_E,
ST_F, ST_F,
ST_G, ST_G,
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) { {
case ST_START: switch (state)
if (str[i] <= 0x7F) { {
continue; case ST_START:
} else if (str[i] >= 0xC2 && str[i] <= 0xDF) { if (str[i] <= 0x7F)
state = ST_A; {
} else if (str[i] >= 0xE1 && str[i] <= 0xEC) { continue;
state = ST_B; }
} else if (str[i] >= 0xEE && str[i] <= 0xEF) { else if (str[i] >= 0xC2 && str[i] <= 0xDF)
state = ST_B; {
} else if (str[i] == 0xE0) { state = ST_A;
state = ST_C; }
} else if (str[i] == 0xED) { else if (str[i] >= 0xE1 && str[i] <= 0xEC)
state = ST_D; {
} else if (str[i] >= 0xF1 && str[i] <= 0xF3) { state = ST_B;
state = ST_E; }
} else if (str[i] == 0xF0) { else if (str[i] >= 0xEE && str[i] <= 0xEF)
state = ST_F; {
} else if (str[i] >= 0xF4) { state = ST_B;
state = ST_G; }
} else { else if (str[i] == 0xE0)
state = ST_ERROR; {
} state = ST_C;
break; }
case ST_A: else if (str[i] == 0xED)
state = (str[i] >= 0x80 && str[i] <= 0xBF) ? ST_START : ST_ERROR; {
break; state = ST_D;
case ST_B: }
state = (str[i] >= 0x80 && str[i] <= 0xBF) ? ST_A : ST_ERROR; else if (str[i] >= 0xF1 && str[i] <= 0xF3)
break; {
case ST_C: state = ST_E;
state = (str[i] >= 0xA0 && str[i] <= 0xBF) ? ST_A : ST_ERROR; }
break; else if (str[i] == 0xF0)
case ST_D: {
state = (str[i] >= 0x80 && str[i] <= 0x9F) ? ST_A : ST_ERROR; state = ST_F;
break; }
case ST_E: else if (str[i] >= 0xF4)
state = (str[i] >= 0x80 && str[i] <= 0xBF) ? ST_B : ST_ERROR; {
break; state = ST_G;
case ST_F: }
state = (str[i] >= 0x90 && str[i] <= 0xBF) ? ST_B : ST_ERROR; else
break; {
case ST_G: state = ST_ERROR;
state = (str[i] >= 0x80 && str[i] <= 0x8F) ? ST_B : ST_ERROR; }
break; break;
} case ST_A:
if (state == ST_ERROR) { state = (str[i] >= 0x80 && str[i] <= 0xBF) ? ST_START : ST_ERROR;
return -1; break;
} case ST_B:
} state = (str[i] >= 0x80 && str[i] <= 0xBF) ? ST_A : ST_ERROR;
if (state != ST_START) { break;
return -1; case ST_C:
} else { state = (str[i] >= 0xA0 && str[i] <= 0xBF) ? ST_A : ST_ERROR;
return 0; break;
} case ST_D:
state = (str[i] >= 0x80 && str[i] <= 0x9F) ? ST_A : ST_ERROR;
break;
case ST_E:
state = (str[i] >= 0x80 && str[i] <= 0xBF) ? ST_B : ST_ERROR;
break;
case ST_F:
state = (str[i] >= 0x90 && str[i] <= 0xBF) ? ST_B : ST_ERROR;
break;
case ST_G:
state = (str[i] >= 0x80 && str[i] <= 0x8F) ? ST_B : ST_ERROR;
break;
}
if (state == ST_ERROR)
{
return -1;
}
}
if (state != ST_START)
{
return -1;
}
else
{
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;