Back to home page

LXR

 
 

    


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