[llvm-dwp] - Reuse object::Decompressor class

One more place where Decompressor class can be reused.

Differential revision: https://reviews.llvm.org/D28679

llvm-svn: 291906
diff --git a/llvm/test/tools/llvm-dwp/X86/compressfail.test b/llvm/test/tools/llvm-dwp/X86/compressfail.test
index 78b6255..b1961e8 100644
--- a/llvm/test/tools/llvm-dwp/X86/compressfail.test
+++ b/llvm/test/tools/llvm-dwp/X86/compressfail.test
@@ -4,4 +4,4 @@
 
 REQUIRES: zlib
 
-CHECK: error: failure while decompressing compressed section: 'zdebug_{{.*}}.dwo'
+CHECK: error: failure while decompressing compressed section: '.zdebug_{{.*}}.dwo'
diff --git a/llvm/test/tools/llvm-dwp/X86/nocompress.test b/llvm/test/tools/llvm-dwp/X86/nocompress.test
index 1de9444..c3df6b3 100644
--- a/llvm/test/tools/llvm-dwp/X86/nocompress.test
+++ b/llvm/test/tools/llvm-dwp/X86/nocompress.test
@@ -2,4 +2,4 @@
 
 REQUIRES: nozlib
 
-CHECK: error: zlib not available
+CHECK: error: failure while decompressing compressed section: '.zdebug_{{.*}}.dwo', zlib is not available
diff --git a/llvm/tools/llvm-dwp/llvm-dwp.cpp b/llvm/tools/llvm-dwp/llvm-dwp.cpp
index 7418c77..0c4af75 100644
--- a/llvm/tools/llvm-dwp/llvm-dwp.cpp
+++ b/llvm/tools/llvm-dwp/llvm-dwp.cpp
@@ -27,6 +27,7 @@
 #include "llvm/MC/MCSectionELF.h"
 #include "llvm/MC/MCStreamer.h"
 #include "llvm/MC/MCTargetOptionsCommandFlags.h"
+#include "llvm/Object/Decompressor.h"
 #include "llvm/Object/ObjectFile.h"
 #include "llvm/Support/Compression.h"
 #include "llvm/Support/DataExtractor.h"
@@ -334,21 +335,6 @@
   writeIndexTable(Out, ContributionOffsets, IndexEntries,
                   &DWARFUnitIndex::Entry::SectionContribution::Length);
 }
-static bool consumeCompressedDebugSectionHeader(StringRef &data,
-                                                uint64_t &OriginalSize) {
-  // Consume "ZLIB" prefix.
-  if (!data.startswith("ZLIB"))
-    return false;
-  data = data.substr(4);
-  // Consume uncompressed section size (big-endian 8 bytes).
-  DataExtractor extractor(data, false, 8);
-  uint32_t Offset = 0;
-  OriginalSize = extractor.getU64(&Offset);
-  if (Offset == 0)
-    return false;
-  data = data.substr(Offset);
-  return true;
-}
 
 std::string buildDWODescription(StringRef Name, StringRef DWPName, StringRef DWOName) {
   std::string Text = "\'";
@@ -368,22 +354,29 @@
   return Text;
 }
 
-static Error handleCompressedSection(
-    std::deque<SmallString<32>> &UncompressedSections, StringRef &Name,
-    StringRef &Contents) {
-  if (!Name.startswith("zdebug_"))
+static Error createError(StringRef Name, Error E) {
+  return make_error<DWPError>(
+      ("failure while decompressing compressed section: '" + Name + "', " +
+       llvm::toString(std::move(E)))
+          .str());
+}
+
+static Error
+handleCompressedSection(std::deque<SmallString<32>> &UncompressedSections,
+                        StringRef &Name, StringRef &Contents) {
+  if (!Decompressor::isGnuStyle(Name))
     return Error::success();
+
+  Expected<Decompressor> Dec =
+      Decompressor::create(Name, Contents, false /*IsLE*/, false /*Is64Bit*/);
+  if (!Dec)
+    return createError(Name, Dec.takeError());
+
   UncompressedSections.emplace_back();
-  uint64_t OriginalSize;
-  if (!zlib::isAvailable())
-    return make_error<DWPError>("zlib not available");
-  if (!consumeCompressedDebugSectionHeader(Contents, OriginalSize) ||
-      zlib::uncompress(Contents, UncompressedSections.back(), OriginalSize) !=
-          zlib::StatusOK)
-    return make_error<DWPError>(
-        ("failure while decompressing compressed section: '" + Name + "\'")
-            .str());
-  Name = Name.substr(1);
+  if (Error E = Dec->decompress(UncompressedSections.back()))
+    return createError(Name, std::move(E));
+
+  Name = Name.substr(2); // Drop ".z"
   Contents = UncompressedSections.back();
   return Error::success();
 }
@@ -409,8 +402,6 @@
   if (std::error_code Err = Section.getName(Name))
     return errorCodeToError(Err);
 
-  Name = Name.substr(Name.find_first_not_of("._"));
-
   StringRef Contents;
   if (auto Err = Section.getContents(Contents))
     return errorCodeToError(Err);
@@ -418,6 +409,8 @@
   if (auto Err = handleCompressedSection(UncompressedSections, Name, Contents))
     return Err;
 
+  Name = Name.substr(Name.find_first_not_of("._"));
+
   auto SectionPair = KnownSections.find(Name);
   if (SectionPair == KnownSections.end())
     return Error::success();