src/asn1_helper.c
author Volker Birk <vb@pep.foundation>
Thu, 01 Sep 2016 22:47:08 +0200
branchkeysync
changeset 1116 c9a03f2423c1
child 1137 f5990ddf49f4
permissions -rw-r--r--
decode_sync_msg() for debugging
vb@1116
     1
#include "asn1_helper.h"
vb@1116
     2
#include <assert.h>
vb@1116
     3
#include <stdlib.h>
vb@1116
     4
vb@1116
     5
growing_buf_t *new_growing_buf(void)
vb@1116
     6
{
vb@1116
     7
    growing_buf_t *result = calloc(1, sizeof(growing_buf_t));
vb@1116
     8
    assert(result);
vb@1116
     9
    return result;
vb@1116
    10
}
vb@1116
    11
vb@1116
    12
void free_growing_buf(growing_buf_t *buf)
vb@1116
    13
{
vb@1116
    14
    if (buf) {
vb@1116
    15
        free(buf->data);
vb@1116
    16
        free(buf);
vb@1116
    17
    }
vb@1116
    18
}
vb@1116
    19
vb@1116
    20
int consume_bytes(const void *src, size_t size, growing_buf_t *dst)
vb@1116
    21
{
vb@1116
    22
    assert(src && dst);
vb@1116
    23
    if (!(src && dst))
vb@1116
    24
        return -1;
vb@1116
    25
vb@1116
    26
    char *new_data = realloc(dst->data, dst->size + size + 1);
vb@1116
    27
    if (!new_data)
vb@1116
    28
        return -1;
vb@1116
    29
    dst->data = new_data;
vb@1116
    30
    memcpy(dst->data + dst->size, src, size);
vb@1116
    31
    dst->size += size;
vb@1116
    32
    dst->data[dst->size] = 0; // safeguard
vb@1116
    33
vb@1116
    34
    return 1;
vb@1116
    35
}
vb@1116
    36