Migrated unknown payload to INIT/METHOD macros
authorMartin Willi <martin@revosec.ch>
Wed, 10 Nov 2010 12:56:18 +0000 (13:56 +0100)
committerMartin Willi <martin@revosec.ch>
Wed, 5 Jan 2011 15:45:43 +0000 (16:45 +0100)
src/libcharon/encoding/payloads/unknown_payload.c

index dd5547d..06e5754 100644 (file)
@@ -18,8 +18,6 @@
 
 #include "unknown_payload.h"
 
-
-
 typedef struct private_unknown_payload_t private_unknown_payload_t;
 
 /**
@@ -62,21 +60,21 @@ struct private_unknown_payload_t {
  */
 encoding_rule_t unknown_payload_encodings[] = {
        /* 1 Byte next payload type, stored in the field next_payload */
-       { U_INT_8,                      offsetof(private_unknown_payload_t, next_payload)},
+       { U_INT_8,                      offsetof(private_unknown_payload_t, next_payload)       },
        /* the critical bit */
-       { FLAG,                         offsetof(private_unknown_payload_t, critical)   },
+       { FLAG,                         offsetof(private_unknown_payload_t, critical)           },
        /* 7 Bit reserved bits, nowhere stored */
-       { RESERVED_BIT, 0                                                                                                       },
-       { RESERVED_BIT, 0                                                                                                       },
-       { RESERVED_BIT, 0                                                                                                       },
-       { RESERVED_BIT, 0                                                                                                       },
-       { RESERVED_BIT, 0                                                                                                       },
-       { RESERVED_BIT, 0                                                                                                       },
-       { RESERVED_BIT, 0                                                                                                       },
+       { RESERVED_BIT,         0                                                                                                       },
+       { RESERVED_BIT,         0                                                                                                       },
+       { RESERVED_BIT,         0                                                                                                       },
+       { RESERVED_BIT,         0                                                                                                       },
+       { RESERVED_BIT,         0                                                                                                       },
+       { RESERVED_BIT,         0                                                                                                       },
+       { RESERVED_BIT,         0                                                                                                       },
        /* Length of the whole payload*/
-       { PAYLOAD_LENGTH,       offsetof(private_unknown_payload_t, payload_length)},
+       { PAYLOAD_LENGTH,       offsetof(private_unknown_payload_t, payload_length)     },
        /* some unknown data bytes, length is defined in PAYLOAD_LENGTH */
-       { UNKNOWN_DATA,         offsetof(private_unknown_payload_t, data)               }
+       { UNKNOWN_DATA,         offsetof(private_unknown_payload_t, data)                       },
 };
 
 /*
@@ -91,82 +89,59 @@ encoding_rule_t unknown_payload_encodings[] = {
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
 
-/**
- * Implementation of payload_t.verify.
- */
-static status_t verify(private_unknown_payload_t *this)
+METHOD(payload_t, verify, status_t,
+       private_unknown_payload_t *this)
 {
-       /* can't do any checks, so we assume its good */
        return SUCCESS;
 }
 
-/**
- * Implementation of payload_t.get_encoding_rules.
- */
-static void get_encoding_rules(private_unknown_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
+METHOD(payload_t, get_encoding_rules, void,
+       private_unknown_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
 {
        *rules = unknown_payload_encodings;
        *rule_count = sizeof(unknown_payload_encodings) / sizeof(encoding_rule_t);
 }
 
-/**
- * Implementation of payload_t.get_type.
- */
-static payload_type_t get_payload_type(private_unknown_payload_t *this)
+METHOD(payload_t, get_payload_type, payload_type_t,
+       private_unknown_payload_t *this)
 {
        return UNKNOWN_PAYLOAD;
 }
 
-/**
- * Implementation of payload_t.get_next_type.
- */
-static payload_type_t get_next_type(private_unknown_payload_t *this)
+METHOD(payload_t, get_next_type, payload_type_t,
+       private_unknown_payload_t *this)
 {
-       return (this->next_payload);
+       return this->next_payload;
 }
 
-/**
- * Implementation of payload_t.set_next_type.
- */
-static void set_next_type(private_unknown_payload_t *this,payload_type_t type)
+METHOD(payload_t, set_next_type, void,
+       private_unknown_payload_t *this,payload_type_t type)
 {
        this->next_payload = type;
 }
 
-/**
- * Implementation of payload_t.get_length.
- */
-static size_t get_length(private_unknown_payload_t *this)
+METHOD(payload_t, get_length, size_t,
+       private_unknown_payload_t *this)
 {
        return this->payload_length;
 }
 
-/**
- * Implementation of unknown_payload_t.get_data.
- */
-static bool is_critical(private_unknown_payload_t *this)
+METHOD(unknown_payload_t, is_critical, bool,
+       private_unknown_payload_t *this)
 {
        return this->critical;
 }
 
-/**
- * Implementation of unknown_payload_t.get_data.
- */
-static chunk_t get_data (private_unknown_payload_t *this)
+METHOD(unknown_payload_t, get_data, chunk_t,
+       private_unknown_payload_t *this)
 {
-       return (this->data);
+       return this->data;
 }
 
-/**
- * Implementation of payload_t.destroy and unknown_payload_t.destroy.
- */
-static void destroy(private_unknown_payload_t *this)
+METHOD2(payload_t, unknown_payload_t, destroy, void,
+       private_unknown_payload_t *this)
 {
-       if (this->data.ptr != NULL)
-       {
-               chunk_free(&(this->data));
-       }
-
+       free(this->data.ptr);
        free(this);
 }
 
@@ -175,27 +150,26 @@ static void destroy(private_unknown_payload_t *this)
  */
 unknown_payload_t *unknown_payload_create()
 {
-       private_unknown_payload_t *this = malloc_thing(private_unknown_payload_t);
-
-       /* interface functions */
-       this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
-       this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
-       this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
-       this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
-       this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
-       this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_payload_type;
-       this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
-
-       /* public functions */
-       this->public.destroy = (void (*) (unknown_payload_t *)) destroy;
-       this->public.is_critical = (bool (*) (unknown_payload_t *)) is_critical;
-       this->public.get_data = (chunk_t (*) (unknown_payload_t *)) get_data;
-
-       /* private variables */
-       this->critical = FALSE;
-       this->next_payload = NO_PAYLOAD;
-       this->payload_length = UNKNOWN_PAYLOAD_HEADER_LENGTH;
-       this->data = chunk_empty;
-
-       return (&(this->public));
+       private_unknown_payload_t *this;
+
+       INIT(this,
+               .public = {
+                       .payload_interface = {
+                               .verify = _verify,
+                               .get_encoding_rules = _get_encoding_rules,
+                               .get_length = _get_length,
+                               .get_next_type = _get_next_type,
+                               .set_next_type = _set_next_type,
+                               .get_type = _get_payload_type,
+                               .destroy = _destroy,
+                       },
+                       .is_critical = _is_critical,
+                       .get_data = _get_data,
+                       .destroy = _destroy,
+               },
+               .next_payload = NO_PAYLOAD,
+               .payload_length = UNKNOWN_PAYLOAD_HEADER_LENGTH,
+       );
+
+       return &this->public;
 }