summaryrefslogtreecommitdiff
path: root/src/charon/encoding/generator.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/charon/encoding/generator.c')
-rw-r--r--src/charon/encoding/generator.c172
1 files changed, 87 insertions, 85 deletions
diff --git a/src/charon/encoding/generator.c b/src/charon/encoding/generator.c
index 406cfc688..6485da492 100644
--- a/src/charon/encoding/generator.c
+++ b/src/charon/encoding/generator.c
@@ -53,55 +53,55 @@ struct private_generator_t {
* Public part of a generator_t object.
*/
generator_t public;
-
+
/**
* 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).
*/
u_int8_t current_bit;
-
+
/**
* Associated data struct to read informations from.
*/
void *data_struct;
-
+
/*
* Last payload length position offset in the buffer.
*/
u_int32_t last_payload_length_position_offset;
-
+
/**
* Offset of the header length field in the buffer.
*/
u_int32_t header_length_position_offset;
-
+
/**
* Last SPI size.
*/
u_int8_t last_spi_size;
-
+
/**
* Attribute format of the last generated transform attribute.
*
- * Used to check if a variable value field is used or not for
+ * Used to check if a variable value field is used or not for
* the transform attribute value.
*/
bool attribute_format;
-
+
/**
* Depending on the value of attribute_format this field is used
* to hold the length of the transform attribute in bytes.
@@ -149,14 +149,14 @@ static void make_space_available(private_generator_t *this, int bits)
while ((get_space(this) * 8 - this->current_bit) < bits)
{
int 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",
+
+ DBG2(DBG_ENC, "increasing gen buffer from %d to %d byte",
old_buffer_size, new_buffer_size);
-
+
this->buffer = realloc(this->buffer,new_buffer_size);
this->out_position = (this->buffer + out_position_offset);
this->roof_position = (this->buffer + new_buffer_size);
@@ -171,9 +171,9 @@ static void write_bytes_to_buffer(private_generator_t *this, void *bytes,
{
int i;
u_int8_t *read_position = (u_int8_t *)bytes;
-
+
make_space_available(this, number_of_bytes * 8);
-
+
for (i = 0; i < number_of_bytes; i++)
{
*(this->out_position) = *(read_position);
@@ -192,14 +192,14 @@ static void write_bytes_to_buffer_at_offset(private_generator_t *this,
u_int8_t *read_position = (u_int8_t *)bytes;
u_int8_t *write_position;
u_int32_t free_space_after_offset = get_size(this) - offset;
-
- /* check first if enough space for new data is available */
+
+ /* check first if enough space for new data is available */
if (number_of_bytes > free_space_after_offset)
{
- make_space_available(this,
+ make_space_available(this,
(number_of_bytes - free_space_after_offset) * 8);
}
-
+
write_position = this->buffer + offset;
for (i = 0; i < number_of_bytes; i++)
{
@@ -216,7 +216,7 @@ static void generate_u_int_type(private_generator_t *this,
encoding_type_t int_type,u_int32_t offset)
{
int number_of_bits = 0;
-
+
/* find out number of bits of each U_INT type to check for enough space */
switch (int_type)
{
@@ -251,14 +251,14 @@ static void generate_u_int_type(private_generator_t *this,
encoding_type_names, int_type);
return;
}
-
+
make_space_available(this, number_of_bits);
switch (int_type)
{
case U_INT_4:
{
u_int8_t high, low;
-
+
if (this->current_bit == 0)
{
/* high of current byte in buffer has to be set to the new value*/
@@ -303,7 +303,7 @@ static void generate_u_int_type(private_generator_t *this,
{
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)
{
@@ -325,7 +325,7 @@ static void generate_u_int_type(private_generator_t *this,
write_bytes_to_buffer(this, &val, sizeof(u_int16_t));
this->current_bit = 0;
break;
-
+
}
case U_INT_16:
case CONFIGURATION_ATTRIBUTE_LENGTH:
@@ -372,11 +372,11 @@ static void generate_reserved_field(private_generator_t *this, int bits)
return ;
}
make_space_available(this, bits);
-
+
if (bits == 1)
{
u_int8_t reserved_bit = ~(1 << (7 - this->current_bit));
-
+
*(this->out_position) = *(this->out_position) & reserved_bit;
if (this->current_bit == 0)
{
@@ -410,11 +410,11 @@ static void generate_flag(private_generator_t *this, u_int32_t offset)
{
u_int8_t flag_value;
u_int8_t flag;
-
+
flag_value = (*((bool *) (this->data_struct + offset))) ? 1 : 0;
/* get flag position */
flag = (flag_value << (7 - this->current_bit));
-
+
/* make sure one bit is available in buffer */
make_space_available(this, 1);
if (this->current_bit == 0)
@@ -422,10 +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;
DBG3(DBG_ENC, " => %d", *this->out_position);
-
+
this->current_bit++;
if (this->current_bit >= 8)
{
@@ -440,16 +440,16 @@ 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 ;
}
-
+
value = (chunk_t *)(this->data_struct + offset);
DBG3(DBG_ENC, " => %B", value);
-
+
write_bytes_to_buffer(this, value->ptr, value->len);
}
@@ -460,7 +460,7 @@ static void write_to_chunk(private_generator_t *this,chunk_t *data)
{
int 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)
{
@@ -468,14 +468,14 @@ static void write_to_chunk(private_generator_t *this,chunk_t *data)
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 = chunk_alloc(data_length);
memcpy(data->ptr, this->buffer, data_length);
-
+
DBG3(DBG_ENC, "generated data of this generator %B", data);
}
@@ -488,20 +488,20 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
size_t rule_count;
encoding_rule_t *rules;
payload_type_t payload_type;
-
+
this->data_struct = payload;
payload_type = payload->get_type(payload);
/* spi size has to get reseted */
this->last_spi_size = 0;
-
+
offset_start = this->out_position - this->buffer;
-
+
DBG2(DBG_ENC, "generating payload of type %N",
payload_type_names, payload_type);
-
+
/* 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",
@@ -529,7 +529,7 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
{
generate_reserved_field(this, 8);
break;
- }
+ }
case FLAG:
{
generate_flag(this, rules[i].offset);
@@ -578,7 +578,7 @@ 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:
@@ -619,13 +619,13 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
break;
}
generate_from_chunk(this, rules[i].offset);
-
+
payload_length_position_offset =
this->last_payload_length_position_offset;
-
- length_of_payload = header_length +
+
+ length_of_payload = header_length +
((chunk_t *)(this->data_struct + rules[i].offset))->len;
-
+
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);
@@ -633,7 +633,7 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
}
case PROPOSALS:
{
- u_int32_t 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;
@@ -642,13 +642,13 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
(this->data_struct + rules[i].offset));
iterator_t *iterator;
payload_t *current_proposal;
-
+
iterator = proposals->create_iterator(proposals,TRUE);
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_offset(this);
generate_payload(this, current_proposal);
after_generate_position_offset = get_offset(this);
@@ -656,7 +656,7 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
before_generate_position_offset);
}
iterator->destroy(iterator);
-
+
int16_val = htons(length_of_sa_payload);
write_bytes_to_buffer_at_offset(this, &int16_val,
sizeof(u_int16_t),payload_length_position_offset);
@@ -664,36 +664,36 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
}
case TRANSFORMS:
{
- u_int32_t payload_length_position_offset =
+ u_int32_t payload_length_position_offset =
this->last_payload_length_position_offset;
- u_int16_t length_of_proposal =
+ 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));
iterator_t *iterator;
payload_t *current_transform;
-
+
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_offset(this);
generate_payload(this, current_transform);
after_generate_position_offset = get_offset(this);
-
+
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:
{
u_int32_t transform_length_position_offset =
@@ -705,32 +705,32 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
(this->data_struct + rules[i].offset));
iterator_t *iterator;
payload_t *current_attribute;
-
+
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_offset(this);
generate_payload(this, current_attribute);
after_generate_position_offset = get_offset(this);
-
+
length_of_transform += (after_generate_position_offset -
before_generate_position_offset);
}
-
+
iterator->destroy(iterator);
-
+
int16_val = htons(length_of_transform);
- write_bytes_to_buffer_at_offset(this, &int16_val,
+ write_bytes_to_buffer_at_offset(this, &int16_val,
sizeof(u_int16_t),transform_length_position_offset);
break;
}
case CONFIGURATION_ATTRIBUTES:
{
- u_int32_t configurations_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;
@@ -738,29 +738,29 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
(this->data_struct + rules[i].offset));
iterator_t *iterator;
payload_t *current_attribute;
-
+
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_offset(this);
generate_payload(this, current_attribute);
after_generate_position_offset = get_offset(this);
-
+
length_of_configurations += after_generate_position_offset -
before_generate_position_offset;
}
-
+
iterator->destroy(iterator);
-
+
int16_val = htons(length_of_configurations);
- write_bytes_to_buffer_at_offset(this, &int16_val,
+ 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);
@@ -768,7 +768,7 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
this->attribute_format =
*((bool *)(this->data_struct + rules[i].offset));
break;
- }
+ }
case ATTRIBUTE_LENGTH_OR_VALUE:
{
@@ -797,7 +797,7 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
}
case TRAFFIC_SELECTORS:
{
- u_int32_t payload_length_position_offset =
+ 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;
@@ -805,29 +805,29 @@ static void generate_payload (private_generator_t *this,payload_t *payload)
(this->data_struct + rules[i].offset));
iterator_t *iterator;
payload_t *current_tss;
-
+
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_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);
-
+
int16_val = htons(length_of_ts_payload);
write_bytes_to_buffer_at_offset(this, &int16_val,
sizeof(u_int16_t),payload_length_position_offset);
break;
- }
-
+ }
+
case ENCRYPTED_DATA:
{
generate_from_chunk(this, rules[i].offset);
@@ -869,10 +869,10 @@ generator_t *generator_create()
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;
-
+
/* allocate memory for buffer */
this->buffer = malloc(GENERATOR_DATA_BUFFER_SIZE);
-
+
/* initiate private variables */
this->out_position = this->buffer;
this->roof_position = this->buffer + GENERATOR_DATA_BUFFER_SIZE;
@@ -880,7 +880,9 @@ generator_t *generator_create()
this->current_bit = 0;
this->last_payload_length_position_offset = 0;
this->header_length_position_offset = 0;
-
+ this->attribute_format = FALSE;
+ this->attribute_length = 0;
+
return &(this->public);
}