memmem.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "gnutls_global.h"
00023 #include <string.h>
00024
00025 #if !HAVE_MEMMEM
00026
00027 #ifndef _LIBC
00028 # define __builtin_expect(expr, val) (expr)
00029 #endif
00030
00031 #define RETURN_TYPE void *
00032 #define AVAILABLE(h, h_l, j, n_l) ((j) <= (h_l) - (n_l))
00033 #include "str-two-way.h"
00034
00035
00036
00037
00038 void *
00039 memmem (const void *haystack_start, size_t haystack_len,
00040 const void *needle_start, size_t needle_len)
00041 {
00042
00043
00044 const unsigned char *haystack = (const unsigned char *) haystack_start;
00045 const unsigned char *needle = (const unsigned char *) needle_start;
00046
00047 if (needle_len == 0)
00048
00049
00050 return (void *) haystack;
00051
00052
00053
00054 if (__builtin_expect (haystack_len < needle_len, 0))
00055 return NULL;
00056
00057
00058
00059
00060
00061 if (needle_len < LONG_NEEDLE_THRESHOLD)
00062 {
00063 haystack = memchr (haystack, *needle, haystack_len);
00064 if (!haystack || __builtin_expect (needle_len == 1, 0))
00065 return (void *) haystack;
00066 haystack_len -= haystack - (const unsigned char *) haystack_start;
00067 if (haystack_len < needle_len)
00068 return NULL;
00069 return two_way_short_needle (haystack, haystack_len, needle,
00070 needle_len);
00071 }
00072 else
00073 return two_way_long_needle (haystack, haystack_len, needle, needle_len);
00074 }
00075
00076 #undef LONG_NEEDLE_THRESHOLD
00077
00078 #endif