src/growing_buf.c
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Thu, 04 Jun 2020 11:18:45 +0200
changeset 4729 3df9a2a67597
parent 2840 b881c48c9e9d
child 4792 7056435ab9e7
permissions -rw-r--r--
forgot test files
vb@2840
     1
// This file is under GNU General Public License 3.0
vb@2840
     2
// see LICENSE.txt
vb@2840
     3
vb@2840
     4
#include "pEp_internal.h"
vb@2840
     5
#include "growing_buf.h"
vb@2840
     6
vb@2840
     7
growing_buf_t *new_growing_buf(void)
vb@2840
     8
{
vb@2840
     9
    growing_buf_t *result = calloc(1, sizeof(growing_buf_t));
vb@2840
    10
    assert(result);
vb@2840
    11
    return result;
vb@2840
    12
}
vb@2840
    13
vb@2840
    14
void free_growing_buf(growing_buf_t *buf)
vb@2840
    15
{
vb@2840
    16
    if (buf) {
vb@2840
    17
        free(buf->data);
vb@2840
    18
        free(buf);
vb@2840
    19
    }
vb@2840
    20
}
vb@2840
    21
vb@2840
    22
int growing_buf_consume(const void *src, size_t size, growing_buf_t *dst)
vb@2840
    23
{
vb@2840
    24
    assert(src && dst);
vb@2840
    25
    if (!(src && dst))
vb@2840
    26
        return -1;
vb@2840
    27
vb@2840
    28
    char *new_data = realloc(dst->data, dst->size + size + 1);
vb@2840
    29
    assert(new_data);
vb@2840
    30
    if (!new_data)
vb@2840
    31
        return -1;
vb@2840
    32
    dst->data = new_data;
vb@2840
    33
    memcpy(dst->data + dst->size, src, size);
vb@2840
    34
    dst->size += size;
vb@2840
    35
    dst->data[dst->size] = 0; // safeguard
vb@2840
    36
vb@2840
    37
    return 1;
vb@2840
    38
}
vb@2840
    39