}
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.
*/
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;
+}
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;
+}
#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
}
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;
+}