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.

316 lines
5.5 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. utils.c
  5. Abstract:
  6. Various utility routines used by the extensions.
  7. Author:
  8. Ravisankar Pudipeddi
  9. Environment:
  10. User Mode.
  11. Revision History:
  12. --*/
  13. #include "pch.h"
  14. #include "local.h"
  15. VOID
  16. xdprintf(
  17. ULONG Depth,
  18. PCCHAR S,
  19. ...
  20. )
  21. {
  22. va_list ap;
  23. ULONG i;
  24. CCHAR DebugBuffer[256];
  25. for (i=0; i<Depth; i++) {
  26. dprintf (" ");
  27. }
  28. va_start(ap, S);
  29. vsprintf(DebugBuffer, S, ap);
  30. dprintf (DebugBuffer);
  31. va_end(ap);
  32. }
  33. BOOLEAN
  34. xReadMemory (
  35. ULONG64 Src,
  36. PVOID Dst,
  37. ULONG Len
  38. )
  39. {
  40. ULONG result;
  41. return (ReadMemory (Src, Dst, Len, &result) && (result == Len));
  42. }
  43. ULONG GetUlongFromAddress (ULONG64 Location)
  44. {
  45. ULONG Value = 0;
  46. ULONG result;
  47. if ((!ReadMemory (Location, &Value, sizeof (ULONG), &result)) || (result < sizeof (ULONG)))
  48. {
  49. dprintf ("unable to read from %08x\n", Location);
  50. }
  51. return (Value);
  52. }
  53. ULONG64 GetPointerFromAddress (ULONG64 Location)
  54. {
  55. ULONG64 Value = 0;
  56. ULONG result;
  57. if (ReadPointer (Location, &Value))
  58. {
  59. dprintf ("unable to read from %016p\n", Location);
  60. }
  61. return (Value);
  62. }
  63. ULONG GetUlongValue (PCHAR String)
  64. {
  65. ULONG64 Location;
  66. ULONG Value = 0;
  67. ULONG result;
  68. Location = GetExpression (String);
  69. if (!Location)
  70. {
  71. dprintf ("unable to get %s\n", String);
  72. }
  73. return GetUlongFromAddress( Location );
  74. }
  75. ULONG64 GetPointerValue (PCHAR String)
  76. {
  77. ULONG64 Location;
  78. ULONG64 Value = 0;
  79. Location = GetExpression (String);
  80. if (!Location)
  81. {
  82. dprintf ("unable to get %s\n", String);
  83. }
  84. ReadPointer (Location, &Value);
  85. return (Value);
  86. }
  87. ULONG GetFieldValueUlong32 (ULONG64 ul64addrStructureBase,
  88. PCHAR pchStructureType,
  89. PCHAR pchFieldname)
  90. {
  91. ULONG ulReturnValue = 0;
  92. GetFieldValue (ul64addrStructureBase, pchStructureType, pchFieldname, ulReturnValue);
  93. return (ulReturnValue);
  94. }
  95. ULONG64 GetFieldValueUlong64 (ULONG64 ul64addrStructureBase,
  96. PCHAR pchStructureType,
  97. PCHAR pchFieldname)
  98. {
  99. ULONG64 ul64ReturnValue = 0;
  100. GetFieldValue (ul64addrStructureBase, pchStructureType, pchFieldname, ul64ReturnValue);
  101. return (ul64ReturnValue);
  102. }
  103. ULONG FormatDateAndTime (ULONG64 ul64Time, PCHAR pszFormattedDateAndTime, ULONG ulBufferLength)
  104. {
  105. FILETIME ftTimeOriginal;
  106. FILETIME ftTimeLocal;
  107. SYSTEMTIME stTimeSystem;
  108. CHAR achFormattedDateString [200];
  109. CHAR achFormattedTimeString [200];
  110. DWORD dwStatus = 0;
  111. BOOL bSucceeded = FALSE;
  112. ULARGE_INTEGER uliConversionTemp;
  113. int iReturnValue;
  114. uliConversionTemp.QuadPart = ul64Time;
  115. ftTimeOriginal.dwLowDateTime = uliConversionTemp.LowPart;
  116. ftTimeOriginal.dwHighDateTime = uliConversionTemp.HighPart;
  117. if (0 == dwStatus)
  118. {
  119. bSucceeded = FileTimeToLocalFileTime (&ftTimeOriginal, &ftTimeLocal);
  120. if (!bSucceeded)
  121. {
  122. dwStatus = GetLastError ();
  123. }
  124. }
  125. if (0 == dwStatus)
  126. {
  127. bSucceeded = FileTimeToSystemTime (&ftTimeLocal, &stTimeSystem);
  128. if (!bSucceeded)
  129. {
  130. dwStatus = GetLastError ();
  131. }
  132. }
  133. if (0 == dwStatus)
  134. {
  135. iReturnValue = GetDateFormat (LOCALE_USER_DEFAULT,
  136. 0,
  137. &stTimeSystem,
  138. NULL,
  139. achFormattedDateString,
  140. sizeof (achFormattedDateString) / sizeof (CHAR));
  141. if (0 == iReturnValue)
  142. {
  143. dwStatus = GetLastError ();
  144. }
  145. }
  146. if (0 == dwStatus)
  147. {
  148. iReturnValue = GetTimeFormat (LOCALE_USER_DEFAULT,
  149. 0,
  150. &stTimeSystem,
  151. NULL,
  152. achFormattedTimeString,
  153. sizeof (achFormattedTimeString) / sizeof (CHAR));
  154. if (0 == iReturnValue)
  155. {
  156. dwStatus = GetLastError ();
  157. }
  158. }
  159. if (0 == dwStatus)
  160. {
  161. iReturnValue = _snprintf (pszFormattedDateAndTime,
  162. ulBufferLength / sizeof (CHAR),
  163. "%s %s",
  164. achFormattedDateString,
  165. achFormattedTimeString);
  166. if (iReturnValue < 0)
  167. {
  168. dwStatus = ERROR_INSUFFICIENT_BUFFER;
  169. }
  170. else
  171. {
  172. dwStatus = 0;
  173. }
  174. }
  175. if (0 != dwStatus)
  176. {
  177. if (0 == ul64Time)
  178. {
  179. _snprintf (pszFormattedDateAndTime,
  180. ulBufferLength / sizeof (CHAR),
  181. "Date/Time not specified");
  182. }
  183. else
  184. {
  185. _snprintf (pszFormattedDateAndTime,
  186. ulBufferLength / sizeof (CHAR),
  187. "Date/Time invalid");
  188. }
  189. }
  190. return (dwStatus);
  191. }
  192. /*
  193. ** {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
  194. */
  195. ULONG FormatGUID (GUID guidValue, PCHAR pszFormattedGUID, ULONG ulBufferLength)
  196. {
  197. DWORD dwStatus = 0;
  198. if (sizeof ("{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}") > ulBufferLength)
  199. {
  200. dwStatus = ERROR_INSUFFICIENT_BUFFER;
  201. }
  202. if (0 == dwStatus)
  203. {
  204. _snprintf (pszFormattedGUID, ulBufferLength, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
  205. guidValue.Data1,
  206. guidValue.Data2,
  207. guidValue.Data3,
  208. guidValue.Data4[0],
  209. guidValue.Data4[1],
  210. guidValue.Data4[2],
  211. guidValue.Data4[3],
  212. guidValue.Data4[4],
  213. guidValue.Data4[5],
  214. guidValue.Data4[6],
  215. guidValue.Data4[7]);
  216. }
  217. return (dwStatus);
  218. }