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.

272 lines
7.1 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. #ifndef WIN16
  24. #define IS_VALID_STRING_PTRA(ptr, cch) \
  25. ((IsBadReadPtr((ptr), sizeof(char)) || IsBadStringPtrA((ptr), (UINT)(cch))) ? \
  26. (TraceMsgA(TF_ERROR, "invalid LPSTR pointer - %#08lx", (ptr)), FALSE) : \
  27. TRUE)
  28. #define IS_VALID_STRING_PTRW(ptr, cch) \
  29. ((IsBadReadPtr((ptr), sizeof(WCHAR)) || IsBadStringPtrW((ptr), (UINT)(cch))) ? \
  30. (TraceMsgA(TF_ERROR, "invalid LPWSTR pointer - %#08lx", (ptr)), FALSE) : \
  31. TRUE)
  32. #else
  33. #define IS_VALID_STRING_PTRA(ptr, cch) \
  34. ((IsBadReadPtr((ptr), sizeof(char)) || IsBadStringPtr((ptr), (UINT)(cch))) ? \
  35. (TraceMsg(TF_ERROR, "invalid LPSTR pointer - %#08lx", (ptr)), FALSE) : \
  36. TRUE)
  37. #define IS_VALID_STRING_PTRW(ptr, cch) \
  38. ((IsBadReadPtr((ptr), sizeof(WCHAR)) || IsBadStringPtr((ptr), (UINT)(cch))) ? \
  39. (TraceMsg(TF_ERROR, "invalid LPWSTR pointer - %#08lx", (ptr)), FALSE) : \
  40. TRUE)
  41. #endif
  42. #define IS_VALID_CODE_PTR(ptr, type) \
  43. (IsBadCodePtr((FARPROC)(ptr)) ? \
  44. (TraceMsgA(TF_ERROR, "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. (TraceMsgA(TF_ERROR, "invalid %hs read buffer pointer - %#08lx", (LPCSTR)#type" *", (ptr)), FALSE) : \
  49. TRUE)
  50. #define IS_VALID_WRITE_BUFFER(ptr, type, len) \
  51. (IsBadWritePtr((ptr), sizeof(type)*(len)) ? \
  52. (TraceMsgA(TF_ERROR, "invalid %hs write buffer pointer - %#08lx", (LPCSTR)#type" *", (ptr)), FALSE) : \
  53. TRUE)
  54. #define FLAGS_ARE_VALID(dwFlags, dwAllFlags) \
  55. (((dwFlags) & (~(dwAllFlags))) ? \
  56. (TraceMsgA(TF_ERROR, "invalid flags set - %#08lx", ((dwFlags) & (~(dwAllFlags)))), FALSE) : \
  57. TRUE)
  58. #define IS_VALID_PIDL(ptr) \
  59. ( !IsValidPIDL(ptr) ? \
  60. (TraceMsgA(TF_ERROR, "invalid PIDL pointer - %#08lx", (ptr)), FALSE) : \
  61. TRUE)
  62. #define IS_VALID_SIZE(cb, cbExpected) \
  63. ((cb) != (cbExpected) ? \
  64. (TraceMsgA(TF_ERROR, "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_HANDLE(hnd, type) \
  96. (IsValidH##type(hnd) ? \
  97. TRUE : \
  98. (TraceMsgA(TF_ERROR, "invalid H" #type " - %#08lx", (hnd)), FALSE))
  99. #else
  100. #define IS_VALID_HANDLE(hnd, type) \
  101. (IsValidH##type(hnd))
  102. #endif
  103. /* structure validation macros */
  104. // Define VSTF if you want to validate the fields in structures. This
  105. // requires a handler function (of the form IsValid*()) that knows how
  106. // to validate the specific structure type.
  107. #ifdef VSTF
  108. #ifdef DEBUG
  109. #define IS_VALID_STRUCT_PTR(ptr, type) \
  110. (IsValidP##type(ptr) ? \
  111. TRUE : \
  112. (TraceMsgA(TF_ERROR, "invalid %hs pointer - %#08lx", (LPCSTR)#type" *", (ptr)), FALSE))
  113. #define IS_VALID_STRUCTEX_PTR(ptr, type, x) \
  114. (IsValidP##type(ptr, x) ? \
  115. TRUE : \
  116. (TraceMsgA(TF_ERROR, "invalid %hs pointer - %#08lx", (LPCSTR)#type" *", (ptr)), FALSE))
  117. #else
  118. #define IS_VALID_STRUCT_PTR(ptr, type) \
  119. (IsValidP##type(ptr))
  120. #define IS_VALID_STRUCTEX_PTR(ptr, type, x) \
  121. (IsValidP##type(ptr, x))
  122. #endif
  123. #else
  124. #define IS_VALID_STRUCT_PTR(ptr, type) \
  125. (! IsBadReadPtr((ptr), sizeof(type)))
  126. #define IS_VALID_STRUCTEX_PTR(ptr, type, x) \
  127. (! IsBadReadPtr((ptr), sizeof(type)))
  128. #endif // VSTF
  129. /* OLE interface validation macro */
  130. #define IS_VALID_INTERFACE_PTR(ptr, iface) \
  131. IS_VALID_STRUCT_PTR(ptr, ##iface)
  132. #if !defined(NO_SHELL_VALIDATION)
  133. BOOL IsValidPathA(LPCSTR pcszPath);
  134. BOOL IsValidPathW(LPCWSTR pcszPath);
  135. BOOL IsValidPathResultA(HRESULT hr, LPCSTR pcszPath, UINT cchPathBufLen);
  136. BOOL IsValidPathResultW(HRESULT hr, LPCWSTR pcszPath, UINT cchPathBufLen);
  137. BOOL IsValidExtensionA(LPCSTR pcszExt);
  138. BOOL IsValidExtensionW(LPCWSTR pcszExt);
  139. BOOL IsValidIconIndexA(HRESULT hr, LPCSTR pcszIconFile, UINT cchIconFileBufLen, int niIcon);
  140. BOOL IsValidIconIndexW(HRESULT hr, LPCWSTR pcszIconFile, UINT cchIconFileBufLen, int niIcon);
  141. BOOL IsFullPathA(LPCSTR pcszPath);
  142. BOOL IsFullPathW(LPCWSTR pcszPath);
  143. BOOL IsStringContainedA(LPCSTR pcszBigger, LPCSTR pcszSuffix);
  144. BOOL IsStringContainedW(LPCWSTR pcszBigger, LPCWSTR pcszSuffix);
  145. #ifdef UNICODE
  146. #define IsValidPath IsValidPathW
  147. #define IsValidPathResult IsValidPathResultW
  148. #define IsValidExtension IsValidExtensionW
  149. #define IsValidIconIndex IsValidIconIndexW
  150. #define IsFullPath IsFullPathW
  151. #define IsStringContained IsStringContainedW
  152. #else
  153. #define IsValidPath IsValidPathA
  154. #define IsValidPathResult IsValidPathResultA
  155. #define IsValidExtension IsValidExtensionA
  156. #define IsValidIconIndex IsValidIconIndexA
  157. #define IsFullPath IsFullPathA
  158. #define IsStringContained IsStringContainedA
  159. #endif
  160. BOOL IsValidHANDLE(HANDLE hnd); // Compares with NULL and INVALID_HANDLE_VALUE
  161. BOOL IsValidHANDLE2(HANDLE hnd); // Compares with INVALID_HANDLE_VALUE
  162. #define IsValidHEVENT IsValidHANDLE
  163. #define IsValidHGLOBAL IsValidHANDLE
  164. #define IsValidHFILE IsValidHANDLE
  165. #define IsValidHINSTANCE IsValidHANDLE
  166. #define IsValidHICON IsValidHANDLE
  167. #define IsValidHKEY IsValidHANDLE
  168. #define IsValidHMODULE IsValidHANDLE
  169. #define IsValidHPROCESS IsValidHANDLE
  170. BOOL
  171. IsValidHWND(
  172. HWND hwnd);
  173. BOOL
  174. IsValidHMENU(
  175. HMENU hmenu);
  176. BOOL
  177. IsValidShowCmd(
  178. int nShow);
  179. // Hack for components like shlwapi who don't #include commctrl.h
  180. #ifdef DPA_GetPtrCount
  181. BOOL
  182. IsValidHDPA(
  183. HDPA hdpa);
  184. BOOL
  185. IsValidHDSA(
  186. HDSA hdsa);
  187. #endif
  188. // For components like comctl32 who don't #include shlobj.h
  189. #ifdef _SHLOBJ_H_
  190. BOOL
  191. IsValidPIDL(
  192. LPCITEMIDLIST pidl);
  193. #endif
  194. #endif // NO_SHELL_VALIDATION
  195. #ifdef __cplusplus
  196. };
  197. #endif
  198. #endif // _validate_h_