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.

1475 lines
33 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1997-2002 Microsoft Corporation, All Rights Reserved
  4. //
  5. //***************************************************************************
  6. #include "precomp.h"
  7. #include "wmicom.h"
  8. #include "wmimap.h"
  9. #include <stdlib.h>
  10. #include <CWbemTime.h>
  11. /*
  12. These are the types we support
  13. CIM_SINT8 = 16,
  14. CIM_UINT8 = 17,
  15. CIM_SINT16 = 2,
  16. CIM_UINT16 = 18,
  17. CIM_SINT32 = 3,
  18. CIM_UINT32 = 19,
  19. CIM_SINT64 = 20,
  20. CIM_UINT64 = 21,
  21. CIM_REAL32 = 4,
  22. CIM_REAL64 = 5,
  23. CIM_BOOLEAN = 11,
  24. CIM_STRING = 8,
  25. CIM_DATETIME = 101,
  26. CIM_REFERENCE = 102,
  27. CIM_CHAR16 = 103,
  28. CIM_OBJECT = 13,
  29. CIM_FLAG_ARRAY = 0x2000
  30. */
  31. BOOL ValidateDateTime(WCHAR * wcsValue)
  32. {
  33. BOOL fRc = FALSE;
  34. // Pre-test
  35. // ========
  36. WCHAR * psz = wcsValue;
  37. if( psz )
  38. {
  39. if(wcslen(psz) == 25){
  40. if(psz[14] != L'.' && psz[14] != L'*'){
  41. goto ExitValidateTime;
  42. }
  43. if(psz[21] != L':' && psz[21] != L'-' && psz[21] != L'+' && psz[21] != L'*'){
  44. goto ExitValidateTime;
  45. }
  46. for(int i = 0; i < 25; i++){
  47. if(i == 21 || i == 14)
  48. continue;
  49. if(psz[i] != L'*' && !wbem_iswdigit(psz[i])){
  50. goto ExitValidateTime;
  51. }
  52. }
  53. // Passed pre-test. Check if any stars were specified
  54. // ==================================================
  55. if(wcschr(psz, L'*')) {
  56. // No further checking
  57. fRc = TRUE;
  58. goto ExitValidateTime;
  59. }
  60. if(psz[21] == L':'){
  61. // Interval -- no checking
  62. fRc = TRUE;
  63. goto ExitValidateTime;
  64. }
  65. WCHAR wcsTmp[SIZEOFWBEMDATETIME+2];
  66. StringCchCopyW(wcsTmp,SIZEOFWBEMDATETIME+2,wcsValue);
  67. CWbemTime Time;
  68. fRc = Time.SetDMTF(wcsTmp);
  69. }
  70. }
  71. ExitValidateTime:
  72. return fRc;
  73. }
  74. //=============================================================
  75. BOOL CWMIDataTypeMap::ConvertDWORDToWMIDataTypeAndWriteItToBlock(DWORD dwLong, int nSize )
  76. {
  77. BOOL fRc = FALSE;
  78. if( NaturallyAlignData( nSize, WRITE_IT)){
  79. if( nSize == sizeof(BYTE) ){
  80. m_pWMIBlockInfo->SetByte((BYTE)dwLong);
  81. }
  82. else if( nSize == sizeof(short)){
  83. m_pWMIBlockInfo->SetWord((WORD)dwLong);
  84. }
  85. else{
  86. m_pWMIBlockInfo->SetDWORD(dwLong);
  87. }
  88. fRc = TRUE;
  89. }
  90. return fRc;
  91. }
  92. //=============================================================
  93. DWORD CWMIDataTypeMap::ConvertWMIDataTypeToDWORD(int nSize)
  94. {
  95. DWORD dwLong = 0L;
  96. if( nSize == sizeof(BYTE) ){
  97. BYTE bByte;
  98. m_pWMIBlockInfo->GetByte(bByte);
  99. dwLong = (DWORD) bByte;
  100. }
  101. else if( nSize == sizeof(short)){
  102. WORD nInt;
  103. m_pWMIBlockInfo->GetWord(nInt);
  104. dwLong = (DWORD) nInt;
  105. }
  106. else{
  107. m_pWMIBlockInfo->GetDWORD(dwLong);
  108. }
  109. return dwLong;
  110. }
  111. //=============================================================
  112. BOOL CWMIDataTypeMap::SetDefaultMissingQualifierValue( CVARIANT & v, long lType, CVARIANT & vToSet )
  113. {
  114. BOOL fRc = TRUE;
  115. switch (lType){
  116. // CIM_SINT16 = 2,
  117. // CIM_CHAR16
  118. case CIM_CHAR16:
  119. break;
  120. // CIM_SINT8 = 16,
  121. case VT_I1:
  122. case VT_I2:
  123. vToSet.Clear();
  124. vToSet.SetShort(v.GetShort());
  125. break;
  126. // CIM_SINT32 = 3,
  127. // CIM_UINT32 = 19,
  128. // CIM_UINT16 = 18,
  129. case VT_UI2:
  130. case VT_I4:
  131. case VT_UI4:
  132. vToSet.Clear();
  133. vToSet.SetLONG(v.GetLONG());
  134. break;
  135. // CIM_REAL32 = 4,
  136. case VT_R4:
  137. case VT_R8:
  138. vToSet.Clear();
  139. vToSet.SetDouble(v.GetDouble());
  140. break;
  141. // CIM_SINT64 = 20,
  142. // CIM_UINT64 = 21,
  143. case VT_I8:
  144. case VT_UI8:
  145. vToSet.Clear();
  146. vToSet.SetStr(v.GetStr());
  147. break;
  148. // CIM_DATETIME = 101,
  149. case CIM_DATETIME:
  150. break;
  151. // CIM_STRING = 8,
  152. // CIM_REFERENCE = 102,
  153. // CIM_OBJECT = 13,
  154. // CIM_FLAG_ARRAY = 0x2000
  155. case VT_BSTR:
  156. break;
  157. // CIM_BOOLEAN = 11,
  158. case VT_BOOL:
  159. vToSet.Clear();
  160. vToSet.SetBool(v.GetBool());
  161. break;
  162. // CIM_UINT8 = 17,
  163. case VT_UI1:
  164. vToSet.Clear();
  165. vToSet.SetByte(v.GetByte());
  166. break;
  167. default:
  168. fRc = FALSE;
  169. }
  170. return fRc;
  171. }
  172. //=============================================================
  173. BOOL CWMIDataTypeMap::MissingQualifierValueMatches( CSAFEARRAY * pSafe, long i, CVARIANT & v, long lType, CVARIANT & vToCompare )
  174. {
  175. BOOL fRc = FALSE;
  176. switch (lType){
  177. // CIM_SINT16 = 2,
  178. // CIM_CHAR16
  179. case CIM_CHAR16:
  180. break;
  181. // CIM_SINT8 = 16,
  182. case VT_I1:
  183. case VT_I2:
  184. {
  185. short v1 = 0;
  186. if( pSafe )
  187. {
  188. if( S_OK != pSafe->Get(i, &v1))
  189. {
  190. return FALSE;
  191. }
  192. }
  193. else
  194. {
  195. v1 = (short) v.GetShort();
  196. }
  197. short v2 = (short)vToCompare.GetShort();
  198. if( v1 == v2){
  199. fRc = TRUE;
  200. }
  201. break;
  202. }
  203. // CIM_SINT32 = 3,
  204. // CIM_UINT32 = 19,
  205. // CIM_UINT16 = 18,
  206. case VT_UI2:
  207. case VT_I4:
  208. case VT_UI4:
  209. {
  210. long v1 = 0;
  211. if( pSafe )
  212. {
  213. if( S_OK != pSafe->Get(i, &v1))
  214. {
  215. return FALSE;
  216. }
  217. }
  218. else
  219. {
  220. v1 = (long)v.GetLONG();
  221. }
  222. long v2 = (long)vToCompare.GetLONG();
  223. if( v1 == v2){
  224. fRc = TRUE;
  225. }
  226. break;
  227. }
  228. // CIM_REAL32 = 4,
  229. case VT_R4:
  230. case VT_R8:
  231. {
  232. double v1 = 0;
  233. if( pSafe )
  234. {
  235. if( S_OK != pSafe->Get(i, &v1))
  236. {
  237. return FALSE;
  238. }
  239. }
  240. else
  241. {
  242. v1 = (double)v.GetDouble();
  243. }
  244. double v2 = (double)vToCompare.GetDouble();
  245. if( v1 == v2 ){
  246. fRc = TRUE;
  247. }
  248. break;
  249. }
  250. // CIM_SINT64 = 20,
  251. // CIM_UINT64 = 21,
  252. case VT_UI8:
  253. {
  254. unsigned __int64 I1 = 0L, I2 = 0L;
  255. CBSTR bstr2;
  256. CBSTR bstr1;
  257. if( pSafe )
  258. {
  259. if( S_OK != pSafe->Get(i, &bstr1))
  260. {
  261. return FALSE;
  262. }
  263. }
  264. else
  265. {
  266. bstr1.SetStr(v.GetStr());
  267. }
  268. bstr2.SetStr(vToCompare.GetStr());
  269. if( bstr1 && bstr2)
  270. {
  271. I1 =(unsigned __int64) _wtoi64(bstr1);
  272. I2 =(unsigned __int64)_wtoi64(bstr2);
  273. if( I1 == I2 ){
  274. fRc = TRUE;
  275. }
  276. }
  277. break;
  278. }
  279. case VT_I8:
  280. {
  281. __int64 I1 = 0L, I2 = 0L;
  282. CBSTR bstr2;
  283. CBSTR bstr1;
  284. if( pSafe )
  285. {
  286. if( S_OK != pSafe->Get(i, &bstr1))
  287. {
  288. return FALSE;
  289. }
  290. }
  291. else
  292. {
  293. bstr1.SetStr(v.GetStr());
  294. }
  295. bstr2.SetStr(vToCompare.GetStr());
  296. if( bstr1 && bstr2)
  297. {
  298. I1 = _wtoi64(bstr1);
  299. I2 = _wtoi64(bstr2);
  300. if( I1 == I2 ){
  301. fRc = TRUE;
  302. }
  303. }
  304. break;
  305. }
  306. // CIM_DATETIME = 101,
  307. case CIM_DATETIME:
  308. break;
  309. // CIM_STRING = 8,
  310. // CIM_REFERENCE = 102,
  311. // CIM_OBJECT = 13,
  312. // CIM_FLAG_ARRAY = 0x2000
  313. case VT_BSTR:
  314. break;
  315. // CIM_BOOLEAN = 11,
  316. case VT_BOOL:
  317. {
  318. BOOL v1 = 0;
  319. if( pSafe )
  320. {
  321. if( S_OK != pSafe->Get(i, &v1))
  322. {
  323. return FALSE;
  324. }
  325. }
  326. else
  327. {
  328. v1 = (BOOL)v.GetBool();
  329. }
  330. BOOL v2 = (BOOL)vToCompare.GetBool();
  331. if( v1 == v2 )
  332. {
  333. fRc = TRUE;
  334. }
  335. break;
  336. }
  337. // CIM_UINT8 = 17,
  338. case VT_UI1:
  339. {
  340. BYTE v1 = 0;
  341. if( pSafe )
  342. {
  343. if( S_OK != pSafe->Get(i, &v1))
  344. {
  345. return FALSE;
  346. }
  347. }
  348. else
  349. {
  350. v1 = (BYTE) v.GetByte();
  351. }
  352. BYTE v2 = (BYTE)vToCompare.GetByte();
  353. if( v1 == v2){
  354. fRc = TRUE;
  355. }
  356. break;
  357. }
  358. default:
  359. fRc = FALSE;
  360. }
  361. return fRc;
  362. }
  363. //=============================================================
  364. DWORD CWMIDataTypeMap::ArraySize(long lType,CVARIANT & var)
  365. {
  366. DWORD dwCount = 0;
  367. switch( lType ){
  368. case VT_I2:
  369. case VT_UI2:
  370. dwCount = (DWORD) var.GetShort();
  371. break;
  372. case VT_I4:
  373. case VT_UI4:
  374. case VT_R4:
  375. dwCount = (DWORD) var.GetLONG();
  376. break;
  377. case VT_UI1:
  378. case VT_I1:
  379. dwCount = (DWORD) var.GetByte();
  380. break;
  381. }
  382. return dwCount;
  383. }
  384. //=============================================================
  385. BOOL CWMIDataTypeMap::SetDataInDataBlock(CSAFEARRAY * pSafe, int i, CVARIANT & v, long lType, int nSize)
  386. {
  387. BOOL fRc = TRUE;
  388. switch (lType){
  389. // CIM_SINT16 = 2,
  390. // CIM_CHAR16
  391. case CIM_CHAR16:
  392. case VT_I2:
  393. {
  394. SHORT iShort;
  395. if( pSafe ){
  396. if( S_OK != pSafe->Get(i, &iShort)){
  397. return FALSE;
  398. }
  399. }
  400. else{
  401. iShort = v.GetShort();
  402. }
  403. if( NaturallyAlignData( sizeof(WORD), WRITE_IT)){
  404. m_pWMIBlockInfo->SetWord(iShort);
  405. }
  406. break;
  407. }
  408. // CIM_SINT32 = 3,
  409. // CIM_UINT32 = 19,
  410. // CIM_UINT16 = 18,
  411. case VT_UI2:
  412. case VT_I4:
  413. case VT_UI4:
  414. {
  415. DWORD dwLong = 0L;
  416. if( pSafe ){
  417. if( S_OK != pSafe->Get(i, &dwLong)){
  418. return FALSE;
  419. }
  420. }
  421. else{
  422. dwLong = v.GetLONG();
  423. }
  424. ConvertDWORDToWMIDataTypeAndWriteItToBlock(dwLong,nSize);
  425. break;
  426. }
  427. // CIM_REAL32 = 4,
  428. case VT_R4:
  429. {
  430. float fFloat;
  431. if( pSafe ){
  432. if( S_OK != pSafe->Get(i, &fFloat)){
  433. return FALSE;
  434. }
  435. }
  436. else{
  437. fFloat =(float) v.GetDouble();
  438. }
  439. if( NaturallyAlignData( sizeof(WORD), WRITE_IT )){
  440. m_pWMIBlockInfo->SetFloat(fFloat);
  441. }
  442. break;
  443. }
  444. // CIM_REAL64 = 5,
  445. case VT_R8:
  446. {
  447. DOUBLE dDouble;
  448. if( pSafe ){
  449. if( S_OK != pSafe->Get(i, &dDouble)){
  450. return FALSE;
  451. }
  452. }
  453. else{
  454. dDouble = v.GetDouble();
  455. }
  456. if( NaturallyAlignData( sizeof(DOUBLE),WRITE_IT)){
  457. m_pWMIBlockInfo->SetDouble(dDouble);
  458. }
  459. break;
  460. }
  461. // CIM_SINT64 = 20,
  462. case VT_I8:
  463. {
  464. __int64 Int64 = 0L;
  465. CBSTR bstr;
  466. if( pSafe )
  467. {
  468. if( S_OK != pSafe->Get(i, &bstr))
  469. {
  470. return FALSE;
  471. }
  472. }
  473. else
  474. {
  475. bstr.SetStr(v.GetStr());
  476. }
  477. if( bstr )
  478. {
  479. Int64 = _wtoi64(bstr);
  480. }
  481. if( NaturallyAlignData( sizeof(__int64), WRITE_IT ))
  482. {
  483. m_pWMIBlockInfo->SetSInt64(Int64);
  484. }
  485. break;
  486. }
  487. // CIM_UINT64 = 21,
  488. case VT_UI8:
  489. {
  490. unsigned __int64 Int64 = 0L;
  491. CBSTR bstr;
  492. if( pSafe )
  493. {
  494. if( S_OK != pSafe->Get(i, &bstr))
  495. {
  496. return FALSE;
  497. }
  498. }
  499. else
  500. {
  501. bstr.SetStr(v.GetStr());
  502. }
  503. if( bstr )
  504. {
  505. Int64 = (unsigned __int64) _wtoi64(bstr);
  506. }
  507. if( NaturallyAlignData( sizeof(unsigned __int64),WRITE_IT))
  508. {
  509. m_pWMIBlockInfo->SetUInt64(Int64);
  510. }
  511. break;
  512. }
  513. // CIM_DATETIME = 101,
  514. case CIM_DATETIME:
  515. {
  516. WORD wCount=0;
  517. CBSTR bstr;
  518. WCHAR wDateTime[SIZEOFWBEMDATETIME+2];
  519. memset( wDateTime,NULL,SIZEOFWBEMDATETIME+2 );
  520. BOOL fContinue = TRUE;
  521. if( pSafe )
  522. {
  523. if( S_OK != pSafe->Get(i, &bstr))
  524. {
  525. return FALSE;
  526. }
  527. }
  528. else
  529. {
  530. bstr.SetStr(v.GetStr());
  531. }
  532. //=========================================================
  533. // Initialize buffer
  534. //=========================================================
  535. if( bstr != NULL )
  536. {
  537. if( ValidateDateTime(bstr))
  538. {
  539. StringCchCopyW(wDateTime, SIZEOFWBEMDATETIME+2, bstr);
  540. }
  541. else
  542. {
  543. fContinue = FALSE;
  544. }
  545. }
  546. else
  547. {
  548. StringCchCopyW(wDateTime, SIZEOFWBEMDATETIME+2, L"00000000000000.000000:000");
  549. }
  550. if( fContinue )
  551. {
  552. if( NaturallyAlignData( SIZEOFWBEMDATETIME, WRITE_IT))
  553. {
  554. m_pWMIBlockInfo->SetString(wDateTime,SIZEOFWBEMDATETIME);
  555. }
  556. }
  557. else
  558. {
  559. return FALSE ;
  560. }
  561. break;
  562. }
  563. // CIM_STRING = 8,
  564. // CIM_REFERENCE = 102,
  565. // CIM_OBJECT = 13,
  566. // CIM_FLAG_ARRAY = 0x2000
  567. case VT_BSTR:
  568. {
  569. WORD wCount=0;
  570. CBSTR bstr;
  571. if( pSafe )
  572. {
  573. if( S_OK != pSafe->Get(i, &bstr))
  574. {
  575. return FALSE;
  576. }
  577. }
  578. else
  579. {
  580. bstr.SetStr(v.GetStr());
  581. }
  582. if( bstr != NULL )
  583. {
  584. wCount = (wcslen(bstr))* sizeof(WCHAR);
  585. }
  586. if( NaturallyAlignData( sizeof ( WORD ), WRITE_IT ) )
  587. {
  588. m_pWMIBlockInfo->SetWord(wCount);
  589. }
  590. if( bstr )
  591. {
  592. if( S_OK != m_pWMIBlockInfo->GetBufferReady ( wCount ) )
  593. {
  594. return FALSE;
  595. }
  596. m_pWMIBlockInfo->SetString(bstr,wCount);
  597. *m_pdwAccumulativeSizeOfBlock += wCount;
  598. }
  599. break;
  600. }
  601. // CIM_BOOLEAN = 11,
  602. case VT_BOOL:
  603. {
  604. BYTE bByte;
  605. if( pSafe ){
  606. BOOL bTmp;
  607. if( S_OK != pSafe->Get(i, &bTmp)){
  608. return FALSE;
  609. }
  610. bByte = (BYTE) bTmp;
  611. }
  612. else{
  613. bByte =(BYTE) v.GetBool();
  614. }
  615. if( NaturallyAlignData( sizeof(BYTE), WRITE_IT )){
  616. m_pWMIBlockInfo->SetByte(bByte);
  617. }
  618. break;
  619. }
  620. // CIM_UINT8 = 17,
  621. case VT_UI1:
  622. {
  623. BYTE bByte;
  624. if( pSafe ){
  625. if( S_OK != pSafe->Get(i, &bByte)){
  626. return FALSE;
  627. }
  628. }
  629. else{
  630. bByte = v.GetByte();
  631. }
  632. if( NaturallyAlignData( 1, WRITE_IT )){
  633. m_pWMIBlockInfo->SetByte(bByte);
  634. }
  635. break;
  636. }
  637. // CIM_SINT8 = 16,
  638. case VT_I1:
  639. {
  640. short tmpShort;
  641. if( pSafe ){
  642. if( S_OK != pSafe->Get(i, &tmpShort)){
  643. return FALSE;
  644. }
  645. }
  646. else{
  647. tmpShort = v.GetShort();
  648. }
  649. if( NaturallyAlignData( 1, WRITE_IT )){
  650. BYTE bByte = (signed char)tmpShort;
  651. m_pWMIBlockInfo->SetByte(bByte);
  652. }
  653. break;
  654. }
  655. default:
  656. fRc = FALSE;
  657. }
  658. return fRc;
  659. }
  660. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  661. HRESULT CWMIDataTypeMap::GetDataFromDataBlock(IWbemObjectAccess * p, long lHandle, long lType, int nSize)
  662. {
  663. HRESULT hr = S_OK;
  664. switch (lType){
  665. // CIM_SINT32 = 3,
  666. // CIM_UINT32 = 19,
  667. case VT_I4:
  668. case VT_UI4:
  669. if( NaturallyAlignData( nSize, READ_IT )){
  670. hr = p->WriteDWORD(lHandle,ConvertWMIDataTypeToDWORD(nSize));
  671. }
  672. break;
  673. case VT_I8:
  674. case VT_UI8:
  675. if( NaturallyAlignData( nSize, READ_IT ))
  676. {
  677. unsigned __int64 uInt64;
  678. m_pWMIBlockInfo->GetQWORD(uInt64);
  679. hr = p->WriteQWORD(lHandle,uInt64);
  680. }
  681. break;
  682. // CIM_UINT16 = 18,
  683. // CIM_SINT16 = 2,
  684. // CIM_CHAR16 = 103,
  685. case VT_I2:
  686. case VT_UI2:
  687. case CIM_CHAR16:
  688. if( NaturallyAlignData( nSize, READ_IT )){
  689. WORD wWord;
  690. m_pWMIBlockInfo->GetWord(wWord);
  691. // Read but don't assign to anything
  692. }
  693. break;
  694. // CIM_REAL32 = 4,
  695. case VT_R4:
  696. if( NaturallyAlignData( nSize, READ_IT)){
  697. float fFloat;
  698. m_pWMIBlockInfo->GetFloat(fFloat);
  699. // Read but don't assign to anything
  700. }
  701. break;
  702. // CIM_REAL64 = 5,
  703. case VT_R8:
  704. if( NaturallyAlignData( nSize, READ_IT )){
  705. DOUBLE dDouble;
  706. m_pWMIBlockInfo->GetDouble(dDouble);
  707. // Read but don't assign to anything
  708. }
  709. break;
  710. // CIM_DATETIME = 101, which is 25 WCHARS
  711. case CIM_DATETIME:
  712. if( NaturallyAlignData( SIZEOFWBEMDATETIME, READ_IT ))
  713. {
  714. WORD wSize = SIZEOFWBEMDATETIME + 2 ;
  715. WCHAR Buffer[SIZEOFWBEMDATETIME + 2] ;
  716. m_pWMIBlockInfo->GetString(Buffer,SIZEOFWBEMDATETIME,wSize);
  717. // Read but don't assign to anything
  718. }
  719. break;
  720. // CIM_REFERENCE = 102,
  721. // CIM_STRING = 8,
  722. case VT_BSTR:
  723. if( NaturallyAlignData( 2, READ_IT )){
  724. WORD wCount=0;
  725. WCHAR * pBuffer=NULL;
  726. // Get the size of the string
  727. m_pWMIBlockInfo->GetWord(wCount);
  728. if( wCount > 0 )
  729. {
  730. if( m_pWMIBlockInfo->CurrentPtrOk((ULONG)(wCount)) )
  731. {
  732. WORD wSize = wCount + 2;
  733. pBuffer = new WCHAR[wSize];
  734. if( pBuffer )
  735. {
  736. try
  737. {
  738. m_pWMIBlockInfo->GetString(pBuffer,wCount,wSize);
  739. // Read but don't assign to anything
  740. SAFE_DELETE_ARRAY(pBuffer);
  741. *m_pdwAccumulativeSizeOfBlock += wCount;
  742. }
  743. catch(...)
  744. {
  745. hr = WBEM_E_UNEXPECTED;
  746. SAFE_DELETE_ARRAY(pBuffer);
  747. throw;
  748. }
  749. }
  750. }
  751. else
  752. {
  753. hr = WBEM_E_INVALID_OBJECT;
  754. }
  755. }
  756. }
  757. break;
  758. // CIM_BOOLEAN = 11,
  759. case VT_BOOL:
  760. if( NaturallyAlignData( nSize, READ_IT )){
  761. BYTE bByte=0;
  762. m_pWMIBlockInfo->GetByte(bByte);
  763. // Read but don't assign to anything
  764. }
  765. break;
  766. // CIM_SINT8 = 16,
  767. // CIM_UINT8 = 17,
  768. case VT_UI1:
  769. case VT_I1:
  770. if( NaturallyAlignData( nSize, READ_IT )){
  771. BYTE bByte=0;
  772. m_pWMIBlockInfo->GetByte(bByte);
  773. // Read but don't assign to anything
  774. }
  775. break;
  776. default:
  777. return WBEM_E_INVALID_OBJECT;
  778. }
  779. return hr;
  780. }
  781. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  782. HRESULT CWMIDataTypeMap::GetDataFromDataBlock(CVARIANT & v, long lType, int nSize )
  783. {
  784. HRESULT hr = WBEM_E_INVALID_OBJECT;
  785. switch (lType){
  786. // CIM_SINT16 = 2,
  787. // CIM_CHAR16 = 103,
  788. case VT_I2:
  789. case CIM_CHAR16:
  790. if( NaturallyAlignData( nSize, READ_IT )){
  791. hr = WBEM_S_NO_ERROR;
  792. WORD wWord;
  793. m_pWMIBlockInfo->GetWord(wWord);
  794. v.SetShort(wWord);
  795. }
  796. break;
  797. // CIM_SINT32 = 3,
  798. // CIM_UINT16 = 18,
  799. // CIM_UINT32 = 19,
  800. case VT_I4:
  801. case VT_UI2:
  802. case VT_UI4:
  803. if( NaturallyAlignData( nSize, READ_IT )){
  804. hr = WBEM_S_NO_ERROR;
  805. v.SetLONG(ConvertWMIDataTypeToDWORD(nSize));
  806. }
  807. break;
  808. // CIM_REAL32 = 4,
  809. case VT_R4:
  810. if( NaturallyAlignData( nSize, READ_IT)){
  811. hr = WBEM_S_NO_ERROR;
  812. float fFloat;
  813. m_pWMIBlockInfo->GetFloat(fFloat);
  814. v.SetDouble(fFloat);
  815. }
  816. break;
  817. // CIM_REAL64 = 5,
  818. case VT_R8:
  819. if( NaturallyAlignData( nSize, READ_IT )){
  820. hr = WBEM_S_NO_ERROR;
  821. DOUBLE dDouble;
  822. m_pWMIBlockInfo->GetDouble(dDouble);
  823. v.SetDouble(dDouble);
  824. }
  825. break;
  826. // CIM_SINT64 = 20,
  827. case VT_I8:
  828. if( NaturallyAlignData( nSize, READ_IT )){
  829. hr = WBEM_S_NO_ERROR;
  830. WCHAR pwcsBuffer[256];
  831. memset(pwcsBuffer,NULL,256);
  832. if ( SUCCEEDED ( hr = m_pWMIBlockInfo->GetSInt64 ( pwcsBuffer, 256 ) ) )
  833. {
  834. v.SetStr(pwcsBuffer);
  835. }
  836. }
  837. break;
  838. // CIM_UINT64 = 21,
  839. case VT_UI8:
  840. if( NaturallyAlignData( nSize, READ_IT )){
  841. hr = WBEM_S_NO_ERROR;
  842. WCHAR pwcsBuffer[256];
  843. memset(pwcsBuffer,NULL,256);
  844. if ( SUCCEEDED ( hr = m_pWMIBlockInfo->GetUInt64 ( pwcsBuffer, 256 ) ) )
  845. {
  846. v.SetStr(pwcsBuffer);
  847. }
  848. }
  849. break;
  850. // CIM_DATETIME = 101, which is 25 WCHARS
  851. case CIM_DATETIME:
  852. v.SetStr(NULL);
  853. if( NaturallyAlignData( SIZEOFWBEMDATETIME, READ_IT ))
  854. {
  855. hr = WBEM_S_NO_ERROR;
  856. WORD wSize = SIZEOFWBEMDATETIME + 2 ;
  857. WCHAR Buffer[SIZEOFWBEMDATETIME + 2] ;
  858. m_pWMIBlockInfo->GetString(Buffer,SIZEOFWBEMDATETIME,wSize);
  859. if( wbem_wcsicmp(Buffer,L"00000000000000.000000:000") != 0 ){
  860. v.SetStr(Buffer);
  861. }
  862. }
  863. break;
  864. // CIM_REFERENCE = 102,
  865. // CIM_STRING = 8,
  866. case VT_BSTR:
  867. v.SetStr(NULL);
  868. if( NaturallyAlignData( 2, READ_IT )){
  869. WORD wCount=0;
  870. WCHAR * pBuffer=NULL;
  871. // Get the size of the string
  872. m_pWMIBlockInfo->GetWord(wCount);
  873. hr = WBEM_S_NO_ERROR;
  874. if( wCount > 0 ){
  875. if( m_pWMIBlockInfo->CurrentPtrOk((ULONG)(wCount)) ){
  876. WORD wSize = wCount + 2;
  877. pBuffer = new WCHAR[wSize];
  878. if( pBuffer )
  879. {
  880. try
  881. {
  882. m_pWMIBlockInfo->GetString(pBuffer,wCount,wSize);
  883. v.SetStr(pBuffer);
  884. SAFE_DELETE_ARRAY(pBuffer);
  885. *m_pdwAccumulativeSizeOfBlock += wCount;
  886. }
  887. catch(...)
  888. {
  889. hr = WBEM_E_UNEXPECTED;
  890. SAFE_DELETE_ARRAY(pBuffer);
  891. throw;
  892. }
  893. }
  894. }
  895. else{
  896. hr = WBEM_E_INVALID_OBJECT;
  897. }
  898. }
  899. else{
  900. v.SetStr(NULL);
  901. }
  902. }
  903. break;
  904. // CIM_BOOLEAN = 11,
  905. case VT_BOOL:
  906. if( NaturallyAlignData( nSize, READ_IT )){
  907. hr = WBEM_S_NO_ERROR;
  908. BYTE bByte=0;
  909. m_pWMIBlockInfo->GetByte(bByte);
  910. v.SetBool((BOOL)bByte);
  911. }
  912. break;
  913. // CIM_SINT8 = 16,
  914. // CIM_UINT8 = 17,
  915. case VT_UI1:
  916. case VT_I1:
  917. if( NaturallyAlignData( nSize, READ_IT )){
  918. hr = WBEM_S_NO_ERROR;
  919. BYTE bByte=0;
  920. m_pWMIBlockInfo->GetByte(bByte);
  921. if( lType == VT_I1 ){
  922. v.SetShort((signed char)bByte);
  923. }
  924. else{
  925. v.SetByte(bByte);
  926. }
  927. }
  928. break;
  929. default:
  930. return WBEM_E_INVALID_OBJECT;
  931. }
  932. return hr;
  933. }
  934. //////////////////////////////////////////////////////////////////////
  935. int CWMIDataTypeMap::GetWMISize(long lType)
  936. {
  937. int nWMISize = 0;
  938. switch(lType){
  939. // CIM_SINT8 = 16,
  940. // CIM_UINT8 = 17,
  941. case VT_I1:
  942. case VT_UI1:
  943. nWMISize = sizeof(BYTE);
  944. break;
  945. // CIM_SINT16 = 2,
  946. // CIM_UINT16 = 18,
  947. case VT_I2:
  948. case CIM_CHAR16:
  949. case VT_UI2:
  950. nWMISize = sizeof(short);
  951. break;
  952. // CIM_SINT32 = 3,
  953. // CIM_UINT32 = 19,
  954. case VT_I4:
  955. case VT_UI4:
  956. nWMISize = sizeof(DWORD);
  957. break;
  958. // CIM_SINT64 = 20,
  959. // CIM_UINT64 = 21,
  960. case VT_I8:
  961. case VT_UI8:
  962. nWMISize = sizeof(__int64);
  963. break;
  964. // CIM_REAL32 = 4,
  965. case VT_R4:
  966. nWMISize = sizeof(float);
  967. break;
  968. // CIM_REAL64 = 5,
  969. case VT_R8:
  970. nWMISize = sizeof(double);
  971. break;
  972. // CIM_BOOLEAN = 11,
  973. case VT_BOOL:
  974. nWMISize = sizeof(BYTE);
  975. break;
  976. case CIM_DATETIME:
  977. nWMISize = SIZEOFWBEMDATETIME;
  978. break;
  979. case CIM_STRING:
  980. nWMISize = 2;
  981. break;
  982. default:
  983. // CIM_STRING = 8,
  984. // CIM_REFERENCE = 102,
  985. // CIM_OBJECT = 13,
  986. // CIM_FLAG_ARRAY = 0x2000
  987. nWMISize = 0;
  988. }
  989. return nWMISize;
  990. }
  991. ///////////////////////////////////////////////////////////////
  992. long CWMIDataTypeMap::GetVariantType(WCHAR * wcsType)
  993. {
  994. long lType;
  995. // CIM_SINT8 = 16,
  996. if( 0 == wbem_wcsicmp( L"sint8",wcsType) ){
  997. lType = VT_I1;
  998. }
  999. // CIM_UINT8 = 17,
  1000. else if( 0 == wbem_wcsicmp( L"uint8",wcsType) ){
  1001. lType = VT_UI1;
  1002. }
  1003. // CIM_CHAR16 = 103,
  1004. else if( 0 == wbem_wcsicmp( L"char16",wcsType) ){
  1005. lType = VT_I2;
  1006. }
  1007. // CIM_SINT16 = 2,
  1008. else if( 0 == wbem_wcsicmp( L"sint16",wcsType) ){
  1009. lType = VT_I2;
  1010. }
  1011. // CIM_UINT16 = 18,
  1012. else if( 0 == wbem_wcsicmp( L"uint16",wcsType) ){
  1013. lType = VT_UI2;
  1014. }
  1015. // CIM_SINT32 = 3,
  1016. else if( 0 == wbem_wcsicmp( L"sint32",wcsType) ){
  1017. lType = VT_I4;
  1018. }
  1019. // CIM_UINT32 = 19,
  1020. else if( 0 == wbem_wcsicmp( L"uint32",wcsType) ){
  1021. lType = VT_UI4;
  1022. }
  1023. // CIM_SINT64 = 20,
  1024. else if( 0 == wbem_wcsicmp( L"sint64",wcsType) ){
  1025. lType = VT_I8;
  1026. }
  1027. // CIM_UINT64 = 21,
  1028. else if( 0 == wbem_wcsicmp( L"uint64",wcsType) ){
  1029. lType = VT_UI8;
  1030. }
  1031. // CIM_REAL32 = 4,
  1032. else if( 0 == wbem_wcsicmp( L"real32",wcsType) ){
  1033. lType = VT_R4;
  1034. }
  1035. // CIM_REAL64 = 5,
  1036. else if( 0 == wbem_wcsicmp( L"real64",wcsType) ){
  1037. lType = VT_R8;
  1038. }
  1039. // CIM_BOOLEAN = 11,
  1040. else if( 0 == wbem_wcsicmp( L"boolean",wcsType) ){
  1041. lType = VT_BOOL;
  1042. }
  1043. // CIM_DATETIME = 101,
  1044. else if( 0 == wbem_wcsicmp( L"datetime",wcsType) ){
  1045. lType = CIM_DATETIME;
  1046. }
  1047. // CIM_STRING = 8,
  1048. // CIM_REFERENCE = 102,
  1049. // CIM_OBJECT = 13,
  1050. // CIM_FLAG_ARRAY = 0x2000
  1051. else{
  1052. lType = VT_BSTR;
  1053. }
  1054. return lType;
  1055. }
  1056. ///////////////////////////////////////////////////////////////
  1057. WCHAR * CWMIDataTypeMap::SetVariantType(long lType)
  1058. {
  1059. if( lType & CIM_FLAG_ARRAY ){
  1060. lType = lType &~ CIM_FLAG_ARRAY;
  1061. }
  1062. switch(lType){
  1063. // CIM_SINT8 = 16,
  1064. case VT_I1:
  1065. return L"sint8";
  1066. // CIM_UINT8 = 17,
  1067. case VT_UI1:
  1068. return L"uint8";
  1069. // CIM_SINT16 = 2,
  1070. case VT_I2:
  1071. return L"sint16";
  1072. // CIM_UINT16 = 18,
  1073. case VT_UI2:
  1074. return L"uint16";
  1075. // CIM_SINT32 = 3,
  1076. case VT_I4:
  1077. return L"sint32";
  1078. // CIM_UINT32 = 19,
  1079. case VT_UI4:
  1080. return L"uint32";
  1081. // CIM_SINT64 = 20,
  1082. case VT_I8:
  1083. return L"sint64";
  1084. // CIM_UINT64 = 21,
  1085. case VT_UI8:
  1086. return L"uint64";
  1087. // CIM_REAL32 = 4,
  1088. case VT_R4:
  1089. return L"real32";
  1090. // CIM_REAL64 = 5,
  1091. case VT_R8:
  1092. return L"real64";
  1093. // CIM_BOOLEAN = 11,
  1094. case VT_BOOL:
  1095. return L"boolean";
  1096. // CIM_STRING = 8,
  1097. case VT_BSTR:
  1098. return L"string";
  1099. // CIM_CHAR16 = 103,
  1100. case CIM_CHAR16:
  1101. return L"char16";
  1102. // CIM_OBJECT = 13,
  1103. case CIM_OBJECT:
  1104. return L"object";
  1105. // CIM_REFERENCE = 102,
  1106. case CIM_REFERENCE:
  1107. return L"ref";
  1108. // CIM_DATETIME = 101,
  1109. case CIM_DATETIME:
  1110. return L"datetime";
  1111. default: return NULL;
  1112. }
  1113. }
  1114. ////////////////////////////////////////////////////////////////
  1115. void CWMIDataTypeMap::GetSizeAndType( WCHAR * wcsType, IDOrder * p, long & lType, int & nWMISize )
  1116. {
  1117. BOOL fArray = FALSE;
  1118. if( lType & CIM_FLAG_ARRAY ){
  1119. fArray = TRUE;
  1120. }
  1121. DWORD dwSizeObject = wcslen(L"object:");
  1122. if( 0 == wbem_wcsnicmp( L"object:",wcsType,dwSizeObject ) )
  1123. {
  1124. //============================================
  1125. // Extract out the object name
  1126. //============================================
  1127. WCHAR * pName = & wcsType [ dwSizeObject ];
  1128. p->SetEmbeddedName(pName);
  1129. lType = VT_UNKNOWN;
  1130. nWMISize = 0;
  1131. }
  1132. else{
  1133. lType = GetVariantType(wcsType);
  1134. nWMISize = GetWMISize(lType);
  1135. }
  1136. if( fArray ){
  1137. lType |= CIM_FLAG_ARRAY;
  1138. }
  1139. }
  1140. /////////////////////////////////////////////////////////////////////
  1141. long CWMIDataTypeMap::ConvertType(long lType )
  1142. {
  1143. long lConvert = lType;
  1144. switch (lType){
  1145. // CIM_SINT16 = 2,
  1146. // CIM_UINT16 = 18,
  1147. // CIM_SINT8
  1148. case VT_I1:
  1149. case VT_I2:
  1150. lConvert = VT_I2;
  1151. break;
  1152. case VT_UI2:
  1153. lConvert = VT_I4;
  1154. break;
  1155. // CIM_SINT32 = 3,
  1156. // CIM_UINT32 = 19,
  1157. case VT_I4:
  1158. case VT_UI4:
  1159. lConvert = VT_I4;
  1160. break;
  1161. // CIM_REAL32 = 4,
  1162. // CIM_REAL64 = 5,
  1163. case VT_R4:
  1164. case VT_R8:
  1165. lConvert = VT_R8;
  1166. break;
  1167. // CIM_SINT64 = 20,
  1168. // CIM_UINT64 = 21,
  1169. // CIM_STRING = 8,
  1170. // CIM_DATETIME = 101,
  1171. // CIM_REFERENCE = 102,
  1172. // CIM_CHAR16 = 103,
  1173. // CIM_OBJECT = 13,
  1174. // CIM_FLAG_ARRAY = 0x2000
  1175. case VT_I8:
  1176. case VT_UI8:
  1177. case VT_BSTR:
  1178. case CIM_DATETIME:
  1179. lConvert = VT_BSTR;
  1180. break;
  1181. // CIM_BOOLEAN = 11,
  1182. case VT_BOOL:
  1183. lConvert = VT_BOOL;
  1184. break;
  1185. // CIM_UINT8 = 17,
  1186. case VT_UI1:
  1187. lConvert = VT_UI1;
  1188. break;
  1189. case VT_UNKNOWN:
  1190. lConvert = VT_UNKNOWN;
  1191. break;
  1192. default:
  1193. break;
  1194. }
  1195. return lConvert;
  1196. }
  1197. /////////////////////////////////////////////////////////////////////
  1198. HRESULT CWMIDataTypeMap::PutInArray(SAFEARRAY * & psa,long * pi, long & lType, VARIANT * pVar)
  1199. {
  1200. HRESULT hr = WBEM_E_INVALID_OBJECT;
  1201. VARIANT v = *pVar;
  1202. switch (lType){
  1203. // CIM_SINT16 = 2,
  1204. // CIM_UINT16 = 18,
  1205. // CIM_SINT8
  1206. case VT_I1:
  1207. case VT_I2:
  1208. lType = V_VT(&v) = VT_I2;
  1209. hr = SafeArrayPutElement(psa,pi,&V_I2(&v));
  1210. break;
  1211. case VT_UI2:
  1212. lType = V_VT(&v) = VT_I4;
  1213. hr = SafeArrayPutElement(psa,pi,&V_I4(&v));
  1214. break;
  1215. // CIM_SINT32 = 3,
  1216. // CIM_UINT32 = 19,
  1217. case VT_I4:
  1218. case VT_UI4:
  1219. lType = V_VT(&v) = VT_I4;
  1220. hr = SafeArrayPutElement(psa,pi,&V_I4(&v));
  1221. break;
  1222. // CIM_REAL32 = 4,
  1223. // CIM_REAL64 = 5,
  1224. case VT_R4:
  1225. case VT_R8:
  1226. lType = V_VT(&v) = VT_R8;
  1227. hr = SafeArrayPutElement(psa,pi,&V_R8(&v));
  1228. break;
  1229. // CIM_SINT64 = 20,
  1230. // CIM_UINT64 = 21,
  1231. // CIM_STRING = 8,
  1232. // CIM_DATETIME = 101,
  1233. // CIM_REFERENCE = 102,
  1234. // CIM_CHAR16 = 103,
  1235. // CIM_OBJECT = 13,
  1236. // CIM_FLAG_ARRAY = 0x2000
  1237. case VT_I8:
  1238. case VT_UI8:
  1239. case VT_BSTR:
  1240. case CIM_DATETIME:
  1241. lType = V_VT(&v) = VT_BSTR;
  1242. hr = SafeArrayPutElement(psa,pi,V_BSTR(&v));
  1243. break;
  1244. // CIM_BOOLEAN = 11,
  1245. case VT_BOOL:
  1246. lType = V_VT(&v) = VT_BOOL;
  1247. hr = SafeArrayPutElement(psa,pi,&V_BOOL(&v));
  1248. break;
  1249. // CIM_UINT8 = 17,
  1250. case VT_UI1:
  1251. lType = V_VT(&v) = VT_UI1;
  1252. hr = SafeArrayPutElement(psa,pi,&V_UI1(&v));
  1253. break;
  1254. case VT_UNKNOWN:
  1255. lType = V_VT(&v) = VT_UNKNOWN;
  1256. hr = SafeArrayPutElement(psa,pi,V_UNKNOWN(&v));
  1257. break;
  1258. default:
  1259. break;
  1260. }
  1261. return hr;
  1262. }
  1263. //////////////////////////////////////////////////////////////////////
  1264. BOOL CWMIDataTypeMap::NaturallyAlignData( int nSize, BOOL fRead )
  1265. {
  1266. BOOL fRc = FALSE;
  1267. DWORD dwBytesToPad = 0;
  1268. if( *m_pdwAccumulativeSizeOfBlock != 0 ){
  1269. DWORD dwMod;
  1270. int nNewSize = nSize;
  1271. if( nSize == SIZEOFWBEMDATETIME ){
  1272. nNewSize = 2;
  1273. }
  1274. dwMod = *m_pdwAccumulativeSizeOfBlock % nNewSize;
  1275. if( dwMod > 0 ){
  1276. dwBytesToPad = (DWORD)nNewSize - dwMod;
  1277. }
  1278. }
  1279. if( fRead ){
  1280. if( m_pWMIBlockInfo->CurrentPtrOk((ULONG)(dwBytesToPad+nSize)) ){
  1281. fRc = TRUE;
  1282. }
  1283. else{
  1284. dwBytesToPad = 0;
  1285. }
  1286. }
  1287. else{
  1288. fRc = TRUE;
  1289. }
  1290. m_pWMIBlockInfo->AddPadding(dwBytesToPad);
  1291. *m_pdwAccumulativeSizeOfBlock += nSize + dwBytesToPad;
  1292. return fRc;
  1293. }