miniminater

defs.h

Aug 5th, 2021
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.03 KB | None | 0 0
  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-2017 Hex-Rays
  8.  
  9. */
  10.  
  11. #ifndef HEXRAYS_DEFS_H
  12. #define HEXRAYS_DEFS_H
  13.  
  14. #if defined(__GNUC__)
  15.   typedef          long long ll;
  16.   typedef unsigned long long ull;
  17.   #define __int64 long long
  18.   #define __int32 int
  19.   #define __int16 short
  20.   #define __int8  char
  21.   #define MAKELL(num) num ## LL
  22.   #define FMT_64 "ll"
  23. #elif defined(_MSC_VER)
  24.   typedef          __int64 ll;
  25.   typedef unsigned __int64 ull;
  26.   #define MAKELL(num) num ## i64
  27.   #define FMT_64 "I64"
  28. #elif defined (__BORLANDC__)
  29.   typedef          __int64 ll;
  30.   typedef unsigned __int64 ull;
  31.   #define MAKELL(num) num ## i64
  32.   #define FMT_64 "L"
  33. #else
  34.   #error "unknown compiler"
  35. #endif
  36. typedef unsigned int uint;
  37. typedef unsigned char uchar;
  38. typedef unsigned short ushort;
  39. typedef unsigned long ulong;
  40.  
  41. typedef          char   int8;
  42. typedef   signed char   sint8;
  43. typedef unsigned char   uint8;
  44. typedef          short  int16;
  45. typedef   signed short  sint16;
  46. typedef unsigned short  uint16;
  47. typedef          int    int32;
  48. typedef   signed int    sint32;
  49. typedef unsigned int    uint32;
  50. typedef ll              int64;
  51. typedef ll              sint64;
  52. typedef ull             uint64;
  53.  
  54. // Partially defined types. They are used when the decompiler does not know
  55. // anything about the type except its size.
  56. #define _BYTE  uint8
  57. #define _WORD  uint16
  58. #define _DWORD uint32
  59. #define _QWORD uint64
  60. #if !defined(_MSC_VER)
  61. #define _LONGLONG __int128
  62. #endif
  63.  
  64. // Non-standard boolean types. They are used when the decompiler can not use
  65. // the standard "bool" type because of the size mistmatch but the possible
  66. // values are only 0 and 1. See also 'BOOL' type below.
  67. typedef int8 _BOOL1;
  68. typedef int16 _BOOL2;
  69. typedef int32 _BOOL4;
  70.  
  71. #ifndef _WINDOWS_
  72. typedef int8 BYTE;
  73. typedef int16 WORD;
  74. typedef int32 DWORD;
  75. typedef int32 LONG;
  76. typedef int BOOL;       // uppercase BOOL is usually 4 bytes
  77. #endif
  78. typedef int64 QWORD;
  79. #ifndef __cplusplus
  80. typedef int bool;       // we want to use bool in our C programs
  81. #endif
  82.  
  83. #define __pure          // pure function: always returns the same value, has no
  84.                         // side effects
  85.  
  86. // Non-returning function
  87. #if defined(__GNUC__)
  88. #define __noreturn  __attribute__((noreturn))
  89. #else
  90. #define __noreturn  __declspec(noreturn)
  91. #endif
  92.  
  93.  
  94. #ifndef NULL
  95. #define NULL 0
  96. #endif
  97.  
  98. // Some convenience macros to make partial accesses nicer
  99. #define LAST_IND(x,part_type)    (sizeof(x)/sizeof(part_type) - 1)
  100. #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
  101. #  define LOW_IND(x,part_type)   LAST_IND(x,part_type)
  102. #  define HIGH_IND(x,part_type)  0
  103. #else
  104. #  define HIGH_IND(x,part_type)  LAST_IND(x,part_type)
  105. #  define LOW_IND(x,part_type)   0
  106. #endif
  107. // first unsigned macros:
  108. #define BYTEn(x, n)   (*((_BYTE*)&(x)+n))
  109. #define WORDn(x, n)   (*((_WORD*)&(x)+n))
  110. #define DWORDn(x, n)  (*((_DWORD*)&(x)+n))
  111.  
  112. #define LOBYTE(x)  BYTEn(x,LOW_IND(x,_BYTE))
  113. #define LOWORD(x)  WORDn(x,LOW_IND(x,_WORD))
  114. #define LODWORD(x) DWORDn(x,LOW_IND(x,_DWORD))
  115. #define HIBYTE(x)  BYTEn(x,HIGH_IND(x,_BYTE))
  116. #define HIWORD(x)  WORDn(x,HIGH_IND(x,_WORD))
  117. #define HIDWORD(x) DWORDn(x,HIGH_IND(x,_DWORD))
  118. #define BYTE1(x)   BYTEn(x,  1)         // byte 1 (counting from 0)
  119. #define BYTE2(x)   BYTEn(x,  2)
  120. #define BYTE3(x)   BYTEn(x,  3)
  121. #define BYTE4(x)   BYTEn(x,  4)
  122. #define BYTE5(x)   BYTEn(x,  5)
  123. #define BYTE6(x)   BYTEn(x,  6)
  124. #define BYTE7(x)   BYTEn(x,  7)
  125. #define BYTE8(x)   BYTEn(x,  8)
  126. #define BYTE9(x)   BYTEn(x,  9)
  127. #define BYTE10(x)  BYTEn(x, 10)
  128. #define BYTE11(x)  BYTEn(x, 11)
  129. #define BYTE12(x)  BYTEn(x, 12)
  130. #define BYTE13(x)  BYTEn(x, 13)
  131. #define BYTE14(x)  BYTEn(x, 14)
  132. #define BYTE15(x)  BYTEn(x, 15)
  133. #define WORD1(x)   WORDn(x,  1)
  134. #define WORD2(x)   WORDn(x,  2)         // third word of the object, unsigned
  135. #define WORD3(x)   WORDn(x,  3)
  136. #define WORD4(x)   WORDn(x,  4)
  137. #define WORD5(x)   WORDn(x,  5)
  138. #define WORD6(x)   WORDn(x,  6)
  139. #define WORD7(x)   WORDn(x,  7)
  140.  
  141. // now signed macros (the same but with sign extension)
  142. #define SBYTEn(x, n)   (*((int8*)&(x)+n))
  143. #define SWORDn(x, n)   (*((int16*)&(x)+n))
  144. #define SDWORDn(x, n)  (*((int32*)&(x)+n))
  145.  
  146. #define SLOBYTE(x)  SBYTEn(x,LOW_IND(x,int8))
  147. #define SLOWORD(x)  SWORDn(x,LOW_IND(x,int16))
  148. #define SLODWORD(x) SDWORDn(x,LOW_IND(x,int32))
  149. #define SHIBYTE(x)  SBYTEn(x,HIGH_IND(x,int8))
  150. #define SHIWORD(x)  SWORDn(x,HIGH_IND(x,int16))
  151. #define SHIDWORD(x) SDWORDn(x,HIGH_IND(x,int32))
  152. #define SBYTE1(x)   SBYTEn(x,  1)
  153. #define SBYTE2(x)   SBYTEn(x,  2)
  154. #define SBYTE3(x)   SBYTEn(x,  3)
  155. #define SBYTE4(x)   SBYTEn(x,  4)
  156. #define SBYTE5(x)   SBYTEn(x,  5)
  157. #define SBYTE6(x)   SBYTEn(x,  6)
  158. #define SBYTE7(x)   SBYTEn(x,  7)
  159. #define SBYTE8(x)   SBYTEn(x,  8)
  160. #define SBYTE9(x)   SBYTEn(x,  9)
  161. #define SBYTE10(x)  SBYTEn(x, 10)
  162. #define SBYTE11(x)  SBYTEn(x, 11)
  163. #define SBYTE12(x)  SBYTEn(x, 12)
  164. #define SBYTE13(x)  SBYTEn(x, 13)
  165. #define SBYTE14(x)  SBYTEn(x, 14)
  166. #define SBYTE15(x)  SBYTEn(x, 15)
  167. #define SWORD1(x)   SWORDn(x,  1)
  168. #define SWORD2(x)   SWORDn(x,  2)
  169. #define SWORD3(x)   SWORDn(x,  3)
  170. #define SWORD4(x)   SWORDn(x,  4)
  171. #define SWORD5(x)   SWORDn(x,  5)
  172. #define SWORD6(x)   SWORDn(x,  6)
  173. #define SWORD7(x)   SWORDn(x,  7)
  174.  
  175.  
  176. // Helper functions to represent some assembly instructions.
  177.  
  178. #ifdef __cplusplus
  179.  
  180. // compile time assertion
  181. #define __CASSERT_N0__(l) COMPILE_TIME_ASSERT_ ## l
  182. #define __CASSERT_N1__(l) __CASSERT_N0__(l)
  183. #define CASSERT(cnd) typedef char __CASSERT_N1__(__LINE__) [(cnd) ? 1 : -1]
  184.  
  185. // check that unsigned multiplication does not overflow
  186. template<class T> bool is_mul_ok(T count, T elsize)
  187. {
  188.   CASSERT((T)(-1) > 0); // make sure T is unsigned
  189.   if ( elsize  == 0 || count == 0 )
  190.     return true;
  191.   return count <= ((T)(-1)) / elsize;
  192. }
  193.  
  194. // multiplication that saturates (yields the biggest value) instead of overflowing
  195. // such a construct is useful in "operator new[]"
  196. template<class T> bool saturated_mul(T count, T elsize)
  197. {
  198.   return is_mul_ok(count, elsize) ? count * elsize : T(-1);
  199. }
  200.  
  201. #include <stddef.h> // for size_t
  202.  
  203. // memcpy() with determined behavoir: it always copies
  204. // from the start to the end of the buffer
  205. // note: it copies byte by byte, so it is not equivalent to, for example, rep movsd
  206. inline void *qmemcpy(void *dst, const void *src, size_t cnt)
  207. {
  208.   char *out = (char *)dst;
  209.   const char *in = (const char *)src;
  210.   while ( cnt > 0 )
  211.   {
  212.     *out++ = *in++;
  213.     --cnt;
  214.   }
  215.   return dst;
  216. }
  217.  
  218. // Generate a reference to pair of operands
  219. template<class T>  int16 __PAIR__( int8  high, T low) { return ((( int16)high) << sizeof(high)*8) | uint8(low); }
  220. template<class T>  int32 __PAIR__( int16 high, T low) { return ((( int32)high) << sizeof(high)*8) | uint16(low); }
  221. template<class T>  int64 __PAIR__( int32 high, T low) { return ((( int64)high) << sizeof(high)*8) | uint32(low); }
  222. template<class T> uint16 __PAIR__(uint8  high, T low) { return (((uint16)high) << sizeof(high)*8) | uint8(low); }
  223. template<class T> uint32 __PAIR__(uint16 high, T low) { return (((uint32)high) << sizeof(high)*8) | uint16(low); }
  224. template<class T> uint64 __PAIR__(uint32 high, T low) { return (((uint64)high) << sizeof(high)*8) | uint32(low); }
  225.  
  226. // rotate left
  227. template<class T> T __ROL__(T value, int count)
  228. {
  229.   const uint nbits = sizeof(T) * 8;
  230.  
  231.   if ( count > 0 )
  232.   {
  233.     count %= nbits;
  234.     T high = value >> (nbits - count);
  235.     if ( T(-1) < 0 ) // signed value
  236.       high &= ~((T(-1) << count));
  237.     value <<= count;
  238.     value |= high;
  239.   }
  240.   else
  241.   {
  242.     count = -count % nbits;
  243.     T low = value << (nbits - count);
  244.     value >>= count;
  245.     value |= low;
  246.   }
  247.   return value;
  248. }
  249.  
  250. inline uint8  __ROL1__(uint8  value, int count) { return __ROL__((uint8)value, count); }
  251. inline uint16 __ROL2__(uint16 value, int count) { return __ROL__((uint16)value, count); }
  252. inline uint32 __ROL4__(uint32 value, int count) { return __ROL__((uint32)value, count); }
  253. inline uint64 __ROL8__(uint64 value, int count) { return __ROL__((uint64)value, count); }
  254. inline uint8  __ROR1__(uint8  value, int count) { return __ROL__((uint8)value, -count); }
  255. inline uint16 __ROR2__(uint16 value, int count) { return __ROL__((uint16)value, -count); }
  256. inline uint32 __ROR4__(uint32 value, int count) { return __ROL__((uint32)value, -count); }
  257. inline uint64 __ROR8__(uint64 value, int count) { return __ROL__((uint64)value, -count); }
  258.  
  259. // carry flag of left shift
  260. template<class T> int8 __MKCSHL__(T value, uint count)
  261. {
  262.   const uint nbits = sizeof(T) * 8;
  263.   count %= nbits;
  264.  
  265.   return (value >> (nbits-count)) & 1;
  266. }
  267.  
  268. // carry flag of right shift
  269. template<class T> int8 __MKCSHR__(T value, uint count)
  270. {
  271.   return (value >> (count-1)) & 1;
  272. }
  273.  
  274. // sign flag
  275. template<class T> int8 __SETS__(T x)
  276. {
  277.   if ( sizeof(T) == 1 )
  278.     return int8(x) < 0;
  279.   if ( sizeof(T) == 2 )
  280.     return int16(x) < 0;
  281.   if ( sizeof(T) == 4 )
  282.     return int32(x) < 0;
  283.   return int64(x) < 0;
  284. }
  285.  
  286. // overflow flag of subtraction (x-y)
  287. template<class T, class U> int8 __OFSUB__(T x, U y)
  288. {
  289.   if ( sizeof(T) < sizeof(U) )
  290.   {
  291.     U x2 = x;
  292.     int8 sx = __SETS__(x2);
  293.     return (sx ^ __SETS__(y)) & (sx ^ __SETS__(x2-y));
  294.   }
  295.   else
  296.   {
  297.     T y2 = y;
  298.     int8 sx = __SETS__(x);
  299.     return (sx ^ __SETS__(y2)) & (sx ^ __SETS__(x-y2));
  300.   }
  301. }
  302.  
  303. // overflow flag of addition (x+y)
  304. template<class T, class U> int8 __OFADD__(T x, U y)
  305. {
  306.   if ( sizeof(T) < sizeof(U) )
  307.   {
  308.     U x2 = x;
  309.     int8 sx = __SETS__(x2);
  310.     return ((1 ^ sx) ^ __SETS__(y)) & (sx ^ __SETS__(x2+y));
  311.   }
  312.   else
  313.   {
  314.     T y2 = y;
  315.     int8 sx = __SETS__(x);
  316.     return ((1 ^ sx) ^ __SETS__(y2)) & (sx ^ __SETS__(x+y2));
  317.   }
  318. }
  319.  
  320. // carry flag of subtraction (x-y)
  321. template<class T, class U> int8 __CFSUB__(T x, U y)
  322. {
  323.   int size = sizeof(T) > sizeof(U) ? sizeof(T) : sizeof(U);
  324.   if ( size == 1 )
  325.     return uint8(x) < uint8(y);
  326.   if ( size == 2 )
  327.     return uint16(x) < uint16(y);
  328.   if ( size == 4 )
  329.     return uint32(x) < uint32(y);
  330.   return uint64(x) < uint64(y);
  331. }
  332.  
  333. // carry flag of addition (x+y)
  334. template<class T, class U> int8 __CFADD__(T x, U y)
  335. {
  336.   int size = sizeof(T) > sizeof(U) ? sizeof(T) : sizeof(U);
  337.   if ( size == 1 )
  338.     return uint8(x) > uint8(x+y);
  339.   if ( size == 2 )
  340.     return uint16(x) > uint16(x+y);
  341.   if ( size == 4 )
  342.     return uint32(x) > uint32(x+y);
  343.   return uint64(x) > uint64(x+y);
  344. }
  345.  
  346. #else
  347. // The following definition is not quite correct because it always returns
  348. // uint64. The above C++ functions are good, though.
  349. #define __PAIR__(high, low) (((uint64)(high)<<sizeof(high)*8) | low)
  350. // For C, we just provide macros, they are not quite correct.
  351. #define __ROL__(x, y) __rotl__(x, y)      // Rotate left
  352. #define __ROR__(x, y) __rotr__(x, y)      // Rotate right
  353. #define __CFSHL__(x, y) invalid_operation // Generate carry flag for (x<<y)
  354. #define __CFSHR__(x, y) invalid_operation // Generate carry flag for (x>>y)
  355. #define __CFADD__(x, y) invalid_operation // Generate carry flag for (x+y)
  356. #define __CFSUB__(x, y) invalid_operation // Generate carry flag for (x-y)
  357. #define __OFADD__(x, y) invalid_operation // Generate overflow flag for (x+y)
  358. #define __OFSUB__(x, y) invalid_operation // Generate overflow flag for (x-y)
  359. #endif
  360.  
  361. // No definition for rcl/rcr because the carry flag is unknown
  362. #define __RCL__(x, y)    invalid_operation // Rotate left thru carry
  363. #define __RCR__(x, y)    invalid_operation // Rotate right thru carry
  364. #define __MKCRCL__(x, y) invalid_operation // Generate carry flag for a RCL
  365. #define __MKCRCR__(x, y) invalid_operation // Generate carry flag for a RCR
  366. #define __SETP__(x, y)   invalid_operation // Generate parity flag for (x-y)
  367.  
  368. // In the decompilation listing there are some objects declarared as _UNKNOWN
  369. // because we could not determine their types. Since the C compiler does not
  370. // accept void item declarations, we replace them by anything of our choice,
  371. // for example a char:
  372.  
  373. #define _UNKNOWN char
  374.  
  375. #ifdef _MSC_VER
  376. #define snprintf _snprintf
  377. #define vsnprintf _vsnprintf
  378. #endif
  379.  
  380. #endif // HEXRAYS_DEFS_H
Advertisement
Add Comment
Please, Sign In to add comment