src/map_asn1.c
author Volker Birk <vb@pep-project.org>
Tue, 17 May 2016 21:43:06 +0200
changeset 648 1f83d8ad4f55
parent 647 e4cfa409ceef
child 654 0aa96447e38f
permissions -rw-r--r--
typo
     1 #include "pEp_internal.h"
     2 #include "map_asn1.h"
     3 
     4 Identity_t *Identity_from_Struct(const pEp_identity *ident)
     5 {
     6     assert(ident);
     7     if (!ident)
     8         return NULL;
     9 
    10     Identity_t *result = (Identity_t *) calloc(1, sizeof(Identity_t));
    11     assert(result);
    12     if (!result)
    13         return NULL;
    14 
    15     if (ident->address) {
    16         result->address = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
    17                 ident->address, -1);
    18         if (ident->address && !result->address)
    19             goto enomem;
    20     }
    21 
    22     if (ident->fpr) {
    23         if (OCTET_STRING_fromString(&result->fpr, ident->fpr))
    24             goto enomem;
    25     }
    26 
    27     if (ident->user_id) {
    28         result->user_id = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
    29                 ident->user_id, -1);
    30         if (ident->user_id && !result->user_id)
    31             goto enomem;
    32     }
    33 
    34     if (ident->username) {
    35         result->username = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
    36                 ident->username, -1);
    37         if (ident->username && !result->username)
    38             goto enomem;
    39     }
    40 
    41     if (ident->comm_type != PEP_ct_unknown) {
    42         result->comm_type = malloc(sizeof(long));
    43         assert(result->comm_type);
    44         if (!result->comm_type)
    45             goto enomem;
    46         *result->comm_type = ident->comm_type;
    47     }
    48 
    49     if (ident->lang[0]) {
    50         result->lang = OCTET_STRING_new_fromBuf(&asn_DEF_ISO639_1,
    51                 ident->lang, 2);
    52         if (!result->lang)
    53             goto enomem;
    54     }
    55 
    56     return result;
    57 
    58 enomem:
    59     ASN_STRUCT_FREE(asn_DEF_Identity, result);
    60     return NULL;
    61 }
    62 
    63 pEp_identity *Identity_to_Struct(Identity_t *ident)
    64 {
    65     assert(ident);
    66     if (!ident)
    67         return NULL;
    68     
    69     pEp_identity *result = new_identity(NULL, NULL, NULL, NULL);
    70     if (!result)
    71         return NULL;
    72 
    73     if (ident->address) {
    74         result->address = strndup((char *) ident->address->buf,
    75                 ident->address->size);
    76         assert(result->address);
    77         if (!result->address)
    78             goto enomem;
    79     }
    80 
    81     result->fpr = strndup((char *) ident->fpr.buf, ident->fpr.size);
    82     assert(result->fpr);
    83     if (!result->fpr)
    84         goto enomem;
    85 
    86     if (ident->user_id) {
    87         result->user_id = strndup((char *) ident->user_id->buf,
    88                 ident->user_id->size);
    89         assert(result->user_id);
    90         if (!result->user_id)
    91             goto enomem;
    92     }
    93 
    94     if (ident->username) {
    95         result->username = strndup((char *) ident->username->buf,
    96                 ident->username->size);
    97         assert(result->username);
    98         if (!result->username)
    99             goto enomem;
   100     }
   101 
   102     if (ident->comm_type)
   103         result->comm_type = (PEP_comm_type) *ident->comm_type;
   104 
   105     if (ident->lang) {
   106         result->lang[0] = ident->lang->buf[0];
   107         result->lang[1] = ident->lang->buf[1];
   108     }
   109 
   110     return result;
   111 
   112 enomem:
   113     free_identity(result);
   114     return NULL;
   115 }
   116 
   117 KeyList_t *KeyList_from_stringlist(const stringlist_t *list)
   118 {
   119     assert(list);
   120     if (!list)
   121         return NULL;
   122 
   123     KeyList_t *result = (KeyList_t *) calloc(1, sizeof(KeyList_t));
   124     assert(result);
   125     if (!result)
   126         return NULL;
   127 
   128     for (const stringlist_t *l = list; l && l->value; l=l->next) {
   129         Hash_t *key = OCTET_STRING_new_fromBuf(&asn_DEF_Hash, l->value, -1);
   130         if (!key)
   131             goto enomem;
   132 
   133         if (ASN_SEQUENCE_ADD(&result->list, key)) {
   134             ASN_STRUCT_FREE(asn_DEF_Hash, key);
   135             goto enomem;
   136         }
   137     }
   138 
   139     return result;
   140 
   141 enomem:
   142     ASN_STRUCT_FREE(asn_DEF_KeyList, result);
   143     return NULL;
   144 }
   145 
   146 stringlist_t *KeyList_to_stringlist(KeyList_t *list)
   147 {
   148     assert(list);
   149     if (!list)
   150         return NULL;
   151 
   152     stringlist_t *result = new_stringlist(NULL);
   153     if (!result)
   154         return NULL;
   155 
   156     stringlist_t *r = result;
   157     for (int i=0; i<list->list.count; i++) {
   158         char *str = strndup((char *) list->list.array[i]->buf,
   159                 list->list.array[i]->size);
   160         assert(str);
   161         if (!str)
   162             goto enomem;
   163         r = stringlist_add(r, str);
   164         free(str);
   165         if (!r)
   166             goto enomem;
   167     }
   168 
   169     return result;
   170 
   171 enomem:
   172     free_stringlist(result);
   173     return NULL;
   174 }
   175