^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * uncompress.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * (C) Copyright 1999 Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * cramfs interfaces to the uncompression library. There's really just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * three entrypoints:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * - cramfs_uncompress_init() - called to initialize the thing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * - cramfs_uncompress_exit() - tell me when you're done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * - cramfs_uncompress_block() - uncompress a block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * NOTE NOTE NOTE! The uncompression is entirely single-threaded. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * only have one stream, and we'll initialize it only once even if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * then is used by multiple filesystems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/zlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static z_stream stream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static int initialized;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /* Returns length of decompressed data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int cramfs_uncompress_block(void *dst, int dstlen, void *src, int srclen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) stream.next_in = src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) stream.avail_in = srclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) stream.next_out = dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) stream.avail_out = dstlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) err = zlib_inflateReset(&stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (err != Z_OK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) pr_err("zlib_inflateReset error %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) zlib_inflateEnd(&stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) zlib_inflateInit(&stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) err = zlib_inflate(&stream, Z_FINISH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (err != Z_STREAM_END)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return stream.total_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) pr_err("Error %d while decompressing!\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) pr_err("%p(%d)->%p(%d)\n", src, srclen, dst, dstlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) int cramfs_uncompress_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (!initialized++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) stream.workspace = vmalloc(zlib_inflate_workspacesize());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (!stream.workspace) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) initialized = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) stream.next_in = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) stream.avail_in = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) zlib_inflateInit(&stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) void cramfs_uncompress_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (!--initialized) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) zlib_inflateEnd(&stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) vfree(stream.workspace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }