VMM: vmresume works! (handle IRQ)
[akaros.git] / kern / lib / zlib_inflate / infutil.c
1 #include <zutil.h>
2 #include <error.h>
3 #include <slab.h>
4 #include <mm.h>
5 #include <kmalloc.h>
6
7 /* Utility function: initialize zlib, unpack binary blob, clean up zlib,
8  * return len or negative error code.
9  */
10 int zlib_inflate_blob(void *gunzip_buf, unsigned int sz,
11                       const void *buf, unsigned int len)
12 {
13         const uint8_t *zbuf = buf;
14         struct z_stream_s *strm;
15         int rc;
16
17         rc = -ENOMEM;
18         strm = kmalloc(sizeof(*strm), KMALLOC_WAIT);
19         if (strm == NULL)
20                 goto gunzip_nomem1;
21         strm->workspace = kmalloc(zlib_inflate_workspacesize(), KMALLOC_WAIT);
22         if (strm->workspace == NULL)
23                 goto gunzip_nomem2;
24
25         /* gzip header (1f,8b,08... 10 bytes total + possible asciz filename)
26          * expected to be stripped from input
27          */
28         strm->next_in = zbuf;
29         strm->avail_in = len;
30         strm->next_out = gunzip_buf;
31         strm->avail_out = sz;
32
33         rc = zlib_inflateInit2(strm, -MAX_WBITS);
34         if (rc == Z_OK) {
35                 rc = zlib_inflate(strm, Z_FINISH);
36                 /* after Z_FINISH, only Z_STREAM_END is "we unpacked it all" */
37                 if (rc == Z_STREAM_END)
38                         rc = sz - strm->avail_out;
39                 else
40                         rc = -EINVAL;
41                 zlib_inflateEnd(strm);
42         } else
43                 rc = -EINVAL;
44
45         kfree(strm->workspace);
46 gunzip_nomem2:
47         kfree(strm);
48 gunzip_nomem1:
49         return rc; /* returns Z_OK (0) if successful */
50 }