decompress.c 1.1 KB

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