retdec
plugin.h
Go to the documentation of this file.
1 
7 #ifndef RETDEC_UNPACKER_PLUGIN_H
8 #define RETDEC_UNPACKER_PLUGIN_H
9 
10 #include <memory>
11 #include <sstream>
12 #include <string>
13 
14 #include "retdec/utils/io/log.h"
16 
17 #define plugin(T) retdec::unpackertool::Plugin::instance<T>()
18 
19 using namespace retdec::utils::io;
20 
21 namespace retdec {
22 namespace unpackertool {
23 
28 {
32 };
33 
50 class Plugin
51 {
52 public:
59  struct Info
60  {
61  Info() : name(""), pluginVersion(""), packerVersion(""), author("") {}
62 
68  bool isUninitialized() const
69  {
70  return (name == "") || (pluginVersion == "") || (packerVersion == "") || (author == "");
71  }
72 
73  std::string name;
74  std::string pluginVersion;
75  std::string packerVersion;
76  std::string author;
77  };
78 
83  struct Arguments
84  {
85  std::string inputFile;
86  std::string outputFile;
87  bool brute;
88  };
89 
90  virtual ~Plugin() = default;
91 
97  const Plugin::Info* getInfo() const
98  {
99  return &info;
100  }
101 
108  {
109  return &startupArgs;
110  }
111 
120  {
121  // Check whether we have cached exit code
122  if (_cachedExitCode != PLUGIN_EXIT_UNPACKED)
123  {
124  log("Exiting with cached exit code ", _cachedExitCode);
125  return _cachedExitCode;
126  }
127 
128  _cachedExitCode = PLUGIN_EXIT_UNPACKED;
129  startupArgs = args;
130 
131  try
132  {
133  prepare();
134  unpack();
135  }
136  catch (const retdec::unpacker::FatalException& ex)
137  {
138  error(ex.getMessage());
139  _cachedExitCode = PLUGIN_EXIT_FAILED;
140  }
142  {
143  error(ex.getMessage());
144  _cachedExitCode = PLUGIN_EXIT_UNSUPPORTED;
145  }
146 
147  cleanup();
148  return _cachedExitCode;
149  }
150 
154  virtual void prepare() = 0;
155 
159  virtual void unpack() = 0;
160 
164  virtual void cleanup() = 0;
165 
174  template <typename... Args> void log(const Args&... args)
175  {
176  Plugin::logImpl(Log::get(Log::Type::Info), "[", getInfo()->name, "] ", args...);
177  }
178 
187  template <typename... Args> void error(const Args&... args)
188  {
189  Plugin::logImpl(Log::get(Log::Type::Error), "[ERROR] [", getInfo()->name, "] ", args...);
190  }
191 
198  template <typename T>
199  static T* instance()
200  {
201  static std::unique_ptr<T> pluginInstance = std::make_unique<T>();
202  return pluginInstance.get();
203  }
204 
205 protected:
206  Plugin() : _cachedExitCode(PLUGIN_EXIT_UNPACKED) {}
207  Plugin(const Plugin&);
208  Plugin& operator =(const Plugin&);
209 
212 
213 private:
215 
216  template <typename T, typename... Args> static void logImpl(Logger& out, const T& data, const Args&... args)
217  {
218  out << data;
219  logImpl(out, args...);
220  }
221 
222  static void logImpl(Logger& out)
223  {
224  out << std::endl;
225  }
226 };
227 
228 } // namespace unpackertool
229 } // namespace retdec
230 
231 #endif
Definition: unpacker_exception.h:79
const std::string & getMessage() const noexcept
Definition: unpacker_exception.h:47
Definition: unpacker_exception.h:88
The abstract base of unpacking plugin.
Definition: plugin.h:51
Plugin()
Definition: plugin.h:206
PluginExitCode _cachedExitCode
Cached exit code of the plugin for the unpacked file.
Definition: plugin.h:214
const Plugin::Info * getInfo() const
Definition: plugin.h:97
static void logImpl(Logger &out, const T &data, const Args &... args)
Definition: plugin.h:216
void log(const Args &... args)
Definition: plugin.h:174
const Plugin::Arguments * getStartupArguments() const
Definition: plugin.h:107
static T * instance()
Definition: plugin.h:199
Plugin::Info info
The static info of the plugin.
Definition: plugin.h:210
virtual ~Plugin()=default
void error(const Args &... args)
Definition: plugin.h:187
Plugin::Arguments startupArgs
Startup arguments of the plugin.
Definition: plugin.h:211
static void logImpl(Logger &out)
Definition: plugin.h:222
PluginExitCode run(const Plugin::Arguments &args)
Definition: plugin.h:119
static Logger & get(const Type &logType)
Definition: log.cpp:32
Provides Logger inteface that is used for logging events during decompilation.
Definition: logger.h:22
PluginExitCode
Definition: plugin.h:28
@ PLUGIN_EXIT_UNPACKED
Unpacking successful.
Definition: plugin.h:29
@ PLUGIN_EXIT_FAILED
Unpacking failed because of malformed data.
Definition: plugin.h:31
@ PLUGIN_EXIT_UNSUPPORTED
Unpacking recognized valid data, but it doesn't support unpacking of them.
Definition: plugin.h:30
Definition: log.h:14
Definition: archive_wrapper.h:19
void cleanup(ProgramOptions &po)
Definition: retdec-decompiler.cpp:910
std::string outputFile
Path to the output file (unpacked file).
Definition: plugin.h:86
bool brute
Brute mode of the unpacking was chosen.
Definition: plugin.h:87
std::string inputFile
Path to the input file (packed file).
Definition: plugin.h:85
The structure representing the plugin metadata.
Definition: plugin.h:60
Info()
Definition: plugin.h:61
bool isUninitialized() const
Definition: plugin.h:68
std::string name
Name of the plugin and also the packer.
Definition: plugin.h:73
std::string packerVersion
Regular expression of packer version it supports.
Definition: plugin.h:75
std::string author
Author of the plugin.
Definition: plugin.h:76
std::string pluginVersion
Plugin version.
Definition: plugin.h:74
Declaration of unpacker exceptions that can be subclassed in unpacker plugins.