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/slab.h>
28 #include <linux/zlib.h>
29 #include <linux/vmalloc.h>
31 #include "squashfs_fs.h"
32 #include "squashfs_fs_sb.h"
34 #include "decompressor.h"
36 static void *zlib_init(struct squashfs_sb_info
*dummy
, void *buff
, int len
)
38 z_stream
*stream
= kmalloc(sizeof(z_stream
), GFP_KERNEL
);
41 stream
->workspace
= vmalloc(zlib_inflate_workspacesize());
42 if (stream
->workspace
== NULL
)
48 ERROR("Failed to allocate zlib workspace\n");
50 return ERR_PTR(-ENOMEM
);
54 static void zlib_free(void *strm
)
56 z_stream
*stream
= strm
;
59 vfree(stream
->workspace
);
64 static int zlib_uncompress(struct squashfs_sb_info
*msblk
, void **buffer
,
65 struct buffer_head
**bh
, int b
, int offset
, int length
, int srclength
,
68 int zlib_err
, zlib_init
= 0;
70 z_stream
*stream
= msblk
->stream
;
72 mutex_lock(&msblk
->read_data_mutex
);
74 stream
->avail_out
= 0;
78 if (stream
->avail_in
== 0 && k
< b
) {
79 int avail
= min(length
, msblk
->devblksize
- offset
);
81 wait_on_buffer(bh
[k
]);
82 if (!buffer_uptodate(bh
[k
]))
85 stream
->next_in
= bh
[k
]->b_data
+ offset
;
86 stream
->avail_in
= avail
;
90 if (stream
->avail_out
== 0 && page
< pages
) {
91 stream
->next_out
= buffer
[page
++];
92 stream
->avail_out
= PAGE_CACHE_SIZE
;
96 zlib_err
= zlib_inflateInit(stream
);
97 if (zlib_err
!= Z_OK
) {
98 ERROR("zlib_inflateInit returned unexpected "
99 "result 0x%x, srclength %d\n",
100 zlib_err
, srclength
);
106 zlib_err
= zlib_inflate(stream
, Z_SYNC_FLUSH
);
108 if (stream
->avail_in
== 0 && k
< b
)
110 } while (zlib_err
== Z_OK
);
112 if (zlib_err
!= Z_STREAM_END
) {
113 ERROR("zlib_inflate error, data probably corrupt\n");
117 zlib_err
= zlib_inflateEnd(stream
);
118 if (zlib_err
!= Z_OK
) {
119 ERROR("zlib_inflate error, data probably corrupt\n");
124 ERROR("zlib_uncompress error, data remaining\n");
128 length
= stream
->total_out
;
129 mutex_unlock(&msblk
->read_data_mutex
);
133 mutex_unlock(&msblk
->read_data_mutex
);
141 const struct squashfs_decompressor squashfs_zlib_comp_ops
= {
144 .decompress
= zlib_uncompress
,
145 .id
= ZLIB_COMPRESSION
,