src/resource_id.c
author Dirk Zimmermann <dz@pep.security>
Thu, 11 Apr 2019 10:46:46 +0200
branchIOS-1482
changeset 3505 3fe4b41696d3
parent 3083 da1eb049cca1
permissions -rw-r--r--
IOS-1482 close branch
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include "pEp_internal.h"
     5 #include "resource_id.h"
     6 
     7 pEp_rid_list_t* new_rid_node(pEp_resource_id_type type, const char* resource) {
     8     assert(resource);
     9     if (!resource)
    10         return NULL;
    11 
    12     pEp_rid_list_t* retval = (pEp_rid_list_t*)calloc(1, sizeof(pEp_rid_list_t));
    13     assert(retval);
    14     if (!retval)
    15         return NULL;
    16 
    17     retval->rid_type = type;
    18     retval->rid = strdup(resource);
    19     assert(retval->rid);
    20     if (!retval->rid) {
    21         free(retval);
    22         return NULL;
    23     }
    24     return retval;
    25 }
    26 
    27 void free_rid_list(pEp_rid_list_t* list) {
    28     while (list) {
    29         pEp_rid_list_t* nextptr = list->next;
    30         free(list->rid);
    31         free(list);
    32         list = nextptr;
    33     }
    34 }
    35 
    36 const char* get_resource_ptr_noown(const char* uri) {
    37     char* uri_delim = strstr(uri, "://");
    38     if (!uri_delim)
    39         return uri;
    40     else
    41         return uri_delim + 3;
    42 }
    43 
    44 char* get_resource(char* uri) {
    45     const char* resource_ptr = get_resource_ptr_noown(uri);
    46     char* resource_str = NULL;
    47     if (resource_ptr)
    48         resource_str = strdup(resource_ptr);
    49     return resource_str;
    50 }
    51 
    52 bool is_file_uri(char* str) {
    53     return(!str ? false : strncmp(str, "file://", 7) == 0);
    54 }
    55 
    56 bool is_cid_uri(const char* str) {
    57     return(!str ? false : strncmp(str, "cid://", 6) == 0);
    58 }
    59 
    60 pEp_rid_list_t* parse_uri(const char* uri) {
    61     if (!uri)
    62         return NULL;
    63     pEp_resource_id_type type = (is_cid_uri(uri) ? PEP_RID_CID : PEP_RID_FILENAME);
    64     const char* resource = get_resource_ptr_noown(uri);
    65     return new_rid_node(type, resource);
    66 }
    67 
    68 char* build_uri(const char* uri_prefix, const char* resource) {
    69     if (!uri_prefix || !resource)
    70         return NULL;
    71     const char* delim = "://";
    72     const int delim_len = 3;
    73     int prefix_len = strlen(uri_prefix);
    74     int resource_len = strlen(resource);
    75     int retval_len = prefix_len + delim_len + resource_len;
    76 
    77     char* retval = calloc(1, retval_len + 1);
    78     strlcpy(retval, uri_prefix, retval_len + 1);
    79     strlcat(retval, delim, retval_len + 1);
    80     strlcat(retval, resource, retval_len + 1);
    81     
    82     return retval;
    83 }