xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Object/Decompressor.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 
9 #ifndef LLVM_OBJECT_DECOMPRESSOR_H
10 #define LLVM_OBJECT_DECOMPRESSOR_H
11 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/Compiler.h"
15 #include "llvm/Support/Compression.h"
16 #include "llvm/Support/Error.h"
17 
18 namespace llvm {
19 namespace object {
20 
21 /// Decompressor helps to handle decompression of compressed sections.
22 class Decompressor {
23 public:
24   /// Create decompressor object.
25   /// @param Name        Section name.
26   /// @param Data        Section content.
27   /// @param IsLE        Flag determines if Data is in little endian form.
28   /// @param Is64Bit     Flag determines if object is 64 bit.
29   LLVM_ABI static Expected<Decompressor> create(StringRef Name, StringRef Data,
30                                                 bool IsLE, bool Is64Bit);
31 
32   /// Resize the buffer and uncompress section data into it.
33   /// @param Out         Destination buffer.
resizeAndDecompress(T & Out)34   template <class T> Error resizeAndDecompress(T &Out) {
35     Out.resize(DecompressedSize);
36     return decompress({(uint8_t *)Out.data(), (size_t)DecompressedSize});
37   }
38 
39   /// Uncompress section data to raw buffer provided.
40   LLVM_ABI Error decompress(MutableArrayRef<uint8_t> Output);
41 
42   /// Return memory buffer size required for decompression.
getDecompressedSize()43   uint64_t getDecompressedSize() { return DecompressedSize; }
44 
45 private:
46   Decompressor(StringRef Data);
47 
48   Error consumeCompressedHeader(bool Is64Bit, bool IsLittleEndian);
49 
50   StringRef SectionData;
51   uint64_t DecompressedSize;
52   DebugCompressionType CompressionType = DebugCompressionType::None;
53 };
54 
55 } // end namespace object
56 } // end namespace llvm
57 
58 #endif // LLVM_OBJECT_DECOMPRESSOR_H
59