xref: /netbsd-src/external/gpl3/binutils.old/dist/libiberty/memmem.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
1*e992f068Schristos /* Copyright (C) 1991-2022 Free Software Foundation, Inc.
216dce513Schristos    This file is part of the GNU C Library.
316dce513Schristos 
416dce513Schristos    This program is free software; you can redistribute it and/or modify
516dce513Schristos    it under the terms of the GNU General Public License as published by
616dce513Schristos    the Free Software Foundation; either version 2, or (at your option)
716dce513Schristos    any later version.
816dce513Schristos 
916dce513Schristos    This program is distributed in the hope that it will be useful,
1016dce513Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1116dce513Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1216dce513Schristos    GNU General Public License for more details.
1316dce513Schristos 
1416dce513Schristos    You should have received a copy of the GNU General Public License along
1516dce513Schristos    with this program; if not, write to the Free Software Foundation,
1616dce513Schristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
1716dce513Schristos 
1816dce513Schristos /*
1916dce513Schristos 
2016dce513Schristos @deftypefn Supplemental void* memmem (const void *@var{haystack}, @
2116dce513Schristos   size_t @var{haystack_len} const void *@var{needle}, size_t @var{needle_len})
2216dce513Schristos 
2316dce513Schristos Returns a pointer to the first occurrence of @var{needle} (length
2416dce513Schristos @var{needle_len}) in @var{haystack} (length @var{haystack_len}).
2516dce513Schristos Returns @code{NULL} if not found.
2616dce513Schristos 
2716dce513Schristos @end deftypefn
2816dce513Schristos 
2916dce513Schristos */
3016dce513Schristos 
3116dce513Schristos #ifndef _LIBC
3216dce513Schristos # include <config.h>
3316dce513Schristos #endif
3416dce513Schristos 
3516dce513Schristos #include <stddef.h>
3616dce513Schristos #include <string.h>
3716dce513Schristos 
3816dce513Schristos #ifndef _LIBC
3916dce513Schristos # define __builtin_expect(expr, val)   (expr)
4016dce513Schristos #endif
4116dce513Schristos 
4216dce513Schristos #undef memmem
4316dce513Schristos 
4416dce513Schristos /* Return the first occurrence of NEEDLE in HAYSTACK.  */
4516dce513Schristos void *
memmem(const void * haystack,size_t haystack_len,const void * needle,size_t needle_len)4616dce513Schristos memmem (const void *haystack, size_t haystack_len, const void *needle,
4716dce513Schristos 	size_t needle_len)
4816dce513Schristos {
4916dce513Schristos   const char *begin;
5016dce513Schristos   const char *const last_possible
5116dce513Schristos     = (const char *) haystack + haystack_len - needle_len;
5216dce513Schristos 
5316dce513Schristos   if (needle_len == 0)
5416dce513Schristos     /* The first occurrence of the empty string is deemed to occur at
5516dce513Schristos        the beginning of the string.  */
5616dce513Schristos     return (void *) haystack;
5716dce513Schristos 
5816dce513Schristos   /* Sanity check, otherwise the loop might search through the whole
5916dce513Schristos      memory.  */
6016dce513Schristos   if (__builtin_expect (haystack_len < needle_len, 0))
6116dce513Schristos     return NULL;
6216dce513Schristos 
6316dce513Schristos   for (begin = (const char *) haystack; begin <= last_possible; ++begin)
6416dce513Schristos     if (begin[0] == ((const char *) needle)[0] &&
6516dce513Schristos 	!memcmp ((const void *) &begin[1],
6616dce513Schristos 		 (const void *) ((const char *) needle + 1),
6716dce513Schristos 		 needle_len - 1))
6816dce513Schristos       return (void *) begin;
6916dce513Schristos 
7016dce513Schristos   return NULL;
7116dce513Schristos }
72