summaryrefslogtreecommitdiff
blob: 788080f4af749e09dc37c13a94eac4b4cf47f936 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "../include/extract_alloc.h"

#include "mem.h"

#include <assert.h>
#include <stdio.h>
#include <string.h>

#ifdef _MSC_VER
    #include "compat_va_copy.h"
#endif


void extract_bzero(void *b, size_t len)
{
    memset(b, 0, len);
}

int extract_vasprintf(extract_alloc_t* alloc, char** out, const char* format, va_list va)
{
    int n;
    int ret;
    va_list va2;
    va_copy(va2, va);
    n = vsnprintf(NULL, 0, format, va);
    if (n < 0)
    {
        ret = n;
        goto end;
    }
    if (extract_malloc(alloc, out, n + 1))
    {
        ret = -1;
        goto end;
    }
    vsnprintf(*out, n + 1, format, va2);
    ret = 0;

    end:
    va_end(va2);
    return ret;
}


int extract_asprintf(extract_alloc_t* alloc, char** out, const char* format, ...)
{
    va_list va;
    int     ret;
    va_start(va, format);
    ret = extract_vasprintf(alloc, out, format, va);
    va_end(va);
    return ret;
}

int extract_strdup(extract_alloc_t* alloc, const char* s, char** o_out)
{
    size_t l = strlen(s) + 1;
    if (extract_malloc(alloc, o_out, l)) return -1;
    memcpy(*o_out, s, l);
    return 0;
}