[U-Boot-Users] [PATCH 1/2] Add memmem() library function
Grant Likely
grant.likely at secretlab.ca
Thu Aug 30 16:12:41 CEST 2007
From: Grant Likely <grant.likely at secretlab.ca>
Signed-off-by: Grant Likely <grant.likely at secretlab.ca>
---
include/linux/string.h | 3 +++
lib_generic/string.c | 34 ++++++++++++++++++++++++++++++++++
2 files changed, 37 insertions(+), 0 deletions(-)
diff --git a/include/linux/string.h b/include/linux/string.h
index 6239039..e18bcbf 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -81,6 +81,9 @@ extern int memcmp(const void *,const void *,__kernel_size_t);
#ifndef __HAVE_ARCH_MEMCHR
extern void * memchr(const void *,int,__kernel_size_t);
#endif
+#ifndef __HAVE_ARCH_MEMMEM
+extern void * memmem(const void *,__kernel_size_t,const void *,__kernel_size_t);
+#endif
#ifdef __cplusplus
}
diff --git a/lib_generic/string.c b/lib_generic/string.c
index e0b793a..36262de 100644
--- a/lib_generic/string.c
+++ b/lib_generic/string.c
@@ -574,5 +574,39 @@ void *memchr(const void *s, int c, size_t n)
}
return NULL;
}
+#endif
+
+#ifndef __HAVE_ARCH_MEMMEM
+/**
+ * memmem - Find a sequence in an area of memory
+ * @a: The memory area
+ * @len1: Size of the memory area
+ * @b: The sequence to search for
+ * @len2: Size of the sequence
+ *
+ * returns the address of the first occurrence of @b, or %NULL
+ * if @b is not found
+ */
+void *memmem (const void *m, size_t ml, const void *s, size_t sl)
+{
+ const char * start = m;
+ const char * end = m + ml;
+ if (sl > ml)
+ return NULL;
+
+ while (start < end)
+ {
+ size_t len = sl;
+ const char * ptr1 = start;
+ const char * ptr2 = s;
+ while (len > 0 && *ptr1++ == *ptr2++)
+ len--;
+ if (len == 0)
+ return (void *)start;
+ start++;
+ }
+
+ return NULL;
+}
#endif
More information about the U-Boot
mailing list