1*c9ad075bSchristos /* $OpenBSD: memmem.c,v 1.4 2015/08/31 02:53:57 guenther Exp $ */
2*c9ad075bSchristos /*-
3*c9ad075bSchristos * Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com>
4*c9ad075bSchristos *
5*c9ad075bSchristos * Redistribution and use in source and binary forms, with or without
6*c9ad075bSchristos * modification, are permitted provided that the following conditions
7*c9ad075bSchristos * are met:
8*c9ad075bSchristos * 1. Redistributions of source code must retain the above copyright
9*c9ad075bSchristos * notice, this list of conditions and the following disclaimer.
10*c9ad075bSchristos * 2. Redistributions in binary form must reproduce the above copyright
11*c9ad075bSchristos * notice, this list of conditions and the following disclaimer in the
12*c9ad075bSchristos * documentation and/or other materials provided with the distribution.
13*c9ad075bSchristos * 3. The name of the author may not be used to endorse or promote
14*c9ad075bSchristos * products derived from this software without specific prior written
15*c9ad075bSchristos * permission.
16*c9ad075bSchristos *
17*c9ad075bSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18*c9ad075bSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*c9ad075bSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*c9ad075bSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21*c9ad075bSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*c9ad075bSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*c9ad075bSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*c9ad075bSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*c9ad075bSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*c9ad075bSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*c9ad075bSchristos * SUCH DAMAGE.
28*c9ad075bSchristos */
29*c9ad075bSchristos
30*c9ad075bSchristos #include <string.h>
31*c9ad075bSchristos
32*c9ad075bSchristos #include "compat.h"
33*c9ad075bSchristos
34*c9ad075bSchristos /*
35*c9ad075bSchristos * Find the first occurrence of the byte string s in byte string l.
36*c9ad075bSchristos */
37*c9ad075bSchristos
38*c9ad075bSchristos void *
memmem(const void * l,size_t l_len,const void * s,size_t s_len)39*c9ad075bSchristos memmem(const void *l, size_t l_len, const void *s, size_t s_len)
40*c9ad075bSchristos {
41*c9ad075bSchristos const char *cur, *last;
42*c9ad075bSchristos const char *cl = l;
43*c9ad075bSchristos const char *cs = s;
44*c9ad075bSchristos
45*c9ad075bSchristos /* a zero length needle should just return the haystack */
46*c9ad075bSchristos if (s_len == 0)
47*c9ad075bSchristos return (void *)cl;
48*c9ad075bSchristos
49*c9ad075bSchristos /* "s" must be smaller or equal to "l" */
50*c9ad075bSchristos if (l_len < s_len)
51*c9ad075bSchristos return NULL;
52*c9ad075bSchristos
53*c9ad075bSchristos /* special case where s_len == 1 */
54*c9ad075bSchristos if (s_len == 1)
55*c9ad075bSchristos return memchr(l, *cs, l_len);
56*c9ad075bSchristos
57*c9ad075bSchristos /* the last position where its possible to find "s" in "l" */
58*c9ad075bSchristos last = cl + l_len - s_len;
59*c9ad075bSchristos
60*c9ad075bSchristos for (cur = cl; cur <= last; cur++)
61*c9ad075bSchristos if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
62*c9ad075bSchristos return (void *)cur;
63*c9ad075bSchristos
64*c9ad075bSchristos return NULL;
65*c9ad075bSchristos }
66