src/map_asn1.c
author Volker Birk <vb@pep.foundation>
Wed, 29 Aug 2018 17:10:23 +0200
branchsync
changeset 2899 63b619aef131
parent 2830 d6f044e43e1a
child 3786 008dfd0ca85f
permissions -rw-r--r--
somewhat bigger
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include "pEp_internal.h"
     5 #include "map_asn1.h"
     6 
     7 Identity_t *Identity_from_Struct(
     8         const pEp_identity *ident,
     9         Identity_t *result
    10     )
    11 {
    12     bool allocated = !result;
    13 
    14     assert(ident);
    15     if (!ident)
    16         return NULL;
    17 
    18     if (allocated){
    19         result = (Identity_t *) calloc(1, sizeof(Identity_t));
    20     }
    21     assert(result);
    22     if (!result)
    23         return NULL;
    24 
    25     if (ident->address) {
    26         int r = OCTET_STRING_fromBuf(&result->address, ident->address, -1);
    27         if (r)
    28             goto enomem;
    29     }
    30 
    31     if (ident->fpr) {
    32         if (OCTET_STRING_fromString(&result->fpr, ident->fpr))
    33             goto enomem;
    34     }
    35 
    36     if (ident->user_id) {
    37         int r = OCTET_STRING_fromBuf(&result->user_id, ident->user_id, -1);
    38         if (r)
    39             goto enomem;
    40     }
    41 
    42     if (ident->username) {
    43         int r = OCTET_STRING_fromBuf(&result->username, ident->username, -1);
    44         if (r)
    45             goto enomem;
    46     }
    47 
    48     if (ident->comm_type != PEP_ct_unknown) {
    49         result->comm_type = ident->comm_type;
    50     }
    51 
    52     if (ident->lang[0]) {
    53         int r = OCTET_STRING_fromBuf(&result->lang, ident->lang, 2);
    54         assert(r == 0);
    55         if(r != 0)
    56             goto enomem;
    57     }
    58     else {
    59         int r = OCTET_STRING_fromBuf(&result->lang, "en", 2);
    60         assert(r == 0);
    61         if(r != 0)
    62             goto enomem;
    63     }
    64 
    65     return result;
    66 
    67 enomem:
    68     if (allocated){
    69         ASN_STRUCT_FREE(asn_DEF_Identity, result);
    70     }
    71     return NULL;
    72 }
    73 
    74 pEp_identity *Identity_to_Struct(Identity_t *ident, pEp_identity *result)
    75 {
    76     bool allocated = !result;
    77 
    78     assert(ident);
    79     if (!ident)
    80         return NULL;
    81     
    82     if (allocated)
    83         result = new_identity(NULL, NULL, NULL, NULL);
    84     if (!result)
    85         return NULL;
    86 
    87     result->address = strndup((char *) ident->address.buf,
    88             ident->address.size);
    89     assert(result->address);
    90     if (!result->address)
    91         goto enomem;
    92 
    93     result->fpr = strndup((char *) ident->fpr.buf, ident->fpr.size);
    94     assert(result->fpr);
    95     if (!result->fpr)
    96         goto enomem;
    97 
    98     result->user_id = strndup((char *) ident->user_id.buf,
    99             ident->user_id.size);
   100     assert(result->user_id);
   101     if (!result->user_id)
   102         goto enomem;
   103 
   104     result->username = strndup((char *) ident->username.buf,
   105             ident->username.size);
   106     assert(result->username);
   107     if (!result->username)
   108         goto enomem;
   109 
   110     result->comm_type = (PEP_comm_type) ident->comm_type;
   111 
   112     if (ident->lang.size == 2) {
   113         result->lang[0] = ident->lang.buf[0];
   114         result->lang[1] = ident->lang.buf[1];
   115         result->lang[2] = 0;
   116     }
   117 
   118     return result;
   119 
   120 enomem:
   121     if (allocated)
   122         free_identity(result);
   123     return NULL;
   124 }
   125 
   126 IdentityList_t *IdentityList_from_identity_list(
   127         const identity_list *list,
   128         IdentityList_t *result
   129     )
   130 {
   131     bool allocated = !result;
   132 
   133     assert(list);
   134     if (!list)
   135         return NULL;
   136 
   137     if (allocated)
   138         result = (IdentityList_t *) calloc(1, sizeof(IdentityList_t));
   139     assert(result);
   140     if (!result)
   141         return NULL;
   142 
   143     for (const identity_list *l = list; l && l->ident; l=l->next) {
   144         Identity_t *ident = Identity_from_Struct(l->ident, NULL);
   145         if (ASN_SEQUENCE_ADD(&result->list, ident)) {
   146             ASN_STRUCT_FREE(asn_DEF_Identity, ident);
   147             goto enomem;
   148         }
   149     }
   150 
   151     return result;
   152 
   153 enomem:
   154     if (allocated)
   155         ASN_STRUCT_FREE(asn_DEF_IdentityList, result);
   156     return NULL;
   157 }
   158 
   159 identity_list *IdentityList_to_identity_list(IdentityList_t *list, identity_list *result)
   160 {
   161     bool allocated = !result;
   162 
   163     assert(list);
   164     if (!list)
   165         return NULL;
   166 
   167     if (allocated)
   168         result = new_identity_list(NULL);
   169     if (!result)
   170         return NULL;
   171 
   172     identity_list *r = result;
   173     for (int i=0; i<list->list.count; i++) {
   174         pEp_identity *ident = Identity_to_Struct(list->list.array[i], NULL);
   175         r = identity_list_add(r, ident);
   176         if (!r)
   177             goto enomem;
   178     }
   179 
   180     return result;
   181 
   182 enomem:
   183     if (allocated)
   184         free_identity_list(result);
   185     return NULL;
   186 }
   187