fcml 1.2.2
Loading...
Searching...
No Matches
fcml_stateful_disassembler.hpp
Go to the documentation of this file.
1/*
2 * FCML - Free Code Manipulation Library.
3 * Copyright (C) 2010-2019 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
33namespace fcml {
34
43public:
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
191private:
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 configuration.
Definition fcml_disassembler.hpp:58
void setThrowExceptionOnError(bool throwExceptionOnError)
Sets the way how the error handling is done.
Definition fcml_disassembler.hpp:164
void setIncrementIp(bool incrementIp)
Definition fcml_disassembler.hpp:132
Disassembler context.
Definition fcml_disassembler.hpp:183
const DisassemblerConf & getDisassemblerConf() const
Gets a reference to the configuration object associated with the context.
Definition fcml_disassembler.hpp:253
Disassembler result.
Definition fcml_disassembler.hpp:1877
const Instruction & getInstruction() const
Gets errors container with errors related to the failed disassembling process.
Definition fcml_disassembler.hpp:1899
Disassembler wrapper.
Definition fcml_disassembler.hpp:2211
fcml_ceh_error disassemble(DisassemblerContext &ctx, DisassemblerResult &disassemblerResult)
Disassembled the next instruction from the context.
Definition fcml_disassembler.hpp:2253
Dialect & getDialect() const
Gets dialect associated with the disassembler.
Definition fcml_disassembler.hpp:2313
Illegal state exception.
Definition fcml_common.hpp:253
Describes an instruction.
Definition fcml_common.hpp:7185
Renderer configuration.
Definition fcml_renderer.hpp:52
void setThrowExceptionOnError(bool throwExceptionOnError)
Sets the way how the error handling is done.
Definition fcml_renderer.hpp:127
Renderer wrapper.
Definition fcml_renderer.hpp:163
fcml_ceh_error render(const RenderConfig &renderConfig, DisassemblerResult &assemblerResult, fcml_cstring &result)
Renders a disassembled instruction.
Definition fcml_renderer.hpp:185
Stateful disassembler can be used when you have to disassemble a larger piece of code one instruction...
Definition fcml_stateful_disassembler.hpp:42
StatefulDisassembler(Disassembler &disassembler, DisassemblerContext &context, bool enableRendering=false)
Creates a stateful disassembler for given FCML disassembler and context.
Definition fcml_stateful_disassembler.hpp:53
RenderConfig & getRendererConfig()
Gets renderer configuration used by the instruction buffer.
Definition fcml_stateful_disassembler.hpp:157
const RenderConfig & getRendererConfig() const
Gets renderer configuration used by the internally managed instruction renderer.
Definition fcml_stateful_disassembler.hpp:167
const fcml_cstring & getEndOfLine() const
Gets end of line characters sequence used by the renderer.
Definition fcml_stateful_disassembler.hpp:177
void setEndOfLine(const fcml_cstring &endOfLine)
Sets dedicated end of line characters.
Definition fcml_stateful_disassembler.hpp:187
virtual ~StatefulDisassembler()
Destructor.
Definition fcml_stateful_disassembler.hpp:69
StatefulDisassembler & operator>>(Instruction &instruction)
Disassembles the next instruction pointed by the disassembler state.
Definition fcml_stateful_disassembler.hpp:82
std::basic_ostringstream< fcml_char > fcml_costream
String output stream.
Definition fcml_common.hpp:59
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
C++ wrapper for the FCML disassembler.
C++ wrapper for FCML renderer.
#define FCML_TEXT(x)
Used to code literal strings.
Definition fcml_types.h:61