src/map_asn1.c
author Krista Bennett <krista@pep-project.org>
Fri, 04 May 2018 16:30:21 +0200
branchlocal_cpptest
changeset 2652 43b913f99a27
parent 1670 8e2509f0ddad
child 2830 d6f044e43e1a
permissions -rw-r--r--
Shelving broken things to break other things
     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         result->address = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
    27                 ident->address, -1);
    28         if (ident->address && !result->address)
    29             goto enomem;
    30     }
    31 
    32     if (ident->fpr) {
    33         if (OCTET_STRING_fromString(&result->fpr, ident->fpr))
    34             goto enomem;
    35     }
    36 
    37     if (ident->user_id) {
    38         result->user_id = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
    39                 ident->user_id, -1);
    40         if (ident->user_id && !result->user_id)
    41             goto enomem;
    42     }
    43 
    44     if (ident->username) {
    45         result->username = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
    46                 ident->username, -1);
    47         if (ident->username && !result->username)
    48             goto enomem;
    49     }
    50 
    51     if (ident->comm_type != PEP_ct_unknown) {
    52         result->comm_type = malloc(sizeof(long));
    53         assert(result->comm_type);
    54         if (!result->comm_type)
    55             goto enomem;
    56         *result->comm_type = ident->comm_type;
    57     }
    58 
    59     if (ident->lang[0]) {
    60         int r = OCTET_STRING_fromBuf(&result->lang, ident->lang, 2);
    61         assert(r == 0);
    62         if(r != 0)
    63             goto enomem;
    64     }
    65     else {
    66         int r = OCTET_STRING_fromBuf(&result->lang, "en", 2);
    67         assert(r == 0);
    68         if(r != 0)
    69             goto enomem;
    70     }
    71 
    72     return result;
    73 
    74 enomem:
    75     if (allocated){
    76         ASN_STRUCT_FREE(asn_DEF_Identity, result);
    77     }
    78     return NULL;
    79 }
    80 
    81 pEp_identity *Identity_to_Struct(Identity_t *ident, pEp_identity *result)
    82 {
    83     bool allocated = !result;
    84 
    85     assert(ident);
    86     if (!ident)
    87         return NULL;
    88     
    89     if (allocated)
    90         result = new_identity(NULL, NULL, NULL, NULL);
    91     if (!result)
    92         return NULL;
    93 
    94     if (ident->address) {
    95         result->address = strndup((char *) ident->address->buf,
    96                 ident->address->size);
    97         assert(result->address);
    98         if (!result->address)
    99             goto enomem;
   100     }
   101 
   102     result->fpr = strndup((char *) ident->fpr.buf, ident->fpr.size);
   103     assert(result->fpr);
   104     if (!result->fpr)
   105         goto enomem;
   106 
   107     if (ident->user_id) {
   108         result->user_id = strndup((char *) ident->user_id->buf,
   109                 ident->user_id->size);
   110         assert(result->user_id);
   111         if (!result->user_id)
   112             goto enomem;
   113     }
   114 
   115     if (ident->username) {
   116         result->username = strndup((char *) ident->username->buf,
   117                 ident->username->size);
   118         assert(result->username);
   119         if (!result->username)
   120             goto enomem;
   121     }
   122 
   123     if (ident->comm_type)
   124         result->comm_type = (PEP_comm_type) *ident->comm_type;
   125 
   126     if (ident->lang.size == 2) {
   127         result->lang[0] = ident->lang.buf[0];
   128         result->lang[1] = ident->lang.buf[1];
   129         result->lang[2] = 0;
   130     }
   131 
   132     return result;
   133 
   134 enomem:
   135     if (allocated)
   136         free_identity(result);
   137     return NULL;
   138 }
   139 
   140 IdentityList_t *IdentityList_from_identity_list(
   141         const identity_list *list,
   142         IdentityList_t *result
   143     )
   144 {
   145     bool allocated = !result;
   146 
   147     assert(list);
   148     if (!list)
   149         return NULL;
   150 
   151     if (allocated)
   152         result = (IdentityList_t *) calloc(1, sizeof(IdentityList_t));
   153     assert(result);
   154     if (!result)
   155         return NULL;
   156 
   157     for (const identity_list *l = list; l && l->ident; l=l->next) {
   158         Identity_t *ident = Identity_from_Struct(l->ident, NULL);
   159         if (ASN_SEQUENCE_ADD(&result->list, ident)) {
   160             ASN_STRUCT_FREE(asn_DEF_Identity, ident);
   161             goto enomem;
   162         }
   163     }
   164 
   165     return result;
   166 
   167 enomem:
   168     if (allocated)
   169         ASN_STRUCT_FREE(asn_DEF_IdentityList, result);
   170     return NULL;
   171 }
   172 
   173 identity_list *IdentityList_to_identity_list(IdentityList_t *list, identity_list *result)
   174 {
   175     bool allocated = !result;
   176 
   177     assert(list);
   178     if (!list)
   179         return NULL;
   180 
   181     if (allocated)
   182         result = new_identity_list(NULL);
   183     if (!result)
   184         return NULL;
   185 
   186     identity_list *r = result;
   187     for (int i=0; i<list->list.count; i++) {
   188         pEp_identity *ident = Identity_to_Struct(list->list.array[i], NULL);
   189         r = identity_list_add(r, ident);
   190         if (!r)
   191             goto enomem;
   192     }
   193 
   194     return result;
   195 
   196 enomem:
   197     if (allocated)
   198         free_identity_list(result);
   199     return NULL;
   200 }
   201