zlib_wrapper.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Squashfs - a compressed read only filesystem for Linux
  3. *
  4. * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
  5. * Phillip Lougher <phillip@lougher.demon.co.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. *
  21. * zlib_wrapper.c
  22. */
  23. #include <linux/mutex.h>
  24. #include <linux/buffer_head.h>
  25. #include <linux/zlib.h>
  26. #include "squashfs_fs.h"
  27. #include "squashfs_fs_sb.h"
  28. #include "squashfs_fs_i.h"
  29. #include "squashfs.h"
  30. int squashfs_zlib_uncompress(struct squashfs_sb_info *msblk, void **buffer,
  31. struct buffer_head **bh, int b, int offset, int length, int srclength,
  32. int pages)
  33. {
  34. int zlib_err = 0, zlib_init = 0;
  35. int avail, bytes, k = 0, page = 0;
  36. mutex_lock(&msblk->read_data_mutex);
  37. msblk->stream.avail_out = 0;
  38. msblk->stream.avail_in = 0;
  39. bytes = length;
  40. do {
  41. if (msblk->stream.avail_in == 0 && k < b) {
  42. avail = min(bytes, msblk->devblksize - offset);
  43. bytes -= avail;
  44. wait_on_buffer(bh[k]);
  45. if (!buffer_uptodate(bh[k]))
  46. goto release_mutex;
  47. if (avail == 0) {
  48. offset = 0;
  49. put_bh(bh[k++]);
  50. continue;
  51. }
  52. msblk->stream.next_in = bh[k]->b_data + offset;
  53. msblk->stream.avail_in = avail;
  54. offset = 0;
  55. }
  56. if (msblk->stream.avail_out == 0 && page < pages) {
  57. msblk->stream.next_out = buffer[page++];
  58. msblk->stream.avail_out = PAGE_CACHE_SIZE;
  59. }
  60. if (!zlib_init) {
  61. zlib_err = zlib_inflateInit(&msblk->stream);
  62. if (zlib_err != Z_OK) {
  63. ERROR("zlib_inflateInit returned unexpected "
  64. "result 0x%x, srclength %d\n",
  65. zlib_err, srclength);
  66. goto release_mutex;
  67. }
  68. zlib_init = 1;
  69. }
  70. zlib_err = zlib_inflate(&msblk->stream, Z_SYNC_FLUSH);
  71. if (msblk->stream.avail_in == 0 && k < b)
  72. put_bh(bh[k++]);
  73. } while (zlib_err == Z_OK);
  74. if (zlib_err != Z_STREAM_END) {
  75. ERROR("zlib_inflate error, data probably corrupt\n");
  76. goto release_mutex;
  77. }
  78. zlib_err = zlib_inflateEnd(&msblk->stream);
  79. if (zlib_err != Z_OK) {
  80. ERROR("zlib_inflate error, data probably corrupt\n");
  81. goto release_mutex;
  82. }
  83. mutex_unlock(&msblk->read_data_mutex);
  84. return msblk->stream.total_out;
  85. release_mutex:
  86. mutex_unlock(&msblk->read_data_mutex);
  87. for (; k < b; k++)
  88. put_bh(bh[k]);
  89. return -EIO;
  90. }