From 7d22ce5573952dec6e3fa724d5e6f071cf8947ff Mon Sep 17 00:00:00 2001
From: Yang Tse <yangsita@gmail.com>
Date: Mon, 28 Sep 2009 16:05:20 +0000
Subject: [PATCH] libcurl private function Curl_memrchr() now in curl_memrchr.c
 and curl_memrchr.h

---
 lib/Makefile.Watcom                | 10 ++++-
 lib/Makefile.inc                   |  7 +++-
 lib/Makefile.riscos                |  7 +++-
 lib/Makefile.vc6                   |  1 +
 lib/cookie.c                       | 18 +--------
 lib/curl_memrchr.c                 | 63 ++++++++++++++++++++++++++++++
 lib/curl_memrchr.h                 | 45 +++++++++++++++++++++
 lib/makefile.amiga                 |  4 +-
 packages/Symbian/group/libcurl.mmp |  2 +-
 packages/TPF/curl.mak              |  1 +
 10 files changed, 134 insertions(+), 24 deletions(-)
 create mode 100644 lib/curl_memrchr.c
 create mode 100644 lib/curl_memrchr.h

diff --git a/lib/Makefile.Watcom b/lib/Makefile.Watcom
index 610ae640b1..306d876b59 100644
--- a/lib/Makefile.Watcom
+++ b/lib/Makefile.Watcom
@@ -70,7 +70,7 @@ OBJS = $(OBJ_DIR)\base64.obj           $(OBJ_DIR)\connect.obj        &
        $(OBJ_DIR)\timeval.obj          $(OBJ_DIR)\transfer.obj       &
        $(OBJ_DIR)\url.obj              $(OBJ_DIR)\version.obj        &
        $(OBJ_DIR)\slist.obj            $(OBJ_DIR)\nonblock.obj       &
-       $(OBJ_DIR)\curl_rand.obj
+       $(OBJ_DIR)\curl_rand.obj        $(OBJ_DIR)\curl_memrchr.obj
 
 #
 # Use $(OBJS) as a template to generate $(OBJS_STAT) and $(OBJS_DYN).
@@ -194,7 +194,7 @@ $(OBJ_DIR)\cookie.obj: cookie.c setup.h config-win32.h ..\include\curl\curlbuild
   ..\include\curl\multi.h urldata.h cookie.h ..\include\curl\curl.h &
   formdata.h timeval.h http_chunks.h hostip.h hash.h llist.h &
   curl_addrinfo.h splay.h strequal.h strtok.h sendf.h curl_memory.h share.h &
-  strtoofft.h rawstr.h memdebug.h
+  strtoofft.h rawstr.h memdebug.h curl_memrchr.h
 $(OBJ_DIR)\http.obj: http.c setup.h config-win32.h ..\include\curl\curlbuild.h &
   ..\include\curl\curlrules.h setup_once.h urldata.h cookie.h &
   ..\include\curl\curl.h ..\include\curl\curlver.h &
@@ -548,3 +548,9 @@ $(OBJ_DIR)\curl_rand.obj: curl_rand.c setup.h config-win32.h &
   ..\include\curl\curlrules.h ..\include\curl\easy.h &
   ..\include\curl\multi.h ..\include\curl\curl.h curl_rand.h &
   ..\include\curl\mprintf.h curl_memory.h memdebug.h
+$(OBJ_DIR)\curl_memrchr.obj: curl_memrchr.c setup.h config-win32.h &
+  ..\include\curl\curlbuild.h ..\include\curl\curlrules.h setup_once.h &
+  ..\include\curl\curl.h ..\include\curl\curlver.h &
+  ..\include\curl\curlrules.h ..\include\curl\easy.h &
+  ..\include\curl\multi.h ..\include\curl\curl.h curl_memrchr.h &
+  ..\include\curl\mprintf.h curl_memory.h memdebug.h
diff --git a/lib/Makefile.inc b/lib/Makefile.inc
index 34961229c1..1b619a350e 100644
--- a/lib/Makefile.inc
+++ b/lib/Makefile.inc
@@ -10,7 +10,8 @@ CSOURCES = file.c timeval.c base64.c hostip.c progress.c formdata.c	\
   hostares.c hostasyn.c hostip4.c hostip6.c hostsyn.c hostthre.c	\
   inet_ntop.c parsedate.c select.c gtls.c sslgen.c tftp.c splay.c	\
   strdup.c socks.c ssh.c nss.c qssl.c rawstr.c curl_addrinfo.c          \
-  socks_gssapi.c socks_sspi.c curl_sspi.c slist.c nonblock.c
+  socks_gssapi.c socks_sspi.c curl_sspi.c slist.c nonblock.c		\
+  curl_memrchr.c
 
 HHEADERS = arpa_telnet.h netrc.h file.h timeval.h qssl.h hostip.h	\
   progress.h formdata.h cookie.h http.h sendf.h ftp.h url.h dict.h	\
@@ -21,4 +22,6 @@ HHEADERS = arpa_telnet.h netrc.h file.h timeval.h qssl.h hostip.h	\
   strtoofft.h strerror.h inet_ntop.h curlx.h curl_memory.h setup.h	\
   transfer.h select.h easyif.h multiif.h parsedate.h sslgen.h gtls.h	\
   tftp.h sockaddr.h splay.h strdup.h setup_once.h socks.h ssh.h nssg.h	\
-  curl_base64.h rawstr.h curl_addrinfo.h curl_sspi.h slist.h nonblock.h
+  curl_base64.h rawstr.h curl_addrinfo.h curl_sspi.h slist.h nonblock.h	\
+  curl_memrchr.h
+
diff --git a/lib/Makefile.riscos b/lib/Makefile.riscos
index d192f6fa56..8eaa8ee47b 100644
--- a/lib/Makefile.riscos
+++ b/lib/Makefile.riscos
@@ -13,7 +13,9 @@ objs =	o.base64 o.connect o.cookie o.dict \
 	o.security o.select o.sendf o.speedcheck o.ssluse \
 	o.strequal o.strtok o.telnet o.timeval \
 	o.transfer o.url o.version o.strtoofft o.sslgen o.gtls \
-	o.rawstr o.curl_addrinfo o.slist o.nonblock o.curl_rand
+	o.rawstr o.curl_addrinfo o.slist o.nonblock o.curl_rand \
+	o.curl_memrchr
+
 
 # Compile options:
 linkopts	= -o libcurl
@@ -36,6 +38,9 @@ o.cookie:	c.cookie
 o.curl_addrinfo:	c.curl_addrinfo
 		gcc $(compileropts) -c -o curl_addrinfo.o c.curl_addrinfo
 
+o.curl_memrchr:	c.curl_memrchr
+		gcc $(compileropts) -c -o curl_memrchr.o c.curl_memrchr
+
 o.curl_rand:	c.curl_rand
 		gcc $(compileropts) -c -o curl_rand.o c.curl_rand
 
diff --git a/lib/Makefile.vc6 b/lib/Makefile.vc6
index 8ad31981d6..8fb394577c 100644
--- a/lib/Makefile.vc6
+++ b/lib/Makefile.vc6
@@ -450,6 +450,7 @@ X_OBJS= \
 	$(DIROBJ)\content_encoding.obj \
 	$(DIROBJ)\cookie.obj \
 	$(DIROBJ)\curl_addrinfo.obj \
+	$(DIROBJ)\curl_memrchr.obj \
 	$(DIROBJ)\curl_rand.obj \
 	$(DIROBJ)\curl_sspi.obj \
 	$(DIROBJ)\dict.obj \
diff --git a/lib/cookie.c b/lib/cookie.c
index 13941857c9..6b19ab1669 100644
--- a/lib/cookie.c
+++ b/lib/cookie.c
@@ -97,6 +97,7 @@ Example set of cookies:
 #include "share.h"
 #include "strtoofft.h"
 #include "rawstr.h"
+#include "curl_memrchr.h"
 
 /* The last #include file should be: */
 #include "memdebug.h"
@@ -168,23 +169,6 @@ static void strstore(char **str, const char *newstr)
 }
 
 
-/*
- * The memrchr() function is like the memchr() function, except that it
- * searches backwards from the end of the n bytes pointed to by s instead of
- * forwards from the front.
- *
- * Exists in glibc but is not widely available on other systems.
- */
-static void *memrchr(const char *s, int c, size_t n)
-{
-  while(n--) {
-    if(s[n] == c)
-      return &s[n];
-  }
-  return NULL;
-}
-
-
 /****************************************************************************
  *
  * Curl_cookie_add()
diff --git a/lib/curl_memrchr.c b/lib/curl_memrchr.c
new file mode 100644
index 0000000000..7ba16f3bca
--- /dev/null
+++ b/lib/curl_memrchr.c
@@ -0,0 +1,63 @@
+/***************************************************************************
+ *                                  _   _ ____  _
+ *  Project                     ___| | | |  _ \| |
+ *                             / __| | | | |_) | |
+ *                            | (__| |_| |  _ <| |___
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ * $Id$
+ ***************************************************************************/
+
+#include "setup.h"
+
+#include "curl_memrchr.h"
+
+#define _MPRINTF_REPLACE /* use our functions only */
+#include <curl/mprintf.h>
+
+#include "curl_memory.h"
+/* The last #include file should be: */
+#include "memdebug.h"
+
+#ifndef HAVE_MEMRCHR
+
+/*
+ * Curl_memrchr()
+ *
+ * Our memrchr() function clone for systems which lack this function. The
+ * memrchr() function is like the memchr() function, except that it searches
+ * backwards from the end of the n bytes pointed to by s instead of forward
+ * from the beginning.
+ */
+
+void *
+Curl_memrchr(const void *s, int c, size_t n)
+{
+  const unsigned char *p = s;
+  const unsigned char *q = s;
+
+  p += n - 1;
+
+  while (p >= q) {
+    if (*p == (unsigned char)c)
+      return (void *)p;
+    p--;
+  }
+
+  return NULL;
+}
+
+#endif /* HAVE_MEMRCHR */
diff --git a/lib/curl_memrchr.h b/lib/curl_memrchr.h
new file mode 100644
index 0000000000..2096b6e8b7
--- /dev/null
+++ b/lib/curl_memrchr.h
@@ -0,0 +1,45 @@
+#ifndef HEADER_CURL_MEMRCHR_H
+#define HEADER_CURL_MEMRCHR_H
+/***************************************************************************
+ *                                  _   _ ____  _
+ *  Project                     ___| | | |  _ \| |
+ *                             / __| | | | |_) | |
+ *                            | (__| |_| |  _ <| |___
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ * $Id$
+ ***************************************************************************/
+
+#include "setup.h"
+
+#ifdef HAVE_MEMRCHR
+
+#ifdef HAVE_STRING_H
+#  include <string.h>
+#endif
+#ifdef HAVE_STRINGS_H
+#  include <strings.h>
+#endif
+
+#else /* HAVE_MEMRCHR */
+
+void *Curl_memrchr(const void *s, int c, size_t n);
+
+#define memrchr(x,y,z) Curl_memrchr((x),(y),(z))
+
+#endif /* HAVE_MEMRCHR */
+
+#endif /* HEADER_CURL_MEMRCHR_H */
diff --git a/lib/makefile.amiga b/lib/makefile.amiga
index a867368465..2dd423e60a 100644
--- a/lib/makefile.amiga
+++ b/lib/makefile.amiga
@@ -18,7 +18,9 @@ OBJS = amigaos.c base64.c connect.c content_encoding.c cookie.c dict.c easy.c \
 	progress.c security.c select.c sendf.c share.c speedcheck.c ssluse.c  \
 	strequal.c strtok.c telnet.c timeval.c transfer.c url.c version.c     \
 	sslgen.c gtls.c strerror.c rawstr.c curl_addrinfo.c curl_rand.c	      \
-	socks_gssapi.c socks_sspi.c curl_sspi.c slist.c nonblock.c
+	socks_gssapi.c socks_sspi.c curl_sspi.c slist.c nonblock.c            \
+	curl_memrchr.c
+
 
 all:	$(OBJS:.c=.o)
 	ar cru libcurl.a $(OBJS:.c=.o)
diff --git a/packages/Symbian/group/libcurl.mmp b/packages/Symbian/group/libcurl.mmp
index a01c01a820..8c6c926f26 100644
--- a/packages/Symbian/group/libcurl.mmp
+++ b/packages/Symbian/group/libcurl.mmp
@@ -32,7 +32,7 @@ SOURCE \
   inet_ntop.c parsedate.c select.c gtls.c sslgen.c tftp.c splay.c   \
   strdup.c socks.c ssh.c nss.c qssl.c rawstr.c curl_addrinfo.c      \
   socks_gssapi.c socks_sspi.c curl_sspi.c slist.c nonblock.c        \
-  curl_rand.c
+  curl_rand.c curl_memrchr.c
 
 USERINCLUDE   ../../../lib ../../../include/curl
 #ifdef ENABLE_SSL
diff --git a/packages/TPF/curl.mak b/packages/TPF/curl.mak
index cd2d25666e..7223b9476a 100644
--- a/packages/TPF/curl.mak
+++ b/packages/TPF/curl.mak
@@ -35,6 +35,7 @@ C_SRC += connect.c
 C_SRC += content_encoding.c
 C_SRC += cookie.c
 C_SRC += curl_addrinfo.c
+C_SRC += curl_memrchr.c
 C_SRC += curl_rand.c
 C_SRC += curl_sspi.c
 C_SRC += dict.c
-- 
GitLab