diff options
author | Yves-Alexis Perez <corsac@corsac.net> | 2012-06-28 21:16:07 +0200 |
---|---|---|
committer | Yves-Alexis Perez <corsac@corsac.net> | 2012-06-28 21:16:07 +0200 |
commit | b34738ed08c2227300d554b139e2495ca5da97d6 (patch) | |
tree | 62f33b52820f2e49f0e53c0f8c636312037c8054 /src/libstrongswan/credentials/ietf_attributes | |
parent | 0a9d51a49042a68daa15b0c74a2b7f152f52606b (diff) | |
download | vyos-strongswan-b34738ed08c2227300d554b139e2495ca5da97d6.tar.gz vyos-strongswan-b34738ed08c2227300d554b139e2495ca5da97d6.zip |
Imported Upstream version 4.6.4
Diffstat (limited to 'src/libstrongswan/credentials/ietf_attributes')
-rw-r--r-- | src/libstrongswan/credentials/ietf_attributes/ietf_attributes.c | 118 |
1 files changed, 55 insertions, 63 deletions
diff --git a/src/libstrongswan/credentials/ietf_attributes/ietf_attributes.c b/src/libstrongswan/credentials/ietf_attributes/ietf_attributes.c index fecc9910e..fb18fb53d 100644 --- a/src/libstrongswan/credentials/ietf_attributes/ietf_attributes.c +++ b/src/libstrongswan/credentials/ietf_attributes/ietf_attributes.c @@ -102,15 +102,14 @@ static void ietf_attr_destroy(ietf_attr_t *this) */ static ietf_attr_t* ietf_attr_create(ietf_attribute_type_t type, chunk_t value) { - ietf_attr_t *this = malloc_thing(ietf_attr_t); + ietf_attr_t *this; - /* initialize */ - this->type = type; - this->value = chunk_clone(value); - - /* function */ - this->compare = ietf_attr_compare; - this->destroy = ietf_attr_destroy; + INIT(this, + .compare = ietf_attr_compare, + .destroy = ietf_attr_destroy, + .type = type, + .value = chunk_clone(value), + ); return this; } @@ -142,10 +141,8 @@ struct private_ietf_attributes_t { refcount_t ref; }; -/** - * Implementation of ietf_attributes_t.get_string. - */ -static char* get_string(private_ietf_attributes_t *this) +METHOD(ietf_attributes_t, get_string, char*, + private_ietf_attributes_t *this) { if (this->string == NULL) { @@ -217,10 +214,8 @@ static char* get_string(private_ietf_attributes_t *this) return this->string; } -/** - * Implementation of ietf_attributes_t.get_encoding. - */ -static chunk_t get_encoding(private_ietf_attributes_t *this) +METHOD(ietf_attributes_t, get_encoding, chunk_t, + private_ietf_attributes_t *this) { chunk_t values; size_t size = 0; @@ -270,7 +265,11 @@ static chunk_t get_encoding(private_ietf_attributes_t *this) return asn1_wrap(ASN1_SEQUENCE, "m", values); } -static bool equals(private_ietf_attributes_t *this, private_ietf_attributes_t *other) +/** + * Implementation of ietf_attributes_t.equals. + */ +static bool equals(private_ietf_attributes_t *this, + private_ietf_attributes_t *other) { bool result = TRUE; @@ -304,7 +303,11 @@ static bool equals(private_ietf_attributes_t *this, private_ietf_attributes_t *o return result; } -static bool matches(private_ietf_attributes_t *this, private_ietf_attributes_t *other) +/** + * Implementation of ietf_attributes_t.matches. + */ +static bool matches(private_ietf_attributes_t *this, + private_ietf_attributes_t *other) { bool result = FALSE; ietf_attr_t *attr_a, *attr_b; @@ -364,19 +367,15 @@ static bool matches(private_ietf_attributes_t *this, private_ietf_attributes_t * return result; } -/** - * Implementation of ietf_attributes_t.get_ref - */ -static private_ietf_attributes_t* get_ref(private_ietf_attributes_t *this) +METHOD(ietf_attributes_t, get_ref, ietf_attributes_t*, + private_ietf_attributes_t *this) { ref_get(&this->ref); - return this; + return &this->public; } -/** - * Implementation of ietf_attributes_t.destroy. - */ -static void destroy(private_ietf_attributes_t *this) +METHOD(ietf_attributes_t, destroy, void, + private_ietf_attributes_t *this) { if (ref_put(&this->ref)) { @@ -388,18 +387,21 @@ static void destroy(private_ietf_attributes_t *this) static private_ietf_attributes_t* create_empty(void) { - private_ietf_attributes_t *this = malloc_thing(private_ietf_attributes_t); - - this->public.get_string = (char* (*)(ietf_attributes_t*))get_string; - this->public.get_encoding = (chunk_t (*)(ietf_attributes_t*))get_encoding; - this->public.equals = (bool (*)(ietf_attributes_t*,ietf_attributes_t*))equals; - this->public.matches = (bool (*)(ietf_attributes_t*,ietf_attributes_t*))matches; - this->public.get_ref = (ietf_attributes_t* (*)(ietf_attributes_t*))get_ref; - this->public.destroy = (void (*)(ietf_attributes_t*))destroy; - - this->list = linked_list_create(); - this->string = NULL; - this->ref = 1; + private_ietf_attributes_t *this; + + INIT(this, + .public = { + .get_string = _get_string, + .get_encoding = _get_encoding, + .equals = (bool (*)(ietf_attributes_t*,ietf_attributes_t*))equals, + .matches = (bool (*)(ietf_attributes_t*,ietf_attributes_t*))matches, + .get_ref = _get_ref, + .destroy = _destroy, + }, + .list = linked_list_create(), + .ref = 1, + ); + return this; } @@ -410,34 +412,24 @@ static void ietf_attributes_add(private_ietf_attributes_t *this, ietf_attr_t *attr) { ietf_attr_t *current_attr; - bool found = FALSE; - iterator_t *iterator; + enumerator_t *enumerator; + int cmp = -1; - iterator = this->list->create_iterator(this->list, TRUE); - while (iterator->iterate(iterator, (void **)¤t_attr)) + enumerator = this->list->create_enumerator(this->list); + while (enumerator->enumerate(enumerator, (void **)¤t_attr) && + (cmp = attr->compare(attr, current_attr)) > 0) { - int cmp = attr->compare(attr, current_attr); - - if (cmp > 0) - { - continue; - } - if (cmp == 0) - { - attr->destroy(attr); - } - else - { - iterator->insert_before(iterator, attr); - } - found = TRUE; - break; + continue; } - iterator->destroy(iterator); - if (!found) + if (cmp == 0) { - this->list->insert_last(this->list, attr); + attr->destroy(attr); + } + else + { /* the enumerator either points to the end or to the attribute > attr */ + this->list->insert_before(this->list, enumerator, attr); } + enumerator->destroy(enumerator); } /* @@ -527,7 +519,7 @@ ietf_attributes_t *ietf_attributes_create_from_encoding(chunk_t encoded) ietf_attr_t *attr; type = (objectID - IETF_ATTR_OCTETS) / 2; - attr = ietf_attr_create(type, object); + attr = ietf_attr_create(type, object); ietf_attributes_add(this, attr); } break; |