uncompress.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * uncompress.c
  3. *
  4. * (C) Copyright 1999 Linus Torvalds
  5. *
  6. * cramfs interfaces to the uncompression library. There's really just
  7. * three entrypoints:
  8. *
  9. * - cramfs_uncompress_init() - called to initialize the thing.
  10. * - cramfs_uncompress_exit() - tell me when you're done
  11. * - cramfs_uncompress_block() - uncompress a block.
  12. *
  13. * NOTE NOTE NOTE! The uncompression is entirely single-threaded. We
  14. * only have one stream, and we'll initialize it only once even if it
  15. * then is used by multiple filesystems.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/zlib.h>
  22. #include "internal.h"
  23. static z_stream stream;
  24. static int initialized;
  25. /* Returns length of decompressed data. */
  26. int cramfs_uncompress_block(void *dst, int dstlen, void *src, int srclen)
  27. {
  28. int err;
  29. stream.next_in = src;
  30. stream.avail_in = srclen;
  31. stream.next_out = dst;
  32. stream.avail_out = dstlen;
  33. err = zlib_inflateReset(&stream);
  34. if (err != Z_OK) {
  35. pr_err("zlib_inflateReset error %d\n", err);
  36. zlib_inflateEnd(&stream);
  37. zlib_inflateInit(&stream);
  38. }
  39. err = zlib_inflate(&stream, Z_FINISH);
  40. if (err != Z_STREAM_END)
  41. goto err;
  42. return stream.total_out;
  43. err:
  44. pr_err("Error %d while decompressing!\n", err);
  45. pr_err("%p(%d)->%p(%d)\n", src, srclen, dst, dstlen);
  46. return -EIO;
  47. }
  48. int cramfs_uncompress_init(void)
  49. {
  50. if (!initialized++) {
  51. stream.workspace = vmalloc(zlib_inflate_workspacesize());
  52. if (!stream.workspace) {
  53. initialized = 0;
  54. return -ENOMEM;
  55. }
  56. stream.next_in = NULL;
  57. stream.avail_in = 0;
  58. zlib_inflateInit(&stream);
  59. }
  60. return 0;
  61. }
  62. void cramfs_uncompress_exit(void)
  63. {
  64. if (!--initialized) {
  65. zlib_inflateEnd(&stream);
  66. vfree(stream.workspace);
  67. }
  68. }