H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * decompress.c |
| 3 | * |
| 4 | * Detect the decompression method based on magic number |
| 5 | */ |
| 6 | |
| 7 | #include <linux/decompress/generic.h> |
| 8 | |
| 9 | #include <linux/decompress/bunzip2.h> |
| 10 | #include <linux/decompress/unlzma.h> |
Lasse Collin | 3ebe124 | 2011-01-12 17:01:23 -0800 | [diff] [blame] | 11 | #include <linux/decompress/unxz.h> |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 12 | #include <linux/decompress/inflate.h> |
Albin Tonnerre | cacb246 | 2010-01-08 14:42:46 -0800 | [diff] [blame] | 13 | #include <linux/decompress/unlzo.h> |
Kyungsik Lee | e76e1fd | 2013-07-08 16:01:46 -0700 | [diff] [blame] | 14 | #include <linux/decompress/unlz4.h> |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 15 | |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/string.h> |
Hein Tibosch | 33e2a42 | 2012-10-04 17:16:58 -0700 | [diff] [blame] | 18 | #include <linux/init.h> |
Daniel M. Weeks | 6aa7a29 | 2014-04-07 15:39:16 -0700 | [diff] [blame] | 19 | #include <linux/printk.h> |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 20 | |
H. Peter Anvin | 23a22d5 | 2009-01-12 14:24:04 -0800 | [diff] [blame] | 21 | #ifndef CONFIG_DECOMPRESS_GZIP |
| 22 | # define gunzip NULL |
| 23 | #endif |
| 24 | #ifndef CONFIG_DECOMPRESS_BZIP2 |
| 25 | # define bunzip2 NULL |
| 26 | #endif |
| 27 | #ifndef CONFIG_DECOMPRESS_LZMA |
| 28 | # define unlzma NULL |
| 29 | #endif |
Lasse Collin | 3ebe124 | 2011-01-12 17:01:23 -0800 | [diff] [blame] | 30 | #ifndef CONFIG_DECOMPRESS_XZ |
| 31 | # define unxz NULL |
| 32 | #endif |
Albin Tonnerre | cacb246 | 2010-01-08 14:42:46 -0800 | [diff] [blame] | 33 | #ifndef CONFIG_DECOMPRESS_LZO |
| 34 | # define unlzo NULL |
| 35 | #endif |
Kyungsik Lee | e76e1fd | 2013-07-08 16:01:46 -0700 | [diff] [blame] | 36 | #ifndef CONFIG_DECOMPRESS_LZ4 |
| 37 | # define unlz4 NULL |
| 38 | #endif |
H. Peter Anvin | 23a22d5 | 2009-01-12 14:24:04 -0800 | [diff] [blame] | 39 | |
Hein Tibosch | 33e2a42 | 2012-10-04 17:16:58 -0700 | [diff] [blame] | 40 | struct compress_format { |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 41 | unsigned char magic[2]; |
| 42 | const char *name; |
| 43 | decompress_fn decompressor; |
Hein Tibosch | 33e2a42 | 2012-10-04 17:16:58 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
Andi Kleen | 6f9982b | 2013-04-30 15:28:50 -0700 | [diff] [blame] | 46 | static const struct compress_format compressed_formats[] __initconst = { |
Haesung Kim | a060bfe | 2014-12-12 16:58:08 -0800 | [diff] [blame] | 47 | { {0x1f, 0x8b}, "gzip", gunzip }, |
| 48 | { {0x1f, 0x9e}, "gzip", gunzip }, |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 49 | { {0x42, 0x5a}, "bzip2", bunzip2 }, |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 50 | { {0x5d, 0x00}, "lzma", unlzma }, |
Lasse Collin | 3ebe124 | 2011-01-12 17:01:23 -0800 | [diff] [blame] | 51 | { {0xfd, 0x37}, "xz", unxz }, |
Albin Tonnerre | cacb246 | 2010-01-08 14:42:46 -0800 | [diff] [blame] | 52 | { {0x89, 0x4c}, "lzo", unlzo }, |
Kyungsik Lee | e76e1fd | 2013-07-08 16:01:46 -0700 | [diff] [blame] | 53 | { {0x02, 0x21}, "lz4", unlz4 }, |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 54 | { {0, 0}, NULL, NULL } |
| 55 | }; |
| 56 | |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 57 | decompress_fn __init decompress_method(const unsigned char *inbuf, long len, |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 58 | const char **name) |
| 59 | { |
| 60 | const struct compress_format *cf; |
| 61 | |
| 62 | if (len < 2) |
| 63 | return NULL; /* Need at least this much... */ |
| 64 | |
Daniel M. Weeks | 6aa7a29 | 2014-04-07 15:39:16 -0700 | [diff] [blame] | 65 | pr_debug("Compressed data magic: %#.2x %#.2x\n", inbuf[0], inbuf[1]); |
| 66 | |
Alain Knaff | e4aa7ca | 2009-02-19 13:36:55 -0800 | [diff] [blame] | 67 | for (cf = compressed_formats; cf->name; cf++) { |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 68 | if (!memcmp(inbuf, cf->magic, 2)) |
| 69 | break; |
| 70 | |
| 71 | } |
| 72 | if (name) |
| 73 | *name = cf->name; |
| 74 | return cf->decompressor; |
| 75 | } |