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.

224 lines
4.2 KiB

  1. //
  2. // Validation code
  3. //
  4. #include "stock.h"
  5. #pragma hdrstop
  6. //
  7. // Validations
  8. //
  9. // Hack: size of the internal data structures, as defined in comctl32\da.c.
  10. // We measure this by the number of DWORD-sized fields.
  11. #ifdef DEBUG
  12. #define CB_DSA (6 * sizeof(DWORD))
  13. #define CB_DPA (6 * sizeof(DWORD))
  14. #else
  15. #define CB_DSA (5 * sizeof(DWORD))
  16. #define CB_DPA (5 * sizeof(DWORD))
  17. #endif
  18. BOOL
  19. IsValidHDSA(
  20. HDSA hdsa)
  21. {
  22. return (IS_VALID_WRITE_BUFFER(hdsa, BYTE, CB_DSA));
  23. }
  24. BOOL
  25. IsValidHDPA(
  26. HDPA hdpa)
  27. {
  28. return (IS_VALID_WRITE_BUFFER(hdpa, BYTE, CB_DPA));
  29. }
  30. BOOL
  31. IsValidPIDL(
  32. LPCITEMIDLIST pidl)
  33. {
  34. return (IS_VALID_READ_PTR(pidl, USHORT) &&
  35. IS_VALID_READ_BUFFER((LPBYTE)pidl+sizeof(USHORT), BYTE, pidl->mkid.cb) &&
  36. (0 == _ILNext(pidl)->mkid.cb || IS_VALID_PIDL(_ILNext(pidl))));
  37. }
  38. BOOL
  39. IsValidHWND(
  40. HWND hwnd)
  41. {
  42. /* Ask User if this is a valid window. */
  43. return(IsWindow(hwnd));
  44. }
  45. BOOL
  46. IsValidHMENU(
  47. HMENU hmenu)
  48. {
  49. return IsMenu(hmenu);
  50. }
  51. BOOL
  52. IsValidHANDLE(
  53. HANDLE hnd)
  54. {
  55. return(NULL != hnd && INVALID_HANDLE_VALUE != hnd);
  56. }
  57. BOOL
  58. IsValidHANDLE2(
  59. HANDLE hnd)
  60. {
  61. return(hnd != INVALID_HANDLE_VALUE);
  62. }
  63. BOOL
  64. IsValidShowCmd(
  65. int nShow)
  66. {
  67. BOOL bResult;
  68. switch (nShow)
  69. {
  70. case SW_HIDE:
  71. case SW_SHOWNORMAL:
  72. case SW_SHOWMINIMIZED:
  73. case SW_SHOWMAXIMIZED:
  74. case SW_SHOWNOACTIVATE:
  75. case SW_SHOW:
  76. case SW_MINIMIZE:
  77. case SW_SHOWMINNOACTIVE:
  78. case SW_SHOWNA:
  79. case SW_RESTORE:
  80. case SW_SHOWDEFAULT:
  81. bResult = TRUE;
  82. break;
  83. default:
  84. bResult = FALSE;
  85. TraceMsg(TF_ERROR, "IsValidShowCmd(): Invalid show command %d.",
  86. nShow);
  87. break;
  88. }
  89. return(bResult);
  90. }
  91. BOOL
  92. IsValidPathA(
  93. LPCSTR pcszPath)
  94. {
  95. return(IS_VALID_STRING_PTRA(pcszPath, MAX_PATH) &&
  96. EVAL((UINT)lstrlenA(pcszPath) < MAX_PATH));
  97. }
  98. BOOL
  99. IsValidPathW(
  100. LPCWSTR pcszPath)
  101. {
  102. return(IS_VALID_STRING_PTRW(pcszPath, MAX_PATH) &&
  103. EVAL((UINT)lstrlenW(pcszPath) < MAX_PATH));
  104. }
  105. BOOL
  106. IsValidPathResultA(
  107. HRESULT hr,
  108. LPCSTR pcszPath,
  109. UINT cchPathBufLen)
  110. {
  111. return((hr == S_OK &&
  112. EVAL(IsValidPathA(pcszPath)) &&
  113. EVAL((UINT)lstrlenA(pcszPath) < cchPathBufLen)) ||
  114. (hr != S_OK &&
  115. EVAL(! cchPathBufLen ||
  116. ! pcszPath ||
  117. ! *pcszPath)));
  118. }
  119. BOOL
  120. IsValidPathResultW(
  121. HRESULT hr,
  122. LPCWSTR pcszPath,
  123. UINT cchPathBufLen)
  124. {
  125. return((hr == S_OK &&
  126. EVAL(IsValidPathW(pcszPath)) &&
  127. EVAL((UINT)lstrlenW(pcszPath) < cchPathBufLen)) ||
  128. (hr != S_OK &&
  129. EVAL(! cchPathBufLen ||
  130. ! pcszPath ||
  131. ! *pcszPath)));
  132. }
  133. BOOL
  134. IsValidExtensionA(
  135. LPCSTR pcszExt)
  136. {
  137. return(IS_VALID_STRING_PTRA(pcszExt, MAX_PATH) &&
  138. EVAL(lstrlenA(pcszExt) < MAX_PATH) &&
  139. EVAL(*pcszExt == '.'));
  140. }
  141. BOOL
  142. IsValidExtensionW(
  143. LPCWSTR pcszExt)
  144. {
  145. return(IS_VALID_STRING_PTRW(pcszExt, MAX_PATH) &&
  146. EVAL(lstrlenW(pcszExt) < MAX_PATH) &&
  147. EVAL(*pcszExt == TEXTW('.')));
  148. }
  149. BOOL
  150. IsValidIconIndexA(
  151. HRESULT hr,
  152. LPCSTR pcszIconFile,
  153. UINT cchIconFileBufLen,
  154. int niIcon)
  155. {
  156. return(EVAL(IsValidPathResultA(hr, pcszIconFile, cchIconFileBufLen)) &&
  157. EVAL(hr == S_OK ||
  158. ! niIcon));
  159. }
  160. BOOL
  161. IsValidIconIndexW(
  162. HRESULT hr,
  163. LPCWSTR pcszIconFile,
  164. UINT cchIconFileBufLen,
  165. int niIcon)
  166. {
  167. return(EVAL(IsValidPathResultW(hr, pcszIconFile, cchIconFileBufLen)) &&
  168. EVAL(hr == S_OK ||
  169. ! niIcon));
  170. }
  171. BOOL IsStringContainedA(LPCSTR pcszBigger, LPCSTR pcszSuffix)
  172. {
  173. ASSERT(IS_VALID_STRING_PTRA(pcszBigger, -1));
  174. ASSERT(IS_VALID_STRING_PTRA(pcszSuffix, -1));
  175. return (pcszSuffix >= pcszBigger &&
  176. pcszSuffix <= pcszBigger + lstrlenA(pcszBigger));
  177. }
  178. BOOL IsStringContainedW(LPCWSTR pcszBigger, LPCWSTR pcszSuffix)
  179. {
  180. ASSERT(IS_VALID_STRING_PTRW(pcszBigger, -1));
  181. ASSERT(IS_VALID_STRING_PTRW(pcszSuffix, -1));
  182. return (pcszSuffix >= pcszBigger &&
  183. pcszSuffix <= pcszBigger + lstrlenW(pcszBigger));
  184. }