decompress.c 1.3 KB

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