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.

505 lines
11 KiB

  1. /*++
  2. Copyright (C) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. TEST.C
  5. Abstract:
  6. Demonstration program for dumping out Binary Managed Object Format (BMOF)
  7. data.
  8. History:
  9. a-davj 14-April-97 Created.
  10. --*/
  11. #include "precomp.h"
  12. #include <stdio.h>
  13. #include <io.h>
  14. #include <fcntl.h>
  15. #include <stdio.h>
  16. #include <sys/stat.h>
  17. #include "bmof.h"
  18. #include "mrcicode.h"
  19. void DisplayObject(CBMOFObj * po);
  20. //***************************************************************************
  21. //
  22. // void * BMOFAlloc
  23. //
  24. // DESCRIPTION:
  25. //
  26. // Provides allocation service for BMOF.C. This allows users to choose
  27. // the allocation method that is used.
  28. //
  29. // PARAMETERS:
  30. //
  31. // Size Input. Size of allocation in bytes.
  32. //
  33. // RETURN VALUE:
  34. //
  35. // pointer to new data. NULL if allocation failed.
  36. //
  37. //***************************************************************************
  38. void * BMOFAlloc(size_t Size)
  39. {
  40. return malloc(Size);
  41. }
  42. //***************************************************************************
  43. //
  44. // void BMOFFree
  45. //
  46. // DESCRIPTION:
  47. //
  48. // Provides allocation service for BMOF.C. This frees what ever was
  49. // allocated via BMOFAlloc.
  50. //
  51. // PARAMETERS:
  52. //
  53. // pointer to memory to be freed.
  54. //
  55. //***************************************************************************
  56. void BMOFFree(void * pFree)
  57. {
  58. free(pFree);
  59. }
  60. //***************************************************************************
  61. //
  62. // void DisplayVariant
  63. //
  64. // DESCRIPTION:
  65. //
  66. // Displays VARIANT data. Note that this is only done in this sample for
  67. // convienence, and using OLE is NOT necessary.
  68. //
  69. // PARAMETERS:
  70. //
  71. // pvar Input. Pointer to VARIANT which is to be displayed.
  72. // It is assumed that the variant type is simple, I.E.
  73. // neither the VT_ARRAY or VT_BYREF bits are set.
  74. //
  75. //***************************************************************************
  76. void DisplayVariant(VARIANT * pvar)
  77. {
  78. SCODE sc;
  79. VARIANT vTemp;
  80. // Uninitialized data will have a VT_NULL type.
  81. if(pvar->vt == VT_NULL)
  82. {
  83. printf(" data is NULL");
  84. return;
  85. }
  86. // String types can just be dumped.
  87. if(pvar->vt == VT_BSTR)
  88. {
  89. printf("value is %S",pvar->bstrVal);
  90. return;
  91. }
  92. else if(pvar->vt == VT_UNKNOWN)
  93. {
  94. CBMOFObj * pObj;
  95. printf(" got an embedded object");
  96. pObj = (CBMOFObj *)pvar->bstrVal;
  97. DisplayObject(pObj);
  98. return;
  99. }
  100. // For non string data, convert the infomation to a bstr and display it.
  101. VariantInit(&vTemp);
  102. sc = VariantChangeTypeEx(&vTemp, pvar,0,0, VT_BSTR);
  103. if(sc == S_OK)
  104. {
  105. printf("value is %S",vTemp.bstrVal);
  106. }
  107. else
  108. printf(" Couldnt convert type 0x%x, error code 0x%x", pvar->vt, sc);
  109. VariantClear(&vTemp);
  110. }
  111. //***************************************************************************
  112. //
  113. // void DisplayData
  114. //
  115. // DESCRIPTION:
  116. //
  117. // Displays the data held in a data item. Note that Ole is use just for
  118. // ease of use and is optional.
  119. //
  120. // PARAMETERS:
  121. //
  122. // pItem Input. Item to be displayed.
  123. //
  124. //***************************************************************************
  125. void DisplayData(CBMOFDataItem * pItem)
  126. {
  127. DWORD dwType, dwSimpleType;
  128. long lNumDim, lCnt;
  129. long lFirstDim;
  130. VARIANT var;
  131. // Determine the data type and clear out the variant
  132. dwType = pItem->m_dwType;
  133. printf("\nData type is 0x%x ", dwType);
  134. dwSimpleType = dwType & ~VT_ARRAY & ~VT_BYREF;
  135. memset((void *)&var.lVal, 0, 8);
  136. lNumDim = GetNumDimensions(pItem);
  137. if(lNumDim == 0)
  138. {
  139. // handle the simple scalar case. Note that uninitialized properties
  140. // will not have data.
  141. if(GetData(pItem, (BYTE *)&(var.lVal), NULL))
  142. {
  143. var.vt = (VARTYPE)dwSimpleType;
  144. DisplayVariant(&var);
  145. // Note the GetData does not use OLE to allocate BSTRs
  146. // and so we need to use our own freeing routine here.
  147. if(var.vt == VT_BSTR)
  148. BMOFFree(var.bstrVal);
  149. }
  150. else
  151. printf(" NULL ");
  152. }
  153. else if(lNumDim == 1)
  154. {
  155. // For the array case, just loop getting each element.
  156. // Start by getting the number of elements
  157. lFirstDim = GetNumElements(pItem, 0);
  158. if(lFirstDim < 1)
  159. {
  160. printf("\n CANT DISPLAY, BOGUS DIMENSION");
  161. return;
  162. }
  163. printf("\n");
  164. for(lCnt = 0; lCnt < lFirstDim; lCnt++)
  165. {
  166. if(GetData(pItem, (BYTE *)&(var.lVal), &lCnt))
  167. {
  168. var.vt = (VARTYPE)dwSimpleType;
  169. DisplayVariant(&var);
  170. // Note the GetData does not use OLE to allocate BSTRs
  171. // and so we need to use our own freeing routine here.
  172. if(var.vt == VT_BSTR)
  173. BMOFFree(var.bstrVal);
  174. printf("\n");
  175. }
  176. else
  177. printf(" NULL ");
  178. }
  179. }
  180. else if(lNumDim == -1)
  181. {
  182. printf("\n Undefined array");
  183. return;
  184. }
  185. else
  186. {
  187. // Currently multidimension arrays are not supported.
  188. printf("\n CANT DISPLAY, TOO MANY DIMEMSIONS");
  189. return;
  190. }
  191. return;
  192. }
  193. //***************************************************************************
  194. //
  195. // void DisplayQualList
  196. //
  197. // DESCRIPTION:
  198. //
  199. // Helper routine for displaying a qualifier list.
  200. //
  201. // PARAMETERS:
  202. //
  203. // pql Input. Pointer to structure which wraps the
  204. // qualifier list.
  205. //
  206. //***************************************************************************
  207. void DisplayQualList(CBMOFQualList * pql)
  208. {
  209. WCHAR * pName = NULL;
  210. CBMOFDataItem Item;
  211. ResetQualList(pql);
  212. printf("\nDisplaying qual list");
  213. while(NextQual(pql, &pName, &Item))
  214. {
  215. printf("\nQualifier name is -%S- ",pName);
  216. DisplayData(&Item);
  217. BMOFFree(pName);
  218. }
  219. }
  220. //***************************************************************************
  221. //
  222. // void DisplayObject
  223. //
  224. // DESCRIPTION:
  225. //
  226. // Helper routine for displaying a class or instance.
  227. //
  228. // PARAMETERS:
  229. //
  230. // po Input. Pointer to structure that wraps the object.
  231. //
  232. //***************************************************************************
  233. void DisplayObject(CBMOFObj * po)
  234. {
  235. CBMOFQualList * pql;
  236. CBMOFDataItem Item;
  237. WCHAR * pName = NULL;
  238. BOOL bfirstmethod = TRUE;
  239. // Display the objects name, its type (Is it a class or instance), and
  240. // display the qualifier set which is attached to the object.
  241. if(GetName(po, &pName))
  242. {
  243. printf("\n\nLooking at object %S",pName);
  244. BMOFFree(pName);
  245. }
  246. printf("\nThe objects type is 0x%x", GetType(po));
  247. pql = GetQualList(po);
  248. if(pql)
  249. {
  250. DisplayQualList(pql);
  251. BMOFFree(pql);
  252. pql = NULL;
  253. }
  254. // Display each property and it associated qualifier list
  255. ResetObj(po);
  256. printf("\nDisplaying prop list");
  257. while(NextProp(po, &pName, &Item))
  258. {
  259. printf("\n\nProperty name is -%S- type is 0x%x",pName, Item.m_dwType);
  260. DisplayData(&Item);
  261. pql = GetPropQualList(po, pName);
  262. if(pql)
  263. {
  264. DisplayQualList(pql);
  265. BMOFFree(pql);
  266. pql = NULL;
  267. }
  268. BMOFFree(pName);
  269. }
  270. while(NextMeth(po, &pName, &Item))
  271. {
  272. if(bfirstmethod)
  273. printf("\nDisplaying method list");
  274. bfirstmethod = FALSE;
  275. printf("\n\nMethod name is -%S- type is 0x%x",pName, Item.m_dwType);
  276. DisplayData(&Item);
  277. pql = GetPropQualList(po, pName);
  278. if(pql)
  279. {
  280. DisplayQualList(pql);
  281. BMOFFree(pql);
  282. pql = NULL;
  283. }
  284. BMOFFree(pName);
  285. }
  286. }
  287. //***************************************************************************
  288. //
  289. // BYTE * ReadBMOFFile
  290. //
  291. // DESCRIPTION:
  292. //
  293. // Opens and decompresses the binary mof file
  294. //
  295. // PARAMETERS:
  296. //
  297. // pFileName Input. Pointer to structure that wraps the object.
  298. //
  299. // RETURN VALUE:
  300. //
  301. // pointer to the binary mof data. This should be freed using "free". Note that
  302. // NULL is returned for all errors.
  303. //
  304. //***************************************************************************
  305. BYTE * ReadBMOFFile(char * pFileName)
  306. {
  307. int fh1 = -1;
  308. int iRet;
  309. DWORD dwCompType, dwCompressedSize, dwExpandedSize, dwSig, dwResSize;
  310. BYTE * pCompressed = NULL;
  311. BYTE * pExpanded = NULL;
  312. fh1 = _open(pFileName, _O_BINARY | _O_RDONLY);
  313. if(fh1 == -1)
  314. {
  315. printf("\nCould not open the file %s", pFileName);
  316. return NULL;
  317. }
  318. // get the signature, compression type, and the sizes
  319. iRet = _read(fh1, &dwSig, sizeof(DWORD));
  320. if((DWORD)iRet != sizeof(DWORD))
  321. {
  322. printf("\nError reading file");
  323. _close(fh1);
  324. return NULL;
  325. }
  326. iRet = _read(fh1, &dwCompType, sizeof(DWORD));
  327. iRet = _read(fh1, &dwCompressedSize, sizeof(DWORD));
  328. iRet = _read(fh1, &dwExpandedSize, sizeof(DWORD));
  329. // make sure the signature is valid and that the compression type is one
  330. // we understand!
  331. if(dwSig != BMOF_SIG ||dwCompType != 1)
  332. {
  333. _close(fh1);
  334. return NULL;
  335. }
  336. // Allocate storage for the compressed data and
  337. // expanded data
  338. pCompressed = malloc(dwCompressedSize);
  339. pExpanded = malloc(dwExpandedSize);
  340. if(pCompressed == NULL || pExpanded == NULL)
  341. {
  342. _close(fh1);
  343. return NULL;
  344. }
  345. // Read the compressed data.
  346. iRet = _read(fh1, pCompressed, dwCompressedSize);
  347. if((DWORD)iRet != dwCompressedSize)
  348. {
  349. printf("\nError reading data");
  350. free(pExpanded);
  351. free(pCompressed);
  352. return NULL;
  353. }
  354. _close(fh1);
  355. // Decompress the data
  356. dwResSize = Mrci1Decompress(pCompressed, dwCompressedSize, pExpanded, dwExpandedSize);
  357. free(pCompressed);
  358. if(dwResSize != dwExpandedSize)
  359. {
  360. printf("\nError expanding data!!!");
  361. free(pExpanded);
  362. return NULL;
  363. }
  364. return pExpanded;
  365. }
  366. //***************************************************************************
  367. //
  368. // int main
  369. //
  370. // DESCRIPTION:
  371. //
  372. // Entry point.
  373. //
  374. // COMMAND LINE ARGUMENT:
  375. //
  376. // This program should be launced with a single argument which is the
  377. // name of the file which contains the BMOF.
  378. //
  379. // RETURN VALUE:
  380. //
  381. // 0 if OK, 1 if error.
  382. //
  383. //***************************************************************************
  384. int main(int argc, char ** argv)
  385. {
  386. BYTE * pTest;
  387. CBMOFObjList * pol;
  388. CBMOFObj * po;
  389. // check the command line
  390. if(argc < 2)
  391. {
  392. printf("\nusage: test BMOFFILE\nwhere BMOFFILE is the binary mof file to dump");
  393. return 1;
  394. }
  395. pTest = ReadBMOFFile(argv[1]);
  396. if(pTest == NULL)
  397. {
  398. printf("\nterminating abnormally, could not read binary mof");
  399. return 1;
  400. }
  401. // Now use the helper functions to dump out the file. Create an object
  402. // list structure and use it to enumerate the objects.
  403. pol = CreateObjList(pTest);
  404. if(pol == NULL)
  405. {
  406. return 1;
  407. }
  408. printf("\nThe number of objects is %d",pol->m_pol->dwNumberOfObjects);
  409. ResetObjList (pol);
  410. while(po = NextObj(pol))
  411. {
  412. DisplayObject(po);
  413. BMOFFree(po);
  414. }
  415. BMOFFree(pol);
  416. free(pTest);
  417. printf("\nTerminating normally\n");
  418. return 0;
  419. }