]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - lib/decompress.c
Linux 6.9-rc7
[thirdparty/kernel/linux.git] / lib / decompress.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
889c92d2
PA
2/*
3 * decompress.c
4 *
5 * Detect the decompression method based on magic number
6 */
7
8#include <linux/decompress/generic.h>
9
10#include <linux/decompress/bunzip2.h>
11#include <linux/decompress/unlzma.h>
3ebe1243 12#include <linux/decompress/unxz.h>
889c92d2 13#include <linux/decompress/inflate.h>
cacb246f 14#include <linux/decompress/unlzo.h>
e76e1fdf 15#include <linux/decompress/unlz4.h>
4963bb2b 16#include <linux/decompress/unzstd.h>
889c92d2
PA
17
18#include <linux/types.h>
19#include <linux/string.h>
33e2a422 20#include <linux/init.h>
6aa7a29a 21#include <linux/printk.h>
889c92d2 22
23a22d57
PA
23#ifndef CONFIG_DECOMPRESS_GZIP
24# define gunzip NULL
25#endif
26#ifndef CONFIG_DECOMPRESS_BZIP2
27# define bunzip2 NULL
28#endif
29#ifndef CONFIG_DECOMPRESS_LZMA
30# define unlzma NULL
31#endif
3ebe1243
LC
32#ifndef CONFIG_DECOMPRESS_XZ
33# define unxz NULL
34#endif
cacb246f
AT
35#ifndef CONFIG_DECOMPRESS_LZO
36# define unlzo NULL
37#endif
e76e1fdf
KL
38#ifndef CONFIG_DECOMPRESS_LZ4
39# define unlz4 NULL
40#endif
4963bb2b
NT
41#ifndef CONFIG_DECOMPRESS_ZSTD
42# define unzstd NULL
43#endif
23a22d57 44
33e2a422 45struct compress_format {
889c92d2
PA
46 unsigned char magic[2];
47 const char *name;
48 decompress_fn decompressor;
33e2a422
HT
49};
50
6f9982bd 51static const struct compress_format compressed_formats[] __initconst = {
a060bfe0
HK
52 { {0x1f, 0x8b}, "gzip", gunzip },
53 { {0x1f, 0x9e}, "gzip", gunzip },
889c92d2 54 { {0x42, 0x5a}, "bzip2", bunzip2 },
889c92d2 55 { {0x5d, 0x00}, "lzma", unlzma },
3ebe1243 56 { {0xfd, 0x37}, "xz", unxz },
cacb246f 57 { {0x89, 0x4c}, "lzo", unlzo },
e76e1fdf 58 { {0x02, 0x21}, "lz4", unlz4 },
4963bb2b 59 { {0x28, 0xb5}, "zstd", unzstd },
889c92d2
PA
60 { {0, 0}, NULL, NULL }
61};
62
d97b07c5 63decompress_fn __init decompress_method(const unsigned char *inbuf, long len,
889c92d2
PA
64 const char **name)
65{
66 const struct compress_format *cf;
67
5a09e6ce
AK
68 if (len < 2) {
69 if (name)
70 *name = NULL;
889c92d2 71 return NULL; /* Need at least this much... */
5a09e6ce 72 }
889c92d2 73
6aa7a29a
DW
74 pr_debug("Compressed data magic: %#.2x %#.2x\n", inbuf[0], inbuf[1]);
75
e4aa7ca5 76 for (cf = compressed_formats; cf->name; cf++) {
889c92d2
PA
77 if (!memcmp(inbuf, cf->magic, 2))
78 break;
79
80 }
81 if (name)
82 *name = cf->name;
83 return cf->decompressor;
84}