• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 
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>
30 
31 #include "squashfs_fs.h"
32 #include "squashfs_fs_sb.h"
33 #include "squashfs.h"
34 #include "decompressor.h"
35 #include "page_actor.h"
36 
zlib_init(struct squashfs_sb_info * dummy,void * buff)37 static void *zlib_init(struct squashfs_sb_info *dummy, void *buff)
38 {
39 	z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL);
40 	if (stream == NULL)
41 		goto failed;
42 	stream->workspace = vmalloc(zlib_inflate_workspacesize());
43 	if (stream->workspace == NULL)
44 		goto failed;
45 
46 	return stream;
47 
48 failed:
49 	ERROR("Failed to allocate zlib workspace\n");
50 	kfree(stream);
51 	return ERR_PTR(-ENOMEM);
52 }
53 
54 
zlib_free(void * strm)55 static void zlib_free(void *strm)
56 {
57 	z_stream *stream = strm;
58 
59 	if (stream)
60 		vfree(stream->workspace);
61 	kfree(stream);
62 }
63 
64 
zlib_uncompress(struct squashfs_sb_info * msblk,void * strm,struct buffer_head ** bh,int b,int offset,int length,struct squashfs_page_actor * output)65 static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm,
66 	struct buffer_head **bh, int b, int offset, int length,
67 	struct squashfs_page_actor *output)
68 {
69 	void *buf = NULL;
70 	int zlib_err, zlib_init = 0, k = 0;
71 	z_stream *stream = strm;
72 
73 	stream->avail_out = PAGE_SIZE;
74 	stream->next_out = squashfs_first_page(output);
75 	stream->avail_in = 0;
76 
77 	do {
78 		if (stream->avail_in == 0 && k < b) {
79 			int avail = min(length, msblk->devblksize - offset);
80 			length -= avail;
81 			stream->next_in = bh[k]->b_data + offset;
82 			stream->avail_in = avail;
83 			offset = 0;
84 		}
85 
86 		if (stream->avail_out == 0) {
87 			stream->next_out = squashfs_next_page(output);
88 			if (!IS_ERR(stream->next_out))
89 				stream->avail_out = PAGE_SIZE;
90 		}
91 
92 		if (!stream->next_out) {
93 			if (!buf) {
94 				buf = kmalloc(PAGE_SIZE, GFP_ATOMIC);
95 				if (!buf)
96 					goto out;
97 			}
98 			stream->next_out = buf;
99 		}
100 
101 		if (!zlib_init) {
102 			zlib_err = zlib_inflateInit(stream);
103 			if (zlib_err != Z_OK) {
104 				squashfs_finish_page(output);
105 				goto out;
106 			}
107 			zlib_init = 1;
108 		}
109 
110 		zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
111 
112 		if (stream->avail_in == 0 && k < b)
113 			put_bh(bh[k++]);
114 	} while (zlib_err == Z_OK);
115 
116 	squashfs_finish_page(output);
117 
118 	if (zlib_err != Z_STREAM_END)
119 		goto out;
120 
121 	zlib_err = zlib_inflateEnd(stream);
122 	if (zlib_err != Z_OK)
123 		goto out;
124 
125 	if (k < b)
126 		goto out;
127 
128 	kfree(buf);
129 	return stream->total_out;
130 
131 out:
132 	for (; k < b; k++)
133 		put_bh(bh[k]);
134 	kfree(buf);
135 
136 	return -EIO;
137 }
138 
139 const struct squashfs_decompressor squashfs_zlib_comp_ops = {
140 	.init = zlib_init,
141 	.free = zlib_free,
142 	.decompress = zlib_uncompress,
143 	.id = ZLIB_COMPRESSION,
144 	.name = "zlib",
145 	.supported = 1
146 };
147 
148