src/map_asn1.c
author Volker Birk <vb@pep-project.org>
Fri, 29 Jul 2016 12:09:04 +0200
branchkeysync
changeset 940 5f0a9d0f0ecd
parent 667 a3615d44326a
child 1077 d22b9d70370d
permissions -rw-r--r--
switching to full list of identities
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@940
   122
IdentityList_t *IdentityList_from_identity_list(
vb@940
   123
        const identity_list *list,
vb@940
   124
        IdentityList_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@940
   132
        result = (IdentityList_t *) calloc(1, sizeof(IdentityList_t));
vb@645
   133
    assert(result);
vb@645
   134
    if (!result)
vb@645
   135
        return NULL;
vb@645
   136
vb@940
   137
    for (const identity_list *l = list; l && l->ident; l=l->next) {
vb@940
   138
        Identity_t *ident = Identity_from_Struct(l->ident, NULL);
vb@940
   139
        if (ASN_SEQUENCE_ADD(&result->list, ident)) {
vb@940
   140
            ASN_STRUCT_FREE(asn_DEF_Identity, ident);
vb@645
   141
            goto enomem;
vb@647
   142
        }
vb@645
   143
    }
vb@645
   144
vb@645
   145
    return result;
vb@645
   146
vb@645
   147
enomem:
vb@940
   148
    ASN_STRUCT_FREE(asn_DEF_IdentityList, result);
vb@645
   149
    return NULL;
vb@645
   150
}
vb@645
   151
vb@940
   152
identity_list *IdentityList_to_identity_list(IdentityList_t *list, identity_list *result)
vb@645
   153
{
vb@645
   154
    assert(list);
vb@645
   155
    if (!list)
vb@645
   156
        return NULL;
vb@645
   157
vb@654
   158
    if (!result)
vb@940
   159
        result = new_identity_list(NULL);
vb@645
   160
    if (!result)
vb@645
   161
        return NULL;
vb@645
   162
vb@940
   163
    identity_list *r = result;
vb@645
   164
    for (int i=0; i<list->list.count; i++) {
vb@940
   165
        pEp_identity *ident = Identity_to_Struct(list->list.array[i], NULL);
vb@940
   166
        r = identity_list_add(r, ident);
vb@645
   167
        if (!r)
vb@645
   168
            goto enomem;
vb@645
   169
    }
vb@645
   170
vb@645
   171
    return result;
vb@645
   172
vb@645
   173
enomem:
vb@940
   174
    free_identity_list(result);
vb@645
   175
    return NULL;
vb@645
   176
}
vb@645
   177