decompress.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * decompress.c
  3. *
  4. * Detect the decompression method based on magic number
  5. */
  6. #include <linux/decompress/generic.h>
  7. #include <linux/decompress/bunzip2.h>
  8. #include <linux/decompress/unlzma.h>
  9. #include <linux/decompress/unxz.h>
  10. #include <linux/decompress/inflate.h>
  11. #include <linux/decompress/unlzo.h>
  12. #include <linux/decompress/unlz4.h>
  13. #include <linux/types.h>
  14. #include <linux/string.h>
  15. #include <linux/init.h>
  16. #include <linux/printk.h>
  17. #ifndef CONFIG_DECOMPRESS_GZIP
  18. # define gunzip NULL
  19. #endif
  20. #ifndef CONFIG_DECOMPRESS_BZIP2
  21. # define bunzip2 NULL
  22. #endif
  23. #ifndef CONFIG_DECOMPRESS_LZMA
  24. # define unlzma NULL
  25. #endif
  26. #ifndef CONFIG_DECOMPRESS_XZ
  27. # define unxz NULL
  28. #endif
  29. #ifndef CONFIG_DECOMPRESS_LZO
  30. # define unlzo NULL
  31. #endif
  32. #ifndef CONFIG_DECOMPRESS_LZ4
  33. # define unlz4 NULL
  34. #endif
  35. struct compress_format {
  36. unsigned char magic[2];
  37. const char *name;
  38. decompress_fn decompressor;
  39. };
  40. static const struct compress_format compressed_formats[] __initconst = {
  41. { {0x1f, 0x8b}, "gzip", gunzip },
  42. { {0x1f, 0x9e}, "gzip", gunzip },
  43. { {0x42, 0x5a}, "bzip2", bunzip2 },
  44. { {0x5d, 0x00}, "lzma", unlzma },
  45. { {0xfd, 0x37}, "xz", unxz },
  46. { {0x89, 0x4c}, "lzo", unlzo },
  47. { {0x02, 0x21}, "lz4", unlz4 },
  48. { {0, 0}, NULL, NULL }
  49. };
  50. decompress_fn __init decompress_method(const unsigned char *inbuf, long len,
  51. const char **name)
  52. {
  53. const struct compress_format *cf;
  54. if (len < 2) {
  55. if (name)
  56. *name = NULL;
  57. return NULL; /* Need at least this much... */
  58. }
  59. pr_debug("Compressed data magic: %#.2x %#.2x\n", inbuf[0], inbuf[1]);
  60. for (cf = compressed_formats; cf->name; cf++) {
  61. if (!memcmp(inbuf, cf->magic, 2))
  62. break;
  63. }
  64. if (name)
  65. *name = cf->name;
  66. return cf->decompressor;
  67. }