infutil.c 1.2 KB

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