Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===-- Decompressor.h ------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===/ |
| 9 | |
| 10 | #ifndef LLVM_OBJECT_DECOMPRESSOR_H |
| 11 | #define LLVM_OBJECT_DECOMPRESSOR_H |
| 12 | |
| 13 | #include "llvm/ADT/SmallString.h" |
| 14 | #include "llvm/ADT/StringRef.h" |
| 15 | #include "llvm/Object/ObjectFile.h" |
| 16 | |
| 17 | namespace llvm { |
| 18 | namespace object { |
| 19 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 20 | /// Decompressor helps to handle decompression of compressed sections. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 21 | class Decompressor { |
| 22 | public: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 23 | /// Create decompressor object. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 24 | /// @param Name Section name. |
| 25 | /// @param Data Section content. |
| 26 | /// @param IsLE Flag determines if Data is in little endian form. |
| 27 | /// @param Is64Bit Flag determines if object is 64 bit. |
| 28 | static Expected<Decompressor> create(StringRef Name, StringRef Data, |
| 29 | bool IsLE, bool Is64Bit); |
| 30 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 31 | /// Resize the buffer and uncompress section data into it. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 32 | /// @param Out Destination buffer. |
| 33 | template <class T> Error resizeAndDecompress(T &Out) { |
| 34 | Out.resize(DecompressedSize); |
| 35 | return decompress({Out.data(), (size_t)DecompressedSize}); |
| 36 | } |
| 37 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 38 | /// Uncompress section data to raw buffer provided. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 39 | /// @param Buffer Destination buffer. |
| 40 | Error decompress(MutableArrayRef<char> Buffer); |
| 41 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 42 | /// Return memory buffer size required for decompression. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 43 | uint64_t getDecompressedSize() { return DecompressedSize; } |
| 44 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 45 | /// Return true if section is compressed, including gnu-styled case. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 46 | static bool isCompressed(const object::SectionRef &Section); |
| 47 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 48 | /// Return true if section is a ELF compressed one. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 49 | static bool isCompressedELFSection(uint64_t Flags, StringRef Name); |
| 50 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 51 | /// Return true if section name matches gnu style compressed one. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 52 | static bool isGnuStyle(StringRef Name); |
| 53 | |
| 54 | private: |
| 55 | Decompressor(StringRef Data); |
| 56 | |
| 57 | Error consumeCompressedGnuHeader(); |
| 58 | Error consumeCompressedZLibHeader(bool Is64Bit, bool IsLittleEndian); |
| 59 | |
| 60 | StringRef SectionData; |
| 61 | uint64_t DecompressedSize; |
| 62 | }; |
| 63 | |
| 64 | } // end namespace object |
| 65 | } // end namespace llvm |
| 66 | |
| 67 | #endif // LLVM_OBJECT_DECOMPRESSOR_H |