1 // SPDX-License-Identifier: GPL-2.0
5 * (C) Copyright 1999 Linus Torvalds
7 * cramfs interfaces to the uncompression library. There's really just
10 * - cramfs_uncompress_init() - called to initialize the thing.
11 * - cramfs_uncompress_exit() - tell me when you're done
12 * - cramfs_uncompress_block() - uncompress a block.
14 * NOTE NOTE NOTE! The uncompression is entirely single-threaded. We
15 * only have one stream, and we'll initialize it only once even if it
16 * then is used by multiple filesystems.
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/vmalloc.h>
24 #include <linux/zlib.h>
27 static z_stream stream;
28 static int initialized;
30 /* Returns length of decompressed data. */
31 int cramfs_uncompress_block(void *dst, int dstlen, void *src, int srclen)
36 stream.avail_in = srclen;
38 stream.next_out = dst;
39 stream.avail_out = dstlen;
41 err = zlib_inflateReset(&stream);
43 pr_err("zlib_inflateReset error %d\n", err);
44 zlib_inflateEnd(&stream);
45 zlib_inflateInit(&stream);
48 err = zlib_inflate(&stream, Z_FINISH);
49 if (err != Z_STREAM_END)
51 return stream.total_out;
54 pr_err("Error %d while decompressing!\n", err);
55 pr_err("%p(%d)->%p(%d)\n", src, srclen, dst, dstlen);
59 int cramfs_uncompress_init(void)
62 stream.workspace = vmalloc(zlib_inflate_workspacesize());
63 if (!stream.workspace) {
67 stream.next_in = NULL;
69 zlib_inflateInit(&stream);
74 void cramfs_uncompress_exit(void)
77 zlib_inflateEnd(&stream);
78 vfree(stream.workspace);