]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
added memstr and extract_token_str helper functions
authorTobias Brunner <tobias@strongswan.org>
Wed, 3 Dec 2008 09:45:58 +0000 (09:45 -0000)
committerTobias Brunner <tobias@strongswan.org>
Wed, 3 Dec 2008 09:45:58 +0000 (09:45 -0000)
src/libstrongswan/utils.c
src/libstrongswan/utils.h
src/libstrongswan/utils/lexparser.c
src/libstrongswan/utils/lexparser.h

index aa50b86b4fca24a605d3a5bbb96ed862b2dd8cae..b1974cf97ff1dc44cda67402f3a3e1f0fb9172de 100644 (file)
@@ -76,6 +76,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.
  */
index 298253fdd4a684bb1180adcd46516b6fc4c7f1bb..c794b079732a13875716a3b8c43499b6b8dc8102 100644 (file)
@@ -224,6 +224,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. 
  *
index 9019c26025fcd14c641e823a4f476ced5731a4c3..34eb340a564577691f121bac613921e71812c3c2 100644 (file)
@@ -75,6 +75,33 @@ bool extract_token(chunk_t *token, const char termination, chunk_t *src)
        return TRUE;
 }
 
+/**
+ * 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
  */
index 1cbbc05f77342dae800312811907b94cbdcd1731..46a715b214c8a5ca506fe92ea32838a837a7de2c 100644 (file)
@@ -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);