fcml  1.1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
fcml_stateful_disassembler.hpp
Go to the documentation of this file.
1 /*
2  * FCML - Free Code Manipulation Library.
3  * Copyright (C) 2010-2015 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 
42 public:
43 
49  StatefulDisassembler( Disassembler &disassembler, DisassemblerContext &context, bool enableRendering = false ) :
50  _disassembler(disassembler),
51  _disassemblerContext(context) {
52  _renderer = enableRendering ? new Renderer( disassembler.getDialect() ) : NULL;
53  // End of line characters to be used when instructions are rendered directly to the output stream.
54 #if defined(_WIN32)
55  _endOfLine = FCML_TEXT( "\r\n" );
56 #else
57  _endOfLine = FCML_TEXT( "\n" );
58 #endif
59  }
60 
63  if( _renderer ) {
64  delete _renderer;
65  }
66  }
67 
76 
77  // IP has to be incremented automatically, instruction by instruction.
78  DisassemblerConf& config = _disassemblerContext.getDisassemblerConf();
79  config.setIncrementIp( true );
80  config.setThrowExceptionOnError( true );
81 
82  // We don't care about error handling here, because it's disassembler
83  // who is responsible for correctly handling it.
84  _disassembler.disassemble( _disassemblerContext, _disassemblerResult );
85 
86  instruction = _disassemblerResult.getInstruction();
87 
88  return *this;
89 
90  }
91 
101  StatefulDisassembler& operator >>( fcml_cstring &instruction ) {
102 
103  if( !_renderer ) {
104  throw IllegalStateException( FCML_TEXT( "Rendering is disabled." ) );
105  }
106 
107  // IP has to be incremented automatically, instruction by instruction.
108  DisassemblerConf& config = _disassemblerContext.getDisassemblerConf();
109  config.setIncrementIp( true );
110  config.setThrowExceptionOnError( true );
111 
112  _disassembler.disassemble( _disassemblerContext, _disassemblerResult );
113 
114  _rendererConfig.setThrowExceptionOnError( true );
115 
116  _renderer->render( _rendererConfig, _disassemblerResult, instruction );
117 
118  return *this;
119 
120  }
121 
131  StatefulDisassembler& operator >>( fcml_costream &ostream ) {
132 
133  fcml_cstring instruction;
134 
135  // Render the next instruction into a string.
136  *this >> instruction;
137 
138  // Appends the instruction into an output stream.
139  ostream << instruction << _endOfLine;
140 
141  return *this;
142 
143  }
144 
151  return _rendererConfig;
152  }
153 
160  return _rendererConfig;
161  }
162 
169  const fcml_cstring& getEndOfLine() const {
170  return _endOfLine;
171  }
172 
179  void setEndOfLine(const fcml_cstring& endOfLine) {
180  _endOfLine = endOfLine;
181  }
182 
183 private:
184 
186  fcml_cstring _endOfLine;
188  DisassemblerResult _disassemblerResult;
190  Disassembler &_disassembler;
192  DisassemblerContext &_disassemblerContext;
194  RenderConfig _rendererConfig;
196  Renderer *_renderer;
197 
198 };
199 
200 }
201 
202 #endif /* FCML_STATEFUL_DISASSEMBLER_HPP_ */
Disassembler wrapper.
Definition: fcml_disassembler.hpp:1849
Illegal state exception.
Definition: fcml_common.hpp:228
C++ wrapper for the FCML disassembler.
const DisassemblerConf & getDisassemblerConf() const
Gets a reference to the configuration object associated with the context.
Definition: fcml_disassembler.hpp:251
StatefulDisassembler(Disassembler &disassembler, DisassemblerContext &context, bool enableRendering=false)
Creates a stateful disassembler for given FCML disassembler and context.
Definition: fcml_stateful_disassembler.hpp:49
Disassembler configuration.
Definition: fcml_disassembler.hpp:56
const RenderConfig & getRendererConfig() const
Gets renderer configuration used by the internally managed instruction renderer.
Definition: fcml_stateful_disassembler.hpp:159
#define FCML_TEXT(x)
Used to code literal strings.
Definition: fcml_types.h:61
virtual ~StatefulDisassembler()
Destructor.
Definition: fcml_stateful_disassembler.hpp:62
Definition: fcml_assembler.hpp:39
void setIncrementIp(bool incrementIp)
Definition: fcml_disassembler.hpp:130
void setThrowExceptionOnError(bool throwExceptionOnError)
Sets the way how the error handling is done.
Definition: fcml_renderer.hpp:124
StatefulDisassembler & operator>>(Instruction &instruction)
Disassembles the next instruction pointed by the disassembler state.
Definition: fcml_stateful_disassembler.hpp:75
C++ wrapper for FCML renderer.
Dialect & getDialect() const
Gets dialect associated with the disassembler.
Definition: fcml_disassembler.hpp:1945
fcml_ceh_error disassemble(DisassemblerContext &ctx, DisassemblerResult &disassemblerResult)
Disassembled the next instruction from the context.
Definition: fcml_disassembler.hpp:1889
Renderer configuration.
Definition: fcml_renderer.hpp:51
fcml_ceh_error render(const RenderConfig &renderConfig, DisassemblerResult &assemblerResult, fcml_cstring &result)
Renders a disassembled instruction.
Definition: fcml_renderer.hpp:182
Disassembler result.
Definition: fcml_disassembler.hpp:1567
Stateful disassembler can be used when you have to disassemble a larger piece of code one instruction...
Definition: fcml_stateful_disassembler.hpp:41
Describes an instruction.
Definition: fcml_common.hpp:6207
RenderConfig & getRendererConfig()
Gets renderer configuration used by the instruction buffer.
Definition: fcml_stateful_disassembler.hpp:150
Disassembler context.
Definition: fcml_disassembler.hpp:179
void setThrowExceptionOnError(bool throwExceptionOnError)
Sets the way how the error handling is done.
Definition: fcml_disassembler.hpp:160
Renderer wrapper.
Definition: fcml_renderer.hpp:160
const Instruction & getInstruction() const
Gets errors container with errors related to the failed disassembling process.
Definition: fcml_disassembler.hpp:1587
const fcml_cstring & getEndOfLine() const
Gets end of line characters sequence used by the renderer.
Definition: fcml_stateful_disassembler.hpp:169
void setEndOfLine(const fcml_cstring &endOfLine)
Sets dedicated end of line characters.
Definition: fcml_stateful_disassembler.hpp:179