1*28de4f3cSToomas Soome /*
2*28de4f3cSToomas Soome * CDDL HEADER START
3*28de4f3cSToomas Soome *
4*28de4f3cSToomas Soome * The contents of this file are subject to the terms of the
5*28de4f3cSToomas Soome * Common Development and Distribution License (the "License").
6*28de4f3cSToomas Soome * You may not use this file except in compliance with the License.
7*28de4f3cSToomas Soome *
8*28de4f3cSToomas Soome * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*28de4f3cSToomas Soome * or http://www.opensolaris.org/os/licensing.
10*28de4f3cSToomas Soome * See the License for the specific language governing permissions
11*28de4f3cSToomas Soome * and limitations under the License.
12*28de4f3cSToomas Soome *
13*28de4f3cSToomas Soome * When distributing Covered Code, include this CDDL HEADER in each
14*28de4f3cSToomas Soome * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*28de4f3cSToomas Soome * If applicable, add the following below this CDDL HEADER, with the
16*28de4f3cSToomas Soome * fields enclosed by brackets "[]" replaced with your own identifying
17*28de4f3cSToomas Soome * information: Portions Copyright [yyyy] [name of copyright owner]
18*28de4f3cSToomas Soome *
19*28de4f3cSToomas Soome * CDDL HEADER END
20*28de4f3cSToomas Soome */
21*28de4f3cSToomas Soome
22*28de4f3cSToomas Soome /*
23*28de4f3cSToomas Soome * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24*28de4f3cSToomas Soome */
25*28de4f3cSToomas Soome
26*28de4f3cSToomas Soome /*
27*28de4f3cSToomas Soome * Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com>
28*28de4f3cSToomas Soome *
29*28de4f3cSToomas Soome * Redistribution and use in source and binary forms, with or without
30*28de4f3cSToomas Soome * modification, are permitted provided that the following conditions
31*28de4f3cSToomas Soome * are met:
32*28de4f3cSToomas Soome * 1. Redistributions of source code must retain the above copyright
33*28de4f3cSToomas Soome * notice, this list of conditions and the following disclaimer.
34*28de4f3cSToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
35*28de4f3cSToomas Soome * notice, this list of conditions and the following disclaimer in the
36*28de4f3cSToomas Soome * documentation and/or other materials provided with the distribution.
37*28de4f3cSToomas Soome * 3. The name of the author may not be used to endorse or promote
38*28de4f3cSToomas Soome * products derived from this software without specific prior written
39*28de4f3cSToomas Soome * permission.
40*28de4f3cSToomas Soome *
41*28de4f3cSToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
42*28de4f3cSToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*28de4f3cSToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*28de4f3cSToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*28de4f3cSToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*28de4f3cSToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*28de4f3cSToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*28de4f3cSToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*28de4f3cSToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*28de4f3cSToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*28de4f3cSToomas Soome * SUCH DAMAGE.
52*28de4f3cSToomas Soome */
53*28de4f3cSToomas Soome
54*28de4f3cSToomas Soome #include <string.h>
55*28de4f3cSToomas Soome
56*28de4f3cSToomas Soome /*
57*28de4f3cSToomas Soome * Find the first occurrence of the byte string s in byte string l.
58*28de4f3cSToomas Soome */
59*28de4f3cSToomas Soome
60*28de4f3cSToomas Soome void *
memmem(const void * l,size_t l_len,const void * s,size_t s_len)61*28de4f3cSToomas Soome memmem(const void *l, size_t l_len, const void *s, size_t s_len)
62*28de4f3cSToomas Soome {
63*28de4f3cSToomas Soome char *cur, *last;
64*28de4f3cSToomas Soome const char *cl = (const char *)l;
65*28de4f3cSToomas Soome const char *cs = (const char *)s;
66*28de4f3cSToomas Soome
67*28de4f3cSToomas Soome /* we need something to compare */
68*28de4f3cSToomas Soome if (l_len == 0 || s_len == 0)
69*28de4f3cSToomas Soome return (NULL);
70*28de4f3cSToomas Soome
71*28de4f3cSToomas Soome /* "s" must be smaller or equal to "l" */
72*28de4f3cSToomas Soome if (l_len < s_len)
73*28de4f3cSToomas Soome return (NULL);
74*28de4f3cSToomas Soome
75*28de4f3cSToomas Soome /* special case where s_len == 1 */
76*28de4f3cSToomas Soome if (s_len == 1)
77*28de4f3cSToomas Soome return (memchr(l, (int)*cs, l_len));
78*28de4f3cSToomas Soome
79*28de4f3cSToomas Soome /* the last position where its possible to find "s" in "l" */
80*28de4f3cSToomas Soome last = (char *)cl + l_len - s_len;
81*28de4f3cSToomas Soome
82*28de4f3cSToomas Soome for (cur = (char *)cl; cur <= last; cur++)
83*28de4f3cSToomas Soome if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
84*28de4f3cSToomas Soome return (cur);
85*28de4f3cSToomas Soome
86*28de4f3cSToomas Soome return (NULL);
87*28de4f3cSToomas Soome }
88