zlib_wrapper.c 3.5 KB

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