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.

237 lines
5.0 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999
  5. //
  6. // File: assert.cpp
  7. //
  8. // Contents: Cert Server wrapper routines
  9. //
  10. //---------------------------------------------------------------------------
  11. #include <pch.cpp>
  12. #pragma hdrstop
  13. #define DBG_CERTSRV_DEBUG_PRINT
  14. #ifdef DBG_CERTSRV_DEBUG_PRINT
  15. __inline VOID CSDbgBreakPoint(VOID)
  16. {
  17. DebugBreak();
  18. }
  19. //+---------------------------------------------------------------------------
  20. // Function: CSPrintAssert, public
  21. //
  22. // Synopsis: Display message and break
  23. //
  24. // Arguments: [pszFailedAssertion] -- failed assertion in string form
  25. // [pszFileName] -- filename
  26. // [LineNumber] -- line number
  27. // [pszMessage] -- optional message
  28. //
  29. // Returns: None
  30. //----------------------------------------------------------------------------
  31. char const *
  32. csTrimPath(
  33. char const *pszFile)
  34. {
  35. char const *psz;
  36. char *pszT;
  37. char const *pszTrim;
  38. static char s_path[MAX_PATH];
  39. if (NULL == pszFile)
  40. {
  41. pszFile = "null.cpp";
  42. }
  43. pszTrim = pszFile;
  44. psz = strrchr(pszFile, '\\');
  45. if (NULL != psz)
  46. {
  47. DWORD count = 1;
  48. while (count != 0 && psz > pszFile)
  49. {
  50. if (*--psz == '\\')
  51. {
  52. if (0 == strncmp(psz, "\\..\\", 4) ||
  53. 0 == strncmp(psz, "\\.\\", 3))
  54. {
  55. count++;
  56. }
  57. else
  58. {
  59. count--;
  60. pszTrim = &psz[1];
  61. }
  62. }
  63. }
  64. if (strlen(pszTrim) < ARRAYSIZE(s_path))
  65. {
  66. pszT = s_path;
  67. while ('\0' != *pszTrim)
  68. {
  69. if ('\\' == *pszTrim)
  70. {
  71. if (0 == strncmp(pszTrim, "\\..\\", 4))
  72. {
  73. pszTrim += 3;
  74. continue;
  75. }
  76. if (0 == strncmp(pszTrim, "\\.\\", 3))
  77. {
  78. pszTrim += 2;
  79. continue;
  80. }
  81. }
  82. *pszT++ = *pszTrim++;
  83. }
  84. *pszT = '\0';
  85. pszTrim = s_path;
  86. }
  87. }
  88. return(pszTrim);
  89. }
  90. VOID
  91. CSPrintAssert(
  92. IN char const *pszFailedAssertion,
  93. IN char const *pszFileName,
  94. IN ULONG LineNumber,
  95. IN char const *pszMessage OPTIONAL)
  96. {
  97. BOOLEAN fReprint;
  98. char buf[1024];
  99. do
  100. {
  101. fReprint = FALSE;
  102. DBGPRINT((
  103. DBG_SS_ASSERT,
  104. "\n"
  105. "*** Certificate Services Assertion failed: %hs %hs\n"
  106. "*** Source File: %hs, line %ld\n"
  107. "\n",
  108. pszMessage == NULL? "" : pszMessage,
  109. pszFailedAssertion,
  110. csTrimPath(pszFileName),
  111. LineNumber));
  112. if (IsDebuggerPresent())
  113. {
  114. #if i386
  115. _asm xor al,al
  116. #endif
  117. CSDbgBreakPoint();
  118. #if i386
  119. _asm mov byte ptr [fReprint],al
  120. #endif
  121. }
  122. }
  123. while (fReprint);
  124. }
  125. BOOL
  126. csShouldPrintError(
  127. IN HRESULT hr,
  128. IN HRESULT hrquiet)
  129. {
  130. BOOL fPrint = TRUE;
  131. if (myIsDelayLoadHResult(hr))
  132. {
  133. #if DBG_CERTSRV
  134. if (!DbgIsSSActive(DBG_SS_MODLOAD))
  135. #endif
  136. {
  137. fPrint = FALSE;
  138. }
  139. }
  140. if (S_OK != hrquiet && hrquiet == hr)
  141. {
  142. #if DBG_CERTSRV
  143. if (!DbgIsSSActive(DBG_SS_NOQUIET))
  144. #endif
  145. {
  146. fPrint = FALSE;
  147. }
  148. }
  149. return(fPrint);
  150. }
  151. VOID
  152. CSPrintError(
  153. IN char const *pszMessage,
  154. OPTIONAL IN WCHAR const *pwszData,
  155. IN char const *pszFile,
  156. IN DWORD dwLine,
  157. IN HRESULT hr,
  158. IN HRESULT hrquiet)
  159. {
  160. char acherr[1024];
  161. WCHAR awchr[cwcHRESULTSTRING];
  162. if (csShouldPrintError(hr, hrquiet))
  163. {
  164. if (NULL != pwszData)
  165. {
  166. if (-1 == _snprintf(
  167. acherr,
  168. sizeof(acherr),
  169. "%hs(%ws)",
  170. pszMessage,
  171. pwszData))
  172. {
  173. strcpy(&acherr[sizeof(acherr) - 4], "...");
  174. }
  175. pszMessage = acherr;
  176. }
  177. DBGPRINT((
  178. DBG_SS_ERROR,
  179. "%hs(%u): %hs: error %ws\n",
  180. csTrimPath(pszFile),
  181. dwLine,
  182. pszMessage,
  183. myHResultToString(awchr, hr)));
  184. }
  185. }
  186. VOID
  187. CSPrintErrorInt(
  188. IN char const *pszMessage,
  189. IN DWORD dwData,
  190. IN char const *pszFile,
  191. IN DWORD dwLine,
  192. IN HRESULT hr,
  193. IN HRESULT hrquiet)
  194. {
  195. char achdata[32];
  196. WCHAR awchr[cwcHRESULTSTRING];
  197. if (csShouldPrintError(hr, hrquiet))
  198. {
  199. if (-1 == _snprintf(achdata, sizeof(achdata), "0x%x(%d)", dwData, dwData))
  200. {
  201. strcpy(&achdata[sizeof(achdata) - 4], "...");
  202. }
  203. DBGPRINT((
  204. DBG_SS_ERROR,
  205. "%hs(%u): %hs(%hs): error %ws\n",
  206. csTrimPath(pszFile),
  207. dwLine,
  208. pszMessage,
  209. achdata,
  210. myHResultToString(awchr, hr)));
  211. }
  212. }
  213. #endif // DBG_CERTSRV_DEBUG_PRINT