From 7705676a9e7765690b924e457da219d14c7e2cb6 Mon Sep 17 00:00:00 2001 From: Jeremy Lakeman Date: Mon, 6 Aug 2012 12:25:45 +0930 Subject: [PATCH] Move str functions to c file to enable reuse --- Android.mk | 1 + Makefile.in | 2 ++ rhizome_http.c | 37 +------------------------------------ str.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ str.h | 41 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 89 insertions(+), 36 deletions(-) create mode 100644 str.c create mode 100644 str.h diff --git a/Android.mk b/Android.mk index e47369cd..488d9c79 100644 --- a/Android.mk +++ b/Android.mk @@ -42,6 +42,7 @@ SERVALD_SRC_FILES = \ serval-dna/sha2.c \ serval-dna/simulate.c \ serval-dna/srandomdev.c \ + serval-dna/str.c \ serval-dna/keyring.c \ serval-dna/vomp.c \ serval-dna/lsif.c \ diff --git a/Makefile.in b/Makefile.in index 116d3eb2..2bd249ca 100644 --- a/Makefile.in +++ b/Makefile.in @@ -53,6 +53,7 @@ SRCS= \ simulate.c \ sqlite3.c \ srandomdev.c \ + str.c \ strbuf.c \ strbuf_helpers.c \ vomp.c \ @@ -63,6 +64,7 @@ MONITORCLIENTSRCS=conf.c \ mkdir.c \ monitor-client.c \ net.c \ + str.c \ strbuf.c \ strbuf_helpers.c diff --git a/rhizome_http.c b/rhizome_http.c index f324a351..1c6d264b 100644 --- a/rhizome_http.c +++ b/rhizome_http.c @@ -20,9 +20,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include #include -#include #include "serval.h" +#include "str.h" #include "rhizome.h" typedef struct rhizome_http_request { @@ -501,41 +501,6 @@ int http_header_complete(const char *buf, size_t len, size_t tail) return count == 2; } -/* Check if a given string starts with a given sub-string. If so, return 1 and, if afterp is not - NULL, set *afterp to point to the character immediately following the substring. Otherwise - return 0. - This function is used to parse HTTP headers and responses, which are typically not - nul-terminated, but are held in a buffer which has an associated length. To avoid this function - running past the end of the buffer, the caller must ensure that the buffer contains a sub-string - that is not part of the sub-string being sought, eg, "\r\n\r\n" as detected by - http_header_complete(). This guarantees that this function will return nonzero before running - past the end of the buffer. - @author Andrew Bettison - */ -int str_startswith(char *str, const char *substring, char **afterp) -{ - while (*substring && *substring == *str) - ++substring, ++str; - if (*substring) - return 0; - if (afterp) - *afterp = str; - return 1; -} - -/* Case-insensitive form of str_startswith(). - */ -int strcase_startswith(char *str, const char *substring, char **afterp) -{ - while (*substring && *str && toupper(*substring) == toupper(*str)) - ++substring, ++str; - if (*substring) - return 0; - if (afterp) - *afterp = str; - return 1; -} - static int rhizome_server_parse_http_request(rhizome_http_request *r) { /* Switching to writing, so update the call-back */ diff --git a/str.c b/str.c new file mode 100644 index 00000000..496760ab --- /dev/null +++ b/str.c @@ -0,0 +1,44 @@ +/* + Serval string primitives + Copyright (C) 2012 Serval Project Inc. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include +#include "str.h" + +int str_startswith(char *str, const char *substring, char **afterp) +{ + while (*substring && *substring == *str) + ++substring, ++str; + if (*substring) + return 0; + if (afterp) + *afterp = str; + return 1; +} + +int strcase_startswith(char *str, const char *substring, char **afterp) +{ + while (*substring && *str && toupper(*substring) == toupper(*str)) + ++substring, ++str; + if (*substring) + return 0; + if (afterp) + *afterp = str; + return 1; +} + diff --git a/str.h b/str.h new file mode 100644 index 00000000..7b792700 --- /dev/null +++ b/str.h @@ -0,0 +1,41 @@ +/* + Serval string primitives + Copyright (C) 2012 Serval Project Inc. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef __STR_H__ +#define __STR_H__ + +/* Check if a given string starts with a given sub-string. If so, return 1 and, if afterp is not + NULL, set *afterp to point to the character immediately following the substring. Otherwise + return 0. + This function is used to parse HTTP headers and responses, which are typically not + nul-terminated, but are held in a buffer which has an associated length. To avoid this function + running past the end of the buffer, the caller must ensure that the buffer contains a sub-string + that is not part of the sub-string being sought, eg, "\r\n\r\n" as detected by + http_header_complete(). This guarantees that this function will return nonzero before running + past the end of the buffer. + @author Andrew Bettison + */ +int str_startswith(char *str, const char *substring, char **afterp); + +/* Case-insensitive form of str_startswith(). + */ +int strcase_startswith(char *str, const char *substring, char **afterp); + + +#endif \ No newline at end of file