14bf30831SGeorge Rimar //===-- Decompressor.h ------------------------------------------*- C++ -*-===// 24bf30831SGeorge Rimar // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 64bf30831SGeorge Rimar // 74bf30831SGeorge Rimar //===----------------------------------------------------------------------===/ 84bf30831SGeorge Rimar 94bf30831SGeorge Rimar #ifndef LLVM_OBJECT_DECOMPRESSOR_H 104bf30831SGeorge Rimar #define LLVM_OBJECT_DECOMPRESSOR_H 114bf30831SGeorge Rimar 12e72c195fSserge-sans-paille #include "llvm/ADT/ArrayRef.h" 136bda14b3SChandler Carruth #include "llvm/ADT/StringRef.h" 14*0b140d09SFangrui Song #include "llvm/Support/Compression.h" 15e72c195fSserge-sans-paille #include "llvm/Support/Error.h" 164bf30831SGeorge Rimar 174bf30831SGeorge Rimar namespace llvm { 184bf30831SGeorge Rimar namespace object { 194bf30831SGeorge Rimar 204dfcc4a7SAdrian Prantl /// Decompressor helps to handle decompression of compressed sections. 214bf30831SGeorge Rimar class Decompressor { 224bf30831SGeorge Rimar public: 234dfcc4a7SAdrian Prantl /// Create decompressor object. 244bf30831SGeorge Rimar /// @param Name Section name. 254bf30831SGeorge Rimar /// @param Data Section content. 264bf30831SGeorge Rimar /// @param IsLE Flag determines if Data is in little endian form. 27fef77a43SSimon Pilgrim /// @param Is64Bit Flag determines if object is 64 bit. 284bf30831SGeorge Rimar static Expected<Decompressor> create(StringRef Name, StringRef Data, 294bf30831SGeorge Rimar bool IsLE, bool Is64Bit); 304bf30831SGeorge Rimar 314dfcc4a7SAdrian Prantl /// Resize the buffer and uncompress section data into it. 324bf30831SGeorge Rimar /// @param Out Destination buffer. resizeAndDecompress(T & Out)33d2aaf5bdSGeorge Rimar template <class T> Error resizeAndDecompress(T &Out) { 34f98b9ac5SGeorge Rimar Out.resize(DecompressedSize); 35e690137dSFangrui Song return decompress({(uint8_t *)Out.data(), (size_t)DecompressedSize}); 36f98b9ac5SGeorge Rimar } 374bf30831SGeorge Rimar 384dfcc4a7SAdrian Prantl /// Uncompress section data to raw buffer provided. 39*0b140d09SFangrui Song Error decompress(MutableArrayRef<uint8_t> Output); 404bf30831SGeorge Rimar 414dfcc4a7SAdrian Prantl /// Return memory buffer size required for decompression. getDecompressedSize()424bf30831SGeorge Rimar uint64_t getDecompressedSize() { return DecompressedSize; } 434bf30831SGeorge Rimar 444bf30831SGeorge Rimar private: 453ae41704SVedant Kumar Decompressor(StringRef Data); 464bf30831SGeorge Rimar 47*0b140d09SFangrui Song Error consumeCompressedHeader(bool Is64Bit, bool IsLittleEndian); 484bf30831SGeorge Rimar 494bf30831SGeorge Rimar StringRef SectionData; 504bf30831SGeorge Rimar uint64_t DecompressedSize; 51*0b140d09SFangrui Song DebugCompressionType CompressionType = DebugCompressionType::None; 524bf30831SGeorge Rimar }; 534bf30831SGeorge Rimar 544bf30831SGeorge Rimar } // end namespace object 554bf30831SGeorge Rimar } // end namespace llvm 564bf30831SGeorge Rimar 574bf30831SGeorge Rimar #endif // LLVM_OBJECT_DECOMPRESSOR_H 58