xref: /dflybsd-src/lib/libc/string/memmem.c (revision 68e609aa42b5c8082ca864b600d1683c6a82dc68)
1716024cdSPeter Avalos /*-
2716024cdSPeter Avalos  * Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com>
3716024cdSPeter Avalos  *
4716024cdSPeter Avalos  * Redistribution and use in source and binary forms, with or without
5716024cdSPeter Avalos  * modification, are permitted provided that the following conditions
6716024cdSPeter Avalos  * are met:
7716024cdSPeter Avalos  * 1. Redistributions of source code must retain the above copyright
8716024cdSPeter Avalos  *    notice, this list of conditions and the following disclaimer.
9716024cdSPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
10716024cdSPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
11716024cdSPeter Avalos  *    documentation and/or other materials provided with the distribution.
12716024cdSPeter Avalos  * 3. The name of the author may not be used to endorse or promote
13716024cdSPeter Avalos  *    products derived from this software without specific prior written
14716024cdSPeter Avalos  *    permission.
15716024cdSPeter Avalos  *
16716024cdSPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17716024cdSPeter Avalos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18716024cdSPeter Avalos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19716024cdSPeter Avalos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20716024cdSPeter Avalos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21716024cdSPeter Avalos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22716024cdSPeter Avalos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23716024cdSPeter Avalos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24716024cdSPeter Avalos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25716024cdSPeter Avalos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26716024cdSPeter Avalos  * SUCH DAMAGE.
27716024cdSPeter Avalos  *
280d5acd74SJohn Marino  * $FreeBSD: head/lib/libc/string/memmem.c 188080 2009-02-03 17:58:20Z danger $
29716024cdSPeter Avalos  */
30716024cdSPeter Avalos 
31716024cdSPeter Avalos #include <string.h>
32716024cdSPeter Avalos 
33716024cdSPeter Avalos /*
34716024cdSPeter Avalos  * Find the first occurrence of the byte string s in byte string l.
35716024cdSPeter Avalos  */
36716024cdSPeter Avalos 
37716024cdSPeter Avalos void *
memmem(const void * l,size_t l_len,const void * s,size_t s_len)38716024cdSPeter Avalos memmem(const void *l, size_t l_len, const void *s, size_t s_len)
39716024cdSPeter Avalos {
40*68e609aaSSascha Wildner 	char *cur, *last;
41716024cdSPeter Avalos 	const char *cl = (const char *)l;
42716024cdSPeter Avalos 	const char *cs = (const char *)s;
43716024cdSPeter Avalos 
44716024cdSPeter Avalos 	/* we need something to compare */
45716024cdSPeter Avalos 	if (l_len == 0 || s_len == 0)
460d5acd74SJohn Marino 		return NULL;
47716024cdSPeter Avalos 
48716024cdSPeter Avalos 	/* "s" must be smaller or equal to "l" */
49716024cdSPeter Avalos 	if (l_len < s_len)
500d5acd74SJohn Marino 		return NULL;
51716024cdSPeter Avalos 
52716024cdSPeter Avalos 	/* special case where s_len == 1 */
53716024cdSPeter Avalos 	if (s_len == 1)
540d5acd74SJohn Marino 		return memchr(l, (int)*cs, l_len);
55716024cdSPeter Avalos 
56716024cdSPeter Avalos 	/* the last position where its possible to find "s" in "l" */
57716024cdSPeter Avalos 	last = (char *)cl + l_len - s_len;
58716024cdSPeter Avalos 
59716024cdSPeter Avalos 	for (cur = (char *)cl; cur <= last; cur++)
60716024cdSPeter Avalos 		if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
610d5acd74SJohn Marino 			return cur;
62716024cdSPeter Avalos 
630d5acd74SJohn Marino 	return NULL;
64716024cdSPeter Avalos }
65