src/bloblist.c
author Krista Grothoff <krista@pep-project.org>
Sun, 17 Jul 2016 16:52:31 +0200
changeset 898 2d2bd8d77f5e
parent 886 a3a45c619db8
child 899 ce7ae6e03713
permissions -rw-r--r--
fix #16 #17: removed recursion from free_bloblist, added NULL checks.
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;
krista@898
    51
        if (curr->value)
krista@898
    52
            free(curr->value);
krista@898
    53
        if (curr->mime_type)
krista@898
    54
            free(curr->mime_type);
krista@898
    55
        if (curr->filename)
krista@898
    56
            free(curr->filename);
krista@898
    57
        free(curr);
krista@898
    58
        curr = next;
vb@98
    59
    }
vb@98
    60
}
vb@98
    61
vb@98
    62
DYNAMIC_API bloblist_t *bloblist_dup(const bloblist_t *src)
vb@98
    63
{
vb@98
    64
    bloblist_t *bloblist = NULL;
vb@98
    65
vb@98
    66
    assert(src);
vb@98
    67
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
vb@98
    80
    if (src->next) {
vb@98
    81
        bloblist->next = bloblist_dup(src->next);
vb@98
    82
        if (bloblist->next == NULL)
vb@98
    83
            goto enomem;
vb@98
    84
    }
vb@98
    85
vb@98
    86
    return bloblist;
vb@98
    87
vb@98
    88
enomem:
vb@268
    89
    free(blob2);
vb@98
    90
    free_bloblist(bloblist);
vb@98
    91
    return NULL;
vb@98
    92
}
vb@98
    93
vb@98
    94
DYNAMIC_API bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size,
vb@113
    95
        const char *mime_type, const char *filename)
vb@98
    96
{
vb@98
    97
    assert(blob);
vb@98
    98
vb@98
    99
    if (bloblist == NULL)
vb@113
   100
        return new_bloblist(blob, size, mime_type, filename);
vb@98
   101
vb@301
   102
    if (bloblist->value == NULL) {
vb@98
   103
        if (mime_type) {
vb@98
   104
            bloblist->mime_type = strdup(mime_type);
vb@98
   105
            if (bloblist->mime_type == NULL) {
vb@98
   106
                free(bloblist);
vb@98
   107
                return NULL;
vb@98
   108
            }
vb@98
   109
        }
vb@113
   110
        if (filename) {
vb@113
   111
            bloblist->filename = strdup(filename);
vb@113
   112
            if (bloblist->filename == NULL) {
vb@98
   113
                free(bloblist->mime_type);
vb@98
   114
                free(bloblist);
vb@98
   115
                return NULL;
vb@98
   116
            }
vb@98
   117
        }
vb@235
   118
vb@301
   119
        bloblist->value = blob;
vb@98
   120
        bloblist->size = size;
vb@235
   121
vb@98
   122
        return bloblist;
vb@98
   123
    }
vb@98
   124
vb@98
   125
    if (bloblist->next == NULL) {
vb@113
   126
        bloblist->next = new_bloblist(blob, size, mime_type, filename);
vb@98
   127
        return bloblist->next;
vb@98
   128
    }
vb@98
   129
vb@113
   130
    return bloblist_add(bloblist->next, blob, size, mime_type, filename);
vb@98
   131
}
vb@98
   132
vb@230
   133
DYNAMIC_API int bloblist_length(const bloblist_t *bloblist)
vb@230
   134
{
vb@301
   135
    int len = 0;
vb@230
   136
vb@302
   137
    for (const bloblist_t *_bl = bloblist; _bl && _bl->value; _bl = _bl->next)
vb@301
   138
        len++;
vb@230
   139
vb@230
   140
    return len;
vb@230
   141
}