Revision ef937140a602bccea6f36235f1f7f926a90b85f2 authored by Junio C Hamano on 22 July 2014, 17:26:34 UTC, committed by Junio C Hamano on 22 July 2014, 17:26:34 UTC
* sk/test-cmp-bin:
  t5000, t5003: do not use test_cmp to compare binary files
2 parent s 79e9dba + b93e6e3
Raw File
memmem.c
#include "../git-compat-util.h"

void *gitmemmem(const void *haystack, size_t haystack_len,
                const void *needle, size_t needle_len)
{
	const char *begin = haystack;
	const char *last_possible = begin + haystack_len - needle_len;
	const char *tail = needle;
	char point;

	/*
	 * The first occurrence of the empty string is deemed to occur at
	 * the beginning of the string.
	 */
	if (needle_len == 0)
		return (void *)begin;

	/*
	 * Sanity check, otherwise the loop might search through the whole
	 * memory.
	 */
	if (haystack_len < needle_len)
		return NULL;

	point = *tail++;
	for (; begin <= last_possible; begin++) {
		if (*begin == point && !memcmp(begin + 1, tail, needle_len - 1))
			return (void *)begin;
	}

	return NULL;
}
back to top