From c3bdc3cd7fcb246f0345a39ae4678982b3259511 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Wed, 3 Dec 2008 10:03:59 +0000 Subject: [PATCH] enable quoted tokens in the token enumerator --- .../unit_tester/tests/test_enumerator.c | 2 + src/libstrongswan/utils/enumerator.c | 58 +++++++++++++------ 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/charon/plugins/unit_tester/tests/test_enumerator.c b/src/charon/plugins/unit_tester/tests/test_enumerator.c index 32d3ff8d2..6898084fc 100644 --- a/src/charon/plugins/unit_tester/tests/test_enumerator.c +++ b/src/charon/plugins/unit_tester/tests/test_enumerator.c @@ -231,6 +231,8 @@ bool test_enumerate_token() {" abc 1:2 cde;3 4efg5. ", ":;.,", " 12345"}, {"abc.cde,efg", ",.", ""}, {" abc cde efg ", " ", " "}, + {"a'abc' c 'cde' cefg", " ", " abcd"}, + {"'abc' abc 'cde'd 'efg'", " ", " abcd"}, }, tests2[] = { {"a, b, c", ",", " "}, {"a,b,c", ",", " "}, diff --git a/src/libstrongswan/utils/enumerator.c b/src/libstrongswan/utils/enumerator.c index ad8f7ad02..d4d32fb6f 100644 --- a/src/libstrongswan/utils/enumerator.c +++ b/src/libstrongswan/utils/enumerator.c @@ -1,4 +1,5 @@ /* + * Copyright (C) 2008 Tobias Brunner * Copyright (C) 2007 Martin Willi * Hochschule fuer Technik Rapperswil * @@ -215,27 +216,48 @@ static bool enumerate_token_enum(token_enum_t *this, char **token) } } - /* find separators */ - sep = this->sep; - while (*sep) + switch (*this->pos) { - tmp = strchr(this->pos, *sep); - if (tmp && (pos == NULL || tmp < pos)) + case '"': + case '\'': { - pos = tmp; + /* read quoted token */ + tmp = strchr(this->pos + 1, *this->pos); + if (tmp) + { + *token = this->pos + 1; + *tmp = '\0'; + this->pos = tmp + 1; + return TRUE; + } + /* unterminated string, FALL-THROUGH */ + } + default: + { + /* find nearest separator */ + 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'); + } + break; } - 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 */