xref: /linux/lib/decompress.c (revision 33e2a4227ddff7c18921ac175fae3ab0e3ff8a76)
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>
14889c92d2SH. Peter Anvin 
15889c92d2SH. Peter Anvin #include <linux/types.h>
16889c92d2SH. Peter Anvin #include <linux/string.h>
17*33e2a422SHein Tibosch #include <linux/init.h>
18889c92d2SH. Peter Anvin 
1923a22d57SH. Peter Anvin #ifndef CONFIG_DECOMPRESS_GZIP
2023a22d57SH. Peter Anvin # define gunzip NULL
2123a22d57SH. Peter Anvin #endif
2223a22d57SH. Peter Anvin #ifndef CONFIG_DECOMPRESS_BZIP2
2323a22d57SH. Peter Anvin # define bunzip2 NULL
2423a22d57SH. Peter Anvin #endif
2523a22d57SH. Peter Anvin #ifndef CONFIG_DECOMPRESS_LZMA
2623a22d57SH. Peter Anvin # define unlzma NULL
2723a22d57SH. Peter Anvin #endif
283ebe1243SLasse Collin #ifndef CONFIG_DECOMPRESS_XZ
293ebe1243SLasse Collin # define unxz NULL
303ebe1243SLasse Collin #endif
31cacb246fSAlbin Tonnerre #ifndef CONFIG_DECOMPRESS_LZO
32cacb246fSAlbin Tonnerre # define unlzo NULL
33cacb246fSAlbin Tonnerre #endif
3423a22d57SH. Peter Anvin 
35*33e2a422SHein Tibosch struct compress_format {
36889c92d2SH. Peter Anvin 	unsigned char magic[2];
37889c92d2SH. Peter Anvin 	const char *name;
38889c92d2SH. Peter Anvin 	decompress_fn decompressor;
39*33e2a422SHein Tibosch };
40*33e2a422SHein Tibosch 
41*33e2a422SHein Tibosch static const struct compress_format compressed_formats[] __initdata = {
42889c92d2SH. Peter Anvin 	{ {037, 0213}, "gzip", gunzip },
43889c92d2SH. Peter Anvin 	{ {037, 0236}, "gzip", gunzip },
44889c92d2SH. Peter Anvin 	{ {0x42, 0x5a}, "bzip2", bunzip2 },
45889c92d2SH. Peter Anvin 	{ {0x5d, 0x00}, "lzma", unlzma },
463ebe1243SLasse Collin 	{ {0xfd, 0x37}, "xz", unxz },
47cacb246fSAlbin Tonnerre 	{ {0x89, 0x4c}, "lzo", unlzo },
48889c92d2SH. Peter Anvin 	{ {0, 0}, NULL, NULL }
49889c92d2SH. Peter Anvin };
50889c92d2SH. Peter Anvin 
51*33e2a422SHein Tibosch decompress_fn __init decompress_method(const unsigned char *inbuf, int len,
52889c92d2SH. Peter Anvin 				const char **name)
53889c92d2SH. Peter Anvin {
54889c92d2SH. Peter Anvin 	const struct compress_format *cf;
55889c92d2SH. Peter Anvin 
56889c92d2SH. Peter Anvin 	if (len < 2)
57889c92d2SH. Peter Anvin 		return NULL;	/* Need at least this much... */
58889c92d2SH. Peter Anvin 
59e4aa7ca5SAlain Knaff 	for (cf = compressed_formats; cf->name; cf++) {
60889c92d2SH. Peter Anvin 		if (!memcmp(inbuf, cf->magic, 2))
61889c92d2SH. Peter Anvin 			break;
62889c92d2SH. Peter Anvin 
63889c92d2SH. Peter Anvin 	}
64889c92d2SH. Peter Anvin 	if (name)
65889c92d2SH. Peter Anvin 		*name = cf->name;
66889c92d2SH. Peter Anvin 	return cf->decompressor;
67889c92d2SH. Peter Anvin }
68