aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libxc/xc_dom_decompress_unsafe.c
blob: 164e35558f0dd8374f5f7d80507ac485c0284b57 (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
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

#include "xg_private.h"
#include "xc_dom_decompress_unsafe.h"

static struct xc_dom_image *unsafe_dom;
static unsigned char *output_blob;
static unsigned int output_size;

static void unsafe_error(const char *msg)
{
    xc_dom_panic(unsafe_dom->xch, XC_INVALID_KERNEL, "%s", msg);
}

static int unsafe_flush(void *src, unsigned int size)
{
    void *n = realloc(output_blob, output_size + size);
    if (!n)
        return -1;
    output_blob = n;

    memcpy(&output_blob[output_size], src, size);
    output_size += size;
    return size;
}

int xc_dom_decompress_unsafe(
    decompress_fn fn, struct xc_dom_image *dom, void **blob, size_t *size)
{
    int ret;

    unsafe_dom = dom;
    output_blob = NULL;
    output_size = 0;

    ret = fn(dom->kernel_blob, dom->kernel_size, NULL, unsafe_flush, NULL, NULL, unsafe_error);

    if (ret)
        free(output_blob);
    else {
        *blob = output_blob;
        *size = output_size;
    }

    return ret;
}