Source code of Windows XP (NT5)
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.

225 lines
5.2 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. validate.h
  5. Abstract:
  6. Author:
  7. Vlad Sadovsky (vlads) 26-Jun-1998
  8. Revision History:
  9. 26-Jun-1998 VladS created
  10. --*/
  11. #ifndef _validate_h_
  12. #define _validate_h_
  13. #include <stidebug.h>
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /* parameter validation macros */
  18. /*
  19. * call as:
  20. *
  21. * bOK = IS_VALID_READ_PTR(pfoo, CFOO);
  22. *
  23. * bOK = IS_VALID_HANDLE(hfoo, FOO);
  24. */
  25. #ifdef DEBUG
  26. #define IS_VALID_READ_PTR(ptr, type) \
  27. (IsBadReadPtr((ptr), sizeof(type)) ? \
  28. (DPRINTF(DM_ERROR, TEXT("invalid %hs read pointer - %#08lx"), (LPCSTR)#type TEXT(" *"), (ptr)), FALSE) : \
  29. TRUE)
  30. #define IS_VALID_WRITE_PTR(ptr, type) \
  31. (IsBadWritePtr((PVOID)(ptr), sizeof(type)) ? \
  32. (DPRINTF(DM_ERROR, TEXT("invalid %hs write pointer - %#08lx"), (LPCSTR)#type TEXT(" *"), (ptr)), FALSE) : \
  33. TRUE)
  34. #define IS_VALID_STRING_PTRA(ptr, cch) \
  35. ((IsBadReadPtr((ptr), sizeof(char)) || IsBadStringPtrA((ptr), (UINT)(cch))) ? \
  36. (DPRINTF(DM_ERROR, TEXT("invalid LPSTR pointer - %#08lx"), (ptr)), FALSE) : \
  37. TRUE)
  38. #define IS_VALID_STRING_PTRW(ptr, cch) \
  39. ((IsBadReadPtr((ptr), sizeof(WCHAR)) || IsBadStringPtrW((ptr), (UINT)(cch))) ? \
  40. (DPRINTF(DM_ERROR, TEXT("invalid LPWSTR pointer - %#08lx"), (ptr)), FALSE) : \
  41. TRUE)
  42. #define IS_VALID_CODE_PTR(ptr, type) \
  43. (IsBadCodePtr((FARPROC)(ptr)) ? \
  44. (DPRINTF(DM_ERROR, TEXT("invalid %hs code pointer - %#08lx"), (LPCSTR)#type, (ptr)), FALSE) : \
  45. TRUE)
  46. #define IS_VALID_READ_BUFFER(ptr, type, len) \
  47. (IsBadReadPtr((ptr), sizeof(type)*(len)) ? \
  48. (DPRINTF(DM_ERROR, TEXT("invalid %hs read buffer pointer - %#08lx"), (LPCSTR)#type TEXT(" *"), (ptr)), FALSE) : \
  49. TRUE)
  50. #define IS_VALID_WRITE_BUFFER(ptr, type, len) \
  51. (IsBadWritePtr((ptr), sizeof(type)*(len)) ? \
  52. (DPRINTF(DM_ERROR, TEXT("invalid %hs write buffer pointer - %#08lx"), (LPCSTR)#type TEXT(" *"), (ptr)), FALSE) : \
  53. TRUE)
  54. #define FLAGS_ARE_VALID(dwFlags, dwAllFlags) \
  55. (((dwFlags) & (~(dwAllFlags))) ? \
  56. (DPRINTF(DM_ERROR, TEXT("invalid flags set - %#08lx"), ((dwFlags) & (~(dwAllFlags)))), FALSE) : \
  57. TRUE)
  58. #define IS_VALID_PIDL(ptr) \
  59. ( !IsValidPIDL(ptr) ? \
  60. (DPRINTF(DM_ERROR, TEXT("invalid PIDL pointer - %#08lx"), (ptr)), FALSE) : \
  61. TRUE)
  62. #define IS_VALID_SIZE(cb, cbExpected) \
  63. ((cb) != (cbExpected) ? \
  64. (DPRINTF(DM_ERROR, TEXT("invalid size - is %#08lx, expected %#08lx"), (cb), (cbExpected)), FALSE) : \
  65. TRUE)
  66. #else
  67. #define IS_VALID_READ_PTR(ptr, type) \
  68. (! IsBadReadPtr((ptr), sizeof(type)))
  69. #define IS_VALID_WRITE_PTR(ptr, type) \
  70. (! IsBadWritePtr((PVOID)(ptr), sizeof(type)))
  71. #define IS_VALID_STRING_PTRA(ptr, cch) \
  72. (! IsBadStringPtrA((ptr), (UINT)(cch)))
  73. #define IS_VALID_STRING_PTRW(ptr, cch) \
  74. (! IsBadStringPtrW((ptr), (UINT)(cch)))
  75. #define IS_VALID_CODE_PTR(ptr, type) \
  76. (! IsBadCodePtr((FARPROC)(ptr)))
  77. #define IS_VALID_READ_BUFFER(ptr, type, len) \
  78. (! IsBadReadPtr((ptr), sizeof(type)*(len)))
  79. #define IS_VALID_WRITE_BUFFER(ptr, type, len) \
  80. (! IsBadWritePtr((ptr), sizeof(type)*(len)))
  81. #define FLAGS_ARE_VALID(dwFlags, dwAllFlags) \
  82. (((dwFlags) & (~(dwAllFlags))) ? FALSE : TRUE)
  83. #define IS_VALID_PIDL(ptr) \
  84. (IsValidPIDL(ptr))
  85. #define IS_VALID_SIZE(cb, cbExpected) \
  86. ((cb) == (cbExpected))
  87. #endif
  88. #ifdef UNICODE
  89. #define IS_VALID_STRING_PTR IS_VALID_STRING_PTRW
  90. #else
  91. #define IS_VALID_STRING_PTR IS_VALID_STRING_PTRA
  92. #endif
  93. /* handle validation macros */
  94. #ifdef DEBUG
  95. #define IS_VALID_HANDLET(hnd, type) \
  96. (IsValidH##type(hnd) ? \
  97. TRUE : \
  98. (DPRINTF(DM_ERROR, TEXT("invalid H") #type TEXT(" - %#08lx"), (hnd)), FALSE))
  99. #else
  100. #define IS_VALID_HANDLET(hnd, type) \
  101. (IsValidH##type(hnd))
  102. #endif
  103. //
  104. // Validation macros
  105. //
  106. //#define IS_VALID_HANDLE(h) (((h) != NULL) && ((h) != INVALID_HANDLE_VALUE))
  107. #define IS_VALID_HANDLE(hnd) (IsValidHANDLE(hnd))
  108. /* structure validation macros */
  109. // Define VSTF if you want to validate the fields in structures. This
  110. // requires a handler function (of the form IsValid*()) that knows how
  111. // to validate the specific structure type.
  112. #ifdef VSTF
  113. #ifdef DEBUG
  114. #define IS_VALID_STRUCT_PTR(ptr, type) \
  115. (IsValidP##type(ptr) ? \
  116. TRUE : \
  117. (DPRINTF(DM_ERROR, TEXT("invalid %hs pointer - %#08lx"), (LPCSTR)#type TEXT(" *"), (ptr)), FALSE))
  118. #define IS_VALID_STRUCTEX_PTR(ptr, type, x) \
  119. (IsValidP##type(ptr, x) ? \
  120. TRUE : \
  121. (DPRINTF(DM_ERROR, TEXT("invalid %hs pointer - %#08lx"), (LPCSTR)#type TEXT(" *"), (ptr)), FALSE))
  122. #else
  123. #define IS_VALID_STRUCT_PTR(ptr, type) \
  124. (IsValidP##type(ptr))
  125. #define IS_VALID_STRUCTEX_PTR(ptr, type, x) \
  126. (IsValidP##type(ptr, x))
  127. #endif
  128. #else
  129. #define IS_VALID_STRUCT_PTR(ptr, type) \
  130. (! IsBadReadPtr((ptr), sizeof(type)))
  131. #define IS_VALID_STRUCTEX_PTR(ptr, type, x) \
  132. (! IsBadReadPtr((ptr), sizeof(type)))
  133. #endif // VSTF
  134. /* OLE interface validation macro */
  135. #define IS_VALID_INTERFACE_PTR(ptr, iface) \
  136. IS_VALID_STRUCT_PTR(ptr, ##iface)
  137. BOOL IsValidHANDLE(HANDLE hnd); // Compares with NULL and INVALID_HANDLE_VALUE
  138. BOOL IsValidHANDLE2(HANDLE hnd); // Compares with INVALID_HANDLE_VALUE
  139. BOOL
  140. IsValidHWND(
  141. HWND hwnd);
  142. BOOL
  143. IsValidHMENU(
  144. HMENU hmenu);
  145. BOOL
  146. IsValidShowCmd(
  147. int nShow);
  148. #ifdef __cplusplus
  149. };
  150. #endif
  151. #endif // _validate_h_