blob: 4d1cd0397aab0d9857f7a82f528c72946b520f87 [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>
Lasse Collin3ebe1242011-01-12 17:01:23 -080011#include <linux/decompress/unxz.h>
H. Peter Anvin889c92d2009-01-08 15:14:17 -080012#include <linux/decompress/inflate.h>
Albin Tonnerrecacb2462010-01-08 14:42:46 -080013#include <linux/decompress/unlzo.h>
Kyungsik Leee76e1fd2013-07-08 16:01:46 -070014#include <linux/decompress/unlz4.h>
H. Peter Anvin889c92d2009-01-08 15:14:17 -080015
16#include <linux/types.h>
17#include <linux/string.h>
Hein Tibosch33e2a422012-10-04 17:16:58 -070018#include <linux/init.h>
H. Peter Anvin889c92d2009-01-08 15:14:17 -080019
H. Peter Anvin23a22d52009-01-12 14:24:04 -080020#ifndef CONFIG_DECOMPRESS_GZIP
21# define gunzip NULL
22#endif
23#ifndef CONFIG_DECOMPRESS_BZIP2
24# define bunzip2 NULL
25#endif
26#ifndef CONFIG_DECOMPRESS_LZMA
27# define unlzma NULL
28#endif
Lasse Collin3ebe1242011-01-12 17:01:23 -080029#ifndef CONFIG_DECOMPRESS_XZ
30# define unxz NULL
31#endif
Albin Tonnerrecacb2462010-01-08 14:42:46 -080032#ifndef CONFIG_DECOMPRESS_LZO
33# define unlzo NULL
34#endif
Kyungsik Leee76e1fd2013-07-08 16:01:46 -070035#ifndef CONFIG_DECOMPRESS_LZ4
36# define unlz4 NULL
37#endif
H. Peter Anvin23a22d52009-01-12 14:24:04 -080038
Hein Tibosch33e2a422012-10-04 17:16:58 -070039struct compress_format {
H. Peter Anvin889c92d2009-01-08 15:14:17 -080040 unsigned char magic[2];
41 const char *name;
42 decompress_fn decompressor;
Hein Tibosch33e2a422012-10-04 17:16:58 -070043};
44
Andi Kleen6f9982b2013-04-30 15:28:50 -070045static const struct compress_format compressed_formats[] __initconst = {
H. Peter Anvin889c92d2009-01-08 15:14:17 -080046 { {037, 0213}, "gzip", gunzip },
47 { {037, 0236}, "gzip", gunzip },
H. Peter Anvin889c92d2009-01-08 15:14:17 -080048 { {0x42, 0x5a}, "bzip2", bunzip2 },
H. Peter Anvin889c92d2009-01-08 15:14:17 -080049 { {0x5d, 0x00}, "lzma", unlzma },
Lasse Collin3ebe1242011-01-12 17:01:23 -080050 { {0xfd, 0x37}, "xz", unxz },
Albin Tonnerrecacb2462010-01-08 14:42:46 -080051 { {0x89, 0x4c}, "lzo", unlzo },
Kyungsik Leee76e1fd2013-07-08 16:01:46 -070052 { {0x02, 0x21}, "lz4", unlz4 },
H. Peter Anvin889c92d2009-01-08 15:14:17 -080053 { {0, 0}, NULL, NULL }
54};
55
Hein Tibosch33e2a422012-10-04 17:16:58 -070056decompress_fn __init decompress_method(const unsigned char *inbuf, int len,
H. Peter Anvin889c92d2009-01-08 15:14:17 -080057 const char **name)
58{
59 const struct compress_format *cf;
60
61 if (len < 2)
62 return NULL; /* Need at least this much... */
63
Alain Knaffe4aa7ca2009-02-19 13:36:55 -080064 for (cf = compressed_formats; cf->name; cf++) {
H. Peter Anvin889c92d2009-01-08 15:14:17 -080065 if (!memcmp(inbuf, cf->magic, 2))
66 break;
67
68 }
69 if (name)
70 *name = cf->name;
71 return cf->decompressor;
72}