src/map_asn1.c
author Volker Birk <vb@pep-project.org>
Tue, 17 May 2016 12:55:55 +0200
changeset 640 65840a4ac45a
parent 639 0a2411d3d55a
child 645 0bb373719dce
permissions -rw-r--r--
...
     1 #include "pEp_internal.h"
     2 #include "map_asn1.h"
     3 
     4 Identity_t *Identity_from_Struct(const pEp_identity *ident)
     5 {
     6     Identity_t *result = (Identity_t *) calloc(1, sizeof(Identity_t));
     7     assert(result);
     8     if (!result)
     9         return NULL;
    10 
    11     if (ident->address) {
    12         result->address = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
    13                 ident->address, -1);
    14         if (ident->address && !result->address)
    15             goto enomem;
    16     }
    17 
    18     if (ident->fpr) {
    19         if (OCTET_STRING_fromString(&result->fpr, ident->fpr))
    20             goto enomem;
    21     }
    22 
    23     if (ident->user_id) {
    24         result->user_id = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
    25                 ident->user_id, -1);
    26         if (ident->user_id && !result->user_id)
    27             goto enomem;
    28     }
    29 
    30     if (ident->username) {
    31         result->username = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
    32                 ident->username, -1);
    33         if (ident->username && !result->username)
    34             goto enomem;
    35     }
    36 
    37     if (ident->comm_type != PEP_ct_unknown) {
    38         result->comm_type = malloc(sizeof(long));
    39         assert(result->comm_type);
    40         if (!result->comm_type)
    41             goto enomem;
    42         *result->comm_type = ident->comm_type;
    43     }
    44 
    45     if (ident->lang[0]) {
    46         result->lang = OCTET_STRING_new_fromBuf(&asn_DEF_ISO936_1,
    47                 ident->lang, 2);
    48         if (!result->lang)
    49             goto enomem;
    50     }
    51 
    52     return result;
    53 
    54 enomem:
    55     asn_DEF_UTF8String.free_struct(&asn_DEF_UTF8String, result, 0);
    56     return NULL;
    57 }
    58 
    59 pEp_identity *Identity_to_Struct(Identity_t *ident)
    60 {
    61     pEp_identity *result = new_identity(NULL, NULL, NULL, NULL);
    62     if (!result)
    63         return NULL;
    64 
    65     if (ident->address) {
    66         result->address = strndup((char *) ident->address->buf,
    67                 ident->address->size);
    68         assert(result->address);
    69         if (!result->address)
    70             goto enomem;
    71     }
    72 
    73     result->fpr = strndup((char *) ident->fpr.buf, ident->fpr.size);
    74     assert(result->fpr);
    75     if (!result->fpr)
    76         goto enomem;
    77 
    78     return result;
    79 
    80     if (ident->user_id) {
    81         result->user_id = strndup((char *) ident->user_id->buf,
    82                 ident->user_id->size);
    83         assert(result->user_id);
    84         if (!result->user_id)
    85             goto enomem;
    86     }
    87 
    88     if (ident->username) {
    89         result->username = strndup((char *) ident->username->buf,
    90                 ident->username->size);
    91         assert(result->username);
    92         if (!result->username)
    93             goto enomem;
    94     }
    95 
    96     if (ident->comm_type)
    97         result->comm_type = (PEP_comm_type) *ident->comm_type;
    98 
    99     if (ident->lang) {
   100         result->lang[0] = ident->lang->buf[0];
   101         result->lang[1] = ident->lang->buf[1];
   102     }
   103 
   104     return result;
   105 
   106 enomem:
   107     free_identity(result);
   108     return NULL;
   109 }
   110