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.

405 lines
13 KiB

  1. //+--------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1991 - 1996.
  5. //
  6. // File: docfile.c
  7. //
  8. // Contents: DocFile root functions (Stg* functions)
  9. //
  10. //---------------------------------------------------------------
  11. #include "exphead.cxx"
  12. #include "expst.hxx"
  13. #include "h/rexpdf.hxx"
  14. #include "h/docfile.hxx"
  15. #include "ascii.hxx"
  16. #include "logfile.hxx"
  17. #include "h/refilb.hxx"
  18. //+--------------------------------------------------------------
  19. //
  20. // Function: DfFromLB, private
  21. //
  22. // Synopsis: Starts a root Docfile on an ILockBytes
  23. //
  24. // Arguments: [plst] - LStream to start on
  25. // [df] - Permissions
  26. // [dwStartFlags] - Startup flags
  27. // [snbExclude] - Partial instantiation list
  28. // [ppdfExp] - DocFile return
  29. // [pcid] - Class ID return for opens
  30. //
  31. // Returns: Appropriate status code
  32. //
  33. // Modifies: [ppdfExp]
  34. // [pcid]
  35. //
  36. //---------------------------------------------------------------
  37. SCODE DfFromLB(ILockBytes *plst,
  38. DFLAGS df,
  39. DWORD dwStartFlags,
  40. SNBW snbExclude,
  41. CExposedDocFile **ppdfExp,
  42. CLSID *pcid)
  43. {
  44. SCODE sc, scConv;
  45. CRootExposedDocFile *prpdf;
  46. CDFBasis *pdfb=NULL;
  47. UNREFERENCED_PARM(pcid);
  48. olDebugOut((DEB_ITRACE, "In DfFromLB(%p, %X, %lX, %p, %p, %p)\n",
  49. plst, df, dwStartFlags, snbExclude, ppdfExp, pcid));
  50. // If we're not creating or converting, do a quick check
  51. // to make sure that the ILockBytes contains a storage
  52. if ((dwStartFlags & (RSF_CREATEFLAGS | RSF_CONVERT)) == 0)
  53. olHChk(StgIsStorageILockBytes(plst));
  54. if (dwStartFlags & RSF_SECTORSIZE4K)
  55. df |= DF_LARGE;
  56. // Make root
  57. olMem(pdfb = new CDFBasis);
  58. olMemTo(EH_pdfb, prpdf = new CRootExposedDocFile(pdfb));
  59. olChkTo(EH_ppcInit, scConv = prpdf->InitRoot(plst, dwStartFlags, df,
  60. snbExclude));
  61. *ppdfExp = prpdf;
  62. olDebugOut((DEB_ITRACE, "Out DfFromLB => %p\n", SAFE_DREF(ppdfExp)));
  63. return scConv;
  64. EH_ppcInit:
  65. prpdf->Release();
  66. EH_pdfb:
  67. delete pdfb;
  68. EH_Err:
  69. return sc;
  70. }
  71. //+--------------------------------------------------------------
  72. //
  73. // Function: StgCreateDocfile, public
  74. //
  75. // Synopsis: char version
  76. //
  77. //---------------------------------------------------------------
  78. STDAPI StgCreateDocfile(TCHAR const *pszName,
  79. DWORD grfMode,
  80. DWORD reserved,
  81. IStorage **ppstgOpen)
  82. {
  83. SCODE sc;
  84. CFileILB *pilb = NULL;
  85. int i;
  86. olDebugOut((DEB_ITRACE, "In StgCreateDocfile(%p, %lX, %lu, %p)\n",
  87. pszName, grfMode, reserved, ppstgOpen));
  88. olChk(ValidatePtrBuffer(ppstgOpen));
  89. *ppstgOpen = NULL;
  90. olChk(VerifyPerms(grfMode));
  91. if ((grfMode & STGM_RDWR) == STGM_READ ||
  92. (grfMode & (STGM_DELETEONRELEASE | STGM_CONVERT)) ==
  93. (STGM_DELETEONRELEASE | STGM_CONVERT))
  94. olErr(EH_Err, STG_E_INVALIDFLAG);
  95. pilb = new CFileILB(pszName, grfMode, FALSE);
  96. if (!pilb) olErr(EH_Err, STG_E_INSUFFICIENTMEMORY);
  97. olChk( pilb->Create(grfMode) );
  98. if ( (grfMode & (STGM_CREATE|STGM_CONVERT)) == STGM_FAILIFTHERE)
  99. grfMode |= STGM_CREATE; // no file exists, we will 'overwrite' the new
  100. // file
  101. grfMode &= (~STGM_DELETEONRELEASE); // remove the flag
  102. sc=GetScode(StgCreateDocfileOnILockBytes(pilb, grfMode, reserved,
  103. ppstgOpen));
  104. EH_Err:
  105. if (pilb)
  106. {
  107. if (FAILED(sc)) i=pilb->ReleaseOnError();
  108. else i=pilb->Release();
  109. olAssert(SUCCEEDED(sc) ? i==1 : i==0);
  110. }
  111. olDebugOut((DEB_ITRACE, "Out StgCreateDocfile: *ppstgOpen=%p ret=>%l\n",
  112. ppstgOpen?(*ppstgOpen):NULL, sc));
  113. return ResultFromScode(sc);
  114. }
  115. //+--------------------------------------------------------------
  116. //
  117. // Function: StgCreateDocfileOnILockBytes, public
  118. //
  119. // Synopsis: Creates a root Docfile on an lstream
  120. //
  121. // Arguments: [plkbyt] - LStream
  122. // [grfMode] - Permissions
  123. // [reserved] - Unused
  124. // [ppstgOpen] - Docfile return
  125. //
  126. // Returns: Appropriate status code
  127. //
  128. // Modifies: [ppstgOpen]
  129. //
  130. //---------------------------------------------------------------
  131. STDAPI StgCreateDocfileOnILockBytes(ILockBytes *plkbyt,
  132. DWORD grfMode,
  133. DWORD reserved,
  134. IStorage **ppstgOpen)
  135. {
  136. CExposedDocFile *pdfExp;
  137. SCODE sc;
  138. DFLAGS df;
  139. olLog(("--------::In StgCreateDocFileOnILockBytes(%p, %lX, %lu, %p)\n",
  140. plkbyt, grfMode, reserved, ppstgOpen));
  141. olDebugOut((DEB_ITRACE, "In StgCreateDocfileOnILockBytes("
  142. "%p, %lX, %lu, %p)\n",
  143. plkbyt, grfMode, reserved, ppstgOpen));
  144. TRY
  145. {
  146. olChk(ValidatePtrBuffer(ppstgOpen));
  147. *ppstgOpen = NULL;
  148. olChk(ValidateInterface(plkbyt, IID_ILockBytes));
  149. if (reserved != 0)
  150. olErr(EH_Err, STG_E_INVALIDPARAMETER);
  151. if ((grfMode & (STGM_CREATE | STGM_CONVERT)) == 0)
  152. olErr(EH_Err, STG_E_FILEALREADYEXISTS);
  153. olChk(VerifyPerms(grfMode));
  154. if (grfMode & STGM_DELETEONRELEASE)
  155. olErr(EH_Err, STG_E_INVALIDFUNCTION);
  156. df = ModeToDFlags(grfMode);
  157. if ((grfMode & (STGM_TRANSACTED | STGM_CONVERT)) ==
  158. (STGM_TRANSACTED | STGM_CONVERT))
  159. df |= DF_INDEPENDENT;
  160. olChkTo(EH_Truncate,
  161. sc = DfFromLB(plkbyt, df,
  162. RSF_CREATE |
  163. ((grfMode & STGM_CREATE) ? RSF_TRUNCATE : 0) |
  164. ((grfMode & STGM_CONVERT) ? RSF_CONVERT : 0),
  165. NULL, &pdfExp, NULL));
  166. *ppstgOpen = pdfExp;
  167. }
  168. CATCH(CException, e)
  169. {
  170. sc = e.GetErrorCode();
  171. }
  172. END_CATCH
  173. olDebugOut((DEB_ITRACE, "Out StgCreateDocfileOnILockBytes => %p\n",
  174. ppstgOpen?(*ppstgOpen):NULL));
  175. EH_Err:
  176. olLog(("--------::Out StgCreateDocFileOnILockBytes(). *ppstgOpen == %p, ret == %lx\n",
  177. *ppstgOpen, sc));
  178. FreeLogFile();
  179. return ResultFromScode(sc);
  180. EH_Truncate:
  181. if ((grfMode & STGM_CREATE) && (grfMode & STGM_TRANSACTED) == 0)
  182. {
  183. ULARGE_INTEGER ulSize;
  184. ULISet32(ulSize, 0);
  185. olHChk(plkbyt->SetSize(ulSize));
  186. }
  187. goto EH_Err;
  188. }
  189. //+--------------------------------------------------------------
  190. //
  191. // Function: DfOpenStorageOnILockBytes, public
  192. //
  193. // Synopsis: Instantiates a root Docfile from an LStream,
  194. // converting if necessary
  195. //
  196. // Arguments: [plkbyt] - Source LStream
  197. // [pstgPriority] - For priority reopens
  198. // [grfMode] - Permissions
  199. // [snbExclude] - For priority reopens
  200. // [reserved]
  201. // [ppstgOpen] - Docfile return
  202. // [pcid] - Class ID return
  203. //
  204. // Returns: Appropriate status code
  205. //
  206. // Modifies: [ppstgOpen]
  207. // [pcid]
  208. //
  209. //---------------------------------------------------------------
  210. HRESULT DfOpenStorageOnILockBytesW(ILockBytes *plkbyt,
  211. IStorage *pstgPriority,
  212. DWORD grfMode,
  213. SNBW snbExclude,
  214. DWORD reserved,
  215. IStorage **ppstgOpen,
  216. CLSID *pcid)
  217. {
  218. SCODE sc;
  219. CExposedDocFile *pdfExp;
  220. olLog(("--------::In DfOpenStorageOnILockBytes("
  221. "%p, %p, %lX, %p, %lu, %p, %p)\n",
  222. plkbyt, pstgPriority, grfMode, snbExclude, reserved, ppstgOpen,
  223. pcid));
  224. olDebugOut((DEB_ITRACE, "In DfOpenStorageOnILockBytes("
  225. "%p, %p, %lX, %p, %lu, %p, %p)\n", plkbyt, pstgPriority,
  226. grfMode, snbExclude, reserved, ppstgOpen, pcid));
  227. TRY
  228. {
  229. #ifdef _UNICODE // do checking if there is an ANSI layer
  230. olChk(ValidatePtrBuffer(ppstgOpen));
  231. *ppstgOpen = NULL;
  232. if (snbExclude) olChk(ValidateSNBW(snbExclude));
  233. #endif /!UNICODE
  234. olChk(ValidateInterface(plkbyt, IID_ILockBytes));
  235. if (pstgPriority)
  236. olChk(ValidateInterface(pstgPriority, IID_IStorage));
  237. olChk(VerifyPerms(grfMode));
  238. if (grfMode & STGM_DELETEONRELEASE)
  239. olErr(EH_Err, STG_E_INVALIDFUNCTION);
  240. if (snbExclude)
  241. {
  242. if ((grfMode & STGM_RDWR) != STGM_READWRITE)
  243. olErr(EH_Err, STG_E_ACCESSDENIED);
  244. }
  245. if (reserved != 0)
  246. olErr(EH_Err, STG_E_INVALIDPARAMETER);
  247. if (FAILED(DllIsMultiStream(plkbyt)))
  248. olErr(EH_Err, STG_E_FILEALREADYEXISTS); //file is not storage obj
  249. if (pstgPriority)
  250. olChk(pstgPriority->Release());
  251. olChk(DfFromLB(plkbyt, ModeToDFlags(grfMode), RSF_OPEN, snbExclude,
  252. &pdfExp, pcid));
  253. *ppstgOpen = pdfExp;
  254. }
  255. CATCH(CException, e)
  256. {
  257. sc = e.GetErrorCode();
  258. }
  259. END_CATCH
  260. olDebugOut((DEB_ITRACE, "Out DfOpenStorageOnILockBytes => %p\n",
  261. ppstgOpen?(*ppstgOpen):NULL));
  262. EH_Err:
  263. olLog(("--------::Out DfOpenStorageOnILockBytes(). *ppstgOpen == %p"
  264. ", ret == %lx\n", *ppstgOpen, sc));
  265. FreeLogFile();
  266. return sc;
  267. }
  268. //+---------------------------------------------------------------------------
  269. //
  270. // Function: StgCreateStorageEx, public
  271. //
  272. // Synopsis: Creates a storage or stream object
  273. //
  274. // Arguments: [pwcsName] - pathname of file
  275. // [grfMode] - open mode flags
  276. // [stgfmt] - storage format
  277. // [grfAttrs] - reserved
  278. // [pSecurity] - reserved
  279. // [pTransaction] - reserved
  280. // [riid] - GUID of interface pointer to return
  281. // [ppObjectOpen] - interface pointer to return
  282. //
  283. // Returns: Appropriate status code
  284. //
  285. //----------------------------------------------------------------------------
  286. STDAPI StgCreateStorageEx (const WCHAR* pwcsName,
  287. DWORD grfMode,
  288. DWORD stgfmt, // enum
  289. DWORD grfAttrs, // reserved
  290. STGOPTIONS * pStgOptions,
  291. void * reserved,
  292. REFIID riid,
  293. void ** ppObjectOpen)
  294. {
  295. CExposedDocFile *pdfExp;
  296. CFileILB *pilb = NULL;
  297. SCODE sc = S_OK;
  298. DFLAGS df;
  299. BOOL fLarge = FALSE;
  300. olDebugOut((DEB_TRACE, "In StgCreateStorageEx(%ws, %p, %p, %p, %p)\n",
  301. pwcsName, grfMode, stgfmt, riid, ppObjectOpen));
  302. olChk(ValidatePtrBuffer(ppObjectOpen));
  303. *ppObjectOpen = NULL;
  304. if (reserved != NULL)
  305. olErr (EH_Err, STG_E_INVALIDPARAMETER);
  306. if (grfAttrs != 0)
  307. olErr (EH_Err, STG_E_INVALIDPARAMETER);
  308. olChk(VerifyPerms(grfMode));
  309. if (riid != IID_IStorage)
  310. olErr (EH_Err, STG_E_INVALIDPARAMETER);
  311. if (pStgOptions != NULL)
  312. {
  313. if (pStgOptions->usVersion > STGOPTIONS_VERSION ||
  314. pStgOptions->usVersion == 0 ||
  315. pStgOptions->reserved != 0 ||
  316. (pStgOptions->ulSectorSize != 512 &&
  317. pStgOptions->ulSectorSize != 4096) )
  318. {
  319. olErr (EH_Err, STG_E_INVALIDPARAMETER);
  320. }
  321. if (pStgOptions->ulSectorSize == 4096)
  322. fLarge = TRUE;
  323. }
  324. if (stgfmt != STGFMT_DOCFILE)
  325. olErr (EH_Err, STG_E_INVALIDPARAMETER);
  326. olChk(VerifyPerms(grfMode));
  327. if ((grfMode & STGM_RDWR) == STGM_READ ||
  328. (grfMode & (STGM_DELETEONRELEASE | STGM_CONVERT)) ==
  329. (STGM_DELETEONRELEASE | STGM_CONVERT))
  330. olErr(EH_Err, STG_E_INVALIDFLAG);
  331. pilb = new CFileILB(pwcsName, grfMode, FALSE);
  332. if (!pilb) olErr(EH_Err, STG_E_INSUFFICIENTMEMORY);
  333. olChk( pilb->Create(grfMode) );
  334. grfMode &= (~STGM_DELETEONRELEASE); // remove the flag
  335. df = ModeToDFlags(grfMode);
  336. olChk(DfFromLB(pilb, df, RSF_CREATE |
  337. ((grfMode & STGM_CREATE) ? RSF_TRUNCATE : 0) |
  338. ((grfMode & STGM_CONVERT) ? RSF_CONVERT : 0) |
  339. (fLarge ? RSF_SECTORSIZE4K : 0),
  340. NULL, &pdfExp, NULL));
  341. *ppObjectOpen = pdfExp;
  342. EH_Err:
  343. if (pilb)
  344. {
  345. int i;
  346. if (FAILED(sc)) i=pilb->ReleaseOnError();
  347. else i=pilb->Release();
  348. olAssert(SUCCEEDED(sc) ? i==1 : i==0);
  349. }
  350. return sc;
  351. }