From: Jan Janssen Date: Fri, 13 Aug 2021 17:03:35 +0000 (+0200) Subject: sd-boot: Add memmem_safe and memory_startswith X-Git-Tag: v250-rc1~829^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=43ee1fe0868ff611636c3aca7746cec80bc3c448;p=thirdparty%2Fsystemd.git sd-boot: Add memmem_safe and memory_startswith --- diff --git a/src/basic/string-util.h b/src/basic/string-util.h index 9155e50ba89..5c11adf393f 100644 --- a/src/basic/string-util.h +++ b/src/basic/string-util.h @@ -189,22 +189,6 @@ static inline void strncpy_exact(char *buf, const char *src, size_t buf_len) { } REENABLE_WARNING; -/* Like startswith(), but operates on arbitrary memory blocks */ -static inline void *memory_startswith(const void *p, size_t sz, const char *token) { - assert(token); - - size_t n = strlen(token); - if (sz < n) - return NULL; - - assert(p); - - if (memcmp(p, token, n) != 0) - return NULL; - - return (uint8_t*) p + n; -} - /* Like startswith_no_case(), but operates on arbitrary memory blocks. * It works only for ASCII strings. */ diff --git a/src/boot/efi/util.c b/src/boot/efi/util.c index 0c28cab2467..823689e2234 100644 --- a/src/boot/efi/util.c +++ b/src/boot/efi/util.c @@ -501,3 +501,17 @@ EFI_STATUS log_oom(void) { log_error_stall(L"Out of memory."); return EFI_OUT_OF_RESOURCES; } + +VOID *memmem_safe(const VOID *haystack, UINTN haystack_len, const VOID *needle, UINTN needle_len) { + assert(haystack || haystack_len == 0); + assert(needle || needle_len == 0); + + if (needle_len == 0) + return (VOID*)haystack; + + for (const CHAR8 *h = haystack, *n = needle; haystack_len >= needle_len; h++, haystack_len--) + if (*h == *n && CompareMem(h + 1, n + 1, needle_len - 1) == 0) + return (VOID*)h; + + return NULL; +} diff --git a/src/boot/efi/util.h b/src/boot/efi/util.h index d3bf848a955..f620f14212a 100644 --- a/src/boot/efi/util.h +++ b/src/boot/efi/util.h @@ -84,3 +84,10 @@ EFI_STATUS log_oom(void); log_error_stall(fmt, ##__VA_ARGS__); \ err; \ }) + +VOID *memmem_safe(const VOID *haystack, UINTN haystack_len, const VOID *needle, UINTN needle_len); + +static inline VOID *mempmem_safe(const VOID *haystack, UINTN haystack_len, const VOID *needle, UINTN needle_len) { + CHAR8 *p = memmem_safe(haystack, haystack_len, needle, needle_len); + return p ? p + needle_len : NULL; +} diff --git a/src/fundamental/string-util-fundamental.h b/src/fundamental/string-util-fundamental.h index 407cede48d9..7455c054923 100644 --- a/src/fundamental/string-util-fundamental.h +++ b/src/fundamental/string-util-fundamental.h @@ -16,6 +16,7 @@ #define strncmp(a, b, n) StrnCmp((a), (b), (n)) #define strcasecmp(a, b) StriCmp((a), (b)) #define STR_C(str) (L ## str) +#define memcmp(a, b, n) CompareMem(a, b, n) #else #define STR_C(str) (str) #endif @@ -65,3 +66,19 @@ static inline const sd_char *yes_no(sd_bool b) { } sd_int strverscmp_improved(const sd_char *a, const sd_char *b); + +/* Like startswith(), but operates on arbitrary memory blocks */ +static inline void *memory_startswith(const void *p, sd_size_t sz, const sd_char *token) { + assert(token); + + sd_size_t n = strlen(token) * sizeof(sd_char); + if (sz < n) + return NULL; + + assert(p); + + if (memcmp(p, token, n) != 0) + return NULL; + + return (uint8_t*) p + n; +}