|
@@ -13,7 +13,9 @@ const char compat_c_id[] = "$Id$";
|
|
|
* the platform.
|
|
|
**/
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+ * We also need it to make memmem get defined (where available)
|
|
|
+ */
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
|
#include "orconfig.h"
|
|
@@ -134,6 +136,38 @@ tor_vsnprintf(char *str, size_t size, const char *format, va_list args)
|
|
|
return r;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * <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
|
|
|
+
|
|
|
+ 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
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
* function is called on __FILE__ to fix a MSVC nit where __FILE__
|
|
|
* contains the full path to the file. This is bad, because it
|