Reverting eba28948a584b9d02474cf5d256b04b8d2adbe6a which was only necessary when...
[strongswan.git] / src / charon / encoding / parser.c
index d73ca27..9aa34b1 100644 (file)
@@ -50,7 +50,7 @@ typedef struct private_parser_t private_parser_t;
 
 /**
  * Private data stored in a context.
- * 
+ *
  * Contains pointers and counters to store current state.
  */
 struct private_parser_t {
@@ -58,27 +58,27 @@ struct private_parser_t {
         * Public members, see parser_t.
         */
        parser_t public;
-       
+
        /**
         * Current bit for reading in input data.
         */
        u_int8_t bit_pos;
-       
+
        /**
         * Current byte for reading in input data.
         */
        u_int8_t *byte_pos;
-       
+
        /**
         * Input data to parse.
         */
        u_int8_t *input;
-       
+
        /**
         * Roof of input, used for length-checking.
         */
        u_int8_t *input_roof;
-       
+
        /**
         * Set of encoding rules for this parsing session.
         */
@@ -244,35 +244,10 @@ static bool parse_uint32(private_parser_t *this, int rule_number,
 }
 
 /**
- * Parse a 64-Bit unsigned integer from the current parsing position.
- */
-static bool parse_uint64(private_parser_t *this, int rule_number,
-                                                        u_int64_t *output_pos)
-{
-       if (this->byte_pos + sizeof(u_int64_t) > this->input_roof)
-       {
-               return short_input(this, rule_number);
-       }
-       if (this->bit_pos)
-       {
-               return bad_bitpos(this, rule_number);
-       }
-       if (output_pos)
-       {
-               /* assuming little endian host order */
-               *(output_pos + 1) = ntohl(*((u_int32_t*)this->byte_pos));
-               *output_pos = ntohl(*(((u_int32_t*)this->byte_pos) + 1));
-               DBG3(DBG_ENC, "   => %b", output_pos, sizeof(u_int64_t));
-       }
-       this->byte_pos += sizeof(u_int64_t);
-       return TRUE;
-}
-
-/**
  * Parse a given amount of bytes and writes them to a specific location
  */
 static bool parse_bytes(private_parser_t *this, int rule_number,
-                                               u_int8_t *output_pos, size_t bytes)
+                                               u_int8_t *output_pos, int bytes)
 {
        if (this->byte_pos + bytes > this->input_roof)
        {
@@ -302,11 +277,11 @@ static bool parse_bit(private_parser_t *this, int rule_number,
                return short_input(this, rule_number);
        }
        if (output_pos)
-       {       
+       {
                u_int8_t mask;
                mask = 0x01 << (7 - this->bit_pos);
                *output_pos = *this->byte_pos & mask;
-               
+
                if (*output_pos)
                {       /* set to a "clean", comparable true */
                        *output_pos = TRUE;
@@ -325,10 +300,10 @@ static bool parse_bit(private_parser_t *this, int rule_number,
  * Parse substructures in a list.
  */
 static bool parse_list(private_parser_t *this, int rule_number,
-               linked_list_t **output_pos, payload_type_t payload_type, size_t length)
+                       linked_list_t **output_pos, payload_type_t payload_type, int length)
 {
        linked_list_t *list = *output_pos;
-       
+
        if (length < 0)
        {
                return short_input(this, rule_number);
@@ -341,10 +316,10 @@ static bool parse_list(private_parser_t *this, int rule_number,
        {
                u_int8_t *pos_before = this->byte_pos;
                payload_t *payload;
-               
+
                DBG2(DBG_ENC, "  %d bytes left, parsing recursively %N",
                         length, payload_type_names, payload_type);
-               
+
                if (parse_payload(this, payload_type, &payload) != SUCCESS)
                {
                        DBG1(DBG_ENC, "  parsing of a %N substructure failed",
@@ -354,6 +329,12 @@ static bool parse_list(private_parser_t *this, int rule_number,
                list->insert_last(list, payload);
                length -= this->byte_pos - pos_before;
        }
+       if (length != 0)
+       {       /* must yield exactly to zero */
+               DBG1(DBG_ENC, "  length of %N substructure list invalid",
+                        payload_type_names, payload_type);
+               return FALSE;
+       }
        *output_pos = list;
        return TRUE;
 }
@@ -362,7 +343,7 @@ static bool parse_list(private_parser_t *this, int rule_number,
  * Parse data from current parsing position in a chunk.
  */
 static bool parse_chunk(private_parser_t *this, int rule_number,
-                                                       chunk_t *output_pos, size_t length)
+                                               chunk_t *output_pos, int length)
 {
        if (this->byte_pos + length > this->input_roof)
        {
@@ -390,32 +371,33 @@ static status_t parse_payload(private_parser_t *this,
 {
        payload_t *pld;
        void *output;
-       size_t rule_count, payload_length = 0, spi_size = 0, attribute_length = 0;
+       size_t rule_count;
+       int payload_length = 0, spi_size = 0, attribute_length = 0;
        u_int16_t ts_type = 0;
        bool attribute_format = FALSE;
        int rule_number;
        encoding_rule_t *rule;
-       
+
        /* create instance of the payload to parse */
        pld = payload_create(payload_type);
-       
+
        DBG2(DBG_ENC, "parsing %N payload, %d bytes left",
                 payload_type_names, payload_type, this->input_roof - this->byte_pos);
-       
+
        DBG3(DBG_ENC, "parsing payload from %b",
                 this->byte_pos, this->input_roof - this->byte_pos);
-       
+
        if (pld->get_type(pld) == UNKNOWN_PAYLOAD)
        {
                DBG1(DBG_ENC, "  payload type %d is unknown, handling as %N",
                         payload_type, payload_type_names, UNKNOWN_PAYLOAD);
        }
-       
+
        /* base pointer for output, avoids casting in every rule */
        output = pld;
-       
+
        /* parse the payload with its own rulse */
-       pld->get_encoding_rules(pld, &(this->rules), &rule_count);
+       pld->get_encoding_rules(pld, &this->rules, &rule_count);
        for (rule_number = 0; rule_number < rule_count; rule_number++)
        {
                rule = &(this->rules[rule_number]);
@@ -459,15 +441,6 @@ static status_t parse_payload(private_parser_t *this,
                                }
                                break;
                        }
-                       case U_INT_64:
-                       {
-                               if (!parse_uint64(this, rule_number, output + rule->offset))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
                        case IKE_SPI:
                        {
                                if (!parse_bytes(this, rule_number, output + rule->offset, 8))
@@ -615,7 +588,6 @@ static status_t parse_payload(private_parser_t *this,
                                        pld->destroy(pld);
                                        return PARSE_ERROR;
                                }
-                               attribute_format = *(bool*)(output + rule->offset);
                                break;
                        }
                        case CONFIGURATION_ATTRIBUTE_LENGTH:
@@ -792,8 +764,8 @@ static status_t parse_payload(private_parser_t *this,
                        }
                        case ADDRESS:
                        {
-                               size_t address_length = (ts_type == TS_IPV4_ADDR_RANGE) ? 4 : 16;
-                               
+                               int address_length = (ts_type == TS_IPV4_ADDR_RANGE) ? 4 : 16;
+
                                if (!parse_chunk(this, rule_number, output + rule->offset,
                                                                 address_length))
                                {
@@ -836,7 +808,7 @@ static status_t parse_payload(private_parser_t *this,
                /* process next rulue */
                rule++;
        }
-       
+
        *payload = pld;
        DBG2(DBG_ENC, "parsing %N payload finished",
                 payload_type_names, payload_type);
@@ -874,17 +846,17 @@ static void destroy(private_parser_t *this)
 parser_t *parser_create(chunk_t data)
 {
        private_parser_t *this = malloc_thing(private_parser_t);
-       
+
        this->public.parse_payload = (status_t(*)(parser_t*,payload_type_t,payload_t**))parse_payload;
        this->public.reset_context = (void(*)(parser_t*)) reset_context;
        this->public.get_remaining_byte_count = (int (*) (parser_t *))get_remaining_byte_count;
        this->public.destroy = (void(*)(parser_t*)) destroy;
-       
+
        this->input = data.ptr;
        this->byte_pos = data.ptr;
        this->bit_pos = 0;
        this->input_roof = data.ptr + data.len;
-       
+
        return &this->public;
 }