[or-cvs] Add a tor_memmem function
Nick Mathewson
nickm at seul.org
Sat Jun 18 02:17:13 UTC 2005
Update of /home/or/cvsroot/tor/src/common
In directory moria:/tmp/cvs-serv19901/src/common
Modified Files:
compat.c compat.h
Log Message:
Add a tor_memmem function
Index: compat.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/compat.c,v
retrieving revision 1.52
retrieving revision 1.53
diff -u -d -r1.52 -r1.53
--- compat.c 12 Jun 2005 04:33:26 -0000 1.52
+++ compat.c 18 Jun 2005 02:17:11 -0000 1.53
@@ -13,7 +13,9 @@
* the platform.
**/
-/* This is required on rh7 to make strptime not complain. */
+/* This is required on rh7 to make strptime not complain.
+ * We also need it to make memmem get defined (where available)
+ */
#define _GNU_SOURCE
#include "orconfig.h"
@@ -134,6 +136,38 @@
return r;
}
+/** Given <b>hlen</b> bytes at <b>haystack</b> and <b>nlen</b> bytes at
+ * <b>needle</b>, return a pointer to the first occurence of the needle
+ * within the haystack, or NULL if there is no such occurrence.
+ *
+ * Requires that nlen be greater than zero.
+ */
+const void *
+tor_memmem(const void *haystack, size_t hlen, const void *needle, size_t nlen)
+{
+#if defined(HAVE_MEMMEM) && (!defined(__GNUC__) || __GNUC__ >= 2)
+ tor_assert(nlen);
+ return memmem(haystack, hlen, needle, nlen);
+#else
+ /* This isn't as fast as the GLIBC implementation, but it doesn't need to be. */
+ const void *p, *end;
+ char first;
+ tor_assert(nlen);
+
+ p = haystack;
+ end = haystack + hlen;
+ first = *(const char*)needle;
+ while ((p = memchr(p, first, end-p))) {
+ if (end-p >= nlen)
+ return NULL;
+ if (!memcmp(p, needle, nlen))
+ return p;
+ ++p;
+ }
+ return NULL;
+#endif
+}
+
/** Take a filename and return a pointer to its final element. This
* function is called on __FILE__ to fix a MSVC nit where __FILE__
* contains the full path to the file. This is bad, because it
Index: compat.h
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/compat.h,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -d -r1.30 -r1.31
--- compat.h 9 Jun 2005 19:03:31 -0000 1.30
+++ compat.h 18 Jun 2005 02:17:11 -0000 1.31
@@ -87,6 +87,9 @@
CHECK_PRINTF(3,4);
int tor_vsnprintf(char *str, size_t size, const char *format, va_list args);
+const void *tor_memmem(const void *haystack, size_t hlen, const void *needle,
+ size_t nlen);
+
#define TOR_ISAPLHA(c) isalpha((int)(unsigned char)(c))
#define TOR_ISALNUM(c) isalnum((int)(unsigned char)(c))
#define TOR_ISSPACE(c) isspace((int)(unsigned char)(c))
More information about the tor-commits
mailing list