From d1b33eb8bf0f502761844d09dc64803ed5c9e687 Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Tue, 11 Jul 2023 16:45:07 -0700 Subject: [PATCH] shared/util: Introduce strisutf8 This introduces strisutf8 which can be used to verify if a string is encoded using UTF-8 format. --- src/shared/util.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++ src/shared/util.h | 3 +++ 2 files changed, 65 insertions(+) diff --git a/src/shared/util.c b/src/shared/util.c index 4d1c0d005..e9c1c18f5 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -1705,3 +1705,65 @@ int strsuffix(const char *str, const char *suffix) return strncmp(str + len - suffix_len, suffix, suffix_len); } + +char *strstrip(char *str) +{ + size_t size; + char *end; + + if (!str) + return NULL; + + size = strlen(str); + if (!size) + return str; + + end = str + size - 1; + while (end >= str && isspace(*end)) + end--; + *(end + 1) = '\0'; + + while (*str && isspace(*str)) + str++; + + return str; +} + +bool strisutf8(const char *str, size_t len) +{ + size_t i = 0; + + while (i < len) { + unsigned char c = str[i]; + size_t size = 0; + + /* Check the first byte to determine the number of bytes in the + * UTF-8 character. + */ + if ((c & 0x80) == 0x00) + size = 1; + else if ((c & 0xE0) == 0xC0) + size = 2; + else if ((c & 0xF0) == 0xE0) + size = 3; + else if ((c & 0xF8) == 0xF0) + size = 4; + else + /* Invalid UTF-8 sequence */ + return false; + + /* Check the following bytes to ensure they have the correct + * format. + */ + for (size_t j = 1; j < size; ++j) { + if (i + j > len || (str[i + j] & 0xC0) != 0x80) + /* Invalid UTF-8 sequence */ + return false; + } + + /* Move to the next character */ + i += size; + } + + return true; +} diff --git a/src/shared/util.h b/src/shared/util.h index ce57b53be..c37b0f729 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -88,6 +89,8 @@ do { \ char *strdelimit(char *str, char *del, char c); int strsuffix(const char *str, const char *suffix); +char *strstrip(char *str); +bool strisutf8(const char *str, size_t length); void *util_malloc(size_t size); void *util_memdup(const void *src, size_t size); -- 2.47.3