1 // This file is under GNU General Public License 3.0
4 #include "pEp_internal.h"
12 static bool set_blob_data(bloblist_t* bloblist, char* blob, size_t size, const char* mime_type,
13 const char* filename) {
19 bloblist->mime_type = strdup(mime_type);
20 if (bloblist->mime_type == NULL) {
26 bloblist->filename = strdup(filename);
27 if (bloblist->filename == NULL) {
28 free(bloblist->mime_type);
31 /* Default behaviour, can be overwritten post-allocation with
32 set_blob_content_disposition */
33 if (strstr(filename, "cid://") == filename)
34 bloblist->disposition = PEP_CONTENT_DISP_INLINE;
39 bloblist->value = blob;
40 bloblist->size = size;
46 DYNAMIC_API bloblist_t *new_bloblist(char *blob, size_t size, const char *mime_type,
49 bloblist_t * bloblist = calloc(1, sizeof(bloblist_t));
54 if (!set_blob_data(bloblist, blob, size, mime_type, filename)) {
62 DYNAMIC_API void free_bloblist(bloblist_t *bloblist)
64 bloblist_t *curr = bloblist;
67 bloblist_t *next = curr->next;
69 free(curr->mime_type);
76 DYNAMIC_API bloblist_t *bloblist_dup(const bloblist_t *src)
82 bloblist_t *bloblist = NULL;
85 char *blob2 = malloc(src->size);
90 memcpy(blob2, src->value, src->size);
92 bloblist = new_bloblist(blob2, src->size, src->mime_type, src->filename);
97 bloblist_t* src_curr = src->next;
98 bloblist_t** dst_curr_ptr = &bloblist->next;
102 blob2 = malloc(src_curr->size);
108 memcpy(blob2, src_curr->value, src_curr->size);
109 *dst_curr_ptr = new_bloblist(blob2, src_curr->size, src_curr->mime_type, src_curr->filename);
110 if (*dst_curr_ptr == NULL)
113 src_curr = src_curr->next;
114 dst_curr_ptr = &((*dst_curr_ptr)->next);
121 free_bloblist(bloblist);
125 DYNAMIC_API bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size,
126 const char *mime_type, const char *filename)
132 if (bloblist == NULL)
133 return new_bloblist(blob, size, mime_type, filename);
135 if (bloblist->value == NULL) { // empty list
136 if (bloblist->next != NULL)
137 return NULL; // invalid list
139 if (!set_blob_data(bloblist, blob, size, mime_type, filename)) {
147 bloblist_t* list_curr = bloblist;
149 while (list_curr->next)
150 list_curr = list_curr->next;
152 list_curr->next = new_bloblist(blob, size, mime_type, filename);
154 assert(list_curr->next);
155 if (list_curr->next == NULL)
158 return list_curr->next;
162 DYNAMIC_API int bloblist_length(const bloblist_t *bloblist)
166 for (const bloblist_t *_bl = bloblist; _bl && _bl->value; _bl = _bl->next)
173 DYNAMIC_API bloblist_t* bloblist_iterate(bloblist_t *bloblist, bool(*func)(bloblist_t* element))
175 while(bloblist && !func(bloblist))
177 bloblist = bloblist->next;
183 DYNAMIC_API void set_blob_disposition(bloblist_t* blob,
184 content_disposition_type disposition) {
186 blob->disposition = disposition;