retdec
macho_upx_stub.h
Go to the documentation of this file.
1 
7 #ifndef UNPACKERTOOL_PLUGINS_UPX_MACHO_MACHO_UPX_STUB_H
8 #define UNPACKERTOOL_PLUGINS_UPX_MACHO_MACHO_UPX_STUB_H
9 
10 #include <cstdint>
11 
15 
16 using namespace retdec::utils;
17 
18 namespace retdec {
19 namespace unpackertool {
20 namespace upx {
21 
25 template <int /*bits*/> struct MachOUpxStubTraits {};
26 
30 template <> struct MachOUpxStubTraits<32>
31 {
32  using AddressType = std::uint32_t;
33  using MachOHeaderType = llvm::MachO::mach_header;
34  using MachOSegmentCommandType = llvm::MachO::segment_command;
35 
36  static const std::uint64_t MachOHeaderNcmdsOffset = 0x10;
37  static const std::uint64_t MachOHeaderSize = 0x1C;
38  static const std::uint32_t LoadCommandSegment = llvm::MachO::LC_SEGMENT;
39  static const std::uint64_t LoadCommandSegmentFileoffOffset = 0x20;
40  static const std::uint64_t LoadCommandSegmentFilesizeOffset = 0x24;
41 };
42 
46 template <> struct MachOUpxStubTraits<64>
47 {
48  using AddressType = std::uint64_t;
49  using MachOHeaderType = llvm::MachO::mach_header_64;
50  using MachOSegmentCommandType = llvm::MachO::segment_command_64;
51 
52  static const std::uint64_t MachOHeaderNcmdsOffset = 0x10;
53  static const std::uint64_t MachOHeaderSize = 0x20;
54  static const std::uint32_t LoadCommandSegment = llvm::MachO::LC_SEGMENT_64;
55  static const std::uint64_t LoadCommandSegmentFileoffOffset = 0x28;
56  static const std::uint64_t LoadCommandSegmentFilesizeOffset = 0x30;
57 };
58 
65 template <int bits> class MachOUpxStub : public UpxStub
66 {
67 public:
71 
72  MachOUpxStub(retdec::loader::Image* inputFile, const UpxStubData* stubData, const DynamicBuffer& stubCapturedData,
73  std::unique_ptr<Decompressor> decompressor, const UpxMetadata& metadata);
74 
75  virtual void unpack(const std::string& outputFile) override;
76  virtual void cleanup() override;
77 
78  void setupPackingMethod(std::uint8_t packingMethod);
79  void decompress(DynamicBuffer& packedData, DynamicBuffer& unpackedData);
80 
81  void unpack(std::ifstream& inputFile, std::ofstream& outputFile, std::uint64_t baseInputOffset, std::uint64_t baseOutputOffset);
82 
83 protected:
84  std::uint32_t getFirstBlockOffset(std::ifstream& inputFile) const;
85  DynamicBuffer readNextBlock(std::ifstream& inputFile);
86  DynamicBuffer unpackBlock(DynamicBuffer& packedBlock);
87  void unfilterBlock(const DynamicBuffer& packedBlock, DynamicBuffer& unpackedData);
88 
89 private:
90  std::uint64_t _readPos;
91  std::unique_ptr<retdec::unpacker::BitParser> _bitParser;
92 };
93 
94 } // namespace upx
95 } // namespace unpackertool
96 } // namespace retdec
97 
98 #endif
Bit getters for NRV decompression algorithms.
Definition: image.h:22
Definition: macho_upx_stub.h:66
typename MachOUpxStubTraits< bits >::AddressType AddressType
Definition: macho_upx_stub.h:68
typename MachOUpxStubTraits< bits >::MachOSegmentCommandType MachOSegmentCommandType
Definition: macho_upx_stub.h:70
std::unique_ptr< retdec::unpacker::BitParser > _bitParser
Definition: macho_upx_stub.h:91
std::uint64_t _readPos
Definition: macho_upx_stub.h:90
typename MachOUpxStubTraits< bits >::MachOHeaderType MachOHeaderType
Definition: macho_upx_stub.h:69
Definition: upx_stub.h:111
The class for dynamic buffered data manipulation taking the endianness of the data in account.
Definition: dynamic_buffer.h:36
Declaration of class for buffered data mainpulation.
Definition: unpacking_stub.h:16
Definition: archive_wrapper.h:19
void cleanup(ProgramOptions &po)
Definition: retdec-decompiler.cpp:910
llvm::MachO::segment_command MachOSegmentCommandType
Definition: macho_upx_stub.h:34
llvm::MachO::mach_header MachOHeaderType
Definition: macho_upx_stub.h:33
std::uint32_t AddressType
Type with default word size.
Definition: macho_upx_stub.h:32
llvm::MachO::segment_command_64 MachOSegmentCommandType
Definition: macho_upx_stub.h:50
llvm::MachO::mach_header_64 MachOHeaderType
Definition: macho_upx_stub.h:49
std::uint64_t AddressType
Type with default word size.
Definition: macho_upx_stub.h:48
Definition: macho_upx_stub.h:25
Definition: upx_stub_signatures.h:25
Declaration of abstract UPX stub class that represents the unpacking procedure itself.