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.

456 lines
11 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1999 - 1999
  5. //
  6. // File: mscparser.cpp
  7. //
  8. // Contents: Implementation of the code to upgrade legacy (MMC1.0, MMC1.1 and
  9. // MMC1.2) .msc files to the new XML format
  10. //
  11. // History: 04-Aug-99 VivekJ Created
  12. //
  13. //--------------------------------------------------------------------------
  14. #include <stdafx.h>
  15. #include "strtable.h"
  16. #include "stgio.h"
  17. #include "comdbg.h"
  18. #include "mmcdata.h"
  19. #include "mscparser.h"
  20. /*+-------------------------------------------------------------------------*
  21. *
  22. * CConsoleFile::ScUpgrade
  23. *
  24. * PURPOSE:
  25. *
  26. * PARAMETERS:
  27. * LPCTSTR lpszPathName :
  28. *
  29. * RETURNS:
  30. * SC
  31. *
  32. *+-------------------------------------------------------------------------*/
  33. SC
  34. CConsoleFile::ScUpgrade(LPCTSTR lpszPathName)
  35. {
  36. SC sc;
  37. IStoragePtr spStorage;
  38. TCHAR szTempFile[MAX_PATH];
  39. DWORD dwRet = 0;
  40. USES_CONVERSION;
  41. // short circuit
  42. return sc;
  43. ASSERT(lpszPathName != NULL && *lpszPathName != 0);
  44. if (lpszPathName == NULL || *lpszPathName == 0)
  45. {
  46. sc = ScFromMMC(IDS_UnableToOpenDocumentMessage);
  47. goto Error;
  48. }
  49. // Open the specified file
  50. sc = OpenDebugStorage(T2OLE((LPTSTR)lpszPathName), STGM_READ|STGM_SHARE_DENY_WRITE, &spStorage);
  51. if(sc.IsError() || spStorage==NULL)
  52. {
  53. sc = ScFromMMC(IDS_UnableToOpenDocumentMessage);
  54. goto Error;
  55. }
  56. // get the console file's version
  57. sc = ScGetFileVersion(spStorage);
  58. if(sc)
  59. goto Error;
  60. // Load the string table.
  61. sc = ScLoadStringTable(spStorage);
  62. if(sc)
  63. goto Error;
  64. // Load column settings.
  65. sc = ScLoadColumnSettings(spStorage);
  66. if(sc)
  67. goto Error;
  68. // load the view settings
  69. sc = ScLoadViewSettings(spStorage);
  70. if(sc)
  71. goto Error;
  72. // load the tree
  73. sc = ScLoadTree(spStorage);
  74. if(sc)
  75. goto Error;
  76. // load the favorites
  77. sc = ScLoadFavorites(spStorage);
  78. if(sc)
  79. goto Error;
  80. // load custom data (including the icon)
  81. sc = ScLoadCustomData(spStorage);
  82. if(sc)
  83. goto Error;
  84. // The LoadAppMode, LoadViews and LoadFrame should be called in that order
  85. // load the app mode
  86. sc = ScLoadAppMode(spStorage);
  87. if(sc)
  88. goto Error;
  89. // load the views
  90. sc = ScLoadViews(spStorage);
  91. if(sc)
  92. goto Error;
  93. // load the frame
  94. sc = ScLoadFrame(spStorage);
  95. if(sc)
  96. goto Error;
  97. Cleanup:
  98. return sc;
  99. Error:
  100. //TraceError(TEXT("CConsoleFile::ScUpgrade"), sc);
  101. goto Cleanup;
  102. }
  103. /*+-------------------------------------------------------------------------*
  104. *
  105. * CConsoleFile::ScGetFileVersion
  106. *
  107. * PURPOSE:
  108. *
  109. * PARAMETERS:
  110. * IStorage* pstgRoot :
  111. *
  112. * RETURNS:
  113. * SC
  114. *
  115. *+-------------------------------------------------------------------------*/
  116. SC
  117. CConsoleFile::ScGetFileVersion(IStorage* pstgRoot)
  118. {
  119. static const wchar_t* AMCSignatureStreamName = L"signature";
  120. static const long double dOldVersion10 = 0.00000015; // MMC version 1.0
  121. static const long double dOldVersion11 = 1.1; // MMC version 1.1
  122. static const BYTE byStreamVersionMagic = 0xFF;
  123. SC sc;
  124. ConsoleFileVersion eFileVer = FileVer_0100;
  125. IStreamPtr spStream;
  126. int nVersion = 0;
  127. IStoragePtr spStorage;
  128. ASSERT (sizeof(eFileVer) == sizeof(int));
  129. ASSERT(pstgRoot != NULL);
  130. // check for a valid pointer
  131. if (pstgRoot == NULL)
  132. {
  133. sc = ScFromMMC(IDS_INVALIDFILE); // TODO: add this IDS.
  134. goto Error;
  135. }
  136. // Open the stream containing the signature
  137. sc = OpenDebugStream(pstgRoot, AMCSignatureStreamName, STGM_SHARE_EXCLUSIVE | STGM_READ, L"\\signature", &spStream);
  138. if(sc.IsError() || spStream==NULL)
  139. {
  140. sc = ScFromMMC(IDS_UnableToOpenDocumentMessage);
  141. goto Error;
  142. }
  143. // read the signature (stream extraction operators will throw
  144. // _com_error's, so we need an exception block here)
  145. try
  146. {
  147. // MMC v1.2 and later write a marker as the first
  148. // byte of the signature stream.
  149. BYTE byMagic;
  150. *spStream >> byMagic;
  151. // if this file was written by v1.2,
  152. // read the console file version (int)
  153. if (byMagic == byStreamVersionMagic)
  154. {
  155. *spStream >> nVersion;
  156. ASSERT (nVersion == FileVer_0120);
  157. }
  158. // Otherwise, the file was written by v1.0 or v1.1.
  159. // Back up to re-read the marker byte, and read the old-style
  160. // file version (long double), then map it to a new-style version
  161. else
  162. {
  163. LARGE_INTEGER pos = {0, 0};
  164. spStream->Seek (pos, STREAM_SEEK_SET, NULL);
  165. long double dVersion;
  166. *spStream >> dVersion;
  167. // v1.1?
  168. if (dVersion == dOldVersion11)
  169. nVersion = FileVer_0110;
  170. // v1.0?
  171. else if (dVersion == dOldVersion10)
  172. {
  173. // If we got a v1.0 signature, we still may have a v1.1 file.
  174. // There was a period of time where MMC v1.1 wrote a v1.0
  175. // signature, but the file format had in fact changed. We
  176. // can determine this by checking the \FrameData stream in
  177. // the file. If the first DWORD in the \FrameData stream is
  178. // sizeof(WINDOWPLACEMENT), we have a true v1.0 file, otherwise
  179. // it's a funky v1.1 file.
  180. IStreamPtr spFrameDataStm;
  181. sc = OpenDebugStream (pstgRoot, L"FrameData", STGM_SHARE_EXCLUSIVE | STGM_READ,
  182. &spFrameDataStm);
  183. if(sc)
  184. goto Error;
  185. DWORD dw;
  186. *spFrameDataStm >> dw;
  187. if (dw == sizeof (WINDOWPLACEMENT))
  188. nVersion = FileVer_0100;
  189. else
  190. nVersion = FileVer_0110;
  191. }
  192. // unexpected version
  193. else
  194. {
  195. ASSERT (false && "Unexpected old-style signature");
  196. sc = E_UNEXPECTED;
  197. goto Error;
  198. }
  199. }
  200. }
  201. catch (_com_error& err)
  202. {
  203. sc = err.Error();
  204. goto Error;
  205. }
  206. // make sure the version number is valid.
  207. if(IsValidFileVersion(eFileVer))
  208. {
  209. sc = ScFromMMC(IDS_InvalidVersion); // TODO: add this IDS
  210. goto Error;
  211. }
  212. Cleanup:
  213. return sc;
  214. Error:
  215. TraceError(TEXT("CConsoleFile::ScGetFileVersion"), sc);
  216. goto Cleanup;
  217. }
  218. /*+-------------------------------------------------------------------------*
  219. *
  220. * CConsoleFile::ScLoadStringTable
  221. *
  222. * PURPOSE: Reads in the string table for an .msc file.
  223. *
  224. * PARAMETERS:
  225. * IStorage* pstgRoot :
  226. *
  227. * RETURNS:
  228. * SC
  229. *
  230. *+-------------------------------------------------------------------------*/
  231. SC
  232. CConsoleFile::ScLoadStringTable(IStorage* pstgRoot)
  233. {
  234. SC sc;
  235. static const wchar_t* AMCStringTableStorageName = L"String Table";
  236. /*
  237. * open the string table storage
  238. */
  239. IStoragePtr spStringTableStg;
  240. sc = OpenDebugStorage (pstgRoot, AMCStringTableStorageName,
  241. STGM_SHARE_EXCLUSIVE | STGM_READ,
  242. &spStringTableStg);
  243. /*
  244. * If there's no string table, things are OK. We allow this so
  245. * we can continue to open older console files.
  246. */
  247. if (sc == SC(STG_E_FILENOTFOUND) )
  248. return (true);
  249. if(sc)
  250. goto Error;
  251. /*
  252. * read the string table from the storage
  253. */
  254. try
  255. {
  256. *spStringTableStg >> *m_pStringTable;
  257. }
  258. catch (_com_error& err)
  259. {
  260. sc = err.Error();
  261. ASSERT (false && "Caught _com_error");
  262. goto Error;
  263. }
  264. Cleanup:
  265. return sc;
  266. Error:
  267. TraceError(TEXT("CConsoleFile::ScLoadStringTable"), sc);
  268. goto Cleanup;
  269. }
  270. SC
  271. CConsoleFile::ScLoadFrame(IStorage* pstgRoot)
  272. {
  273. SC sc;
  274. return sc;
  275. }
  276. SC
  277. CConsoleFile::ScLoadViews(IStorage* pstgRoot)
  278. {
  279. SC sc;
  280. return sc;
  281. }
  282. SC
  283. CConsoleFile::ScLoadAppMode(IStorage* pstgRoot)
  284. {
  285. SC sc;
  286. return sc;
  287. }
  288. /*+-------------------------------------------------------------------------*
  289. *
  290. * CConsoleFile::ScLoadColumnSettings
  291. *
  292. * PURPOSE:
  293. *
  294. * PARAMETERS:
  295. * IStorage* pstgRoot :
  296. *
  297. * RETURNS:
  298. * SC
  299. *
  300. *+-------------------------------------------------------------------------*/
  301. SC
  302. CConsoleFile::ScLoadColumnSettings(IStorage* pstgRoot)
  303. {
  304. static const wchar_t* AMCColumnDataStreamName = L"ColumnData";
  305. SC sc;
  306. IPersistStreamPtr spPersistStreamColumnData; // TODO: create this object!
  307. IStreamPtr spStream;
  308. sc = OpenDebugStream (pstgRoot, AMCColumnDataStreamName,
  309. STGM_SHARE_EXCLUSIVE | STGM_READ, &spStream);
  310. if (sc)
  311. goto Error;
  312. if (NULL == spPersistStreamColumnData)
  313. {
  314. sc = E_POINTER;
  315. goto Error;
  316. }
  317. sc = spPersistStreamColumnData->Load(spStream); // $CHANGE to use Load(spColumnData, spStream).
  318. if(sc)
  319. {
  320. sc = ScFromMMC(IDS_UnableToOpenDocumentMessage);
  321. goto Error;
  322. }
  323. Cleanup:
  324. return sc;
  325. Error:
  326. TraceError(TEXT("CConsoleFile::ScLoadColumnSettings"), sc);
  327. goto Cleanup;
  328. }
  329. /*+-------------------------------------------------------------------------*
  330. *
  331. * CConsoleFile::ScLoadViewSettings
  332. *
  333. * PURPOSE:
  334. *
  335. * PARAMETERS:
  336. * IStorage* pstgRoot :
  337. *
  338. * RETURNS:
  339. * SC
  340. *
  341. *+-------------------------------------------------------------------------*/
  342. SC
  343. CConsoleFile::ScLoadViewSettings(IStorage* pstgRoot)
  344. {
  345. static const wchar_t* AMCViewSettingDataStreamName = L"ViewSettingData"; // View settings data stream
  346. SC sc;
  347. IPersistStreamPtr spPersistStreamViewSettingData; // TODO: create this object!
  348. IStreamPtr spStream;
  349. sc = OpenDebugStream (pstgRoot, AMCViewSettingDataStreamName,
  350. STGM_SHARE_EXCLUSIVE | STGM_READ, &spStream);
  351. if (sc)
  352. goto Error;
  353. if (NULL == spPersistStreamViewSettingData)
  354. {
  355. sc = E_POINTER;
  356. goto Error;
  357. }
  358. sc = spPersistStreamViewSettingData->Load(spStream); // $CHANGE to use Load(spPersistStreamViewSettingData, spStream).
  359. if(sc)
  360. {
  361. sc = ScFromMMC(IDS_UnableToOpenDocumentMessage);
  362. goto Error;
  363. }
  364. Cleanup:
  365. return sc;
  366. Error:
  367. TraceError(TEXT("CConsoleFile::ScLoadViewSettings"), sc);
  368. goto Cleanup;
  369. }
  370. SC
  371. CConsoleFile::ScLoadTree(IStorage* pstgRoot)
  372. {
  373. SC sc;
  374. return sc;
  375. }
  376. SC
  377. CConsoleFile::ScLoadFavorites(IStorage* pstgRoot)
  378. {
  379. SC sc;
  380. return sc;
  381. }
  382. SC
  383. CConsoleFile::ScLoadCustomData(IStorage* pstgRoot)
  384. {
  385. SC sc;
  386. return sc;
  387. }