zlib_wrapper.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/slab.h>
  26. #include <linux/zlib.h>
  27. #include "squashfs_fs.h"
  28. #include "squashfs_fs_sb.h"
  29. #include "squashfs_fs_i.h"
  30. #include "squashfs.h"
  31. #include "decompressor.h"
  32. static void *zlib_init(struct squashfs_sb_info *dummy)
  33. {
  34. z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL);
  35. if (stream == NULL)
  36. goto failed;
  37. stream->workspace = kmalloc(zlib_inflate_workspacesize(),
  38. GFP_KERNEL);
  39. if (stream->workspace == NULL)
  40. goto failed;
  41. return stream;
  42. failed:
  43. ERROR("Failed to allocate zlib workspace\n");
  44. kfree(stream);
  45. return NULL;
  46. }
  47. static void zlib_free(void *strm)
  48. {
  49. z_stream *stream = strm;
  50. if (stream)
  51. kfree(stream->workspace);
  52. kfree(stream);
  53. }
  54. static int zlib_uncompress(struct squashfs_sb_info *msblk, void **buffer,
  55. struct buffer_head **bh, int b, int offset, int length, int srclength,
  56. int pages)
  57. {
  58. int zlib_err = 0, zlib_init = 0;
  59. int avail, bytes, k = 0, page = 0;
  60. z_stream *stream = msblk->stream;
  61. mutex_lock(&msblk->read_data_mutex);
  62. stream->avail_out = 0;
  63. stream->avail_in = 0;
  64. bytes = length;
  65. do {
  66. if (stream->avail_in == 0 && k < b) {
  67. avail = min(bytes, msblk->devblksize - offset);
  68. bytes -= avail;
  69. wait_on_buffer(bh[k]);
  70. if (!buffer_uptodate(bh[k]))
  71. goto release_mutex;
  72. if (avail == 0) {
  73. offset = 0;
  74. put_bh(bh[k++]);
  75. continue;
  76. }
  77. stream->next_in = bh[k]->b_data + offset;
  78. stream->avail_in = avail;
  79. offset = 0;
  80. }
  81. if (stream->avail_out == 0 && page < pages) {
  82. stream->next_out = buffer[page++];
  83. stream->avail_out = PAGE_CACHE_SIZE;
  84. }
  85. if (!zlib_init) {
  86. zlib_err = zlib_inflateInit(stream);
  87. if (zlib_err != Z_OK) {
  88. ERROR("zlib_inflateInit returned unexpected "
  89. "result 0x%x, srclength %d\n",
  90. zlib_err, srclength);
  91. goto release_mutex;
  92. }
  93. zlib_init = 1;
  94. }
  95. zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
  96. if (stream->avail_in == 0 && k < b)
  97. put_bh(bh[k++]);
  98. } while (zlib_err == Z_OK);
  99. if (zlib_err != Z_STREAM_END) {
  100. ERROR("zlib_inflate error, data probably corrupt\n");
  101. goto release_mutex;
  102. }
  103. zlib_err = zlib_inflateEnd(stream);
  104. if (zlib_err != Z_OK) {
  105. ERROR("zlib_inflate error, data probably corrupt\n");
  106. goto release_mutex;
  107. }
  108. length = stream->total_out;
  109. mutex_unlock(&msblk->read_data_mutex);
  110. return length;
  111. release_mutex:
  112. mutex_unlock(&msblk->read_data_mutex);
  113. for (; k < b; k++)
  114. put_bh(bh[k]);
  115. return -EIO;
  116. }
  117. const struct squashfs_decompressor squashfs_zlib_comp_ops = {
  118. .init = zlib_init,
  119. .free = zlib_free,
  120. .decompress = zlib_uncompress,
  121. .id = ZLIB_COMPRESSION,
  122. .name = "zlib",
  123. .supported = 1
  124. };