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> |
| 11 | #include <linux/decompress/inflate.h> |
| 12 | |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/string.h> |
| 15 | |
H. Peter Anvin | 23a22d5 | 2009-01-12 14:24:04 -0800 | [diff] [blame] | 16 | #ifndef CONFIG_DECOMPRESS_GZIP |
| 17 | # define gunzip NULL |
| 18 | #endif |
| 19 | #ifndef CONFIG_DECOMPRESS_BZIP2 |
| 20 | # define bunzip2 NULL |
| 21 | #endif |
| 22 | #ifndef CONFIG_DECOMPRESS_LZMA |
| 23 | # define unlzma NULL |
| 24 | #endif |
| 25 | |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 26 | static const struct compress_format { |
| 27 | unsigned char magic[2]; |
| 28 | const char *name; |
| 29 | decompress_fn decompressor; |
| 30 | } compressed_formats[] = { |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 31 | { {037, 0213}, "gzip", gunzip }, |
| 32 | { {037, 0236}, "gzip", gunzip }, |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 33 | { {0x42, 0x5a}, "bzip2", bunzip2 }, |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 34 | { {0x5d, 0x00}, "lzma", unlzma }, |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 35 | { {0, 0}, NULL, NULL } |
| 36 | }; |
| 37 | |
| 38 | decompress_fn decompress_method(const unsigned char *inbuf, int len, |
| 39 | const char **name) |
| 40 | { |
| 41 | const struct compress_format *cf; |
| 42 | |
| 43 | if (len < 2) |
| 44 | return NULL; /* Need at least this much... */ |
| 45 | |
Alain Knaff | e4aa7ca | 2009-02-19 13:36:55 -0800 | [diff] [blame] | 46 | for (cf = compressed_formats; cf->name; cf++) { |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 47 | if (!memcmp(inbuf, cf->magic, 2)) |
| 48 | break; |
| 49 | |
| 50 | } |
| 51 | if (name) |
| 52 | *name = cf->name; |
| 53 | return cf->decompressor; |
| 54 | } |