diff options
Diffstat (limited to 'lib/decompress.c')
-rw-r--r-- | lib/decompress.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/decompress.c b/lib/decompress.c new file mode 100644 index 000000000000..edac55cc7823 --- /dev/null +++ b/lib/decompress.c | |||
@@ -0,0 +1,50 @@ | |||
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 | |||
16 | static const struct compress_format { | ||
17 | unsigned char magic[2]; | ||
18 | const char *name; | ||
19 | decompress_fn decompressor; | ||
20 | } compressed_formats[] = { | ||
21 | #ifdef CONFIG_DECOMPRESS_GZIP | ||
22 | { {037, 0213}, "gzip", gunzip }, | ||
23 | { {037, 0236}, "gzip", gunzip }, | ||
24 | #endif | ||
25 | #ifdef CONFIG_DECOMPRESS_BZIP2 | ||
26 | { {0x42, 0x5a}, "bzip2", bunzip2 }, | ||
27 | #endif | ||
28 | #ifdef CONFIG_DECOMPRESS_LZMA | ||
29 | { {0x5d, 0x00}, "lzma", unlzma }, | ||
30 | #endif | ||
31 | { {0, 0}, NULL, NULL } | ||
32 | }; | ||
33 | |||
34 | decompress_fn decompress_method(const unsigned char *inbuf, int len, | ||
35 | const char **name) | ||
36 | { | ||
37 | const struct compress_format *cf; | ||
38 | |||
39 | if (len < 2) | ||
40 | return NULL; /* Need at least this much... */ | ||
41 | |||
42 | for (cf = compressed_formats; cf->decompressor; cf++) { | ||
43 | if (!memcmp(inbuf, cf->magic, 2)) | ||
44 | break; | ||
45 | |||
46 | } | ||
47 | if (name) | ||
48 | *name = cf->name; | ||
49 | return cf->decompressor; | ||
50 | } | ||