Decompressor.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- Decompressor.h ------------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===/
  13. #ifndef LLVM_OBJECT_DECOMPRESSOR_H
  14. #define LLVM_OBJECT_DECOMPRESSOR_H
  15. #include "llvm/ADT/SmallString.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/Object/ObjectFile.h"
  18. namespace llvm {
  19. namespace object {
  20. /// Decompressor helps to handle decompression of compressed sections.
  21. class Decompressor {
  22. public:
  23. /// Create decompressor object.
  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. /// 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({Out.data(), (size_t)DecompressedSize});
  35. }
  36. /// Uncompress section data to raw buffer provided.
  37. /// @param Buffer Destination buffer.
  38. Error decompress(MutableArrayRef<char> Buffer);
  39. /// Return memory buffer size required for decompression.
  40. uint64_t getDecompressedSize() { return DecompressedSize; }
  41. /// Return true if section is compressed, including gnu-styled case.
  42. static bool isCompressed(const object::SectionRef &Section);
  43. /// Return true if section is a ELF compressed one.
  44. static bool isCompressedELFSection(uint64_t Flags, StringRef Name);
  45. /// Return true if section name matches gnu style compressed one.
  46. static bool isGnuStyle(StringRef Name);
  47. private:
  48. Decompressor(StringRef Data);
  49. Error consumeCompressedGnuHeader();
  50. Error consumeCompressedZLibHeader(bool Is64Bit, bool IsLittleEndian);
  51. StringRef SectionData;
  52. uint64_t DecompressedSize;
  53. };
  54. } // end namespace object
  55. } // end namespace llvm
  56. #endif // LLVM_OBJECT_DECOMPRESSOR_H
  57. #ifdef __GNUC__
  58. #pragma GCC diagnostic pop
  59. #endif