src/identity_list.c
author Krista Grothoff <krista@pep-project.org>
Sat, 24 Sep 2016 18:09:18 +0200
branchENGINE-27
changeset 1185 4b01328f3cf2
parent 914 08a0252d82c3
child 1513 e7f7e42385b5
permissions -rw-r--r--
ENGINE-27: update_identity no longer pays attention to the input identity's fpr, but pulls one from elect_pubkey.
     1 #include "pEp_internal.h"
     2 
     3 #include <stdlib.h>
     4 #include <assert.h>
     5 
     6 #include "pEpEngine.h"
     7 #include "identity_list.h"
     8 
     9 DYNAMIC_API identity_list *new_identity_list(pEp_identity *ident)
    10 {
    11     identity_list *id_list = calloc(1, sizeof(identity_list));
    12     assert(id_list);
    13     if (id_list == NULL)
    14         return NULL;
    15 
    16     id_list->ident = ident;
    17 
    18     return id_list;
    19 }
    20 
    21 DYNAMIC_API identity_list *identity_list_dup(const identity_list *src)
    22 {
    23     assert(src);
    24     if (src == NULL)
    25         return NULL;
    26 
    27     pEp_identity *_ident = identity_dup(src->ident);
    28     if (_ident == NULL)
    29         return NULL;
    30 
    31     identity_list *id_list = new_identity_list(_ident);
    32     if (id_list == NULL) {
    33         free_identity(_ident);
    34         return NULL;
    35     }
    36 
    37     identity_list* src_curr = src->next;
    38     identity_list** dst_curr_ptr = &id_list->next;
    39     
    40     while (src_curr) {
    41         _ident = identity_dup(src_curr->ident);
    42         if (_ident == NULL) {
    43             free_identity_list(id_list);
    44             return NULL;
    45         }
    46         
    47         *dst_curr_ptr = new_identity_list(_ident);
    48         if (*dst_curr_ptr == NULL) {
    49             free_identity(_ident);
    50             free_identity_list(id_list);
    51             return NULL;
    52         }
    53         
    54         src_curr = src_curr->next;
    55         dst_curr_ptr = &((*dst_curr_ptr)->next);
    56     }
    57     
    58     return id_list;
    59     
    60 }
    61 
    62 DYNAMIC_API void free_identity_list(identity_list *id_list)
    63 {
    64     identity_list *curr;
    65     identity_list *next;
    66     
    67     curr = id_list;
    68     
    69     while (curr) {
    70         next = curr->next;
    71         free_identity(curr->ident);
    72         free(curr);
    73         curr = next;
    74     }
    75 }
    76 
    77 DYNAMIC_API identity_list *identity_list_add(identity_list *id_list, pEp_identity *ident)
    78 {
    79     assert(ident);
    80     if (ident == NULL)
    81         return NULL;
    82     
    83     if (id_list == NULL)
    84         return new_identity_list(ident);
    85 
    86     // empty list
    87     if (id_list->ident == NULL) {
    88         if (id_list->next)
    89             return NULL; // invalid list
    90             
    91         id_list->ident = ident;
    92         
    93         if (id_list->ident == NULL)
    94             return NULL;
    95         
    96         return id_list;
    97     }
    98  
    99     identity_list* list_curr = id_list;
   100     while (list_curr->next)
   101         list_curr = list_curr->next;
   102     
   103     list_curr->next = new_identity_list(ident);
   104     
   105     return list_curr->next;
   106 }
   107 
   108 DYNAMIC_API int identity_list_length(const identity_list *id_list)
   109 {
   110     int len = 0;
   111 
   112     for (; id_list && id_list->ident; id_list = id_list->next)
   113         ++len;
   114 
   115     return len;
   116 }