uncompress.c 2.2 KB

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