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