scummvm random work
[patches.git] / linux-kallsyms-no-memmove.patch
CommitLineData
5e993f12 1Index: scripts/kallsyms.c
2===================================================================
3--- scripts/kallsyms.c (revision 3266)
4+++ scripts/kallsyms.c (working copy)
5@@ -26,8 +26,6 @@
6 *
7 */
8
9-#define _GNU_SOURCE
10-
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14@@ -56,6 +54,37 @@ int token_profit[0x10000];
15 unsigned char best_table[256][2];
16 unsigned char best_table_len[256];
17
18+/* memmem(), while useful, is not in POSIX, so create a local version
19+ * so we can compile on non-GNU systems (Darwin, *BSD, etc...)
20+ */
21+void *memmem(const void *haystack, size_t haystack_len,
22+ const void *needle, size_t needle_len)
23+{
24+ const char *begin;
25+ const char *const last_possible =
26+ (const char *)haystack + haystack_len - needle_len;
27+
28+ /* The first occurrence of the empty string is deemed to occur at
29+ * the beginning of the string.
30+ */
31+ if (needle_len == 0)
32+ return (void *)haystack;
33+
34+ /* Sanity check, otherwise the loop might search through the whole
35+ * memory.
36+ */
37+ if (haystack_len < needle_len)
38+ return NULL;
39+
40+ for (begin = (const char *)haystack; begin <= last_possible; ++begin)
41+ if (begin[0] == ((const char *)needle)[0] &&
42+ !memcmp((const void *)&begin[1],
43+ (const void *)((const char *)needle + 1),
44+ needle_len - 1))
45+ return (void *)begin;
46+
47+ return NULL;
48+}
49
50 static void usage(void)
51 {