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.

471 lines
13 KiB

  1. //+------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1993.
  5. //
  6. // File: bmp_stg.cxx
  7. //
  8. // Contents: Generic Storage parser based test
  9. //
  10. // Classes: CStorageParserTest, CStorageParser
  11. //
  12. // Functions:
  13. //
  14. // History: 15-June-94 t-vadims Created
  15. //
  16. //--------------------------------------------------------------------------
  17. #include <headers.cxx>
  18. #pragma hdrstop
  19. #include <valid.h>
  20. #include <tchar.h>
  21. #include <bmp_stg.hxx>
  22. //+------------------------------------------------------------------------
  23. //
  24. // Class: CStorageParserTest
  25. //
  26. // Functions:
  27. //
  28. // History: 15-June-94 t-vadims Created
  29. //
  30. //--------------------------------------------------------------------------
  31. SCODE CStorageParserTest::SetParserObject ()
  32. {
  33. m_pParser = new CStorageParser;
  34. return S_OK;
  35. }
  36. SCODE CStorageParserTest::DeleteParserObject ()
  37. {
  38. delete m_pParser;
  39. return S_OK;
  40. }
  41. TCHAR* CStorageParserTest::Name ()
  42. {
  43. return TEXT("StorageParserTest");
  44. }
  45. TCHAR *CStorageParserTest::SectionHeader()
  46. {
  47. return TEXT("Storage test from log");
  48. }
  49. //+------------------------------------------------------------------------
  50. //
  51. // Class: CStorageParser
  52. //
  53. // Functions:
  54. //
  55. // History: 15-June-94 t-vadims Created
  56. //
  57. //--------------------------------------------------------------------------
  58. #define STATE_OFFSET 10 // location of "In"/"Out" in log string
  59. #define NAME_OFFSET 14 // location of command name in log string
  60. #define MAX_NAME 50
  61. #define DEF_ARRAYSIZE 10
  62. //+-------------------------------------------------------------------
  63. //
  64. // Member: CStorageParser::Setup, public
  65. //
  66. // Synopsis: Makes all neccessary initializations.
  67. //
  68. // History: 16-June-94 t-vadims Created
  69. //
  70. //--------------------------------------------------------------------
  71. SCODE CStorageParser::Setup (CTestInput *pInput)
  72. {
  73. SCODE sc;
  74. sc = CoGetMalloc(MEMCTX_TASK, &m_piMalloc);
  75. if (FAILED(sc))
  76. return sc;
  77. m_iStreamCount = 0;
  78. m_iStreamArraySize = DEF_ARRAYSIZE;
  79. m_aulStreamID = (ULONG *)m_piMalloc->Alloc(m_iStreamArraySize *
  80. sizeof(ULONG));
  81. m_apStreams = (LPSTREAM *)m_piMalloc->Alloc(m_iStreamArraySize *
  82. sizeof(LPSTREAM));
  83. m_iStorageCount = 0;
  84. m_iStorageArraySize = DEF_ARRAYSIZE;
  85. m_aulStorageID = (ULONG *)m_piMalloc->Alloc(m_iStorageArraySize *
  86. sizeof(ULONG));
  87. m_apStorages = (LPSTORAGE *)m_piMalloc->Alloc(m_iStorageArraySize *
  88. sizeof(LPSTORAGE));
  89. m_iInstrCount = 0;
  90. m_iInstrArraySize = DEF_ARRAYSIZE;
  91. m_apInstrData = (LPINSTRDATA *)m_piMalloc->Alloc(m_iInstrArraySize *
  92. sizeof(LPINSTRDATA));
  93. m_bGotFirstPart = FALSE;
  94. if (m_apInstrData == NULL || m_aulStorageID == NULL ||
  95. m_apStorages == NULL || m_aulStreamID == NULL ||
  96. m_apStreams == NULL )
  97. {
  98. Cleanup();
  99. Log(TEXT("Setup can't allocate memory"), E_OUTOFMEMORY);
  100. return E_OUTOFMEMORY;
  101. }
  102. return S_OK;
  103. }
  104. //+-------------------------------------------------------------------
  105. //
  106. // Member: CStorageParser::Cleanup, public
  107. //
  108. // Synopsis: Makes all neccessary cleanup
  109. //
  110. // History: 16-June-94 t-vadims Created
  111. //
  112. //--------------------------------------------------------------------
  113. SCODE CStorageParser::Cleanup ()
  114. {
  115. /* do any neccessary clean up */
  116. if(m_piMalloc)
  117. {
  118. if (m_aulStreamID)
  119. m_piMalloc->Free(m_aulStreamID);
  120. if (m_apStreams)
  121. m_piMalloc->Free(m_apStreams);
  122. if (m_aulStorageID)
  123. m_piMalloc->Free(m_aulStorageID);
  124. if (m_apStorages)
  125. m_piMalloc->Free(m_apStorages);
  126. for (ULONG i = 0; i < m_iInstrCount; i++)
  127. delete m_apInstrData[i];
  128. if (m_apInstrData)
  129. m_piMalloc->Free(m_apInstrData);
  130. m_piMalloc->Release();
  131. m_piMalloc = NULL;
  132. }
  133. return S_OK;
  134. }
  135. //+-------------------------------------------------------------------
  136. //
  137. // Member: CStorageParser::AddInstruction, public
  138. //
  139. // Synopsis: Parse new instruction and add it to array if it is valid
  140. //
  141. // Return: ID of instruction
  142. //
  143. // History: 16-June-94 t-vadims Created
  144. //
  145. //--------------------------------------------------------------------
  146. ULONG CStorageParser::AddInstruction(LPTSTR pszFirstPart, LPTSTR pszSecondPart)
  147. {
  148. ULONG ulInstrID;
  149. SCODE sc;
  150. TCHAR szInstrName[MAX_NAME];
  151. TCHAR szSecondName[MAX_NAME];
  152. if (_tcsncmp(pszFirstPart + STATE_OFFSET, TEXT("In"), 2) != 0)
  153. return INVALID_INSTRUCTION;
  154. if (_tcsncmp(pszSecondPart + STATE_OFFSET, TEXT("Out"), 3) != 0)
  155. return INVALID_INSTRUCTION;
  156. // check if the same instruction name;
  157. GetInstructionName(szInstrName, pszFirstPart);
  158. GetInstructionName(szSecondName, pszSecondPart);
  159. if(_tcscmp(szInstrName, szSecondName) != 0)
  160. return INVALID_INSTRUCTION;
  161. // determine the instruction
  162. ulInstrID = INVALID_INSTRUCTION;
  163. for (ULONG i = 0; i < m_iMaxInstruction; i++)
  164. if (_tcscmp(szInstrName, m_aInstructions[i].szLogName) == 0)
  165. {
  166. ulInstrID = i;
  167. break;
  168. }
  169. if(ulInstrID == INVALID_INSTRUCTION)
  170. return INVALID_INSTRUCTION;
  171. // fill appropriate structure fields
  172. SInstrData *pInstrData = new SInstrData;
  173. pInstrData->ulInstrID = ulInstrID;
  174. sc = (this->*m_aInstructions[ulInstrID].Parse)(pInstrData,
  175. pszFirstPart,
  176. pszSecondPart);
  177. if (FAILED(sc))
  178. {
  179. delete pInstrData;
  180. return INVALID_INSTRUCTION;
  181. }
  182. return AddNewInstrData(pInstrData);
  183. }
  184. //+-------------------------------------------------------------------
  185. //
  186. // Member: CStorageParser::IgnoreInstruction, private
  187. //
  188. // Synopsis: Check if this instruction should be ignored.
  189. //
  190. // History: 16-June-94 t-vadims Created
  191. //
  192. //--------------------------------------------------------------------
  193. BOOL CStorageParser::IgnoreInstruction(LPTSTR pszInstr)
  194. {
  195. TCHAR szName[MAX_NAME];
  196. // We Ignore those instructions that are completely implemented
  197. // in terms of other instructions that are also logged.
  198. GetInstructionName(szName, pszInstr);
  199. if (_tcscmp (szName, TEXT("CExposedStream::QueryInterface")) == 0 ||
  200. _tcscmp (szName, TEXT("CExposedDocFile::QueryInterface")) == 0 ||
  201. _tcscmp (szName, TEXT("CExposedStream::CopyTo")) == 0 ||
  202. _tcscmp (szName, TEXT("CExposedDocFile::MoveElementTo")) == 0 ||
  203. _tcscmp (szName, TEXT("CExposedDocFile::CopyTo")) == 0 ||
  204. _tcscmp (szName, TEXT("ExposedDocFile::CopyTo")) == 0
  205. )
  206. {
  207. return TRUE;
  208. }
  209. return FALSE;
  210. }
  211. //+-------------------------------------------------------------------
  212. //
  213. // Member: CStorageParser::ParseNewInstruction, public
  214. //
  215. // Synopsis: Parse new line of file, and return its id.
  216. //
  217. // History: 16-June-94 t-vadims Created
  218. //
  219. //--------------------------------------------------------------------
  220. ULONG CStorageParser::ParseNewInstruction(LPTSTR pszInstr)
  221. {
  222. ULONG ulID;
  223. if (IgnoreInstruction(pszInstr))
  224. return NOT_INSTRUCTION;
  225. if (m_bGotFirstPart)
  226. {
  227. // out part of instruction. We can now add it.
  228. ulID = AddInstruction(m_szBuffer, pszInstr);
  229. m_bGotFirstPart = FALSE;
  230. }
  231. else
  232. {
  233. // save In part of instruction, and fake CTimerBase into
  234. // thinking that this wasn't an instruction.
  235. _tcscpy(m_szBuffer, pszInstr);
  236. ulID = NOT_INSTRUCTION;
  237. m_bGotFirstPart = TRUE;
  238. }
  239. return ulID;
  240. }
  241. //+-------------------------------------------------------------------
  242. //
  243. // Member: CStorageParser::ExecuteInstruction, public
  244. //
  245. // Synopsis: Execute instruction with given id.
  246. //
  247. // Return: time taken to execute it.
  248. //
  249. // History: 16-June-94 t-vadims Created
  250. //
  251. //--------------------------------------------------------------------
  252. ULONG CStorageParser::ExecuteInstruction(ULONG ulID)
  253. {
  254. SInstrData *pInstrData = m_apInstrData[ulID];
  255. return (this->*m_aInstructions[pInstrData->ulInstrID].Execute)(pInstrData);
  256. }
  257. //+-------------------------------------------------------------------
  258. //
  259. // Member: CStorageParser::InstructionName, public
  260. //
  261. // Synopsis: Return name of instruction.
  262. //
  263. // History: 16-June-94 t-vadims Created
  264. //
  265. //--------------------------------------------------------------------
  266. TCHAR * CStorageParser::InstructionName(ULONG ulID)
  267. {
  268. SInstrData *pInstrData = m_apInstrData[ulID];
  269. if (m_aInstructions[pInstrData->ulInstrID].GetName != NULL)
  270. return (this->*m_aInstructions[pInstrData->ulInstrID].GetName)(pInstrData);
  271. else
  272. return m_aInstructions[pInstrData->ulInstrID].szPrintName;
  273. }
  274. //+-------------------------------------------------------------------
  275. //
  276. // Member: CStorageParser::GetInstructionName, public
  277. //
  278. // Synopsis: Extract instruction name from the instruction.
  279. //
  280. // History: 16-June-94 t-vadims Created
  281. //
  282. //--------------------------------------------------------------------
  283. SCODE CStorageParser::GetInstructionName (LPTSTR pszName, LPTSTR pszInstruction)
  284. {
  285. _stscanf(pszInstruction + NAME_OFFSET, TEXT("%[^(]"), pszName);
  286. return S_OK;
  287. }
  288. //+-------------------------------------------------------------------
  289. //
  290. // Member: CStorageParser::AddNewInstrData, public
  291. //
  292. // Synopsis: Adds new instruction to instrData array.
  293. //
  294. // History: 16-June-94 t-vadims Created
  295. //
  296. //--------------------------------------------------------------------
  297. ULONG CStorageParser::AddNewInstrData (LPINSTRDATA pInstrData)
  298. {
  299. if (m_iInstrCount >= m_iInstrArraySize)
  300. {
  301. m_iInstrArraySize *= 2;
  302. m_apInstrData = (LPINSTRDATA *)m_piMalloc->Realloc(m_apInstrData,
  303. m_iInstrArraySize * sizeof(LPINSTRDATA));
  304. }
  305. m_apInstrData[m_iInstrCount] = pInstrData;
  306. return m_iInstrCount++;
  307. }
  308. //+-------------------------------------------------------------------
  309. //
  310. // Member: CStorageParser::FindStorageID, public
  311. //
  312. // Synopsis: finds or creates new storage, based on ID.
  313. //
  314. // History: 16-June-94 t-vadims Created
  315. //
  316. //--------------------------------------------------------------------
  317. ULONG CStorageParser::FindStorageID (ULONG ulStgID)
  318. {
  319. for (ULONG i = 0; i < m_iStorageCount; i++)
  320. if (m_aulStorageID[i] == ulStgID)
  321. return i;
  322. if (m_iStorageCount >= m_iStorageArraySize)
  323. {
  324. m_iStorageArraySize *= 2;
  325. m_aulStorageID = (ULONG *)m_piMalloc->Realloc(m_aulStorageID,
  326. m_iStorageArraySize * sizeof(ULONG));
  327. m_apStorages = (LPSTORAGE *)m_piMalloc->Realloc(m_apStorages,
  328. m_iStorageArraySize * sizeof(LPSTORAGE));
  329. }
  330. m_aulStorageID[m_iStorageCount] = ulStgID;
  331. m_apStorages[m_iStorageCount] = NULL;
  332. return m_iStorageCount++;
  333. }
  334. //+-------------------------------------------------------------------
  335. //
  336. // Member: CStorageParser::FindStreamID, public
  337. //
  338. // Synopsis: finds or creates new stream, based on ID.
  339. //
  340. // History: 16-June-94 t-vadims Created
  341. //
  342. //--------------------------------------------------------------------
  343. ULONG CStorageParser::FindStreamID (ULONG ulStreamID)
  344. {
  345. for (ULONG i = 0; i < m_iStreamCount; i++)
  346. if (m_aulStreamID[i] == ulStreamID)
  347. return i;
  348. if (m_iStreamCount >= m_iStreamArraySize)
  349. {
  350. m_iStreamArraySize *=2;
  351. m_aulStreamID = (ULONG *)m_piMalloc->Realloc(m_aulStreamID,
  352. m_iStreamArraySize * sizeof(ULONG));
  353. m_apStreams = (LPSTREAM *)m_piMalloc->Realloc(m_apStreams,
  354. m_iStreamArraySize * sizeof(LPSTREAM));
  355. }
  356. m_aulStreamID[m_iStreamCount] = ulStreamID;
  357. m_apStreams[m_iStreamCount] = NULL;
  358. return m_iStreamCount++;
  359. }
  360. //+-------------------------------------------------------------------
  361. //
  362. // Members: CStorageParser::CheckThisStorageID, public
  363. // CStorageParser::CheckThisStreamID, public
  364. //
  365. // Synopsis: Check if storage/stream with given id can be dereferenced.
  366. // (must be non null).
  367. //
  368. // History: 16-June-94 t-vadims Created
  369. //
  370. //--------------------------------------------------------------------
  371. SCODE CStorageParser::CheckThisStorageID(ULONG ulStorageID)
  372. {
  373. if(m_apStorages[ulStorageID] == NULL ||
  374. ! IsValidInterface(m_apStorages[ulStorageID]))
  375. {
  376. Log(TEXT("Trying to dereference an unassigned Storage"), E_FAIL);
  377. return E_FAIL;
  378. }
  379. return S_OK;
  380. }
  381. SCODE CStorageParser::CheckThisStreamID(ULONG ulStreamID)
  382. {
  383. if(m_apStreams[ulStreamID] == NULL ||
  384. ! IsValidInterface(m_apStreams[ulStreamID]))
  385. {
  386. Log(TEXT("Trying to dereference an unassigned Stream"), E_FAIL);
  387. return E_FAIL;
  388. }
  389. return S_OK;
  390. }