Skip to contentSkip to conversation

Add memmem() library function

PATCH New Grant Likely 1 participant 2 messages Aug 30, 2007 Generic string library

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Add memmem() library function

From: Grant Likely <[email protected]>

---

 include/linux/string.h |    3 +++
 lib_generic/string.c   |   34 ++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 0 deletions(-)
include/linux/string.h +3 -0
index 6239039..e18bcbf 100644
@@ -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
 }
lib_generic/string.c +34 -0
index e0b793a..8c699d9 100644
@@ -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
+ */
+static void *memmem (const void *m, size_t ml, const void *s, size_t sl)
+{
+	char * start = m;
+	char * end = m + ml;
 
+	if (sl > ml)
+		return NULL;
+
+	while (start < end)
+	{
+		size_t len = sl;
+		char * ptr1 = start;
+		char * ptr2 = (char *)s;
+		while (len > 0 && *ptr1++ == *ptr2++)
+			len--;
+		if (len == 0)
+			return start;
+		start++;
+	}
+
+	return NULL;
+}
 #endif
#1 lore raw

PATCH 1/2 Add memmem() library function

Gah! Ignore this patch; it's incorrect.

I'll send the correct one shortly.

g.

Quoted 69 lines · Grant Likely

From: Grant Likely <[email protected]>

---

 include/linux/string.h |    3 +++
 lib_generic/string.c   |   34 ++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 0 deletions(-)
include/linux/string.h +3 -0
index 6239039..e18bcbf 100644
@@ -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
 }
lib_generic/string.c +34 -0
index e0b793a..8c699d9 100644
@@ -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
+ */
+static void *memmem (const void *m, size_t ml, const void *s, size_t sl)
+{
+       char * start = m;
+       char * end = m + ml;
 
+       if (sl > ml)
+               return NULL;
+
+       while (start < end)
+       {
+               size_t len = sl;
+               char * ptr1 = start;
+               char * ptr2 = (char *)s;
+               while (len > 0 && *ptr1++ == *ptr2++)
+                       len--;
+               if (len == 0)
+                       return start;
+               start++;
+       }
+
+       return NULL;
+}
 #endif
Signature · 4 lines
Grant Likely, B.Sc., P.Eng.
Secret Lab Technologies Ltd.
grant.likely at secretlab.ca
(403) 399-0195