src/map_asn1.c
author Roker <roker@pep-project.org>
Tue, 20 Sep 2016 15:54:57 +0200
branchroker-linux
changeset 1168 4d4e3ecbaf8b
parent 667 a3615d44326a
child 940 5f0a9d0f0ecd
permissions -rw-r--r--
merge "default" into my branch
vb@639
     1
#include "pEp_internal.h"
vb@639
     2
#include "map_asn1.h"
vb@639
     3
vb@667
     4
Identity_t *Identity_from_Struct(
vb@667
     5
        const pEp_identity *ident,
vb@667
     6
        Identity_t *result
vb@667
     7
    )
vb@639
     8
{
vb@645
     9
    assert(ident);
vb@645
    10
    if (!ident)
vb@645
    11
        return NULL;
vb@645
    12
vb@654
    13
    if (!result)
vb@654
    14
        result = (Identity_t *) calloc(1, sizeof(Identity_t));
vb@639
    15
    assert(result);
vb@639
    16
    if (!result)
vb@639
    17
        return NULL;
vb@639
    18
vb@639
    19
    if (ident->address) {
vb@639
    20
        result->address = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
vb@639
    21
                ident->address, -1);
vb@639
    22
        if (ident->address && !result->address)
vb@639
    23
            goto enomem;
vb@639
    24
    }
vb@639
    25
vb@639
    26
    if (ident->fpr) {
vb@639
    27
        if (OCTET_STRING_fromString(&result->fpr, ident->fpr))
vb@639
    28
            goto enomem;
vb@639
    29
    }
vb@639
    30
vb@639
    31
    if (ident->user_id) {
vb@639
    32
        result->user_id = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
vb@639
    33
                ident->user_id, -1);
vb@639
    34
        if (ident->user_id && !result->user_id)
vb@639
    35
            goto enomem;
vb@639
    36
    }
vb@639
    37
vb@639
    38
    if (ident->username) {
vb@639
    39
        result->username = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
vb@639
    40
                ident->username, -1);
vb@639
    41
        if (ident->username && !result->username)
vb@639
    42
            goto enomem;
vb@639
    43
    }
vb@639
    44
vb@639
    45
    if (ident->comm_type != PEP_ct_unknown) {
vb@639
    46
        result->comm_type = malloc(sizeof(long));
vb@639
    47
        assert(result->comm_type);
vb@639
    48
        if (!result->comm_type)
vb@639
    49
            goto enomem;
vb@639
    50
        *result->comm_type = ident->comm_type;
vb@639
    51
    }
vb@639
    52
vb@639
    53
    if (ident->lang[0]) {
vb@648
    54
        result->lang = OCTET_STRING_new_fromBuf(&asn_DEF_ISO639_1,
vb@639
    55
                ident->lang, 2);
vb@639
    56
        if (!result->lang)
vb@639
    57
            goto enomem;
vb@639
    58
    }
vb@640
    59
vb@639
    60
    return result;
vb@639
    61
vb@639
    62
enomem:
vb@645
    63
    ASN_STRUCT_FREE(asn_DEF_Identity, result);
vb@639
    64
    return NULL;
vb@639
    65
}
vb@639
    66
vb@654
    67
pEp_identity *Identity_to_Struct(Identity_t *ident, pEp_identity *result)
vb@639
    68
{
vb@645
    69
    assert(ident);
vb@645
    70
    if (!ident)
vb@645
    71
        return NULL;
vb@645
    72
    
vb@654
    73
    if (!result)
vb@654
    74
        result = new_identity(NULL, NULL, NULL, NULL);
vb@639
    75
    if (!result)
vb@639
    76
        return NULL;
vb@639
    77
vb@640
    78
    if (ident->address) {
vb@640
    79
        result->address = strndup((char *) ident->address->buf,
vb@640
    80
                ident->address->size);
vb@640
    81
        assert(result->address);
vb@640
    82
        if (!result->address)
vb@640
    83
            goto enomem;
vb@640
    84
    }
vb@640
    85
vb@640
    86
    result->fpr = strndup((char *) ident->fpr.buf, ident->fpr.size);
vb@640
    87
    assert(result->fpr);
vb@640
    88
    if (!result->fpr)
vb@640
    89
        goto enomem;
vb@640
    90
vb@640
    91
    if (ident->user_id) {
vb@640
    92
        result->user_id = strndup((char *) ident->user_id->buf,
vb@640
    93
                ident->user_id->size);
vb@640
    94
        assert(result->user_id);
vb@640
    95
        if (!result->user_id)
vb@640
    96
            goto enomem;
vb@640
    97
    }
vb@640
    98
vb@640
    99
    if (ident->username) {
vb@640
   100
        result->username = strndup((char *) ident->username->buf,
vb@640
   101
                ident->username->size);
vb@640
   102
        assert(result->username);
vb@640
   103
        if (!result->username)
vb@640
   104
            goto enomem;
vb@640
   105
    }
vb@640
   106
vb@640
   107
    if (ident->comm_type)
vb@640
   108
        result->comm_type = (PEP_comm_type) *ident->comm_type;
vb@640
   109
vb@640
   110
    if (ident->lang) {
vb@640
   111
        result->lang[0] = ident->lang->buf[0];
vb@640
   112
        result->lang[1] = ident->lang->buf[1];
vb@640
   113
    }
vb@640
   114
vb@640
   115
    return result;
vb@640
   116
vb@640
   117
enomem:
vb@640
   118
    free_identity(result);
vb@640
   119
    return NULL;
vb@639
   120
}
vb@639
   121
vb@667
   122
KeyList_t *KeyList_from_stringlist(
vb@667
   123
        const stringlist_t *list,
vb@667
   124
        KeyList_t *result
vb@667
   125
    )
vb@645
   126
{
vb@645
   127
    assert(list);
vb@645
   128
    if (!list)
vb@645
   129
        return NULL;
vb@645
   130
vb@654
   131
    if (!result)
vb@654
   132
        result = (KeyList_t *) calloc(1, sizeof(KeyList_t));
vb@645
   133
    assert(result);
vb@645
   134
    if (!result)
vb@645
   135
        return NULL;
vb@645
   136
vb@645
   137
    for (const stringlist_t *l = list; l && l->value; l=l->next) {
vb@647
   138
        Hash_t *key = OCTET_STRING_new_fromBuf(&asn_DEF_Hash, l->value, -1);
vb@647
   139
        if (!key)
vb@645
   140
            goto enomem;
vb@645
   141
vb@647
   142
        if (ASN_SEQUENCE_ADD(&result->list, key)) {
vb@647
   143
            ASN_STRUCT_FREE(asn_DEF_Hash, key);
vb@645
   144
            goto enomem;
vb@647
   145
        }
vb@645
   146
    }
vb@645
   147
vb@645
   148
    return result;
vb@645
   149
vb@645
   150
enomem:
vb@645
   151
    ASN_STRUCT_FREE(asn_DEF_KeyList, result);
vb@645
   152
    return NULL;
vb@645
   153
}
vb@645
   154
vb@654
   155
stringlist_t *KeyList_to_stringlist(KeyList_t *list, stringlist_t *result)
vb@645
   156
{
vb@645
   157
    assert(list);
vb@645
   158
    if (!list)
vb@645
   159
        return NULL;
vb@645
   160
vb@654
   161
    if (!result)
vb@654
   162
        result = new_stringlist(NULL);
vb@645
   163
    if (!result)
vb@645
   164
        return NULL;
vb@645
   165
vb@645
   166
    stringlist_t *r = result;
vb@645
   167
    for (int i=0; i<list->list.count; i++) {
vb@645
   168
        char *str = strndup((char *) list->list.array[i]->buf,
vb@645
   169
                list->list.array[i]->size);
vb@645
   170
        assert(str);
vb@645
   171
        if (!str)
vb@645
   172
            goto enomem;
vb@645
   173
        r = stringlist_add(r, str);
vb@645
   174
        free(str);
vb@645
   175
        if (!r)
vb@645
   176
            goto enomem;
vb@645
   177
    }
vb@645
   178
vb@645
   179
    return result;
vb@645
   180
vb@645
   181
enomem:
vb@645
   182
    free_stringlist(result);
vb@645
   183
    return NULL;
vb@645
   184
}
vb@645
   185