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.

242 lines
7.3 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // General utility routines.
  4. //
  5. // Copyright (C) Microsoft Corporation, 2000-2002.
  6. //
  7. //----------------------------------------------------------------------------
  8. #ifndef __CMNUTIL_HPP__
  9. #define __CMNUTIL_HPP__
  10. #ifndef VER_PLATFORM_WIN32_CE
  11. #define VER_PLATFORM_WIN32_CE 3
  12. #endif
  13. #define AMD_VENDOR_ID_EBX ('htuA')
  14. #define AMD_VENDOR_ID_EDX ('itne')
  15. #define AMD_VENDOR_ID_ECX ('DMAc')
  16. #define INTEL_VENDOR_ID_EBX ('uneG')
  17. #define INTEL_VENDOR_ID_EDX ('Ieni')
  18. #define INTEL_VENDOR_ID_ECX ('letn')
  19. #define DIMAT(Array, EltType) (sizeof(Array) / sizeof(EltType))
  20. #define DIMA(Array) DIMAT(Array, (Array)[0])
  21. #define CSRSS_PROCESS_ID ((ULONG)-1)
  22. // Converts an NTSTATUS into an HRESULT with success check.
  23. #define CONV_NT_STATUS(Status) \
  24. (NT_SUCCESS(Status) ? S_OK : HRESULT_FROM_NT(Status))
  25. // Converts a Win32 status into an HRESULT with a guarantee it's
  26. // an error code. This avoids problems with routines which
  27. // don't set a last error.
  28. #define WIN32_STATUS(Err) ((Err) == 0 ? E_FAIL : HRESULT_FROM_WIN32(Err))
  29. #define WIN32_LAST_STATUS() WIN32_STATUS(GetLastError())
  30. // Converts a BOOL into an HRESULT with success check.
  31. #define CONV_W32_STATUS(Status) \
  32. ((Status) ? S_OK : WIN32_LAST_STATUS())
  33. #define COM_THREAD_MODEL COINIT_APARTMENTTHREADED
  34. #include "copystr.h"
  35. //----------------------------------------------------------------------------
  36. //
  37. // Assertions.
  38. //
  39. //----------------------------------------------------------------------------
  40. #if DBG
  41. void DbgAssertionFailed(PCSTR File, int Line, PCSTR Str);
  42. #define DBG_ASSERT(Expr) \
  43. if (!(Expr)) \
  44. { \
  45. DbgAssertionFailed(__FILE__, __LINE__, #Expr); \
  46. } \
  47. else 0
  48. #else
  49. #define DBG_ASSERT(Expr)
  50. #endif
  51. //----------------------------------------------------------------------------
  52. //
  53. // COM help.
  54. //
  55. //----------------------------------------------------------------------------
  56. // Wrapper that can be locally implemented if necessary to
  57. // remove usage of ole32.dll on platforms where IsEqualIID
  58. // is not inlined.
  59. #define DbgIsEqualIID(Id1, Id2) \
  60. IsEqualIID(Id1, Id2)
  61. // Safely releases and NULLs an interface pointer.
  62. #define RELEASE(pUnk) \
  63. ((pUnk) != NULL ? ((pUnk)->Release(), (pUnk) = NULL) : NULL)
  64. // Transfers an interface pointer into a holder that may or
  65. // may not already hold and interface.
  66. #define TRANSFER(pOld, pNew) \
  67. (((pNew) != NULL ? (pNew)->AddRef() : 0), \
  68. ((pOld) != NULL ? (pOld)->Release() : 0), \
  69. (pOld) = (pNew))
  70. //----------------------------------------------------------------------------
  71. //
  72. // Utility functions.
  73. //
  74. //----------------------------------------------------------------------------
  75. extern PSECURITY_DESCRIPTOR g_AllAccessSecDesc;
  76. extern SECURITY_ATTRIBUTES g_AllAccessSecAttr;
  77. PSTR FormatStatusCode(HRESULT Status);
  78. PSTR FormatAnyStatus(HRESULT Status, PVOID Arguments,
  79. PBOOL IsNtStatus, PSTR* ErrorGroup);
  80. #define FormatStatus(Status) \
  81. FormatAnyStatus(Status, NULL, NULL, NULL)
  82. #define FormatStatusArgs(Status, Args) \
  83. FormatAnyStatus(Status, Args, NULL, NULL)
  84. BOOL InstallAsAeDebug(PCSTR Append);
  85. HANDLE CreatePidEvent(ULONG Pid, ULONG CreateOrOpen);
  86. BOOL SetPidEvent(ULONG Pid, ULONG CreateOrOpen);
  87. HRESULT EnableDebugPrivilege(void);
  88. HRESULT FillDataBuffer(PVOID Data, ULONG DataLen,
  89. PVOID Buffer, ULONG BufferLen, PULONG BufferUsed);
  90. HRESULT FillStringBuffer(PCSTR String, ULONG StringLenIn,
  91. PSTR Buffer, ULONG BufferLen, PULONG StringLenOut);
  92. HRESULT AppendToStringBuffer(HRESULT Status, PCSTR String, BOOL First,
  93. PSTR* Buffer, ULONG* BufferLen, PULONG LenOut);
  94. HRESULT FillStringBufferW(PCWSTR String, ULONG StringLenIn,
  95. PWSTR Buffer, ULONG BufferLen, PULONG StringLenOut);
  96. HRESULT AppendToStringBufferW(HRESULT Status, PCWSTR String, BOOL First,
  97. PWSTR* Buffer, ULONG* BufferLen, PULONG LenOut);
  98. PSTR FindPathElement(PSTR Path, ULONG Element, PSTR* EltEnd);
  99. void Win32ToNtTimeout(ULONG Win32Timeout, PLARGE_INTEGER NtTimeout);
  100. HRESULT InitializeAllAccessSecObj(void);
  101. void DeleteAllAccessSecObj(void);
  102. #define BASE_YEAR_ADJUSTMENT 11644473600
  103. // Convert to seconds and then from base year 1601 to base year 1970.
  104. #define FileTimeToTimeDateStamp(FileTime) \
  105. (ULONG)(((FileTime) / 10000000) - BASE_YEAR_ADJUSTMENT)
  106. // Adjust date back to 1601 from 1970 and convert to 100 nanoseconds
  107. #define TimeDateStampToFileTime(TimeDate) \
  108. (((ULONG64)(TimeDate) + BASE_YEAR_ADJUSTMENT) * 10000000)
  109. // Convert to seconds
  110. #define FileTimeToTime(FileTime) \
  111. (ULONG)((FileTime) / 10000000)
  112. // Convert to seconds .
  113. #define TimeToFileTime(TimeDate) \
  114. ((ULONG64)(TimeDate) * 10000000)
  115. HRESULT QueryVersionDataBuffer(PVOID VerData, PCSTR Item,
  116. PVOID Buffer, ULONG BufferSize,
  117. PULONG DataSize);
  118. PVOID GetAllFileVersionInfo(PCWSTR VerFile);
  119. BOOL GetFileStringFileInfo(PCWSTR VerFile, PCSTR SubItem,
  120. PSTR Buffer, ULONG BufferSize);
  121. HRESULT ExpandDumpCab(PCSTR CabFile, ULONG FileFlags, PCSTR FileToOpen,
  122. PSTR DmpFile, ULONG DmpFileLen, INT_PTR* DmpFh);
  123. HRESULT CreateDumpCab(PCSTR FileName);
  124. HRESULT AddToDumpCab(PCSTR FileName);
  125. void CloseDumpCab(void);
  126. enum FILE_IO_TYPE
  127. {
  128. FIO_WIN32,
  129. FIO_WININET,
  130. };
  131. class PathFile
  132. {
  133. public:
  134. PathFile(FILE_IO_TYPE IoType)
  135. {
  136. m_IoType = IoType;
  137. }
  138. virtual ~PathFile(void);
  139. virtual HRESULT Open(PCSTR PathComponent, PCSTR PathAndFile,
  140. ULONG SymOpt) = 0;
  141. virtual HRESULT QueryDataAvailable(PULONG Avail) = 0;
  142. virtual HRESULT GetLastWriteTime(PFILETIME Time) = 0;
  143. virtual HRESULT Read(PVOID Buffer, ULONG BufferLen, PULONG Done) = 0;
  144. FILE_IO_TYPE m_IoType;
  145. };
  146. BOOL IsUrlPathComponent(PCSTR Path);
  147. #ifndef NT_NATIVE
  148. BOOL PathFileExists(PCSTR PathComponent, PCSTR PathAndFile,
  149. ULONG SymOpt, FILE_IO_TYPE* IoType);
  150. HRESULT OpenPathFile(PCSTR PathComponent, PCSTR PathAndFile,
  151. ULONG SymOpt, PathFile** File);
  152. #endif
  153. HRESULT AnsiToWide(PCSTR Ansi, PWSTR* Wide);
  154. void FreeWide(PCWSTR Wide);
  155. HRESULT WideToAnsi(PCWSTR Wide, PSTR* Ansi);
  156. void FreeAnsi(PCSTR Ansi);
  157. void ImageNtHdr32To64(PIMAGE_NT_HEADERS32 Hdr32,
  158. PIMAGE_NT_HEADERS64 Hdr64);
  159. enum VALUE_FORMAT
  160. {
  161. VALUE_DEFAULT,
  162. VALUE_INT8,
  163. VALUE_UINT8,
  164. VALUE_INT16,
  165. VALUE_UINT16,
  166. VALUE_INT32,
  167. VALUE_UINT32,
  168. VALUE_INT64,
  169. VALUE_UINT64,
  170. VALUE_FLT32,
  171. VALUE_FLT64,
  172. };
  173. typedef struct _VALUE_FORMAT_DESC
  174. {
  175. PSTR Name;
  176. PSTR FmtStr;
  177. ULONG Size;
  178. BOOL Signed;
  179. } VALUE_FORMAT_DESC, *PVALUE_FORMAT_DESC;
  180. void GetValueFormatDesc(VALUE_FORMAT Format, PVALUE_FORMAT_DESC Desc);
  181. PSTR ParseValueFormat(PSTR Str, VALUE_FORMAT* Format, PULONG Elts);
  182. BOOL FormatValue(VALUE_FORMAT Format, PUCHAR Value, ULONG ValSize, ULONG Elts,
  183. PSTR Buffer, ULONG BufferChars);
  184. ULONG ProcArchToImageMachine(ULONG ProcArch);
  185. ULONG ImageMachineToProcArch(ULONG ImageMachine);
  186. #endif // #ifndef __CMNUTIL_HPP__