- creation from encoded never failes

This commit is contained in:
Martin Willi 2005-12-04 16:37:39 +00:00
parent 6efb10ece3
commit 9151843fec
2 changed files with 42 additions and 23 deletions

View File

@ -33,14 +33,14 @@
* String mappings for id_type_t.
*/
mapping_t id_type_m[] = {
{ID_IPV4_ADDR, "ID_IPV4_ADDR"},
{ID_FQDN, "ID_FQDN"},
{ID_RFC822_ADDR, "ID_RFC822_ADDR"},
{ID_IPV6_ADDR, "ID_IPV6_ADDR"},
{ID_DER_ASN1_DN, "ID_DER_ASN1_DN"},
{ID_DER_ASN1_GN, "ID_DER_ASN1_GN"},
{ID_KEY_ID, "ID_KEY_ID"},
{MAPPING_END, NULL}
{ID_IPV4_ADDR, "ID_IPV4_ADDR"},
{ID_FQDN, "ID_FQDN"},
{ID_RFC822_ADDR, "ID_RFC822_ADDR"},
{ID_IPV6_ADDR, "ID_IPV6_ADDR"},
{ID_DER_ASN1_DN, "ID_DER_ASN1_DN"},
{ID_DER_ASN1_GN, "ID_DER_ASN1_GN"},
{ID_KEY_ID, "ID_KEY_ID"},
{MAPPING_END, NULL}
};
@ -130,7 +130,6 @@ static void destroy(private_identification_t *this)
*/
static private_identification_t *identification_create()
{
private_identification_t *this = allocator_alloc_thing(private_identification_t);
/* assign methods */
@ -152,6 +151,7 @@ static private_identification_t *identification_create()
identification_t *identification_create_from_string(id_type_t type, char *string)
{
private_identification_t *this = identification_create();
this->type = type;
switch (type)
{
@ -192,36 +192,55 @@ identification_t *identification_create_from_string(id_type_t type, char *string
identification_t *identification_create_from_encoding(id_type_t type, chunk_t encoded)
{
private_identification_t *this = identification_create();
this->encoded = allocator_clone_chunk(encoded);
this->type = type;
switch (type)
{
case ID_IPV4_ADDR:
{
char *tmp;
/* clone chunk */
if (encoded.len != 4)
{
allocator_free(this);
return NULL;
}
this->encoded = allocator_clone_chunk(encoded);
tmp = inet_ntoa(*((struct in_addr*)(encoded.ptr)));
/* build string, must be cloned */
this->string = allocator_alloc(strlen(tmp)+1);
strcpy(this->string, tmp);
return &(this->public);
}
case ID_IPV6_ADDR:
{
this->string = "[ID_IPV6_ADDR]";
break;
}
case ID_FQDN:
{
this->string = "[ID_FQDN]";
break;
}
case ID_RFC822_ADDR:
{
this->string = "[ID_RFC822_ADDR]";
break;
}
case ID_DER_ASN1_DN:
{
this->string = "[ID_DER_ASN1_DN]";
break;
}
case ID_DER_ASN1_GN:
{
this->string = "[ID_DER_ASN1_GN]";
break;
}
case ID_KEY_ID:
{
this->string = "[ID_KEY_ID]";
break;
}
default:
{
/* not supported */
allocator_free(this);
return NULL;
this->string = "[unknown id_type_t]";
}
}
return &(this->public);
}

View File

@ -152,7 +152,8 @@ struct identification_t {
*
* @param type type of this id, such as ID_IPV4_ADDR or ID_RFC822_ADDR
* @param string input string, which will be converted
* @return - created identification_t object, or
* @return
* - created identification_t object, or
* - NULL if type not supported.
*
* @ingroup utils
@ -165,8 +166,7 @@ identification_t * identification_create_from_string(id_type_t type, char *strin
*
* @param type type of this id, such as ID_IPV4_ADDR or ID_RFC822_ADDR
* @param encoded encoded bytes, such as from identification_t.get_encoding
* @return - created identification_t object, or
* - NULL if type not supported.
* @return created identification_t object
*
* @ingroup utils
*/