Decompressor.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //===-- Decompressor.h ------------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===/
  8. #ifndef LLVM_OBJECT_DECOMPRESSOR_H
  9. #define LLVM_OBJECT_DECOMPRESSOR_H
  10. #include "llvm/ADT/SmallString.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/Object/ObjectFile.h"
  13. namespace llvm {
  14. namespace object {
  15. /// Decompressor helps to handle decompression of compressed sections.
  16. class Decompressor {
  17. public:
  18. /// Create decompressor object.
  19. /// @param Name Section name.
  20. /// @param Data Section content.
  21. /// @param IsLE Flag determines if Data is in little endian form.
  22. /// @param Is64Bit Flag determines if object is 64 bit.
  23. static Expected<Decompressor> create(StringRef Name, StringRef Data,
  24. bool IsLE, bool Is64Bit);
  25. /// Resize the buffer and uncompress section data into it.
  26. /// @param Out Destination buffer.
  27. template <class T> Error resizeAndDecompress(T &Out) {
  28. Out.resize(DecompressedSize);
  29. return decompress({Out.data(), (size_t)DecompressedSize});
  30. }
  31. /// Uncompress section data to raw buffer provided.
  32. /// @param Buffer Destination buffer.
  33. Error decompress(MutableArrayRef<char> Buffer);
  34. /// Return memory buffer size required for decompression.
  35. uint64_t getDecompressedSize() { return DecompressedSize; }
  36. /// Return true if section is compressed, including gnu-styled case.
  37. static bool isCompressed(const object::SectionRef &Section);
  38. /// Return true if section is a ELF compressed one.
  39. static bool isCompressedELFSection(uint64_t Flags, StringRef Name);
  40. /// Return true if section name matches gnu style compressed one.
  41. static bool isGnuStyle(StringRef Name);
  42. private:
  43. Decompressor(StringRef Data);
  44. Error consumeCompressedGnuHeader();
  45. Error consumeCompressedZLibHeader(bool Is64Bit, bool IsLittleEndian);
  46. StringRef SectionData;
  47. uint64_t DecompressedSize;
  48. };
  49. } // end namespace object
  50. } // end namespace llvm
  51. #endif // LLVM_OBJECT_DECOMPRESSOR_H