uncompress.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:2k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  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_fs.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_fs_inflateReset(&stream);
  32. if (err != Z_OK) {
  33. printk("zlib_fs_inflateReset error %dn", err);
  34. zlib_fs_inflateEnd(&stream);
  35. zlib_fs_inflateInit(&stream);
  36. }
  37. err = zlib_fs_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_fs_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_fs_inflateInit(&stream);
  57. }
  58. return 0;
  59. }
  60. int cramfs_uncompress_exit(void)
  61. {
  62. if (!--initialized) {
  63. zlib_fs_inflateEnd(&stream);
  64. vfree(stream.workspace);
  65. }
  66. return 0;
  67. }