src/map_asn1.c
author Krista Bennett <krista@pep-project.org>
Fri, 04 May 2018 16:30:21 +0200
branchlocal_cpptest
changeset 2652 43b913f99a27
parent 1670 8e2509f0ddad
child 2830 d6f044e43e1a
permissions -rw-r--r--
Shelving broken things to break other things
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@639
    26
        result->address = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
vb@639
    27
                ident->address, -1);
vb@639
    28
        if (ident->address && !result->address)
vb@639
    29
            goto enomem;
vb@639
    30
    }
vb@639
    31
vb@639
    32
    if (ident->fpr) {
vb@639
    33
        if (OCTET_STRING_fromString(&result->fpr, ident->fpr))
vb@639
    34
            goto enomem;
vb@639
    35
    }
vb@639
    36
vb@639
    37
    if (ident->user_id) {
vb@639
    38
        result->user_id = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
vb@639
    39
                ident->user_id, -1);
vb@639
    40
        if (ident->user_id && !result->user_id)
vb@639
    41
            goto enomem;
vb@639
    42
    }
vb@639
    43
vb@639
    44
    if (ident->username) {
vb@639
    45
        result->username = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
vb@639
    46
                ident->username, -1);
vb@639
    47
        if (ident->username && !result->username)
vb@639
    48
            goto enomem;
vb@639
    49
    }
vb@639
    50
vb@639
    51
    if (ident->comm_type != PEP_ct_unknown) {
vb@639
    52
        result->comm_type = malloc(sizeof(long));
vb@639
    53
        assert(result->comm_type);
vb@639
    54
        if (!result->comm_type)
vb@639
    55
            goto enomem;
vb@639
    56
        *result->comm_type = ident->comm_type;
vb@639
    57
    }
vb@639
    58
vb@639
    59
    if (ident->lang[0]) {
vb@1077
    60
        int r = OCTET_STRING_fromBuf(&result->lang, ident->lang, 2);
vb@1077
    61
        assert(r == 0);
edouard@1670
    62
        if(r != 0)
edouard@1670
    63
            goto enomem;
vb@1077
    64
    }
vb@1077
    65
    else {
vb@1077
    66
        int r = OCTET_STRING_fromBuf(&result->lang, "en", 2);
vb@1077
    67
        assert(r == 0);
edouard@1670
    68
        if(r != 0)
edouard@1670
    69
            goto enomem;
vb@639
    70
    }
vb@640
    71
vb@639
    72
    return result;
vb@639
    73
vb@639
    74
enomem:
edouard@1670
    75
    if (allocated){
edouard@1670
    76
        ASN_STRUCT_FREE(asn_DEF_Identity, result);
edouard@1670
    77
    }
vb@639
    78
    return NULL;
vb@639
    79
}
vb@639
    80
vb@654
    81
pEp_identity *Identity_to_Struct(Identity_t *ident, pEp_identity *result)
vb@639
    82
{
edouard@1670
    83
    bool allocated = !result;
edouard@1670
    84
vb@645
    85
    assert(ident);
vb@645
    86
    if (!ident)
vb@645
    87
        return NULL;
vb@645
    88
    
edouard@1670
    89
    if (allocated)
vb@654
    90
        result = new_identity(NULL, NULL, NULL, NULL);
vb@639
    91
    if (!result)
vb@639
    92
        return NULL;
vb@639
    93
vb@640
    94
    if (ident->address) {
vb@640
    95
        result->address = strndup((char *) ident->address->buf,
vb@640
    96
                ident->address->size);
vb@640
    97
        assert(result->address);
vb@640
    98
        if (!result->address)
vb@640
    99
            goto enomem;
vb@640
   100
    }
vb@640
   101
vb@640
   102
    result->fpr = strndup((char *) ident->fpr.buf, ident->fpr.size);
vb@640
   103
    assert(result->fpr);
vb@640
   104
    if (!result->fpr)
vb@640
   105
        goto enomem;
vb@640
   106
vb@640
   107
    if (ident->user_id) {
vb@640
   108
        result->user_id = strndup((char *) ident->user_id->buf,
vb@640
   109
                ident->user_id->size);
vb@640
   110
        assert(result->user_id);
vb@640
   111
        if (!result->user_id)
vb@640
   112
            goto enomem;
vb@640
   113
    }
vb@640
   114
vb@640
   115
    if (ident->username) {
vb@640
   116
        result->username = strndup((char *) ident->username->buf,
vb@640
   117
                ident->username->size);
vb@640
   118
        assert(result->username);
vb@640
   119
        if (!result->username)
vb@640
   120
            goto enomem;
vb@640
   121
    }
vb@640
   122
vb@640
   123
    if (ident->comm_type)
vb@640
   124
        result->comm_type = (PEP_comm_type) *ident->comm_type;
vb@640
   125
vb@1077
   126
    if (ident->lang.size == 2) {
vb@1077
   127
        result->lang[0] = ident->lang.buf[0];
vb@1077
   128
        result->lang[1] = ident->lang.buf[1];
vb@1077
   129
        result->lang[2] = 0;
vb@640
   130
    }
vb@640
   131
vb@640
   132
    return result;
vb@640
   133
vb@640
   134
enomem:
edouard@1670
   135
    if (allocated)
edouard@1670
   136
        free_identity(result);
vb@640
   137
    return NULL;
vb@639
   138
}
vb@639
   139
vb@940
   140
IdentityList_t *IdentityList_from_identity_list(
vb@940
   141
        const identity_list *list,
vb@940
   142
        IdentityList_t *result
vb@667
   143
    )
vb@645
   144
{
edouard@1670
   145
    bool allocated = !result;
edouard@1670
   146
vb@645
   147
    assert(list);
vb@645
   148
    if (!list)
vb@645
   149
        return NULL;
vb@645
   150
edouard@1670
   151
    if (allocated)
vb@940
   152
        result = (IdentityList_t *) calloc(1, sizeof(IdentityList_t));
vb@645
   153
    assert(result);
vb@645
   154
    if (!result)
vb@645
   155
        return NULL;
vb@645
   156
vb@940
   157
    for (const identity_list *l = list; l && l->ident; l=l->next) {
vb@940
   158
        Identity_t *ident = Identity_from_Struct(l->ident, NULL);
vb@940
   159
        if (ASN_SEQUENCE_ADD(&result->list, ident)) {
vb@940
   160
            ASN_STRUCT_FREE(asn_DEF_Identity, ident);
vb@645
   161
            goto enomem;
vb@647
   162
        }
vb@645
   163
    }
vb@645
   164
vb@645
   165
    return result;
vb@645
   166
vb@645
   167
enomem:
edouard@1670
   168
    if (allocated)
edouard@1670
   169
        ASN_STRUCT_FREE(asn_DEF_IdentityList, result);
vb@645
   170
    return NULL;
vb@645
   171
}
vb@645
   172
vb@940
   173
identity_list *IdentityList_to_identity_list(IdentityList_t *list, identity_list *result)
vb@645
   174
{
edouard@1670
   175
    bool allocated = !result;
edouard@1670
   176
vb@645
   177
    assert(list);
vb@645
   178
    if (!list)
vb@645
   179
        return NULL;
vb@645
   180
edouard@1670
   181
    if (allocated)
vb@940
   182
        result = new_identity_list(NULL);
vb@645
   183
    if (!result)
vb@645
   184
        return NULL;
vb@645
   185
vb@940
   186
    identity_list *r = result;
vb@645
   187
    for (int i=0; i<list->list.count; i++) {
vb@940
   188
        pEp_identity *ident = Identity_to_Struct(list->list.array[i], NULL);
vb@940
   189
        r = identity_list_add(r, ident);
vb@645
   190
        if (!r)
vb@645
   191
            goto enomem;
vb@645
   192
    }
vb@645
   193
vb@645
   194
    return result;
vb@645
   195
vb@645
   196
enomem:
edouard@1670
   197
    if (allocated)
edouard@1670
   198
        free_identity_list(result);
vb@645
   199
    return NULL;
vb@645
   200
}
vb@645
   201