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.

232 lines
5.6 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1990 - 2000
  3. All rights reserved.
  4. Module Name:
  5. dbgutil.c
  6. Abstract:
  7. This module provides all the Spooler Subsystem Debugger utility
  8. functions.
  9. Author:
  10. Krishna Ganugapati (KrishnaG) 1-July-1993
  11. Revision History:
  12. --*/
  13. #include "precomp.h"
  14. #pragma hdrstop
  15. #include "dbglocal.h"
  16. #include "dbgsec.h"
  17. SYSTEM_INFO gSysInfo;
  18. DWORD EvalValue(
  19. LPSTR *pptstr,
  20. PNTSD_GET_EXPRESSION EvalExpression,
  21. PNTSD_OUTPUT_ROUTINE Print)
  22. {
  23. LPSTR lpArgumentString;
  24. LPSTR lpAddress;
  25. DWORD dw;
  26. char ach[80];
  27. UINT_PTR cch;
  28. UNREFERENCED_PARAMETER(Print);
  29. lpArgumentString = *pptstr;
  30. while (isspace(*lpArgumentString))
  31. lpArgumentString++;
  32. lpAddress = lpArgumentString;
  33. while ((!isspace(*lpArgumentString)) && (*lpArgumentString != 0))
  34. lpArgumentString++;
  35. cch = (UINT_PTR)lpArgumentString - (UINT_PTR)lpAddress;
  36. if (cch > 79)
  37. cch = 79;
  38. StringCchCopyA(ach, (UINT)cch, lpAddress);
  39. dw = (DWORD)EvalExpression(lpAddress);
  40. *pptstr = lpArgumentString;
  41. return dw;
  42. }
  43. VOID
  44. ConvertSidToAsciiString(
  45. PSID pSid,
  46. LPSTR String,
  47. size_t cchString
  48. )
  49. /*++
  50. Routine Description:
  51. This function generates a printable unicode string representation
  52. of a SID.
  53. The resulting string will take one of two forms. If the
  54. IdentifierAuthority value is not greater than 2^32, then
  55. the SID will be in the form:
  56. S-1-281736-12-72-9-110
  57. ^ ^^ ^^ ^ ^^^
  58. | | | | |
  59. +-----+--+-+--+---- Decimal
  60. Otherwise it will take the form:
  61. S-1-0x173495281736-12-72-9-110
  62. ^^^^^^^^^^^^^^ ^^ ^^ ^ ^^^
  63. Hexidecimal | | | |
  64. +--+-+--+---- Decimal
  65. Arguments:
  66. pSid - opaque pointer that supplies the SID that is to be
  67. converted to Unicode.
  68. Return Value:
  69. If the Sid is successfully converted to a Unicode string, a
  70. pointer to the Unicode string is returned, else NULL is
  71. returned.
  72. --*/
  73. {
  74. UCHAR Buffer[256];
  75. UCHAR i;
  76. ULONG Tmp;
  77. SID_IDENTIFIER_AUTHORITY *pSidIdentifierAuthority;
  78. PUCHAR pSidSubAuthorityCount;
  79. if (!IsValidSid( pSid )) {
  80. *String= '\0';
  81. return;
  82. }
  83. StringCchPrintfA(Buffer, COUNTOF(Buffer), "S-%u-", (USHORT)(((PISID)pSid)->Revision ));
  84. StringCchCopyA(String, cchString, Buffer);
  85. pSidIdentifierAuthority = GetSidIdentifierAuthority(pSid);
  86. if ( (pSidIdentifierAuthority->Value[0] != 0) ||
  87. (pSidIdentifierAuthority->Value[1] != 0) ){
  88. StringCchPrintfA(Buffer, COUNTOF(Buffer),
  89. "0x%02hx%02hx%02hx%02hx%02hx%02hx",
  90. (USHORT)pSidIdentifierAuthority->Value[0],
  91. (USHORT)pSidIdentifierAuthority->Value[1],
  92. (USHORT)pSidIdentifierAuthority->Value[2],
  93. (USHORT)pSidIdentifierAuthority->Value[3],
  94. (USHORT)pSidIdentifierAuthority->Value[4],
  95. (USHORT)pSidIdentifierAuthority->Value[5] );
  96. StringCchCatA(String, cchString, Buffer);
  97. } else {
  98. Tmp = (ULONG)pSidIdentifierAuthority->Value[5] +
  99. (ULONG)(pSidIdentifierAuthority->Value[4] << 8) +
  100. (ULONG)(pSidIdentifierAuthority->Value[3] << 16) +
  101. (ULONG)(pSidIdentifierAuthority->Value[2] << 24);
  102. StringCchPrintfA(Buffer, COUNTOF(Buffer), "%lu", Tmp);
  103. StringCchCatA(String, cchString, Buffer);
  104. }
  105. pSidSubAuthorityCount = GetSidSubAuthorityCount(pSid);
  106. for (i=0;i< *(pSidSubAuthorityCount);i++ ) {
  107. StringCchPrintfA(Buffer, COUNTOF(Buffer), "-%lu", *(GetSidSubAuthority(pSid, i)));
  108. StringCchCatA(String, cchString, Buffer);
  109. }
  110. }
  111. BOOL
  112. ReadProcessString(
  113. IN HANDLE hProcess,
  114. IN LPCVOID lpBaseAddress,
  115. OUT LPVOID lpBuffer,
  116. IN SIZE_T nSize,
  117. OUT SIZE_T *lpNumberOfBytesRead
  118. )
  119. {
  120. BOOL bRetval = FALSE;
  121. SIZE_T cbRead = 0;
  122. UINT_PTR nAddress = 0;
  123. //
  124. // Attempt to read the memory, up to the provided size.
  125. //
  126. bRetval = ReadProcessMemory(hProcess, lpBaseAddress, lpBuffer, nSize, &cbRead);
  127. //
  128. // The string in the debugged process may have unmapped memory just after
  129. // the end of the string, (this is true when page heap is enabled),
  130. //
  131. // If the read failed and the address plus the string buffer size crosses
  132. // a page boundary then retry the operation up to the page end.
  133. //
  134. if (!bRetval)
  135. {
  136. nAddress = (UINT_PTR)lpBaseAddress;
  137. //
  138. // If we have crossed a page boundary.
  139. //
  140. if (((nAddress & (gSysInfo.dwPageSize-1)) + nSize) > gSysInfo.dwPageSize-1)
  141. {
  142. nSize = (SIZE_T)((gSysInfo.dwPageSize-1) - (nAddress & (gSysInfo.dwPageSize-1)));
  143. bRetval = ReadProcessMemory(hProcess, lpBaseAddress, lpBuffer, nSize, &cbRead);
  144. }
  145. }
  146. //
  147. // The read succeeded.
  148. //
  149. if (bRetval)
  150. {
  151. //
  152. // If the caller wants to know the number of bytes read.
  153. //
  154. if (lpNumberOfBytesRead)
  155. {
  156. *lpNumberOfBytesRead = cbRead;
  157. }
  158. }
  159. return bRetval;
  160. }
  161. //
  162. // Query the system for the page size.
  163. //
  164. BOOL
  165. QuerySystemInformation(
  166. VOID
  167. )
  168. {
  169. GetSystemInfo(&gSysInfo);
  170. return TRUE;
  171. }