refactored bloblist data initialisation code ENGINE-233
authorKrista Bennett <krista@pep-project.org>
Tue, 29 Aug 2017 17:50:45 +0200
branchENGINE-233
changeset 2020bb198b900e2d
parent 2017 cb9212af4a8c
child 2021 c15acce7cee9
refactored bloblist data initialisation code
src/bloblist.c
     1.1 --- a/src/bloblist.c	Tue Aug 29 15:06:57 2017 +0200
     1.2 +++ b/src/bloblist.c	Tue Aug 29 17:50:45 2017 +0200
     1.3 @@ -9,6 +9,40 @@
     1.4  
     1.5  #include "bloblist.h"
     1.6  
     1.7 +static bool set_blob_data(bloblist_t* bloblist, char* blob, size_t size, const char* mime_type,
     1.8 +                          const char* filename) {
     1.9 +    
    1.10 +    if (!bloblist)
    1.11 +        return false;
    1.12 +        
    1.13 +    if (mime_type) {
    1.14 +       bloblist->mime_type = strdup(mime_type);
    1.15 +       if (bloblist->mime_type == NULL) {
    1.16 +           return false;
    1.17 +       }
    1.18 +    }
    1.19 +    
    1.20 +    if (filename) {
    1.21 +       bloblist->filename = strdup(filename);
    1.22 +       if (bloblist->filename == NULL) {
    1.23 +           free(bloblist->mime_type);
    1.24 +           return false;
    1.25 +       }
    1.26 +       /* Default behaviour, can be overwritten post-allocation with
    1.27 +          set_blob_content_disposition */
    1.28 +       if (strstr(filename, "cid://") == filename)
    1.29 +           bloblist->disposition = PEP_CONTENT_DISP_INLINE;
    1.30 +                        
    1.31 +    }               
    1.32 +    
    1.33 +    if (blob) {
    1.34 +        bloblist->value = blob;
    1.35 +        bloblist->size = size;
    1.36 +    }
    1.37 +    
    1.38 +    return true;
    1.39 +}
    1.40 +
    1.41  DYNAMIC_API bloblist_t *new_bloblist(char *blob, size_t size, const char *mime_type,
    1.42          const char *filename)
    1.43  {
    1.44 @@ -17,30 +51,9 @@
    1.45      if (bloblist == NULL)
    1.46          return NULL;
    1.47  
    1.48 -    if (mime_type) {
    1.49 -        bloblist->mime_type = strdup(mime_type);
    1.50 -        if (bloblist->mime_type == NULL) {
    1.51 -            free(bloblist);
    1.52 -            return NULL;
    1.53 -        }
    1.54 -    }
    1.55 -
    1.56 -    if (filename) {
    1.57 -        bloblist->filename = strdup(filename);
    1.58 -        if (bloblist->filename == NULL) {
    1.59 -            free(bloblist->mime_type);
    1.60 -            free(bloblist);
    1.61 -            return NULL;
    1.62 -        }
    1.63 -        /* Default behaviour, can be overwritten post-allocation with
    1.64 -           set_blob_content_disposition */
    1.65 -        if (strstr(filename, "cid://") == filename)
    1.66 -            bloblist->disposition = PEP_CONTENT_DISP_INLINE;
    1.67 -    }
    1.68 -
    1.69 -    if (blob) {
    1.70 -        bloblist->value = blob;
    1.71 -        bloblist->size = size;
    1.72 +    if (!set_blob_data(bloblist, blob, size, mime_type, filename)) {
    1.73 +        free(bloblist);
    1.74 +        bloblist = NULL;
    1.75      }
    1.76  
    1.77      return bloblist;
    1.78 @@ -122,29 +135,11 @@
    1.79      if (bloblist->value == NULL) { // empty list
    1.80          if (bloblist->next != NULL)
    1.81              return NULL; // invalid list
    1.82 -
    1.83 -        if (mime_type) {
    1.84 -            bloblist->mime_type = strdup(mime_type);
    1.85 -            if (bloblist->mime_type == NULL) {
    1.86 -                free(bloblist);
    1.87 -                return NULL;
    1.88 -            }
    1.89 +            
    1.90 +        if (!set_blob_data(bloblist, blob, size, mime_type, filename)) {
    1.91 +            free(bloblist);
    1.92 +            bloblist = NULL;
    1.93          }
    1.94 -        if (filename) {
    1.95 -            bloblist->filename = strdup(filename);
    1.96 -            if (bloblist->filename == NULL) {
    1.97 -                free(bloblist->mime_type);
    1.98 -                free(bloblist);
    1.99 -                return NULL;
   1.100 -            }
   1.101 -            /* Default behaviour, can be overwritten post-allocation with
   1.102 -               set_blob_content_disposition */
   1.103 -            if (strstr(filename, "cid://") == filename)
   1.104 -                bloblist->disposition = PEP_CONTENT_DISP_INLINE;
   1.105 -        }
   1.106 -
   1.107 -        bloblist->value = blob;
   1.108 -        bloblist->size = size;
   1.109  
   1.110          return bloblist;
   1.111      }