src/map_asn1.c
author Volker Birk <vb@pep.foundation>
Wed, 24 Aug 2016 00:30:50 +0200
branchkeysync
changeset 1077 d22b9d70370d
parent 940 5f0a9d0f0ecd
child 1248 c332c47fe26e
permissions -rw-r--r--
...
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@1077
    54
        int r = OCTET_STRING_fromBuf(&result->lang, ident->lang, 2);
vb@1077
    55
        assert(r == 0);
vb@1077
    56
    }
vb@1077
    57
    else {
vb@1077
    58
        int r = OCTET_STRING_fromBuf(&result->lang, "en", 2);
vb@1077
    59
        assert(r == 0);
vb@639
    60
    }
vb@640
    61
vb@639
    62
    return result;
vb@639
    63
vb@639
    64
enomem:
vb@645
    65
    ASN_STRUCT_FREE(asn_DEF_Identity, result);
vb@639
    66
    return NULL;
vb@639
    67
}
vb@639
    68
vb@654
    69
pEp_identity *Identity_to_Struct(Identity_t *ident, pEp_identity *result)
vb@639
    70
{
vb@645
    71
    assert(ident);
vb@645
    72
    if (!ident)
vb@645
    73
        return NULL;
vb@645
    74
    
vb@654
    75
    if (!result)
vb@654
    76
        result = new_identity(NULL, NULL, NULL, NULL);
vb@639
    77
    if (!result)
vb@639
    78
        return NULL;
vb@639
    79
vb@640
    80
    if (ident->address) {
vb@640
    81
        result->address = strndup((char *) ident->address->buf,
vb@640
    82
                ident->address->size);
vb@640
    83
        assert(result->address);
vb@640
    84
        if (!result->address)
vb@640
    85
            goto enomem;
vb@640
    86
    }
vb@640
    87
vb@640
    88
    result->fpr = strndup((char *) ident->fpr.buf, ident->fpr.size);
vb@640
    89
    assert(result->fpr);
vb@640
    90
    if (!result->fpr)
vb@640
    91
        goto enomem;
vb@640
    92
vb@640
    93
    if (ident->user_id) {
vb@640
    94
        result->user_id = strndup((char *) ident->user_id->buf,
vb@640
    95
                ident->user_id->size);
vb@640
    96
        assert(result->user_id);
vb@640
    97
        if (!result->user_id)
vb@640
    98
            goto enomem;
vb@640
    99
    }
vb@640
   100
vb@640
   101
    if (ident->username) {
vb@640
   102
        result->username = strndup((char *) ident->username->buf,
vb@640
   103
                ident->username->size);
vb@640
   104
        assert(result->username);
vb@640
   105
        if (!result->username)
vb@640
   106
            goto enomem;
vb@640
   107
    }
vb@640
   108
vb@640
   109
    if (ident->comm_type)
vb@640
   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:
vb@640
   121
    free_identity(result);
vb@640
   122
    return NULL;
vb@639
   123
}
vb@639
   124
vb@940
   125
IdentityList_t *IdentityList_from_identity_list(
vb@940
   126
        const identity_list *list,
vb@940
   127
        IdentityList_t *result
vb@667
   128
    )
vb@645
   129
{
vb@645
   130
    assert(list);
vb@645
   131
    if (!list)
vb@645
   132
        return NULL;
vb@645
   133
vb@654
   134
    if (!result)
vb@940
   135
        result = (IdentityList_t *) calloc(1, sizeof(IdentityList_t));
vb@645
   136
    assert(result);
vb@645
   137
    if (!result)
vb@645
   138
        return NULL;
vb@645
   139
vb@940
   140
    for (const identity_list *l = list; l && l->ident; l=l->next) {
vb@940
   141
        Identity_t *ident = Identity_from_Struct(l->ident, NULL);
vb@940
   142
        if (ASN_SEQUENCE_ADD(&result->list, ident)) {
vb@940
   143
            ASN_STRUCT_FREE(asn_DEF_Identity, ident);
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@940
   151
    ASN_STRUCT_FREE(asn_DEF_IdentityList, result);
vb@645
   152
    return NULL;
vb@645
   153
}
vb@645
   154
vb@940
   155
identity_list *IdentityList_to_identity_list(IdentityList_t *list, identity_list *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@940
   162
        result = new_identity_list(NULL);
vb@645
   163
    if (!result)
vb@645
   164
        return NULL;
vb@645
   165
vb@940
   166
    identity_list *r = result;
vb@645
   167
    for (int i=0; i<list->list.count; i++) {
vb@940
   168
        pEp_identity *ident = Identity_to_Struct(list->list.array[i], NULL);
vb@940
   169
        r = identity_list_add(r, ident);
vb@645
   170
        if (!r)
vb@645
   171
            goto enomem;
vb@645
   172
    }
vb@645
   173
vb@645
   174
    return result;
vb@645
   175
vb@645
   176
enomem:
vb@940
   177
    free_identity_list(result);
vb@645
   178
    return NULL;
vb@645
   179
}
vb@645
   180