zlib_wrapper.c 3.06 KB
Newer Older
1 2 3 4
/*
 * Squashfs - a compressed read only filesystem for Linux
 *
 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5
 * Phillip Lougher <phillip@squashfs.org.uk>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2,
 * or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * zlib_wrapper.c
 */


#include <linux/mutex.h>
#include <linux/buffer_head.h>
27
#include <linux/slab.h>
28
#include <linux/zlib.h>
29
#include <linux/vmalloc.h>
30 31 32 33

#include "squashfs_fs.h"
#include "squashfs_fs_sb.h"
#include "squashfs.h"
34
#include "decompressor.h"
35
#include "page_actor.h"
36

37
static void *zlib_init(struct squashfs_sb_info *dummy, void *buff)
38 39 40 41
{
	z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL);
	if (stream == NULL)
		goto failed;
42
	stream->workspace = vmalloc(zlib_inflate_workspacesize());
43 44 45 46 47 48 49 50
	if (stream->workspace == NULL)
		goto failed;

	return stream;

failed:
	ERROR("Failed to allocate zlib workspace\n");
	kfree(stream);
51
	return ERR_PTR(-ENOMEM);
52 53 54
}


55
static void zlib_free(void *strm)
56 57 58 59
{
	z_stream *stream = strm;

	if (stream)
60
		vfree(stream->workspace);
61 62 63 64
	kfree(stream);
}


65
static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm,
66 67
	struct buffer_head **bh, int b, int offset, int length,
	struct squashfs_page_actor *output)
68
{
69
	int zlib_err, zlib_init = 0, k = 0;
70
	z_stream *stream = strm;
71

72
	stream->avail_out = PAGE_SIZE;
73
	stream->next_out = squashfs_first_page(output);
74
	stream->avail_in = 0;
75 76

	do {
77
		if (stream->avail_in == 0 && k < b) {
78 79
			int avail = min(length, msblk->devblksize - offset);
			length -= avail;
80 81
			stream->next_in = bh[k]->b_data + offset;
			stream->avail_in = avail;
82 83 84
			offset = 0;
		}

85 86 87
		if (stream->avail_out == 0) {
			stream->next_out = squashfs_next_page(output);
			if (stream->next_out != NULL)
88
				stream->avail_out = PAGE_SIZE;
89 90 91
		}

		if (!zlib_init) {
92
			zlib_err = zlib_inflateInit(stream);
93 94
			if (zlib_err != Z_OK) {
				squashfs_finish_page(output);
95
				goto out;
96
			}
97 98 99
			zlib_init = 1;
		}

100
		zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
101

102
		if (stream->avail_in == 0 && k < b)
103 104 105
			put_bh(bh[k++]);
	} while (zlib_err == Z_OK);

106 107
	squashfs_finish_page(output);

108 109
	if (zlib_err != Z_STREAM_END)
		goto out;
110

111
	zlib_err = zlib_inflateEnd(stream);
112 113
	if (zlib_err != Z_OK)
		goto out;
114

115 116
	if (k < b)
		goto out;
117

118
	return stream->total_out;
119

120
out:
121 122 123 124 125
	for (; k < b; k++)
		put_bh(bh[k]);

	return -EIO;
}
126 127 128 129 130 131 132 133 134 135

const struct squashfs_decompressor squashfs_zlib_comp_ops = {
	.init = zlib_init,
	.free = zlib_free,
	.decompress = zlib_uncompress,
	.id = ZLIB_COMPRESSION,
	.name = "zlib",
	.supported = 1
};