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