| 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 |
|
| 20 |
/** @file fcml_stateful_disassembler.hpp |
| 21 |
* Stateful FCML disassembler implementation. |
| 22 |
* |
| 23 |
* @copyright Copyright (C) 2010-2020 Slawomir Wojtasiak. All rights reserved. |
| 24 |
* This project is released under the GNU Lesser General Public License. |
| 25 |
*/ |
| 26 |
|
| 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 |
|
| 35 |
/** Stateful disassembler can be used when you have to disassemble a larger |
| 36 |
* piece of code one instruction by one. |
| 37 |
* It also supports rendering directly using internally managed renderer. |
| 38 |
* |
| 39 |
* @since 1.1.0 |
| 40 |
* @remarks This class isn't thread-safe. |
| 41 |
*/ |
| 42 |
class StatefulDisassembler { |
| 43 |
public: |
| 44 |
|
| 45 |
/** Creates a stateful disassembler for given FCML disassembler and context. |
| 46 |
* Rendering support can be enabled optionally. |
| 47 |
* @param disassembler The classic FCML disassembler used to |
| 48 |
* disassemble instructions. |
| 49 |
* @param context The disassembler context. |
| 50 |
* @param enableRendering Enables instruction rendering, which is |
| 51 |
* disabled by default. |
| 52 |
*/ |
| 53 |
StatefulDisassembler(Disassembler &disassembler, |
| 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 |
|
| 68 |
/** Destructor. */ |
| 69 |
virtual ~StatefulDisassembler() { |
| 70 |
if (_renderer) { |
| 71 |
delete _renderer; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Disassembles the next instruction pointed by the disassembler state. |
| 77 |
* @param[out] instruction The destination instruction model. |
| 78 |
* @return A reference to the stateful disassembler. |
| 79 |
* @throws DisassemblingFailedException |
| 80 |
* @since 1.1.0 |
| 81 |
*/ |
| 82 |
StatefulDisassembler& operator >>(Instruction &instruction) { |
| 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 |
|
| 99 |
/** |
| 100 |
* Disassembles the next instruction from the buffer and renders it. |
| 101 |
* @param[out] instruction Destination string for the instruction. |
| 102 |
* @return A reference to the stateful disassembler. |
| 103 |
* @throws DisassemblingFailedException, |
| 104 |
* IllegalStateException, |
| 105 |
* RenderingFailedException |
| 106 |
* @since 1.1.0 |
| 107 |
*/ |
| 108 |
StatefulDisassembler& operator >>(fcml_cstring &instruction) { |
| 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 |
|
| 128 |
/** |
| 129 |
* Disassembles the next instruction from the buffer and renders it |
| 130 |
* directly into the output stream. |
| 131 |
* @param[out] ostream The output stream. |
| 132 |
* @return A reference to the stateful disassembler. |
| 133 |
* @throws DisassemblingFailedException, |
| 134 |
* IllegalStateException, |
| 135 |
* RenderingFailedException |
| 136 |
* @since 1.1.0 |
| 137 |
*/ |
| 138 |
StatefulDisassembler& operator >>(fcml_costream &ostream) { |
| 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 |
|
| 152 |
/** |
| 153 |
* Gets renderer configuration used by the instruction buffer. |
| 154 |
* @return The renderer configuration. |
| 155 |
* @since 1.1.0 |
| 156 |
*/ |
| 157 |
RenderConfig& getRendererConfig() { |
| 158 |
return _rendererConfig; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Gets renderer configuration used by the internally managed |
| 163 |
* instruction renderer. |
| 164 |
* @return The renderer configuration. |
| 165 |
* @since 1.1.0 |
| 166 |
*/ |
| 167 |
const RenderConfig& getRendererConfig() const { |
| 168 |
return _rendererConfig; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Gets end of line characters sequence used by the renderer. |
| 173 |
* |
| 174 |
* @return End of line characters sequence used by the renderer. |
| 175 |
* @since 1.1.0 |
| 176 |
*/ |
| 177 |
const fcml_cstring& getEndOfLine() const { |
| 178 |
return _endOfLine; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Sets dedicated end of line characters. |
| 183 |
* |
| 184 |
* @param endOfLine A sequence of characters used as line ending. |
| 185 |
* @since 1.1.0 |
| 186 |
*/ |
| 187 |
void setEndOfLine(const fcml_cstring &endOfLine) { |
| 188 |
_endOfLine = endOfLine; |
| 189 |
} |
| 190 |
|
| 191 |
private: |
| 192 |
|
| 193 |
/** End of line character sequence. */ |
| 194 |
fcml_cstring _endOfLine; |
| 195 |
/** Disassembler result used when disassembling instructions. */ |
| 196 |
DisassemblerResult _disassemblerResult; |
| 197 |
/** The disassembler used to disassemble the code. */ |
| 198 |
Disassembler &_disassembler; |
| 199 |
/** Disassembler context pointing to the machine code. */ |
| 200 |
DisassemblerContext &_disassemblerContext; |
| 201 |
/** Renderer configuration used when instructions have to be rendered. */ |
| 202 |
RenderConfig _rendererConfig; |
| 203 |
/** Optional renderer used only when instructions are rendered. */ |
| 204 |
Renderer *_renderer; |
| 205 |
|
| 206 |
}; |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
#endif /* FCML_STATEFUL_DISASSEMBLER_HPP_ */ |