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