cherry-pick.FILE5_37-54-g119cc185.add-lzma-decompression-support.patch 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. Subject: Add lzma decompression support
  2. Origin: FILE5_37-54-g119cc185 <https://github.com/file/file/commit/FILE5_37-54-g119cc185>
  3. Upstream-Author: Christos Zoulas <christos@zoulas.com>
  4. Date: Thu Jul 18 15:43:54 2019 +0000
  5. --- a/src/compress.c
  6. +++ b/src/compress.c
  7. @@ -118,6 +118,16 @@
  8. }
  9. #endif
  10. +static int
  11. +lzmacmp(const unsigned char *buf)
  12. +{
  13. + if (buf[0] != 0x5d || buf[1] || buf[2])
  14. + return 0;
  15. + if (buf[12] && buf[12] != 0xff)
  16. + return 0;
  17. + return 1;
  18. +}
  19. +
  20. #define gzip_flags "-cd"
  21. #define lrzip_flags "-do"
  22. #define lzip_flags gzip_flags
  23. @@ -152,29 +162,30 @@
  24. private const struct {
  25. const void *magic;
  26. - size_t maglen;
  27. + int maglen;
  28. const char **argv;
  29. void *unused;
  30. } compr[] = {
  31. - { "\037\235", 2, gzip_args, NULL }, /* compressed */
  32. + { "\037\235", 2, gzip_args, NULL }, /* 0, compressed */
  33. /* Uncompress can get stuck; so use gzip first if we have it
  34. * Idea from Damien Clark, thanks! */
  35. - { "\037\235", 2, uncompress_args, NULL }, /* compressed */
  36. - { "\037\213", 2, gzip_args, do_zlib }, /* gzipped */
  37. - { "\037\236", 2, gzip_args, NULL }, /* frozen */
  38. - { "\037\240", 2, gzip_args, NULL }, /* SCO LZH */
  39. + { "\037\235", 2, uncompress_args, NULL }, /* 1, compressed */
  40. + { "\037\213", 2, gzip_args, do_zlib }, /* 2, gzipped */
  41. + { "\037\236", 2, gzip_args, NULL }, /* 3, frozen */
  42. + { "\037\240", 2, gzip_args, NULL }, /* 4, SCO LZH */
  43. /* the standard pack utilities do not accept standard input */
  44. - { "\037\036", 2, gzip_args, NULL }, /* packed */
  45. - { "PK\3\4", 4, gzip_args, NULL }, /* pkzipped, */
  46. + { "\037\036", 2, gzip_args, NULL }, /* 5, packed */
  47. + { "PK\3\4", 4, gzip_args, NULL }, /* 6, pkzipped, */
  48. /* ...only first file examined */
  49. - { "BZh", 3, bzip2_args, do_bzlib }, /* bzip2-ed */
  50. - { "LZIP", 4, lzip_args, NULL }, /* lzip-ed */
  51. - { "\3757zXZ\0", 6, xz_args, NULL }, /* XZ Utils */
  52. - { "LRZI", 4, lrzip_args, NULL }, /* LRZIP */
  53. - { "\004\"M\030",4, lz4_args, NULL }, /* LZ4 */
  54. - { "\x28\xB5\x2F\xFD", 4, zstd_args, NULL }, /* zstd */
  55. + { "BZh", 3, bzip2_args, do_bzlib }, /* 7, bzip2-ed */
  56. + { "LZIP", 4, lzip_args, NULL }, /* 8, lzip-ed */
  57. + { "\3757zXZ\0", 6, xz_args, NULL }, /* 9, XZ Utils */
  58. + { "LRZI", 4, lrzip_args, NULL }, /* 10, LRZIP */
  59. + { "\004\"M\030",4, lz4_args, NULL }, /* 11, LZ4 */
  60. + { "\x28\xB5\x2F\xFD", 4, zstd_args, NULL }, /* 12, zstd */
  61. + { RCAST(const void *, lzmacmp), -13, xz_args, NULL }, /* 13, lzma */
  62. #ifdef ZLIBSUPPORT
  63. - { RCAST(const void *, zlibcmp), 0, zlib_args, NULL }, /* zlib */
  64. + { RCAST(const void *, zlibcmp), -2, zlib_args, NULL }, /* 14, zlib */
  65. #endif
  66. };
  67. @@ -243,15 +254,15 @@
  68. for (i = 0; i < ncompr; i++) {
  69. int zm;
  70. - if (nbytes < compr[i].maglen)
  71. + if (nbytes < CAST(size_t, abs(compr[i].maglen)))
  72. continue;
  73. -#ifdef ZLIBSUPPORT
  74. - if (compr[i].maglen == 0)
  75. + if (compr[i].maglen < 0) {
  76. zm = (RCAST(int (*)(const unsigned char *),
  77. CCAST(void *, compr[i].magic)))(buf);
  78. - else
  79. -#endif
  80. - zm = memcmp(buf, compr[i].magic, compr[i].maglen) == 0;
  81. + } else {
  82. + zm = memcmp(buf, compr[i].magic,
  83. + CAST(size_t, compr[i].maglen)) == 0;
  84. + }
  85. if (!zm)
  86. continue;