src/map_asn1.c
author Volker Birk <vb@pep-project.org>
Tue, 17 May 2016 20:56:41 +0200
changeset 645 0bb373719dce
parent 640 65840a4ac45a
child 647 e4cfa409ceef
permissions -rw-r--r--
converting KeyList
     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_ISO936_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;
   130         memset(&key, 0, sizeof(Hash_t));
   131         if (OCTET_STRING_fromBuf(&key, l->value, -1))
   132             goto enomem;
   133 
   134         int r = ASN_SEQUENCE_ADD(&result->list, &key);
   135         ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_Hash, &key);
   136         if (r)
   137             goto enomem;
   138     }
   139 
   140     return result;
   141 
   142 enomem:
   143     ASN_STRUCT_FREE(asn_DEF_KeyList, result);
   144     return NULL;
   145 }
   146 
   147 stringlist_t *KeyList_to_stringlist(KeyList_t *list)
   148 {
   149     assert(list);
   150     if (!list)
   151         return NULL;
   152 
   153     stringlist_t *result = new_stringlist(NULL);
   154     if (!result)
   155         return NULL;
   156 
   157     stringlist_t *r = result;
   158     for (int i=0; i<list->list.count; i++) {
   159         char *str = strndup((char *) list->list.array[i]->buf,
   160                 list->list.array[i]->size);
   161         assert(str);
   162         if (!str)
   163             goto enomem;
   164         r = stringlist_add(r, str);
   165         free(str);
   166         if (!r)
   167             goto enomem;
   168     }
   169 
   170     return result;
   171 
   172 enomem:
   173     free_stringlist(result);
   174     return NULL;
   175 }
   176