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.
     1 #include "pEp_internal.h"
     2 
     3 #include <stdlib.h>
     4 #include <assert.h>
     5 #include <string.h>
     6 
     7 #include "bloblist.h"
     8 
     9 DYNAMIC_API bloblist_t *new_bloblist(char *blob, size_t size, const char *mime_type,
    10         const char *filename)
    11 {
    12     bloblist_t * bloblist = calloc(1, sizeof(bloblist_t));
    13     assert(bloblist);
    14     if (bloblist == NULL)
    15         return NULL;
    16 
    17     if (mime_type) {
    18         bloblist->mime_type = strdup(mime_type);
    19         if (bloblist->mime_type == NULL) {
    20             free(bloblist);
    21             return NULL;
    22         }
    23     }
    24 
    25     if (filename) {
    26         bloblist->filename = strdup(filename);
    27         if (bloblist->filename == NULL) {
    28             free(bloblist->mime_type);
    29             free(bloblist);
    30             return NULL;
    31         }
    32     }
    33 
    34     if (blob) {
    35         bloblist->value = blob;
    36         bloblist->size = size;
    37     }
    38 
    39     return bloblist;
    40 }
    41 
    42 DYNAMIC_API void free_bloblist(bloblist_t *bloblist)
    43 {
    44     bloblist_t *curr;
    45     bloblist_t *next;
    46     
    47     curr = bloblist;
    48     
    49     while (curr) {
    50         next = curr->next;
    51         free(curr->value);
    52         free(curr->mime_type);
    53         free(curr->filename);
    54         free(curr);
    55         curr = next;
    56     }
    57 }
    58 
    59 DYNAMIC_API bloblist_t *bloblist_dup(const bloblist_t *src)
    60 {
    61     bloblist_t *bloblist = NULL;
    62 
    63     assert(src);
    64     if (src == NULL)
    65         return NULL;
    66 
    67     // head
    68     char *blob2 = malloc(src->size);
    69     assert(blob2);
    70     if (blob2 == NULL)
    71         goto enomem;
    72 
    73     memcpy(blob2, src->value, src->size);
    74 
    75     bloblist = new_bloblist(blob2, src->size, src->mime_type, src->filename);
    76     if (bloblist == NULL)
    77         goto enomem;
    78     blob2 = NULL;
    79 
    80     bloblist_t* src_curr = src->next;
    81     bloblist_t** dst_curr_ptr = &bloblist->next;
    82     
    83     // list
    84     while (src_curr) {
    85         blob2 = malloc(src_curr->size);
    86 
    87         assert(blob2);
    88         if (blob2 == NULL)
    89             goto enomem;
    90 
    91         memcpy(blob2, src_curr->value, src_curr->size);
    92         *dst_curr_ptr = new_bloblist(blob2, src_curr->size, src_curr->mime_type, src_curr->filename);
    93         if (*dst_curr_ptr == NULL)
    94             goto enomem;
    95         
    96         src_curr = src_curr->next;
    97         dst_curr_ptr = &((*dst_curr_ptr)->next);
    98     }
    99         
   100     return bloblist;
   101 
   102 enomem:
   103     free(blob2);
   104     free_bloblist(bloblist);
   105     return NULL;
   106 }
   107 
   108 DYNAMIC_API bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size,
   109         const char *mime_type, const char *filename)
   110 {
   111     assert(blob);
   112     if (blob == NULL)
   113         return NULL;
   114     
   115     if (bloblist == NULL)
   116         return new_bloblist(blob, size, mime_type, filename);
   117 
   118     if (bloblist->value == NULL) { // empty list
   119         if (bloblist->next != NULL)
   120             return NULL; // invalid list
   121             
   122         if (mime_type) {
   123             bloblist->mime_type = strdup(mime_type);
   124             if (bloblist->mime_type == NULL) {
   125                 free(bloblist);
   126                 return NULL;
   127             }
   128         }
   129         if (filename) {
   130             bloblist->filename = strdup(filename);
   131             if (bloblist->filename == NULL) {
   132                 free(bloblist->mime_type);
   133                 free(bloblist);
   134                 return NULL;
   135             }
   136         }
   137 
   138         bloblist->value = blob;
   139         bloblist->size = size;
   140 
   141         return bloblist;
   142     }
   143 
   144     bloblist_t* list_curr = bloblist;
   145     
   146     while (list_curr->next)
   147         list_curr = list_curr->next;
   148     
   149     list_curr->next = new_bloblist(blob, size, mime_type, filename);
   150     
   151     assert(list_curr->next);
   152     if (list_curr->next == NULL)
   153         return NULL;
   154    
   155     return list_curr->next;
   156     
   157 }
   158 
   159 DYNAMIC_API int bloblist_length(const bloblist_t *bloblist)
   160 {
   161     int len = 0;
   162 
   163     for (const bloblist_t *_bl = bloblist; _bl && _bl->value; _bl = _bl->next)
   164         len++;
   165 
   166     return len;
   167 }