annotate src/c/memmem.c @ 987:6dd122f10c0c

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