| 1 | /* Copyright (C) 1991-2019 Free Software Foundation, Inc. | 
| 2 |    This file is part of the GNU C Library. | 
| 3 |  | 
| 4 |    The GNU C Library is free software; you can redistribute it and/or | 
| 5 |    modify it under the terms of the GNU Lesser General Public | 
| 6 |    License as published by the Free Software Foundation; either | 
| 7 |    version 2.1 of the License, or (at your option) any later version. | 
| 8 |  | 
| 9 |    The GNU C Library is distributed in the hope that it will be useful, | 
| 10 |    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 11 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
| 12 |    Lesser General Public License for more details. | 
| 13 |  | 
| 14 |    You should have received a copy of the GNU Lesser General Public | 
| 15 |    License along with the GNU C Library; if not, see | 
| 16 |    <http://www.gnu.org/licenses/>.  */ | 
| 17 |  | 
| 18 | /* This particular implementation was written by Eric Blake, 2008.  */ | 
| 19 |  | 
| 20 | #ifndef _LIBC | 
| 21 | # include <config.h> | 
| 22 | #endif | 
| 23 |  | 
| 24 | /* Specification of memmem.  */ | 
| 25 | #include <string.h> | 
| 26 |  | 
| 27 | #ifndef _LIBC | 
| 28 | # define __builtin_expect(expr, val)   (expr) | 
| 29 | # define __memmem	memmem | 
| 30 | #endif | 
| 31 |  | 
| 32 | #define RETURN_TYPE void * | 
| 33 | #define AVAILABLE(h, h_l, j, n_l) ((j) <= (h_l) - (n_l)) | 
| 34 | #define FASTSEARCH(S,C,N) (void*) memchr ((void *)(S), (C), (N)) | 
| 35 | #include "str-two-way.h" | 
| 36 |  | 
| 37 | #undef memmem | 
| 38 |  | 
| 39 | /* Return the first occurrence of NEEDLE in HAYSTACK.  Return HAYSTACK | 
| 40 |    if NEEDLE_LEN is 0, otherwise NULL if NEEDLE is not found in | 
| 41 |    HAYSTACK.  */ | 
| 42 | void * | 
| 43 | __memmem (const void *haystack_start, size_t haystack_len, | 
| 44 | 	  const void *needle_start, size_t needle_len) | 
| 45 | { | 
| 46 |   /* Abstract memory is considered to be an array of 'unsigned char' values, | 
| 47 |      not an array of 'char' values.  See ISO C 99 section 6.2.6.1.  */ | 
| 48 |   const unsigned char *haystack = (const unsigned char *) haystack_start; | 
| 49 |   const unsigned char *needle = (const unsigned char *) needle_start; | 
| 50 |  | 
| 51 |   if (needle_len == 0) | 
| 52 |     /* The first occurrence of the empty string is deemed to occur at | 
| 53 |        the beginning of the string.  */ | 
| 54 |     return (void *) haystack; | 
| 55 |  | 
| 56 |   /* Sanity check, otherwise the loop might search through the whole | 
| 57 |      memory.  */ | 
| 58 |   if (__glibc_unlikely (haystack_len < needle_len)) | 
| 59 |     return NULL; | 
| 60 |  | 
| 61 |   /* Use optimizations in memchr when possible, to reduce the search | 
| 62 |      size of haystack using a linear algorithm with a smaller | 
| 63 |      coefficient.  However, avoid memchr for long needles, since we | 
| 64 |      can often achieve sublinear performance.  */ | 
| 65 |   if (needle_len < LONG_NEEDLE_THRESHOLD) | 
| 66 |     { | 
| 67 |       haystack = memchr (haystack, *needle, haystack_len); | 
| 68 |       if (!haystack || __builtin_expect (needle_len == 1, 0)) | 
| 69 | 	return (void *) haystack; | 
| 70 |       haystack_len -= haystack - (const unsigned char *) haystack_start; | 
| 71 |       if (haystack_len < needle_len) | 
| 72 | 	return NULL; | 
| 73 |       /* Check whether we have a match.  This improves performance since we | 
| 74 | 	 avoid the initialization overhead of the two-way algorithm.  */ | 
| 75 |       if (memcmp (haystack, needle, needle_len) == 0) | 
| 76 | 	return (void *) haystack; | 
| 77 |       return two_way_short_needle (haystack, haystack_len, needle, needle_len); | 
| 78 |     } | 
| 79 |   else | 
| 80 |     return two_way_long_needle (haystack, haystack_len, needle, needle_len); | 
| 81 | } | 
| 82 | libc_hidden_def (__memmem) | 
| 83 | weak_alias (__memmem, memmem) | 
| 84 | libc_hidden_weak (memmem) | 
| 85 |  | 
| 86 | #undef LONG_NEEDLE_THRESHOLD | 
| 87 |  |