2 * Squashfs - a compressed read only filesystem for Linux
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 * Phillip Lougher <phillip@lougher.demon.co.uk>
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.
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.
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.
25 #include <linux/mutex.h>
26 #include <linux/buffer_head.h>
27 #include <linux/zlib.h>
29 #include "squashfs_fs.h"
30 #include "squashfs_fs_sb.h"
31 #include "squashfs_fs_i.h"
34 int squashfs_zlib_uncompress(struct squashfs_sb_info *msblk, void **buffer,
35 struct buffer_head **bh, int b, int offset, int length, int srclength,
38 int zlib_err = 0, zlib_init = 0;
39 int avail, bytes, k = 0, page = 0;
41 mutex_lock(&msblk->read_data_mutex);
43 msblk->stream.avail_out = 0;
44 msblk->stream.avail_in = 0;
48 if (msblk->stream.avail_in == 0 && k < b) {
49 avail = min(bytes, msblk->devblksize - offset);
51 wait_on_buffer(bh[k]);
52 if (!buffer_uptodate(bh[k]))
61 msblk->stream.next_in = bh[k]->b_data + offset;
62 msblk->stream.avail_in = avail;
66 if (msblk->stream.avail_out == 0 && page < pages) {
67 msblk->stream.next_out = buffer[page++];
68 msblk->stream.avail_out = PAGE_CACHE_SIZE;
72 zlib_err = zlib_inflateInit(&msblk->stream);
73 if (zlib_err != Z_OK) {
74 ERROR("zlib_inflateInit returned unexpected "
75 "result 0x%x, srclength %d\n",
82 zlib_err = zlib_inflate(&msblk->stream, Z_SYNC_FLUSH);
84 if (msblk->stream.avail_in == 0 && k < b)
86 } while (zlib_err == Z_OK);
88 if (zlib_err != Z_STREAM_END) {
89 ERROR("zlib_inflate error, data probably corrupt\n");
93 zlib_err = zlib_inflateEnd(&msblk->stream);
94 if (zlib_err != Z_OK) {
95 ERROR("zlib_inflate error, data probably corrupt\n");
99 mutex_unlock(&msblk->read_data_mutex);
100 return msblk->stream.total_out;
103 mutex_unlock(&msblk->read_data_mutex);