blob: ec4f1d4fdad062b2b5e0003adb77f05c3382cdbd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * uncompress.c
3 *
4 * (C) Copyright 1999 Linus Torvalds
5 *
6 * cramfs interfaces to the uncompression library. There's really just
7 * three entrypoints:
8 *
9 * - cramfs_uncompress_init() - called to initialize the thing.
10 * - cramfs_uncompress_exit() - tell me when you're done
11 * - cramfs_uncompress_block() - uncompress a block.
12 *
13 * NOTE NOTE NOTE! The uncompression is entirely single-threaded. We
14 * only have one stream, and we'll initialize it only once even if it
15 * then is used by multiple filesystems.
16 */
17
Fabian Frederick4f21e1e2014-08-08 14:22:50 -070018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/kernel.h>
21#include <linux/errno.h>
22#include <linux/vmalloc.h>
23#include <linux/zlib.h>
Al Virof7f4f4d2013-12-10 16:54:28 -050024#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static z_stream stream;
27static int initialized;
28
29/* Returns length of decompressed data. */
30int cramfs_uncompress_block(void *dst, int dstlen, void *src, int srclen)
31{
32 int err;
33
34 stream.next_in = src;
35 stream.avail_in = srclen;
36
37 stream.next_out = dst;
38 stream.avail_out = dstlen;
39
40 err = zlib_inflateReset(&stream);
41 if (err != Z_OK) {
Fabian Frederickf175ff82014-08-08 14:22:48 -070042 pr_err("zlib_inflateReset error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 zlib_inflateEnd(&stream);
44 zlib_inflateInit(&stream);
45 }
46
47 err = zlib_inflate(&stream, Z_FINISH);
48 if (err != Z_STREAM_END)
49 goto err;
50 return stream.total_out;
51
52err:
Fabian Frederickf175ff82014-08-08 14:22:48 -070053 pr_err("Error %d while decompressing!\n", err);
54 pr_err("%p(%d)->%p(%d)\n", src, srclen, dst, dstlen);
David VomLehn98310e52009-04-02 16:59:15 -070055 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
58int cramfs_uncompress_init(void)
59{
60 if (!initialized++) {
61 stream.workspace = vmalloc(zlib_inflate_workspacesize());
Fabian Frederick31d92e52014-08-08 14:22:52 -070062 if (!stream.workspace) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 initialized = 0;
64 return -ENOMEM;
65 }
66 stream.next_in = NULL;
67 stream.avail_in = 0;
68 zlib_inflateInit(&stream);
69 }
70 return 0;
71}
72
Alexey Dobriyan368bdb32006-09-29 02:01:05 -070073void cramfs_uncompress_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 if (!--initialized) {
76 zlib_inflateEnd(&stream);
77 vfree(stream.workspace);
78 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}