blob: a7606815541f4631d675707950a11de0609aff89 [file] [log] [blame]
H. Peter Anvin889c92d2009-01-08 15:14:17 -08001/*
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>
Albin Tonnerrecacb2462010-01-08 14:42:46 -080012#include <linux/decompress/unlzo.h>
H. Peter Anvin889c92d2009-01-08 15:14:17 -080013
14#include <linux/types.h>
15#include <linux/string.h>
16
H. Peter Anvin23a22d52009-01-12 14:24:04 -080017#ifndef CONFIG_DECOMPRESS_GZIP
18# define gunzip NULL
19#endif
20#ifndef CONFIG_DECOMPRESS_BZIP2
21# define bunzip2 NULL
22#endif
23#ifndef CONFIG_DECOMPRESS_LZMA
24# define unlzma NULL
25#endif
Albin Tonnerrecacb2462010-01-08 14:42:46 -080026#ifndef CONFIG_DECOMPRESS_LZO
27# define unlzo NULL
28#endif
H. Peter Anvin23a22d52009-01-12 14:24:04 -080029
H. Peter Anvin889c92d2009-01-08 15:14:17 -080030static const struct compress_format {
31 unsigned char magic[2];
32 const char *name;
33 decompress_fn decompressor;
34} compressed_formats[] = {
H. Peter Anvin889c92d2009-01-08 15:14:17 -080035 { {037, 0213}, "gzip", gunzip },
36 { {037, 0236}, "gzip", gunzip },
H. Peter Anvin889c92d2009-01-08 15:14:17 -080037 { {0x42, 0x5a}, "bzip2", bunzip2 },
H. Peter Anvin889c92d2009-01-08 15:14:17 -080038 { {0x5d, 0x00}, "lzma", unlzma },
Albin Tonnerrecacb2462010-01-08 14:42:46 -080039 { {0x89, 0x4c}, "lzo", unlzo },
H. Peter Anvin889c92d2009-01-08 15:14:17 -080040 { {0, 0}, NULL, NULL }
41};
42
43decompress_fn decompress_method(const unsigned char *inbuf, int len,
44 const char **name)
45{
46 const struct compress_format *cf;
47
48 if (len < 2)
49 return NULL; /* Need at least this much... */
50
Alain Knaffe4aa7ca2009-02-19 13:36:55 -080051 for (cf = compressed_formats; cf->name; cf++) {
H. Peter Anvin889c92d2009-01-08 15:14:17 -080052 if (!memcmp(inbuf, cf->magic, 2))
53 break;
54
55 }
56 if (name)
57 *name = cf->name;
58 return cf->decompressor;
59}