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.

1289 lines
33 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. TEXTCONV.CPP
  5. Abstract:
  6. History:
  7. --*/
  8. #include "precomp.h"
  9. #include <float.h>
  10. #include <textconv.h>
  11. #include <WT_wstring.h>
  12. #include <bstring.h>
  13. #include <wbemidl.h>
  14. #include "WT_converter.h"
  15. char *g_aValidPropTypes[] =
  16. {
  17. "CIM_UINT8",
  18. "CIM_SINT8",
  19. "CIM_SINT16",
  20. "CIM_UINT16",
  21. "CIM_SINT32",
  22. "CIM_UINT32",
  23. "CIM_SINT64",
  24. "CIM_UINT64",
  25. "CIM_STRING",
  26. "CIM_BOOLEAN",
  27. "CIM_REAL32",
  28. "CIM_REAL64",
  29. "CIM_DATETIME",
  30. "CIM_REFERENCE",
  31. "CIM_OBJECT",
  32. "CIM_CHAR16",
  33. "CIM_EMPTY",
  34. };
  35. const int g_nNumValidPropTypes = sizeof(g_aValidPropTypes) / sizeof(char *);
  36. LPWSTR CreateUnicode(LPSTR sz)
  37. {
  38. int len = strlen(sz);
  39. WCHAR* wsz = new WCHAR[len+1];
  40. if (wsz == 0)
  41. return 0;
  42. mbstowcs(wsz, sz, len+1);
  43. wsz[len] = L'\0';
  44. return wsz;
  45. }
  46. LPSTR TypeToString(int nType)
  47. {
  48. static char *pCIM_EMPTY = "CIM_EMPTY";
  49. static char *pCIM_UINT8 = "CIM_UINT8";
  50. static char *pCIM_SINT8 = "CIM_SINT8";
  51. static char *pCIM_SINT16 = "CIM_SINT16";
  52. static char *pCIM_UINT16 = "CIM_UINT16";
  53. static char *pCIM_SINT32 = "CIM_SINT32";
  54. static char *pCIM_UINT32 = "CIM_UINT32";
  55. static char *pCIM_SINT64 = "CIM_SINT64";
  56. static char *pCIM_UINT64 = "CIM_UINT64";
  57. static char *pCIM_REAL32 = "CIM_REAL32";
  58. static char *pCIM_REAL64 = "CIM_REAL64";
  59. static char *pCIM_BOOLEAN = "CIM_BOOLEAN";
  60. static char *pCIM_STRING = "CIM_STRING";
  61. static char *pCIM_DATETIME = "CIM_DATETIME";
  62. static char *pCIM_REFERENCE = "CIM_REFERENCE";
  63. static char *pCIM_OBJECT = "CIM_OBJECT";
  64. static char *pCIM_CHAR16 = "CIM_CHAR16";
  65. static char *pCIM_UINT8_ARRAY = "CIM_UINT8 | CIM_FLAG_ARRAY";
  66. static char *pCIM_SINT8_ARRAY = "CIM_SINT8 | CIM_FLAG_ARRAY";
  67. static char *pCIM_SINT16_ARRAY = "CIM_SINT16 | CIM_FLAG_ARRAY";
  68. static char *pCIM_UINT16_ARRAY = "CIM_UINT16 | CIM_FLAG_ARRAY";
  69. static char *pCIM_SINT32_ARRAY = "CIM_SINT32 | CIM_FLAG_ARRAY";
  70. static char *pCIM_UINT32_ARRAY = "CIM_UINT32 | CIM_FLAG_ARRAY";
  71. static char *pCIM_SINT64_ARRAY = "CIM_SINT64 | CIM_FLAG_ARRAY";
  72. static char *pCIM_UINT64_ARRAY = "CIM_UINT64 | CIM_FLAG_ARRAY";
  73. static char *pCIM_REAL32_ARRAY = "CIM_REAL32 | CIM_FLAG_ARRAY";
  74. static char *pCIM_REAL64_ARRAY = "CIM_REAL64 | CIM_FLAG_ARRAY";
  75. static char *pCIM_BOOLEAN_ARRAY = "CIM_BOOLEA | CIM_FLAG_ARRAY";
  76. static char *pCIM_STRING_ARRAY = "CIM_STRING | CIM_FLAG_ARRAY";
  77. static char *pCIM_DATETIME_ARRAY = "CIM_DATETIME | CIM_FLAG_ARRAY";
  78. static char *pCIM_REFERENCE_ARRAY = "CIM_REFERENCE | CIM_FLAG_ARRAY";
  79. static char *pCIM_OBJECT_ARRAY = "CIM_OBJECT | CIM_FLAG_ARRAY";
  80. static char *pCIM_CHAR16_ARRAY = "CIM_CHAR16 | CIM_FLAG_ARRAY";
  81. char *pRetVal = "<unknown>";
  82. switch (nType)
  83. {
  84. case CIM_UINT8: pRetVal = pCIM_UINT8; break;
  85. case CIM_SINT16: pRetVal = pCIM_SINT16; break;
  86. case CIM_SINT32: pRetVal = pCIM_SINT32; break;
  87. case CIM_SINT8: pRetVal = pCIM_SINT8; break;
  88. case CIM_UINT16: pRetVal = pCIM_UINT16; break;
  89. case CIM_UINT32: pRetVal = pCIM_UINT32; break;
  90. case CIM_UINT64: pRetVal = pCIM_UINT64; break;
  91. case CIM_SINT64: pRetVal = pCIM_SINT64; break;
  92. case CIM_REAL32: pRetVal = pCIM_REAL32; break;
  93. case CIM_REAL64: pRetVal = pCIM_REAL64; break;
  94. case CIM_BOOLEAN: pRetVal = pCIM_BOOLEAN; break;
  95. case CIM_STRING: pRetVal = pCIM_STRING; break;
  96. case CIM_DATETIME: pRetVal = pCIM_DATETIME; break;
  97. case CIM_REFERENCE: pRetVal = pCIM_REFERENCE; break;
  98. case CIM_OBJECT: pRetVal = pCIM_OBJECT; break;
  99. case CIM_CHAR16: pRetVal = pCIM_CHAR16; break;
  100. case CIM_UINT8|CIM_FLAG_ARRAY: pRetVal = pCIM_UINT8_ARRAY; break;
  101. case CIM_SINT16|CIM_FLAG_ARRAY: pRetVal = pCIM_SINT16_ARRAY; break;
  102. case CIM_SINT32|CIM_FLAG_ARRAY: pRetVal = pCIM_SINT32_ARRAY; break;
  103. case CIM_SINT8|CIM_FLAG_ARRAY: pRetVal = pCIM_SINT8_ARRAY; break;
  104. case CIM_UINT16|CIM_FLAG_ARRAY: pRetVal = pCIM_UINT16_ARRAY; break;
  105. case CIM_UINT32|CIM_FLAG_ARRAY: pRetVal = pCIM_UINT32_ARRAY; break;
  106. case CIM_UINT64|CIM_FLAG_ARRAY: pRetVal = pCIM_UINT64_ARRAY; break;
  107. case CIM_SINT64|CIM_FLAG_ARRAY: pRetVal = pCIM_SINT64_ARRAY; break;
  108. case CIM_REAL32|CIM_FLAG_ARRAY: pRetVal = pCIM_REAL32_ARRAY; break;
  109. case CIM_REAL64|CIM_FLAG_ARRAY: pRetVal = pCIM_REAL64_ARRAY; break;
  110. case CIM_BOOLEAN|CIM_FLAG_ARRAY: pRetVal = pCIM_BOOLEAN_ARRAY; break;
  111. case CIM_STRING|CIM_FLAG_ARRAY: pRetVal = pCIM_STRING_ARRAY; break;
  112. case CIM_DATETIME|CIM_FLAG_ARRAY: pRetVal = pCIM_DATETIME_ARRAY; break;
  113. case CIM_REFERENCE|CIM_FLAG_ARRAY: pRetVal = pCIM_REFERENCE_ARRAY;break;
  114. case CIM_OBJECT|CIM_FLAG_ARRAY: pRetVal = pCIM_OBJECT_ARRAY; break;
  115. case CIM_CHAR16|CIM_FLAG_ARRAY: pRetVal = pCIM_CHAR16_ARRAY; break;
  116. }
  117. return pRetVal;
  118. }
  119. // Returns 0 on error
  120. int StringToType(LPSTR pString)
  121. {
  122. if(pString == NULL)
  123. return 0;
  124. if (wbem_stricmp(pString, "CIM_STRING") == 0)
  125. return CIM_STRING;
  126. if (wbem_stricmp(pString, "CIM_UINT8") == 0)
  127. return CIM_UINT8;
  128. if (wbem_stricmp(pString, "CIM_SINT16") == 0)
  129. return CIM_SINT16;
  130. if (wbem_stricmp(pString, "CIM_SINT32") == 0)
  131. return CIM_SINT32;
  132. if (wbem_stricmp(pString, "CIM_SINT8") == 0)
  133. return CIM_SINT8;
  134. if (wbem_stricmp(pString, "CIM_UINT16") == 0)
  135. return CIM_UINT16;
  136. if (wbem_stricmp(pString, "CIM_UINT32") == 0)
  137. return CIM_UINT32;
  138. if (wbem_stricmp(pString, "CIM_UINT64") == 0)
  139. return CIM_UINT64;
  140. if (wbem_stricmp(pString, "CIM_SINT64") == 0)
  141. return CIM_SINT64;
  142. if (wbem_stricmp(pString, "CIM_BOOLEAN") == 0)
  143. return CIM_BOOLEAN;
  144. if (wbem_stricmp(pString, "CIM_DATETIME") == 0)
  145. return CIM_DATETIME;
  146. if (wbem_stricmp(pString, "CIM_REFERENCE") == 0)
  147. return CIM_REFERENCE;
  148. if (wbem_stricmp(pString, "CIM_REAL32") == 0)
  149. return CIM_REAL32;
  150. if (wbem_stricmp(pString, "CIM_REAL64") == 0)
  151. return CIM_REAL64;
  152. if (wbem_stricmp(pString, "CIM_EMPTY") == 0)
  153. return CIM_EMPTY;
  154. if (wbem_stricmp(pString, "CIM_OBJECT") == 0)
  155. return CIM_OBJECT;
  156. if (wbem_stricmp(pString, "CIM_CHAR16") == 0)
  157. return CIM_CHAR16;
  158. return 0;
  159. }
  160. // The functions does a preliminary check on arrays looking for leading commas,
  161. // multiple commas, and trailing commas
  162. bool PrelimCheck(LPSTR pStr)
  163. {
  164. int iNumCommaSinceLastNum = 3;
  165. for(; *pStr; pStr++)
  166. {
  167. // If not a space or comma, assume we are in a number
  168. if(!isspace(*pStr) && *pStr != ',')
  169. iNumCommaSinceLastNum = 0;
  170. if(*pStr == ',')
  171. {
  172. if(iNumCommaSinceLastNum > 0)
  173. return false;
  174. iNumCommaSinceLastNum++;
  175. }
  176. }
  177. if(iNumCommaSinceLastNum > 0)
  178. return false;
  179. else
  180. return true;
  181. }
  182. CVarVector* GetVT_I8Array(LPSTR pStr)
  183. {
  184. if(!PrelimCheck(pStr))
  185. return NULL;
  186. CVarVector *pVec = new CVarVector(VT_BSTR);
  187. char buf[TEMP_BUF];
  188. int n = 0;
  189. buf[0] = 0;
  190. char* pc = pStr;
  191. while(*pc)
  192. {
  193. if(*pc == ',')
  194. {
  195. buf[n] = 0;
  196. if(n == 0)
  197. {
  198. delete pVec;
  199. return NULL;
  200. }
  201. WString s = buf;
  202. pVec->Add(CVar(VT_BSTR, s));
  203. n = 0;
  204. buf[0] = 0;
  205. }
  206. else if(!isspace(*pc) && *pc != '"')
  207. {
  208. buf[n++] = *pc;
  209. }
  210. pc++;
  211. }
  212. if(n != 0)
  213. {
  214. buf[n] = 0;
  215. WString s = buf;
  216. pVec->Add(CVar(VT_BSTR, s));
  217. n = 0;
  218. }
  219. return pVec;
  220. }
  221. CVarVector* GetVT_BSTRArray(LPSTR pStr)
  222. {
  223. CVarVector *pVec = new CVarVector(VT_BSTR);
  224. char buf[TEMP_BUF];
  225. int n = 0;
  226. BOOL bInString = FALSE;
  227. while (*pStr)
  228. {
  229. if (*pStr == '\"')
  230. {
  231. if (bInString)
  232. {
  233. WString s = buf;
  234. pVec->Add(CVar(VT_BSTR, s));
  235. n = 0;
  236. bInString = FALSE;
  237. pStr++;
  238. }
  239. else {
  240. bInString = TRUE;
  241. pStr++;
  242. n=0;
  243. buf[0] = 0;
  244. }
  245. continue;
  246. }
  247. // Allow for \" escape sequence to include quotes in strings.
  248. // ==========================================================
  249. if (*pStr == '\\' && (*(pStr+1) == '\"' || (*(pStr+1) == '\\'))) {
  250. buf[n] = *(pStr+1);
  251. buf[++n] = 0;
  252. pStr += 2;
  253. continue;
  254. }
  255. if (!bInString)
  256. {
  257. if (isspace(*pStr) || *pStr == ',') {
  258. pStr++;
  259. continue;
  260. }
  261. // Error in array element separators
  262. delete pVec;
  263. return 0;
  264. }
  265. else
  266. {
  267. buf[n] = *pStr++;
  268. buf[++n] = 0;
  269. }
  270. }
  271. if(pVec->Size() < 1)
  272. {
  273. delete pVec;
  274. return 0;
  275. }
  276. return pVec;
  277. }
  278. CVarVector* GetVT_BOOLArray(LPSTR pStr)
  279. {
  280. if(!PrelimCheck(pStr))
  281. return NULL;
  282. CVarVector *pVec = new CVarVector(VT_BOOL);
  283. char buf[TEMP_BUF];
  284. int n = 0;
  285. BOOL bPending = FALSE;
  286. while (*pStr)
  287. {
  288. if (isspace(*pStr))
  289. pStr++;
  290. else if (*pStr == ',')
  291. {
  292. if (wbem_stricmp(buf, "TRUE") == 0 ||
  293. wbem_stricmp(buf, "1") == 0 ||
  294. wbem_stricmp(buf, "-1") == 0)
  295. pVec->Add(CVar((VARIANT_BOOL) -1, VT_BOOL));
  296. else if (wbem_stricmp(buf, "FALSE") == 0 || wbem_stricmp(buf, "0") == 0)
  297. pVec->Add(CVar((VARIANT_BOOL) 0, VT_BOOL));
  298. else
  299. {
  300. delete pVec;
  301. return NULL;
  302. }
  303. pStr++;
  304. bPending = FALSE;
  305. n = 0;
  306. buf[n] = 0;
  307. }
  308. else
  309. {
  310. buf[n] = *pStr++;
  311. buf[++n] = 0;
  312. bPending = TRUE;
  313. }
  314. }
  315. if (bPending)
  316. {
  317. if (wbem_stricmp(buf, "TRUE") == 0 ||
  318. wbem_stricmp(buf, "1") == 0 ||
  319. wbem_stricmp(buf, "-1") == 0)
  320. pVec->Add(CVar((VARIANT_BOOL) -1, VT_BOOL));
  321. else if (wbem_stricmp(buf, "FALSE") == 0 || wbem_stricmp(buf, "0") == 0)
  322. pVec->Add(CVar((VARIANT_BOOL) 0, VT_BOOL));
  323. else
  324. {
  325. delete pVec;
  326. return NULL;
  327. }
  328. }
  329. return pVec;
  330. }
  331. CVarVector* GetVT_I4Array(LPSTR pStr)
  332. {
  333. if(!PrelimCheck(pStr))
  334. return NULL;
  335. CVarVector *pVec = new CVarVector(VT_I4);
  336. char buf[TEMP_BUF];
  337. int n = 0;
  338. BOOL bInNum = FALSE;
  339. while (*pStr)
  340. {
  341. if (wbem_isdigit(*pStr) || *pStr == '-')
  342. {
  343. bInNum = TRUE;
  344. buf[n] = *pStr++;
  345. buf[++n] = 0;
  346. continue;
  347. }
  348. else // Non digit
  349. {
  350. if (bInNum)
  351. {
  352. pVec->Add(CVar((LONG) atol(buf)));
  353. n = 0;
  354. bInNum = FALSE;
  355. pStr++;
  356. }
  357. else // A separator or trash
  358. {
  359. if (*pStr == ',' || isspace(*pStr))
  360. pStr++;
  361. else
  362. {
  363. delete pVec;
  364. return 0;
  365. }
  366. }
  367. }
  368. }
  369. if (bInNum)
  370. pVec->Add(CVar((LONG) atol(buf)));
  371. return pVec;
  372. }
  373. CVarVector* GetVT_I2Array(LPSTR pStr)
  374. {
  375. if(!PrelimCheck(pStr))
  376. return NULL;
  377. CVarVector *pVec = new CVarVector(VT_I2);
  378. char buf[TEMP_BUF];
  379. int n = 0;
  380. BOOL bInNum = FALSE;
  381. while (*pStr)
  382. {
  383. if (wbem_isdigit(*pStr) || *pStr == '-')
  384. {
  385. bInNum = TRUE;
  386. buf[n] = *pStr++;
  387. buf[++n] = 0;
  388. continue;
  389. }
  390. else // Non digit
  391. {
  392. if (bInNum)
  393. {
  394. pVec->Add(CVar((SHORT) atol(buf)));
  395. n = 0;
  396. bInNum = FALSE;
  397. pStr++;
  398. }
  399. else // A separator or trash
  400. {
  401. if (*pStr == ',' || isspace(*pStr))
  402. pStr++;
  403. else
  404. {
  405. delete pVec;
  406. return 0;
  407. }
  408. }
  409. }
  410. }
  411. if (bInNum)
  412. pVec->Add(CVar((SHORT) atol(buf)));
  413. return pVec;
  414. }
  415. CVarVector* GetVT_UI1Array(LPSTR pStr)
  416. {
  417. if(!PrelimCheck(pStr))
  418. return NULL;
  419. CVarVector *pVec = new CVarVector(VT_UI1);
  420. char buf[TEMP_BUF];
  421. int n = 0;
  422. BOOL bPending = FALSE,
  423. fFailedConvert = FALSE;
  424. while ( !fFailedConvert && *pStr )
  425. {
  426. if (isspace(*pStr))
  427. pStr++;
  428. else if (*pStr == ',')
  429. {
  430. BYTE b = 0;
  431. int nRes = sscanf(buf, "'%c'", &b);
  432. if (nRes == 0)
  433. {
  434. int n2 = 0;
  435. nRes = sscanf(buf, "0x%X", &n2);
  436. if (nRes == 0)
  437. {
  438. nRes = sscanf(buf, "%d", &n2);
  439. // check that n is in the byte range
  440. if ( n2 >= 0 && n2 <= 0xFF )
  441. {
  442. b = (BYTE)n2;
  443. }
  444. else
  445. {
  446. fFailedConvert = TRUE;
  447. }
  448. }
  449. else b = (BYTE)n2;
  450. }
  451. if ( !fFailedConvert )
  452. {
  453. pVec->Add(CVar(b));
  454. pStr++;
  455. bPending = FALSE;
  456. n = 0;
  457. buf[n] = 0;
  458. }
  459. }
  460. else
  461. {
  462. buf[n] = *pStr++;
  463. buf[++n] = 0;
  464. bPending = TRUE;
  465. }
  466. }
  467. if ( !fFailedConvert && bPending )
  468. {
  469. BYTE b = 0;
  470. int nRes = sscanf(buf, "'%c'", &b);
  471. if (nRes == 0)
  472. {
  473. int n2 = 0;
  474. nRes = sscanf(buf, "0x%X", &n2);
  475. if (nRes == 0)
  476. {
  477. nRes = sscanf(buf, "%d", &n2);
  478. // check that n is in the byte range
  479. if ( n2 >= 0 && n2 <= 0xFF )
  480. {
  481. b = (BYTE)n2;
  482. }
  483. else
  484. {
  485. fFailedConvert = TRUE;
  486. }
  487. }
  488. else b = (BYTE)n2;
  489. }
  490. // Don't set the value if the conversion failed
  491. if ( !fFailedConvert )
  492. {
  493. pVec->Add(CVar(b));
  494. }
  495. }
  496. // Check that we didn't fail conversion
  497. if ( fFailedConvert )
  498. {
  499. delete pVec;
  500. pVec = NULL;
  501. }
  502. return pVec;
  503. }
  504. CVarVector* GetVT_R4Array(LPSTR pStr)
  505. {
  506. int iNumConv;
  507. if(!PrelimCheck(pStr))
  508. return NULL;
  509. CVarVector *pVec = new CVarVector(VT_R4);
  510. char buf[TEMP_BUF];
  511. int n = 0;
  512. BOOL bInNum = FALSE;
  513. while (*pStr)
  514. {
  515. if (*pStr != ',' && !isspace(*pStr))
  516. {
  517. bInNum = TRUE;
  518. buf[n] = *pStr++;
  519. buf[++n] = 0;
  520. continue;
  521. }
  522. else // Non digit
  523. {
  524. if (bInNum)
  525. {
  526. double d = 0.0;
  527. iNumConv = sscanf(buf, "%lG", &d);
  528. if (!_finite(d) || iNumConv == 0)
  529. {
  530. delete pVec;
  531. return NULL;
  532. }
  533. pVec->Add(CVar((float) d));
  534. n = 0;
  535. bInNum = FALSE;
  536. pStr++;
  537. }
  538. else // A separator or trash
  539. {
  540. if (*pStr == ',' || isspace(*pStr))
  541. pStr++;
  542. else
  543. {
  544. delete pVec;
  545. return 0;
  546. }
  547. }
  548. }
  549. }
  550. if (bInNum)
  551. {
  552. double d = 0.0;
  553. iNumConv = sscanf(buf, "%lG", &d);
  554. if (!_finite(d) || iNumConv == 0)
  555. {
  556. delete pVec;
  557. return NULL;
  558. }
  559. pVec->Add(CVar(float(d)));
  560. }
  561. return pVec;
  562. }
  563. CVarVector* GetVT_R8Array(LPSTR pStr)
  564. {
  565. int iNumConv;
  566. if(!PrelimCheck(pStr))
  567. return NULL;
  568. CVarVector *pVec = new CVarVector(VT_R8);
  569. char buf[TEMP_BUF];
  570. int n = 0;
  571. BOOL bInNum = FALSE;
  572. while (*pStr)
  573. {
  574. if (*pStr != ',' && !isspace(*pStr))
  575. {
  576. bInNum = TRUE;
  577. buf[n] = *pStr++;
  578. buf[++n] = 0;
  579. continue;
  580. }
  581. else // Non digit
  582. {
  583. if (bInNum)
  584. {
  585. double d = 0.0;
  586. iNumConv = sscanf(buf, "%lG", &d);
  587. if (!_finite(d) || iNumConv == 0)
  588. {
  589. delete pVec;
  590. return NULL;
  591. }
  592. pVec->Add(CVar(d));
  593. n = 0;
  594. bInNum = FALSE;
  595. pStr++;
  596. }
  597. else // A separator or trash
  598. {
  599. if (*pStr == ',' || isspace(*pStr))
  600. pStr++;
  601. else
  602. {
  603. delete pVec;
  604. return 0;
  605. }
  606. }
  607. }
  608. }
  609. if (bInNum)
  610. {
  611. double d = 0.0;
  612. iNumConv = sscanf(buf, "%lG", &d);
  613. if (!_finite(d) || iNumConv ==0)
  614. {
  615. delete pVec;
  616. return NULL;
  617. }
  618. pVec->Add(CVar(d));
  619. }
  620. return pVec;
  621. }
  622. // Allocates a new copy which must be deleted.
  623. CVar* StringToValue(LPSTR pString, int nValType)
  624. {
  625. char g;
  626. CVar *pRet = 0;
  627. if (pString == 0)
  628. return 0;
  629. switch (nValType)
  630. {
  631. case CIM_EMPTY:
  632. pRet = new CVar;
  633. pRet->SetAsNull();
  634. break;
  635. case CIM_CHAR16:
  636. {
  637. long l;
  638. if(sscanf(pString, "%d %c", &l, &g) != 1 && g != '(')
  639. return NULL;
  640. pRet = new CVar;
  641. pRet->SetLong(l);
  642. }
  643. break;
  644. case CIM_UINT8:
  645. case CIM_SINT8:
  646. case CIM_SINT16:
  647. case CIM_UINT16:
  648. case CIM_SINT32:
  649. case CIM_UINT32:
  650. {
  651. pRet = new CVar;
  652. UINT uRetVal = CConverter::Convert(pString, nValType, pRet);
  653. if (ERR_NOERROR != uRetVal)
  654. return NULL;
  655. }
  656. break;
  657. case CIM_REAL32:
  658. {
  659. double d;
  660. if(sscanf(pString, "%lG %c", &d, &g) != 1)
  661. return NULL;
  662. if (!_finite(d))
  663. return NULL;
  664. if ((d > 3.4E+38) || (d < 3.4E-38))
  665. return NULL;
  666. pRet = new CVar;
  667. pRet->SetFloat(float(d));
  668. }
  669. break;
  670. case CIM_REAL64:
  671. {
  672. double d;
  673. if(sscanf(pString, "%lG %c", &d, &g) != 1)
  674. return NULL;
  675. if (!_finite(d))
  676. return NULL;
  677. pRet = new CVar;
  678. pRet->SetDouble(d);
  679. }
  680. break;
  681. case CIM_BOOLEAN:
  682. {
  683. pRet = new CVar;
  684. pRet->SetBool(0); // False by default
  685. if (wbem_stricmp(pString, "TRUE") == 0)
  686. pRet->SetBool(-1);
  687. else if (wbem_stricmp(pString, "FALSE") == 0)
  688. pRet->SetBool(0);
  689. else if (atoi(pString) == 1)
  690. pRet->SetBool(-1);
  691. else if (atoi(pString) == 0)
  692. pRet->SetBool(0);
  693. else
  694. return NULL;
  695. }
  696. break;
  697. case CIM_SINT64:
  698. case CIM_UINT64:
  699. case CIM_STRING:
  700. case CIM_DATETIME:
  701. case CIM_REFERENCE:
  702. {
  703. pRet = new CVar;
  704. wchar_t * wbuf = CreateUnicode(pString);
  705. if (wbuf != 0)
  706. {
  707. CBString bsTemp(wbuf);
  708. pRet->SetBSTR(bsTemp.GetString());
  709. delete wbuf;
  710. }
  711. }
  712. break;
  713. // Array types.
  714. // ============
  715. case CIM_SINT64|CIM_FLAG_ARRAY:
  716. case CIM_UINT64|CIM_FLAG_ARRAY:
  717. {
  718. CVarVector *pVec = GetVT_I8Array(pString);
  719. if (!pVec)
  720. return 0;
  721. pRet = new CVar;
  722. pRet->SetVarVector(pVec, TRUE);
  723. }
  724. break;
  725. case CIM_STRING|CIM_FLAG_ARRAY:
  726. case CIM_DATETIME|CIM_FLAG_ARRAY:
  727. case CIM_REFERENCE|CIM_FLAG_ARRAY:
  728. {
  729. CVarVector *pVec = GetVT_BSTRArray(pString);
  730. if (!pVec)
  731. return 0;
  732. pRet = new CVar;
  733. pRet->SetVarVector(pVec, TRUE);
  734. }
  735. break;
  736. case CIM_BOOLEAN|CIM_FLAG_ARRAY:
  737. {
  738. CVarVector *pVec = GetVT_BOOLArray(pString);
  739. if (!pVec)
  740. return 0;
  741. pRet = new CVar;
  742. pRet->SetVarVector(pVec, TRUE);
  743. }
  744. break;
  745. case CIM_UINT8|CIM_FLAG_ARRAY:
  746. case CIM_SINT8|CIM_FLAG_ARRAY:
  747. case CIM_UINT16|CIM_FLAG_ARRAY:
  748. case CIM_SINT16|CIM_FLAG_ARRAY:
  749. case CIM_UINT32|CIM_FLAG_ARRAY:
  750. case CIM_SINT32|CIM_FLAG_ARRAY:
  751. {
  752. CVarVector *pVec = new CVarVector;
  753. UINT uRes = CConverter::Convert(pString, (nValType & ~CIM_FLAG_ARRAY), pVec);
  754. if (ERR_NOERROR != uRes)
  755. return NULL;
  756. pRet = new CVar;
  757. pRet->SetVarVector(pVec, TRUE);
  758. }break;
  759. case CIM_CHAR16|CIM_FLAG_ARRAY:
  760. {
  761. CVarVector *pVec = GetVT_I2Array(pString);
  762. if (!pVec)
  763. return 0;
  764. pRet = new CVar;
  765. pRet->SetVarVector(pVec, TRUE);
  766. }
  767. break;
  768. case CIM_REAL32|CIM_FLAG_ARRAY:
  769. {
  770. CVarVector *pVec = GetVT_R4Array(pString);
  771. if (!pVec)
  772. return 0;
  773. pRet = new CVar;
  774. pRet->SetVarVector(pVec, TRUE);
  775. }
  776. break;
  777. case CIM_REAL64|CIM_FLAG_ARRAY:
  778. {
  779. CVarVector *pVec = GetVT_R8Array(pString);
  780. if (!pVec)
  781. return 0;
  782. pRet = new CVar;
  783. pRet->SetVarVector(pVec, TRUE);
  784. }
  785. break;
  786. }
  787. return pRet;
  788. }
  789. LPSTR ValueToNewString(CVar *pValue, int vt)
  790. {
  791. WString wsOut;
  792. char buf[LARGE_BUF];
  793. int nType = 0;
  794. if(pValue->GetType() == VT_NULL)
  795. {
  796. nType = CIM_EMPTY;
  797. }
  798. else if(vt != 0)
  799. {
  800. nType = vt;
  801. }
  802. else if (pValue->GetType() == VT_EX_CVARVECTOR)
  803. {
  804. nType = pValue->GetVarVector()->GetType();
  805. nType |= CIM_FLAG_ARRAY;
  806. }
  807. else
  808. {
  809. nType = pValue->GetType();
  810. }
  811. buf[0] = 0;
  812. switch (nType)
  813. {
  814. case CIM_EMPTY:
  815. sprintf(buf, "<null>");
  816. break;
  817. case CIM_OBJECT:
  818. if(pValue->GetEmbeddedObject() != NULL)
  819. sprintf(buf, "<embedded object>");
  820. else
  821. buf[0] = 0;
  822. break;
  823. case CIM_BOOLEAN:
  824. {
  825. VARIANT_BOOL b = pValue->GetBool();
  826. if (!b)
  827. sprintf(buf, "FALSE");
  828. else
  829. sprintf(buf, "TRUE");
  830. }
  831. break;
  832. case CIM_UINT8:
  833. {
  834. BYTE b = pValue->GetByte();
  835. sprintf(buf, "%d", (long)b);
  836. }
  837. break;
  838. case CIM_SINT8:
  839. {
  840. signed char b = (signed char)pValue->GetByte();
  841. sprintf(buf, "%d", (long)b);
  842. }
  843. break;
  844. case CIM_SINT16:
  845. case CIM_CHAR16:
  846. {
  847. SHORT i = pValue->GetShort();
  848. sprintf(buf, "%d (0x%04hx)", i, i);
  849. }
  850. break;
  851. case CIM_UINT16:
  852. {
  853. USHORT i = (USHORT)pValue->GetShort();
  854. sprintf(buf, "%d (0x%X)", (long)i, (long)i);
  855. }
  856. break;
  857. case CIM_SINT32:
  858. {
  859. LONG l = pValue->GetLong();
  860. sprintf(buf, "%d (0x%X)", l, l);
  861. }
  862. break;
  863. case CIM_UINT32:
  864. {
  865. ULONG l = (ULONG)pValue->GetLong();
  866. sprintf(buf, "%lu (0x%X)", l, l);
  867. }
  868. break;
  869. case CIM_REAL32:
  870. {
  871. float f = pValue->GetFloat();
  872. sprintf(buf, "%G", f);
  873. }
  874. break;
  875. case CIM_REAL64:
  876. {
  877. double d = pValue->GetDouble();
  878. sprintf(buf, "%G", d);
  879. }
  880. break;
  881. case CIM_SINT64:
  882. case CIM_UINT64:
  883. case CIM_STRING:
  884. case CIM_DATETIME:
  885. case CIM_REFERENCE:
  886. {
  887. LPWSTR pWStr = pValue->GetLPWSTR();
  888. wsOut += pWStr;
  889. *buf = 0;
  890. }
  891. break;
  892. case CIM_SINT64|CIM_FLAG_ARRAY:
  893. case CIM_UINT64|CIM_FLAG_ARRAY:
  894. case CIM_STRING|CIM_FLAG_ARRAY:
  895. case CIM_DATETIME|CIM_FLAG_ARRAY:
  896. case CIM_REFERENCE|CIM_FLAG_ARRAY:
  897. {
  898. CVarVector *pVec = pValue->GetVarVector();
  899. BOOL bFirst = TRUE;
  900. for (int i = 0; i < pVec->Size(); i++)
  901. {
  902. if (!bFirst)
  903. wsOut += L",";
  904. CVar v;
  905. pVec->FillCVarAt( i, v );
  906. LPWSTR pTmp = v.GetLPWSTR();
  907. wsOut += L"\"";
  908. LPWSTR pTmp2 = new WCHAR[ lstrlenW(pTmp)+1000 ];
  909. int nIdx = 0;
  910. // Add '\' before any "'s or '\'s
  911. // ==============================
  912. while(*pTmp) {
  913. if(*pTmp == '\"' || *pTmp == '\\') {
  914. pTmp2[nIdx++] = '\\';
  915. }
  916. pTmp2[nIdx++] = *pTmp++;
  917. }
  918. pTmp2[nIdx] = 0;
  919. wsOut += pTmp2;
  920. wsOut += L"\"";
  921. bFirst = FALSE;
  922. delete[] pTmp2;
  923. }
  924. *buf = 0;
  925. }
  926. break;
  927. case CIM_UINT8|CIM_FLAG_ARRAY:
  928. {
  929. CVarVector *pVec = pValue->GetVarVector();
  930. BOOL bFirst = TRUE;
  931. for (int i = 0; i < pVec->Size(); i++)
  932. {
  933. if (!bFirst)
  934. wsOut += L",";
  935. CVar v;
  936. pVec->FillCVarAt( i, v );
  937. BYTE b = v.GetByte();
  938. wchar_t buf2[128];
  939. swprintf(buf2, L"%d", (long)b);
  940. wsOut += buf2;
  941. bFirst = FALSE;
  942. }
  943. *buf = 0;
  944. }
  945. break;
  946. case CIM_SINT8|CIM_FLAG_ARRAY:
  947. {
  948. CVarVector *pVec = pValue->GetVarVector();
  949. BOOL bFirst = TRUE;
  950. for (int i = 0; i < pVec->Size(); i++)
  951. {
  952. if (!bFirst)
  953. wsOut += L",";
  954. CVar v;
  955. pVec->FillCVarAt( i, v );
  956. signed char b = (signed char)v.GetByte();
  957. wchar_t buf2[128];
  958. swprintf(buf2, L"%d", (long)b);
  959. wsOut += buf2;
  960. bFirst = FALSE;
  961. }
  962. *buf = 0;
  963. }
  964. break;
  965. case CIM_BOOLEAN|CIM_FLAG_ARRAY:
  966. {
  967. CVarVector *pVec = pValue->GetVarVector();
  968. BOOL bFirst = TRUE;
  969. for (int i = 0; i < pVec->Size(); i++)
  970. {
  971. if (!bFirst)
  972. wsOut += L",";
  973. CVar v;
  974. pVec->FillCVarAt( i, v );
  975. VARIANT_BOOL b = v.GetBool();
  976. if (b)
  977. wsOut += L"TRUE";
  978. else
  979. wsOut += L"FALSE";
  980. bFirst = FALSE;
  981. }
  982. *buf = 0;
  983. }
  984. break;
  985. case CIM_SINT16|CIM_FLAG_ARRAY:
  986. case CIM_CHAR16|CIM_FLAG_ARRAY:
  987. {
  988. CVarVector *pVec = pValue->GetVarVector();
  989. BOOL bFirst = TRUE;
  990. for (int i = 0; i < pVec->Size(); i++)
  991. {
  992. if (!bFirst)
  993. wsOut += L",";
  994. CVar v;
  995. pVec->FillCVarAt( i, v );
  996. SHORT Tmp = v.GetShort();
  997. wchar_t buf2[128];
  998. swprintf(buf2, L"%ld", (long)Tmp);
  999. wsOut += buf2;
  1000. bFirst = FALSE;
  1001. }
  1002. *buf = 0;
  1003. }
  1004. break;
  1005. case CIM_UINT16|CIM_FLAG_ARRAY:
  1006. {
  1007. CVarVector *pVec = pValue->GetVarVector();
  1008. BOOL bFirst = TRUE;
  1009. for (int i = 0; i < pVec->Size(); i++)
  1010. {
  1011. if (!bFirst)
  1012. wsOut += L",";
  1013. CVar v;
  1014. pVec->FillCVarAt( i, v );
  1015. USHORT Tmp = (USHORT)v.GetShort();
  1016. wchar_t buf2[128];
  1017. swprintf(buf2, L"%ld", (long)Tmp);
  1018. wsOut += buf2;
  1019. bFirst = FALSE;
  1020. }
  1021. *buf = 0;
  1022. }
  1023. break;
  1024. case CIM_SINT32|CIM_FLAG_ARRAY:
  1025. {
  1026. CVarVector *pVec = pValue->GetVarVector();
  1027. BOOL bFirst = TRUE;
  1028. for (int i = 0; i < pVec->Size(); i++)
  1029. {
  1030. if (!bFirst)
  1031. wsOut += L",";
  1032. CVar v;
  1033. pVec->FillCVarAt( i, v );
  1034. LONG Tmp = v.GetLong();
  1035. wchar_t buf2[128];
  1036. swprintf(buf2, L"%d", Tmp);
  1037. wsOut += buf2;
  1038. bFirst = FALSE;
  1039. }
  1040. *buf = 0;
  1041. }
  1042. break;
  1043. case CIM_UINT32|CIM_FLAG_ARRAY:
  1044. {
  1045. CVarVector *pVec = pValue->GetVarVector();
  1046. BOOL bFirst = TRUE;
  1047. for (int i = 0; i < pVec->Size(); i++)
  1048. {
  1049. if (!bFirst)
  1050. wsOut += L",";
  1051. CVar v;
  1052. pVec->FillCVarAt( i, v );
  1053. ULONG Tmp = (ULONG)v.GetLong();
  1054. wchar_t buf2[128];
  1055. swprintf(buf2, L"%lu", Tmp);
  1056. wsOut += buf2;
  1057. bFirst = FALSE;
  1058. }
  1059. *buf = 0;
  1060. }
  1061. break;
  1062. case CIM_REAL32|CIM_FLAG_ARRAY:
  1063. {
  1064. CVarVector *pVec = pValue->GetVarVector();
  1065. BOOL bFirst = TRUE;
  1066. for (int i = 0; i < pVec->Size(); i++)
  1067. {
  1068. if (!bFirst)
  1069. wsOut += L",";
  1070. CVar v;
  1071. pVec->FillCVarAt( i, v );
  1072. double d = v.GetFloat();
  1073. wchar_t buf2[128];
  1074. swprintf(buf2, L"%G", d);
  1075. wsOut += buf2;
  1076. bFirst = FALSE;
  1077. }
  1078. *buf = 0;
  1079. }
  1080. break;
  1081. case CIM_REAL64|CIM_FLAG_ARRAY:
  1082. {
  1083. CVarVector *pVec = pValue->GetVarVector();
  1084. BOOL bFirst = TRUE;
  1085. for (int i = 0; i < pVec->Size(); i++)
  1086. {
  1087. if (!bFirst)
  1088. wsOut += L",";
  1089. CVar v;
  1090. pVec->FillCVarAt( i, v );
  1091. double d = v.GetDouble();
  1092. wchar_t buf2[128];
  1093. swprintf(buf2, L"%G", d);
  1094. wsOut += buf2;
  1095. bFirst = FALSE;
  1096. }
  1097. *buf = 0;
  1098. }
  1099. break;
  1100. case CIM_OBJECT|CIM_FLAG_ARRAY:
  1101. if(pValue->GetVarVector() != NULL)
  1102. sprintf(buf, "<array of embedded objects>");
  1103. else
  1104. buf[0] = 0;
  1105. break;
  1106. default:
  1107. sprintf(buf, "<error>");
  1108. }
  1109. wsOut += WString(buf);
  1110. return wsOut.GetLPSTR();
  1111. }
  1112. void StripTrailingWs(LPSTR pVal)
  1113. {
  1114. if (!pVal || strlen(pVal) == 0)
  1115. return;
  1116. for (int i = strlen(pVal) - 1; i >= 0; i--)
  1117. if (isspace(pVal[i])) pVal[i] = 0;
  1118. else break;
  1119. }
  1120. void StripTrailingWs(LPWSTR pVal)
  1121. {
  1122. if (!pVal || wcslen(pVal) == 0)
  1123. return;
  1124. for (int i = wcslen(pVal) - 1; i >= 0; i--)
  1125. if (iswspace(pVal[i])) pVal[i] = 0;
  1126. else break;
  1127. }