src/bloblist.c
author Edouard Tisserant <edouard@pep-project.org>
Tue, 06 Jun 2017 18:10:01 +0200
branchENGINE-179
changeset 1826 720922a950e9
parent 1560 6024c4e5434c
child 1713 71b188fe765d
permissions -rw-r--r--
Closed ENGINE-179 branch
vb@1513
     1
// This file is under GNU General Public License 3.0
vb@1513
     2
// see LICENSE.txt
vb@1513
     3
vb@125
     4
#include "pEp_internal.h"
vb@125
     5
vb@98
     6
#include <stdlib.h>
vb@98
     7
#include <assert.h>
vb@98
     8
#include <string.h>
vb@98
     9
vb@98
    10
#include "bloblist.h"
vb@98
    11
vb@98
    12
DYNAMIC_API bloblist_t *new_bloblist(char *blob, size_t size, const char *mime_type,
vb@113
    13
        const char *filename)
vb@98
    14
{
vb@98
    15
    bloblist_t * bloblist = calloc(1, sizeof(bloblist_t));
vb@98
    16
    assert(bloblist);
vb@98
    17
    if (bloblist == NULL)
vb@98
    18
        return NULL;
vb@98
    19
vb@98
    20
    if (mime_type) {
vb@98
    21
        bloblist->mime_type = strdup(mime_type);
vb@98
    22
        if (bloblist->mime_type == NULL) {
vb@98
    23
            free(bloblist);
vb@98
    24
            return NULL;
vb@98
    25
        }
vb@98
    26
    }
vb@98
    27
vb@113
    28
    if (filename) {
vb@113
    29
        bloblist->filename = strdup(filename);
vb@113
    30
        if (bloblist->filename == NULL) {
vb@98
    31
            free(bloblist->mime_type);
vb@98
    32
            free(bloblist);
vb@98
    33
            return NULL;
vb@98
    34
        }
vb@98
    35
    }
vb@98
    36
vb@289
    37
    if (blob) {
vb@301
    38
        bloblist->value = blob;
vb@289
    39
        bloblist->size = size;
vb@268
    40
    }
vb@98
    41
vb@98
    42
    return bloblist;
vb@98
    43
}
vb@98
    44
vb@98
    45
DYNAMIC_API void free_bloblist(bloblist_t *bloblist)
vb@98
    46
{
roker@1559
    47
    bloblist_t *curr = bloblist;
krista@1440
    48
krista@898
    49
    while (curr) {
roker@1559
    50
        bloblist_t *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
    assert(src);
krista@902
    62
    if (src == NULL)
krista@902
    63
        return NULL;
edouard@1560
    64
    
edouard@1560
    65
    bloblist_t *bloblist = 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
edouard@1560
    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@1440
    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@1440
    95
krista@902
    96
        src_curr = src_curr->next;
krista@902
    97
        dst_curr_ptr = &((*dst_curr_ptr)->next);
vb@98
    98
    }
krista@1440
    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@1440
   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@1440
   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@1440
   145
krista@899
   146
    while (list_curr->next)
krista@899
   147
        list_curr = list_curr->next;
krista@1440
   148
krista@899
   149
    list_curr->next = new_bloblist(blob, size, mime_type, filename);
krista@1440
   150
krista@899
   151
    assert(list_curr->next);
krista@899
   152
    if (list_curr->next == NULL)
krista@899
   153
        return NULL;
krista@1440
   154
krista@899
   155
    return list_curr->next;
krista@1440
   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
}