src/map_asn1.c
author Volker Birk <vb@pep.foundation>
Tue, 27 Dec 2016 21:13:41 +0100
changeset 1513 e7f7e42385b5
parent 1257 0c12d65fe859
child 1670 8e2509f0ddad
permissions -rw-r--r--
adding license info to each file
vb@1513
     1
// This file is under GNU General Public License 3.0
vb@1513
     2
// see LICENSE.txt
vb@1513
     3
vb@639
     4
#include "pEp_internal.h"
vb@639
     5
#include "map_asn1.h"
vb@639
     6
vb@667
     7
Identity_t *Identity_from_Struct(
vb@667
     8
        const pEp_identity *ident,
vb@667
     9
        Identity_t *result
vb@667
    10
    )
vb@639
    11
{
vb@645
    12
    assert(ident);
vb@645
    13
    if (!ident)
vb@645
    14
        return NULL;
vb@645
    15
vb@654
    16
    if (!result)
vb@654
    17
        result = (Identity_t *) calloc(1, sizeof(Identity_t));
vb@639
    18
    assert(result);
vb@639
    19
    if (!result)
vb@639
    20
        return NULL;
vb@639
    21
vb@639
    22
    if (ident->address) {
vb@639
    23
        result->address = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
vb@639
    24
                ident->address, -1);
vb@639
    25
        if (ident->address && !result->address)
vb@639
    26
            goto enomem;
vb@639
    27
    }
vb@639
    28
vb@639
    29
    if (ident->fpr) {
vb@639
    30
        if (OCTET_STRING_fromString(&result->fpr, ident->fpr))
vb@639
    31
            goto enomem;
vb@639
    32
    }
vb@639
    33
vb@639
    34
    if (ident->user_id) {
vb@639
    35
        result->user_id = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
vb@639
    36
                ident->user_id, -1);
vb@639
    37
        if (ident->user_id && !result->user_id)
vb@639
    38
            goto enomem;
vb@639
    39
    }
vb@639
    40
vb@639
    41
    if (ident->username) {
vb@639
    42
        result->username = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
vb@639
    43
                ident->username, -1);
vb@639
    44
        if (ident->username && !result->username)
vb@639
    45
            goto enomem;
vb@639
    46
    }
vb@639
    47
vb@639
    48
    if (ident->comm_type != PEP_ct_unknown) {
vb@639
    49
        result->comm_type = malloc(sizeof(long));
vb@639
    50
        assert(result->comm_type);
vb@639
    51
        if (!result->comm_type)
vb@639
    52
            goto enomem;
vb@639
    53
        *result->comm_type = ident->comm_type;
vb@639
    54
    }
vb@639
    55
vb@639
    56
    if (ident->lang[0]) {
vb@1077
    57
        int r = OCTET_STRING_fromBuf(&result->lang, ident->lang, 2);
vb@1077
    58
        assert(r == 0);
vb@1077
    59
    }
vb@1077
    60
    else {
vb@1077
    61
        int r = OCTET_STRING_fromBuf(&result->lang, "en", 2);
vb@1077
    62
        assert(r == 0);
vb@639
    63
    }
vb@640
    64
vb@639
    65
    return result;
vb@639
    66
vb@639
    67
enomem:
vb@645
    68
    ASN_STRUCT_FREE(asn_DEF_Identity, result);
vb@639
    69
    return NULL;
vb@639
    70
}
vb@639
    71
vb@654
    72
pEp_identity *Identity_to_Struct(Identity_t *ident, pEp_identity *result)
vb@639
    73
{
vb@645
    74
    assert(ident);
vb@645
    75
    if (!ident)
vb@645
    76
        return NULL;
vb@645
    77
    
vb@654
    78
    if (!result)
vb@654
    79
        result = new_identity(NULL, NULL, NULL, NULL);
vb@639
    80
    if (!result)
vb@639
    81
        return NULL;
vb@639
    82
vb@640
    83
    if (ident->address) {
vb@640
    84
        result->address = strndup((char *) ident->address->buf,
vb@640
    85
                ident->address->size);
vb@640
    86
        assert(result->address);
vb@640
    87
        if (!result->address)
vb@640
    88
            goto enomem;
vb@640
    89
    }
vb@640
    90
vb@640
    91
    result->fpr = strndup((char *) ident->fpr.buf, ident->fpr.size);
vb@640
    92
    assert(result->fpr);
vb@640
    93
    if (!result->fpr)
vb@640
    94
        goto enomem;
vb@640
    95
vb@640
    96
    if (ident->user_id) {
vb@640
    97
        result->user_id = strndup((char *) ident->user_id->buf,
vb@640
    98
                ident->user_id->size);
vb@640
    99
        assert(result->user_id);
vb@640
   100
        if (!result->user_id)
vb@640
   101
            goto enomem;
vb@640
   102
    }
vb@640
   103
vb@640
   104
    if (ident->username) {
vb@640
   105
        result->username = strndup((char *) ident->username->buf,
vb@640
   106
                ident->username->size);
vb@640
   107
        assert(result->username);
vb@640
   108
        if (!result->username)
vb@640
   109
            goto enomem;
vb@640
   110
    }
vb@640
   111
vb@640
   112
    if (ident->comm_type)
vb@640
   113
        result->comm_type = (PEP_comm_type) *ident->comm_type;
vb@640
   114
vb@1077
   115
    if (ident->lang.size == 2) {
vb@1077
   116
        result->lang[0] = ident->lang.buf[0];
vb@1077
   117
        result->lang[1] = ident->lang.buf[1];
vb@1077
   118
        result->lang[2] = 0;
vb@640
   119
    }
vb@640
   120
vb@640
   121
    return result;
vb@640
   122
vb@640
   123
enomem:
vb@640
   124
    free_identity(result);
vb@640
   125
    return NULL;
vb@639
   126
}
vb@639
   127
vb@940
   128
IdentityList_t *IdentityList_from_identity_list(
vb@940
   129
        const identity_list *list,
vb@940
   130
        IdentityList_t *result
vb@667
   131
    )
vb@645
   132
{
vb@645
   133
    assert(list);
vb@645
   134
    if (!list)
vb@645
   135
        return NULL;
vb@645
   136
vb@654
   137
    if (!result)
vb@940
   138
        result = (IdentityList_t *) calloc(1, sizeof(IdentityList_t));
vb@645
   139
    assert(result);
vb@645
   140
    if (!result)
vb@645
   141
        return NULL;
vb@645
   142
vb@940
   143
    for (const identity_list *l = list; l && l->ident; l=l->next) {
vb@940
   144
        Identity_t *ident = Identity_from_Struct(l->ident, NULL);
vb@940
   145
        if (ASN_SEQUENCE_ADD(&result->list, ident)) {
vb@940
   146
            ASN_STRUCT_FREE(asn_DEF_Identity, ident);
vb@645
   147
            goto enomem;
vb@647
   148
        }
vb@645
   149
    }
vb@645
   150
vb@645
   151
    return result;
vb@645
   152
vb@645
   153
enomem:
vb@940
   154
    ASN_STRUCT_FREE(asn_DEF_IdentityList, result);
vb@645
   155
    return NULL;
vb@645
   156
}
vb@645
   157
vb@940
   158
identity_list *IdentityList_to_identity_list(IdentityList_t *list, identity_list *result)
vb@645
   159
{
vb@645
   160
    assert(list);
vb@645
   161
    if (!list)
vb@645
   162
        return NULL;
vb@645
   163
vb@654
   164
    if (!result)
vb@940
   165
        result = new_identity_list(NULL);
vb@645
   166
    if (!result)
vb@645
   167
        return NULL;
vb@645
   168
vb@940
   169
    identity_list *r = result;
vb@645
   170
    for (int i=0; i<list->list.count; i++) {
vb@940
   171
        pEp_identity *ident = Identity_to_Struct(list->list.array[i], NULL);
vb@940
   172
        r = identity_list_add(r, ident);
vb@645
   173
        if (!r)
vb@645
   174
            goto enomem;
vb@645
   175
    }
vb@645
   176
vb@645
   177
    return result;
vb@645
   178
vb@645
   179
enomem:
vb@940
   180
    free_identity_list(result);
vb@645
   181
    return NULL;
vb@645
   182
}
vb@645
   183