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.

368 lines
10 KiB

  1. #include "PreComp.h"
  2. #undef POLARITY
  3. #include <windows.h>
  4. #include <objbase.h>
  5. #include "os.h"
  6. #include <autoptr.h>
  7. namespace OS
  8. {
  9. int sysVersion();
  10. const bool unicodeOS_ = unicodeOS();
  11. const bool secureOS_ = unicodeOS();
  12. const int osVer_ = sysVersion();
  13. DWORD convert2ansi(const wchar_t* unicode, wmilib::auto_buffer<char>& ansi)
  14. {
  15. if (unicode)
  16. {
  17. size_t class_len = wcslen(unicode);
  18. ansi = wmilib::auto_buffer<char>(new char[2*class_len+1],2*class_len+1);
  19. if (ansi.get() == 0)
  20. return ERROR_NOT_ENOUGH_MEMORY;
  21. if (wcstombs(ansi.get(),unicode, 2*class_len+1)==-1)
  22. return ERROR_NO_UNICODE_TRANSLATION;
  23. }
  24. return ERROR_SUCCESS;
  25. };
  26. bool unicodeOS()
  27. {
  28. OSVERSIONINFOA OsVersionInfoA;
  29. OsVersionInfoA.dwOSVersionInfoSize = sizeof (OSVERSIONINFOA) ;
  30. GetVersionExA(&OsVersionInfoA);
  31. return (OsVersionInfoA.dwPlatformId == VER_PLATFORM_WIN32_NT);
  32. };
  33. int sysVersion()
  34. {
  35. OSVERSIONINFOA OsVersionInfoA;
  36. OsVersionInfoA.dwOSVersionInfoSize = sizeof (OSVERSIONINFOA) ;
  37. GetVersionExA(&OsVersionInfoA);
  38. //return (OsVersionInfoA.dwMajorVersion);
  39. return 4;
  40. };
  41. HRESULT
  42. CoImpersonateClient()
  43. {
  44. if (secureOS_) return ::CoImpersonateClient();
  45. else return S_OK;
  46. }
  47. LONG RegOpenKeyExW (HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
  48. {
  49. if (unicodeOS_)
  50. return ::RegOpenKeyExW(hKey, lpSubKey, ulOptions, samDesired, phkResult);
  51. size_t key_len = wcslen(lpSubKey);
  52. wmilib::auto_buffer<char> ansi_key (new char[2*key_len+1]);
  53. if (ansi_key.get() == 0)
  54. return ERROR_NOT_ENOUGH_MEMORY;
  55. if (wcstombs(ansi_key.get(),lpSubKey, 2*key_len+1)==-1)
  56. return ERROR_NO_UNICODE_TRANSLATION;
  57. return ::RegOpenKeyExA(hKey, ansi_key.get(), ulOptions, samDesired, phkResult);
  58. };
  59. LONG RegCreateKeyExW(HKEY hKey, LPCWSTR lpSubKey,DWORD Reserved, LPWSTR lpClass,DWORD dwOptions,REGSAM samDesired,LPSECURITY_ATTRIBUTES lpSecurityAttributes, PHKEY phkResult, LPDWORD lpdwDisposition)
  60. {
  61. if (unicodeOS_)
  62. return ::RegCreateKeyExW(hKey, lpSubKey, Reserved, lpClass, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition);
  63. wmilib::auto_buffer<char> ansi_class;
  64. if (lpClass)
  65. {
  66. size_t class_len = wcslen(lpClass);
  67. ansi_class = wmilib::auto_buffer<char>(new char[2*class_len+1]);
  68. if (ansi_class.get() == 0)
  69. return ERROR_NOT_ENOUGH_MEMORY;
  70. if (wcstombs(ansi_class.get(),lpClass, 2*class_len+1)==-1)
  71. return ERROR_NO_UNICODE_TRANSLATION;
  72. };
  73. size_t key_len = wcslen(lpSubKey);
  74. wmilib::auto_buffer<char> ansi_key (new char[2*key_len+1]);
  75. if (ansi_key.get() == 0)
  76. return ERROR_NOT_ENOUGH_MEMORY;
  77. if (wcstombs(ansi_key.get(),lpSubKey, 2*key_len+1)==-1)
  78. return ERROR_NO_UNICODE_TRANSLATION;
  79. return ::RegCreateKeyExA(hKey, ansi_key.get(), Reserved, ansi_class.get(), dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition);
  80. };
  81. LONG RegEnumKeyExW (HKEY hKey,DWORD dwIndex,LPWSTR lpName,LPDWORD lpcName,LPDWORD lpReserved,LPWSTR lpClass,LPDWORD lpcClass,PFILETIME lpftLastWriteTime)
  82. {
  83. if (unicodeOS_)
  84. return ::RegEnumKeyExW(hKey,dwIndex,lpName,lpcName,lpReserved,lpClass,lpcClass,lpftLastWriteTime);
  85. DWORD nameLength = *lpcName ;
  86. wmilib::auto_buffer<char> ansi_name(new char[(*lpcName)*2]);
  87. if (ansi_name.get() == 0)
  88. return ERROR_NOT_ENOUGH_MEMORY;
  89. LONG return_code = ::RegEnumKeyExA(hKey,dwIndex,ansi_name.get(),&nameLength,0, 0, 0,lpftLastWriteTime);
  90. if (return_code == ERROR_SUCCESS)
  91. {
  92. mbstowcs(lpName, ansi_name.get(), *lpcName);
  93. *lpcName = nameLength;
  94. }
  95. return return_code;
  96. };
  97. LONG RegDeleteKeyW (HKEY hKey, LPCWSTR lpSubKey)
  98. {
  99. if (unicodeOS_)
  100. return ::RegDeleteKeyW(hKey, lpSubKey);
  101. size_t key_len = wcslen(lpSubKey);
  102. wmilib::auto_buffer<char> ansi_key (new char[2*key_len+1]);
  103. if (ansi_key.get() == 0)
  104. return ERROR_NOT_ENOUGH_MEMORY;
  105. if (wcstombs(ansi_key.get(),lpSubKey, 2*key_len+1)==-1)
  106. return ERROR_NO_UNICODE_TRANSLATION;
  107. return ::RegDeleteKeyA(hKey, ansi_key.get());
  108. };
  109. LONG RegQueryValueExW( HKEY hKey, LPCWSTR lpValueName, LPDWORD lpReserved, LPDWORD lpType, LPBYTE lpData, LPDWORD lpcbData )
  110. {
  111. if (unicodeOS_)
  112. return ::RegQueryValueExW(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData);
  113. size_t key_len = wcslen(lpValueName);
  114. wmilib::auto_buffer<char> ansi_key (new char[2*key_len+1]);
  115. if (ansi_key.get() == 0)
  116. return ERROR_NOT_ENOUGH_MEMORY;
  117. if (wcstombs(ansi_key.get(),lpValueName, 2*key_len+1)==-1)
  118. return ERROR_NO_UNICODE_TRANSLATION;
  119. LONG available_size = *lpcbData;
  120. LONG return_code = ::RegQueryValueExA(hKey, ansi_key.get(), lpReserved, lpType, lpData, lpcbData);
  121. if (lpData==0)
  122. {
  123. *lpcbData *= 2;
  124. return return_code;
  125. }
  126. if (return_code == ERROR_SUCCESS && (*lpType == REG_EXPAND_SZ || *lpType == REG_SZ))
  127. {
  128. wmilib::auto_buffer<BYTE> tempData(new BYTE[*lpcbData]);
  129. if (tempData.get() == 0)
  130. return ERROR_NOT_ENOUGH_MEMORY;
  131. memcpy(tempData.get(), lpData, *lpcbData);
  132. const char * src = (char *)tempData.get();
  133. wchar_t * dst = (wchar_t*)lpData;
  134. if (*lpType==REG_SZ)
  135. *lpcbData = (mbstowcs(dst, src, available_size)+1)*sizeof(wchar_t);
  136. else
  137. {
  138. // Not Implemented
  139. return ERROR_NOT_ENOUGH_MEMORY;
  140. };
  141. }
  142. return return_code;
  143. };
  144. LONG RegSetValueExW(HKEY hKey, LPCWSTR lpValueName, DWORD Reserved, DWORD dwType, CONST BYTE *lpData, DWORD cbData)
  145. {
  146. if (unicodeOS_)
  147. return ::RegSetValueExW(hKey, lpValueName, Reserved, dwType, lpData, cbData);
  148. size_t key_len = wcslen(lpValueName);
  149. wmilib::auto_buffer<char> ansi_key (new char[2*key_len+1]);
  150. if (ansi_key.get() == 0)
  151. return ERROR_NOT_ENOUGH_MEMORY;
  152. if (wcstombs(ansi_key.get(),lpValueName, 2*key_len+1)==-1)
  153. return ERROR_NO_UNICODE_TRANSLATION;
  154. if (dwType==REG_EXPAND_SZ || dwType==REG_SZ)
  155. {
  156. const wchar_t * src = reinterpret_cast<const wchar_t*>(lpData);
  157. size_t value_len = cbData / sizeof(wchar_t) ;
  158. wmilib::auto_buffer<char> ansi_value (new char[2*value_len+1]);
  159. if (ansi_value.get() == 0)
  160. return ERROR_NOT_ENOUGH_MEMORY;
  161. char * dest = ansi_value.get();
  162. if (dwType==REG_SZ)
  163. {
  164. if ((value_len = wcstombs(dest,src, value_len)+1)==-1)
  165. return ERROR_NO_UNICODE_TRANSLATION;
  166. }
  167. else
  168. {
  169. // Not Implemented
  170. return ERROR_NOT_ENOUGH_MEMORY;
  171. };
  172. return ::RegSetValueExA(hKey, ansi_key.get(), Reserved, dwType, (const BYTE*)ansi_value.get(), value_len);
  173. }
  174. return ::RegSetValueExA(hKey, ansi_key.get(), Reserved, dwType, lpData, cbData);
  175. }
  176. BOOL GetProcessTimes(
  177. HANDLE hProcess, // handle to process
  178. LPFILETIME lpCreationTime, // process creation time
  179. LPFILETIME lpExitTime, // process exit time
  180. LPFILETIME lpKernelTime, // process kernel-mode time
  181. LPFILETIME lpUserTime // process user-mode time
  182. )
  183. {
  184. if (unicodeOS_)
  185. return ::GetProcessTimes(hProcess, lpCreationTime, lpExitTime, lpKernelTime, lpUserTime);
  186. FILETIME zero = {0, 0};
  187. *lpCreationTime = zero;
  188. *lpExitTime = zero;
  189. *lpKernelTime = zero;
  190. *lpUserTime = zero;
  191. return 1;
  192. };
  193. HANDLE CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCWSTR lpName )
  194. {
  195. if (unicodeOS_)
  196. return ::CreateEventW(lpEventAttributes, bManualReset, bInitialState, lpName);
  197. wmilib::auto_buffer<char> name;
  198. DWORD res = convert2ansi(lpName, name);
  199. if (res == ERROR_SUCCESS)
  200. return ::CreateEventA(lpEventAttributes, bManualReset, bInitialState, name.get());
  201. else
  202. ::SetLastError(res);
  203. return NULL;
  204. }
  205. HANDLE CreateMutexW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bInitialOwner, LPCWSTR lpName )
  206. {
  207. if (unicodeOS_)
  208. return ::CreateMutexW(lpEventAttributes, bInitialOwner, lpName);
  209. wmilib::auto_buffer<char> name;
  210. DWORD res = convert2ansi(lpName, name);
  211. if (res == ERROR_SUCCESS)
  212. return ::CreateMutexA(lpEventAttributes, bInitialOwner, name.get());
  213. else
  214. ::SetLastError(res);
  215. return NULL;
  216. }
  217. wchar_t ToUpper(wchar_t c)
  218. {
  219. if (unicodeOS_)
  220. {
  221. wchar_t wideChar ;
  222. if (LCMapStringW(LOCALE_INVARIANT, LCMAP_UPPERCASE, &c, 1, &wideChar, 1) ==0)
  223. {
  224. _DBG_BREAK;
  225. return c;
  226. }
  227. return wideChar;
  228. }
  229. else
  230. {
  231. char ansiString[16];
  232. char ansiUpper[16];
  233. wchar_t wideChar = c;
  234. if (WideCharToMultiByte(CP_ACP, 0, &c, 1, ansiString, 16, NULL, NULL))
  235. {
  236. if (LCMapStringA(LOCALE_INVARIANT, LCMAP_UPPERCASE, ansiString, -1, ansiUpper, 16) ==0)
  237. {
  238. _DBG_BREAK;
  239. return c;
  240. }
  241. MultiByteToWideChar(CP_ACP, 0, ansiUpper, -1, &wideChar, 1);
  242. return wideChar;
  243. };
  244. return c;
  245. }
  246. }
  247. wchar_t ToLower(wchar_t c)
  248. {
  249. if (unicodeOS_)
  250. {
  251. wchar_t wideChar ;
  252. if (LCMapStringW(LOCALE_INVARIANT, LCMAP_LOWERCASE, &c, 1, &wideChar, 1) ==0)
  253. {
  254. _DBG_BREAK;
  255. return c;
  256. }
  257. return wideChar;
  258. }
  259. else
  260. {
  261. char ansiString[16];
  262. char ansiUpper[16];
  263. wchar_t wideChar = c;
  264. if (WideCharToMultiByte(CP_ACP, 0, &c, 1, ansiString, 16, NULL, NULL))
  265. {
  266. if (LCMapStringA(LOCALE_INVARIANT, LCMAP_LOWERCASE, ansiString, -1, ansiUpper, 16) ==0)
  267. {
  268. _DBG_BREAK;
  269. return c;
  270. }
  271. MultiByteToWideChar(CP_ACP, 0, ansiUpper, -1, &wideChar, 1);
  272. return wideChar;
  273. };
  274. return c;
  275. }
  276. }
  277. bool wbem_iswdigit(wchar_t c)
  278. {
  279. WORD result;
  280. if (unicodeOS_)
  281. {
  282. if (GetStringTypeExW(LOCALE_INVARIANT, CT_CTYPE1, &c, 1, &result))
  283. {
  284. return (result & C1_DIGIT) != 0;
  285. };
  286. return false;
  287. }
  288. else
  289. {
  290. char ansiString[16];
  291. if (WideCharToMultiByte(CP_ACP, 0, &c, 1, ansiString, 16, NULL, NULL))
  292. {
  293. if (GetStringTypeExA(LOCALE_INVARIANT, CT_CTYPE1, ansiString, 1, &result))
  294. {
  295. return (result & C1_DIGIT) != 0;
  296. }
  297. };
  298. return false;
  299. }
  300. };
  301. bool wbem_iswalnum (wchar_t c)
  302. {
  303. WORD result;
  304. if (unicodeOS_)
  305. {
  306. if (GetStringTypeExW(LOCALE_INVARIANT, CT_CTYPE1, &c, 1, &result))
  307. {
  308. return (result & (C1_DIGIT | C1_ALPHA)) != 0;
  309. };
  310. return false;
  311. }
  312. else
  313. {
  314. char ansiString[16];
  315. if (WideCharToMultiByte(CP_ACP, 0, &c, 1, ansiString, 16, NULL, NULL))
  316. {
  317. if (GetStringTypeExA(LOCALE_INVARIANT, CT_CTYPE1, ansiString, 1, &result))
  318. {
  319. return (result & (C1_DIGIT | C1_ALPHA)) != 0;
  320. }
  321. };
  322. return false;
  323. }
  324. };
  325. };