src/transport.c
author vb
Tue, 10 Mar 2015 15:23:46 +0100
changeset 95 546e631421b9
parent 94 9ec29aa10b58
child 96 fe4931a0413b
permissions -rw-r--r--
stringpair_t and stringpair_map_t
vb@28
     1
#include "pEp_internal.h"
vb@46
     2
#include "trans_auto.h"
vb@23
     3
vb@23
     4
#include <memory.h>
vb@23
     5
#include <assert.h>
vb@23
     6
vb@62
     7
PEP_STATUS init_transport_system(PEP_SESSION session, bool in_first)
vb@23
     8
{
vb@62
     9
    static PEP_transport_t transports[PEP_trans__count];
vb@62
    10
    
vb@30
    11
    assert(session);
vb@62
    12
    session->transports = transports;
vb@30
    13
vb@62
    14
    if (in_first) {
vb@62
    15
        assert(PEP_trans__count == 1);
vb@62
    16
        memset(transports, 0, sizeof(PEP_transport_t) * PEP_trans__count);
vb@28
    17
vb@62
    18
        transports[PEP_trans_auto].id = PEP_trans_auto;
vb@62
    19
        transports[PEP_trans_auto].sendto = auto_sendto;
vb@62
    20
        transports[PEP_trans_auto].readnext = auto_readnext;
vb@62
    21
    }
vb@23
    22
vb@23
    23
    return PEP_STATUS_OK;
vb@23
    24
}
vb@28
    25
vb@62
    26
void release_transport_system(PEP_SESSION session, bool out_last)
vb@28
    27
{
vb@30
    28
    assert(session);
vb@28
    29
    // nothing yet
vb@28
    30
}
vb@29
    31
vb@48
    32
DYNAMIC_API identity_list *new_identity_list(pEp_identity *ident)
vb@29
    33
{
vb@29
    34
    identity_list *id_list = calloc(1, sizeof(identity_list));
vb@29
    35
    assert(id_list);
vb@76
    36
    if (id_list == NULL)
vb@29
    37
        return NULL;
vb@29
    38
vb@39
    39
    id_list->ident = ident;
vb@29
    40
vb@29
    41
    return id_list;
vb@29
    42
}
vb@29
    43
vb@48
    44
DYNAMIC_API identity_list *identity_list_dup(const identity_list *src)
vb@37
    45
{
vb@37
    46
    assert(src);
vb@37
    47
vb@40
    48
    identity_list *id_list = new_identity_list(identity_dup(src->ident));
vb@37
    49
    assert(id_list);
vb@37
    50
    if (id_list == NULL)
vb@37
    51
        return NULL;
vb@37
    52
vb@37
    53
    if (src->next) {
vb@37
    54
        id_list->next = identity_list_dup(src->next);
vb@37
    55
        if (id_list->next == NULL) {
vb@37
    56
            free_identity_list(id_list);
vb@37
    57
            return NULL;
vb@37
    58
        }
vb@37
    59
    }
vb@37
    60
vb@37
    61
    return id_list;
vb@37
    62
}
vb@37
    63
vb@48
    64
DYNAMIC_API void free_identity_list(identity_list *id_list)
vb@29
    65
{
vb@29
    66
    if (id_list) {
vb@29
    67
        free_identity_list(id_list->next);
vb@29
    68
        free_identity(id_list->ident);
vb@29
    69
        free(id_list);
vb@29
    70
    }
vb@29
    71
}
vb@29
    72
vb@48
    73
DYNAMIC_API identity_list *identity_list_add(identity_list *id_list, pEp_identity *ident)
vb@29
    74
{
vb@29
    75
    assert(ident);
vb@29
    76
vb@38
    77
    if (id_list == NULL)
vb@38
    78
        return new_identity_list(ident);
vb@38
    79
vb@29
    80
    if (id_list->ident == NULL) {
vb@39
    81
        id_list->ident = ident;
vb@39
    82
        return id_list;
vb@29
    83
    }
vb@29
    84
    else if (id_list->next == NULL) {
vb@29
    85
        id_list->next = new_identity_list(ident);
vb@29
    86
        return id_list->next;
vb@29
    87
    }
vb@29
    88
    else {
vb@29
    89
        return identity_list_add(id_list->next, ident);
vb@29
    90
    }
vb@29
    91
}
vb@29
    92
vb@48
    93
DYNAMIC_API bloblist_t *new_bloblist(char *blob, size_t size, const char *mime_type,
vb@39
    94
        const char *file_name)
vb@38
    95
{
vb@38
    96
    bloblist_t * bloblist = calloc(1, sizeof(bloblist_t));
vb@81
    97
    assert(bloblist);
vb@38
    98
    if (bloblist == NULL)
vb@38
    99
        return NULL;
vb@81
   100
vb@39
   101
    if (mime_type) {
vb@39
   102
        bloblist->mime_type = strdup(mime_type);
vb@39
   103
        if (bloblist->mime_type == NULL) {
vb@39
   104
            free(bloblist);
vb@39
   105
            return NULL;
vb@39
   106
        }
vb@39
   107
    }
vb@81
   108
vb@39
   109
    if (file_name) {
vb@39
   110
        bloblist->file_name = strdup(file_name);
vb@39
   111
        if (bloblist->file_name == NULL) {
vb@39
   112
            free(bloblist->mime_type);
vb@39
   113
            free(bloblist);
vb@39
   114
            return NULL;
vb@39
   115
        }
vb@39
   116
    }
vb@81
   117
vb@41
   118
    bloblist->data = blob;
vb@41
   119
    bloblist->size = size;
vb@81
   120
vb@38
   121
    return bloblist;
vb@38
   122
}
vb@38
   123
vb@48
   124
DYNAMIC_API void free_bloblist(bloblist_t *bloblist)
vb@38
   125
{
vb@39
   126
    if (bloblist) {
vb@39
   127
        if (bloblist->next)
vb@39
   128
            free_bloblist(bloblist->next);
vb@41
   129
        free(bloblist->data);
vb@41
   130
        free(bloblist->mime_type);
vb@41
   131
        free(bloblist->file_name);
vb@39
   132
        free(bloblist);
vb@39
   133
    }
vb@38
   134
}
vb@38
   135
vb@81
   136
DYNAMIC_API bloblist_t *bloblist_dup(const bloblist_t *src)
vb@81
   137
{
vb@81
   138
    bloblist_t *bloblist = NULL;
vb@81
   139
vb@81
   140
    assert(src);
vb@81
   141
vb@81
   142
    bloblist = new_bloblist(src->data, src->size, src->mime_type, src->file_name);
vb@81
   143
    if (bloblist == NULL)
vb@81
   144
        goto enomem;
vb@81
   145
vb@81
   146
    if (src->next) {
vb@81
   147
        bloblist->next = bloblist_dup(src->next);
vb@81
   148
        if (bloblist->next == NULL)
vb@81
   149
            goto enomem;
vb@81
   150
    }
vb@81
   151
vb@81
   152
    return bloblist;
vb@81
   153
vb@81
   154
enomem:
vb@81
   155
    free_bloblist(bloblist);
vb@81
   156
    return NULL;
vb@81
   157
}
vb@81
   158
vb@48
   159
DYNAMIC_API bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size,
vb@39
   160
        const char *mime_type, const char *file_name)
vb@38
   161
{
vb@38
   162
    assert(blob);
vb@38
   163
vb@38
   164
    if (bloblist == NULL)
vb@39
   165
        return new_bloblist(blob, size, mime_type, file_name);
vb@38
   166
vb@41
   167
    if (bloblist->data == NULL) {
vb@39
   168
        if (mime_type) {
vb@39
   169
            bloblist->mime_type = strdup(mime_type);
vb@39
   170
            if (bloblist->mime_type == NULL) {
vb@39
   171
                free(bloblist);
vb@39
   172
                return NULL;
vb@39
   173
            }
vb@39
   174
        }
vb@39
   175
        if (file_name) {
vb@39
   176
            bloblist->file_name = strdup(file_name);
vb@39
   177
            if (bloblist->file_name == NULL) {
vb@39
   178
                free(bloblist->mime_type);
vb@39
   179
                free(bloblist);
vb@39
   180
                return NULL;
vb@39
   181
            }
vb@39
   182
        }
vb@41
   183
        bloblist->data = blob;
vb@41
   184
        bloblist->size = size;
vb@38
   185
        return bloblist;
vb@38
   186
    }
vb@38
   187
vb@38
   188
    if (bloblist->next == NULL) {
vb@39
   189
        bloblist->next = new_bloblist(blob, size, mime_type, file_name);
vb@38
   190
        return bloblist->next;
vb@38
   191
    }
vb@38
   192
vb@39
   193
    return bloblist_add(bloblist->next, blob, size, mime_type, file_name);
vb@38
   194
}
vb@38
   195
vb@95
   196
DYNAMIC_API stringpair_t * new_stringpair(const char *key, const char *value)
vb@95
   197
{
vb@95
   198
    stringpair_t *pair = NULL;
vb@95
   199
vb@95
   200
    assert(key);
vb@95
   201
    assert(value),
vb@95
   202
vb@95
   203
    pair = calloc(1, sizeof(stringpair_t));
vb@95
   204
    assert(pair);
vb@95
   205
    if (pair == NULL)
vb@95
   206
        goto enomem;
vb@95
   207
vb@95
   208
    pair->key = strdup(key);
vb@95
   209
    assert(pair->key);
vb@95
   210
    if (pair->key == NULL)
vb@95
   211
        goto enomem;
vb@95
   212
vb@95
   213
    pair->value = strdup(value);
vb@95
   214
    assert(pair->value);
vb@95
   215
    if (pair->value == NULL)
vb@95
   216
        goto enomem;
vb@95
   217
vb@95
   218
    return pair;
vb@95
   219
vb@95
   220
enomem:
vb@95
   221
    free_stringpair(pair);
vb@95
   222
    return NULL;
vb@95
   223
}
vb@95
   224
vb@95
   225
DYNAMIC_API void free_stringpair(stringpair_t * pair)
vb@95
   226
{
vb@95
   227
    if (pair) {
vb@95
   228
        free(pair->key);
vb@95
   229
        free(pair->value);
vb@95
   230
        free(pair);
vb@95
   231
    }
vb@95
   232
}
vb@95
   233
vb@95
   234
DYNAMIC_API stringpair_t * stringpair_dup(const stringpair_t *src)
vb@95
   235
{
vb@95
   236
    assert(src);
vb@95
   237
    return new_stringpair(src->key, src->value);
vb@95
   238
}
vb@95
   239
vb@95
   240
DYNAMIC_API stringpair_map_t * new_stringpair_map(const stringpair_t *pair)
vb@95
   241
{
vb@95
   242
    stringpair_map_t *map = NULL;
vb@95
   243
vb@95
   244
    map = calloc(1, sizeof(stringpair_map_t));
vb@95
   245
    assert(map);
vb@95
   246
    if (map == NULL)
vb@95
   247
        goto enomem;
vb@95
   248
vb@95
   249
    if (pair) {
vb@95
   250
        map->pair = stringpair_dup(pair);
vb@95
   251
        if (map->pair == NULL)
vb@95
   252
            goto enomem;
vb@95
   253
    }
vb@95
   254
vb@95
   255
    return map;
vb@95
   256
vb@95
   257
enomem:
vb@95
   258
    free_stringpair_map(map);
vb@95
   259
    return NULL;
vb@95
   260
}
vb@95
   261
vb@95
   262
DYNAMIC_API void free_stringpair_map(stringpair_map_t *map)
vb@95
   263
{
vb@95
   264
    if (map) {
vb@95
   265
        free_stringpair_map(map->left);
vb@95
   266
        free_stringpair_map(map->right);
vb@95
   267
        free_stringpair(map->pair);
vb@95
   268
        free(map);
vb@95
   269
    }
vb@95
   270
}
vb@95
   271
vb@95
   272
static stringpair_map_t * _stringpair_map_dup(
vb@95
   273
        const stringpair_map_t *src,
vb@95
   274
        stringpair_map_t *parent
vb@95
   275
    )
vb@95
   276
{
vb@95
   277
    stringpair_map_t *map = NULL;   
vb@95
   278
vb@95
   279
    assert(src);
vb@95
   280
vb@95
   281
    map = new_stringpair_map(src->pair);
vb@95
   282
    if (map == NULL)
vb@95
   283
        goto enomem;
vb@95
   284
vb@95
   285
    map->color = src->color;
vb@95
   286
vb@95
   287
    if (src->left) {
vb@95
   288
        map->left = _stringpair_map_dup(src->left, map);
vb@95
   289
        if (map->left == NULL)
vb@95
   290
            goto enomem;
vb@95
   291
    }
vb@95
   292
vb@95
   293
    if (src->right) {
vb@95
   294
        map->right = _stringpair_map_dup(src->right, map);
vb@95
   295
        if (map->right == NULL)
vb@95
   296
            goto enomem;
vb@95
   297
    }
vb@95
   298
vb@95
   299
    map->parent_ref = parent;
vb@95
   300
vb@95
   301
    return map;
vb@95
   302
vb@95
   303
enomem:
vb@95
   304
    free_stringpair_map(map);
vb@95
   305
    return NULL;
vb@95
   306
}
vb@95
   307
vb@95
   308
DYNAMIC_API stringpair_map_t * stringpair_map_dup(const stringpair_map_t *src)
vb@95
   309
{
vb@95
   310
    return _stringpair_map_dup(src, NULL);
vb@95
   311
}
vb@95
   312
vb@95
   313
static bool stringpair_map_is_leave(const stringpair_map_t *node)
vb@95
   314
{
vb@95
   315
    assert(node);
vb@95
   316
    return node->left == NULL && node->right == NULL;
vb@95
   317
}
vb@95
   318
vb@95
   319
DYNAMIC_API stringpair_map_t * stringpair_map_find(
vb@95
   320
        stringpair_map_t *map,
vb@95
   321
        const char *key
vb@95
   322
    )
vb@95
   323
{
vb@95
   324
    int c;
vb@95
   325
vb@95
   326
    assert(key);
vb@95
   327
vb@95
   328
    if (map == NULL || map->pair == NULL) // empty map
vb@95
   329
        return NULL;
vb@95
   330
vb@95
   331
    c = strcmp(map->pair->key, key);
vb@95
   332
vb@95
   333
    if (c == 0)
vb@95
   334
        return map;
vb@95
   335
    else if (c < 0)
vb@95
   336
        return stringpair_map_find(map->left, key);
vb@95
   337
    else
vb@95
   338
        return stringpair_map_find(map->right, key);
vb@95
   339
}
vb@95
   340
vb@95
   341
static stringpair_map_t * stringpair_map_grandparent(stringpair_map_t *node)
vb@95
   342
{
vb@95
   343
    assert(node);
vb@95
   344
vb@95
   345
    if (node->parent_ref == NULL)
vb@95
   346
        return NULL;
vb@95
   347
vb@95
   348
    return node->parent_ref->parent_ref;
vb@95
   349
}
vb@95
   350
vb@95
   351
static stringpair_map_t * stringpair_map_uncle(stringpair_map_t *node)
vb@95
   352
{
vb@95
   353
    assert(stringpair_map_grandparent(node));
vb@95
   354
vb@95
   355
    if (node->parent_ref == stringpair_map_grandparent(node)->left)
vb@95
   356
        return stringpair_map_grandparent(node)->right;
vb@95
   357
    else
vb@95
   358
        return stringpair_map_grandparent(node)->left;
vb@95
   359
}
vb@95
   360
vb@95
   361
static stringpair_map_t * _stringpair_map_add(
vb@95
   362
        stringpair_map_t *map,
vb@95
   363
        stringpair_t * pair
vb@95
   364
    )
vb@95
   365
{
vb@95
   366
    int c;
vb@95
   367
vb@95
   368
    assert(map);
vb@95
   369
    assert(pair);
vb@95
   370
vb@95
   371
    if (map->pair == NULL) {
vb@95
   372
        map->pair = stringpair_dup(pair);
vb@95
   373
        if (map->pair == NULL)
vb@95
   374
            return NULL;
vb@95
   375
        return map;
vb@95
   376
    }
vb@95
   377
vb@95
   378
    assert(map->pair->key);
vb@95
   379
    assert(pair->key);
vb@95
   380
vb@95
   381
    c = strcmp(map->pair->key, pair->key);
vb@95
   382
    if (c == 0) {
vb@95
   383
        free(map->pair->value);
vb@95
   384
vb@95
   385
        assert(pair->value);
vb@95
   386
vb@95
   387
        map->pair->value = strdup(pair->value);
vb@95
   388
        assert(map->pair->value);
vb@95
   389
        if (map->pair->value == NULL)
vb@95
   390
            return NULL;
vb@95
   391
    }
vb@95
   392
    else if (c < 0) {
vb@95
   393
        if (map->left == NULL) {
vb@95
   394
            map->left = new_stringpair_map(pair);
vb@95
   395
            if (map->left)
vb@95
   396
                return NULL;
vb@95
   397
            map = map->left;
vb@95
   398
        }
vb@95
   399
        else {
vb@95
   400
            map = _stringpair_map_add(map->left, pair);
vb@95
   401
        }
vb@95
   402
    }
vb@95
   403
    else {
vb@95
   404
        if (map->right == NULL) {
vb@95
   405
            map->right = new_stringpair_map(pair);
vb@95
   406
            if (map->right)
vb@95
   407
                return NULL;
vb@95
   408
            map = map->right;
vb@95
   409
        }
vb@95
   410
        else {
vb@95
   411
            map = _stringpair_map_add(map->right, pair);
vb@95
   412
        }
vb@95
   413
    }
vb@95
   414
vb@95
   415
    return map;
vb@95
   416
}
vb@95
   417
vb@95
   418
static void stringpair_map_rotate_left(stringpair_map_t *l)
vb@95
   419
{
vb@95
   420
    stringpair_map_t * _parent;
vb@95
   421
    stringpair_map_t * _r;
vb@95
   422
vb@95
   423
    assert(l);
vb@95
   424
    assert(l->parent_ref);
vb@95
   425
    assert(l->right);
vb@95
   426
vb@95
   427
    _parent = l->parent_ref;
vb@95
   428
    _r = l->right;
vb@95
   429
vb@95
   430
    l->right = _r->left;
vb@95
   431
    _r->left = l;
vb@95
   432
vb@95
   433
    if (_parent->left == l)
vb@95
   434
        _parent->left = _r;
vb@95
   435
    else
vb@95
   436
        _parent->right = _r;
vb@95
   437
}
vb@95
   438
vb@95
   439
static void stringpair_map_rotate_right(stringpair_map_t *r)
vb@95
   440
{
vb@95
   441
    stringpair_map_t * _parent;
vb@95
   442
    stringpair_map_t * _l;
vb@95
   443
vb@95
   444
    assert(r);
vb@95
   445
    assert(r->parent_ref);
vb@95
   446
    assert(r->left);
vb@95
   447
vb@95
   448
    _parent = r->parent_ref;
vb@95
   449
    _l = r->left;
vb@95
   450
vb@95
   451
    r->left = _l->right;
vb@95
   452
    _l->right = r;
vb@95
   453
vb@95
   454
    if (_parent->left == r)
vb@95
   455
        _parent->left = _l;
vb@95
   456
    else
vb@95
   457
        _parent->right = _l;
vb@95
   458
}
vb@95
   459
vb@95
   460
static void stringpair_map_case5(stringpair_map_t *map)
vb@95
   461
{
vb@95
   462
    map->parent_ref->color = rbt_black;
vb@95
   463
    stringpair_map_grandparent(map)->color = rbt_red;
vb@95
   464
    if (map == map->parent_ref->left &&
vb@95
   465
            map->parent_ref == stringpair_map_grandparent(map)->left) {
vb@95
   466
        stringpair_map_rotate_right(stringpair_map_grandparent(map));
vb@95
   467
    }
vb@95
   468
    else {
vb@95
   469
        assert(map == map->parent_ref->right &&
vb@95
   470
                map->parent_ref == stringpair_map_grandparent(map)->right);
vb@95
   471
        stringpair_map_rotate_left(stringpair_map_grandparent(map));
vb@95
   472
    }
vb@95
   473
}
vb@95
   474
vb@95
   475
static void stringpair_map_case4(stringpair_map_t *map)
vb@95
   476
{
vb@95
   477
    if (map == map->parent_ref->right &&
vb@95
   478
            map->parent_ref == stringpair_map_grandparent(map)->left) {
vb@95
   479
        stringpair_map_rotate_left(map->parent_ref);
vb@95
   480
        map = map->left;
vb@95
   481
    }
vb@95
   482
    else if (map == map->parent_ref->left &&
vb@95
   483
            map->parent_ref == stringpair_map_grandparent(map)->right) {
vb@95
   484
        stringpair_map_rotate_right(map->parent_ref);
vb@95
   485
        map = map->right;
vb@95
   486
    }
vb@95
   487
vb@95
   488
    stringpair_map_case5(map);
vb@95
   489
}
vb@95
   490
vb@95
   491
static void stringpair_map_case1(stringpair_map_t *map);
vb@95
   492
vb@95
   493
static void stringpair_map_case3(stringpair_map_t *map)
vb@95
   494
{
vb@95
   495
    if (stringpair_map_uncle(map) != NULL &&
vb@95
   496
            stringpair_map_uncle(map)->color == rbt_red) {
vb@95
   497
        map->parent_ref->color = rbt_black;
vb@95
   498
        stringpair_map_uncle(map)->color = rbt_black;
vb@95
   499
        stringpair_map_grandparent(map)->color = rbt_red;
vb@95
   500
vb@95
   501
        stringpair_map_case1(stringpair_map_grandparent(map));
vb@95
   502
    }
vb@95
   503
    else {
vb@95
   504
        stringpair_map_case4(map);
vb@95
   505
    }
vb@95
   506
}
vb@95
   507
vb@95
   508
static void stringpair_map_case2(stringpair_map_t *map)
vb@95
   509
{
vb@95
   510
    if (map->parent_ref->color == rbt_black)
vb@95
   511
        return;
vb@95
   512
    else
vb@95
   513
        stringpair_map_case3(map);
vb@95
   514
}
vb@95
   515
vb@95
   516
static void stringpair_map_case1(stringpair_map_t *map)
vb@95
   517
{
vb@95
   518
    assert(map);
vb@95
   519
vb@95
   520
    if (map->parent_ref == NULL)
vb@95
   521
        map->color = rbt_black;
vb@95
   522
    else
vb@95
   523
        stringpair_map_case2(map);
vb@95
   524
}
vb@95
   525
vb@95
   526
static void stringpair_map_repair(stringpair_map_t *map)
vb@95
   527
{
vb@95
   528
    stringpair_map_case1(map);
vb@95
   529
}
vb@95
   530
vb@95
   531
DYNAMIC_API stringpair_map_t * stringpair_map_add(
vb@95
   532
        stringpair_map_t *map,
vb@95
   533
        stringpair_t * pair
vb@95
   534
    )
vb@95
   535
{
vb@95
   536
    stringpair_map_t * _map = NULL;
vb@95
   537
vb@95
   538
    assert(map);
vb@95
   539
    assert(pair);
vb@95
   540
vb@95
   541
    _map = _stringpair_map_add(map, pair);
vb@95
   542
    if (_map == NULL)
vb@95
   543
        return NULL;
vb@95
   544
vb@95
   545
    stringpair_map_repair(_map);
vb@95
   546
vb@95
   547
    return _map;
vb@95
   548
}
vb@95
   549
vb@48
   550
DYNAMIC_API message *new_message(
vb@38
   551
        PEP_msg_direction dir,
vb@39
   552
        pEp_identity *from,
vb@39
   553
        identity_list *to,
vb@29
   554
        const char *shortmsg
vb@29
   555
    )
vb@29
   556
{
vb@29
   557
    message *msg = calloc(1, sizeof(message));
vb@29
   558
    assert(msg);
vb@29
   559
    if (msg == NULL)
vb@29
   560
        return NULL;
vb@29
   561
vb@39
   562
    if (shortmsg) {
vb@37
   563
        msg->shortmsg = strdup(shortmsg);
vb@37
   564
        assert(msg->shortmsg);
vb@37
   565
        if (msg->shortmsg == NULL) {
vb@37
   566
            free(msg);
vb@37
   567
            return NULL;
vb@37
   568
        }
vb@29
   569
    }
vb@29
   570
vb@29
   571
    msg->dir = dir;
vb@39
   572
    msg->from = from;
vb@39
   573
    msg->to = to;
vb@29
   574
vb@29
   575
    return msg;
vb@29
   576
}
vb@29
   577
vb@48
   578
DYNAMIC_API void free_message(message *msg)
vb@29
   579
{
vb@63
   580
    if (msg) {
vb@63
   581
        free(msg->id);
vb@63
   582
        free(msg->shortmsg);
vb@63
   583
        free(msg->longmsg);
vb@63
   584
        free(msg->longmsg_formatted);
vb@63
   585
        free_bloblist(msg->attachments);
vb@89
   586
        free(msg->sent);
vb@89
   587
        free(msg->recv);
vb@63
   588
        free_identity(msg->from);
vb@63
   589
        free_identity_list(msg->to);
vb@63
   590
        free_identity(msg->recv_by);
vb@63
   591
        free_identity_list(msg->cc);
vb@63
   592
        free_identity_list(msg->bcc);
vb@94
   593
        free_identity_list(msg->reply_to);
vb@94
   594
        free_stringlist(msg->in_reply_to);
vb@89
   595
        free_stringlist(msg->references);
vb@89
   596
        free_stringlist(msg->keywords);
vb@89
   597
        free(msg->comments);
vb@63
   598
        free(msg);
vb@63
   599
    }
vb@29
   600
}
vb@29
   601
vb@81
   602
DYNAMIC_API message * message_dup(const message *src)
vb@81
   603
{
vb@81
   604
    message * msg = NULL;
vb@81
   605
    pEp_identity * from = NULL;
vb@81
   606
    identity_list * to = NULL;
vb@81
   607
vb@81
   608
    assert(src);
vb@81
   609
vb@81
   610
    from = identity_dup(src->from);
vb@81
   611
    if (from == NULL)
vb@81
   612
        goto enomem;
vb@81
   613
vb@81
   614
    to = identity_list_dup(src->to);
vb@81
   615
    if (to == NULL)
vb@81
   616
        goto enomem;
vb@81
   617
vb@81
   618
    msg = new_message(src->dir, from, to, src->shortmsg);
vb@81
   619
    if (msg == NULL)
vb@81
   620
        goto enomem;
vb@81
   621
vb@81
   622
    if (src->id) {
vb@81
   623
        msg->id = strdup(src->id);
vb@81
   624
        assert(msg->id);
vb@81
   625
        if (msg->id == NULL)
vb@81
   626
            goto enomem;
vb@81
   627
    }
vb@81
   628
vb@81
   629
    if (src->longmsg) {
vb@81
   630
        msg->longmsg = strdup(src->longmsg);
vb@81
   631
        assert(msg->longmsg);
vb@81
   632
        if (msg->longmsg == NULL)
vb@81
   633
            goto enomem;
vb@81
   634
    }
vb@81
   635
    
vb@81
   636
    if (src->longmsg_formatted) {
vb@81
   637
        msg->longmsg_formatted = strdup(src->longmsg_formatted);
vb@81
   638
        assert(msg->longmsg_formatted);
vb@81
   639
        if (msg->longmsg_formatted == NULL)
vb@81
   640
            goto enomem;
vb@81
   641
    }
vb@81
   642
vb@81
   643
    if (src->attachments) {
vb@81
   644
        msg->attachments = bloblist_dup(src->attachments);
vb@81
   645
        if (msg->attachments == NULL)
vb@81
   646
            goto enomem;
vb@81
   647
    }
vb@81
   648
vb@81
   649
    msg->rawmsg_ref = src->rawmsg_ref;
vb@81
   650
    msg->rawmsg_size = src->rawmsg_size;
vb@89
   651
vb@89
   652
    if (src->sent) {
vb@89
   653
        msg->sent = malloc(sizeof(timestamp));
vb@89
   654
        if (msg->sent == NULL)
vb@89
   655
            goto enomem;
vb@89
   656
        memcpy(msg->sent, src->sent, sizeof(timestamp));
vb@89
   657
    }
vb@89
   658
vb@89
   659
    if (src->recv) {
vb@89
   660
        msg->recv = malloc(sizeof(timestamp));
vb@89
   661
        if (msg->recv == NULL)
vb@89
   662
            goto enomem;
vb@89
   663
        memcpy(msg->recv, src->recv, sizeof(timestamp));
vb@89
   664
    }
vb@81
   665
vb@81
   666
    if (src->recv_by) {
vb@81
   667
        msg->recv_by = identity_dup(src->recv_by);
vb@81
   668
        if (msg->recv_by == NULL)
vb@81
   669
            goto enomem;
vb@81
   670
    }
vb@81
   671
vb@81
   672
    if (src->cc) {
vb@81
   673
        msg->cc = identity_list_dup(src->cc);
vb@81
   674
        if (msg->cc == NULL)
vb@81
   675
            goto enomem;
vb@81
   676
    }
vb@81
   677
vb@81
   678
    if (src->bcc) {
vb@81
   679
        msg->bcc = identity_list_dup(src->bcc);
vb@81
   680
        if (msg->bcc == NULL)
vb@81
   681
            goto enomem;
vb@81
   682
    }
vb@81
   683
vb@81
   684
    if (src->reply_to) {
vb@94
   685
        msg->reply_to = identity_list_dup(src->reply_to);
vb@81
   686
        if (msg->reply_to == NULL)
vb@81
   687
            goto enomem;
vb@81
   688
    }
vb@81
   689
vb@89
   690
    if (src->in_reply_to) {
vb@94
   691
        msg->in_reply_to = stringlist_dup(src->in_reply_to);
vb@89
   692
        assert(msg->in_reply_to);
vb@89
   693
        if (msg->in_reply_to == NULL)
vb@81
   694
            goto enomem;
vb@81
   695
    }
vb@81
   696
vb@81
   697
    msg->refering_msg_ref = src->refering_msg_ref;
vb@81
   698
    
vb@89
   699
    if (src->references) {
vb@89
   700
        msg->references = stringlist_dup(src->references);
vb@89
   701
        if (msg->references == NULL)
vb@89
   702
            goto enomem;
vb@89
   703
    }
vb@89
   704
vb@81
   705
    if (src->refered_by) {
vb@81
   706
        msg->refered_by = message_ref_list_dup(src->refered_by);
vb@81
   707
        if (msg->refered_by == NULL)
vb@81
   708
            goto enomem;
vb@81
   709
    }
vb@81
   710
vb@89
   711
    if (src->keywords) {
vb@89
   712
        msg->keywords = stringlist_dup(src->keywords);
vb@89
   713
        if (msg->keywords == NULL)
vb@89
   714
            goto enomem;
vb@89
   715
    }
vb@89
   716
vb@89
   717
    if (src->comments) {
vb@89
   718
        msg->comments = strdup(src->comments);
vb@89
   719
        assert(msg->comments);
vb@89
   720
        if (msg->comments == NULL)
vb@89
   721
            goto enomem;
vb@89
   722
    }
vb@89
   723
vb@81
   724
    msg->enc_format = src->enc_format;
vb@81
   725
vb@81
   726
    return msg;
vb@81
   727
vb@81
   728
enomem:
vb@81
   729
    if (msg) {
vb@81
   730
        free_message(msg);
vb@81
   731
    }
vb@81
   732
    else {
vb@81
   733
        free_identity(from);
vb@81
   734
        free_identity_list(to);
vb@81
   735
    }
vb@81
   736
vb@81
   737
    return NULL;
vb@81
   738
}
vb@81
   739
vb@48
   740
DYNAMIC_API message_ref_list *new_message_ref_list(message *msg)
vb@29
   741
{
vb@29
   742
    message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
vb@29
   743
    assert(msg_list);
vb@29
   744
    if (msg_list == NULL)
vb@29
   745
        return NULL;
vb@29
   746
vb@29
   747
    msg_list->msg_ref = msg;
vb@29
   748
vb@29
   749
    return msg_list;
vb@29
   750
}
vb@29
   751
vb@48
   752
DYNAMIC_API void free_message_ref_list(message_ref_list *msg_list)
vb@29
   753
{
vb@29
   754
    if (msg_list) {
vb@29
   755
        free_message_ref_list(msg_list->next);
vb@29
   756
        free(msg_list);
vb@29
   757
    }
vb@29
   758
}
vb@29
   759
vb@81
   760
DYNAMIC_API message_ref_list *message_ref_list_dup(
vb@81
   761
        const message_ref_list *src
vb@81
   762
    )
vb@81
   763
{
vb@81
   764
    message_ref_list * msg_list = NULL;
vb@81
   765
vb@81
   766
    assert(src);
vb@81
   767
vb@81
   768
    msg_list = new_message_ref_list(src->msg_ref);
vb@81
   769
    if (msg_list == NULL)
vb@81
   770
        goto enomem;
vb@81
   771
vb@81
   772
    if (src->next) {
vb@81
   773
        msg_list->next = message_ref_list_dup(src->next);
vb@81
   774
        if (msg_list->next == NULL)
vb@81
   775
            goto enomem;
vb@81
   776
    }
vb@81
   777
vb@81
   778
    return msg_list;
vb@81
   779
vb@81
   780
enomem:
vb@81
   781
    free_message_ref_list(msg_list);
vb@81
   782
    return NULL;
vb@81
   783
}
vb@81
   784
vb@48
   785
DYNAMIC_API message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
vb@29
   786
{
vb@29
   787
    assert(msg);
vb@29
   788
vb@38
   789
    if (msg_list == NULL)
vb@38
   790
        return new_message_ref_list(msg);
vb@38
   791
vb@29
   792
    if (msg_list->msg_ref == NULL) {
vb@29
   793
        msg_list->msg_ref = msg;
vb@29
   794
        return msg_list;
vb@29
   795
    }
vb@29
   796
    else if (msg_list->next == NULL) {
vb@29
   797
        msg_list->next = new_message_ref_list(msg);
vb@29
   798
        assert(msg_list->next);
vb@29
   799
        return msg_list->next;
vb@29
   800
    }
vb@29
   801
    else {
vb@29
   802
        return message_ref_list_add(msg_list->next, msg);
vb@29
   803
    }
vb@29
   804
}
vb@29
   805