uncompress.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #include <linux/kernel.h>
  18. #include <linux/errno.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/zlib.h>
  21. static z_stream stream;
  22. static int initialized;
  23. /* Returns length of decompressed data. */
  24. int cramfs_uncompress_block(void *dst, int dstlen, void *src, int srclen)
  25. {
  26. int err;
  27. stream.next_in = src;
  28. stream.avail_in = srclen;
  29. stream.next_out = dst;
  30. stream.avail_out = dstlen;
  31. err = zlib_inflateReset(&stream);
  32. if (err != Z_OK) {
  33. printk("zlib_inflateReset error %d\n", err);
  34. zlib_inflateEnd(&stream);
  35. zlib_inflateInit(&stream);
  36. }
  37. err = zlib_inflate(&stream, Z_FINISH);
  38. if (err != Z_STREAM_END)
  39. goto err;
  40. return stream.total_out;
  41. err:
  42. printk("Error %d while decompressing!\n", err);
  43. printk("%p(%d)->%p(%d)\n", src, srclen, dst, dstlen);
  44. return 0;
  45. }
  46. int cramfs_uncompress_init(void)
  47. {
  48. if (!initialized++) {
  49. stream.workspace = vmalloc(zlib_inflate_workspacesize());
  50. if ( !stream.workspace ) {
  51. initialized = 0;
  52. return -ENOMEM;
  53. }
  54. stream.next_in = NULL;
  55. stream.avail_in = 0;
  56. zlib_inflateInit(&stream);
  57. }
  58. return 0;
  59. }
  60. int cramfs_uncompress_exit(void)
  61. {
  62. if (!--initialized) {
  63. zlib_inflateEnd(&stream);
  64. vfree(stream.workspace);
  65. }
  66. return 0;
  67. }