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