From 111a95951cdb78a5d9f8464ede89368e83f834c6 Mon Sep 17 00:00:00 2001 From: Martin Griffin Date: Tue, 22 Oct 2024 13:25:46 +0100 Subject: [PATCH] Fallback memmem implementation --- tools/mgba-rom-test-hydra/Makefile | 2 +- tools/mgba-rom-test-hydra/main.c | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/tools/mgba-rom-test-hydra/Makefile b/tools/mgba-rom-test-hydra/Makefile index f93f991d9b38..ef0523adc9d1 100644 --- a/tools/mgba-rom-test-hydra/Makefile +++ b/tools/mgba-rom-test-hydra/Makefile @@ -12,7 +12,7 @@ all: mgba-rom-test-hydra$(EXE) @: mgba-rom-test-hydra$(EXE): $(SRCS) - $(CC) $(SRCS) -o $@ -lm $(LDFLAGS) + $(CC) $(SRCS) -Werror=implicit-function-declaration -o $@ -lm $(LDFLAGS) clean: $(RM) mgba-rom-test-hydra$(EXE) diff --git a/tools/mgba-rom-test-hydra/main.c b/tools/mgba-rom-test-hydra/main.c index 841b7328e5fa..d4c0f4e080cc 100644 --- a/tools/mgba-rom-test-hydra/main.c +++ b/tools/mgba-rom-test-hydra/main.c @@ -106,6 +106,28 @@ static const struct Symbol *lookup_address(uint32_t address) return NULL; } +#ifndef _GNU_SOURCE +// Very naive implementation of 'memmem' for systems which don't make it +// available by default. +void *memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) +{ + const char *haystack_ = haystack; + const char *needle_ = needle; + for (size_t i = 0; i < haystacklen - needlelen; i++) + { + size_t j; + for (j = 0; j < needlelen; j++) + { + if (haystack_[i+j] != needle_[j]) + break; + } + if (j == needlelen) + return (void *)&haystack_[i]; + } + return NULL; +} +#endif + // Similar to 'fwrite(buffer, 1, size, f)' except that anything which // looks like the output of '%p' (i.e. '<0x\d{7}>') is translated into // the name of a symbol (if it represents one).