edac55cc7823c8f27ef95bf7e42f45ff270eb189
[safe/jmp/linux-2.6] / lib / decompress.c
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 }