compat / memmem.con commit Merge git://git.kernel.org/pub/scm/gitk/gitk (39a76bd)
   1#include "../git-compat-util.h"
   2
   3void *gitmemmem(const void *haystack, size_t haystack_len,
   4                const void *needle, size_t needle_len)
   5{
   6        const char *begin = haystack;
   7        const char *last_possible = begin + haystack_len - needle_len;
   8
   9        /*
  10         * The first occurrence of the empty string is deemed to occur at
  11         * the beginning of the string.
  12         */
  13        if (needle_len == 0)
  14                return (void *)begin;
  15
  16        /*
  17         * Sanity check, otherwise the loop might search through the whole
  18         * memory.
  19         */
  20        if (haystack_len < needle_len)
  21                return NULL;
  22
  23        for (; begin <= last_possible; begin++) {
  24                if (!memcmp(begin, needle, needle_len))
  25                        return (void *)begin;
  26        }
  27
  28        return NULL;
  29}