decompress.c 1.2 KB

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