blob: 528ff932d8e445ae9e19940d4f13cd20c0273bc7 [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>
Daniel M. Weeks6aa7a292014-04-07 15:39:16 -070019#include <linux/printk.h>
H. Peter Anvin889c92d2009-01-08 15:14:17 -080020
H. Peter Anvin23a22d52009-01-12 14:24:04 -080021#ifndef CONFIG_DECOMPRESS_GZIP
22# define gunzip NULL
23#endif
24#ifndef CONFIG_DECOMPRESS_BZIP2
25# define bunzip2 NULL
26#endif
27#ifndef CONFIG_DECOMPRESS_LZMA
28# define unlzma NULL
29#endif
Lasse Collin3ebe1242011-01-12 17:01:23 -080030#ifndef CONFIG_DECOMPRESS_XZ
31# define unxz NULL
32#endif
Albin Tonnerrecacb2462010-01-08 14:42:46 -080033#ifndef CONFIG_DECOMPRESS_LZO
34# define unlzo NULL
35#endif
Kyungsik Leee76e1fd2013-07-08 16:01:46 -070036#ifndef CONFIG_DECOMPRESS_LZ4
37# define unlz4 NULL
38#endif
H. Peter Anvin23a22d52009-01-12 14:24:04 -080039
Hein Tibosch33e2a422012-10-04 17:16:58 -070040struct compress_format {
H. Peter Anvin889c92d2009-01-08 15:14:17 -080041 unsigned char magic[2];
42 const char *name;
43 decompress_fn decompressor;
Hein Tibosch33e2a422012-10-04 17:16:58 -070044};
45
Andi Kleen6f9982b2013-04-30 15:28:50 -070046static const struct compress_format compressed_formats[] __initconst = {
Haesung Kima060bfe2014-12-12 16:58:08 -080047 { {0x1f, 0x8b}, "gzip", gunzip },
48 { {0x1f, 0x9e}, "gzip", gunzip },
H. Peter Anvin889c92d2009-01-08 15:14:17 -080049 { {0x42, 0x5a}, "bzip2", bunzip2 },
H. Peter Anvin889c92d2009-01-08 15:14:17 -080050 { {0x5d, 0x00}, "lzma", unlzma },
Lasse Collin3ebe1242011-01-12 17:01:23 -080051 { {0xfd, 0x37}, "xz", unxz },
Albin Tonnerrecacb2462010-01-08 14:42:46 -080052 { {0x89, 0x4c}, "lzo", unlzo },
Kyungsik Leee76e1fd2013-07-08 16:01:46 -070053 { {0x02, 0x21}, "lz4", unlz4 },
H. Peter Anvin889c92d2009-01-08 15:14:17 -080054 { {0, 0}, NULL, NULL }
55};
56
Yinghai Lud97b07c2014-08-08 14:23:14 -070057decompress_fn __init decompress_method(const unsigned char *inbuf, long len,
H. Peter Anvin889c92d2009-01-08 15:14:17 -080058 const char **name)
59{
60 const struct compress_format *cf;
61
62 if (len < 2)
63 return NULL; /* Need at least this much... */
64
Daniel M. Weeks6aa7a292014-04-07 15:39:16 -070065 pr_debug("Compressed data magic: %#.2x %#.2x\n", inbuf[0], inbuf[1]);
66
Alain Knaffe4aa7ca2009-02-19 13:36:55 -080067 for (cf = compressed_formats; cf->name; cf++) {
H. Peter Anvin889c92d2009-01-08 15:14:17 -080068 if (!memcmp(inbuf, cf->magic, 2))
69 break;
70
71 }
72 if (name)
73 *name = cf->name;
74 return cf->decompressor;
75}