zlib_wrapper.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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)
  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 *strm,
  54. void **buffer, struct buffer_head **bh, int b, int offset, int length,
  55. int srclength, int pages)
  56. {
  57. int zlib_err, zlib_init = 0;
  58. int k = 0, page = 0;
  59. z_stream *stream = strm;
  60. stream->avail_out = 0;
  61. stream->avail_in = 0;
  62. do {
  63. if (stream->avail_in == 0 && k < b) {
  64. int avail = min(length, msblk->devblksize - offset);
  65. length -= avail;
  66. stream->next_in = bh[k]->b_data + offset;
  67. stream->avail_in = avail;
  68. offset = 0;
  69. }
  70. if (stream->avail_out == 0 && page < pages) {
  71. stream->next_out = buffer[page++];
  72. stream->avail_out = PAGE_CACHE_SIZE;
  73. }
  74. if (!zlib_init) {
  75. zlib_err = zlib_inflateInit(stream);
  76. if (zlib_err != Z_OK)
  77. goto out;
  78. zlib_init = 1;
  79. }
  80. zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
  81. if (stream->avail_in == 0 && k < b)
  82. put_bh(bh[k++]);
  83. } while (zlib_err == Z_OK);
  84. if (zlib_err != Z_STREAM_END)
  85. goto out;
  86. zlib_err = zlib_inflateEnd(stream);
  87. if (zlib_err != Z_OK)
  88. goto out;
  89. if (k < b)
  90. goto out;
  91. return stream->total_out;
  92. out:
  93. for (; k < b; k++)
  94. put_bh(bh[k]);
  95. return -EIO;
  96. }
  97. const struct squashfs_decompressor squashfs_zlib_comp_ops = {
  98. .init = zlib_init,
  99. .free = zlib_free,
  100. .decompress = zlib_uncompress,
  101. .id = ZLIB_COMPRESSION,
  102. .name = "zlib",
  103. .supported = 1
  104. };