aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/libstrongswan/utils.c17
-rw-r--r--src/libstrongswan/utils.h6
-rw-r--r--src/libstrongswan/utils/lexparser.c27
-rw-r--r--src/libstrongswan/utils/lexparser.h9
4 files changed, 57 insertions, 2 deletions
diff --git a/src/libstrongswan/utils.c b/src/libstrongswan/utils.c
index aa50b86b4..b1974cf97 100644
--- a/src/libstrongswan/utils.c
+++ b/src/libstrongswan/utils.c
@@ -79,6 +79,23 @@ void memxor(u_int8_t dest[], u_int8_t src[], size_t n)
/**
* Described in header.
*/
+void *memstr(const void *haystack, const char *needle, size_t n)
+{
+ unsigned const char *pos = haystack;
+ size_t l = strlen(needle);
+ for (; n >= l; ++pos, --n)
+ {
+ if (memeq(pos, needle, l))
+ {
+ return (void*)pos;
+ }
+ }
+ return NULL;
+}
+
+/**
+ * Described in header.
+ */
bool mkdir_p(const char *path, mode_t mode)
{
size_t len;
diff --git a/src/libstrongswan/utils.h b/src/libstrongswan/utils.h
index 298253fdd..c794b0797 100644
--- a/src/libstrongswan/utils.h
+++ b/src/libstrongswan/utils.h
@@ -225,6 +225,12 @@ void *clalloc(void *pointer, size_t size);
void memxor(u_int8_t dest[], u_int8_t src[], size_t n);
/**
+ * A variant of strstr with the characteristics of memchr, where haystack is not
+ * a null-terminated string but simply a memory area of length n.
+ */
+void *memstr(const void *haystack, const char *needle, size_t n);
+
+/**
* Creates a directory and all required parent directories.
*
* @param path path to the new directory
diff --git a/src/libstrongswan/utils/lexparser.c b/src/libstrongswan/utils/lexparser.c
index 9019c2602..34eb340a5 100644
--- a/src/libstrongswan/utils/lexparser.c
+++ b/src/libstrongswan/utils/lexparser.c
@@ -76,6 +76,33 @@ bool extract_token(chunk_t *token, const char termination, chunk_t *src)
}
/**
+ * extracts a token ending with the first occurrence of a given null-terminated string
+ */
+bool extract_token_str(chunk_t *token, const char *termination, chunk_t *src)
+{
+ u_char *eot = memstr(src->ptr, termination, src->len);
+ size_t l = strlen(termination);
+
+ /* initialize empty token */
+ *token = chunk_empty;
+
+ if (eot == NULL) /* termination string not found */
+ {
+ return FALSE;
+ }
+
+ /* extract token */
+ token->ptr = src->ptr;
+ token->len = (u_int)(eot - src->ptr);
+
+ /* advance src pointer after termination string */
+ src->ptr = eot + l;
+ src->len -= (token->len + l);
+
+ return TRUE;
+}
+
+/**
* extracts a token ending with the last occurrence of a given termination symbol
*/
bool extract_last_token(chunk_t *token, const char termination, chunk_t *src)
diff --git a/src/libstrongswan/utils/lexparser.h b/src/libstrongswan/utils/lexparser.h
index 1cbbc05f7..46a715b21 100644
--- a/src/libstrongswan/utils/lexparser.h
+++ b/src/libstrongswan/utils/lexparser.h
@@ -37,12 +37,17 @@ bool eat_whitespace(chunk_t *src);
bool match(const char *pattern, const chunk_t *ch);
/**
- * Extracts a token ending with the first occurence a given termination symbol
+ * Extracts a token ending with the first occurrence of a given termination symbol
*/
bool extract_token(chunk_t *token, const char termination, chunk_t *src);
/**
- * Extracts a token ending with the last occurence a given termination symbol
+ * Extracts a token ending with the first occurrence of a given null-terminated string
+ */
+bool extract_token_str(chunk_t *token, const char *termination, chunk_t *src);
+
+/**
+ * Extracts a token ending with the last occurrence of a given termination symbol
*/
bool extract_last_token(chunk_t *token, const char termination, chunk_t *src);