Skip to content

Commit e5be53c

Browse files
committed
adding memmem fallback implementation proposal.
must be a rare occurence nowadays tough.
1 parent df4c276 commit e5be53c

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

configure.ac

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ if test "$ac_cv_func_getaddrinfo" = yes; then
686686
AC_DEFINE(HAVE_GETADDRINFO,1,[Define if you have the getaddrinfo function])
687687
fi
688688

689-
AC_REPLACE_FUNCS(strlcat strlcpy explicit_bzero getopt)
689+
AC_REPLACE_FUNCS(strlcat strlcpy explicit_bzero getopt memmem)
690690
AC_FUNC_ALLOCA
691691
PHP_TIME_R_TYPE
692692
PHP_CHECK_IN_ADDR_T
@@ -1612,7 +1612,7 @@ PHP_ADD_SOURCES(main, main.c snprintf.c spprintf.c \
16121612
fopen_wrappers.c alloca.c php_scandir.c \
16131613
php_ini.c SAPI.c rfc1867.c php_content_types.c strlcpy.c \
16141614
strlcat.c explicit_bzero.c reentrancy.c php_variables.c php_ticks.c \
1615-
network.c php_open_temporary_file.c \
1615+
network.c php_open_temporary_file.c memmem.c \
16161616
output.c getopt.c php_syslog.c, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
16171617

16181618
PHP_ADD_SOURCES_X(main, fastcgi.c, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1, PHP_FASTCGI_OBJS, no)

main/memmem.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| license@php.net so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Author: |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#include "php.h"
18+
19+
#ifndef HAVE_MEMMEM
20+
21+
#include <string.h>
22+
23+
void* memmem(const void *haystack, size_t hlen, const void *needle, size_t nlen)
24+
{
25+
const char *shaystack = (const char *)haystack;
26+
const char *sneedle = (const char *)needle;
27+
const char *s, *end;
28+
29+
// handful of optimisation cases before doing any heavy lifting
30+
if (!hlen)
31+
return (void *)haystack;
32+
else if (hlen == 1)
33+
return memchr(haystack, *sneedle, hlen);
34+
else if (hlen < nlen)
35+
return NULL;
36+
37+
s = shaystack;
38+
end = s + hlen - nlen;
39+
40+
while (s <= end) {
41+
if (memcmp(s, sneedle, nlen) == 0)
42+
return (void *)s;
43+
++s;
44+
}
45+
46+
return NULL;
47+
}
48+
#endif

main/php.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ END_EXTERN_C()
179179
#define explicit_bzero php_explicit_bzero
180180
#endif
181181

182+
#ifndef HAVE_MEMMEM
183+
BEGIN_EXTERN_C()
184+
void* memmem(const void *haystack, size_t hlen, const void *needle, size_t nlen);
185+
END_EXTERN_C()
186+
#endif
187+
182188
#ifndef HAVE_STRTOK_R
183189
BEGIN_EXTERN_C()
184190
char *strtok_r(char *s, const char *delim, char **last);

0 commit comments

Comments
 (0)