blob: 857ab1af1ef39aa6b46fe6de62b8aa26279f42ba [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
H. Peter Anvin889c92d2009-01-08 15:14:17 -08002/*
3 * decompress.c
4 *
5 * Detect the decompression method based on magic number
6 */
7
8#include <linux/decompress/generic.h>
9
10#include <linux/decompress/bunzip2.h>
11#include <linux/decompress/unlzma.h>
Lasse Collin3ebe1242011-01-12 17:01:23 -080012#include <linux/decompress/unxz.h>
H. Peter Anvin889c92d2009-01-08 15:14:17 -080013#include <linux/decompress/inflate.h>
Albin Tonnerrecacb2462010-01-08 14:42:46 -080014#include <linux/decompress/unlzo.h>
Kyungsik Leee76e1fd2013-07-08 16:01:46 -070015#include <linux/decompress/unlz4.h>
H. Peter Anvin889c92d2009-01-08 15:14:17 -080016
17#include <linux/types.h>
18#include <linux/string.h>
Hein Tibosch33e2a422012-10-04 17:16:58 -070019#include <linux/init.h>
Daniel M. Weeks6aa7a292014-04-07 15:39:16 -070020#include <linux/printk.h>
H. Peter Anvin889c92d2009-01-08 15:14:17 -080021
H. Peter Anvin23a22d52009-01-12 14:24:04 -080022#ifndef CONFIG_DECOMPRESS_GZIP
23# define gunzip NULL
24#endif
25#ifndef CONFIG_DECOMPRESS_BZIP2
26# define bunzip2 NULL
27#endif
28#ifndef CONFIG_DECOMPRESS_LZMA
29# define unlzma NULL
30#endif
Lasse Collin3ebe1242011-01-12 17:01:23 -080031#ifndef CONFIG_DECOMPRESS_XZ
32# define unxz NULL
33#endif
Albin Tonnerrecacb2462010-01-08 14:42:46 -080034#ifndef CONFIG_DECOMPRESS_LZO
35# define unlzo NULL
36#endif
Kyungsik Leee76e1fd2013-07-08 16:01:46 -070037#ifndef CONFIG_DECOMPRESS_LZ4
38# define unlz4 NULL
39#endif
H. Peter Anvin23a22d52009-01-12 14:24:04 -080040
Hein Tibosch33e2a422012-10-04 17:16:58 -070041struct compress_format {
H. Peter Anvin889c92d2009-01-08 15:14:17 -080042 unsigned char magic[2];
43 const char *name;
44 decompress_fn decompressor;
Hein Tibosch33e2a422012-10-04 17:16:58 -070045};
46
Andi Kleen6f9982b2013-04-30 15:28:50 -070047static const struct compress_format compressed_formats[] __initconst = {
Haesung Kima060bfe2014-12-12 16:58:08 -080048 { {0x1f, 0x8b}, "gzip", gunzip },
49 { {0x1f, 0x9e}, "gzip", gunzip },
H. Peter Anvin889c92d2009-01-08 15:14:17 -080050 { {0x42, 0x5a}, "bzip2", bunzip2 },
H. Peter Anvin889c92d2009-01-08 15:14:17 -080051 { {0x5d, 0x00}, "lzma", unlzma },
Lasse Collin3ebe1242011-01-12 17:01:23 -080052 { {0xfd, 0x37}, "xz", unxz },
Albin Tonnerrecacb2462010-01-08 14:42:46 -080053 { {0x89, 0x4c}, "lzo", unlzo },
Kyungsik Leee76e1fd2013-07-08 16:01:46 -070054 { {0x02, 0x21}, "lz4", unlz4 },
H. Peter Anvin889c92d2009-01-08 15:14:17 -080055 { {0, 0}, NULL, NULL }
56};
57
Yinghai Lud97b07c2014-08-08 14:23:14 -070058decompress_fn __init decompress_method(const unsigned char *inbuf, long len,
H. Peter Anvin889c92d2009-01-08 15:14:17 -080059 const char **name)
60{
61 const struct compress_format *cf;
62
Aneesh Kumar K.V5a09e6c2015-07-17 16:24:26 -070063 if (len < 2) {
64 if (name)
65 *name = NULL;
H. Peter Anvin889c92d2009-01-08 15:14:17 -080066 return NULL; /* Need at least this much... */
Aneesh Kumar K.V5a09e6c2015-07-17 16:24:26 -070067 }
H. Peter Anvin889c92d2009-01-08 15:14:17 -080068
Daniel M. Weeks6aa7a292014-04-07 15:39:16 -070069 pr_debug("Compressed data magic: %#.2x %#.2x\n", inbuf[0], inbuf[1]);
70
Alain Knaffe4aa7ca2009-02-19 13:36:55 -080071 for (cf = compressed_formats; cf->name; cf++) {
H. Peter Anvin889c92d2009-01-08 15:14:17 -080072 if (!memcmp(inbuf, cf->magic, 2))
73 break;
74
75 }
76 if (name)
77 *name = cf->name;
78 return cf->decompressor;
79}