src/stringlist.c
author us@chu.huenfield.org
Tue, 25 Dec 2018 14:46:45 +0100
branchsync
changeset 3209 c15b4ca2b52a
parent 3188 cbc9da42a49e
child 3403 61dbe005c428
permissions -rw-r--r--
Replace use of Sequoia's backend with a custom key store.

- Sequoia's key store doesn't meet pep's needs (in particular, the
ability to search on a key's user id) and trying to shoehorn pep's
needs onto Sequoia's key store abstractions is just introducing
overhead with no appreciable gain in functionality.

- This patch changes the Sequoia backend to use a local sqlite
database to store the public keys.
vb@1517
     1
// This file is under GNU General Public License 3.0
vb@1517
     2
// see LICENSE.txt
vb@1517
     3
vb@125
     4
#include "pEp_internal.h"
vb@125
     5
vb@98
     6
#include <stdlib.h>
vb@98
     7
#include <string.h>
vb@98
     8
#include <assert.h>
vb@98
     9
vb@98
    10
#include "stringlist.h"
vb@98
    11
vb@98
    12
vb@98
    13
DYNAMIC_API stringlist_t *new_stringlist(const char *value)
vb@98
    14
{
vb@107
    15
    stringlist_t *result = calloc(1, sizeof(stringlist_t));
vb@109
    16
    assert(result);
vb@109
    17
vb@98
    18
    if (result && value) {
vb@98
    19
        result->value = strdup(value);
vb@98
    20
        assert(result->value);
vb@98
    21
        if (result->value == 0) {
vb@98
    22
            free(result);
vb@98
    23
            return NULL;
vb@98
    24
        }
vb@98
    25
    }
vb@109
    26
vb@98
    27
    return result;
vb@98
    28
}
vb@98
    29
vb@98
    30
DYNAMIC_API stringlist_t *stringlist_dup(const stringlist_t *src)
vb@98
    31
{
vb@98
    32
    assert(src);
vb@98
    33
    if (src == NULL)
vb@98
    34
        return NULL;
vb@98
    35
vb@98
    36
    stringlist_t *dst = new_stringlist(src->value);
vb@98
    37
    if (dst == NULL)
vb@98
    38
        return NULL;
vb@98
    39
krista@825
    40
    stringlist_t* src_curr = src->next;
krista@825
    41
    stringlist_t** dst_curr_ptr = &dst->next;
krista@825
    42
    
krista@825
    43
    while (src_curr) {
krista@825
    44
        *dst_curr_ptr = new_stringlist(src_curr->value);
krista@895
    45
        if (*dst_curr_ptr == NULL) {
krista@895
    46
            free_stringlist(dst);
krista@895
    47
            return NULL;
krista@895
    48
        }
krista@825
    49
        src_curr = src_curr->next;
krista@825
    50
        dst_curr_ptr = &((*dst_curr_ptr)->next);
vb@98
    51
    }
vb@98
    52
vb@98
    53
    return dst;
vb@98
    54
}
vb@98
    55
edouard@1694
    56
static bool _stringlist_add_first(
edouard@1694
    57
        stringlist_t *stringlist,
edouard@1694
    58
        stringlist_t **result,
edouard@1694
    59
        const char *value
edouard@1694
    60
    )
edouard@1694
    61
{  
edouard@1694
    62
    // empty list (no nodes)
edouard@1694
    63
    if (stringlist == NULL) {
edouard@1694
    64
        *result = new_stringlist(value);
edouard@1694
    65
        return true;
edouard@1694
    66
    }
edouard@1694
    67
edouard@1694
    68
    // empty list (one node, no value)
edouard@1694
    69
    if (stringlist->value == NULL) {
edouard@1694
    70
        if (stringlist->next) {
edouard@1694
    71
            *result = NULL; // invalid list
edouard@1694
    72
            return true;
edouard@1694
    73
        } 
edouard@1694
    74
            
edouard@1694
    75
        stringlist->value = strdup(value);
edouard@1694
    76
        assert(stringlist->value);
edouard@1694
    77
        
edouard@1694
    78
        if (stringlist->value == NULL) {
edouard@1694
    79
            *result = NULL;
edouard@1694
    80
            return true;
edouard@1694
    81
        }
edouard@1694
    82
        
edouard@1694
    83
        *result = stringlist;
edouard@1694
    84
        return true;
edouard@1694
    85
    }
edouard@1694
    86
    return false;
edouard@1694
    87
}
edouard@1694
    88
vb@113
    89
DYNAMIC_API stringlist_t *stringlist_add(
vb@113
    90
        stringlist_t *stringlist,
vb@113
    91
        const char *value
vb@113
    92
    )
krista@827
    93
{  
vb@98
    94
    assert(value);
krista@845
    95
    if (value == NULL)
krista@845
    96
        return NULL;
vb@98
    97
edouard@1694
    98
    stringlist_t *result = NULL;
edouard@1694
    99
    if(_stringlist_add_first(stringlist, &result, value))
edouard@1694
   100
        return result;
krista@843
   101
    
krista@827
   102
    stringlist_t* list_curr = stringlist;
krista@843
   103
krista@827
   104
    while (list_curr->next)
krista@827
   105
        list_curr = list_curr->next;
krista@843
   106
     
krista@827
   107
    list_curr->next = new_stringlist(value);
vb@98
   108
krista@827
   109
    assert(list_curr->next);
krista@827
   110
    if (list_curr->next == NULL)
vb@98
   111
        return NULL;
vb@98
   112
krista@827
   113
    return list_curr->next;
vb@98
   114
}
vb@98
   115
krista@2963
   116
stringlist_t* stringlist_search(stringlist_t* head, const char* value) {
krista@2963
   117
    if (!head || !value || !head->value)
krista@2963
   118
        return NULL;
krista@2963
   119
    stringlist_t* retval = head;
krista@2963
   120
    for (; retval ; retval = retval->next) {
krista@2964
   121
        if (strcmp(retval->value, value) == 0)
krista@2963
   122
            break;
krista@2963
   123
    }
krista@2963
   124
    return retval;
krista@2963
   125
}
krista@2963
   126
edouard@1694
   127
DYNAMIC_API stringlist_t *stringlist_add_unique(
edouard@1694
   128
        stringlist_t *stringlist,
edouard@1694
   129
        const char *value
edouard@1694
   130
    )
edouard@1694
   131
{  
edouard@1694
   132
    assert(value);
edouard@1694
   133
    if (value == NULL)
edouard@1694
   134
        return NULL;
edouard@1694
   135
edouard@1694
   136
    stringlist_t *result = NULL;
edouard@1694
   137
    if(_stringlist_add_first(stringlist, &result, value))
edouard@1694
   138
        return result;
edouard@1694
   139
    
edouard@1694
   140
    stringlist_t* list_curr = stringlist;
edouard@1694
   141
edouard@1694
   142
    bool found = false;
edouard@1694
   143
    while (list_curr->next) {
edouard@1694
   144
        if(strcmp(list_curr->value,value)==0)
edouard@1694
   145
            found = true;
edouard@1694
   146
        list_curr = list_curr->next;
edouard@1694
   147
    }
neal@3188
   148
    if(strcmp(list_curr->value,value)==0)
neal@3188
   149
        found = true;
neal@3188
   150
edouard@1694
   151
    if (!found) {
edouard@1694
   152
        list_curr->next = new_stringlist(value);
edouard@1694
   153
edouard@1694
   154
        assert(list_curr->next);
edouard@1694
   155
        if (list_curr->next == NULL)
edouard@1694
   156
            return NULL;
edouard@1694
   157
edouard@1694
   158
        return list_curr->next;
edouard@1694
   159
    } else {
edouard@1694
   160
        return list_curr;
edouard@1694
   161
    }
edouard@1694
   162
}
edouard@1694
   163
edouard@1694
   164
vb@113
   165
DYNAMIC_API stringlist_t *stringlist_append(
vb@113
   166
        stringlist_t *stringlist,
vb@113
   167
        stringlist_t *second
vb@113
   168
    )
vb@98
   169
{
vb@98
   170
    assert(stringlist);
krista@836
   171
    if (stringlist == NULL)
krista@836
   172
        return NULL;
vb@98
   173
krista@843
   174
    // Second list is empty
vb@98
   175
    if (second == NULL || second->value == NULL)
vb@98
   176
        return stringlist;
vb@98
   177
vb@98
   178
    stringlist_t *_s = stringlist;
vb@98
   179
    stringlist_t *_s2;
vb@98
   180
    for (_s2 = second; _s2 != NULL; _s2 = _s2->next) {
vb@98
   181
        _s = stringlist_add(_s, _s2->value);
vb@98
   182
        if (_s == NULL)
vb@98
   183
            return NULL;
vb@98
   184
    }
vb@98
   185
    return _s;
vb@98
   186
}
vb@98
   187
vb@98
   188
DYNAMIC_API int stringlist_length(const stringlist_t *stringlist)
vb@98
   189
{
vb@301
   190
    int len = 0;
vb@98
   191
vb@301
   192
    const stringlist_t *_sl;
vb@301
   193
    for (_sl = stringlist; _sl && _sl->value; _sl = _sl->next)
vb@301
   194
        len++;
vb@98
   195
vb@98
   196
    return len;
vb@98
   197
}
vb@98
   198
vb@523
   199
DYNAMIC_API stringlist_t *stringlist_delete(
vb@523
   200
        stringlist_t *stringlist,
vb@523
   201
        const char *value
vb@523
   202
    )
vb@523
   203
{
vb@523
   204
    assert(stringlist);
vb@523
   205
    assert(value);
vb@523
   206
vb@523
   207
    if (stringlist->value == NULL) {
vb@523
   208
        free_stringlist(stringlist);
vb@523
   209
        return NULL;
vb@523
   210
    }
vb@523
   211
vb@523
   212
    if (value == NULL)
vb@523
   213
        return stringlist;
vb@523
   214
vb@523
   215
    stringlist_t *_sl;
vb@523
   216
    stringlist_t *last = NULL;
vb@523
   217
    for (_sl = stringlist; _sl && _sl->value; _sl = _sl->next) {
vb@523
   218
        if (strcmp(_sl->value, value) == 0) {
vb@523
   219
            if (last == NULL)
vb@523
   220
                stringlist = stringlist->next;
vb@523
   221
            else
vb@523
   222
                last->next = _sl->next;
vb@523
   223
            _sl->next = NULL;
vb@523
   224
            free_stringlist(_sl);
vb@523
   225
            break;
vb@523
   226
        }
vb@523
   227
        last = _sl;
vb@523
   228
    }
vb@523
   229
    return stringlist;
vb@523
   230
}
vb@523
   231
vb@98
   232
DYNAMIC_API void free_stringlist(stringlist_t *stringlist)
vb@98
   233
{
krista@1615
   234
    stringlist_t *curr = stringlist;
krista@828
   235
    
krista@828
   236
    while (curr) {
roker@1559
   237
        stringlist_t *next = curr->next;
krista@828
   238
        free(curr->value);
krista@1615
   239
        curr->value = NULL;
krista@828
   240
        free(curr);
krista@828
   241
        curr = next;
vb@98
   242
    }
vb@98
   243
}