uncompress.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. static z_stream stream;
  27. void *zalloc(void *, unsigned, unsigned);
  28. void zfree(void *, void *, unsigned);
  29. /* Returns length of decompressed data. */
  30. int cramfs_uncompress_block (void *dst, void *src, int srclen)
  31. {
  32. int err;
  33. inflateReset (&stream);
  34. stream.next_in = src;
  35. stream.avail_in = srclen;
  36. stream.next_out = dst;
  37. stream.avail_out = 4096 * 2;
  38. err = inflate (&stream, Z_FINISH);
  39. if (err != Z_STREAM_END)
  40. goto err;
  41. return stream.total_out;
  42. err:
  43. /*printf ("Error %d while decompressing!\n", err); */
  44. /*printf ("%p(%d)->%p\n", src, srclen, dst); */
  45. return -1;
  46. }
  47. int cramfs_uncompress_init (void)
  48. {
  49. int err;
  50. stream.zalloc = zalloc;
  51. stream.zfree = zfree;
  52. stream.next_in = 0;
  53. stream.avail_in = 0;
  54. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  55. stream.outcb = (cb_func) WATCHDOG_RESET;
  56. #else
  57. stream.outcb = Z_NULL;
  58. #endif /* CONFIG_HW_WATCHDOG */
  59. err = inflateInit (&stream);
  60. if (err != Z_OK) {
  61. printf ("Error: inflateInit2() returned %d\n", err);
  62. return -1;
  63. }
  64. return 0;
  65. }
  66. int cramfs_uncompress_exit (void)
  67. {
  68. inflateEnd (&stream);
  69. return 0;
  70. }