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