fcml  1.2.2
fcml_stateful_disassembler.hpp
Go to the documentation of this file.
1 /*
2  * FCML - Free Code Manipulation Library.
3  * Copyright (C) 2010-2020 Slawomir Wojtasiak
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
27 #ifndef FCML_STATEFUL_DISASSEMBLER_HPP_
28 #define FCML_STATEFUL_DISASSEMBLER_HPP_
29 
30 #include "fcml_disassembler.hpp"
31 #include "fcml_renderer.hpp"
32 
33 namespace fcml {
34 
43 public:
44 
54  DisassemblerContext &context, bool enableRendering = false) :
55  _disassembler(disassembler),
56  _disassemblerContext(context) {
57  _renderer = enableRendering ?
58  new Renderer(disassembler.getDialect()) : NULL;
59  // End of line characters to be used when instructions are
60  // rendered directly to the output stream.
61 #if defined(_WIN32)
62  _endOfLine = FCML_TEXT( "\r\n" );
63 #else
64  _endOfLine = FCML_TEXT("\n");
65 #endif
66  }
67 
70  if (_renderer) {
71  delete _renderer;
72  }
73  }
74 
83 
84  // IP has to be incremented automatically, instruction by instruction.
85  DisassemblerConf &config = _disassemblerContext.getDisassemblerConf();
86  config.setIncrementIp(true);
87  config.setThrowExceptionOnError(true);
88 
89  // We don't care about error handling here, because it's disassembler
90  // who is responsible for correctly handling it.
91  _disassembler.disassemble(_disassemblerContext, _disassemblerResult);
92 
93  instruction = _disassemblerResult.getInstruction();
94 
95  return *this;
96 
97  }
98 
109 
110  if (!_renderer) {
111  throw IllegalStateException(FCML_TEXT("Rendering is disabled."));
112  }
113 
114  // IP has to be incremented automatically, instruction by instruction.
115  DisassemblerConf &config = _disassemblerContext.getDisassemblerConf();
116  config.setIncrementIp(true);
117  config.setThrowExceptionOnError(true);
118 
119  _disassembler.disassemble(_disassemblerContext, _disassemblerResult);
120 
121  _rendererConfig.setThrowExceptionOnError(true);
122  _renderer->render(_rendererConfig, _disassemblerResult, instruction);
123 
124  return *this;
125 
126  }
127 
139 
140  fcml_cstring instruction;
141 
142  // Render the next instruction into a string.
143  *this >> instruction;
144 
145  // Appends the instruction into an output stream.
146  ostream << instruction << _endOfLine;
147 
148  return *this;
149 
150  }
151 
158  return _rendererConfig;
159  }
160 
168  return _rendererConfig;
169  }
170 
177  const fcml_cstring& getEndOfLine() const {
178  return _endOfLine;
179  }
180 
187  void setEndOfLine(const fcml_cstring &endOfLine) {
188  _endOfLine = endOfLine;
189  }
190 
191 private:
192 
194  fcml_cstring _endOfLine;
196  DisassemblerResult _disassemblerResult;
198  Disassembler &_disassembler;
200  DisassemblerContext &_disassemblerContext;
202  RenderConfig _rendererConfig;
204  Renderer *_renderer;
205 
206 };
207 
208 }
209 
210 #endif /* FCML_STATEFUL_DISASSEMBLER_HPP_ */
Disassembler wrapper.
Definition: fcml_disassembler.hpp:2211
Illegal state exception.
Definition: fcml_common.hpp:253
C++ wrapper for the FCML disassembler.
std::basic_string< fcml_char > fcml_cstring
By using this type definition here, it will be definitely much easier to support UNICODE in future re...
Definition: fcml_common.hpp:53
StatefulDisassembler(Disassembler &disassembler, DisassemblerContext &context, bool enableRendering=false)
Creates a stateful disassembler for given FCML disassembler and context.
Definition: fcml_stateful_disassembler.hpp:53
Disassembler configuration.
Definition: fcml_disassembler.hpp:58
#define FCML_TEXT(x)
Used to code literal strings.
Definition: fcml_types.h:61
virtual ~StatefulDisassembler()
Destructor.
Definition: fcml_stateful_disassembler.hpp:69
Dialect & getDialect() const
Gets dialect associated with the disassembler.
Definition: fcml_disassembler.hpp:2313
Definition: fcml_assembler.hpp:39
void setIncrementIp(bool incrementIp)
Definition: fcml_disassembler.hpp:132
void setThrowExceptionOnError(bool throwExceptionOnError)
Sets the way how the error handling is done.
Definition: fcml_renderer.hpp:127
StatefulDisassembler & operator>>(Instruction &instruction)
Disassembles the next instruction pointed by the disassembler state.
Definition: fcml_stateful_disassembler.hpp:82
C++ wrapper for FCML renderer.
fcml_ceh_error disassemble(DisassemblerContext &ctx, DisassemblerResult &disassemblerResult)
Disassembled the next instruction from the context.
Definition: fcml_disassembler.hpp:2253
Renderer configuration.
Definition: fcml_renderer.hpp:52
fcml_ceh_error render(const RenderConfig &renderConfig, DisassemblerResult &assemblerResult, fcml_cstring &result)
Renders a disassembled instruction.
Definition: fcml_renderer.hpp:185
const Instruction & getInstruction() const
Gets errors container with errors related to the failed disassembling process.
Definition: fcml_disassembler.hpp:1899
Disassembler result.
Definition: fcml_disassembler.hpp:1877
Stateful disassembler can be used when you have to disassemble a larger piece of code one instruction...
Definition: fcml_stateful_disassembler.hpp:42
const fcml_cstring & getEndOfLine() const
Gets end of line characters sequence used by the renderer.
Definition: fcml_stateful_disassembler.hpp:177
Describes an instruction.
Definition: fcml_common.hpp:7185
RenderConfig & getRendererConfig()
Gets renderer configuration used by the instruction buffer.
Definition: fcml_stateful_disassembler.hpp:157
std::basic_ostringstream< fcml_char > fcml_costream
String output stream.
Definition: fcml_common.hpp:59
const RenderConfig & getRendererConfig() const
Gets renderer configuration used by the internally managed instruction renderer.
Definition: fcml_stateful_disassembler.hpp:167
Disassembler context.
Definition: fcml_disassembler.hpp:183
void setThrowExceptionOnError(bool throwExceptionOnError)
Sets the way how the error handling is done.
Definition: fcml_disassembler.hpp:164
Renderer wrapper.
Definition: fcml_renderer.hpp:163
const DisassemblerConf & getDisassemblerConf() const
Gets a reference to the configuration object associated with the context.
Definition: fcml_disassembler.hpp:253
void setEndOfLine(const fcml_cstring &endOfLine)
Sets dedicated end of line characters.
Definition: fcml_stateful_disassembler.hpp:187