src/map_asn1.c
changeset 645 0bb373719dce
parent 640 65840a4ac45a
child 647 e4cfa409ceef
     1.1 --- a/src/map_asn1.c	Tue May 17 12:55:55 2016 +0200
     1.2 +++ b/src/map_asn1.c	Tue May 17 20:56:41 2016 +0200
     1.3 @@ -3,6 +3,10 @@
     1.4  
     1.5  Identity_t *Identity_from_Struct(const pEp_identity *ident)
     1.6  {
     1.7 +    assert(ident);
     1.8 +    if (!ident)
     1.9 +        return NULL;
    1.10 +
    1.11      Identity_t *result = (Identity_t *) calloc(1, sizeof(Identity_t));
    1.12      assert(result);
    1.13      if (!result)
    1.14 @@ -52,12 +56,16 @@
    1.15      return result;
    1.16  
    1.17  enomem:
    1.18 -    asn_DEF_UTF8String.free_struct(&asn_DEF_UTF8String, result, 0);
    1.19 +    ASN_STRUCT_FREE(asn_DEF_Identity, result);
    1.20      return NULL;
    1.21  }
    1.22  
    1.23  pEp_identity *Identity_to_Struct(Identity_t *ident)
    1.24  {
    1.25 +    assert(ident);
    1.26 +    if (!ident)
    1.27 +        return NULL;
    1.28 +    
    1.29      pEp_identity *result = new_identity(NULL, NULL, NULL, NULL);
    1.30      if (!result)
    1.31          return NULL;
    1.32 @@ -75,8 +83,6 @@
    1.33      if (!result->fpr)
    1.34          goto enomem;
    1.35  
    1.36 -    return result;
    1.37 -
    1.38      if (ident->user_id) {
    1.39          result->user_id = strndup((char *) ident->user_id->buf,
    1.40                  ident->user_id->size);
    1.41 @@ -108,3 +114,63 @@
    1.42      return NULL;
    1.43  }
    1.44  
    1.45 +KeyList_t *KeyList_from_stringlist(const stringlist_t *list)
    1.46 +{
    1.47 +    assert(list);
    1.48 +    if (!list)
    1.49 +        return NULL;
    1.50 +
    1.51 +    KeyList_t *result = (KeyList_t *) calloc(1, sizeof(KeyList_t));
    1.52 +    assert(result);
    1.53 +    if (!result)
    1.54 +        return NULL;
    1.55 +
    1.56 +    for (const stringlist_t *l = list; l && l->value; l=l->next) {
    1.57 +        Hash_t key;
    1.58 +        memset(&key, 0, sizeof(Hash_t));
    1.59 +        if (OCTET_STRING_fromBuf(&key, l->value, -1))
    1.60 +            goto enomem;
    1.61 +
    1.62 +        int r = ASN_SEQUENCE_ADD(&result->list, &key);
    1.63 +        ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_Hash, &key);
    1.64 +        if (r)
    1.65 +            goto enomem;
    1.66 +    }
    1.67 +
    1.68 +    return result;
    1.69 +
    1.70 +enomem:
    1.71 +    ASN_STRUCT_FREE(asn_DEF_KeyList, result);
    1.72 +    return NULL;
    1.73 +}
    1.74 +
    1.75 +stringlist_t *KeyList_to_stringlist(KeyList_t *list)
    1.76 +{
    1.77 +    assert(list);
    1.78 +    if (!list)
    1.79 +        return NULL;
    1.80 +
    1.81 +    stringlist_t *result = new_stringlist(NULL);
    1.82 +    if (!result)
    1.83 +        return NULL;
    1.84 +
    1.85 +    stringlist_t *r = result;
    1.86 +    for (int i=0; i<list->list.count; i++) {
    1.87 +        char *str = strndup((char *) list->list.array[i]->buf,
    1.88 +                list->list.array[i]->size);
    1.89 +        assert(str);
    1.90 +        if (!str)
    1.91 +            goto enomem;
    1.92 +        r = stringlist_add(r, str);
    1.93 +        free(str);
    1.94 +        if (!r)
    1.95 +            goto enomem;
    1.96 +    }
    1.97 +
    1.98 +    return result;
    1.99 +
   1.100 +enomem:
   1.101 +    free_stringlist(result);
   1.102 +    return NULL;
   1.103 +}
   1.104 +