sd-boot: Add memmem_safe and memory_startswith

This commit is contained in:
Jan Janssen
2021-08-16 10:50:58 +02:00
parent 12450f2e77
commit 43ee1fe086
4 changed files with 38 additions and 16 deletions
-16
View File
@@ -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.
*/
+14
View File
@@ -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;
}
+7
View File
@@ -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;
}
+17
View File
@@ -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;
}