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