src/map_asn1.c
author Neal H. Walfield <neal@pep.foundation>
Wed, 08 May 2019 12:20:37 +0200
branchemail_comparison
changeset 3649 1dd837adc30b
parent 2830 d6f044e43e1a
child 3786 008dfd0ca85f
permissions -rw-r--r--
Fix public key.
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
{
edouard@1670
    12
    bool allocated = !result;
edouard@1670
    13
vb@645
    14
    assert(ident);
vb@645
    15
    if (!ident)
vb@645
    16
        return NULL;
vb@645
    17
edouard@1670
    18
    if (allocated){
vb@654
    19
        result = (Identity_t *) calloc(1, sizeof(Identity_t));
edouard@1670
    20
    }
vb@639
    21
    assert(result);
vb@639
    22
    if (!result)
vb@639
    23
        return NULL;
vb@639
    24
vb@639
    25
    if (ident->address) {
vb@2830
    26
        int r = OCTET_STRING_fromBuf(&result->address, ident->address, -1);
vb@2830
    27
        if (r)
vb@639
    28
            goto enomem;
vb@639
    29
    }
vb@639
    30
vb@639
    31
    if (ident->fpr) {
vb@639
    32
        if (OCTET_STRING_fromString(&result->fpr, ident->fpr))
vb@639
    33
            goto enomem;
vb@639
    34
    }
vb@639
    35
vb@639
    36
    if (ident->user_id) {
vb@2830
    37
        int r = OCTET_STRING_fromBuf(&result->user_id, ident->user_id, -1);
vb@2830
    38
        if (r)
vb@639
    39
            goto enomem;
vb@639
    40
    }
vb@639
    41
vb@639
    42
    if (ident->username) {
vb@2830
    43
        int r = OCTET_STRING_fromBuf(&result->username, ident->username, -1);
vb@2830
    44
        if (r)
vb@639
    45
            goto enomem;
vb@639
    46
    }
vb@639
    47
vb@639
    48
    if (ident->comm_type != PEP_ct_unknown) {
vb@2830
    49
        result->comm_type = ident->comm_type;
vb@639
    50
    }
vb@639
    51
vb@639
    52
    if (ident->lang[0]) {
vb@1077
    53
        int r = OCTET_STRING_fromBuf(&result->lang, ident->lang, 2);
vb@1077
    54
        assert(r == 0);
edouard@1670
    55
        if(r != 0)
edouard@1670
    56
            goto enomem;
vb@1077
    57
    }
vb@1077
    58
    else {
vb@1077
    59
        int r = OCTET_STRING_fromBuf(&result->lang, "en", 2);
vb@1077
    60
        assert(r == 0);
edouard@1670
    61
        if(r != 0)
edouard@1670
    62
            goto enomem;
vb@639
    63
    }
vb@640
    64
vb@639
    65
    return result;
vb@639
    66
vb@639
    67
enomem:
edouard@1670
    68
    if (allocated){
edouard@1670
    69
        ASN_STRUCT_FREE(asn_DEF_Identity, result);
edouard@1670
    70
    }
vb@639
    71
    return NULL;
vb@639
    72
}
vb@639
    73
vb@654
    74
pEp_identity *Identity_to_Struct(Identity_t *ident, pEp_identity *result)
vb@639
    75
{
edouard@1670
    76
    bool allocated = !result;
edouard@1670
    77
vb@645
    78
    assert(ident);
vb@645
    79
    if (!ident)
vb@645
    80
        return NULL;
vb@645
    81
    
edouard@1670
    82
    if (allocated)
vb@654
    83
        result = new_identity(NULL, NULL, NULL, NULL);
vb@639
    84
    if (!result)
vb@639
    85
        return NULL;
vb@639
    86
vb@2830
    87
    result->address = strndup((char *) ident->address.buf,
vb@2830
    88
            ident->address.size);
vb@2830
    89
    assert(result->address);
vb@2830
    90
    if (!result->address)
vb@2830
    91
        goto enomem;
vb@640
    92
vb@640
    93
    result->fpr = strndup((char *) ident->fpr.buf, ident->fpr.size);
vb@640
    94
    assert(result->fpr);
vb@640
    95
    if (!result->fpr)
vb@640
    96
        goto enomem;
vb@640
    97
vb@2830
    98
    result->user_id = strndup((char *) ident->user_id.buf,
vb@2830
    99
            ident->user_id.size);
vb@2830
   100
    assert(result->user_id);
vb@2830
   101
    if (!result->user_id)
vb@2830
   102
        goto enomem;
vb@640
   103
vb@2830
   104
    result->username = strndup((char *) ident->username.buf,
vb@2830
   105
            ident->username.size);
vb@2830
   106
    assert(result->username);
vb@2830
   107
    if (!result->username)
vb@2830
   108
        goto enomem;
vb@640
   109
vb@2830
   110
    result->comm_type = (PEP_comm_type) ident->comm_type;
vb@640
   111
vb@1077
   112
    if (ident->lang.size == 2) {
vb@1077
   113
        result->lang[0] = ident->lang.buf[0];
vb@1077
   114
        result->lang[1] = ident->lang.buf[1];
vb@1077
   115
        result->lang[2] = 0;
vb@640
   116
    }
vb@640
   117
vb@640
   118
    return result;
vb@640
   119
vb@640
   120
enomem:
edouard@1670
   121
    if (allocated)
edouard@1670
   122
        free_identity(result);
vb@640
   123
    return NULL;
vb@639
   124
}
vb@639
   125
vb@940
   126
IdentityList_t *IdentityList_from_identity_list(
vb@940
   127
        const identity_list *list,
vb@940
   128
        IdentityList_t *result
vb@667
   129
    )
vb@645
   130
{
edouard@1670
   131
    bool allocated = !result;
edouard@1670
   132
vb@645
   133
    assert(list);
vb@645
   134
    if (!list)
vb@645
   135
        return NULL;
vb@645
   136
edouard@1670
   137
    if (allocated)
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:
edouard@1670
   154
    if (allocated)
edouard@1670
   155
        ASN_STRUCT_FREE(asn_DEF_IdentityList, result);
vb@645
   156
    return NULL;
vb@645
   157
}
vb@645
   158
vb@940
   159
identity_list *IdentityList_to_identity_list(IdentityList_t *list, identity_list *result)
vb@645
   160
{
edouard@1670
   161
    bool allocated = !result;
edouard@1670
   162
vb@645
   163
    assert(list);
vb@645
   164
    if (!list)
vb@645
   165
        return NULL;
vb@645
   166
edouard@1670
   167
    if (allocated)
vb@940
   168
        result = new_identity_list(NULL);
vb@645
   169
    if (!result)
vb@645
   170
        return NULL;
vb@645
   171
vb@940
   172
    identity_list *r = result;
vb@645
   173
    for (int i=0; i<list->list.count; i++) {
vb@940
   174
        pEp_identity *ident = Identity_to_Struct(list->list.array[i], NULL);
vb@940
   175
        r = identity_list_add(r, ident);
vb@645
   176
        if (!r)
vb@645
   177
            goto enomem;
vb@645
   178
    }
vb@645
   179
vb@645
   180
    return result;
vb@645
   181
vb@645
   182
enomem:
edouard@1670
   183
    if (allocated)
edouard@1670
   184
        free_identity_list(result);
vb@645
   185
    return NULL;
vb@645
   186
}
vb@645
   187