adamc@1268: #include "config.h" adamc@1268: adamc@914: /* $NetBSD$ */ adamc@914: adamc@914: /*- adamc@914: * Copyright (c) 2003 The NetBSD Foundation, Inc. adamc@914: * All rights reserved. adamc@914: * adamc@914: * This code is derived from software contributed to The NetBSD Foundation adamc@914: * by adamc@914: * adamc@914: * Redistribution and use in source and binary forms, with or without adamc@914: * modification, are permitted provided that the following conditions adamc@914: * are met: adamc@914: * 1. Redistributions of source code must retain the above copyright adamc@914: * notice, this list of conditions and the following disclaimer. adamc@914: * 2. Redistributions in binary form must reproduce the above copyright adamc@914: * notice, this list of conditions and the following disclaimer in the adamc@914: * documentation and/or other materials provided with the distribution. adamc@914: * 3. All advertising materials mentioning features or use of this software adamc@914: * must display the following acknowledgement: adamc@914: * This product includes software developed by the NetBSD adamc@914: * Foundation, Inc. and its contributors. adamc@914: * 4. Neither the name of The NetBSD Foundation nor the names of its adamc@914: * contributors may be used to endorse or promote products derived adamc@914: * from this software without specific prior written permission. adamc@914: * adamc@914: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS adamc@914: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED adamc@914: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR adamc@914: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS adamc@914: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR adamc@914: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF adamc@914: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS adamc@914: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN adamc@914: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) adamc@914: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE adamc@914: * POSSIBILITY OF SUCH DAMAGE. adamc@914: */ adamc@914: adamc@914: #include adamc@914: #if defined(LIBC_SCCS) && !defined(lint) adamc@914: __RCSID("$NetBSD$"); adamc@914: #endif /* LIBC_SCCS and not lint */ adamc@914: adamc@914: #if !defined(_KERNEL) && !defined(_STANDALONE) adamc@914: #include adamc@914: #include adamc@914: #else adamc@914: #include adamc@914: #define _DIAGASSERT(x) (void)0 adamc@914: #define NULL ((char *)0) adamc@914: #endif adamc@914: adamc@914: /* adamc@914: * memmem() returns the location of the first occurence of data adamc@914: * pattern b2 of size len2 in memory block b1 of size len1 or adamc@914: * NULL if none is found. adamc@914: */ adamc@914: void * adamc@920: memmem(const void *b1, size_t len1, const void *b2, size_t len2) adamc@914: { adamc@914: /* Initialize search pointer */ adamc@914: char *sp = (char *) b1; adamc@914: adamc@914: /* Initialize pattern pointer */ adamc@914: char *pp = (char *) b2; adamc@914: adamc@914: /* Intialize end of search address space pointer */ adamc@914: char *eos = sp + len1 - len2; adamc@914: adamc@914: /* Sanity check */ adamc@914: if(!(b1 && b2 && len1 && len2)) adamc@914: return NULL; adamc@914: adamc@914: while (sp <= eos) { adamc@914: if (*sp == *pp) adamc@914: if (memcmp(sp, pp, len2) == 0) adamc@914: return sp; adamc@914: adamc@914: sp++; adamc@914: } adamc@914: adamc@914: return NULL; adamc@914: }