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