| 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_renderer.hpp |
| 21 |
* C++ wrapper for FCML renderer. |
| 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_RENDERER_HPP_ |
| 28 |
#define FCML_RENDERER_HPP_ |
| 29 |
|
| 30 |
#include "fcml_common.hpp" |
| 31 |
#include "fcml_disassembler.hpp" |
| 32 |
#include "fcml_renderer.h" |
| 33 |
|
| 34 |
namespace fcml { |
| 35 |
|
| 36 |
/** |
| 37 |
* Component can not be initialized correctly. |
| 38 |
*/ |
| 39 |
class RenderingFailedException: public BaseException { |
| 40 |
public: |
| 41 |
RenderingFailedException(const fcml_cstring &msg, fcml_ceh_error error = |
| 42 |
FCML_CEH_GEC_NO_ERROR) : |
| 43 |
BaseException(msg, error) { |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
/** Renderer configuration. |
| 48 |
* |
| 49 |
* It's a counterpart to the fcml_st_render_config structure. |
| 50 |
* @since 1.1.0 |
| 51 |
*/ |
| 52 |
class RenderConfig { |
| 53 |
public: |
| 54 |
|
| 55 |
/** |
| 56 |
* Creates an empty renderer configuration. |
| 57 |
* @since 1.1.0 |
| 58 |
*/ |
| 59 |
RenderConfig() : |
| 60 |
_throwExceptionOnError(true), |
| 61 |
_renderFlags(0), |
| 62 |
_preferedMnemonicPadding(FCML_REND_DEFAULT_MNEMONIC_PADDING), |
| 63 |
_preferedCodePadding(FCML_REND_DEFAULT_CODE_PADDING) { |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Creates a renderer configuration for given renderer flags. |
| 68 |
* |
| 69 |
* @param renderFlags The renderer flags. |
| 70 |
* @since 1.1.0 |
| 71 |
*/ |
| 72 |
RenderConfig(fcml_uint32_t renderFlags) : |
| 73 |
_throwExceptionOnError(true), |
| 74 |
_renderFlags(renderFlags), |
| 75 |
_preferedMnemonicPadding(0), |
| 76 |
_preferedCodePadding(0) { |
| 77 |
} |
| 78 |
|
| 79 |
/** @since 1.1.0 */ |
| 80 |
fcml_uint16_t getPreferedCodePadding() const { |
| 81 |
return _preferedCodePadding; |
| 82 |
} |
| 83 |
|
| 84 |
/** @since 1.1.0 */ |
| 85 |
void setPreferedCodePadding(fcml_uint16_t preferedCodePadding) { |
| 86 |
_preferedCodePadding = preferedCodePadding; |
| 87 |
} |
| 88 |
|
| 89 |
/** @since 1.1.0 */ |
| 90 |
fcml_uint16_t getPreferedMnemonicPadding() const { |
| 91 |
return _preferedMnemonicPadding; |
| 92 |
} |
| 93 |
|
| 94 |
/** @since 1.1.0 */ |
| 95 |
void setPreferedMnemonicPadding(fcml_uint16_t preferedMnemonicPadding) { |
| 96 |
_preferedMnemonicPadding = preferedMnemonicPadding; |
| 97 |
} |
| 98 |
|
| 99 |
/** @since 1.1.0 */ |
| 100 |
fcml_uint32_t getRenderFlags() const { |
| 101 |
return _renderFlags; |
| 102 |
} |
| 103 |
|
| 104 |
/** @since 1.1.0 */ |
| 105 |
void setRenderFlags(fcml_uint32_t renderFlags) { |
| 106 |
_renderFlags = renderFlags; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Returns true if exception should be thrown when disassembling fails. |
| 111 |
* |
| 112 |
* @return True if exception is the preferred way of error handling |
| 113 |
* in case of failure. |
| 114 |
* @since 1.1.0 |
| 115 |
*/ |
| 116 |
bool isThrowExceptionOnError() const { |
| 117 |
return _throwExceptionOnError; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Sets the way how the error handling is done. |
| 122 |
* |
| 123 |
* @param throwExceptionOnError True if exception should be thrown |
| 124 |
* in case of failure. |
| 125 |
* @since 1.1.0 |
| 126 |
*/ |
| 127 |
void setThrowExceptionOnError(bool throwExceptionOnError) { |
| 128 |
_throwExceptionOnError = throwExceptionOnError; |
| 129 |
} |
| 130 |
|
| 131 |
private: |
| 132 |
|
| 133 |
/** Throws exception in case of error. */ |
| 134 |
bool _throwExceptionOnError; |
| 135 |
/** Flags which allows to control rendering process. */ |
| 136 |
fcml_uint32_t _renderFlags; |
| 137 |
/** Preferred mnemonic padding in characters. */ |
| 138 |
fcml_uint16_t _preferedMnemonicPadding; |
| 139 |
/** Preferred code padding in HEX bytes (2 characters on one byte.). */ |
| 140 |
fcml_uint16_t _preferedCodePadding; |
| 141 |
|
| 142 |
}; |
| 143 |
|
| 144 |
/** |
| 145 |
* Converts objects to their structures counterparts. |
| 146 |
* @since 1.1.0 |
| 147 |
* @remarks Internal API, not intended to be used outside. |
| 148 |
*/ |
| 149 |
class RenderTypeConverter { |
| 150 |
public: |
| 151 |
|
| 152 |
static void convert(const RenderConfig &src, fcml_st_render_config &dest) { |
| 153 |
dest.prefered_code_padding = src.getPreferedCodePadding(); |
| 154 |
dest.prefered_mnemonic_padding = src.getPreferedMnemonicPadding(); |
| 155 |
dest.render_flags = src.getRenderFlags(); |
| 156 |
} |
| 157 |
|
| 158 |
}; |
| 159 |
|
| 160 |
/** Renderer wrapper. |
| 161 |
* @since 1.1.0 |
| 162 |
*/ |
| 163 |
class Renderer: protected DialectAware { |
| 164 |
public: |
| 165 |
|
| 166 |
/** |
| 167 |
* Creates a renderer instance for the given dialect. |
| 168 |
* |
| 169 |
* @param dialect The dialect instance. |
| 170 |
* @since 1.1.0 |
| 171 |
*/ |
| 172 |
Renderer(Dialect &dialect) : |
| 173 |
_dialect(dialect) { |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Renders a disassembled instruction. |
| 178 |
* |
| 179 |
* @param renderConfig A renderer configuration. |
| 180 |
* @param assemblerResult The disassembler result. |
| 181 |
* @param[out] result The rendered instruction. |
| 182 |
* @throw RenderingFailedException Rendering failed. |
| 183 |
* @return The error code. |
| 184 |
*/ |
| 185 |
fcml_ceh_error render(const RenderConfig &renderConfig, |
| 186 |
DisassemblerResult &assemblerResult, fcml_cstring &result) { |
| 187 |
|
| 188 |
result.clear(); |
| 189 |
|
| 190 |
fcml_st_render_config render_config; |
| 191 |
RenderTypeConverter::convert(renderConfig, render_config); |
| 192 |
|
| 193 |
fcml_st_disassembler_result disassembler_result; |
| 194 |
fcml_fn_disassembler_result_prepare(&disassembler_result); |
| 195 |
|
| 196 |
DisassemblerTypeConverter::convert(assemblerResult, |
| 197 |
disassembler_result); |
| 198 |
|
| 199 |
fcml_char buffer[FCML_REND_MAX_BUFF_LEN]; |
| 200 |
|
| 201 |
fcml_ceh_error error = ::fcml_fn_render(extractDialect(_dialect), |
| 202 |
&render_config, buffer, FCML_REND_MAX_BUFF_LEN, |
| 203 |
&disassembler_result); |
| 204 |
|
| 205 |
DisassemblerTypeConverter::free(disassembler_result); |
| 206 |
|
| 207 |
if (error && renderConfig.isThrowExceptionOnError()) { |
| 208 |
throw RenderingFailedException( |
| 209 |
FCML_TEXT("Can not render instruction."), error); |
| 210 |
} |
| 211 |
|
| 212 |
result = buffer; |
| 213 |
|
| 214 |
return error; |
| 215 |
} |
| 216 |
|
| 217 |
private: |
| 218 |
|
| 219 |
/** The dialect instance associated with the renderer. */ |
| 220 |
Dialect &_dialect; |
| 221 |
|
| 222 |
}; |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
#endif /* FCML_RENDERER_HPP_ */ |