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.

256 lines
7.0 KiB

  1. #ifndef _validate_h_
  2. #define _validate_h_
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. /* parameter validation macros */
  7. /*
  8. * call as:
  9. *
  10. * bOK = IS_VALID_READ_PTR(pfoo, CFOO);
  11. *
  12. * bOK = IS_VALID_HANDLE(hfoo, FOO);
  13. */
  14. #ifdef DEBUG
  15. #define IS_VALID_READ_PTR(ptr, type) \
  16. (IsBadReadPtr((ptr), sizeof(type)) ? \
  17. (TraceMsgA(TF_ERROR, "invalid %hs read pointer - %#08lx", (LPCSTR)#type" *", (ptr)), FALSE) : \
  18. TRUE)
  19. #define IS_VALID_WRITE_PTR(ptr, type) \
  20. (IsBadWritePtr((PVOID)(ptr), sizeof(type)) ? \
  21. (TraceMsgA(TF_ERROR, "invalid %hs write pointer - %#08lx", (LPCSTR)#type" *", (ptr)), FALSE) : \
  22. TRUE)
  23. #define IS_VALID_STRING_PTRA(ptr, cch) \
  24. ((IsBadReadPtr((ptr), sizeof(char)) || IsBadStringPtrA((ptr), (UINT_PTR)(cch))) ? \
  25. (TraceMsgA(TF_ERROR, "invalid LPSTR pointer - %#08lx", (ptr)), FALSE) : \
  26. TRUE)
  27. #define IS_VALID_STRING_PTRW(ptr, cch) \
  28. ((IsBadReadPtr((ptr), sizeof(WCHAR)) || IsBadStringPtrW((ptr), (UINT_PTR)(cch))) ? \
  29. (TraceMsgA(TF_ERROR, "invalid LPWSTR pointer - %#08lx", (ptr)), FALSE) : \
  30. TRUE)
  31. #define IS_VALID_CODE_PTR(ptr, type) \
  32. (IsBadCodePtr((FARPROC)(ptr)) ? \
  33. (TraceMsgA(TF_ERROR, "invalid %hs code pointer - %#08lx", (LPCSTR)#type, (ptr)), FALSE) : \
  34. TRUE)
  35. #define IS_VALID_READ_BUFFER(ptr, type, len) \
  36. (IsBadReadPtr((ptr), sizeof(type)*(len)) ? \
  37. (TraceMsgA(TF_ERROR, "invalid %hs read buffer pointer - %#08lx", (LPCSTR)#type" *", (ptr)), FALSE) : \
  38. TRUE)
  39. #define IS_VALID_WRITE_BUFFER(ptr, type, len) \
  40. (IsBadWritePtr((ptr), sizeof(type)*(len)) ? \
  41. (TraceMsgA(TF_ERROR, "invalid %hs write buffer pointer - %#08lx", (LPCSTR)#type" *", (ptr)), FALSE) : \
  42. TRUE)
  43. #define FLAGS_ARE_VALID(dwFlags, dwAllFlags) \
  44. (((dwFlags) & (~(dwAllFlags))) ? \
  45. (TraceMsgA(TF_ERROR, "invalid flags set - %#08lx", ((dwFlags) & (~(dwAllFlags)))), FALSE) : \
  46. TRUE)
  47. #define IS_VALID_PIDL(ptr) \
  48. ( !IsValidPIDL(ptr) ? \
  49. (TraceMsgA(TF_ERROR, "invalid PIDL pointer - %#08lx", (ptr)), FALSE) : \
  50. TRUE)
  51. #define IS_VALID_SIZE(cb, cbExpected) \
  52. ((cb) != (cbExpected) ? \
  53. (TraceMsgA(TF_ERROR, "invalid size - is %#08lx, expected %#08lx", (cb), (cbExpected)), FALSE) : \
  54. TRUE)
  55. #else
  56. #define IS_VALID_READ_PTR(ptr, type) \
  57. (! IsBadReadPtr((ptr), sizeof(type)))
  58. #define IS_VALID_WRITE_PTR(ptr, type) \
  59. (! IsBadWritePtr((PVOID)(ptr), sizeof(type)))
  60. #define IS_VALID_STRING_PTRA(ptr, cch) \
  61. (! IsBadStringPtrA((ptr), (UINT_PTR)(cch)))
  62. #define IS_VALID_STRING_PTRW(ptr, cch) \
  63. (! IsBadStringPtrW((ptr), (UINT_PTR)(cch)))
  64. #define IS_VALID_CODE_PTR(ptr, type) \
  65. (! IsBadCodePtr((FARPROC)(ptr)))
  66. #define IS_VALID_READ_BUFFER(ptr, type, len) \
  67. (! IsBadReadPtr((ptr), sizeof(type)*(len)))
  68. #define IS_VALID_WRITE_BUFFER(ptr, type, len) \
  69. (! IsBadWritePtr((ptr), sizeof(type)*(len)))
  70. #define FLAGS_ARE_VALID(dwFlags, dwAllFlags) \
  71. (((dwFlags) & (~(dwAllFlags))) ? FALSE : TRUE)
  72. #define IS_VALID_PIDL(ptr) \
  73. (IsValidPIDL(ptr))
  74. #define IS_VALID_SIZE(cb, cbExpected) \
  75. ((cb) == (cbExpected))
  76. #endif
  77. #ifdef UNICODE
  78. #define IS_VALID_STRING_PTR IS_VALID_STRING_PTRW
  79. #else
  80. #define IS_VALID_STRING_PTR IS_VALID_STRING_PTRA
  81. #endif
  82. /* handle validation macros */
  83. #ifdef DEBUG
  84. #define IS_VALID_HANDLE(hnd, type) \
  85. (IsValidH##type(hnd) ? \
  86. TRUE : \
  87. (TraceMsgA(TF_ERROR, "invalid H" #type " - %#08lx", (hnd)), FALSE))
  88. #else
  89. #define IS_VALID_HANDLE(hnd, type) \
  90. (IsValidH##type(hnd))
  91. #endif
  92. /* structure validation macros */
  93. // Define VSTF if you want to validate the fields in structures. This
  94. // requires a handler function (of the form IsValid*()) that knows how
  95. // to validate the specific structure type.
  96. #ifdef VSTF
  97. #ifdef DEBUG
  98. #define IS_VALID_STRUCT_PTR(ptr, type) \
  99. (IsValidP##type(ptr) ? \
  100. TRUE : \
  101. (TraceMsgA(TF_ERROR, "invalid %hs pointer - %#08lx", (LPCSTR)#type" *", (ptr)), FALSE))
  102. #define IS_VALID_STRUCTEX_PTR(ptr, type, x) \
  103. (IsValidP##type(ptr, x) ? \
  104. TRUE : \
  105. (TraceMsgA(TF_ERROR, "invalid %hs pointer - %#08lx", (LPCSTR)#type" *", (ptr)), FALSE))
  106. #else
  107. #define IS_VALID_STRUCT_PTR(ptr, type) \
  108. (IsValidP##type(ptr))
  109. #define IS_VALID_STRUCTEX_PTR(ptr, type, x) \
  110. (IsValidP##type(ptr, x))
  111. #endif
  112. #else
  113. #define IS_VALID_STRUCT_PTR(ptr, type) \
  114. (! IsBadReadPtr((ptr), sizeof(type)))
  115. #define IS_VALID_STRUCTEX_PTR(ptr, type, x) \
  116. (! IsBadReadPtr((ptr), sizeof(type)))
  117. #endif // VSTF
  118. /* OLE interface validation macro */
  119. #define IS_VALID_INTERFACE_PTR(ptr, iface) \
  120. IS_VALID_STRUCT_PTR(ptr, ##iface)
  121. #if !defined(NO_SHELL_VALIDATION)
  122. BOOL IsValidPathA(LPCSTR pcszPath);
  123. BOOL IsValidPathW(LPCWSTR pcszPath);
  124. BOOL IsValidPathResultA(HRESULT hr, LPCSTR pcszPath, UINT cchPathBufLen);
  125. BOOL IsValidPathResultW(HRESULT hr, LPCWSTR pcszPath, UINT cchPathBufLen);
  126. BOOL IsValidExtensionA(LPCSTR pcszExt);
  127. BOOL IsValidExtensionW(LPCWSTR pcszExt);
  128. BOOL IsValidIconIndexA(HRESULT hr, LPCSTR pcszIconFile, UINT cchIconFileBufLen, int niIcon);
  129. BOOL IsValidIconIndexW(HRESULT hr, LPCWSTR pcszIconFile, UINT cchIconFileBufLen, int niIcon);
  130. BOOL IsFullPathA(LPCSTR pcszPath);
  131. BOOL IsFullPathW(LPCWSTR pcszPath);
  132. BOOL IsStringContainedA(LPCSTR pcszBigger, LPCSTR pcszSuffix);
  133. BOOL IsStringContainedW(LPCWSTR pcszBigger, LPCWSTR pcszSuffix);
  134. #ifdef UNICODE
  135. #define IsValidPath IsValidPathW
  136. #define IsValidPathResult IsValidPathResultW
  137. #define IsValidExtension IsValidExtensionW
  138. #define IsValidIconIndex IsValidIconIndexW
  139. #define IsFullPath IsFullPathW
  140. #define IsStringContained IsStringContainedW
  141. #else
  142. #define IsValidPath IsValidPathA
  143. #define IsValidPathResult IsValidPathResultA
  144. #define IsValidExtension IsValidExtensionA
  145. #define IsValidIconIndex IsValidIconIndexA
  146. #define IsFullPath IsFullPathA
  147. #define IsStringContained IsStringContainedA
  148. #endif
  149. BOOL IsValidHANDLE(HANDLE hnd); // Compares with NULL and INVALID_HANDLE_VALUE
  150. BOOL IsValidHANDLE2(HANDLE hnd); // Compares with INVALID_HANDLE_VALUE
  151. #define IsValidHEVENT IsValidHANDLE
  152. #define IsValidHGLOBAL IsValidHANDLE
  153. #define IsValidHFILE IsValidHANDLE
  154. #define IsValidHINSTANCE IsValidHANDLE
  155. #define IsValidHICON IsValidHANDLE
  156. #define IsValidHKEY IsValidHANDLE
  157. #define IsValidHMODULE IsValidHANDLE
  158. #define IsValidHPROCESS IsValidHANDLE
  159. BOOL
  160. IsValidHWND(
  161. HWND hwnd);
  162. BOOL
  163. IsValidHMENU(
  164. HMENU hmenu);
  165. BOOL
  166. IsValidShowCmd(
  167. int nShow);
  168. // The next few functions must declare the structure types explicitly
  169. // so we don't need to worry about whether <shlobj.h> and/or <comctrlp.h>
  170. // was included before or after us.
  171. BOOL
  172. IsValidPIDL(
  173. const UNALIGNED struct _ITEMIDLIST *pidl);
  174. BOOL
  175. IsValidHDPA(
  176. struct _DPA *hdpa);
  177. BOOL
  178. IsValidHDSA(
  179. struct _DSA *hdsa);
  180. #endif // NO_SHELL_VALIDATION
  181. #ifdef __cplusplus
  182. };
  183. #endif
  184. #endif // _validate_h_