2 #include <Zydis/Zydis.h>
17 template <
class T >
inline T
__ROL__( T value,
int count )
19 const unsigned int nbits =
sizeof( T ) * 8;
24 T high = value >> ( nbits - count );
26 high &= ~( ( T( -1 ) << count ) );
32 count = -count % nbits;
33 T low = value << ( nbits - count );
132 template <
typename T >
using transform_t = std::function< T( T, T ) >;
150 using map_t = std::map< transform::type, zydis_decoded_instr_t >;
153 inline const auto _bswap = []( T a, T b ) -> T {
154 if constexpr ( std::is_same_v< T, std::uint64_t > )
155 return _byteswap_uint64( a );
156 if constexpr ( std::is_same_v< T, std::uint32_t > )
157 return _byteswap_ulong( a );
158 if constexpr ( std::is_same_v< T, std::uint16_t > )
159 return _byteswap_ushort( a );
161 throw std::invalid_argument(
"invalid type size..." );
164 template <
class T >
inline const auto _add = []( T a, T b ) -> T {
return a + b; };
166 template <
class T >
inline const auto _xor = []( T a, T b ) -> T {
return a ^ b; };
168 template <
class T >
inline const auto _sub = []( T a, T b ) -> T {
return a - b; };
170 template <
class T >
inline const auto _neg = []( T a, T b ) -> T {
return a * -1; };
172 template <
class T >
inline const auto _not = []( T a, T b ) -> T {
return ~a; };
175 inline const auto _ror = []( T a, T b ) -> T {
176 if constexpr ( std::is_same_v< T, std::uint64_t > )
178 if constexpr ( std::is_same_v< T, std::uint32_t > )
180 if constexpr ( std::is_same_v< T, std::uint16_t > )
182 if constexpr ( std::is_same_v< T, std::uint8_t > )
185 throw std::invalid_argument(
"invalid type size..." );
189 inline const auto _rol = []( T a, T b ) -> T {
190 if constexpr ( std::is_same_v< T, std::uint64_t > )
192 if constexpr ( std::is_same_v< T, std::uint32_t > )
194 if constexpr ( std::is_same_v< T, std::uint16_t > )
196 if constexpr ( std::is_same_v< T, std::uint8_t > )
199 throw std::invalid_argument(
"invalid type size..." );
202 template <
class T >
inline const auto _inc = []( T a, T b ) -> T {
return a + 1; };
204 template <
class T >
inline const auto _dec = []( T a, T b ) -> T {
return a - 1; };
207 inline std::map< zydis_mnemonic_t, transform_t< T > >
transforms = {
208 { ZYDIS_MNEMONIC_ADD, _add< T > }, { ZYDIS_MNEMONIC_XOR, _xor< T > }, { ZYDIS_MNEMONIC_BSWAP, _bswap< T > },
209 { ZYDIS_MNEMONIC_SUB, _sub< T > }, { ZYDIS_MNEMONIC_NEG, _neg< T > }, { ZYDIS_MNEMONIC_NOT, _not< T > },
210 { ZYDIS_MNEMONIC_ROR, _ror< T > }, { ZYDIS_MNEMONIC_ROL, _rol< T > }, { ZYDIS_MNEMONIC_INC, _inc< T > },
211 { ZYDIS_MNEMONIC_DEC, _dec< T > } };
213 inline std::map< zydis_mnemonic_t, zydis_mnemonic_t >
inverse = {
214 { ZYDIS_MNEMONIC_ADD, ZYDIS_MNEMONIC_SUB }, { ZYDIS_MNEMONIC_XOR, ZYDIS_MNEMONIC_XOR },
215 { ZYDIS_MNEMONIC_BSWAP, ZYDIS_MNEMONIC_BSWAP }, { ZYDIS_MNEMONIC_SUB, ZYDIS_MNEMONIC_ADD },
216 { ZYDIS_MNEMONIC_NEG, ZYDIS_MNEMONIC_NEG }, { ZYDIS_MNEMONIC_NOT, ZYDIS_MNEMONIC_NOT },
217 { ZYDIS_MNEMONIC_ROR, ZYDIS_MNEMONIC_ROL }, { ZYDIS_MNEMONIC_ROL, ZYDIS_MNEMONIC_ROR },
218 { ZYDIS_MNEMONIC_INC, ZYDIS_MNEMONIC_DEC }, { ZYDIS_MNEMONIC_DEC, ZYDIS_MNEMONIC_INC } };
227 return transforms< std::uint64_t >.find( op ) != transforms< std::uint64_t >.end();
269 for (
auto idx = 0u; idx < instrs.size(); idx++ )
270 if ( !( instrs[ idx ].mnemonic =
inverse[ instrs[ idx ].mnemonic ] ) )
273 std::reverse( instrs.begin(), instrs.end() );
280 inline auto apply( std::uint8_t bitsize, ZydisMnemonic op, std::uint64_t a, std::uint64_t b ) -> std::uint64_t
285 return transforms< std::uint8_t >[ op ]( a, b );
287 return transforms< std::uint16_t >[ op ]( a, b );
289 return transforms< std::uint32_t >[ op ]( a, b );
291 return transforms< std::uint64_t >[ op ]( a, b );
293 throw std::invalid_argument(
"invalid bit size..." );
304 return instr->operand_count > 1 && ( instr->operands[ 1 ].type == ZYDIS_OPERAND_TYPE_IMMEDIATE );
unsigned long long u64
Definition: vmutils.hpp:15
unsigned int u32
Definition: vmutils.hpp:14
unsigned short u16
Definition: vmutils.hpp:13
ZydisMnemonic zydis_mnemonic_t
Definition: vmutils.hpp:20
ZydisDecodedInstruction zydis_decoded_instr_t
Definition: vmutils.hpp:18
unsigned char u8
Definition: vmutils.hpp:12