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.

374 lines
9.8 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1999-2000 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // Util.cpp
  7. //
  8. // Description:
  9. // Implementation of utility class and functions
  10. //
  11. // Author:
  12. // Henry Wang (HenryWa) 24-AUG-1999
  13. // MSP Prabu (mprabu) 06-Jan-2001
  14. // Jim Benton (jbenton) 15-Oct-2001
  15. //
  16. //////////////////////////////////////////////////////////////////////////////
  17. #include "Pch.h"
  18. #include "ProvBase.h"
  19. //////////////////////////////////////////////////////////////////////////////
  20. // Global Data
  21. //////////////////////////////////////////////////////////////////////////////
  22. //
  23. // wbem
  24. //
  25. const WCHAR * const PVD_WBEM_PROVIDERNAME = L"Volume Shadow Copy WMI Provider";
  26. const WCHAR * const PVD_WBEM_EXTENDEDSTATUS = L"__ExtendedStatus";
  27. const WCHAR * const PVD_WBEM_DESCRIPTION = L"Description";
  28. const WCHAR * const PVD_WBEM_STATUSCODE = L"StatusCode";
  29. const WCHAR * const PVD_WBEM_STATUS = L"Status";
  30. const WCHAR * const PVD_WBEM_CLASS = L"__CLASS";
  31. const WCHAR * const PVD_WBEM_RELPATH = L"__Relpath";
  32. const WCHAR * const PVD_WBEM_PROP_ANTECEDENT = L"Antecedent";
  33. const WCHAR * const PVD_WBEM_PROP_DEPENDENT = L"Dependent";
  34. const WCHAR * const PVD_WBEM_PROP_ELEMENT = L"Element";
  35. const WCHAR * const PVD_WBEM_PROP_SETTING = L"Setting";
  36. const WCHAR * const PVD_WBEM_PROP_DEVICEID = L"DeviceId";
  37. const WCHAR * const PVD_WBEM_PROP_RETURNVALUE = L"ReturnValue";
  38. const WCHAR * const PVD_WBEM_PROP_PROVIDERNAME = L"ProviderName";
  39. const WCHAR * const PVD_WBEM_QUA_DYNAMIC = L"Dynamic";
  40. const WCHAR * const PVD_WBEM_QUA_CIMTYPE = L"CIMTYPE";
  41. const WCHAR * const PVD_WBEM_DATETIME_FORMAT =L"%04d%02d%02d%02d%02d%02d.%06d%+03d";
  42. //
  43. // Constants used in partial value maps for localization
  44. //
  45. const WCHAR * const PVDR_CONS_UNAVAILABLE = L"Unavailable";
  46. const WCHAR * const PVDR_CONS_ENABLED = L"Enabled";
  47. const WCHAR * const PVDR_CONS_DISABLED = L"Disabled";
  48. //////////////////////////////////////////////////////////////////////////////
  49. //++
  50. //
  51. // void
  52. // CreateClass(
  53. // const WCHAR * pwszClassNameIn,
  54. // CWbemServices * pNamespaceIn,
  55. // auto_ptr< CProvBase > & rNewClassInout
  56. // )
  57. //
  58. // Description:
  59. // Create the specified class
  60. //
  61. // Arguments:
  62. // pwszClassNameIn -- Name of the class to create.
  63. // pNamespaceIn -- WMI namespace
  64. // rNewClassInout -- Receives the new class.
  65. //
  66. // Return Values:
  67. // reference to the array of property maping table
  68. //
  69. //--
  70. //////////////////////////////////////////////////////////////////////////////
  71. void
  72. CreateClass(
  73. IN const WCHAR * pwszClassName,
  74. IN CWbemServices * pNamespace,
  75. IN OUT auto_ptr< CProvBase > & rNewClass
  76. )
  77. {
  78. CVssFunctionTracer ft(VSSDBG_VSSADMIN, L"CreateClass");
  79. ClassMap::iterator itMap;
  80. _ASSERTE(pwszClassName != NULL);
  81. _ASSERTE(pNamespace != NULL);
  82. itMap = g_ClassMap.find(pwszClassName);
  83. if ( itMap != g_ClassMap.end() )
  84. {
  85. CClassCreator& rcc = itMap->second;
  86. auto_ptr< CProvBase > pBase(
  87. rcc.m_pfnConstructor(
  88. rcc.m_pbstrClassName,
  89. pNamespace
  90. )
  91. );
  92. if (pBase.get() == NULL)
  93. throw CProvException( static_cast< HRESULT >( WBEM_E_INITIALIZATION_FAILURE ) );
  94. rNewClass = pBase;
  95. }
  96. else
  97. {
  98. throw CProvException( static_cast< HRESULT >( WBEM_E_INVALID_PARAMETER ) );
  99. }
  100. return;
  101. } //*** void CreateClass()
  102. WCHAR* GuidToString(
  103. IN GUID guid
  104. )
  105. {
  106. WCHAR* pwszGuid = reinterpret_cast<WCHAR*>(::CoTaskMemAlloc((g_cchGUID) * sizeof(WCHAR)));
  107. if (pwszGuid == NULL)
  108. throw CProvException(E_OUTOFMEMORY);
  109. _snwprintf(pwszGuid, g_cchGUID,
  110. L"{%.8x-%.4x-%.4x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x}",
  111. GUID_PRINTF_ARG(guid));
  112. return pwszGuid;
  113. }
  114. _bstr_t EncodeQuotes( WCHAR * wszString )
  115. {
  116. // Encodes only double-quote and back-slash in the key
  117. // property strings in building the object path string.
  118. _bstr_t bstrTemp;
  119. if( wszString == NULL )
  120. return bstrTemp;
  121. int nSize = 0;
  122. WCHAR* des = NULL;
  123. WCHAR* src = wszString;
  124. // loop to find the character to encode
  125. while(*src)
  126. {
  127. // check character
  128. switch(*src)
  129. {
  130. case L'"':
  131. nSize += 2;
  132. break;
  133. case L'\\':
  134. nSize += 2;
  135. break;
  136. default:
  137. nSize++;
  138. break;
  139. }
  140. src++;
  141. }
  142. // create buffer
  143. WCHAR* pwszEncoded = new WCHAR[nSize + 1];
  144. if(pwszEncoded == NULL)
  145. return bstrTemp;
  146. ZeroMemory(pwszEncoded, (nSize + 1)*sizeof(WCHAR));
  147. src = wszString;
  148. des = pwszEncoded;
  149. // loop to encode
  150. while(*src)
  151. {
  152. // check character
  153. switch(*src)
  154. {
  155. case L'"':
  156. lstrcpyn(des, L"\\\"", 3); // the char count includes null termination char
  157. des += 2;
  158. break;
  159. case L'\\':
  160. lstrcpyn(des, L"\\\\", 3); // the char count includes null termination char
  161. des += 2;
  162. break;
  163. default:
  164. *des = *src;
  165. des++;
  166. break;
  167. }
  168. src++;
  169. }
  170. bstrTemp = pwszEncoded;
  171. delete [] pwszEncoded;
  172. return bstrTemp;
  173. }
  174. LPWSTR GetMsg(
  175. IN LONG msgId,
  176. ...
  177. )
  178. {
  179. CVssFunctionTracer ft( VSSDBG_VSSADMIN, L"CVssAdminCLI::GetMsg" );
  180. va_list args;
  181. LPWSTR lpMsgBuf;
  182. LPWSTR lpReturnStr = NULL;
  183. va_start( args, msgId );
  184. if (::FormatMessageW(
  185. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE |
  186. FORMAT_MESSAGE_MAX_WIDTH_MASK,
  187. g_hModule,
  188. msgId,
  189. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  190. (LPWSTR) &lpMsgBuf,
  191. 0,
  192. &args
  193. ))
  194. {
  195. ::VssSafeDuplicateStr( ft, lpReturnStr, lpMsgBuf );
  196. ::LocalFree( lpMsgBuf );
  197. }
  198. else if (::FormatMessageW(
  199. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  200. FORMAT_MESSAGE_MAX_WIDTH_MASK,
  201. NULL,
  202. msgId,
  203. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  204. (LPWSTR) &lpMsgBuf,
  205. 0,
  206. &args ) )
  207. {
  208. ::VssSafeDuplicateStr( ft, lpReturnStr, lpMsgBuf );
  209. ::LocalFree( lpMsgBuf );
  210. }
  211. va_end( args );
  212. // Returns NULL if message was not found
  213. return lpReturnStr;
  214. }
  215. #ifdef PROP_ARRAY_ENABLE
  216. //****************************************************************************
  217. //
  218. // PropMapEntryArray
  219. //
  220. //****************************************************************************
  221. //////////////////////////////////////////////////////////////////////////////
  222. //++
  223. //
  224. // LPCWSTR
  225. // SPropMapEntryArray::PwszLookup(
  226. // LPCWSTR pwszIn
  227. // ) const
  228. //
  229. // Description:
  230. // Lookup an entry in the array.
  231. //
  232. // Arguments:
  233. // pwszIn -- Name of entry to lookup.
  234. //
  235. // Return Values:
  236. // Pointer to string entry in the array.
  237. //
  238. //--
  239. //////////////////////////////////////////////////////////////////////////////
  240. LPCWSTR
  241. SPropMapEntryArray::PwszLookup(
  242. IN LPCWSTR pwsz
  243. ) const
  244. {
  245. UINT idx;
  246. _ASSERTE(pwszIn != NULL);
  247. for ( idx = 0; idx < m_dwSize; idx ++ )
  248. {
  249. if ( _wcsicmp( pwsz, m_pArray[ idx ].clstName ) == 0 )
  250. {
  251. //
  252. // mofName is NULL for clstname not supported
  253. //
  254. return m_pArray[ idx ].mofName;
  255. }
  256. }
  257. //
  258. // mofname is the same as clstname if not found in the table
  259. //
  260. return pwsz;
  261. } //*** SPropMapEntry::PwszLookup()
  262. //////////////////////////////////////////////////////////////////////////////
  263. //++
  264. //
  265. // LPCWSTR
  266. // PwszSpaceReplace(
  267. // LPWSTR pwszTrgInout,
  268. // LPCWSTR pwszSrcIn,
  269. // WCHAR wchArgIn
  270. // )
  271. //
  272. // Description:
  273. // Replace spaces in a string with another character.
  274. // Ignores leading spaces.
  275. //
  276. // Arguments:
  277. // pwszTrgInout -- Target string.
  278. // pwszSrcIn -- Source string.
  279. // wchArgIn -- Character to replace spaces with.
  280. //
  281. // Return Values:
  282. // Pointer to the target string.
  283. //
  284. //--
  285. //////////////////////////////////////////////////////////////////////////////
  286. LPWSTR
  287. PwszSpaceReplace(
  288. IN OUT LPWSTR pwszTrg,
  289. IN LPCWSTR pwszSrc,
  290. IN WCHAR wchArg
  291. )
  292. {
  293. LPCWSTR pwsz = NULL;
  294. LPWSTR pwszTrg = NULL;
  295. if ( ( pwszTrg == NULL ) || ( pwszSrc == NULL ) )
  296. {
  297. return NULL;
  298. }
  299. //
  300. // ignore leading space
  301. //
  302. for ( pwsz = pwszSrc ; *pwsz == L' '; pwsz++ )
  303. {
  304. // empty loop
  305. }
  306. pwszTrg = pwszTrg;
  307. for ( ; *pwsz != L'\0' ; pwsz++ )
  308. {
  309. if ( *pwsz == L' ' )
  310. {
  311. *pwszTrg++ = wchArg;
  312. for ( ; *pwsz == L' '; pwsz++ )
  313. {
  314. // empty loop
  315. }
  316. pwsz--;
  317. }
  318. else
  319. {
  320. *pwszTrg++ = *pwsz;
  321. }
  322. } // for: each character in the source string
  323. *pwszTrg = L'\0';
  324. return pwszTrg;
  325. } //*** PwszSpaceReplace()
  326. #endif