^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) * decompress.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Detect the decompression method based on magic number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/decompress/generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/decompress/bunzip2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/decompress/unlzma.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/decompress/unxz.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/decompress/inflate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/decompress/unlzo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/decompress/unlz4.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/decompress/unzstd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #ifndef CONFIG_DECOMPRESS_GZIP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) # define gunzip NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #ifndef CONFIG_DECOMPRESS_BZIP2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) # define bunzip2 NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #ifndef CONFIG_DECOMPRESS_LZMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) # define unlzma NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #ifndef CONFIG_DECOMPRESS_XZ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) # define unxz NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #ifndef CONFIG_DECOMPRESS_LZO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) # define unlzo NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #ifndef CONFIG_DECOMPRESS_LZ4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) # define unlz4 NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #ifndef CONFIG_DECOMPRESS_ZSTD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) # define unzstd NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct compress_format {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned char magic[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) decompress_fn decompressor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static const struct compress_format compressed_formats[] __initconst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) { {0x1f, 0x8b}, "gzip", gunzip },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) { {0x1f, 0x9e}, "gzip", gunzip },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) { {0x42, 0x5a}, "bzip2", bunzip2 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) { {0x5d, 0x00}, "lzma", unlzma },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) { {0xfd, 0x37}, "xz", unxz },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) { {0x89, 0x4c}, "lzo", unlzo },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) { {0x02, 0x21}, "lz4", unlz4 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) { {0x28, 0xb5}, "zstd", unzstd },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) { {0, 0}, NULL, NULL }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) decompress_fn __init decompress_method(const unsigned char *inbuf, long len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) const char **name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) const struct compress_format *cf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (len < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return NULL; /* Need at least this much... */
^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) pr_debug("Compressed data magic: %#.2x %#.2x\n", inbuf[0], inbuf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) for (cf = compressed_formats; cf->name; cf++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (!memcmp(inbuf, cf->magic, 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) *name = cf->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return cf->decompressor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }