uncompress.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * uncompress.c
  3. *
  4. * Copyright (C) 1999 Linus Torvalds
  5. * Copyright (C) 2000-2002 Transmeta Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License (Version 2) as
  9. * published by the Free Software Foundation.
  10. *
  11. * cramfs interfaces to the uncompression library. There's really just
  12. * three entrypoints:
  13. *
  14. * - cramfs_uncompress_init() - called to initialize the thing.
  15. * - cramfs_uncompress_exit() - tell me when you're done
  16. * - cramfs_uncompress_block() - uncompress a block.
  17. *
  18. * NOTE NOTE NOTE! The uncompression is entirely single-threaded. We
  19. * only have one stream, and we'll initialize it only once even if it
  20. * then is used by multiple filesystems.
  21. */
  22. #include <common.h>
  23. #include <malloc.h>
  24. #include <watchdog.h>
  25. #include <zlib.h>
  26. #if defined(CONFIG_CMD_JFFS2)
  27. static z_stream stream;
  28. void *zalloc(void *, unsigned, unsigned);
  29. void zfree(void *, void *, unsigned);
  30. /* Returns length of decompressed data. */
  31. int cramfs_uncompress_block (void *dst, void *src, int srclen)
  32. {
  33. int err;
  34. inflateReset (&stream);
  35. stream.next_in = src;
  36. stream.avail_in = srclen;
  37. stream.next_out = dst;
  38. stream.avail_out = 4096 * 2;
  39. err = inflate (&stream, Z_FINISH);
  40. if (err != Z_STREAM_END)
  41. goto err;
  42. return stream.total_out;
  43. err:
  44. /*printf ("Error %d while decompressing!\n", err); */
  45. /*printf ("%p(%d)->%p\n", src, srclen, dst); */
  46. return -1;
  47. }
  48. int cramfs_uncompress_init (void)
  49. {
  50. int err;
  51. stream.zalloc = zalloc;
  52. stream.zfree = zfree;
  53. stream.next_in = 0;
  54. stream.avail_in = 0;
  55. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  56. stream.outcb = (cb_func) WATCHDOG_RESET;
  57. #else
  58. stream.outcb = Z_NULL;
  59. #endif /* CONFIG_HW_WATCHDOG */
  60. err = inflateInit (&stream);
  61. if (err != Z_OK) {
  62. printf ("Error: inflateInit2() returned %d\n", err);
  63. return -1;
  64. }
  65. return 0;
  66. }
  67. int cramfs_uncompress_exit (void)
  68. {
  69. inflateEnd (&stream);
  70. return 0;
  71. }
  72. #endif /* CFG_FS_CRAMFS */