1271173Sbenno/*- 2271173Sbenno * Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com> 3271173Sbenno * 4271173Sbenno * Redistribution and use in source and binary forms, with or without 5271173Sbenno * modification, are permitted provided that the following conditions 6271173Sbenno * are met: 7271173Sbenno * 1. Redistributions of source code must retain the above copyright 8271173Sbenno * notice, this list of conditions and the following disclaimer. 9271173Sbenno * 2. Redistributions in binary form must reproduce the above copyright 10271173Sbenno * notice, this list of conditions and the following disclaimer in the 11271173Sbenno * documentation and/or other materials provided with the distribution. 12271173Sbenno * 3. The name of the author may not be used to endorse or promote 13271173Sbenno * products derived from this software without specific prior written 14271173Sbenno * permission. 15271173Sbenno * 16271173Sbenno * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17271173Sbenno * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18271173Sbenno * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19271173Sbenno * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20271173Sbenno * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21271173Sbenno * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22271173Sbenno * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23271173Sbenno * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24271173Sbenno * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25271173Sbenno * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26271173Sbenno * SUCH DAMAGE. 27271173Sbenno */ 28271173Sbenno 29271173Sbenno#include <sys/cdefs.h> 30271173Sbenno__FBSDID("$FreeBSD: releng/11.0/sys/libkern/memmem.c 271251 2014-09-08 08:12:09Z bz $"); 31271173Sbenno 32271173Sbenno#include <sys/libkern.h> 33271173Sbenno#include <sys/param.h> 34271173Sbenno 35271173Sbennovoid * 36271173Sbennomemmem(const void *l, size_t l_len, const void *s, size_t s_len) 37271173Sbenno{ 38271173Sbenno register char *cur, *last; 39271173Sbenno const char *cl = (const char *)l; 40271173Sbenno const char *cs = (const char *)s; 41271173Sbenno 42271173Sbenno /* we need something to compare */ 43271173Sbenno if (l_len == 0 || s_len == 0) 44271173Sbenno return NULL; 45271173Sbenno 46271173Sbenno /* "s" must be smaller or equal to "l" */ 47271173Sbenno if (l_len < s_len) 48271173Sbenno return NULL; 49271173Sbenno 50271173Sbenno /* special case where s_len == 1 */ 51271173Sbenno if (s_len == 1) 52271173Sbenno return memchr(l, (int)*cs, l_len); 53271173Sbenno 54271173Sbenno /* the last position where its possible to find "s" in "l" */ 55271251Sbz last = __DECONST(char *, cl) + l_len - s_len; 56271173Sbenno 57271251Sbz for (cur = __DECONST(char *, cl); cur <= last; cur++) 58271173Sbenno if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0) 59271173Sbenno return cur; 60271173Sbenno 61271173Sbenno return NULL; 62271173Sbenno} 63