From 0c66431df361f6bc054581769f105bcf68c4727f Mon Sep 17 00:00:00 2001 From: Neil Roberts Date: Tue, 7 Aug 2012 11:45:29 +0100 Subject: [PATCH] Add a wrapper for 'memmem' memmem is a GNU libc extension that works like strstr except that the size of the needle and the haystack are passed into the function instead of using null-terminated strings. This patch adds a wrapper function called 'cogl_util_memmem' so that we can use this function. There is a configure check and if the function is not available then a fallback implementation will be used. Otherwise cogl_util_memmem is just defined to memmem. Reviewed-by: Robert Bragg (cherry picked from commit 1dd1b0a67f6238e13f7f9253fb03addada0541b7) --- cogl/cogl-util.c | 24 ++++++++++++++++++++++++ cogl/cogl-util.h | 10 ++++++++++ configure.ac | 3 +++ 3 files changed, 37 insertions(+) diff --git a/cogl/cogl-util.c b/cogl/cogl-util.c index e8ffb04ea..9ecc34c22 100644 --- a/cogl/cogl-util.c +++ b/cogl/cogl-util.c @@ -25,6 +25,8 @@ #include "config.h" #endif +#include + #include "cogl-util.h" #include "cogl-private.h" @@ -252,4 +254,26 @@ _cogl_util_pixel_format_from_masks (unsigned long r_mask, return image_format; } +#ifndef HAVE_MEMMEM + +char * +_cogl_util_memmem (const void *haystack, + size_t haystack_len, + const void *needle, + size_t needle_len) +{ + size_t i; + + if (needle_len > haystack_len) + return NULL; + + for (i = 0; i <= haystack_len - needle_len; i++) + if (!memcmp ((const char *) haystack + i, needle, needle_len)) + return (char *) haystack + i; + + return NULL; +} + +#endif /* HAVE_MEMMEM */ + #endif /* _COGL_IN_TEST_BITMASK */ diff --git a/cogl/cogl-util.h b/cogl/cogl-util.h index 67861bc55..4fb42274c 100644 --- a/cogl/cogl-util.h +++ b/cogl/cogl-util.h @@ -268,4 +268,14 @@ _cogl_util_pixel_format_from_masks (unsigned long r_mask, #define _Static_assert(EXPRESSION, MESSAGE) #endif +#ifdef HAVE_MEMMEM +#define _cogl_util_memmem memmem +#else +char * +_cogl_util_memmem (const void *haystack, + size_t haystack_len, + const void *needle, + size_t needle_len); +#endif + #endif /* __COGL_UTIL_H */ diff --git a/configure.ac b/configure.ac index e6abb8632..d3cc31bd5 100644 --- a/configure.ac +++ b/configure.ac @@ -1103,6 +1103,9 @@ dnl The 'ffs' function is part of C99 so it isn't always dnl available. Cogl has a fallback if needed. AC_CHECK_FUNCS([ffs]) +dnl 'memmem' is a GNU extension but we have a simple fallback +AC_CHECK_FUNCS([memmem]) + dnl ================================================================ dnl Platform values dnl ================================================================