diff options
author | Martin Willi <martin@strongswan.org> | 2008-07-02 08:09:07 +0000 |
---|---|---|
committer | Martin Willi <martin@strongswan.org> | 2008-07-02 08:09:07 +0000 |
commit | fca4d3ee03fa7e020ace6512e5488c50fc56ef4a (patch) | |
tree | abf566660044f50c789ccc89bbe60cd3a05ce47f /src | |
parent | 2f2b99282aceaa8e001c536d4d231d27da49bea8 (diff) | |
download | strongswan-fca4d3ee03fa7e020ace6512e5488c50fc56ef4a.tar.bz2 strongswan-fca4d3ee03fa7e020ace6512e5488c50fc56ef4a.tar.xz |
implementation of a simple "token enumerator"
Diffstat (limited to 'src')
-rw-r--r-- | src/charon/plugins/unit_tester/tests.h | 3 | ||||
-rw-r--r-- | src/charon/plugins/unit_tester/tests/test_chunk.c | 2 | ||||
-rw-r--r-- | src/charon/plugins/unit_tester/tests/test_enumerator.c | 50 | ||||
-rw-r--r-- | src/libstrongswan/utils.h | 5 | ||||
-rw-r--r-- | src/libstrongswan/utils/enumerator.c | 139 | ||||
-rw-r--r-- | src/libstrongswan/utils/enumerator.h | 13 |
6 files changed, 209 insertions, 3 deletions
diff --git a/src/charon/plugins/unit_tester/tests.h b/src/charon/plugins/unit_tester/tests.h index dd3878c76..6826fabac 100644 --- a/src/charon/plugins/unit_tester/tests.h +++ b/src/charon/plugins/unit_tester/tests.h @@ -24,6 +24,7 @@ DEFINE_TEST("linked_list_t->remove()", test_list_remove, FALSE) DEFINE_TEST("simple enumerator", test_enumerate, FALSE) DEFINE_TEST("nested enumerator", test_enumerate_nested, FALSE) DEFINE_TEST("filtered enumerator", test_enumerate_filtered, FALSE) +DEFINE_TEST("token enumerator", test_enumerate_token, TRUE) DEFINE_TEST("auth info", test_auth_info, FALSE) DEFINE_TEST("FIPS PRF", fips_prf_test, FALSE) DEFINE_TEST("CURL get", test_curl_get, FALSE) @@ -34,5 +35,5 @@ DEFINE_TEST("RSA key generation", test_rsa_gen, FALSE) DEFINE_TEST("RSA subjectPublicKeyInfo loading", test_rsa_load_any, FALSE) DEFINE_TEST("Mediation database key fetch", test_med_db, FALSE) DEFINE_TEST("AES-128 encryption", test_aes128, FALSE) -DEFINE_TEST("AES-XCBC", test_aes_xcbc, TRUE) +DEFINE_TEST("AES-XCBC", test_aes_xcbc, FALSE) DEFINE_TEST("Base64 converter", test_chunk_base64, FALSE) diff --git a/src/charon/plugins/unit_tester/tests/test_chunk.c b/src/charon/plugins/unit_tester/tests/test_chunk.c index e7a8586c9..5356c1d96 100644 --- a/src/charon/plugins/unit_tester/tests/test_chunk.c +++ b/src/charon/plugins/unit_tester/tests/test_chunk.c @@ -16,8 +16,6 @@ #include <library.h> #include <daemon.h> -#define countof(array) (sizeof(array)/sizeof(typeof(array[0]))) - /******************************************************************************* * Base64 encoding/decoding test ******************************************************************************/ diff --git a/src/charon/plugins/unit_tester/tests/test_enumerator.c b/src/charon/plugins/unit_tester/tests/test_enumerator.c index d17d62bef..a7f3dd822 100644 --- a/src/charon/plugins/unit_tester/tests/test_enumerator.c +++ b/src/charon/plugins/unit_tester/tests/test_enumerator.c @@ -212,3 +212,53 @@ bool test_enumerate_filtered() list->destroy(list); return !bad_data; } + +/******************************************************************************* + * token parser test + ******************************************************************************/ + +bool test_enumerate_token() +{ + enumerator_t *enumerator; + char *token; + int i, num; + struct { + char *string; + char *sep; + char *trim; + } tests[] = { + {"abc, cde, efg", ",", " "}, + {" abc 1:2 cde;3 4efg5. ", ":;.,", " 12345"}, + {"abc.cde,efg", ",.", ""}, + {" abc cde efg ", " ", " "}, + }; + + for (num = 0; num < countof(tests); num++) + { + i = 0; + enumerator = enumerator_create_token( + tests[num].string, tests[num].sep, tests[num].trim); + while (enumerator->enumerate(enumerator, &token)) + { + switch (i) + { + case 0: + if (!streq(token, "abc")) return FALSE; + break; + case 1: + if (!streq(token, "cde")) return FALSE; + break; + case 2: + if (!streq(token, "efg")) return FALSE; + break; + default: + return FALSE; + } + i++; + } + enumerator->destroy(enumerator); + } + + return TRUE; +} + diff --git a/src/libstrongswan/utils.h b/src/libstrongswan/utils.h index 8b1a8aaba..2669be61a 100644 --- a/src/libstrongswan/utils.h +++ b/src/libstrongswan/utils.h @@ -90,6 +90,11 @@ #define malloc_thing(thing) ((thing*)malloc(sizeof(thing))) /** + * Get the number of elements in an array + */ +#define countof(array) (sizeof(array)/sizeof(array[0])) + +/** * Assign a function as a class method */ #define ASSIGN(method, function) (method = (typeof(method))function) diff --git a/src/libstrongswan/utils/enumerator.c b/src/libstrongswan/utils/enumerator.c index 67d0c8d1a..1d1c5e9a2 100644 --- a/src/libstrongswan/utils/enumerator.c +++ b/src/libstrongswan/utils/enumerator.c @@ -154,6 +154,145 @@ enumerator_t* enumerator_create_directory(char *path) } /** + * Enumerator implementation for directory enumerator + */ +typedef struct { + /** implements enumerator_t */ + enumerator_t public; + /** string to parse */ + char *string; + /** current position */ + char *pos; + /** separater chars */ + char *sep; + /** trim chars */ + char *trim; +} token_enum_t; + +/** + * Implementation of enumerator_create_token().destroy + */ +static void destroy_token_enum(token_enum_t *this) +{ + free(this->string); + free(this); +} + +/** + * Implementation of enumerator_create_token().enumerate + */ +static bool enumerate_token_enum(token_enum_t *this, char **token) +{ + char *pos = NULL, *tmp, *sep, *trim; + bool last = FALSE; + + /* trim leading characters/separators */ + while (*this->pos) + { + trim = this->trim; + while (*trim) + { + if (*trim == *this->pos) + { + this->pos++; + break; + } + trim++; + } + sep = this->sep; + while (*sep) + { + if (*sep == *this->pos) + { + this->pos++; + break; + } + sep++; + } + if (!*trim && !*sep) + { + break; + } + } + + /* find separators */ + sep = this->sep; + while (*sep) + { + tmp = strchr(this->pos, *sep); + if (tmp && (pos == NULL || tmp < pos)) + { + pos = tmp; + } + sep++; + } + *token = this->pos; + if (pos) + { + *pos = '\0'; + this->pos = pos + 1; + } + else + { + last = TRUE; + pos = this->pos = strchr(this->pos, '\0'); + } + + /* trim trailing characters/separators */ + pos--; + while (pos >= *token) + { + trim = this->trim; + while (*trim) + { + if (*trim == *pos) + { + *(pos--) = '\0'; + break; + } + trim++; + } + sep = this->sep; + while (*sep) + { + if (*sep == *pos) + { + *(pos--) = '\0'; + break; + } + sep++; + } + if (!*trim && !*sep) + { + break; + } + } + + if (!last || pos > *token) + { + return TRUE; + } + return FALSE; +} + +/** + * See header + */ +enumerator_t* enumerator_create_token(char *string, char *sep, char *trim) +{ + token_enum_t *enumerator = malloc_thing(token_enum_t); + + enumerator->public.enumerate = (void*)enumerate_token_enum; + enumerator->public.destroy = (void*)destroy_token_enum; + enumerator->string = strdup(string); + enumerator->pos = enumerator->string; + enumerator->sep = sep; + enumerator->trim = trim; + + return &enumerator->public; +} + +/** * enumerator for nested enumerations */ typedef struct { diff --git a/src/libstrongswan/utils/enumerator.h b/src/libstrongswan/utils/enumerator.h index 4de287890..ee749d3b7 100644 --- a/src/libstrongswan/utils/enumerator.h +++ b/src/libstrongswan/utils/enumerator.h @@ -98,6 +98,19 @@ enumerator_t *enumerator_create_single(void *item, void (*cleanup)(void *item)); enumerator_t* enumerator_create_directory(char *path); /** + * Create an enumerator over tokens of a string. + * + * Tokens are separated by one of the characters in sep and trimmed by the + * characters in trim. + * + * @param string string to parse + * @param sep separator characters + * @param trim characters to trim from tokens + * @return enumerator over char* tokens + */ +enumerator_t* enumerator_create_token(char *string, char *sep, char *trim); + +/** * Creates an enumerator which enumerates over enumerated enumerators :-). * * The variable argument list of enumeration values is limit to 5. |