decompress.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #ifndef CONFIG_DECOMPRESS_GZIP
  17. # define gunzip NULL
  18. #endif
  19. #ifndef CONFIG_DECOMPRESS_BZIP2
  20. # define bunzip2 NULL
  21. #endif
  22. #ifndef CONFIG_DECOMPRESS_LZMA
  23. # define unlzma NULL
  24. #endif
  25. #ifndef CONFIG_DECOMPRESS_XZ
  26. # define unxz NULL
  27. #endif
  28. #ifndef CONFIG_DECOMPRESS_LZO
  29. # define unlzo NULL
  30. #endif
  31. #ifndef CONFIG_DECOMPRESS_LZ4
  32. # define unlz4 NULL
  33. #endif
  34. struct compress_format {
  35. unsigned char magic[2];
  36. const char *name;
  37. decompress_fn decompressor;
  38. };
  39. static const struct compress_format compressed_formats[] __initconst = {
  40. { {037, 0213}, "gzip", gunzip },
  41. { {037, 0236}, "gzip", gunzip },
  42. { {0x42, 0x5a}, "bzip2", bunzip2 },
  43. { {0x5d, 0x00}, "lzma", unlzma },
  44. { {0xfd, 0x37}, "xz", unxz },
  45. { {0x89, 0x4c}, "lzo", unlzo },
  46. { {0x02, 0x21}, "lz4", unlz4 },
  47. { {0, 0}, NULL, NULL }
  48. };
  49. decompress_fn __init decompress_method(const unsigned char *inbuf, int len,
  50. const char **name)
  51. {
  52. const struct compress_format *cf;
  53. if (len < 2)
  54. return NULL; /* Need at least this much... */
  55. for (cf = compressed_formats; cf->name; cf++) {
  56. if (!memcmp(inbuf, cf->magic, 2))
  57. break;
  58. }
  59. if (name)
  60. *name = cf->name;
  61. return cf->decompressor;
  62. }