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.

357 lines
6.3 KiB

  1. // Utilities.cpp: implementation of the CUtilities class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #pragma warning (disable : 4786)
  6. #pragma warning (disable : 4275)
  7. #include <iostream>
  8. #include <strstream>
  9. #include <fstream>
  10. #include <string>
  11. #include <sstream>
  12. #include <map>
  13. #include <list>
  14. using namespace std;
  15. #include <tchar.h>
  16. #include <windows.h>
  17. #ifdef NONNT5
  18. typedef unsigned long ULONG_PTR;
  19. #endif
  20. #include <wmistr.h>
  21. #include <guiddef.h>
  22. #include <initguid.h>
  23. #include <evntrace.h>
  24. #include <malloc.h>
  25. #include <WTYPES.H>
  26. #include "t_string.h"
  27. #include <tchar.h>
  28. #include <list>
  29. #include "Persistor.h"
  30. #include "StructureWrappers.h"
  31. #include "StructureWapperHelpers.h"
  32. #include "Utilities.h"
  33. #define MAX_STR 1024
  34. //////////////////////////////////////////////////////////////////////
  35. //
  36. //////////////////////////////////////////////////////////////////////
  37. TCHAR *NewTCHAR(const TCHAR *ptcToCopy)
  38. {
  39. if (ptcToCopy == NULL)
  40. {
  41. return NULL;
  42. }
  43. // This is a gross hack. Need to pin down heap corruption.
  44. int nString = _tcsclen(ptcToCopy) + 100;
  45. int nTCHAR = sizeof(TCHAR);
  46. int nLen = nString * (nTCHAR);
  47. TCHAR *pNew = (TCHAR*) malloc(nLen);
  48. _tcscpy(pNew,ptcToCopy);
  49. return pNew;
  50. }
  51. LPSTR NewLPSTR(LPCWSTR lpwstrToCopy)
  52. {
  53. int nLen = (wcslen(lpwstrToCopy) + 1) * sizeof(WCHAR);
  54. LPSTR pNew = (char *)malloc( nLen );
  55. wcstombs(pNew, lpwstrToCopy, nLen);
  56. return pNew;
  57. }
  58. LPWSTR NewLPWSTR(LPCSTR lpstrToCopy)
  59. {
  60. int nLen = (strlen(lpstrToCopy) + 1);
  61. LPWSTR pNew = (WCHAR *)malloc( nLen * sizeof(WCHAR));
  62. mbstowcs(pNew, lpstrToCopy, nLen);
  63. return pNew;
  64. }
  65. LPTSTR DecodeStatus(IN ULONG Status)
  66. {
  67. LPTSTR lptstrError = (LPTSTR) malloc (MAX_STR);
  68. memset( lptstrError, 0, MAX_STR );
  69. FormatMessage(
  70. FORMAT_MESSAGE_FROM_SYSTEM |
  71. FORMAT_MESSAGE_IGNORE_INSERTS,
  72. NULL,
  73. Status,
  74. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  75. lptstrError,
  76. MAX_STR,
  77. NULL );
  78. for (int i = 0; i < MAX_STR; i++)
  79. {
  80. if (lptstrError[i] == 0x0d)
  81. {
  82. lptstrError[i] = _T('\0');
  83. break;
  84. }
  85. }
  86. return lptstrError;
  87. }
  88. int GetFileList
  89. (LPTSTR lptstrPath, LPTSTR lptstrFileType, list<t_string> &rList)
  90. {
  91. t_string tsFind;
  92. tsFind = lptstrPath;
  93. tsFind += _T("\\");
  94. tsFind += lptstrFileType;
  95. WIN32_FIND_DATA wfdFile;
  96. HANDLE hFindHandle =
  97. FindFirstFile(tsFind.c_str(), &wfdFile);
  98. if (hFindHandle == INVALID_HANDLE_VALUE)
  99. {
  100. return HRESULT_FROM_WIN32(GetLastError());
  101. }
  102. if ((_tcscmp(wfdFile.cFileName,_T(".")) != 0) &&
  103. (_tcscmp(wfdFile.cFileName,_T("..")) != 0))
  104. {
  105. tsFind = lptstrPath;
  106. tsFind += _T("\\");
  107. tsFind += wfdFile.cFileName;
  108. rList.push_back(tsFind);
  109. tsFind.erase();
  110. }
  111. while (FindNextFile(hFindHandle, &wfdFile))
  112. {
  113. if ((_tcscmp(wfdFile.cFileName,_T(".")) != 0) &&
  114. (_tcscmp(wfdFile.cFileName,_T("..")) != 0))
  115. {
  116. tsFind = lptstrPath;
  117. tsFind += _T("\\");
  118. tsFind += wfdFile.cFileName;
  119. rList.push_back(tsFind);
  120. tsFind.erase();
  121. }
  122. }
  123. FindClose(hFindHandle);
  124. return ERROR_SUCCESS;
  125. }
  126. // From Q 118626
  127. BOOL IsAdmin()
  128. {
  129. HANDLE hAccessToken;
  130. UCHAR InfoBuffer[1024];
  131. PTOKEN_GROUPS ptgGroups = (PTOKEN_GROUPS)InfoBuffer;
  132. DWORD dwInfoBufferSize;
  133. PSID psidAdministrators;
  134. SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;
  135. UINT x;
  136. BOOL bSuccess;
  137. if(!OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE,
  138. &hAccessToken )) {
  139. if(GetLastError() != ERROR_NO_TOKEN)
  140. return FALSE;
  141. //
  142. // retry against process token if no thread token exists
  143. //
  144. if(!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY,
  145. &hAccessToken))
  146. return FALSE;
  147. }
  148. bSuccess = GetTokenInformation(hAccessToken,TokenGroups,InfoBuffer,
  149. 1024, &dwInfoBufferSize);
  150. CloseHandle(hAccessToken);
  151. if(!bSuccess )
  152. return FALSE;
  153. if(!AllocateAndInitializeSid(&siaNtAuthority, 2,
  154. SECURITY_BUILTIN_DOMAIN_RID,
  155. DOMAIN_ALIAS_RID_ADMINS,
  156. 0, 0, 0, 0, 0, 0,
  157. &psidAdministrators))
  158. return FALSE;
  159. // assume that we don't find the admin SID.
  160. bSuccess = FALSE;
  161. for(x=0;x<ptgGroups->GroupCount;x++)
  162. {
  163. if( EqualSid(psidAdministrators, ptgGroups->Groups[x].Sid) )
  164. {
  165. bSuccess = TRUE;
  166. break;
  167. }
  168. }
  169. FreeSid(psidAdministrators);
  170. return bSuccess;
  171. }
  172. t_string GUIDToTString(GUID Guid)
  173. {
  174. t_strstream strStream;
  175. t_string tsOut;
  176. strStream << _T("{");
  177. strStream.fill(_T('0'));
  178. strStream.width(8);
  179. strStream.flags(ios_base::right);
  180. strStream << hex << Guid.Data1;
  181. strStream << _T("-");
  182. strStream.width(4);
  183. strStream << hex << Guid.Data2;
  184. strStream << _T("-");
  185. strStream << hex << Guid.Data3;
  186. strStream << _T("-");
  187. // Data4 specifies an array of 8 bytes. The first 2 bytes contain
  188. // the third group of 4 hexadecimal digits. The remaining 6 bytes
  189. // contain the final 12 hexadecimal digits.
  190. #ifndef _UNICODE
  191. int i;
  192. strStream.width(1);
  193. BYTE Byte;
  194. int Int;
  195. for (i = 0; i < 2; i++)
  196. {
  197. Byte = Guid.Data4[i];
  198. Byte = Byte >> 4;
  199. Int = Byte;
  200. strStream << hex << Int;
  201. Byte = Guid.Data4[i];
  202. Byte = 0x0f & Byte;
  203. Int = Byte;
  204. strStream << hex << Int;
  205. }
  206. strStream << _T("-");
  207. strStream.width(1);
  208. for (i = 2; i < 8; i++)
  209. {
  210. BYTE Byte = Guid.Data4[i];
  211. Byte = Byte >> 4;
  212. Int = Byte;
  213. strStream << hex << Int;
  214. Byte = Guid.Data4[i];
  215. Byte = 0x0f & Byte;
  216. Int = Byte;
  217. strStream << hex << Int;
  218. }
  219. #else
  220. int i;
  221. for (i = 0; i < 2; i++)
  222. {
  223. TCHAR tc = Guid.Data4[i];
  224. // For some reason the width is reset each time through the
  225. // loop to be one.
  226. strStream.width(2);
  227. strStream << hex << tc;
  228. }
  229. strStream << _T("-");
  230. BYTE Byte;
  231. strStream.width(1);
  232. for (i = 2; i < 8; i++)
  233. {
  234. Byte = Guid.Data4[i];
  235. Byte = Byte >> 4;
  236. strStream << hex << Byte;
  237. Byte = Guid.Data4[i];
  238. Byte = 0x0f & Byte;
  239. strStream << hex << Byte;
  240. }
  241. #endif
  242. strStream << _T("}");
  243. strStream >> tsOut;
  244. return tsOut;
  245. }
  246. LPTSTR LPTSTRFromGuid(GUID Guid)
  247. {
  248. t_string tsGuid = GUIDToTString(Guid);
  249. return NewTCHAR(tsGuid.c_str());
  250. }
  251. t_string ULONGVarToTString(ULONG ul, bool bHex)
  252. {
  253. t_string tsTemp;
  254. t_strstream strStream;
  255. if (bHex)
  256. {
  257. strStream.width(8);
  258. strStream.fill('0');
  259. strStream.flags(ios_base::right);
  260. strStream << hex << ul;
  261. }
  262. else
  263. {
  264. strStream << ul;
  265. }
  266. strStream >> tsTemp;
  267. if (bHex)
  268. {
  269. t_string tsHex;
  270. tsHex = _T("0x");
  271. tsHex += tsTemp;
  272. return tsHex;
  273. }
  274. else
  275. {
  276. return tsTemp;
  277. }
  278. }