zlib_wrapper.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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@squashfs.org.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 <linux/vmalloc.h>
  28. #include "squashfs_fs.h"
  29. #include "squashfs_fs_sb.h"
  30. #include "squashfs.h"
  31. #include "decompressor.h"
  32. static void *zlib_init(struct squashfs_sb_info *dummy, void *buff, int len)
  33. {
  34. z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL);
  35. if (stream == NULL)
  36. goto failed;
  37. stream->workspace = vmalloc(zlib_inflate_workspacesize());
  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 ERR_PTR(-ENOMEM);
  45. }
  46. static void zlib_free(void *strm)
  47. {
  48. z_stream *stream = strm;
  49. if (stream)
  50. vfree(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. stream->next_in = bh[k]->b_data + offset;
  71. stream->avail_in = avail;
  72. offset = 0;
  73. }
  74. if (stream->avail_out == 0 && page < pages) {
  75. stream->next_out = buffer[page++];
  76. stream->avail_out = PAGE_CACHE_SIZE;
  77. }
  78. if (!zlib_init) {
  79. zlib_err = zlib_inflateInit(stream);
  80. if (zlib_err != Z_OK) {
  81. ERROR("zlib_inflateInit returned unexpected "
  82. "result 0x%x, srclength %d\n",
  83. zlib_err, srclength);
  84. goto release_mutex;
  85. }
  86. zlib_init = 1;
  87. }
  88. zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
  89. if (stream->avail_in == 0 && k < b)
  90. put_bh(bh[k++]);
  91. } while (zlib_err == Z_OK);
  92. if (zlib_err != Z_STREAM_END) {
  93. ERROR("zlib_inflate error, data probably corrupt\n");
  94. goto release_mutex;
  95. }
  96. zlib_err = zlib_inflateEnd(stream);
  97. if (zlib_err != Z_OK) {
  98. ERROR("zlib_inflate error, data probably corrupt\n");
  99. goto release_mutex;
  100. }
  101. if (k < b) {
  102. ERROR("zlib_uncompress error, data remaining\n");
  103. goto release_mutex;
  104. }
  105. length = stream->total_out;
  106. mutex_unlock(&msblk->read_data_mutex);
  107. return length;
  108. release_mutex:
  109. mutex_unlock(&msblk->read_data_mutex);
  110. for (; k < b; k++)
  111. put_bh(bh[k]);
  112. return -EIO;
  113. }
  114. const struct squashfs_decompressor squashfs_zlib_comp_ops = {
  115. .init = zlib_init,
  116. .free = zlib_free,
  117. .decompress = zlib_uncompress,
  118. .id = ZLIB_COMPRESSION,
  119. .name = "zlib",
  120. .supported = 1
  121. };