src/map_asn1.c
author Volker Birk <vb@pep-project.org>
Fri, 29 Jul 2016 12:09:04 +0200
branchkeysync
changeset 940 5f0a9d0f0ecd
parent 667 a3615d44326a
child 1077 d22b9d70370d
permissions -rw-r--r--
switching to full list of identities
     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         result->lang = OCTET_STRING_new_fromBuf(&asn_DEF_ISO639_1,
    55                 ident->lang, 2);
    56         if (!result->lang)
    57             goto enomem;
    58     }
    59 
    60     return result;
    61 
    62 enomem:
    63     ASN_STRUCT_FREE(asn_DEF_Identity, result);
    64     return NULL;
    65 }
    66 
    67 pEp_identity *Identity_to_Struct(Identity_t *ident, pEp_identity *result)
    68 {
    69     assert(ident);
    70     if (!ident)
    71         return NULL;
    72     
    73     if (!result)
    74         result = new_identity(NULL, NULL, NULL, NULL);
    75     if (!result)
    76         return NULL;
    77 
    78     if (ident->address) {
    79         result->address = strndup((char *) ident->address->buf,
    80                 ident->address->size);
    81         assert(result->address);
    82         if (!result->address)
    83             goto enomem;
    84     }
    85 
    86     result->fpr = strndup((char *) ident->fpr.buf, ident->fpr.size);
    87     assert(result->fpr);
    88     if (!result->fpr)
    89         goto enomem;
    90 
    91     if (ident->user_id) {
    92         result->user_id = strndup((char *) ident->user_id->buf,
    93                 ident->user_id->size);
    94         assert(result->user_id);
    95         if (!result->user_id)
    96             goto enomem;
    97     }
    98 
    99     if (ident->username) {
   100         result->username = strndup((char *) ident->username->buf,
   101                 ident->username->size);
   102         assert(result->username);
   103         if (!result->username)
   104             goto enomem;
   105     }
   106 
   107     if (ident->comm_type)
   108         result->comm_type = (PEP_comm_type) *ident->comm_type;
   109 
   110     if (ident->lang) {
   111         result->lang[0] = ident->lang->buf[0];
   112         result->lang[1] = ident->lang->buf[1];
   113     }
   114 
   115     return result;
   116 
   117 enomem:
   118     free_identity(result);
   119     return NULL;
   120 }
   121 
   122 IdentityList_t *IdentityList_from_identity_list(
   123         const identity_list *list,
   124         IdentityList_t *result
   125     )
   126 {
   127     assert(list);
   128     if (!list)
   129         return NULL;
   130 
   131     if (!result)
   132         result = (IdentityList_t *) calloc(1, sizeof(IdentityList_t));
   133     assert(result);
   134     if (!result)
   135         return NULL;
   136 
   137     for (const identity_list *l = list; l && l->ident; l=l->next) {
   138         Identity_t *ident = Identity_from_Struct(l->ident, NULL);
   139         if (ASN_SEQUENCE_ADD(&result->list, ident)) {
   140             ASN_STRUCT_FREE(asn_DEF_Identity, ident);
   141             goto enomem;
   142         }
   143     }
   144 
   145     return result;
   146 
   147 enomem:
   148     ASN_STRUCT_FREE(asn_DEF_IdentityList, result);
   149     return NULL;
   150 }
   151 
   152 identity_list *IdentityList_to_identity_list(IdentityList_t *list, identity_list *result)
   153 {
   154     assert(list);
   155     if (!list)
   156         return NULL;
   157 
   158     if (!result)
   159         result = new_identity_list(NULL);
   160     if (!result)
   161         return NULL;
   162 
   163     identity_list *r = result;
   164     for (int i=0; i<list->list.count; i++) {
   165         pEp_identity *ident = Identity_to_Struct(list->list.array[i], NULL);
   166         r = identity_list_add(r, ident);
   167         if (!r)
   168             goto enomem;
   169     }
   170 
   171     return result;
   172 
   173 enomem:
   174     free_identity_list(result);
   175     return NULL;
   176 }
   177