xref: /freebsd/sys/libkern/memmem.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
127ecc2adSBenno Rice /*-
227ecc2adSBenno Rice  * Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com>
327ecc2adSBenno Rice  *
427ecc2adSBenno Rice  * Redistribution and use in source and binary forms, with or without
527ecc2adSBenno Rice  * modification, are permitted provided that the following conditions
627ecc2adSBenno Rice  * are met:
727ecc2adSBenno Rice  * 1. Redistributions of source code must retain the above copyright
827ecc2adSBenno Rice  *    notice, this list of conditions and the following disclaimer.
927ecc2adSBenno Rice  * 2. Redistributions in binary form must reproduce the above copyright
1027ecc2adSBenno Rice  *    notice, this list of conditions and the following disclaimer in the
1127ecc2adSBenno Rice  *    documentation and/or other materials provided with the distribution.
1227ecc2adSBenno Rice  * 3. The name of the author may not be used to endorse or promote
1327ecc2adSBenno Rice  *    products derived from this software without specific prior written
1427ecc2adSBenno Rice  *    permission.
1527ecc2adSBenno Rice  *
1627ecc2adSBenno Rice  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1727ecc2adSBenno Rice  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1827ecc2adSBenno Rice  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1927ecc2adSBenno Rice  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2027ecc2adSBenno Rice  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2127ecc2adSBenno Rice  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2227ecc2adSBenno Rice  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2327ecc2adSBenno Rice  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2427ecc2adSBenno Rice  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2527ecc2adSBenno Rice  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2627ecc2adSBenno Rice  * SUCH DAMAGE.
2727ecc2adSBenno Rice  */
2827ecc2adSBenno Rice 
2927ecc2adSBenno Rice #include <sys/cdefs.h>
3027ecc2adSBenno Rice #include <sys/libkern.h>
3127ecc2adSBenno Rice #include <sys/param.h>
3227ecc2adSBenno Rice 
3327ecc2adSBenno Rice void *
memmem(const void * l,size_t l_len,const void * s,size_t s_len)3427ecc2adSBenno Rice memmem(const void *l, size_t l_len, const void *s, size_t s_len)
3527ecc2adSBenno Rice {
36*484820d4SConrad Meyer         char *cur, *last;
3727ecc2adSBenno Rice         const char *cl = (const char *)l;
3827ecc2adSBenno Rice         const char *cs = (const char *)s;
3927ecc2adSBenno Rice 
4027ecc2adSBenno Rice         /* we need something to compare */
4127ecc2adSBenno Rice         if (l_len == 0 || s_len == 0)
4227ecc2adSBenno Rice                 return NULL;
4327ecc2adSBenno Rice 
4427ecc2adSBenno Rice         /* "s" must be smaller or equal to "l" */
4527ecc2adSBenno Rice         if (l_len < s_len)
4627ecc2adSBenno Rice                 return NULL;
4727ecc2adSBenno Rice 
4827ecc2adSBenno Rice         /* special case where s_len == 1 */
4927ecc2adSBenno Rice         if (s_len == 1)
5027ecc2adSBenno Rice                 return memchr(l, (int)*cs, l_len);
5127ecc2adSBenno Rice 
5227ecc2adSBenno Rice         /* the last position where its possible to find "s" in "l" */
53afa0a6efSBjoern A. Zeeb         last = __DECONST(char *, cl) + l_len - s_len;
5427ecc2adSBenno Rice 
55afa0a6efSBjoern A. Zeeb         for (cur = __DECONST(char *, cl); cur <= last; cur++)
5627ecc2adSBenno Rice                 if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
5727ecc2adSBenno Rice                         return cur;
5827ecc2adSBenno Rice 
5927ecc2adSBenno Rice         return NULL;
6027ecc2adSBenno Rice }
61