cleanup of generator code
authorMartin Willi <martin@strongswan.org>
Mon, 18 May 2009 12:06:48 +0000 (14:06 +0200)
committerMartin Willi <martin@strongswan.org>
Mon, 18 May 2009 12:06:48 +0000 (14:06 +0200)
src/charon/encoding/generator.c
src/charon/encoding/generator.h

index a969ad0..33ca0ea 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2006 Martin Willi
+ * Copyright (C) 2005-2009 Martin Willi
  * Copyright (C) 2005 Jan Hutter
  * Hochschule fuer Technik Rapperswil
  *
@@ -19,7 +19,6 @@
 #include <arpa/inet.h>
 #include <stdio.h>
 
-
 #include "generator.h"
 
 #include <library.h>
@@ -59,22 +58,22 @@ struct private_generator_t {
         * Buffer used to generate the data into.
         */
        u_int8_t *buffer;
-
+       
        /**
         * Current write position in buffer (one byte aligned).
         */
        u_int8_t *out_position;
-
+       
        /**
         * Position of last byte in buffer.
         */
        u_int8_t *roof_position;
-
+       
        /**
         * Current bit writing to in current byte (between 0 and 7).
         */
        size_t current_bit;
-
+       
        /**
         * Associated data struct to read informations from.
         */
@@ -113,7 +112,7 @@ struct private_generator_t {
 /**
  * Get size of current buffer in bytes.
  */
-static size_t get_current_buffer_size(private_generator_t *this)
+static size_t get_size(private_generator_t *this)
 {
        return this->roof_position - this->buffer;
 }
@@ -121,7 +120,7 @@ static size_t get_current_buffer_size(private_generator_t *this)
 /**
  * Get free space of current buffer in bytes.
  */
-static size_t get_current_buffer_space(private_generator_t *this)
+static size_t get_space(private_generator_t *this)
 {
        return this->roof_position - this->out_position;
 }
@@ -129,7 +128,7 @@ static size_t get_current_buffer_space(private_generator_t *this)
 /**
  * Get length of data in buffer (in bytes).
  */
-static size_t get_current_data_length(private_generator_t *this)
+static size_t get_length(private_generator_t *this)
 {
        return this->out_position - this->buffer;
 }
@@ -137,7 +136,7 @@ static size_t get_current_data_length(private_generator_t *this)
 /**
  * Get current offset in buffer (in bytes).
  */
-static u_int32_t get_current_buffer_offset(private_generator_t *this)
+static u_int32_t get_offset(private_generator_t *this)
 {
        return this->out_position - this->buffer;
 }
@@ -145,21 +144,20 @@ static u_int32_t get_current_buffer_offset(private_generator_t *this)
 /**
  * Makes sure enough space is available in buffer to store amount of bits.
  */
-static void make_space_available (private_generator_t *this, size_t bits)
+static void make_space_available(private_generator_t *this, size_t bits)
 {
-       while ((get_current_buffer_space(this) * 8 - this->current_bit) < bits)
+       while ((get_space(this) * 8 - this->current_bit) < bits)
        {
-               /* must increase buffer */
-               size_t old_buffer_size = get_current_buffer_size(this);
-               size_t new_buffer_size = old_buffer_size + GENERATOR_DATA_BUFFER_INCREASE_VALUE;
-               size_t out_position_offset = ((this->out_position) - (this->buffer));
-
-               DBG2(DBG_ENC, "increased gen buffer from %d to %d byte", 
+               size_t old_buffer_size, new_buffer_size, out_position_offset;
+               
+               old_buffer_size = get_size(this);
+               new_buffer_size = old_buffer_size + GENERATOR_DATA_BUFFER_INCREASE_VALUE;
+               out_position_offset = this->out_position - this->buffer;
+               
+               DBG2(DBG_ENC, "increasing gen buffer from %d to %d byte", 
                         old_buffer_size, new_buffer_size);
                
-               /* Reallocate space for new buffer */
                this->buffer = realloc(this->buffer,new_buffer_size);
-
                this->out_position = (this->buffer + out_position_offset);
                this->roof_position = (this->buffer + new_buffer_size);
        }
@@ -168,11 +166,11 @@ static void make_space_available (private_generator_t *this, size_t bits)
 /**
  * Writes a specific amount of byte into the buffer.
  */
-static void write_bytes_to_buffer(private_generator_t *this, void * bytes,
+static void write_bytes_to_buffer(private_generator_t *this, void *bytes,
                                                                  size_t number_of_bytes)
 {
        int i;
-       u_int8_t *read_position = (u_int8_t *) bytes;
+       u_int8_t *read_position = (u_int8_t *)bytes;
        
        make_space_available(this, number_of_bytes * 8);
        
@@ -191,14 +189,15 @@ static void write_bytes_to_buffer_at_offset (private_generator_t *this,
                                                void *bytes, size_t number_of_bytes, u_int32_t offset)
 {
        int i;
-       u_int8_t *read_position = (u_int8_t *) bytes;
+       u_int8_t *read_position = (u_int8_t *)bytes;
        u_int8_t *write_position;
-       u_int32_t free_space_after_offset = get_current_buffer_size(this) - offset;
-
+       u_int32_t free_space_after_offset = get_size(this) - offset;
+       
        /* check first if enough space for new data is available */     
        if (number_of_bytes > free_space_after_offset)
        {
-               make_space_available(this, (number_of_bytes - free_space_after_offset) * 8);
+               make_space_available(this, 
+                                                        (number_of_bytes - free_space_after_offset) * 8);
        }
        
        write_position = this->buffer + offset;
@@ -212,95 +211,83 @@ static void write_bytes_to_buffer_at_offset (private_generator_t *this,
 
 /**
  * Generates a U_INT-Field type and writes it to buffer.
- *
- * @param this                                         private_generator_t object
- * @param int_type                             type of U_INT field (U_INT_4, U_INT_8, etc.)
- *                                                             ATTRIBUTE_TYPE is also generated in this function
- * @param offset                               offset of value in data struct
- * @param generator_contexts   generator_contexts_t object where the context is written or read from
  */
 static void generate_u_int_type(private_generator_t *this,
                                                                encoding_type_t int_type,u_int32_t offset)
 {
        size_t number_of_bits = 0;
-
-       /* find out number of bits of each U_INT type to check for enough space 
-          in buffer */
+       
+       /* find out number of bits of each U_INT type to check for enough space */
        switch (int_type)
        {
-                       case U_INT_4:
-                               number_of_bits = 4;
-                               break;
-                       case TS_TYPE:
-                       case U_INT_8:
-                               number_of_bits = 8;
-                               break;
-                       case U_INT_16:
-                       case CONFIGURATION_ATTRIBUTE_LENGTH:
-                               number_of_bits = 16;
-                               break;
-                       case U_INT_32:
-                               number_of_bits = 32;
-                               break;
-                       case ATTRIBUTE_TYPE:
-                               number_of_bits = 15;
-                               break;
-                       case IKE_SPI:
-                               number_of_bits = 64;
-                               break;
-
-                       default:
+               case U_INT_4:
+                       number_of_bits = 4;
+                       break;
+               case TS_TYPE:
+               case U_INT_8:
+                       number_of_bits = 8;
+                       break;
+               case U_INT_16:
+               case CONFIGURATION_ATTRIBUTE_LENGTH:
+                       number_of_bits = 16;
+                       break;
+               case U_INT_32:
+                       number_of_bits = 32;
+                       break;
+               case ATTRIBUTE_TYPE:
+                       number_of_bits = 15;
+                       break;
+               case IKE_SPI:
+                       number_of_bits = 64;
+                       break;
+               default:
                        DBG1(DBG_ENC, "U_INT Type %N is not supported",
                                 encoding_type_names, int_type);
-
                        return;
        }
-       /* U_INT Types of multiple then 8 bits must be aligned */
-       if (((number_of_bits % 8) == 0) && (this->current_bit != 0))
+       if ((number_of_bits % 8) == 0 && this->current_bit != 0)
        {
                DBG1(DBG_ENC, "U_INT Type %N is not 8 Bit aligned",
                         encoding_type_names, int_type);
-               /* current bit has to be zero for values multiple of 8 bits */
                return;
        }
        
-       /* make sure enough space is available in buffer */
        make_space_available(this, number_of_bits);
-       /* now handle each u int type differently */
        switch (int_type)
        {
                case U_INT_4:
                {
+                       u_int8_t high, low;
+                       
                        if (this->current_bit == 0)
                        {
-                               /* highval of current byte in buffer has to be set to the new value*/
-                               u_int8_t high_val = *((u_int8_t *)(this->data_struct + offset)) << 4;
-                               /* lowval in buffer is not changed */
-                               u_int8_t low_val = *(this->out_position) & 0x0F;
-                               /* highval is set, low_val is not changed */
-                               *(this->out_position) = high_val | low_val;
+                               /* high of current byte in buffer has to be set to the new value*/
+                               high = *((u_int8_t *)(this->data_struct + offset)) << 4;
+                               /* low in buffer is not changed */
+                               low = *(this->out_position) & 0x0F;
+                               /* high is set, low_val is not changed */
+                               *(this->out_position) = high | low;
                                DBG3(DBG_ENC, "   => %d", *(this->out_position));
                                /* write position is not changed, just bit position is moved */
                                this->current_bit = 4;
                        }
                        else if (this->current_bit == 4)
                        {
-                               /* highval in buffer is not changed */
-                               u_int high_val = *(this->out_position) & 0xF0;
-                               /* lowval of current byte in buffer has to be set to the new value*/
-                               u_int low_val = *((u_int8_t *)(this->data_struct + offset)) & 0x0F;
-                               *(this->out_position) = high_val | low_val;
+                               /* high in buffer is not changed */
+                               high = *(this->out_position) & 0xF0;
+                               /* low of current byte in buffer has to be set to the new value*/
+                               low = *((u_int8_t *)(this->data_struct + offset)) & 0x0F;
+                               *(this->out_position) = high | low;
                                DBG3(DBG_ENC, "   => %d", *(this->out_position));
                                this->out_position++;
                                this->current_bit = 0;
-
                        }
                        else
                        {
                                DBG1(DBG_ENC, "U_INT_4 Type is not 4 Bit aligned");
                                /* 4 Bit integers must have a 4 bit alignment */
                                return;
-                       };
+                       }
                        break;
                }
                case TS_TYPE:
@@ -311,31 +298,31 @@ static void generate_u_int_type(private_generator_t *this,
                        DBG3(DBG_ENC, "   => %d", *(this->out_position));
                        this->out_position++;
                        break;
-
                }
                case ATTRIBUTE_TYPE:
                {
-                       /* attribute type must not change first bit uf current byte ! */
+                       u_int8_t attribute_format_flag;
+                       u_int16_t val;
+                       
+                       /* attribute type must not change first bit of current byte */
                        if (this->current_bit != 1)
                        {
                                DBG1(DBG_ENC, "ATTRIBUTE FORMAT flag is not set");
-                               /* first bit has to be set! */
                                return;
                        }
-                       /* get value of attribute format flag */
-                       u_int8_t attribute_format_flag = *(this->out_position) & 0x80;
+                       attribute_format_flag = *(this->out_position) & 0x80;
                        /* get attribute type value as 16 bit integer*/
-                       u_int16_t int16_val = *((u_int16_t*)(this->data_struct + offset));
+                       val = *((u_int16_t*)(this->data_struct + offset));
                        /* unset most significant bit */
-                       int16_val &= 0x7FFF;
+                       val &= 0x7FFF;
                        if (attribute_format_flag)
                        {
-                               int16_val |= 0x8000;
+                               val |= 0x8000;
                        }
-                       int16_val = htons(int16_val);
-                       DBG3(DBG_ENC, "   => %d", int16_val);
-                       /* write bytes to buffer (set bit is overwritten)*/                             
-                       write_bytes_to_buffer(this, &int16_val, sizeof(u_int16_t));
+                       val = htons(val);
+                       DBG3(DBG_ENC, "   => %d", val);
+                       /* write bytes to buffer (set bit is overwritten) */
+                       write_bytes_to_buffer(this, &val, sizeof(u_int16_t));
                        this->current_bit = 0;
                        break;
                        
@@ -343,23 +330,25 @@ static void generate_u_int_type(private_generator_t *this,
                case U_INT_16:
                case CONFIGURATION_ATTRIBUTE_LENGTH:
                {
-                       u_int16_t int16_val = htons(*((u_int16_t*)(this->data_struct + offset)));
-                       DBG3(DBG_ENC, "   => %b", (void*)&int16_val, sizeof(int16_val));
-                       write_bytes_to_buffer(this, &int16_val, sizeof(u_int16_t));
+                       u_int16_t val = htons(*((u_int16_t*)(this->data_struct + offset)));
+                       DBG3(DBG_ENC, "   => %b", val, sizeof(u_int16_t));
+                       write_bytes_to_buffer(this, &val, sizeof(u_int16_t));
                        break;
                }
                case U_INT_32:
                {
-                       u_int32_t int32_val = htonl(*((u_int32_t*)(this->data_struct + offset)));
-                       DBG3(DBG_ENC, "   => %b", (void*)&int32_val, sizeof(int32_val));
-                       write_bytes_to_buffer(this, &int32_val, sizeof(u_int32_t));
+                       u_int32_t val = htonl(*((u_int32_t*)(this->data_struct + offset)));
+                       DBG3(DBG_ENC, "   => %b", val, sizeof(u_int32_t));
+                       write_bytes_to_buffer(this, &val, sizeof(u_int32_t));
                        break;
                }
                case IKE_SPI:
                {
-                       /* 64 bit are written as they come :-) */
-                       write_bytes_to_buffer(this, this->data_struct + offset, sizeof(u_int64_t));
-                       DBG3(DBG_ENC, "   => %b", (void*)(this->data_struct + offset), sizeof(u_int64_t));
+                       /* 64 bit are written as-is, no host order conversion */
+                       write_bytes_to_buffer(this, this->data_struct + offset,
+                                                                 sizeof(u_int64_t));
+                       DBG3(DBG_ENC, "   => %b", (void*)(this->data_struct + offset),
+                                sizeof(u_int64_t));
                        break;
                }
                default:
@@ -377,18 +366,17 @@ static void generate_u_int_type(private_generator_t *this,
 static void generate_reserved_field(private_generator_t *this, int bits)
 {
        /* only one bit or 8 bit fields are supported */
-       if ((bits != 1) && (bits != 8))
+       if (bits != 1 && bits != 8)
        {
                DBG1(DBG_ENC, "reserved field of %d bits cannot be generated", bits);
                return ;
        }
-       /* make sure enough space is available in buffer */
        make_space_available(this, bits);
        
        if (bits == 1)
-       {       
-               /* one bit processing */
+       {
                u_int8_t reserved_bit = ~(1 << (7 - this->current_bit));
+               
                *(this->out_position) = *(this->out_position) & reserved_bit;
                if (this->current_bit == 0)
                {
@@ -404,7 +392,6 @@ static void generate_reserved_field(private_generator_t *this, int bits)
        }
        else
        {
-               /* one byte processing*/
                if (this->current_bit > 0)
                {
                        DBG1(DBG_ENC, "reserved field cannot be written cause "
@@ -421,12 +408,9 @@ static void generate_reserved_field(private_generator_t *this, int bits)
  */
 static void generate_flag(private_generator_t *this, u_int32_t offset)
 {
-       /* value of current flag */
        u_int8_t flag_value;
-       /* position of flag in current byte */
        u_int8_t flag;
        
-       /* if the value in the data_struct is TRUE, flag_value is set to 1, 0 otherwise */
        flag_value = (*((bool *) (this->data_struct + offset))) ? 1 : 0;
        /* get flag position */
        flag = (flag_value << (7 - this->current_bit));
@@ -438,12 +422,10 @@ static void generate_flag(private_generator_t *this, u_int32_t offset)
                /* memory must be zero */
                *(this->out_position) = 0x00;
        }
-
-       *(this->out_position) = *(this->out_position) | flag;
        
+       *(this->out_position) = *(this->out_position) | flag;
+       DBG3(DBG_ENC, "   => %d", *this->out_position);
        
-       DBG3(DBG_ENC, "   => %d", *(this->out_position));
-
        this->current_bit++;
        if (this->current_bit >= 8)
        {
@@ -457,42 +439,42 @@ static void generate_flag(private_generator_t *this, u_int32_t offset)
  */
 static void generate_from_chunk(private_generator_t *this, u_int32_t offset)
 {
+       chunk_t *value;
+       
        if (this->current_bit != 0)
        {
                DBG1(DBG_ENC, "can not generate a chunk at Bitpos %d", this->current_bit);
                return ;
        }
        
-       /* position in buffer */
-       chunk_t *attribute_value = (chunk_t *)(this->data_struct + offset);
-       
-       DBG3(DBG_ENC, "   => %B", attribute_value);
+       value = (chunk_t *)(this->data_struct + offset);
+       DBG3(DBG_ENC, "   => %B", value);
        
-       /* use write_bytes_to_buffer function to do the job */
-       write_bytes_to_buffer(this, attribute_value->ptr, attribute_value->len);
+       write_bytes_to_buffer(this, value->ptr, value->len);
 }
 
 /**
  * Implementation of private_generator_t.write_to_chunk.
  */
-static void write_to_chunk (private_generator_t *this,chunk_t *data)
+static void write_to_chunk(private_generator_t *this,chunk_t *data)
 {
-       size_t data_length = get_current_data_length(this);
+       size_t data_length = get_length(this);
        u_int32_t header_length_field = data_length;
        
        /* write length into header length field */
        if (this->header_length_position_offset > 0)
        {
-               u_int32_t int32_val = htonl(header_length_field);
-               write_bytes_to_buffer_at_offset(this, &int32_val, sizeof(u_int32_t),
+               u_int32_t val = htonl(header_length_field);
+               write_bytes_to_buffer_at_offset(this, &val, sizeof(u_int32_t),
                                                                                this->header_length_position_offset);
        }
-
+       
        if (this->current_bit > 0)
-       data_length++;
-       data->ptr = malloc(data_length);
-       memcpy(data->ptr,this->buffer,data_length);
-       data->len = data_length;
+       {
+               data_length++;
+       }
+       *data = chunk_alloc(data_length);
+       memcpy(data->ptr, this->buffer, data_length);
        
        DBG3(DBG_ENC, "generated data of this generator %B", data);
 }
@@ -508,7 +490,6 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
        encoding_rule_t *rules;
        payload_type_t payload_type;
        
-       /* get payload type */
        payload_type = payload->get_type(payload);
        /* spi size has to get reseted */
        this->last_spi_size = 0;
@@ -520,7 +501,7 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
        
        /* each payload has its own encoding rules */
        payload->get_encoding_rules(payload,&rules,&rule_count);
-
+       
        for (i = 0; i < rule_count;i++)
        {
                DBG2(DBG_ENC, "  generating rule %d %N",
@@ -556,35 +537,28 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                        }
                        case PAYLOAD_LENGTH:
                        {
-                               /* position of payload lenght field is temporary stored */
-                               this->last_payload_length_position_offset = get_current_buffer_offset(this);
-                               /* payload length is generated like an U_INT_16 */
+                               this->last_payload_length_position_offset = get_offset(this);
                                generate_u_int_type(this, U_INT_16,rules[i].offset);
                                break;
                        }
                        case HEADER_LENGTH:
                        {
-                               /* position of header length field is temporary stored */                       
-                               this->header_length_position_offset = get_current_buffer_offset(this);  
-                               /* header length is generated like an U_INT_32 */
+                               this->header_length_position_offset = get_offset(this);
                                generate_u_int_type(this ,U_INT_32, rules[i].offset);
                                break;
                        }
                        case SPI_SIZE:
-                               /* spi size is handled as 8 bit unsigned integer */
                                generate_u_int_type(this, U_INT_8, rules[i].offset);
-                               /* last spi size is temporary stored */
-                               this->last_spi_size = *((u_int8_t *)(this->data_struct + rules[i].offset));
+                               this->last_spi_size = *((u_int8_t *)(this->data_struct +
+                                                                                                        rules[i].offset));
                                break;
                        case ADDRESS:
                        {
-                               /* the Address value is generated from chunk */
                                generate_from_chunk(this, rules[i].offset);
                                break;
                        }
                        case SPI:
                        {
-                               /* the SPI value is generated from chunk */
                                generate_from_chunk(this, rules[i].offset);
                                break;
                        }
@@ -604,14 +578,15 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                                u_int16_t length_of_payload;
                                u_int16_t header_length = 0;
                                u_int16_t length_in_network_order;
-
+                               
                                switch(rules[i].type)
                                {
                                        case KEY_EXCHANGE_DATA:
                                                header_length = KE_PAYLOAD_HEADER_LENGTH;
                                                break;
                                        case NOTIFICATION_DATA:
-                                               header_length = NOTIFY_PAYLOAD_HEADER_LENGTH + this->last_spi_size ;
+                                               header_length = NOTIFY_PAYLOAD_HEADER_LENGTH +
+                                                                                                               this->last_spi_size;
                                                break;
                                        case NONCE_DATA:
                                                header_length = NONCE_PAYLOAD_HEADER_LENGTH;
@@ -643,47 +618,42 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                                        default:
                                                break;
                                }
-                               
-                               /* the data value is generated from chunk */
                                generate_from_chunk(this, rules[i].offset);
                                
-                               payload_length_position_offset = this->last_payload_length_position_offset;
-                               
+                               payload_length_position_offset =
+                                                                       this->last_payload_length_position_offset;
                                
-                               /* Length of payload is calculated */
-                               length_of_payload = header_length + ((chunk_t *)(this->data_struct + rules[i].offset))->len;
+                               length_of_payload = header_length + 
+                                               ((chunk_t *)(this->data_struct + rules[i].offset))->len;
                                
-                               length_in_network_order = htons(length_of_payload);                     
+                               length_in_network_order = htons(length_of_payload);
                                write_bytes_to_buffer_at_offset(this, &length_in_network_order,
-                                                       sizeof(u_int16_t),payload_length_position_offset);
+                                                       sizeof(u_int16_t), payload_length_position_offset);
                                break;
                        }
                        case PROPOSALS:
                        {
-                               /* before iterative generate the transforms, store the current payload length position */
-                               u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
+                               u_int32_t payload_length_position_offset = 
+                                                                       this->last_payload_length_position_offset;
                                /* Length of SA_PAYLOAD is calculated */
                                u_int16_t length_of_sa_payload = SA_PAYLOAD_HEADER_LENGTH;
                                u_int16_t int16_val;
-                               /* proposals are stored in a linked list and so accessed */
-                               linked_list_t *proposals = *((linked_list_t **)(this->data_struct + rules[i].offset));
+                               linked_list_t *proposals = *((linked_list_t **)
+                                                                               (this->data_struct + rules[i].offset));
                                iterator_t *iterator;
                                payload_t *current_proposal;
                                
-                               /* create forward iterator */
                                iterator = proposals->create_iterator(proposals,TRUE);
-                               /* every proposal is processed (iterative call )*/
                                while (iterator->iterate(iterator, (void**)&current_proposal))
                                {
                                        u_int32_t before_generate_position_offset;
                                        u_int32_t after_generate_position_offset;
                                        
-                                       before_generate_position_offset = get_current_buffer_offset(this);
-                                       this->public.generate_payload(&(this->public),current_proposal);
-                                       after_generate_position_offset = get_current_buffer_offset(this);
-                                       
-                                       /* increase size of transform */
-                                       length_of_sa_payload += (after_generate_position_offset - before_generate_position_offset);
+                                       before_generate_position_offset = get_offset(this);
+                                       generate_payload(this, current_proposal);
+                                       after_generate_position_offset = get_offset(this);
+                                       length_of_sa_payload += (after_generate_position_offset -
+                                                                                        before_generate_position_offset);
                                }
                                iterator->destroy(iterator);
                                
@@ -694,60 +664,61 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                        }
                        case TRANSFORMS:
                        {
-                               /* before iterative generate the transforms, store the current length position */
-                               u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
-                               u_int16_t length_of_proposal = PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH + this->last_spi_size;
+                               u_int32_t payload_length_position_offset = 
+                                                                       this->last_payload_length_position_offset;
+                               u_int16_t length_of_proposal = 
+                                       PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH + this->last_spi_size;
                                u_int16_t int16_val;
-                               linked_list_t *transforms = *((linked_list_t **)(this->data_struct + rules[i].offset));
+                               linked_list_t *transforms = *((linked_list_t **)
+                                                                               (this->data_struct + rules[i].offset));
                                iterator_t *iterator;
                                payload_t *current_transform;
                                
-                               /* create forward iterator */
                                iterator = transforms->create_iterator(transforms,TRUE);
                                while (iterator->iterate(iterator, (void**)&current_transform))
                                {
                                        u_int32_t before_generate_position_offset;
                                        u_int32_t after_generate_position_offset;
                                        
-                                       before_generate_position_offset = get_current_buffer_offset(this);
-                                       this->public.generate_payload(&(this->public),current_transform);
-                                       after_generate_position_offset = get_current_buffer_offset(this);
+                                       before_generate_position_offset = get_offset(this);
+                                       generate_payload(this, current_transform);
+                                       after_generate_position_offset = get_offset(this);
                                        
-                                       /* increase size of transform */
-                                       length_of_proposal += (after_generate_position_offset - before_generate_position_offset);
+                                       length_of_proposal += (after_generate_position_offset -
+                                                                                  before_generate_position_offset);
                                }
-                               
                                iterator->destroy(iterator);
                                
                                int16_val = htons(length_of_proposal);
                                write_bytes_to_buffer_at_offset(this, &int16_val,
                                                        sizeof(u_int16_t), payload_length_position_offset);
-                               
                                break;
                        }       
                        case TRANSFORM_ATTRIBUTES:
                        {
-                               /* before iterative generate the transform attributes, store the current length position */
-                               u_int32_t transform_length_position_offset = this->last_payload_length_position_offset;
-                               u_int16_t length_of_transform = TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH;
+                               u_int32_t transform_length_position_offset =
+                                                                       this->last_payload_length_position_offset;
+                               u_int16_t length_of_transform =
+                                                                       TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH;
                                u_int16_t int16_val;
-                               linked_list_t *transform_attributes =*((linked_list_t **)(this->data_struct + rules[i].offset));
+                               linked_list_t *transform_attributes =*((linked_list_t **)
+                                                                       (this->data_struct + rules[i].offset));
                                iterator_t *iterator;
                                payload_t *current_attribute;
                                
-                               /* create forward iterator */
-                               iterator = transform_attributes->create_iterator(transform_attributes,TRUE);
+                               iterator = transform_attributes->create_iterator(
+                                                                                                       transform_attributes, TRUE);
                                while (iterator->iterate(iterator, (void**)&current_attribute))
                                {
                                        u_int32_t before_generate_position_offset;
                                        u_int32_t after_generate_position_offset;
                                        
-                                       before_generate_position_offset = get_current_buffer_offset(this);
-                                       this->public.generate_payload(&(this->public),current_attribute);
-                                       after_generate_position_offset = get_current_buffer_offset(this);
+                                       before_generate_position_offset = get_offset(this);
+                                       generate_payload(this, current_attribute);
+                                       after_generate_position_offset = get_offset(this);
                                        
-                                       /* increase size of transform */
-                                       length_of_transform += (after_generate_position_offset - before_generate_position_offset);
+                                       length_of_transform += (after_generate_position_offset -
+                                                                                       before_generate_position_offset);
                                }
                                
                                iterator->destroy(iterator);
@@ -755,32 +726,32 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                                int16_val = htons(length_of_transform);
                                write_bytes_to_buffer_at_offset(this, &int16_val, 
                                                        sizeof(u_int16_t),transform_length_position_offset);
-                               
                                break;
                        }
                        case CONFIGURATION_ATTRIBUTES:
                        {
-                               /* before iterative generate the configuration attributes, store the current length position */
-                               u_int32_t configurations_length_position_offset = this->last_payload_length_position_offset;
+                               u_int32_t configurations_length_position_offset = 
+                                                                       this->last_payload_length_position_offset;
                                u_int16_t length_of_configurations = CP_PAYLOAD_HEADER_LENGTH;
                                u_int16_t int16_val;
-                               linked_list_t *configuration_attributes =*((linked_list_t **)(this->data_struct + rules[i].offset));
+                               linked_list_t *configuration_attributes = *((linked_list_t **)
+                                                                               (this->data_struct + rules[i].offset));
                                iterator_t *iterator;
                                payload_t *current_attribute;
                                
-                               /* create forward iterator */
-                               iterator = configuration_attributes->create_iterator(configuration_attributes,TRUE);
+                               iterator = configuration_attributes->create_iterator(
+                                                                                               configuration_attributes,TRUE);
                                while (iterator->iterate(iterator, (void**)&current_attribute))
                                {
                                        u_int32_t before_generate_position_offset;
                                        u_int32_t after_generate_position_offset;
                                        
-                                       before_generate_position_offset = get_current_buffer_offset(this);
-                                       this->public.generate_payload(&(this->public),current_attribute);
-                                       after_generate_position_offset = get_current_buffer_offset(this);
+                                       before_generate_position_offset = get_offset(this);
+                                       generate_payload(this, current_attribute);
+                                       after_generate_position_offset = get_offset(this);
                                        
-                                       /* increase size of transform */
-                                       length_of_configurations += (after_generate_position_offset - before_generate_position_offset);
+                                       length_of_configurations += after_generate_position_offset -
+                                                                                       before_generate_position_offset;
                                }
                                
                                iterator->destroy(iterator);
@@ -788,14 +759,14 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                                int16_val = htons(length_of_configurations);
                                write_bytes_to_buffer_at_offset(this, &int16_val, 
                                         sizeof(u_int16_t),configurations_length_position_offset);
-                               
                                break;
                        }       
                        case ATTRIBUTE_FORMAT:
                        {
                                generate_flag(this, rules[i].offset);
                                /* Attribute format is a flag which is stored in context*/
-                               this->attribute_format = *((bool *) (this->data_struct + rules[i].offset));
+                               this->attribute_format =
+                                                       *((bool *)(this->data_struct + rules[i].offset));
                                break;
                        }       
 
@@ -805,7 +776,8 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                                {
                                        generate_u_int_type(this, U_INT_16, rules[i].offset);
                                        /* this field hold the length of the attribute */
-                                       this->attribute_length = *((u_int16_t *)(this->data_struct + rules[i].offset));
+                                       this->attribute_length =
+                                               *((u_int16_t *)(this->data_struct + rules[i].offset));
                                }
                                else
                                {
@@ -825,30 +797,28 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
                        }
                        case TRAFFIC_SELECTORS:
                        {
-                               /* before iterative generate the traffic_selectors, store the current payload length position */
-                               u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
-                               /* Length of SA_PAYLOAD is calculated */
+                               u_int32_t payload_length_position_offset = 
+                                                                       this->last_payload_length_position_offset;
                                u_int16_t length_of_ts_payload = TS_PAYLOAD_HEADER_LENGTH;
                                u_int16_t int16_val;
-                               /* traffic selectors are stored in a linked list and so accessed */
-                               linked_list_t *traffic_selectors = *((linked_list_t **)(this->data_struct + rules[i].offset));
+                               linked_list_t *traffic_selectors = *((linked_list_t **)
+                                                                               (this->data_struct + rules[i].offset));
                                iterator_t *iterator;
-                               payload_t *current_traffic_selector_substructure;
+                               payload_t *current_tss;
                                
-                               /* create forward iterator */
-                               iterator = traffic_selectors->create_iterator(traffic_selectors,TRUE);
-                               /* every proposal is processed (iterative call )*/
-                               while (iterator->iterate(iterator, (void **)&current_traffic_selector_substructure))
+                               iterator = traffic_selectors->create_iterator(
+                                                                                                       traffic_selectors,TRUE);
+                               while (iterator->iterate(iterator, (void **)&current_tss))
                                {
                                        u_int32_t before_generate_position_offset;
                                        u_int32_t after_generate_position_offset;
-
-                                       before_generate_position_offset = get_current_buffer_offset(this);
-                                       this->public.generate_payload(&(this->public),current_traffic_selector_substructure);
-                                       after_generate_position_offset = get_current_buffer_offset(this);
                                        
-                                       /* increase size of transform */
-                                       length_of_ts_payload += (after_generate_position_offset - before_generate_position_offset);
+                                       before_generate_position_offset = get_offset(this);
+                                       generate_payload(this, current_tss);
+                                       after_generate_position_offset = get_offset(this);
+                                       
+                                       length_of_ts_payload += (after_generate_position_offset -
+                                                                                        before_generate_position_offset);
                                }
                                iterator->destroy(iterator);
                                
@@ -896,9 +866,9 @@ generator_t *generator_create()
        this = malloc_thing(private_generator_t);
 
        /* initiate public functions */
-       this->public.generate_payload = (void(*)(generator_t*, payload_t *)) generate_payload;
+       this->public.generate_payload = (void(*)(generator_t*, payload_t *))generate_payload;
        this->public.destroy = (void(*)(generator_t*)) destroy;
-       this->public.write_to_chunk = (void (*) (generator_t *,chunk_t *)) write_to_chunk;
+       this->public.write_to_chunk = (void (*) (generator_t *,chunk_t *))write_to_chunk;
        
        /* allocate memory for buffer */
        this->buffer = malloc(GENERATOR_DATA_BUFFER_SIZE);
index 4bdeda4..f6fb898 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2006 Martin Willi
+ * Copyright (C) 2005-2009 Martin Willi
  * Copyright (C) 2005 Jan Hutter
  * Hochschule fuer Technik Rapperswil
  *
@@ -65,10 +65,10 @@ struct generator_t {
        /**
         * Writes all generated data of the generator to a chunk.
         *
-        * @param data          chunk to write the data to
+        * @param data          chunk to write the data to
         */
        void (*write_to_chunk) (generator_t *this,chunk_t *data);
-
+       
        /**
         * Destroys a generator_t object.
         */