blob: ab3fc90ffc6460daea218d29cdefc146923425ad [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>
Nick Terrell4963bb22020-07-30 12:08:35 -070016#include <linux/decompress/unzstd.h>
H. Peter Anvin889c92d2009-01-08 15:14:17 -080017
18#include <linux/types.h>
19#include <linux/string.h>
Hein Tibosch33e2a422012-10-04 17:16:58 -070020#include <linux/init.h>
Daniel M. Weeks6aa7a292014-04-07 15:39:16 -070021#include <linux/printk.h>
H. Peter Anvin889c92d2009-01-08 15:14:17 -080022
H. Peter Anvin23a22d52009-01-12 14:24:04 -080023#ifndef CONFIG_DECOMPRESS_GZIP
24# define gunzip NULL
25#endif
26#ifndef CONFIG_DECOMPRESS_BZIP2
27# define bunzip2 NULL
28#endif
29#ifndef CONFIG_DECOMPRESS_LZMA
30# define unlzma NULL
31#endif
Lasse Collin3ebe1242011-01-12 17:01:23 -080032#ifndef CONFIG_DECOMPRESS_XZ
33# define unxz NULL
34#endif
Albin Tonnerrecacb2462010-01-08 14:42:46 -080035#ifndef CONFIG_DECOMPRESS_LZO
36# define unlzo NULL
37#endif
Kyungsik Leee76e1fd2013-07-08 16:01:46 -070038#ifndef CONFIG_DECOMPRESS_LZ4
39# define unlz4 NULL
40#endif
Nick Terrell4963bb22020-07-30 12:08:35 -070041#ifndef CONFIG_DECOMPRESS_ZSTD
42# define unzstd NULL
43#endif
H. Peter Anvin23a22d52009-01-12 14:24:04 -080044
Hein Tibosch33e2a422012-10-04 17:16:58 -070045struct compress_format {
H. Peter Anvin889c92d2009-01-08 15:14:17 -080046 unsigned char magic[2];
47 const char *name;
48 decompress_fn decompressor;
Hein Tibosch33e2a422012-10-04 17:16:58 -070049};
50
Andi Kleen6f9982b2013-04-30 15:28:50 -070051static const struct compress_format compressed_formats[] __initconst = {
Haesung Kima060bfe2014-12-12 16:58:08 -080052 { {0x1f, 0x8b}, "gzip", gunzip },
53 { {0x1f, 0x9e}, "gzip", gunzip },
H. Peter Anvin889c92d2009-01-08 15:14:17 -080054 { {0x42, 0x5a}, "bzip2", bunzip2 },
H. Peter Anvin889c92d2009-01-08 15:14:17 -080055 { {0x5d, 0x00}, "lzma", unlzma },
Lasse Collin3ebe1242011-01-12 17:01:23 -080056 { {0xfd, 0x37}, "xz", unxz },
Albin Tonnerrecacb2462010-01-08 14:42:46 -080057 { {0x89, 0x4c}, "lzo", unlzo },
Kyungsik Leee76e1fd2013-07-08 16:01:46 -070058 { {0x02, 0x21}, "lz4", unlz4 },
Nick Terrell4963bb22020-07-30 12:08:35 -070059 { {0x28, 0xb5}, "zstd", unzstd },
H. Peter Anvin889c92d2009-01-08 15:14:17 -080060 { {0, 0}, NULL, NULL }
61};
62
Yinghai Lud97b07c2014-08-08 14:23:14 -070063decompress_fn __init decompress_method(const unsigned char *inbuf, long len,
H. Peter Anvin889c92d2009-01-08 15:14:17 -080064 const char **name)
65{
66 const struct compress_format *cf;
67
Aneesh Kumar K.V5a09e6c2015-07-17 16:24:26 -070068 if (len < 2) {
69 if (name)
70 *name = NULL;
H. Peter Anvin889c92d2009-01-08 15:14:17 -080071 return NULL; /* Need at least this much... */
Aneesh Kumar K.V5a09e6c2015-07-17 16:24:26 -070072 }
H. Peter Anvin889c92d2009-01-08 15:14:17 -080073
Daniel M. Weeks6aa7a292014-04-07 15:39:16 -070074 pr_debug("Compressed data magic: %#.2x %#.2x\n", inbuf[0], inbuf[1]);
75
Alain Knaffe4aa7ca2009-02-19 13:36:55 -080076 for (cf = compressed_formats; cf->name; cf++) {
H. Peter Anvin889c92d2009-01-08 15:14:17 -080077 if (!memcmp(inbuf, cf->magic, 2))
78 break;
79
80 }
81 if (name)
82 *name = cf->name;
83 return cf->decompressor;
84}