fcml  1.2.2
fcml_renderer.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_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 
40 public:
43  BaseException(msg, error) {
44  }
45 };
46 
52 class RenderConfig {
53 public:
54 
60  _throwExceptionOnError(true),
61  _renderFlags(0),
62  _preferedMnemonicPadding(FCML_REND_DEFAULT_MNEMONIC_PADDING),
63  _preferedCodePadding(FCML_REND_DEFAULT_CODE_PADDING) {
64  }
65 
72  RenderConfig(fcml_uint32_t renderFlags) :
73  _throwExceptionOnError(true),
74  _renderFlags(renderFlags),
75  _preferedMnemonicPadding(0),
76  _preferedCodePadding(0) {
77  }
78 
80  fcml_uint16_t getPreferedCodePadding() const {
81  return _preferedCodePadding;
82  }
83 
85  void setPreferedCodePadding(fcml_uint16_t preferedCodePadding) {
86  _preferedCodePadding = preferedCodePadding;
87  }
88 
90  fcml_uint16_t getPreferedMnemonicPadding() const {
91  return _preferedMnemonicPadding;
92  }
93 
95  void setPreferedMnemonicPadding(fcml_uint16_t preferedMnemonicPadding) {
96  _preferedMnemonicPadding = preferedMnemonicPadding;
97  }
98 
100  fcml_uint32_t getRenderFlags() const {
101  return _renderFlags;
102  }
103 
105  void setRenderFlags(fcml_uint32_t renderFlags) {
106  _renderFlags = renderFlags;
107  }
108 
116  bool isThrowExceptionOnError() const {
117  return _throwExceptionOnError;
118  }
119 
127  void setThrowExceptionOnError(bool throwExceptionOnError) {
128  _throwExceptionOnError = throwExceptionOnError;
129  }
130 
131 private:
132 
134  bool _throwExceptionOnError;
136  fcml_uint32_t _renderFlags;
138  fcml_uint16_t _preferedMnemonicPadding;
140  fcml_uint16_t _preferedCodePadding;
141 
142 };
143 
150 public:
151 
152  static void convert(const RenderConfig &src, fcml_st_render_config &dest) {
155  dest.render_flags = src.getRenderFlags();
156  }
157 
158 };
159 
163 class Renderer: protected DialectAware {
164 public:
165 
172  Renderer(Dialect &dialect) :
173  _dialect(dialect) {
174  }
175 
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()) {
209  FCML_TEXT("Can not render instruction."), error);
210  }
211 
212  result = buffer;
213 
214  return error;
215  }
216 
217 private:
218 
220  Dialect &_dialect;
221 
222 };
223 
224 }
225 
226 #endif /* FCML_RENDERER_HPP_ */
#define FCML_REND_MAX_BUFF_LEN
The rendered instruction size can not exceed this value.
Definition: fcml_renderer.h:42
fcml_uint16_t getPreferedMnemonicPadding() const
Definition: fcml_renderer.hpp:90
C++ wrappers common classes.
RenderConfig()
Creates an empty renderer configuration.
Definition: fcml_renderer.hpp:59
C++ wrapper for the FCML disassembler.
RenderConfig(fcml_uint32_t renderFlags)
Creates a renderer configuration for given renderer flags.
Definition: fcml_renderer.hpp:72
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
#define FCML_TEXT(x)
Used to code literal strings.
Definition: fcml_types.h:61
Converts objects to their structures counterparts.
Definition: fcml_renderer.hpp:149
void setPreferedCodePadding(fcml_uint16_t preferedCodePadding)
Definition: fcml_renderer.hpp:85
fcml_uint16_t prefered_code_padding
Preferred code padding in HEX bytes (2 characters on one byte.).
Definition: fcml_renderer.h:99
void setRenderFlags(fcml_uint32_t renderFlags)
Definition: fcml_renderer.hpp:105
Definition: fcml_assembler.hpp:39
void setThrowExceptionOnError(bool throwExceptionOnError)
Sets the way how the error handling is done.
Definition: fcml_renderer.hpp:127
Inherit from this class in order to get access to the native FCML dialect structure.
Definition: fcml_dialect.hpp:98
Base exception for all exceptions exposed by FCML library.
Definition: fcml_common.hpp:187
Renderer configuration.
Definition: fcml_renderer.h:93
bool isThrowExceptionOnError() const
Returns true if exception should be thrown when disassembling fails.
Definition: fcml_renderer.hpp:116
Reusable disassembler result holder.
Definition: fcml_disassembler.h:267
#define FCML_REND_DEFAULT_CODE_PADDING
Default number of characters used as code padding.
Definition: fcml_renderer.h:86
Structures and functions declarations related to FCML renderers.
fcml_uint16_t prefered_mnemonic_padding
Preferred mnemonic padding in characters.
Definition: fcml_renderer.h:97
Renderer configuration.
Definition: fcml_renderer.hpp:52
LIB_EXPORT void LIB_CALL fcml_fn_disassembler_result_prepare(fcml_st_disassembler_result *result)
Prepares reusable result holder for disassembler.
fcml_ceh_error render(const RenderConfig &renderConfig, DisassemblerResult &assemblerResult, fcml_cstring &result)
Renders a disassembled instruction.
Definition: fcml_renderer.hpp:185
Disassembler result.
Definition: fcml_disassembler.hpp:1877
Component can not be initialized correctly.
Definition: fcml_renderer.hpp:39
Operation succeed.
Definition: fcml_errors.h:42
LIB_EXPORT fcml_ceh_error LIB_CALL fcml_fn_render(const fcml_st_dialect *dialect, const fcml_st_render_config *config, fcml_char *buffer, fcml_usize buffer_len, const fcml_st_disassembler_result *result)
Renders the disassembled instruction into its textual representation.
#define FCML_REND_DEFAULT_MNEMONIC_PADDING
Default number of characters used as mnemonic padding.
Definition: fcml_renderer.h:88
An abstract dialect.
Definition: fcml_dialect.hpp:41
Renderer wrapper.
Definition: fcml_renderer.hpp:163
fcml_uint32_t render_flags
Flags which allows to control rendering process.
Definition: fcml_renderer.h:95
fcml_uint32_t getRenderFlags() const
Definition: fcml_renderer.hpp:100
void setPreferedMnemonicPadding(fcml_uint16_t preferedMnemonicPadding)
Definition: fcml_renderer.hpp:95
fcml_uint16_t getPreferedCodePadding() const
Definition: fcml_renderer.hpp:80
fcml_uint16_t fcml_ceh_error
All error codes should be held in variables of this type.
Definition: fcml_errors.h:156
Renderer(Dialect &dialect)
Creates a renderer instance for the given dialect.
Definition: fcml_renderer.hpp:172