src/bloblist.c
author Krista Grothoff <krista@pep-project.org>
Sat, 24 Sep 2016 18:09:18 +0200
branchENGINE-27
changeset 1185 4b01328f3cf2
parent 904 a3c701c6109f
child 1092 222705a14f1a
permissions -rw-r--r--
ENGINE-27: update_identity no longer pays attention to the input identity's fpr, but pulls one from elect_pubkey.
vb@125
     1
#include "pEp_internal.h"
vb@125
     2
vb@98
     3
#include <stdlib.h>
vb@98
     4
#include <assert.h>
vb@98
     5
#include <string.h>
vb@98
     6
vb@98
     7
#include "bloblist.h"
vb@98
     8
vb@98
     9
DYNAMIC_API bloblist_t *new_bloblist(char *blob, size_t size, const char *mime_type,
vb@113
    10
        const char *filename)
vb@98
    11
{
vb@98
    12
    bloblist_t * bloblist = calloc(1, sizeof(bloblist_t));
vb@98
    13
    assert(bloblist);
vb@98
    14
    if (bloblist == NULL)
vb@98
    15
        return NULL;
vb@98
    16
vb@98
    17
    if (mime_type) {
vb@98
    18
        bloblist->mime_type = strdup(mime_type);
vb@98
    19
        if (bloblist->mime_type == NULL) {
vb@98
    20
            free(bloblist);
vb@98
    21
            return NULL;
vb@98
    22
        }
vb@98
    23
    }
vb@98
    24
vb@113
    25
    if (filename) {
vb@113
    26
        bloblist->filename = strdup(filename);
vb@113
    27
        if (bloblist->filename == NULL) {
vb@98
    28
            free(bloblist->mime_type);
vb@98
    29
            free(bloblist);
vb@98
    30
            return NULL;
vb@98
    31
        }
vb@98
    32
    }
vb@98
    33
vb@289
    34
    if (blob) {
vb@301
    35
        bloblist->value = blob;
vb@289
    36
        bloblist->size = size;
vb@268
    37
    }
vb@98
    38
vb@98
    39
    return bloblist;
vb@98
    40
}
vb@98
    41
vb@98
    42
DYNAMIC_API void free_bloblist(bloblist_t *bloblist)
vb@98
    43
{
krista@898
    44
    bloblist_t *curr;
krista@898
    45
    bloblist_t *next;
krista@898
    46
    
krista@898
    47
    curr = bloblist;
krista@898
    48
    
krista@898
    49
    while (curr) {
krista@898
    50
        next = curr->next;
roker@904
    51
        free(curr->value);
roker@904
    52
        free(curr->mime_type);
roker@904
    53
        free(curr->filename);
krista@898
    54
        free(curr);
krista@898
    55
        curr = next;
vb@98
    56
    }
vb@98
    57
}
vb@98
    58
vb@98
    59
DYNAMIC_API bloblist_t *bloblist_dup(const bloblist_t *src)
vb@98
    60
{
vb@98
    61
    bloblist_t *bloblist = NULL;
vb@98
    62
vb@98
    63
    assert(src);
krista@902
    64
    if (src == NULL)
krista@902
    65
        return NULL;
vb@98
    66
krista@902
    67
    // head
vb@310
    68
    char *blob2 = malloc(src->size);
vb@268
    69
    assert(blob2);
vb@268
    70
    if (blob2 == NULL)
vb@268
    71
        goto enomem;
vb@268
    72
vb@301
    73
    memcpy(blob2, src->value, src->size);
vb@268
    74
vb@268
    75
    bloblist = new_bloblist(blob2, src->size, src->mime_type, src->filename);
vb@98
    76
    if (bloblist == NULL)
vb@98
    77
        goto enomem;
vb@268
    78
    blob2 = NULL;
vb@98
    79
krista@902
    80
    bloblist_t* src_curr = src->next;
krista@902
    81
    bloblist_t** dst_curr_ptr = &bloblist->next;
krista@902
    82
    
krista@902
    83
    // list
krista@902
    84
    while (src_curr) {
krista@902
    85
        blob2 = malloc(src_curr->size);
krista@902
    86
krista@902
    87
        assert(blob2);
krista@902
    88
        if (blob2 == NULL)
vb@98
    89
            goto enomem;
krista@902
    90
krista@902
    91
        memcpy(blob2, src_curr->value, src_curr->size);
krista@902
    92
        *dst_curr_ptr = new_bloblist(blob2, src_curr->size, src_curr->mime_type, src_curr->filename);
krista@902
    93
        if (*dst_curr_ptr == NULL)
krista@902
    94
            goto enomem;
krista@902
    95
        
krista@902
    96
        src_curr = src_curr->next;
krista@902
    97
        dst_curr_ptr = &((*dst_curr_ptr)->next);
vb@98
    98
    }
krista@902
    99
        
vb@98
   100
    return bloblist;
vb@98
   101
vb@98
   102
enomem:
vb@268
   103
    free(blob2);
vb@98
   104
    free_bloblist(bloblist);
vb@98
   105
    return NULL;
vb@98
   106
}
vb@98
   107
vb@98
   108
DYNAMIC_API bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size,
vb@113
   109
        const char *mime_type, const char *filename)
vb@98
   110
{
vb@98
   111
    assert(blob);
krista@899
   112
    if (blob == NULL)
krista@899
   113
        return NULL;
krista@899
   114
    
vb@98
   115
    if (bloblist == NULL)
vb@113
   116
        return new_bloblist(blob, size, mime_type, filename);
vb@98
   117
krista@899
   118
    if (bloblist->value == NULL) { // empty list
krista@901
   119
        if (bloblist->next != NULL)
krista@901
   120
            return NULL; // invalid list
krista@901
   121
            
vb@98
   122
        if (mime_type) {
vb@98
   123
            bloblist->mime_type = strdup(mime_type);
vb@98
   124
            if (bloblist->mime_type == NULL) {
vb@98
   125
                free(bloblist);
vb@98
   126
                return NULL;
vb@98
   127
            }
vb@98
   128
        }
vb@113
   129
        if (filename) {
vb@113
   130
            bloblist->filename = strdup(filename);
vb@113
   131
            if (bloblist->filename == NULL) {
vb@98
   132
                free(bloblist->mime_type);
vb@98
   133
                free(bloblist);
vb@98
   134
                return NULL;
vb@98
   135
            }
vb@98
   136
        }
vb@235
   137
vb@301
   138
        bloblist->value = blob;
vb@98
   139
        bloblist->size = size;
vb@235
   140
vb@98
   141
        return bloblist;
vb@98
   142
    }
vb@98
   143
krista@899
   144
    bloblist_t* list_curr = bloblist;
krista@899
   145
    
krista@899
   146
    while (list_curr->next)
krista@899
   147
        list_curr = list_curr->next;
krista@899
   148
    
krista@899
   149
    list_curr->next = new_bloblist(blob, size, mime_type, filename);
krista@899
   150
    
krista@899
   151
    assert(list_curr->next);
krista@899
   152
    if (list_curr->next == NULL)
krista@899
   153
        return NULL;
krista@899
   154
   
krista@899
   155
    return list_curr->next;
krista@899
   156
    
vb@98
   157
}
vb@98
   158
vb@230
   159
DYNAMIC_API int bloblist_length(const bloblist_t *bloblist)
vb@230
   160
{
vb@301
   161
    int len = 0;
vb@230
   162
vb@302
   163
    for (const bloblist_t *_bl = bloblist; _bl && _bl->value; _bl = _bl->next)
vb@301
   164
        len++;
vb@230
   165
vb@230
   166
    return len;
vb@230
   167
}