src/map_asn1.c
author Volker Birk <vb@pep-project.org>
Wed, 18 May 2016 12:54:22 +0200
changeset 654 0aa96447e38f
parent 648 1f83d8ad4f55
child 667 a3615d44326a
permissions -rw-r--r--
...
vb@639
     1
#include "pEp_internal.h"
vb@639
     2
#include "map_asn1.h"
vb@639
     3
vb@654
     4
Identity_t *Identity_from_Struct(const pEp_identity *ident, Identity_t *result)
vb@639
     5
{
vb@645
     6
    assert(ident);
vb@645
     7
    if (!ident)
vb@645
     8
        return NULL;
vb@645
     9
vb@654
    10
    if (!result)
vb@654
    11
        result = (Identity_t *) calloc(1, sizeof(Identity_t));
vb@639
    12
    assert(result);
vb@639
    13
    if (!result)
vb@639
    14
        return NULL;
vb@639
    15
vb@639
    16
    if (ident->address) {
vb@639
    17
        result->address = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
vb@639
    18
                ident->address, -1);
vb@639
    19
        if (ident->address && !result->address)
vb@639
    20
            goto enomem;
vb@639
    21
    }
vb@639
    22
vb@639
    23
    if (ident->fpr) {
vb@639
    24
        if (OCTET_STRING_fromString(&result->fpr, ident->fpr))
vb@639
    25
            goto enomem;
vb@639
    26
    }
vb@639
    27
vb@639
    28
    if (ident->user_id) {
vb@639
    29
        result->user_id = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
vb@639
    30
                ident->user_id, -1);
vb@639
    31
        if (ident->user_id && !result->user_id)
vb@639
    32
            goto enomem;
vb@639
    33
    }
vb@639
    34
vb@639
    35
    if (ident->username) {
vb@639
    36
        result->username = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
vb@639
    37
                ident->username, -1);
vb@639
    38
        if (ident->username && !result->username)
vb@639
    39
            goto enomem;
vb@639
    40
    }
vb@639
    41
vb@639
    42
    if (ident->comm_type != PEP_ct_unknown) {
vb@639
    43
        result->comm_type = malloc(sizeof(long));
vb@639
    44
        assert(result->comm_type);
vb@639
    45
        if (!result->comm_type)
vb@639
    46
            goto enomem;
vb@639
    47
        *result->comm_type = ident->comm_type;
vb@639
    48
    }
vb@639
    49
vb@639
    50
    if (ident->lang[0]) {
vb@648
    51
        result->lang = OCTET_STRING_new_fromBuf(&asn_DEF_ISO639_1,
vb@639
    52
                ident->lang, 2);
vb@639
    53
        if (!result->lang)
vb@639
    54
            goto enomem;
vb@639
    55
    }
vb@640
    56
vb@639
    57
    return result;
vb@639
    58
vb@639
    59
enomem:
vb@645
    60
    ASN_STRUCT_FREE(asn_DEF_Identity, result);
vb@639
    61
    return NULL;
vb@639
    62
}
vb@639
    63
vb@654
    64
pEp_identity *Identity_to_Struct(Identity_t *ident, pEp_identity *result)
vb@639
    65
{
vb@645
    66
    assert(ident);
vb@645
    67
    if (!ident)
vb@645
    68
        return NULL;
vb@645
    69
    
vb@654
    70
    if (!result)
vb@654
    71
        result = new_identity(NULL, NULL, NULL, NULL);
vb@639
    72
    if (!result)
vb@639
    73
        return NULL;
vb@639
    74
vb@640
    75
    if (ident->address) {
vb@640
    76
        result->address = strndup((char *) ident->address->buf,
vb@640
    77
                ident->address->size);
vb@640
    78
        assert(result->address);
vb@640
    79
        if (!result->address)
vb@640
    80
            goto enomem;
vb@640
    81
    }
vb@640
    82
vb@640
    83
    result->fpr = strndup((char *) ident->fpr.buf, ident->fpr.size);
vb@640
    84
    assert(result->fpr);
vb@640
    85
    if (!result->fpr)
vb@640
    86
        goto enomem;
vb@640
    87
vb@640
    88
    if (ident->user_id) {
vb@640
    89
        result->user_id = strndup((char *) ident->user_id->buf,
vb@640
    90
                ident->user_id->size);
vb@640
    91
        assert(result->user_id);
vb@640
    92
        if (!result->user_id)
vb@640
    93
            goto enomem;
vb@640
    94
    }
vb@640
    95
vb@640
    96
    if (ident->username) {
vb@640
    97
        result->username = strndup((char *) ident->username->buf,
vb@640
    98
                ident->username->size);
vb@640
    99
        assert(result->username);
vb@640
   100
        if (!result->username)
vb@640
   101
            goto enomem;
vb@640
   102
    }
vb@640
   103
vb@640
   104
    if (ident->comm_type)
vb@640
   105
        result->comm_type = (PEP_comm_type) *ident->comm_type;
vb@640
   106
vb@640
   107
    if (ident->lang) {
vb@640
   108
        result->lang[0] = ident->lang->buf[0];
vb@640
   109
        result->lang[1] = ident->lang->buf[1];
vb@640
   110
    }
vb@640
   111
vb@640
   112
    return result;
vb@640
   113
vb@640
   114
enomem:
vb@640
   115
    free_identity(result);
vb@640
   116
    return NULL;
vb@639
   117
}
vb@639
   118
vb@654
   119
KeyList_t *KeyList_from_stringlist(const stringlist_t *list, KeyList_t *result)
vb@645
   120
{
vb@645
   121
    assert(list);
vb@645
   122
    if (!list)
vb@645
   123
        return NULL;
vb@645
   124
vb@654
   125
    if (!result)
vb@654
   126
        result = (KeyList_t *) calloc(1, sizeof(KeyList_t));
vb@645
   127
    assert(result);
vb@645
   128
    if (!result)
vb@645
   129
        return NULL;
vb@645
   130
vb@645
   131
    for (const stringlist_t *l = list; l && l->value; l=l->next) {
vb@647
   132
        Hash_t *key = OCTET_STRING_new_fromBuf(&asn_DEF_Hash, l->value, -1);
vb@647
   133
        if (!key)
vb@645
   134
            goto enomem;
vb@645
   135
vb@647
   136
        if (ASN_SEQUENCE_ADD(&result->list, key)) {
vb@647
   137
            ASN_STRUCT_FREE(asn_DEF_Hash, key);
vb@645
   138
            goto enomem;
vb@647
   139
        }
vb@645
   140
    }
vb@645
   141
vb@645
   142
    return result;
vb@645
   143
vb@645
   144
enomem:
vb@645
   145
    ASN_STRUCT_FREE(asn_DEF_KeyList, result);
vb@645
   146
    return NULL;
vb@645
   147
}
vb@645
   148
vb@654
   149
stringlist_t *KeyList_to_stringlist(KeyList_t *list, stringlist_t *result)
vb@645
   150
{
vb@645
   151
    assert(list);
vb@645
   152
    if (!list)
vb@645
   153
        return NULL;
vb@645
   154
vb@654
   155
    if (!result)
vb@654
   156
        result = new_stringlist(NULL);
vb@645
   157
    if (!result)
vb@645
   158
        return NULL;
vb@645
   159
vb@645
   160
    stringlist_t *r = result;
vb@645
   161
    for (int i=0; i<list->list.count; i++) {
vb@645
   162
        char *str = strndup((char *) list->list.array[i]->buf,
vb@645
   163
                list->list.array[i]->size);
vb@645
   164
        assert(str);
vb@645
   165
        if (!str)
vb@645
   166
            goto enomem;
vb@645
   167
        r = stringlist_add(r, str);
vb@645
   168
        free(str);
vb@645
   169
        if (!r)
vb@645
   170
            goto enomem;
vb@645
   171
    }
vb@645
   172
vb@645
   173
    return result;
vb@645
   174
vb@645
   175
enomem:
vb@645
   176
    free_stringlist(result);
vb@645
   177
    return NULL;
vb@645
   178
}
vb@645
   179