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.

244 lines
4.4 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. valid.cpp
  5. Abstract:
  6. Author:
  7. Vlad Sadovsky (vlads) 26-Jun-1997
  8. Revision History:
  9. 26-Jun-1997 VladS created
  10. --*/
  11. #include "cplusinc.h"
  12. #include "sticomm.h"
  13. //
  14. // Validation code
  15. //
  16. BOOL
  17. IsValidHWND(
  18. HWND hwnd)
  19. {
  20. /* Ask User if this is a valid window. */
  21. return(IsWindow(hwnd));
  22. }
  23. BOOL
  24. IsValidHMENU(
  25. HMENU hmenu)
  26. {
  27. return IsMenu(hmenu);
  28. }
  29. BOOL
  30. IsValidHANDLE(
  31. HANDLE hnd)
  32. {
  33. return(NULL != hnd && INVALID_HANDLE_VALUE != hnd);
  34. }
  35. BOOL
  36. IsValidHANDLE2(
  37. HANDLE hnd)
  38. {
  39. return(hnd != INVALID_HANDLE_VALUE);
  40. }
  41. BOOL
  42. IsValidShowCmd(
  43. int nShow)
  44. {
  45. BOOL bResult;
  46. switch (nShow)
  47. {
  48. case SW_HIDE:
  49. case SW_SHOWNORMAL:
  50. case SW_SHOWMINIMIZED:
  51. case SW_SHOWMAXIMIZED:
  52. case SW_SHOWNOACTIVATE:
  53. case SW_SHOW:
  54. case SW_MINIMIZE:
  55. case SW_SHOWMINNOACTIVE:
  56. case SW_SHOWNA:
  57. case SW_RESTORE:
  58. case SW_SHOWDEFAULT:
  59. bResult = TRUE;
  60. break;
  61. default:
  62. bResult = FALSE;
  63. //TraceMsg(TF_ERROR, "IsValidShowCmd(): Invalid show command %d.",nShow);
  64. break;
  65. }
  66. return(bResult);
  67. }
  68. BOOL
  69. IsValidPathA(
  70. LPCSTR pcszPath)
  71. {
  72. return(IS_VALID_STRING_PTRA(pcszPath, MAX_PATH) &&
  73. ((UINT)lstrlenA(pcszPath) < MAX_PATH));
  74. }
  75. BOOL
  76. IsValidPathW(
  77. LPCWSTR pcszPath)
  78. {
  79. return(IS_VALID_STRING_PTRW(pcszPath, MAX_PATH) &&
  80. ((UINT)lstrlenW(pcszPath) < MAX_PATH));
  81. }
  82. BOOL
  83. IsValidPathResultA(
  84. HRESULT hr,
  85. LPCSTR pcszPath,
  86. UINT cchPathBufLen)
  87. {
  88. return((hr == S_OK &&
  89. (IsValidPathA(pcszPath)) &&
  90. ((UINT)lstrlenA(pcszPath) < cchPathBufLen)) ||
  91. (hr != S_OK &&
  92. (! cchPathBufLen ||
  93. ! pcszPath ||
  94. ! *pcszPath)));
  95. }
  96. BOOL
  97. IsValidPathResultW(
  98. HRESULT hr,
  99. LPCWSTR pcszPath,
  100. UINT cchPathBufLen)
  101. {
  102. return((hr == S_OK &&
  103. (IsValidPathW(pcszPath)) &&
  104. ((UINT)lstrlenW(pcszPath) < cchPathBufLen)) ||
  105. (hr != S_OK &&
  106. (! cchPathBufLen ||
  107. ! pcszPath ||
  108. ! *pcszPath)));
  109. }
  110. BOOL
  111. IsValidExtensionA(
  112. LPCSTR pcszExt)
  113. {
  114. return(IS_VALID_STRING_PTRA(pcszExt, MAX_PATH) &&
  115. (lstrlenA(pcszExt) < MAX_PATH) &&
  116. (*pcszExt == '.'));
  117. }
  118. BOOL
  119. IsValidExtensionW(
  120. LPCWSTR pcszExt)
  121. {
  122. return(IS_VALID_STRING_PTRW(pcszExt, MAX_PATH) &&
  123. (lstrlenW(pcszExt) < MAX_PATH) &&
  124. (*pcszExt == L'.'));
  125. }
  126. BOOL
  127. IsValidIconIndexA(
  128. HRESULT hr,
  129. LPCSTR pcszIconFile,
  130. UINT cchIconFileBufLen,
  131. int niIcon)
  132. {
  133. return((IsValidPathResultA(hr, pcszIconFile, cchIconFileBufLen)) &&
  134. (hr == S_OK ||
  135. ! niIcon));
  136. }
  137. BOOL
  138. IsValidIconIndexW(
  139. HRESULT hr,
  140. LPCWSTR pcszIconFile,
  141. UINT cchIconFileBufLen,
  142. int niIcon)
  143. {
  144. return((IsValidPathResultW(hr, pcszIconFile, cchIconFileBufLen)) &&
  145. (hr == S_OK ||
  146. ! niIcon));
  147. }
  148. BOOL IsStringContainedA(LPCSTR pcszBigger, LPCSTR pcszSuffix)
  149. {
  150. ASSERT(IS_VALID_STRING_PTRA(pcszBigger, -1));
  151. ASSERT(IS_VALID_STRING_PTRA(pcszSuffix, -1));
  152. return (pcszSuffix >= pcszBigger &&
  153. pcszSuffix <= pcszBigger + lstrlenA(pcszBigger));
  154. }
  155. BOOL IsStringContainedW(LPCWSTR pcszBigger, LPCWSTR pcszSuffix)
  156. {
  157. ASSERT(IS_VALID_STRING_PTRW(pcszBigger, -1));
  158. ASSERT(IS_VALID_STRING_PTRW(pcszSuffix, -1));
  159. return (pcszSuffix >= pcszBigger &&
  160. pcszSuffix <= pcszBigger + lstrlenW(pcszBigger));
  161. }
  162. BOOL
  163. AfxIsValidString(
  164. LPCWSTR lpsz,
  165. int nLength
  166. )
  167. {
  168. if (lpsz == NULL)
  169. return FALSE;
  170. return ::IsBadStringPtrW(lpsz, nLength) == 0;
  171. }
  172. BOOL
  173. AfxIsValidString(
  174. LPCSTR lpsz,
  175. int nLength
  176. )
  177. {
  178. if (lpsz == NULL)
  179. return FALSE;
  180. return ::IsBadStringPtrA(lpsz, nLength) == 0;
  181. }
  182. BOOL
  183. AfxIsValidAddress(
  184. const void* lp,
  185. UINT nBytes,
  186. BOOL bReadWrite
  187. )
  188. {
  189. // simple version using Win-32 APIs for pointer validation.
  190. return (lp != NULL && !IsBadReadPtr(lp, nBytes) &&
  191. (!bReadWrite || !IsBadWritePtr((LPVOID)lp, nBytes)));
  192. }