annotate src/c/memmem.c @ 2152:2d9e40e726f2

Adjust new [assert] to work properly from top.ur
author Adam Chlipala <adam@chlipala.net>
date Wed, 03 Jun 2015 09:55:37 -0400
parents 236dc296c32d
children
rev   line source
adamc@1268 1 #include "config.h"
adamc@1268 2
adamc@914 3 /* $NetBSD$ */
adamc@914 4
adamc@914 5 /*-
adamc@914 6 * Copyright (c) 2003 The NetBSD Foundation, Inc.
adamc@914 7 * All rights reserved.
adamc@914 8 *
adamc@914 9 * This code is derived from software contributed to The NetBSD Foundation
adamc@914 10 * by
adamc@914 11 *
adamc@914 12 * Redistribution and use in source and binary forms, with or without
adamc@914 13 * modification, are permitted provided that the following conditions
adamc@914 14 * are met:
adamc@914 15 * 1. Redistributions of source code must retain the above copyright
adamc@914 16 * notice, this list of conditions and the following disclaimer.
adamc@914 17 * 2. Redistributions in binary form must reproduce the above copyright
adamc@914 18 * notice, this list of conditions and the following disclaimer in the
adamc@914 19 * documentation and/or other materials provided with the distribution.
adamc@914 20 * 3. All advertising materials mentioning features or use of this software
adamc@914 21 * must display the following acknowledgement:
adamc@914 22 * This product includes software developed by the NetBSD
adamc@914 23 * Foundation, Inc. and its contributors.
adamc@914 24 * 4. Neither the name of The NetBSD Foundation nor the names of its
adamc@914 25 * contributors may be used to endorse or promote products derived
adamc@914 26 * from this software without specific prior written permission.
adamc@914 27 *
adamc@914 28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
adamc@914 29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
adamc@914 30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
adamc@914 31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
adamc@914 32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@914 33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@914 34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@914 35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@914 36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@914 37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@914 38 * POSSIBILITY OF SUCH DAMAGE.
adamc@914 39 */
adamc@914 40
adamc@914 41 #include <sys/cdefs.h>
adamc@914 42 #if defined(LIBC_SCCS) && !defined(lint)
adamc@914 43 __RCSID("$NetBSD$");
adamc@914 44 #endif /* LIBC_SCCS and not lint */
adamc@914 45
adamc@914 46 #if !defined(_KERNEL) && !defined(_STANDALONE)
adamc@914 47 #include <assert.h>
adamc@914 48 #include <string.h>
adamc@914 49 #else
adamc@914 50 #include <lib/libkern/libkern.h>
adamc@914 51 #define _DIAGASSERT(x) (void)0
adamc@914 52 #define NULL ((char *)0)
adamc@914 53 #endif
adamc@914 54
adamc@914 55 /*
adamc@914 56 * memmem() returns the location of the first occurence of data
adamc@914 57 * pattern b2 of size len2 in memory block b1 of size len1 or
adamc@914 58 * NULL if none is found.
adamc@914 59 */
adamc@914 60 void *
adamc@920 61 memmem(const void *b1, size_t len1, const void *b2, size_t len2)
adamc@914 62 {
adamc@914 63 /* Initialize search pointer */
adamc@914 64 char *sp = (char *) b1;
adamc@914 65
adamc@914 66 /* Initialize pattern pointer */
adamc@914 67 char *pp = (char *) b2;
adamc@914 68
adamc@914 69 /* Intialize end of search address space pointer */
adamc@914 70 char *eos = sp + len1 - len2;
adamc@914 71
adamc@914 72 /* Sanity check */
adamc@914 73 if(!(b1 && b2 && len1 && len2))
adamc@914 74 return NULL;
adamc@914 75
adamc@914 76 while (sp <= eos) {
adamc@914 77 if (*sp == *pp)
adamc@914 78 if (memcmp(sp, pp, len2) == 0)
adamc@914 79 return sp;
adamc@914 80
adamc@914 81 sp++;
adamc@914 82 }
adamc@914 83
adamc@914 84 return NULL;
adamc@914 85 }