1 /*
2
3 ratproxy - strcasestr implementation
4 ------------------------------------
5
6 Some modern operating systems still ship with no strcasestr() or memmem()
7 implementations in place, for reasons beyond comprehension.
8
9 This is a simplified version of the code that ships with NetBSD. The
10 original code is licensed under a BSD license, as follows:
11
12 Copyright (c) 1990, 1993
13 The Regents of the University of California. All rights reserved.
14
15 This code is derived from software contributed to Berkeley by
16 Chris Torek.
17
18 Redistribution and use in source and binary forms, with or without
19 modification, are permitted provided that the following conditions
20 are met:
21 1. Redistributions of source code must retain the above copyright
22 notice, this list of conditions and the following disclaimer.
23 2. Redistributions in binary form must reproduce the above copyright
24 notice, this list of conditions and the following disclaimer in the
25 documentation and/or other materials provided with the distribution.
26 3. Neither the name of the University nor the names of its contributors
27 may be used to endorse or promote products derived from this software
28 without specific prior written permission.
29
30 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 SUCH DAMAGE.
41
42 */
43
44 #ifndef _HAVE_STRCASESTR_INL_H
45 #define _HAVE_STRCASESTR_INL_H
46
47 #include "types.h"
48
rp_strcasestr(const _u8 * haystack,const _u8 * needle)49 static inline _u8* rp_strcasestr(const _u8* haystack, const _u8* needle) {
50 _u8 c, sc;
51 _u32 len;
52
53 if (!haystack || !needle) return 0;
54
55 if ((c = *needle++)) {
56
57 c = tolower(c);
58 len = strlen(needle);
59
60 do {
61 do {
62 if (!(sc = *haystack++)) return 0;
63 } while (tolower(sc) != c);
64 } while (strncasecmp(haystack, needle, len));
65
66 haystack--;
67
68 }
69
70 return (_u8*)haystack;
71
72 }
73
74
rp_memmem(const void * haystack,_u32 h_len,const void * needle,_u32 n_len)75 static inline void* rp_memmem(const void* haystack, _u32 h_len, const void* needle, _u32 n_len) {
76 _u8* sp = (_u8*)haystack;
77 _u8* pp = (_u8*)needle;
78 _u8* eos = sp + h_len - n_len;
79
80 if (!(haystack && needle && h_len && n_len)) return 0;
81
82 while (sp <= eos) {
83 if (*sp == *pp)
84 if (memcmp(sp, pp, n_len) == 0) return sp;
85 sp++;
86 }
87
88 return 0;
89
90 }
91
92 #endif /* !_HAVE_STRCASESTR_INL_H */
93