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.

195 lines
7.6 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: rasclient_strsafe.h
  4. //
  5. // Synopsis: Includes strsafe.h from sdk\inc as well as adding other common RAS
  6. // string related items like Macros
  7. //
  8. // Copyright (c) 2002 Microsoft Corporation
  9. //
  10. // Author: quintinb Created 02/05/2002
  11. //
  12. //+----------------------------------------------------------------------------
  13. #ifndef _RASCLIENT_STRSAFE_INCLUDED_
  14. #define _RASCLIENT_STRSAFE_INCLUDED_
  15. #pragma once
  16. #if DEBUG && defined(CMASSERTMSG)
  17. #define CELEMS(x) ( \
  18. CMASSERTMSG(sizeof(x) != sizeof(void*), TEXT("possible incorrect usage of CELEMS")), \
  19. (sizeof(x))/(sizeof(x[0])) \
  20. )
  21. #else
  22. #define CELEMS(x) ((sizeof(x))/(sizeof(x[0])))
  23. #endif // DEBUG
  24. #include <strsafe.h>
  25. #define NULL_OKAY 0x1
  26. #define EMPTY_STRING_OKAY 0x2
  27. //+----------------------------------------------------------------------------
  28. //
  29. // Function: IsBadInputStringW
  30. //
  31. // Synopsis: Function to check if a given input string is a "good" string.
  32. // The function is basically a wrapper for IsBadStringPtr but
  33. // depending on what flags are passed to the function it will accept
  34. // NULL string pointers (c_dwNullOkay), or empty strings (c_dwEmptyStringOkay).
  35. // Note that we also require the string to be NULL terminated within
  36. // the size given.
  37. //
  38. // Arguments: pszString - String pointer to check
  39. // cchStringSizeToCheck - number of TCHARS to check in the input string (this will often be estimated)
  40. // dwCheckFlags - Flags to control what checking to do on the string or
  41. // what is an acceptable good string. Current list:
  42. // NULL_OKAY -- allows NULL pointers to be "okay"
  43. // EMPTY_STRING_OKAY -- allows the empty string ("")
  44. // to be "okay"
  45. //
  46. // Returns: BOOL - TRUE if the given pointer is bad
  47. // FALSE if the pointer passes all checks
  48. //
  49. // History: quintinb Created 02/18/02
  50. //
  51. //+----------------------------------------------------------------------------
  52. __inline BOOL __stdcall IsBadInputStringW(LPCWSTR pszString, UINT cchStringSizeToCheck, DWORD dwCheckFlags)
  53. {
  54. BOOL bReturn = TRUE; // assume the worst, a bad input string
  55. //
  56. // First check the pointer to see if it is NULL or not
  57. //
  58. if (pszString)
  59. {
  60. //
  61. // No point in having us check 0 characters in the string...
  62. //
  63. if (cchStringSizeToCheck)
  64. {
  65. //
  66. // Okay, now let's check to see if the string is readable.
  67. // Note that IsBadStringPtr will stop checking the string
  68. // when it has examined the number of bytes passed in or
  69. // when it hits a terminating NULL char, whichever it hits
  70. // first.
  71. //
  72. if (!IsBadStringPtrW(pszString, cchStringSizeToCheck*sizeof(WCHAR)))
  73. {
  74. //
  75. // Okay, it's readable. Let's check to see if it is NULL terminated
  76. // within cchStringSizeToCheck, if not then we consider it a bad string.
  77. //
  78. if(SUCCEEDED(StringCchLengthW((LPWSTR)pszString, cchStringSizeToCheck, NULL))) //Note we actually don't care how long the string is...
  79. {
  80. //
  81. // Finally, check to see if the string is the empty string
  82. //
  83. BOOL bEmptyString = (L'\0' == pszString[0]);
  84. BOOL bEmptyStringOkay = (EMPTY_STRING_OKAY == (dwCheckFlags & EMPTY_STRING_OKAY));
  85. if ((bEmptyString && bEmptyStringOkay) || !bEmptyString)
  86. {
  87. bReturn = FALSE;
  88. }
  89. }
  90. }
  91. }
  92. }
  93. else
  94. {
  95. bReturn = (NULL_OKAY != (dwCheckFlags & NULL_OKAY)); // If NULL and NULL is okay, return FALSE.
  96. }
  97. return bReturn;
  98. }
  99. //+----------------------------------------------------------------------------
  100. //
  101. // Function: IsBadInputStringA
  102. //
  103. // Synopsis: Function to check if a given input string is a "good" string.
  104. // The function is basically a wrapper for IsBadStringPtr but
  105. // depending on what flags are passed to the function it will accept
  106. // NULL string pointers (c_dwNullOkay), or empty strings (c_dwEmptyStringOkay).
  107. // Note that we also require the string to be NULL terminated within
  108. // the size given.
  109. //
  110. // Arguments: pszString - String pointer to check
  111. // cchStringSizeToCheck - number of TCHARS to check in the input string (this will often be estimated)
  112. // dwCheckFlags - Flags to control what checking to do on the string or
  113. // what is an acceptable good string. Current list:
  114. // NULL_OKAY -- allows NULL pointers to be "okay"
  115. // EMPTY_STRING_OKAY -- allows the empty string ("")
  116. // to be "okay"
  117. //
  118. // Returns: BOOL - TRUE if the given pointer is bad
  119. // FALSE if the pointer passes all checks
  120. //
  121. // History: quintinb Created 02/18/02
  122. //
  123. //+----------------------------------------------------------------------------
  124. __inline BOOL __stdcall IsBadInputStringA(LPCSTR pszString, UINT cchStringSizeToCheck, DWORD dwCheckFlags)
  125. {
  126. BOOL bReturn = TRUE; // assume the worst, a bad input string
  127. //
  128. // First check the pointer to see if it is NULL or not
  129. //
  130. if (pszString)
  131. {
  132. //
  133. // No point in having us check 0 characters in the string...
  134. //
  135. if (cchStringSizeToCheck)
  136. {
  137. //
  138. // Okay, now let's check to see if the string is readable.
  139. // Note that IsBadStringPtr will stop checking the string
  140. // when it has examined the number of bytes passed in or
  141. // when it hits a terminating NULL char, whichever it hits
  142. // first.
  143. //
  144. if (!IsBadStringPtrA(pszString, cchStringSizeToCheck*sizeof(CHAR)))
  145. {
  146. //
  147. // Okay, it's readable. Let's check to see if it is NULL terminated
  148. // within cchStringSizeToCheck, if not then we consider it a bad string.
  149. //
  150. if(SUCCEEDED(StringCchLengthA((LPSTR)pszString, cchStringSizeToCheck, NULL))) //Note we actually don't care how long the string is...
  151. {
  152. //
  153. // Finally, check to see if the string is the empty string
  154. //
  155. BOOL bEmptyString = ('\0' == pszString[0]);
  156. BOOL bEmptyStringOkay = (EMPTY_STRING_OKAY == (dwCheckFlags & EMPTY_STRING_OKAY));
  157. if ((bEmptyString && bEmptyStringOkay) || !bEmptyString)
  158. {
  159. bReturn = FALSE;
  160. }
  161. }
  162. }
  163. }
  164. }
  165. else
  166. {
  167. bReturn = (NULL_OKAY != (dwCheckFlags & NULL_OKAY)); // If NULL and NULL is okay, return FALSE.
  168. }
  169. return bReturn;
  170. }
  171. #ifdef UNICODE
  172. #define IsBadInputString IsBadInputStringW
  173. #else
  174. #define IsBadInputString IsBadInputStringA
  175. #endif
  176. #endif // _RASCLIENT_STRSAFE_INCLUDED_