]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-boot: Add memmem_safe and memory_startswith
authorJan Janssen <medhefgo@web.de>
Fri, 13 Aug 2021 17:03:35 +0000 (19:03 +0200)
committerJan Janssen <medhefgo@web.de>
Mon, 16 Aug 2021 08:50:58 +0000 (10:50 +0200)
src/basic/string-util.h
src/boot/efi/util.c
src/boot/efi/util.h
src/fundamental/string-util-fundamental.h

index 9155e50ba8927c3af2f2d39c40e422638048ef08..5c11adf393f52172bbc4e66f9f8e201967145b84 100644 (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.
  */
index 0c28cab2467d296b756002ef4b5e7fd5e455a3c6..823689e2234e35dac6ca2c21313de698513b3c5e 100644 (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;
+}
index d3bf848a9558a0d6ee6d6fb5c12b1cb71bde163b..f620f14212a171b0ce7bf0c4eeffa12a9d1d8d9e 100644 (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;
+}
index 407cede48d987e6b955f3a5afed7edadfccc4896..7455c05492392c4fd5bf2a838a8b15d43ae0a7a6 100644 (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;
+}