zlib_wrapper.c 2.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5
/*
 * Squashfs - a compressed read only filesystem for Linux
 *
 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
P
Phillip Lougher 已提交
6
 * Phillip Lougher <phillip@squashfs.org.uk>
7 8 9 10 11 12
 *
 * zlib_wrapper.c
 */


#include <linux/mutex.h>
13
#include <linux/bio.h>
14
#include <linux/slab.h>
15
#include <linux/zlib.h>
16
#include <linux/vmalloc.h>
17 18 19 20

#include "squashfs_fs.h"
#include "squashfs_fs_sb.h"
#include "squashfs.h"
21
#include "decompressor.h"
22
#include "page_actor.h"
23

24
static void *zlib_init(struct squashfs_sb_info *dummy, void *buff)
25 26 27 28
{
	z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL);
	if (stream == NULL)
		goto failed;
29
	stream->workspace = vmalloc(zlib_inflate_workspacesize());
30 31 32 33 34 35 36 37
	if (stream->workspace == NULL)
		goto failed;

	return stream;

failed:
	ERROR("Failed to allocate zlib workspace\n");
	kfree(stream);
38
	return ERR_PTR(-ENOMEM);
39 40 41
}


42
static void zlib_free(void *strm)
43 44 45 46
{
	z_stream *stream = strm;

	if (stream)
47
		vfree(stream->workspace);
48 49 50 51
	kfree(stream);
}


52
static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm,
53
	struct bio *bio, int offset, int length,
54
	struct squashfs_page_actor *output)
55
{
56 57 58
	struct bvec_iter_all iter_all = {};
	struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
	int zlib_init = 0, error = 0;
59
	z_stream *stream = strm;
60

61
	stream->avail_out = PAGE_SIZE;
62
	stream->next_out = squashfs_first_page(output);
63
	stream->avail_in = 0;
64

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	for (;;) {
		int zlib_err;

		if (stream->avail_in == 0) {
			const void *data;
			int avail;

			if (!bio_next_segment(bio, &iter_all)) {
				/* Z_STREAM_END must be reached. */
				error = -EIO;
				break;
			}

			avail = min(length, ((int)bvec->bv_len) - offset);
			data = page_address(bvec->bv_page) + bvec->bv_offset;
80
			length -= avail;
81
			stream->next_in = data + offset;
82
			stream->avail_in = avail;
83 84 85
			offset = 0;
		}

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

		if (!zlib_init) {
93
			zlib_err = zlib_inflateInit(stream);
94
			if (zlib_err != Z_OK) {
95 96
				error = -EIO;
				break;
97
			}
98 99 100
			zlib_init = 1;
		}

101
		zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
102 103 104 105 106 107 108
		if (zlib_err == Z_STREAM_END)
			break;
		if (zlib_err != Z_OK) {
			error = -EIO;
			break;
		}
	}
109

110 111
	squashfs_finish_page(output);

112 113 114
	if (!error)
		if (zlib_inflateEnd(stream) != Z_OK)
			error = -EIO;
115

116
	return error ? error : stream->total_out;
117
}
118 119 120 121 122 123 124 125 126 127

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