decompress.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #include <linux/init.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. struct compress_format {
  31. unsigned char magic[2];
  32. const char *name;
  33. decompress_fn decompressor;
  34. };
  35. static const struct compress_format compressed_formats[] __initdata = {
  36. { {037, 0213}, "gzip", gunzip },
  37. { {037, 0236}, "gzip", gunzip },
  38. { {0x42, 0x5a}, "bzip2", bunzip2 },
  39. { {0x5d, 0x00}, "lzma", unlzma },
  40. { {0xfd, 0x37}, "xz", unxz },
  41. { {0x89, 0x4c}, "lzo", unlzo },
  42. { {0, 0}, NULL, NULL }
  43. };
  44. decompress_fn __init decompress_method(const unsigned char *inbuf, int len,
  45. const char **name)
  46. {
  47. const struct compress_format *cf;
  48. if (len < 2)
  49. return NULL; /* Need at least this much... */
  50. for (cf = compressed_formats; cf->name; cf++) {
  51. if (!memcmp(inbuf, cf->magic, 2))
  52. break;
  53. }
  54. if (name)
  55. *name = cf->name;
  56. return cf->decompressor;
  57. }