xref: /linux/lib/decompress.c (revision e4aa7ca5a2e6d44f07ceb87d9448113f5b48a334)
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>
11889c92d2SH. Peter Anvin #include <linux/decompress/inflate.h>
12889c92d2SH. Peter Anvin 
13889c92d2SH. Peter Anvin #include <linux/types.h>
14889c92d2SH. Peter Anvin #include <linux/string.h>
15889c92d2SH. Peter Anvin 
1623a22d57SH. Peter Anvin #ifndef CONFIG_DECOMPRESS_GZIP
1723a22d57SH. Peter Anvin # define gunzip NULL
1823a22d57SH. Peter Anvin #endif
1923a22d57SH. Peter Anvin #ifndef CONFIG_DECOMPRESS_BZIP2
2023a22d57SH. Peter Anvin # define bunzip2 NULL
2123a22d57SH. Peter Anvin #endif
2223a22d57SH. Peter Anvin #ifndef CONFIG_DECOMPRESS_LZMA
2323a22d57SH. Peter Anvin # define unlzma NULL
2423a22d57SH. Peter Anvin #endif
2523a22d57SH. Peter Anvin 
26889c92d2SH. Peter Anvin static const struct compress_format {
27889c92d2SH. Peter Anvin 	unsigned char magic[2];
28889c92d2SH. Peter Anvin 	const char *name;
29889c92d2SH. Peter Anvin 	decompress_fn decompressor;
30889c92d2SH. Peter Anvin } compressed_formats[] = {
31889c92d2SH. Peter Anvin 	{ {037, 0213}, "gzip", gunzip },
32889c92d2SH. Peter Anvin 	{ {037, 0236}, "gzip", gunzip },
33889c92d2SH. Peter Anvin 	{ {0x42, 0x5a}, "bzip2", bunzip2 },
34889c92d2SH. Peter Anvin 	{ {0x5d, 0x00}, "lzma", unlzma },
35889c92d2SH. Peter Anvin 	{ {0, 0}, NULL, NULL }
36889c92d2SH. Peter Anvin };
37889c92d2SH. Peter Anvin 
38889c92d2SH. Peter Anvin decompress_fn decompress_method(const unsigned char *inbuf, int len,
39889c92d2SH. Peter Anvin 				const char **name)
40889c92d2SH. Peter Anvin {
41889c92d2SH. Peter Anvin 	const struct compress_format *cf;
42889c92d2SH. Peter Anvin 
43889c92d2SH. Peter Anvin 	if (len < 2)
44889c92d2SH. Peter Anvin 		return NULL;	/* Need at least this much... */
45889c92d2SH. Peter Anvin 
46*e4aa7ca5SAlain Knaff 	for (cf = compressed_formats; cf->name; cf++) {
47889c92d2SH. Peter Anvin 		if (!memcmp(inbuf, cf->magic, 2))
48889c92d2SH. Peter Anvin 			break;
49889c92d2SH. Peter Anvin 
50889c92d2SH. Peter Anvin 	}
51889c92d2SH. Peter Anvin 	if (name)
52889c92d2SH. Peter Anvin 		*name = cf->name;
53889c92d2SH. Peter Anvin 	return cf->decompressor;
54889c92d2SH. Peter Anvin }
55