1*f22f0ef4Schristos /* Copyright (C) 1991-2022 Free Software Foundation, Inc.
2b3ac4aedSchristos    This file is part of the GNU C Library.
3b3ac4aedSchristos 
4b3ac4aedSchristos    This program is free software; you can redistribute it and/or modify
5b3ac4aedSchristos    it under the terms of the GNU General Public License as published by
6b3ac4aedSchristos    the Free Software Foundation; either version 2, or (at your option)
7b3ac4aedSchristos    any later version.
8b3ac4aedSchristos 
9b3ac4aedSchristos    This program is distributed in the hope that it will be useful,
10b3ac4aedSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
11b3ac4aedSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12b3ac4aedSchristos    GNU General Public License for more details.
13b3ac4aedSchristos 
14b3ac4aedSchristos    You should have received a copy of the GNU General Public License along
15b3ac4aedSchristos    with this program; if not, write to the Free Software Foundation,
16b3ac4aedSchristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17b3ac4aedSchristos 
18b3ac4aedSchristos /*
19b3ac4aedSchristos 
2005caefcfSchristos @deftypefn Supplemental void* memmem (const void *@var{haystack}, @
2105caefcfSchristos   size_t @var{haystack_len} const void *@var{needle}, size_t @var{needle_len})
22b3ac4aedSchristos 
23b3ac4aedSchristos Returns a pointer to the first occurrence of @var{needle} (length
24b3ac4aedSchristos @var{needle_len}) in @var{haystack} (length @var{haystack_len}).
25b3ac4aedSchristos Returns @code{NULL} if not found.
26b3ac4aedSchristos 
27b3ac4aedSchristos @end deftypefn
28b3ac4aedSchristos 
29b3ac4aedSchristos */
30b3ac4aedSchristos 
31b3ac4aedSchristos #ifndef _LIBC
32b3ac4aedSchristos # include <config.h>
33b3ac4aedSchristos #endif
34b3ac4aedSchristos 
35b3ac4aedSchristos #include <stddef.h>
36b3ac4aedSchristos #include <string.h>
37b3ac4aedSchristos 
38b3ac4aedSchristos #ifndef _LIBC
39b3ac4aedSchristos # define __builtin_expect(expr, val)   (expr)
40b3ac4aedSchristos #endif
41b3ac4aedSchristos 
42b3ac4aedSchristos #undef memmem
43b3ac4aedSchristos 
44b3ac4aedSchristos /* Return the first occurrence of NEEDLE in HAYSTACK.  */
45b3ac4aedSchristos void *
memmem(const void * haystack,size_t haystack_len,const void * needle,size_t needle_len)46b3ac4aedSchristos memmem (const void *haystack, size_t haystack_len, const void *needle,
47b3ac4aedSchristos 	size_t needle_len)
48b3ac4aedSchristos {
49b3ac4aedSchristos   const char *begin;
50b3ac4aedSchristos   const char *const last_possible
51b3ac4aedSchristos     = (const char *) haystack + haystack_len - needle_len;
52b3ac4aedSchristos 
53b3ac4aedSchristos   if (needle_len == 0)
54b3ac4aedSchristos     /* The first occurrence of the empty string is deemed to occur at
55b3ac4aedSchristos        the beginning of the string.  */
56b3ac4aedSchristos     return (void *) haystack;
57b3ac4aedSchristos 
58b3ac4aedSchristos   /* Sanity check, otherwise the loop might search through the whole
59b3ac4aedSchristos      memory.  */
60b3ac4aedSchristos   if (__builtin_expect (haystack_len < needle_len, 0))
61b3ac4aedSchristos     return NULL;
62b3ac4aedSchristos 
63b3ac4aedSchristos   for (begin = (const char *) haystack; begin <= last_possible; ++begin)
64b3ac4aedSchristos     if (begin[0] == ((const char *) needle)[0] &&
65b3ac4aedSchristos 	!memcmp ((const void *) &begin[1],
66b3ac4aedSchristos 		 (const void *) ((const char *) needle + 1),
67b3ac4aedSchristos 		 needle_len - 1))
68b3ac4aedSchristos       return (void *) begin;
69b3ac4aedSchristos 
70b3ac4aedSchristos   return NULL;
71b3ac4aedSchristos }
72