zlib_wrapper.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include "page_actor.h"
  33. static void *zlib_init(struct squashfs_sb_info *dummy, void *buff)
  34. {
  35. z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL);
  36. if (stream == NULL)
  37. goto failed;
  38. stream->workspace = vmalloc(zlib_inflate_workspacesize());
  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 ERR_PTR(-ENOMEM);
  46. }
  47. static void zlib_free(void *strm)
  48. {
  49. z_stream *stream = strm;
  50. if (stream)
  51. vfree(stream->workspace);
  52. kfree(stream);
  53. }
  54. static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm,
  55. struct buffer_head **bh, int b, int offset, int length,
  56. struct squashfs_page_actor *output)
  57. {
  58. int zlib_err, zlib_init = 0, k = 0;
  59. z_stream *stream = strm;
  60. stream->avail_out = PAGE_SIZE;
  61. stream->next_out = squashfs_first_page(output);
  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. stream->next_in = bh[k]->b_data + offset;
  68. stream->avail_in = avail;
  69. offset = 0;
  70. }
  71. if (stream->avail_out == 0) {
  72. stream->next_out = squashfs_next_page(output);
  73. if (stream->next_out != NULL)
  74. stream->avail_out = PAGE_SIZE;
  75. }
  76. if (!zlib_init) {
  77. zlib_err = zlib_inflateInit(stream);
  78. if (zlib_err != Z_OK) {
  79. squashfs_finish_page(output);
  80. goto out;
  81. }
  82. zlib_init = 1;
  83. }
  84. zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
  85. if (stream->avail_in == 0 && k < b)
  86. put_bh(bh[k++]);
  87. } while (zlib_err == Z_OK);
  88. squashfs_finish_page(output);
  89. if (zlib_err != Z_STREAM_END)
  90. goto out;
  91. zlib_err = zlib_inflateEnd(stream);
  92. if (zlib_err != Z_OK)
  93. goto out;
  94. if (k < b)
  95. goto out;
  96. return stream->total_out;
  97. out:
  98. for (; k < b; k++)
  99. put_bh(bh[k]);
  100. return -EIO;
  101. }
  102. const struct squashfs_decompressor squashfs_zlib_comp_ops = {
  103. .init = zlib_init,
  104. .free = zlib_free,
  105. .decompress = zlib_uncompress,
  106. .id = ZLIB_COMPRESSION,
  107. .name = "zlib",
  108. .supported = 1
  109. };