Skip to content

Commit

Permalink
Fallback memmem implementation (#5561)
Browse files Browse the repository at this point in the history
  • Loading branch information
hedara90 authored Oct 25, 2024
2 parents be98de7 + 111a959 commit a9abbdf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tools/mgba-rom-test-hydra/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
22 changes: 22 additions & 0 deletions tools/mgba-rom-test-hydra/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down

0 comments on commit a9abbdf

Please sign in to comment.