src/bloblist.c
author Volker Birk <vb@pep-project.org>
Wed, 25 Mar 2015 15:20:25 +0100
changeset 125 5119178815d8
parent 113 4bee48270834
child 230 5b36a2663539
permissions -rw-r--r--
Windoze fixes
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@98
    34
    bloblist->data = blob;
vb@98
    35
    bloblist->size = size;
vb@98
    36
vb@98
    37
    return bloblist;
vb@98
    38
}
vb@98
    39
vb@98
    40
DYNAMIC_API void free_bloblist(bloblist_t *bloblist)
vb@98
    41
{
vb@98
    42
    if (bloblist) {
vb@98
    43
        if (bloblist->next)
vb@98
    44
            free_bloblist(bloblist->next);
vb@98
    45
        free(bloblist->data);
vb@98
    46
        free(bloblist->mime_type);
vb@113
    47
        free(bloblist->filename);
vb@98
    48
        free(bloblist);
vb@98
    49
    }
vb@98
    50
}
vb@98
    51
vb@98
    52
DYNAMIC_API bloblist_t *bloblist_dup(const bloblist_t *src)
vb@98
    53
{
vb@98
    54
    bloblist_t *bloblist = NULL;
vb@98
    55
vb@98
    56
    assert(src);
vb@98
    57
vb@113
    58
    bloblist = new_bloblist(src->data, src->size, src->mime_type, src->filename);
vb@98
    59
    if (bloblist == NULL)
vb@98
    60
        goto enomem;
vb@98
    61
vb@98
    62
    if (src->next) {
vb@98
    63
        bloblist->next = bloblist_dup(src->next);
vb@98
    64
        if (bloblist->next == NULL)
vb@98
    65
            goto enomem;
vb@98
    66
    }
vb@98
    67
vb@98
    68
    return bloblist;
vb@98
    69
vb@98
    70
enomem:
vb@98
    71
    free_bloblist(bloblist);
vb@98
    72
    return NULL;
vb@98
    73
}
vb@98
    74
vb@98
    75
DYNAMIC_API bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size,
vb@113
    76
        const char *mime_type, const char *filename)
vb@98
    77
{
vb@98
    78
    assert(blob);
vb@98
    79
vb@98
    80
    if (bloblist == NULL)
vb@113
    81
        return new_bloblist(blob, size, mime_type, filename);
vb@98
    82
vb@98
    83
    if (bloblist->data == NULL) {
vb@98
    84
        if (mime_type) {
vb@98
    85
            bloblist->mime_type = strdup(mime_type);
vb@98
    86
            if (bloblist->mime_type == NULL) {
vb@98
    87
                free(bloblist);
vb@98
    88
                return NULL;
vb@98
    89
            }
vb@98
    90
        }
vb@113
    91
        if (filename) {
vb@113
    92
            bloblist->filename = strdup(filename);
vb@113
    93
            if (bloblist->filename == NULL) {
vb@98
    94
                free(bloblist->mime_type);
vb@98
    95
                free(bloblist);
vb@98
    96
                return NULL;
vb@98
    97
            }
vb@98
    98
        }
vb@98
    99
        bloblist->data = blob;
vb@98
   100
        bloblist->size = size;
vb@98
   101
        return bloblist;
vb@98
   102
    }
vb@98
   103
vb@98
   104
    if (bloblist->next == NULL) {
vb@113
   105
        bloblist->next = new_bloblist(blob, size, mime_type, filename);
vb@98
   106
        return bloblist->next;
vb@98
   107
    }
vb@98
   108
vb@113
   109
    return bloblist_add(bloblist->next, blob, size, mime_type, filename);
vb@98
   110
}
vb@98
   111