1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <inttypes.h>
4 
5 #include "xg_private.h"
6 #include "xc_dom_decompress_unsafe.h"
7 
8 static struct xc_dom_image *unsafe_dom;
9 static unsigned char *output_blob;
10 static unsigned int output_size;
11 
unsafe_error(const char * msg)12 static void unsafe_error(const char *msg)
13 {
14     xc_dom_panic(unsafe_dom->xch, XC_INVALID_KERNEL, "%s", msg);
15 }
16 
unsafe_flush(void * src,unsigned int size)17 static int unsafe_flush(void *src, unsigned int size)
18 {
19     void *n = realloc(output_blob, output_size + size);
20     if (!n)
21         return -1;
22     output_blob = n;
23 
24     memcpy(&output_blob[output_size], src, size);
25     output_size += size;
26     return size;
27 }
28 
xc_dom_decompress_unsafe(decompress_fn fn,struct xc_dom_image * dom,void ** blob,size_t * size)29 int xc_dom_decompress_unsafe(
30     decompress_fn fn, struct xc_dom_image *dom, void **blob, size_t *size)
31 {
32     int ret;
33 
34     unsafe_dom = dom;
35     output_blob = NULL;
36     output_size = 0;
37 
38     ret = fn(dom->kernel_blob, dom->kernel_size, NULL, unsafe_flush, NULL, NULL, unsafe_error);
39 
40     if (ret)
41         free(output_blob);
42     else {
43         *blob = output_blob;
44         *size = output_size;
45     }
46 
47     return ret;
48 }
49