Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * decompress.c
0004  *
0005  * Detect the decompression method based on magic number
0006  */
0007 
0008 #include <linux/decompress/generic.h>
0009 
0010 #include <linux/decompress/bunzip2.h>
0011 #include <linux/decompress/unlzma.h>
0012 #include <linux/decompress/unxz.h>
0013 #include <linux/decompress/inflate.h>
0014 #include <linux/decompress/unlzo.h>
0015 #include <linux/decompress/unlz4.h>
0016 #include <linux/decompress/unzstd.h>
0017 
0018 #include <linux/types.h>
0019 #include <linux/string.h>
0020 #include <linux/init.h>
0021 #include <linux/printk.h>
0022 
0023 #ifndef CONFIG_DECOMPRESS_GZIP
0024 # define gunzip NULL
0025 #endif
0026 #ifndef CONFIG_DECOMPRESS_BZIP2
0027 # define bunzip2 NULL
0028 #endif
0029 #ifndef CONFIG_DECOMPRESS_LZMA
0030 # define unlzma NULL
0031 #endif
0032 #ifndef CONFIG_DECOMPRESS_XZ
0033 # define unxz NULL
0034 #endif
0035 #ifndef CONFIG_DECOMPRESS_LZO
0036 # define unlzo NULL
0037 #endif
0038 #ifndef CONFIG_DECOMPRESS_LZ4
0039 # define unlz4 NULL
0040 #endif
0041 #ifndef CONFIG_DECOMPRESS_ZSTD
0042 # define unzstd NULL
0043 #endif
0044 
0045 struct compress_format {
0046     unsigned char magic[2];
0047     const char *name;
0048     decompress_fn decompressor;
0049 };
0050 
0051 static const struct compress_format compressed_formats[] __initconst = {
0052     { {0x1f, 0x8b}, "gzip", gunzip },
0053     { {0x1f, 0x9e}, "gzip", gunzip },
0054     { {0x42, 0x5a}, "bzip2", bunzip2 },
0055     { {0x5d, 0x00}, "lzma", unlzma },
0056     { {0xfd, 0x37}, "xz", unxz },
0057     { {0x89, 0x4c}, "lzo", unlzo },
0058     { {0x02, 0x21}, "lz4", unlz4 },
0059     { {0x28, 0xb5}, "zstd", unzstd },
0060     { {0, 0}, NULL, NULL }
0061 };
0062 
0063 decompress_fn __init decompress_method(const unsigned char *inbuf, long len,
0064                 const char **name)
0065 {
0066     const struct compress_format *cf;
0067 
0068     if (len < 2) {
0069         if (name)
0070             *name = NULL;
0071         return NULL;    /* Need at least this much... */
0072     }
0073 
0074     pr_debug("Compressed data magic: %#.2x %#.2x\n", inbuf[0], inbuf[1]);
0075 
0076     for (cf = compressed_formats; cf->name; cf++) {
0077         if (!memcmp(inbuf, cf->magic, 2))
0078             break;
0079 
0080     }
0081     if (name)
0082         *name = cf->name;
0083     return cf->decompressor;
0084 }