decompress.c 1.5 KB

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