Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
8.0 KiB

  1. //
  2. // Common defines used in the mars project.
  3. //
  4. #ifndef __MARSDEV_H
  5. #define __MARSDEV_H
  6. // Number of elements in array
  7. #define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0]))
  8. // Size of struct up to but not including specified member
  9. #define STRUCT_SIZE_TO_MEMBER(s,m) ((DWORD_PTR)(&(((s *)0)->m)))
  10. // Size of a single member of a structure
  11. #define SIZEOF_MEMBER(s,m) sizeof(((s *)0)->m)
  12. // Size of struct up to and including specified member
  13. #define STRUCT_SIZE_INCLUDING_MEMBER(s,m) (STRUCT_SIZE_TO_MEMBER(s,m) + SIZEOF_MEMBER(s,m))
  14. #define SAFERELEASE(p) if ((p) != NULL) { (p)->Release(); (p) = NULL; } else;
  15. // For destructor use -- doesn't NULL pointer
  16. #define SAFERELEASE2(p) if ((p) != NULL) { (p)->Release();} else;
  17. // Do strong typechecking
  18. #ifdef SAFECAST
  19. #undef SAFECAST
  20. #endif
  21. #define SAFECAST(_src, _type) (static_cast<_type>(_src))
  22. //
  23. // Validation functions.
  24. //
  25. // These functions can all easily corrupt memory
  26. /*
  27. #define IsValidReadPtr(ptr) \
  28. ((ptr) && !IsBadReadPtr((ptr), sizeof(*(ptr))))
  29. #define IsValidWritePtr(ptr) \
  30. ((ptr) && !IsBadWritePtr((ptr), sizeof(*(ptr))))
  31. #define IsValidStringW(pstr) \
  32. ((pstr) && !IsBadStringPtrW((pstr), (UINT)-1))
  33. #define IsValidReadBuffer(ptr, n) \
  34. ((ptr) && !IsBadReadPtr((ptr), sizeof(*(ptr)) * (n)))
  35. #define IsValidWriteBuffer(ptr, n) \
  36. ((ptr) && !IsBadWritePtr((ptr), sizeof(*(ptr)) * (n)))
  37. #define IsValidInterfacePtr(punk) \
  38. ((punk) && IsValidReadPtr(punk) && \
  39. !IsBadCodePtr(*((FARPROC*)punk)))
  40. #define IsValidFunctionPtr(pfunc) \
  41. ((NULL != pfunc) && \
  42. !IsBadCodePtr((FARPROC)pfunc))
  43. */
  44. #define IsValidReadPtr(ptr) (ptr != NULL)
  45. #define IsValidWritePtr(ptr) (ptr != NULL)
  46. #define IsValidStringW(pstr) (pstr != NULL)
  47. #define IsValidReadBuffer(ptr, n) (ptr != NULL)
  48. #define IsValidWriteBuffer(ptr, n) (ptr != NULL)
  49. #define IsValidInterfacePtr(punk) (punk != NULL)
  50. #define IsValidFunctionPtr(pfunc) (pfunc != NULL)
  51. #define IsValidBstr(bstr) \
  52. ((bstr) && IsValidWriteBuffer((BYTE*)(bstr), SysStringByteLen(bstr)))
  53. #define IsValidOptionalBstr(bstr) \
  54. ((!bstr) || IsValidBstr(bstr))
  55. #define IsValidVariantBoolVal(vb) \
  56. (VARIANT_FALSE == vb || VARIANT_TRUE == vb)
  57. #define IsValidVariantI4(var) \
  58. (VT_I4 == (var).vt)
  59. #define IsValidVariantBstr(var) \
  60. (VT_BSTR == (var).vt && IsValidBstr((var).bstrVal))
  61. #define IsValidVariantMissingOptional(var) \
  62. (VT_ERROR == (var).vt && DISP_E_PARAMNOTFOUND == (var).scode)
  63. #define IsValidFlag(f, fAll) \
  64. (!((f) & ~(fAll)))
  65. BOOL IsValidVariant(VARIANT var);
  66. BOOL IsValidStringPtrBufferW(LPOLESTR* ppstr, UINT n);
  67. #define IsValidString IsValidStringW
  68. #define IsValidStringPtrBuffer IsValidStringPtrBufferW
  69. //
  70. // API parameter validation helpers. Use these on public APIs. If a parameter
  71. // is bad on debug build a RIP message will be generated.
  72. //
  73. #ifdef DEBUG
  74. BOOL API_IsValidReadPtr(void* ptr, UINT cbSize);
  75. BOOL API_IsValidWritePtr(void* ptr, UINT cbSize);
  76. BOOL API_IsValidStringW(LPCWSTR psz);
  77. BOOL API_IsValidReadBuffer(void* ptr, UINT cbSize, UINT n);
  78. BOOL API_IsValidWriteBuffer(void* ptr, UINT cbSize, UINT n);
  79. BOOL API_IsValidInterfacePtr(IUnknown* punk);
  80. BOOL API_IsValidFunctionPtr(void *pfunc);
  81. BOOL API_IsValidVariant(VARIANT var);
  82. BOOL API_IsValidVariantI4(VARIANT var);
  83. BOOL API_IsValidVariantBstr(VARIANT var);
  84. BOOL API_IsValidBstr(BSTR bstr);
  85. BOOL API_IsValidOptionalBstr(BSTR bstr);
  86. BOOL API_IsValidFlag(DWORD dwFlag, DWORD dwAllFlags);
  87. BOOL API_IsValidStringPtrBufferW(LPOLESTR* ppStr, UINT n);
  88. #define API_IsValidString API_IsValidStringW
  89. #define API_IsValidStringPtrBuffer API_IsValidStringPtrBufferW
  90. #endif //Debug
  91. #ifdef DEBUG
  92. #define API_IsValidReadPtr(ptr) \
  93. API_IsValidReadPtr((ptr), sizeof(*(ptr)))
  94. #define API_IsValidWritePtr(ptr) \
  95. API_IsValidWritePtr((ptr), sizeof(*(ptr)))
  96. #define API_IsValidReadBuffer(ptr, n) \
  97. API_IsValidReadBuffer((ptr), sizeof(*(ptr)), (n))
  98. #define API_IsValidWriteBuffer(ptr, n) \
  99. API_IsValidWriteBuffer((ptr), sizeof(*(ptr)), (n))
  100. #else // DEBUG
  101. #define API_IsValidReadPtr IsValidReadPtr
  102. #define API_IsValidWritePtr IsValidWritePtr
  103. #define API_IsValidString IsValidString
  104. #define API_IsValidStringW IsValidStringW
  105. #define API_IsValidReadBuffer IsValidReadBuffer
  106. #define API_IsValidWriteBuffer IsValidWriteBuffer
  107. #define API_IsValidInterfacePtr IsValidInterfacePtr
  108. #define API_IsValidFunctionPtr IsValidFunctionPtr
  109. #define API_IsValidVariant IsValidVariant
  110. #define API_IsValidVariantI4 IsValidVariantI4
  111. #define API_IsValidVariantBstr IsValidVariantBstr
  112. #define API_IsValidBstr IsValidBstr
  113. #define API_IsValidOptionalBstr IsValidOptionalBstr
  114. #define API_IsValidFlag IsValidFlag
  115. #define API_IsValidStringPtrBuffer IsValidStringPtrBufferW
  116. #endif // DEBUG
  117. //
  118. // Function prototypes.
  119. //
  120. BOOL StrEqlW(LPCWSTR psz1, LPCWSTR psz2);
  121. BOOL StrEqlA(LPCSTR psz1, LPCSTR psz2);
  122. #define StrEql StrEqlW
  123. UINT64 HexStringToUINT64W(LPCWSTR lpwstr);
  124. //
  125. // Macro magic to help define away functions.
  126. //
  127. // TO USE:
  128. //
  129. // If you don't want a function to be used in the code do the following:
  130. // #undef funcA
  131. // #define funcA DON_USE(funcA, funcB)
  132. //
  133. // This will result in funcA being redefined to Don_not_use_funcA_use_funcB.
  134. // A compilation error complaining that Don_not_use_funcA_use_funcB is undefined
  135. // will be generated whenever anyone tries to use funcA.
  136. //
  137. #define MACRO_CAT(a,b) \
  138. a##b
  139. #define DONT_USE(a,b) \
  140. MACRO_CAT(Do_not_use_##a,_use_##b)
  141. // return SCRIPT_ERROR upon serious error that shouldn't occur; will break in debug builds
  142. #ifdef DEBUG
  143. #define SCRIPT_ERROR E_FAIL
  144. #else
  145. #define SCRIPT_ERROR S_FALSE
  146. #endif
  147. #define RECTWIDTH(rc) ((rc).right-(rc).left)
  148. #define RECTHEIGHT(rc) ((rc).bottom-(rc).top)
  149. HRESULT SanitizeResult(HRESULT hr);
  150. // BITBOOL macros just make using single-bit bools a little safer. You can't nonchalantly assign
  151. // any "int" value to a bit bool and expect it to always work. "BOOLIFY" it first.
  152. //
  153. #define BOOLIFY(expr) (!!(expr))
  154. // BUGBUG (scotth): we should probably make this a 'bool', but be careful
  155. // because the Alpha compiler might not recognize it yet. Talk to AndyP.
  156. // This isn't a BOOL because BOOL is signed and the compiler produces
  157. // sloppy code when testing for a single bit.
  158. typedef DWORD BITBOOL;
  159. //
  160. #define VARIANT_BOOLIFY(expr) ((expr) ? VARIANT_TRUE : VARIANT_FALSE)
  161. /*
  162. TraceResult Macros
  163. The idea behind these macros is to have one entry and exit point per
  164. function to reduce errors (primarily bad state / leaks). They generally
  165. require an HRESULT hr, and an 'exit' label that returns hr and performs
  166. any cleanup that might be needed.
  167. In addition to encouraging a unified exit point, these macros also debug
  168. spew if something fails (try to only use these macros on things that
  169. should never fail). This can be extremely useful when something is
  170. failing many layers deep in the code. To see the spew, you need to set
  171. TF_TRACERESULT. To break on such failures, set BF_TRACERESULT.
  172. Common mistake: you must set hr when you use IF_FAILEXIT as it is not
  173. automatically set to _hresult (for flexibility).
  174. */
  175. #define IF_FAILEXIT(_hresult) \
  176. if (FAILED(_hresult)) { \
  177. TraceResult(hr); \
  178. goto exit; \
  179. } else
  180. #define IF_NULLEXIT(_palloc) \
  181. if (NULL == (_palloc)) { \
  182. hr = TraceResult(E_OUTOFMEMORY); \
  183. goto exit; \
  184. } else
  185. #define IF_TRUEEXIT(_expression, _hresult) \
  186. if (_expression) { \
  187. hr = TraceResult(_hresult); \
  188. goto exit; \
  189. } else
  190. #define IF_FALSEEXIT(_expression, _hresult) \
  191. if (FALSE == _expression) { \
  192. hr = TraceResult(_hresult); \
  193. goto exit; \
  194. } else
  195. #define TraceResult(_hresult) _hresult
  196. #endif // __MARSDEV_H