1149466Sandre/*-
2149466Sandre * Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com>
3149466Sandre *
4149466Sandre * Redistribution and use in source and binary forms, with or without
5149466Sandre * modification, are permitted provided that the following conditions
6149466Sandre * are met:
7149466Sandre * 1. Redistributions of source code must retain the above copyright
8149466Sandre *    notice, this list of conditions and the following disclaimer.
9149466Sandre * 2. Redistributions in binary form must reproduce the above copyright
10149466Sandre *    notice, this list of conditions and the following disclaimer in the
11149466Sandre *    documentation and/or other materials provided with the distribution.
12149466Sandre * 3. The name of the author may not be used to endorse or promote
13149466Sandre *    products derived from this software without specific prior written
14149466Sandre *    permission.
15149466Sandre *
16149466Sandre * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17149466Sandre * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18149466Sandre * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19149466Sandre * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20149466Sandre * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21149466Sandre * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22149466Sandre * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23149466Sandre * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24149466Sandre * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25149466Sandre * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26149466Sandre * SUCH DAMAGE.
27149466Sandre */
28149466Sandre
29149466Sandre#include <sys/cdefs.h>
30149466Sandre__FBSDID("$FreeBSD$");
31149466Sandre
32149466Sandre#include <string.h>
33149466Sandre
34149466Sandre/*
35149466Sandre * Find the first occurrence of the byte string s in byte string l.
36149466Sandre */
37149466Sandre
38149466Sandrevoid *
39188080Sdangermemmem(const void *l, size_t l_len, const void *s, size_t s_len)
40149466Sandre{
41149466Sandre	register char *cur, *last;
42149466Sandre	const char *cl = (const char *)l;
43149466Sandre	const char *cs = (const char *)s;
44149466Sandre
45149466Sandre	/* we need something to compare */
46149466Sandre	if (l_len == 0 || s_len == 0)
47149466Sandre		return NULL;
48149466Sandre
49149466Sandre	/* "s" must be smaller or equal to "l" */
50149466Sandre	if (l_len < s_len)
51149466Sandre		return NULL;
52149466Sandre
53149466Sandre	/* special case where s_len == 1 */
54149466Sandre	if (s_len == 1)
55149466Sandre		return memchr(l, (int)*cs, l_len);
56149466Sandre
57149466Sandre	/* the last position where its possible to find "s" in "l" */
58149466Sandre	last = (char *)cl + l_len - s_len;
59149466Sandre
60149466Sandre	for (cur = (char *)cl; cur <= last; cur++)
61149466Sandre		if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
62149466Sandre			return cur;
63149466Sandre
64149466Sandre	return NULL;
65149466Sandre}
66