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/Error.h"
15 
16 namespace llvm {
17 namespace object {
18 
19 /// Decompressor helps to handle decompression of compressed sections.
20 class Decompressor {
21 public:
22   /// Create decompressor object.
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 
30   /// Resize the buffer and uncompress section data into it.
31   /// @param Out         Destination buffer.
32   template <class T> Error resizeAndDecompress(T &Out) {
33     Out.resize(DecompressedSize);
34     return decompress({(uint8_t *)Out.data(), (size_t)DecompressedSize});
35   }
36 
37   /// Uncompress section data to raw buffer provided.
38   /// @param Buffer      Destination buffer.
39   Error decompress(MutableArrayRef<uint8_t> Buffer);
40 
41   /// Return memory buffer size required for decompression.
42   uint64_t getDecompressedSize() { return DecompressedSize; }
43 
44 private:
45   Decompressor(StringRef Data);
46 
47   Error consumeCompressedZLibHeader(bool Is64Bit, bool IsLittleEndian);
48 
49   StringRef SectionData;
50   uint64_t DecompressedSize;
51 };
52 
53 } // end namespace object
54 } // end namespace llvm
55 
56 #endif // LLVM_OBJECT_DECOMPRESSOR_H
57