1 /* 2 3 This file contains definitions used by the Hex-Rays decompiler output. 4 It has type definitions and convenience macros to make the 5 output more readable. 6 7 Copyright (c) 2007-2011 Hex-Rays 8 9 */ 10 11 #if defined(__GNUC__) 12 typedef long long ll; 13 typedef unsigned long long ull; 14 #define __int64 long long 15 #define __int32 int 16 #define __int16 short 17 #define __int8 char 18 #define MAKELL(num) num ## LL 19 #define FMT_64 "ll" 20 #elif defined(_MSC_VER) 21 typedef __int64 ll; 22 typedef unsigned __int64 ull; 23 #define MAKELL(num) num ## i64 24 #define FMT_64 "I64" 25 #elif defined (__BORLANDC__) 26 typedef __int64 ll; 27 typedef unsigned __int64 ull; 28 #define MAKELL(num) num ## i64 29 #define FMT_64 "L" 30 #else 31 #error "unknown compiler" 32 #endif 33 typedef unsigned int uint; 34 typedef unsigned char uchar; 35 typedef unsigned short ushort; 36 typedef unsigned long ulong; 37 38 typedef char int8; 39 typedef signed char sint8; 40 typedef unsigned char uint8; 41 typedef short int16; 42 typedef signed short sint16; 43 typedef unsigned short uint16; 44 typedef int int32; 45 typedef signed int sint32; 46 typedef unsigned int uint32; 47 typedef ll int64; 48 typedef ll sint64; 49 typedef ull uint64; 50 51 // Partially defined types: 52 #define _BYTE uint8 53 #define _WORD uint16 54 #define _DWORD uint32 55 #define _QWORD uint64 56 #if !defined(_MSC_VER) 57 #define _LONGLONG __int128 58 #endif 59 60 #ifndef _WINDOWS_ 61 typedef int8 BYTE; 62 typedef int16 WORD; 63 typedef int32 DWORD; 64 typedef int32 LONG; 65 #endif 66 typedef int64 QWORD; 67 #ifndef __cplusplus 68 typedef int bool; // we want to use bool in our C programs 69 #endif 70 71 // Some convenience macros to make partial accesses nicer 72 // first unsigned macros: 73 #define LOBYTE(x) (*((_BYTE*)&(x))) // low byte 74 #define LOWORD(x) (*((_WORD*)&(x))) // low word 75 #define LODWORD(x) (*((_DWORD*)&(x))) // low dword 76 #define HIBYTE(x) (*((_BYTE*)&(x)+1)) 77 #define HIWORD(x) (*((_WORD*)&(x)+1)) 78 #define HIDWORD(x) (*((_DWORD*)&(x)+1)) 79 #define BYTEn(x, n) (*((_BYTE*)&(x)+n)) 80 #define WORDn(x, n) (*((_WORD*)&(x)+n)) 81 #define BYTE1(x) BYTEn(x, 1) // byte 1 (counting from 0) 82 #define BYTE2(x) BYTEn(x, 2) 83 #define BYTE3(x) BYTEn(x, 3) 84 #define BYTE4(x) BYTEn(x, 4) 85 #define BYTE5(x) BYTEn(x, 5) 86 #define BYTE6(x) BYTEn(x, 6) 87 #define BYTE7(x) BYTEn(x, 7) 88 #define BYTE8(x) BYTEn(x, 8) 89 #define BYTE9(x) BYTEn(x, 9) 90 #define BYTE10(x) BYTEn(x, 10) 91 #define BYTE11(x) BYTEn(x, 11) 92 #define BYTE12(x) BYTEn(x, 12) 93 #define BYTE13(x) BYTEn(x, 13) 94 #define BYTE14(x) BYTEn(x, 14) 95 #define BYTE15(x) BYTEn(x, 15) 96 #define WORD1(x) WORDn(x, 1) 97 #define WORD2(x) WORDn(x, 2) // third word of the object, unsigned 98 #define WORD3(x) WORDn(x, 3) 99 #define WORD4(x) WORDn(x, 4)100 #define WORD5(x) WORDn(x, 5)101 #define WORD6(x) WORDn(x, 6)102 #define WORD7(x) WORDn(x, 7)103 104 // now signed macros (the same but with sign extension)105 #define SLOBYTE(x) (*((int8*)&(x)))106 #define SLOWORD(x) (*((int16*)&(x)))107 #define SLODWORD(x) (*((int32*)&(x)))108 #define SHIBYTE(x) (*((int8*)&(x)+1))109 #define SHIWORD(x) (*((int16*)&(x)+1))110 #define SHIDWORD(x) (*((int32*)&(x)+1))111 #define SBYTEn(x, n) (*((int8*)&(x)+n))112 #define SWORDn(x, n) (*((int16*)&(x)+n))113 #define SBYTE1(x) SBYTEn(x, 1)114 #define SBYTE2(x) SBYTEn(x, 2)115 #define SBYTE3(x) SBYTEn(x, 3)116 #define SBYTE4(x) SBYTEn(x, 4)117 #define SBYTE5(x) SBYTEn(x, 5)118 #define SBYTE6(x) SBYTEn(x, 6)119 #define SBYTE7(x) SBYTEn(x, 7)120 #define SBYTE8(x) SBYTEn(x, 8)121 #define SBYTE9(x) SBYTEn(x, 9)122 #define SBYTE10(x) SBYTEn(x, 10)123 #define SBYTE11(x) SBYTEn(x, 11)124 #define SBYTE12(x) SBYTEn(x, 12)125 #define SBYTE13(x) SBYTEn(x, 13)126 #define SBYTE14(x) SBYTEn(x, 14)127 #define SBYTE15(x) SBYTEn(x, 15)128 #define SWORD1(x) SWORDn(x, 1)129 #define SWORD2(x) SWORDn(x, 2)130 #define SWORD3(x) SWORDn(x, 3)131 #define SWORD4(x) SWORDn(x, 4)132 #define SWORD5(x) SWORDn(x, 5)133 #define SWORD6(x) SWORDn(x, 6)134 #define SWORD7(x) SWORDn(x, 7)135 136 137 // Helper functions to represent some assembly instructions.138 139 #ifdef __cplusplus140 141 // Fill memory block with an integer value142 inline void memset32(void *ptr, uint32 value, int count)143 {144 uint32 *p = (uint32 *)ptr;145 for ( int i=0; i < count; i++ )146 *p++ = value;147 }148 149 // Generate a reference to pair of operands150 templateint16 __PAIR__( int8 high, T low) { return ((( int16)high) << sizeof(high)*8) | uint8(low); }151 template int32 __PAIR__( int16 high, T low) { return ((( int32)high) << sizeof(high)*8) | uint16(low); }152 template int64 __PAIR__( int32 high, T low) { return ((( int64)high) << sizeof(high)*8) | uint32(low); }153 template uint16 __PAIR__(uint8 high, T low) { return (((uint16)high) << sizeof(high)*8) | uint8(low); }154 template uint32 __PAIR__(uint16 high, T low) { return (((uint32)high) << sizeof(high)*8) | uint16(low); }155 template uint64 __PAIR__(uint32 high, T low) { return (((uint64)high) << sizeof(high)*8) | uint32(low); }156 157 // rotate left158 template T __ROL__(T value, uint count)159 {160 const uint nbits = sizeof(T) * 8;161 count %= nbits;162 163 T high = value >> (nbits - count);164 value <<= count;165 value |= high;166 return value;167 }168 169 // rotate right170 template T __ROR__(T value, uint count)171 {172 const uint nbits = sizeof(T) * 8;173 count %= nbits;174 175 T low = value << (nbits - count);176 value >>= count;177 value |= low;178 return value;179 }180 181 // carry flag of left shift182 template int8 __MKCSHL__(T value, uint count)183 {184 const uint nbits = sizeof(T) * 8;185 count %= nbits;186 187 return (value >> (nbits-count)) & 1;188 }189 190 // carry flag of right shift191 template int8 __MKCSHR__(T value, uint count)192 {193 return (value >> (count-1)) & 1;194 }195 196 // sign flag197 template int8 __SETS__(T x)198 {199 if ( sizeof(T) == 1 )200 return int8(x) < 0;201 if ( sizeof(T) == 2 )202 return int16(x) < 0;203 if ( sizeof(T) == 4 )204 return int32(x) < 0;205 return int64(x) < 0;206 }207 208 // overflow flag of subtraction (x-y)209 template int8 __OFSUB__(T x, U y)210 {211 if ( sizeof(T) < sizeof(U) )212 {213 U x2 = x;214 int8 sx = __SETS__(x2);215 return (sx ^ __SETS__(y)) & (sx ^ __SETS__(x2-y));216 }217 else218 {219 T y2 = y;220 int8 sx = __SETS__(x);221 return (sx ^ __SETS__(y2)) & (sx ^ __SETS__(x-y2));222 }223 }224 225 // overflow flag of addition (x+y)226 template int8 __OFADD__(T x, U y)227 {228 if ( sizeof(T) < sizeof(U) )229 {230 U x2 = x;231 int8 sx = __SETS__(x2);232 return ((1 ^ sx) ^ __SETS__(y)) & (sx ^ __SETS__(x2+y));233 }234 else235 {236 T y2 = y;237 int8 sx = __SETS__(x);238 return ((1 ^ sx) ^ __SETS__(y2)) & (sx ^ __SETS__(x+y2));239 }240 }241 242 // carry flag of subtraction (x-y)243 template int8 __CFSUB__(T x, U y)244 {245 int size = sizeof(T) > sizeof(U) ? sizeof(T) : sizeof(U);246 if ( size == 1 )247 return uint8(x) < uint8(y);248 if ( size == 2 )249 return uint16(x) < uint16(y);250 if ( size == 4 )251 return uint32(x) < uint32(y);252 return uint64(x) < uint64(y);253 }254 255 // carry flag of addition (x+y)256 template int8 __CFADD__(T x, U y)257 {258 int size = sizeof(T) > sizeof(U) ? sizeof(T) : sizeof(U);259 if ( size == 1 )260 return uint8(x) > uint8(x+y);261 if ( size == 2 )262 return uint16(x) > uint16(x+y);263 if ( size == 4 )264 return uint32(x) > uint32(x+y);265 return uint64(x) > uint64(x+y);266 }267 268 #else269 // The following definition is not quite correct because it always returns270 // uint64. The above C++ functions are good, though.271 #define __PAIR__(high, low) (((uint64)(high)< >y)277 #define __CFADD__(x, y) invalid_operation // Generate carry flag for (x+y)278 #define __CFSUB__(x, y) invalid_operation // Generate carry flag for (x-y)279 #define __OFADD__(x, y) invalid_operation // Generate overflow flag for (x+y)280 #define __OFSUB__(x, y) invalid_operation // Generate overflow flag for (x-y)281 #endif282 283 // No definition for rcl/rcr because the carry flag is unknown284 #define __RCL__(x, y) invalid_operation // Rotate left thru carry285 #define __RCR__(x, y) invalid_operation // Rotate right thru carry286 #define __MKCRCL__(x, y) invalid_operation // Generate carry flag for a RCL287 #define __MKCRCR__(x, y) invalid_operation // Generate carry flag for a RCR288 #define __SETP__(x, y) invalid_operation // Generate parity flag for (x-y)289 290 // In the decompilation listing there are some objects declarared as _UNKNOWN291 // because we could not determine their types. Since the C compiler does not292 // accept void item declarations, we replace them by anything of our choice,293 // for example a char:294 295 #define _UNKNOWN char296 297 #ifdef _MSC_VER298 #define snprintf _snprintf299 #define vsnprintf _vsnprintf300 #endif