Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * uncompress.c |
| 4 | * |
| 5 | * (C) Copyright 1999 Linus Torvalds |
| 6 | * |
| 7 | * cramfs interfaces to the uncompression library. There's really just |
| 8 | * three entrypoints: |
| 9 | * |
| 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. |
| 13 | * |
| 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. |
| 17 | */ |
| 18 | |
Fabian Frederick | 4f21e1e | 2014-08-08 14:22:50 -0700 | [diff] [blame] | 19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/kernel.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/vmalloc.h> |
| 24 | #include <linux/zlib.h> |
Al Viro | f7f4f4d | 2013-12-10 16:54:28 -0500 | [diff] [blame] | 25 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | static z_stream stream; |
| 28 | static int initialized; |
| 29 | |
| 30 | /* Returns length of decompressed data. */ |
| 31 | int cramfs_uncompress_block(void *dst, int dstlen, void *src, int srclen) |
| 32 | { |
| 33 | int err; |
| 34 | |
| 35 | stream.next_in = src; |
| 36 | stream.avail_in = srclen; |
| 37 | |
| 38 | stream.next_out = dst; |
| 39 | stream.avail_out = dstlen; |
| 40 | |
| 41 | err = zlib_inflateReset(&stream); |
| 42 | if (err != Z_OK) { |
Fabian Frederick | f175ff8 | 2014-08-08 14:22:48 -0700 | [diff] [blame] | 43 | pr_err("zlib_inflateReset error %d\n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | zlib_inflateEnd(&stream); |
| 45 | zlib_inflateInit(&stream); |
| 46 | } |
| 47 | |
| 48 | err = zlib_inflate(&stream, Z_FINISH); |
| 49 | if (err != Z_STREAM_END) |
| 50 | goto err; |
| 51 | return stream.total_out; |
| 52 | |
| 53 | err: |
Fabian Frederick | f175ff8 | 2014-08-08 14:22:48 -0700 | [diff] [blame] | 54 | pr_err("Error %d while decompressing!\n", err); |
| 55 | pr_err("%p(%d)->%p(%d)\n", src, srclen, dst, dstlen); |
David VomLehn | 98310e5 | 2009-04-02 16:59:15 -0700 | [diff] [blame] | 56 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | int cramfs_uncompress_init(void) |
| 60 | { |
| 61 | if (!initialized++) { |
| 62 | stream.workspace = vmalloc(zlib_inflate_workspacesize()); |
Fabian Frederick | 31d92e5 | 2014-08-08 14:22:52 -0700 | [diff] [blame] | 63 | if (!stream.workspace) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | initialized = 0; |
| 65 | return -ENOMEM; |
| 66 | } |
| 67 | stream.next_in = NULL; |
| 68 | stream.avail_in = 0; |
| 69 | zlib_inflateInit(&stream); |
| 70 | } |
| 71 | return 0; |
| 72 | } |
| 73 | |
Alexey Dobriyan | 368bdb3 | 2006-09-29 02:01:05 -0700 | [diff] [blame] | 74 | void cramfs_uncompress_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
| 76 | if (!--initialized) { |
| 77 | zlib_inflateEnd(&stream); |
| 78 | vfree(stream.workspace); |
| 79 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |