xref: /linux/lib/decompress.c (revision e76e1fdfa8f8dc1ea6699923cf5d92b5bee9c936)
1889c92d2SH. Peter Anvin /*
2889c92d2SH. Peter Anvin  * decompress.c
3889c92d2SH. Peter Anvin  *
4889c92d2SH. Peter Anvin  * Detect the decompression method based on magic number
5889c92d2SH. Peter Anvin  */
6889c92d2SH. Peter Anvin 
7889c92d2SH. Peter Anvin #include <linux/decompress/generic.h>
8889c92d2SH. Peter Anvin 
9889c92d2SH. Peter Anvin #include <linux/decompress/bunzip2.h>
10889c92d2SH. Peter Anvin #include <linux/decompress/unlzma.h>
113ebe1243SLasse Collin #include <linux/decompress/unxz.h>
12889c92d2SH. Peter Anvin #include <linux/decompress/inflate.h>
13cacb246fSAlbin Tonnerre #include <linux/decompress/unlzo.h>
14*e76e1fdfSKyungsik Lee #include <linux/decompress/unlz4.h>
15889c92d2SH. Peter Anvin 
16889c92d2SH. Peter Anvin #include <linux/types.h>
17889c92d2SH. Peter Anvin #include <linux/string.h>
1833e2a422SHein Tibosch #include <linux/init.h>
19889c92d2SH. Peter Anvin 
2023a22d57SH. Peter Anvin #ifndef CONFIG_DECOMPRESS_GZIP
2123a22d57SH. Peter Anvin # define gunzip NULL
2223a22d57SH. Peter Anvin #endif
2323a22d57SH. Peter Anvin #ifndef CONFIG_DECOMPRESS_BZIP2
2423a22d57SH. Peter Anvin # define bunzip2 NULL
2523a22d57SH. Peter Anvin #endif
2623a22d57SH. Peter Anvin #ifndef CONFIG_DECOMPRESS_LZMA
2723a22d57SH. Peter Anvin # define unlzma NULL
2823a22d57SH. Peter Anvin #endif
293ebe1243SLasse Collin #ifndef CONFIG_DECOMPRESS_XZ
303ebe1243SLasse Collin # define unxz NULL
313ebe1243SLasse Collin #endif
32cacb246fSAlbin Tonnerre #ifndef CONFIG_DECOMPRESS_LZO
33cacb246fSAlbin Tonnerre # define unlzo NULL
34cacb246fSAlbin Tonnerre #endif
35*e76e1fdfSKyungsik Lee #ifndef CONFIG_DECOMPRESS_LZ4
36*e76e1fdfSKyungsik Lee # define unlz4 NULL
37*e76e1fdfSKyungsik Lee #endif
3823a22d57SH. Peter Anvin 
3933e2a422SHein Tibosch struct compress_format {
40889c92d2SH. Peter Anvin 	unsigned char magic[2];
41889c92d2SH. Peter Anvin 	const char *name;
42889c92d2SH. Peter Anvin 	decompress_fn decompressor;
4333e2a422SHein Tibosch };
4433e2a422SHein Tibosch 
456f9982bdSAndi Kleen static const struct compress_format compressed_formats[] __initconst = {
46889c92d2SH. Peter Anvin 	{ {037, 0213}, "gzip", gunzip },
47889c92d2SH. Peter Anvin 	{ {037, 0236}, "gzip", gunzip },
48889c92d2SH. Peter Anvin 	{ {0x42, 0x5a}, "bzip2", bunzip2 },
49889c92d2SH. Peter Anvin 	{ {0x5d, 0x00}, "lzma", unlzma },
503ebe1243SLasse Collin 	{ {0xfd, 0x37}, "xz", unxz },
51cacb246fSAlbin Tonnerre 	{ {0x89, 0x4c}, "lzo", unlzo },
52*e76e1fdfSKyungsik Lee 	{ {0x02, 0x21}, "lz4", unlz4 },
53889c92d2SH. Peter Anvin 	{ {0, 0}, NULL, NULL }
54889c92d2SH. Peter Anvin };
55889c92d2SH. Peter Anvin 
5633e2a422SHein Tibosch decompress_fn __init decompress_method(const unsigned char *inbuf, int len,
57889c92d2SH. Peter Anvin 				const char **name)
58889c92d2SH. Peter Anvin {
59889c92d2SH. Peter Anvin 	const struct compress_format *cf;
60889c92d2SH. Peter Anvin 
61889c92d2SH. Peter Anvin 	if (len < 2)
62889c92d2SH. Peter Anvin 		return NULL;	/* Need at least this much... */
63889c92d2SH. Peter Anvin 
64e4aa7ca5SAlain Knaff 	for (cf = compressed_formats; cf->name; cf++) {
65889c92d2SH. Peter Anvin 		if (!memcmp(inbuf, cf->magic, 2))
66889c92d2SH. Peter Anvin 			break;
67889c92d2SH. Peter Anvin 
68889c92d2SH. Peter Anvin 	}
69889c92d2SH. Peter Anvin 	if (name)
70889c92d2SH. Peter Anvin 		*name = cf->name;
71889c92d2SH. Peter Anvin 	return cf->decompressor;
72889c92d2SH. Peter Anvin }
73