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.

344 lines
7.2 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. All rights reserved
  4. Module Name:
  5. sspioutput.cxx
  6. Abstract:
  7. sspioutput
  8. Author:
  9. Larry Zhu (LZhu) December 1, 2001 Created
  10. Environment:
  11. User Mode -Win32
  12. Revision History:
  13. --*/
  14. #include "precomp.hxx"
  15. #pragma hdrstop
  16. #include "sspioutput.hxx"
  17. TSspiLibarayGlobals g_SspiGlobals = {
  18. 1, // major version
  19. 2, // minor version
  20. 0xF, // debug mask
  21. TEXT("SSPI"), // debug prompt
  22. NULL, // no serialization
  23. };
  24. #if defined(UNICODE) || defined(_UNICODE)
  25. #define isprint iswprint
  26. #define SspiToChar SspiToCharW
  27. #else
  28. #define SspiToChar SspiToCharA
  29. #endif
  30. WCHAR SspiToCharW(IN UCHAR c)
  31. {
  32. UCHAR* pChar = &c;
  33. if ((c >= ' ') && (c <= '~'))
  34. {
  35. return RtlAnsiCharToUnicodeChar(&pChar);
  36. }
  37. return TEXT('.');
  38. }
  39. CHAR SspiToCharA(IN UCHAR c)
  40. {
  41. if ((c >= ' ') && (c <= '~'))
  42. {
  43. return c;
  44. }
  45. return TEXT('.');
  46. }
  47. VOID SspiSpaceIt(IN ULONG cchLen, IN TCHAR* buf)
  48. {
  49. for (ULONG i = 0; i < cchLen; i++)
  50. {
  51. buf[i] = TEXT(' ');
  52. }
  53. }
  54. TCHAR SspiToHex(
  55. IN ULONG c
  56. )
  57. {
  58. static PCTSTR pszDigits = TEXT("0123456789abcdef");
  59. static ULONG len = lstrlen(pszDigits);
  60. if (c <= len)
  61. { // c >= 0
  62. return pszDigits[c];
  63. }
  64. return TEXT('*');
  65. }
  66. VOID
  67. SspiPrintHex(
  68. IN ULONG ulLevel,
  69. IN OPTIONAL PCTSTR pszBanner,
  70. IN ULONG cbBuffer,
  71. IN const VOID* pvbuffer
  72. )
  73. {
  74. if (g_SspiGlobals.uDebugMask & ulLevel)
  75. {
  76. PCRITICAL_SECTION pCriticalSection = g_SspiGlobals.pCriticalSection;
  77. const UCHAR* pBuffer = reinterpret_cast<const UCHAR*>(pvbuffer);
  78. ULONG high = 0;
  79. ULONG low = 0;
  80. TCHAR szLine[256] = {0};
  81. ULONG i = 0;
  82. TCHAR szBanner[MAX_PATH * 4] = {0};
  83. DWORD dwPid = GetCurrentProcessId();
  84. DWORD dwTid = GetCurrentThreadId();
  85. _sntprintf(szBanner, COUNTOF(szBanner) - 1,
  86. g_SspiGlobals.pszDbgPrompt ? TEXT("%#x.%#x %s> %s %s") : TEXT("%#x.%#x%s> %s %s"),
  87. dwPid,
  88. dwTid,
  89. g_SspiGlobals.pszDbgPrompt ? g_SspiGlobals.pszDbgPrompt : TEXT(""),
  90. SspiLevel2Str(ulLevel),
  91. pszBanner ? pszBanner : TEXT(""));
  92. if (pCriticalSection)
  93. {
  94. EnterCriticalSection(pCriticalSection);
  95. }
  96. SspiOutputDebugStringPrint(szBanner, TEXT("\n"));
  97. SspiSpaceIt(72, szLine);
  98. for (i = 0; i < cbBuffer; i++)
  99. {
  100. high = pBuffer[i] / 16;
  101. low = pBuffer[i] % 16;
  102. szLine[3 * (i % 16)] = SspiToHex(high);
  103. szLine[3 * (i % 16) + 1] = SspiToHex(low);
  104. szLine [52 + (i % 16)] = SspiToChar(pBuffer[i]);
  105. if (i % 16 == 7 && i != (cbBuffer - 1))
  106. {
  107. szLine[3 * (i % 16) + 2] = TEXT('-');
  108. }
  109. if (i % 16 == 15)
  110. {
  111. SspiOutputDebugStringPrint(NULL, TEXT(" %s\n"), szLine);
  112. SspiSpaceIt(72, szLine);
  113. }
  114. }
  115. SspiOutputDebugStringPrint(NULL, TEXT(" %s\n"), szLine);
  116. if (pCriticalSection)
  117. {
  118. LeaveCriticalSection(pCriticalSection);
  119. }
  120. }
  121. }
  122. PCTSTR
  123. SspiLevel2Str(
  124. IN ULONG ulLevel
  125. )
  126. {
  127. PCTSTR pszText = NULL;
  128. switch (ulLevel)
  129. {
  130. case SSPI_WARN:
  131. pszText = TEXT("[warn]");
  132. break;
  133. case SSPI_ERROR:
  134. pszText = TEXT("[error]");
  135. break;
  136. case SSPI_LOG:
  137. pszText = TEXT("[log]");
  138. break;
  139. case SSPI_LOG_MORE:
  140. pszText = TEXT("[more]");
  141. break;
  142. case SSPI_MSG:
  143. pszText = TEXT("[msg]");
  144. break;
  145. default:
  146. pszText = TEXT("[invalid]");
  147. break;
  148. }
  149. return pszText;
  150. }
  151. VOID
  152. SspiVOutputDebugStringPrint(
  153. IN OPTIONAL PCTSTR pszBanner,
  154. IN PCTSTR pszFmt,
  155. IN va_list pArgs
  156. )
  157. {
  158. TCHAR szBuffer[4096] = {0};
  159. INT cbUsed = 0;
  160. cbUsed = _sntprintf(szBuffer, COUNTOF(szBuffer) - 1, TEXT("%s"), pszBanner ? pszBanner : TEXT(""));
  161. if (cbUsed >= 0)
  162. {
  163. _vsntprintf(szBuffer + cbUsed, sizeof(szBuffer) -1 - cbUsed, pszFmt, pArgs);
  164. }
  165. OutputDebugString(szBuffer);
  166. }
  167. VOID
  168. SspiOutputDebugStringPrint(
  169. IN OPTIONAL PCTSTR pszBanner,
  170. IN PCTSTR pszFmt,
  171. IN ...
  172. )
  173. {
  174. va_list marker;
  175. va_start(marker, pszFmt);
  176. SspiVOutputDebugStringPrint(pszBanner ? pszBanner : TEXT(""), pszFmt, marker);
  177. va_end(marker);
  178. }
  179. VOID
  180. SspiPrint(
  181. IN ULONG ulLevel,
  182. IN PCTSTR pszFmt,
  183. IN ...
  184. )
  185. {
  186. if (g_SspiGlobals.uDebugMask & ulLevel)
  187. {
  188. TCHAR szBanner[MAX_PATH] = {0};
  189. DWORD dwPid = GetCurrentProcessId();
  190. DWORD dwTid = GetCurrentThreadId();
  191. _sntprintf(szBanner, COUNTOF(szBanner) - 1,
  192. g_SspiGlobals.pszDbgPrompt ? TEXT("%#x.%#x %s> %s ") : TEXT("%#x.%#x%s> %s "),
  193. dwPid,
  194. dwTid,
  195. g_SspiGlobals.pszDbgPrompt ? g_SspiGlobals.pszDbgPrompt : TEXT(""), SspiLevel2Str(ulLevel));
  196. va_list marker;
  197. va_start(marker, pszFmt);
  198. SspiVOutputDebugStringPrint(szBanner, pszFmt, marker);
  199. va_end(marker);
  200. }
  201. }
  202. VOID
  203. SspiLogOpen(
  204. IN PCTSTR pszPrompt,
  205. IN ULONG ulMask
  206. )
  207. {
  208. g_SspiGlobals.uDebugMask = ulMask;
  209. g_SspiGlobals.pszDbgPrompt = pszPrompt;
  210. }
  211. VOID
  212. SspiLogOpenSerialized(
  213. IN PCTSTR pszPrompt,
  214. IN ULONG ulMask,
  215. IN PCRITICAL_SECTION pCriticalSection
  216. )
  217. {
  218. g_SspiGlobals.uDebugMask = ulMask;
  219. g_SspiGlobals.pszDbgPrompt = pszPrompt;
  220. g_SspiGlobals.pCriticalSection = pCriticalSection;
  221. }
  222. VOID
  223. SspiLogClose(
  224. VOID
  225. )
  226. {
  227. g_SspiGlobals.uDebugMask = 0;
  228. g_SspiGlobals.pszDbgPrompt = NULL;
  229. }
  230. VOID
  231. SspiPrintSysTimeAsLocalTime(
  232. IN ULONG ulLevel,
  233. IN PCTSTR pszBanner,
  234. IN LARGE_INTEGER* pSysTime
  235. )
  236. {
  237. TNtStatus NtStatus = STATUS_UNSUCCESSFUL;
  238. TIME_FIELDS TimeFields = {0};
  239. LARGE_INTEGER LocalTime = {0};
  240. NtStatus DBGCHK = RtlSystemTimeToLocalTime(pSysTime, &LocalTime);
  241. if (NT_SUCCESS(NtStatus))
  242. {
  243. RtlTimeToTimeFields(&LocalTime, &TimeFields);
  244. SspiPrint(ulLevel, TEXT("%s LocalTime(%ld/%ld/%ld %ld:%2.2ld:%2.2ld) SystemTime(H%8.8lx L%8.8lx)\n"),
  245. pszBanner,
  246. TimeFields.Month,
  247. TimeFields.Day,
  248. TimeFields.Year,
  249. TimeFields.Hour,
  250. TimeFields.Minute,
  251. TimeFields.Second,
  252. pSysTime->HighPart,
  253. pSysTime->LowPart);
  254. }
  255. }
  256. VOID
  257. SspiPrintLocalTime(
  258. IN ULONG ulLevel,
  259. IN PCTSTR pszBanner,
  260. IN LARGE_INTEGER* pLocalTime
  261. )
  262. {
  263. TIME_FIELDS TimeFields = {0};
  264. RtlTimeToTimeFields(pLocalTime, &TimeFields);
  265. SspiPrint(ulLevel, TEXT("%s LocalTime(%ld/%ld/%ld %ld:%2.2ld:%2.2ld) H%8.8lx L%8.8lx\n"),
  266. pszBanner,
  267. TimeFields.Month,
  268. TimeFields.Day,
  269. TimeFields.Year,
  270. TimeFields.Hour,
  271. TimeFields.Minute,
  272. TimeFields.Second,
  273. pLocalTime->HighPart,
  274. pLocalTime->LowPart);
  275. }