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.

421 lines
17 KiB

  1. #if !defined(OBJECTAPI__Validate_h__INCLUDED)
  2. #define OBJECTAPI__Validate_h__INCLUDED
  3. #pragma once
  4. #if DBG
  5. // AutoDebug functions that are only available in DEBUG builds
  6. inline BOOL IsBadCode(const void * pv)
  7. {
  8. return IsBadCodePtr((FARPROC) pv);
  9. }
  10. template <class T>
  11. inline BOOL IsBadRead(const void * pv, T cb)
  12. {
  13. return IsBadReadPtr(pv, (UINT_PTR) cb);
  14. }
  15. template <class T>
  16. inline BOOL IsBadWrite(void * pv, T cb)
  17. {
  18. return IsBadWritePtr(pv, (UINT_PTR) cb);
  19. }
  20. inline BOOL IsBadString(LPCTSTR pv, int cb)
  21. {
  22. return IsBadStringPtr(pv, (UINT_PTR) cb);
  23. }
  24. inline BOOL IsBadStringA(LPCSTR pv, int cb)
  25. {
  26. return IsBadStringPtrA(pv, (UINT_PTR) cb);
  27. }
  28. inline BOOL IsBadStringW(LPCWSTR pv, int cb)
  29. {
  30. return IsBadStringPtrW(pv, (UINT_PTR) cb);
  31. }
  32. #else // DBG
  33. inline BOOL IsBadCode(const void * pv)
  34. {
  35. UNREFERENCED_PARAMETER(pv);
  36. return FALSE;
  37. }
  38. template <class T>
  39. inline BOOL IsBadRead(const void * pv, T cb)
  40. {
  41. UNREFERENCED_PARAMETER(pv);
  42. UNREFERENCED_PARAMETER(cb);
  43. return FALSE;
  44. }
  45. template <class T>
  46. inline BOOL IsBadWrite(void * pv, T cb)
  47. {
  48. UNREFERENCED_PARAMETER(pv);
  49. UNREFERENCED_PARAMETER(cb);
  50. return FALSE;
  51. }
  52. inline BOOL IsBadString(LPCTSTR pv, int cb)
  53. {
  54. UNREFERENCED_PARAMETER(pv);
  55. UNREFERENCED_PARAMETER(cb);
  56. return FALSE;
  57. }
  58. inline BOOL IsBadStringA(LPCSTR pv, int cb)
  59. {
  60. UNREFERENCED_PARAMETER(pv);
  61. UNREFERENCED_PARAMETER(cb);
  62. return FALSE;
  63. }
  64. inline BOOL IsBadStringW(LPCWSTR pv, int cb)
  65. {
  66. UNREFERENCED_PARAMETER(pv);
  67. UNREFERENCED_PARAMETER(cb);
  68. return FALSE;
  69. }
  70. #endif // DBG
  71. //
  72. // API Entry / Exit setup rountines
  73. //
  74. #if DBG
  75. #define BEGIN_API(defermsg, pctx) \
  76. HRESULT retval = DU_E_GENERIC; \
  77. \
  78. { \
  79. Context * pctxThread = pctx; \
  80. ContextLock cl; \
  81. if (!cl.LockNL(defermsg, pctxThread)) { \
  82. retval = E_INVALIDARG; \
  83. goto ErrorExit; \
  84. } \
  85. AssertInstance(pctxThread); \
  86. \
  87. if (pctx != ::RawGetContext()) { \
  88. PromptInvalid("Must use Gadget inside correct Context"); \
  89. retval = DU_E_INVALIDCONTEXT; \
  90. goto ErrorExit; \
  91. } \
  92. if (pmsg == NULL) { \
  93. PromptInvalid("Must specify a valid message"); \
  94. retval = E_INVALIDARG; \
  95. goto ErrorExit; \
  96. } \
  97. #else // DBG
  98. #define BEGIN_API(defermsg, pctx) \
  99. HRESULT retval = DU_E_GENERIC; \
  100. \
  101. { \
  102. Context * pctxThread = pctx; \
  103. ContextLock cl; \
  104. if (!cl.LockNL(defermsg, pctxThread)) { \
  105. retval = E_INVALIDARG; \
  106. goto ErrorExit; \
  107. } \
  108. \
  109. if (pmsg == NULL) { \
  110. PromptInvalid("Must specify a valid message"); \
  111. retval = E_INVALIDARG; \
  112. goto ErrorExit; \
  113. } \
  114. #endif // DBG
  115. #define END_API() \
  116. goto ErrorExit; \
  117. ErrorExit: \
  118. /* Unlocks the Context here */ \
  119. ; \
  120. } \
  121. return retval;
  122. #define BEGIN_API_NOLOCK() \
  123. HRESULT retval = DU_E_GENERIC; \
  124. \
  125. { \
  126. #define END_API_NOLOCK() \
  127. goto ErrorExit; \
  128. ErrorExit: \
  129. ; \
  130. } \
  131. return retval;
  132. #define BEGIN_API_NOCONTEXT() \
  133. HRESULT retval = DU_E_GENERIC; \
  134. #define END_API_NOCONTEXT() \
  135. goto ErrorExit; \
  136. ErrorExit: \
  137. return retval;
  138. #define CHECK_MODIFY() \
  139. if (pctxThread->IsReadOnly()) { \
  140. PromptInvalid("Can not call modifying function while in read-only state / callback"); \
  141. retval = DU_E_READONLYCONTEXT; \
  142. goto ErrorExit; \
  143. } \
  144. //
  145. // Individual parameter validation rountines
  146. //
  147. #define VALIDATE_GADGETCONTEXT(v) \
  148. { \
  149. Context * pctxGad = (v)->GetContext(); \
  150. if (pctxThread != pctxGad) { \
  151. PromptInvalid("Must use Gadget inside correct Context"); \
  152. retval = DU_E_INVALIDCONTEXT; \
  153. goto ErrorExit; \
  154. } \
  155. }
  156. #define VALIDATE_VALUE(x, v) \
  157. if (x != v) { \
  158. retval = E_INVALIDARG; \
  159. goto ErrorExit; \
  160. }
  161. #define VALIDATE_HWND(wnd) \
  162. if ((h##wnd == NULL) || (!IsWindow(h##wnd))) { \
  163. PromptInvalid("Handle is not a valid Window"); \
  164. retval = E_INVALIDARG; \
  165. goto ErrorExit; \
  166. }
  167. #define VALIDATE_REGION(rgn) \
  168. if (h##rgn == NULL) { \
  169. PromptInvalid("Handle is not a valid region"); \
  170. retval = E_INVALIDARG; \
  171. goto ErrorExit; \
  172. }
  173. #define VALIDATE_OBJECT(r, v) \
  174. { \
  175. v = BaseObject::ValidateHandle(r); \
  176. if (v == NULL) { \
  177. PromptInvalid("Handle is not a valid object"); \
  178. retval = E_INVALIDARG; \
  179. goto ErrorExit; \
  180. } \
  181. }
  182. #define VALIDATE_EVENTGADGET(r, v) \
  183. { \
  184. v = ValidateBaseGadget(r); \
  185. if (v == NULL) { \
  186. PromptInvalid("Handle is not a valid Gadget"); \
  187. retval = E_INVALIDARG; \
  188. goto ErrorExit; \
  189. } \
  190. VALIDATE_GADGETCONTEXT(v) \
  191. }
  192. #define VALIDATE_EVENTGADGET_NOCONTEXT(r, v) \
  193. { \
  194. v = ValidateBaseGadget(r); \
  195. if (v == NULL) { \
  196. PromptInvalid("Handle is not a valid Gadget"); \
  197. retval = E_INVALIDARG; \
  198. goto ErrorExit; \
  199. } \
  200. }
  201. #define VALIDATE_VISUAL(r, v) \
  202. { \
  203. v = ValidateVisual(r); \
  204. if (v == NULL) { \
  205. PromptInvalid("Handle is not a valid Gadget"); \
  206. retval = E_INVALIDARG; \
  207. goto ErrorExit; \
  208. } \
  209. VALIDATE_GADGETCONTEXT(v) \
  210. }
  211. #define VALIDATE_ROOTGADGET(r, v) \
  212. { \
  213. { \
  214. DuVisual * pgadTemp = ValidateVisual(r); \
  215. if (pgadTemp == NULL) { \
  216. PromptInvalid("Handle is not a valid Gadget"); \
  217. retval = E_INVALIDARG; \
  218. goto ErrorExit; \
  219. } \
  220. if (!pgadTemp->IsRoot()) { \
  221. goto ErrorExit; \
  222. } \
  223. VALIDATE_GADGETCONTEXT(pgadTemp) \
  224. v = (DuRootGadget *) pgadTemp; \
  225. } \
  226. }
  227. #define VALIDATE_VISUAL_OR_NULL(r, v) \
  228. { \
  229. if (r == NULL) { \
  230. v = NULL; \
  231. } else { \
  232. v = ValidateVisual(r); \
  233. if (v == NULL) { \
  234. PromptInvalid("Handle is not a valid Gadget"); \
  235. retval = E_INVALIDARG; \
  236. goto ErrorExit; \
  237. } \
  238. VALIDATE_GADGETCONTEXT(v) \
  239. } \
  240. }
  241. #define VALIDATE_TRANSITION(trx) \
  242. { \
  243. BaseObject * pbase##trx = BaseObject::ValidateHandle(h##trx); \
  244. p##trx = CastTransition(pbase##trx); \
  245. if (p##trx == NULL) { \
  246. PromptInvalid("Handle is not a valid Transition"); \
  247. retval = E_INVALIDARG; \
  248. goto ErrorExit; \
  249. } \
  250. }
  251. #define VALIDATE_FLAGS(f, m) \
  252. if ((f & m) != f) { \
  253. PromptInvalid("Specified flags are invalid"); \
  254. retval = E_INVALIDARG; \
  255. goto ErrorExit; \
  256. }
  257. #define VALIDATE_RANGE(i, a, b) \
  258. if (((i) < (a)) || ((i) > (b))) { \
  259. PromptInvalid("Value is outside expected range"); \
  260. retval = E_INVALIDARG; \
  261. goto ErrorExit; \
  262. } \
  263. #define VALIDATE_CODE_PTR(p) \
  264. if ((p == NULL) || IsBadCode(p)) { \
  265. PromptInvalid("Bad code pointer: " STRINGIZE(p)); \
  266. retval = E_INVALIDARG; \
  267. goto ErrorExit; \
  268. } \
  269. #define VALIDATE_CODE_PTR_OR_NULL(p) \
  270. if ((p != NULL) && IsBadCode((FARPROC) p)) { \
  271. PromptInvalid("Bad code pointer: " STRINGIZE(p)); \
  272. retval = E_INVALIDARG; \
  273. goto ErrorExit; \
  274. } \
  275. #define VALIDATE_READ_PTR(p) \
  276. if ((p == NULL) || IsBadRead(p, sizeof(char *))) { \
  277. PromptInvalid("Bad read pointer: " STRINGIZE(p)); \
  278. retval = E_INVALIDARG; \
  279. goto ErrorExit; \
  280. } \
  281. #define VALIDATE_READ_PTR_(p, b) \
  282. if ((p == NULL) || IsBadRead(p, b)) { \
  283. PromptInvalid("Bad read pointer: " STRINGIZE(p)); \
  284. retval = E_INVALIDARG; \
  285. goto ErrorExit; \
  286. } \
  287. #define VALIDATE_READ_PTR_OR_NULL_(p, b) \
  288. if ((p != NULL) && IsBadRead(p, b)) { \
  289. PromptInvalid("Bad read pointer: " STRINGIZE(p)); \
  290. retval = E_INVALIDARG; \
  291. goto ErrorExit; \
  292. } \
  293. #define VALIDATE_READ_STRUCT(p, s) \
  294. if ((p == NULL) || IsBadRead(p, sizeof(s))) { \
  295. PromptInvalid("Bad read pointer: " STRINGIZE(p)); \
  296. retval = E_INVALIDARG; \
  297. goto ErrorExit; \
  298. } \
  299. if (p->cbSize != sizeof(s)) { \
  300. PromptInvalid("Structure is not expected size for " STRINGIZE(s)); \
  301. retval = E_INVALIDARG; \
  302. goto ErrorExit; \
  303. }
  304. #define VALIDATE_WRITE_PTR(p) \
  305. if ((p == NULL) || IsBadWrite(p, sizeof(char *))) { \
  306. PromptInvalid("Bad write pointer: " STRINGIZE(p)); \
  307. retval = E_INVALIDARG; \
  308. goto ErrorExit; \
  309. } \
  310. #define VALIDATE_WRITE_PTR_(p, b) \
  311. if ((p == NULL) || IsBadWrite(p, b)) { \
  312. PromptInvalid("Bad write pointer: " STRINGIZE(p)); \
  313. retval = E_INVALIDARG; \
  314. goto ErrorExit; \
  315. } \
  316. #define VALIDATE_WRITE_PTR_OR_NULL_(p, b) \
  317. if ((p != NULL) && IsBadWrite(p, b)) { \
  318. PromptInvalid("Bad write pointer: " STRINGIZE(p)); \
  319. retval = E_INVALIDARG; \
  320. goto ErrorExit; \
  321. } \
  322. #define VALIDATE_WRITE_STRUCT(p, s) \
  323. if ((p == NULL) || IsBadWrite(p, sizeof(s))) { \
  324. PromptInvalid("Bad write pointer: " STRINGIZE(p)); \
  325. retval = E_INVALIDARG; \
  326. goto ErrorExit; \
  327. } \
  328. if (p->cbSize != sizeof(s)) { \
  329. PromptInvalid("Structure is not expected size for " STRINGIZE(s)); \
  330. retval = E_INVALIDARG; \
  331. goto ErrorExit; \
  332. }
  333. #define VALIDATE_STRING_PTR(p, cch) \
  334. if ((p == NULL) || IsBadString(p, cch)) { \
  335. PromptInvalid("Bad string pointer: " STRINGIZE(p)); \
  336. retval = E_INVALIDARG; \
  337. goto ErrorExit; \
  338. } \
  339. #define VALIDATE_STRINGA_PTR(p, cch) \
  340. if ((p == NULL) || IsBadStringA(p, cch)) { \
  341. PromptInvalid("Bad string pointer: " STRINGIZE(p)); \
  342. retval = E_INVALIDARG; \
  343. goto ErrorExit; \
  344. } \
  345. #define VALIDATE_STRINGW_PTR(p, cch) \
  346. if ((p == NULL) || IsBadStringW(p, cch)) { \
  347. PromptInvalid("Bad string pointer: " STRINGIZE(p)); \
  348. retval = E_INVALIDARG; \
  349. goto ErrorExit; \
  350. } \
  351. #endif // OBJECTAPI__Validate_h__INCLUDED