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.

201 lines
5.9 KiB

  1. //+--------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1992.
  5. //
  6. // File: storage.cxx
  7. //
  8. // Contents: Contains generic storage APIs
  9. //
  10. //---------------------------------------------------------------
  11. #include "h/ref.hxx"
  12. #include "h/dfexcept.hxx"
  13. #include "h/msf.hxx"
  14. #include "h/docfile.hxx"
  15. #include "h/storagep.h"
  16. #include "h/refilb.hxx"
  17. #include "h/ole.hxx"
  18. #include "h/funcs.hxx"
  19. #ifdef _MSC_VER
  20. #pragma warning (disable:4514) // unreferenced inline function
  21. #endif
  22. #define MAKE_ERR(c) MAKE_SCODE(SEVERITY_ERROR, FACILITY_STORAGE, c)
  23. //+--------------------------------------------------------------
  24. //
  25. // Function: StgOpenStorage, public
  26. //
  27. // Synopsis: Instantiates a root storage from a file
  28. // by binding to the appropriate implementation
  29. // and starting things up
  30. //
  31. // Arguments: [pwcsName] - Name
  32. // [pstgPriority] - Priority mode reopen IStorage
  33. // [grfMode] - Permissions
  34. // [snbExclude] - Exclusions for priority reopen
  35. // [reserved]
  36. // [ppstgOpen] - Docfile return
  37. //
  38. // Returns: Appropriate status code
  39. //
  40. // Modifies: [ppstgOpen]
  41. //
  42. //---------------------------------------------------------------
  43. STDAPI StgOpenStorage(TCHAR const *pwcsName,
  44. IStorage *pstgPriority,
  45. DWORD grfMode,
  46. SNB snbExclude,
  47. DWORD reserved,
  48. IStorage **ppstgOpen)
  49. {
  50. SCODE sc;
  51. int i;
  52. if (!pwcsName)
  53. return ResultFromScode(STG_E_INVALIDNAME);
  54. CFileILB *pilb = new CFileILB(pwcsName, grfMode, FALSE);
  55. olDebugOut((DEB_TRACE, "In StgOpenStorage(%p, %p, %lX, %p, %lx, %p)\n",
  56. pwcsName, pstgPriority, grfMode, snbExclude,
  57. reserved, ppstgOpen));
  58. if (!pilb) return ResultFromScode(STG_E_INSUFFICIENTMEMORY);
  59. olChk(VerifyPerms(grfMode));
  60. olChk(pilb->Open(grfMode));
  61. sc = GetScode( StgOpenStorageOnILockBytes(
  62. pilb, pstgPriority, grfMode, snbExclude, reserved, ppstgOpen) );
  63. EH_Err:
  64. if (FAILED(sc)) i=pilb->ReleaseOnError();
  65. else i=pilb->Release();
  66. olAssert(SUCCEEDED(sc) ? i==1 : i==0);
  67. olDebugOut((DEB_TRACE,"Out StgOpenStorage: ppstgOpen=%p ret=> %lX\n",
  68. (ppstgOpen)?(*ppstgOpen) : NULL, sc));
  69. return ResultFromScode(sc);
  70. }
  71. //+--------------------------------------------------------------
  72. //
  73. // Function: StgOpenStorageOnILockBytes, public
  74. //
  75. // Synopsis: Instantiates a root storage from an ILockBytes
  76. // by binding to the appropriate implementation
  77. // and starting things up
  78. //
  79. // Arguments: [plkbyt] - Source ILockBytes
  80. // [pstgPriority] - For priority reopens
  81. // [grfMode] - Permissions
  82. // [snbExclude] - For priority reopens
  83. // [reserved]
  84. // [ppstgOpen] - Docfile return
  85. //
  86. // Returns: Appropriate status code
  87. //
  88. // Modifies: [ppstgOpen]
  89. //
  90. //---------------------------------------------------------------
  91. STDAPI StgOpenStorageOnILockBytes(ILockBytes *plkbyt,
  92. IStorage *pstgPriority,
  93. DWORD grfMode,
  94. SNB snbExclude,
  95. DWORD reserved,
  96. IStorage **ppstgOpen)
  97. {
  98. CLSID cid;
  99. #ifndef _UNICODE
  100. return DfOpenStorageOnILockBytes(plkbyt, pstgPriority, grfMode,
  101. snbExclude, reserved, ppstgOpen,
  102. &cid);
  103. #else
  104. return DfOpenStorageOnILockBytesW(plkbyt, pstgPriority, grfMode,
  105. snbExclude, reserved, ppstgOpen,
  106. &cid);
  107. #endif
  108. }
  109. //+--------------------------------------------------------------
  110. //
  111. // Function: StgIsStorageFile, public
  112. //
  113. // Synopsis: Determines whether the named file is a storage or not
  114. //
  115. // Arguments: [pwcsName] - Filename
  116. //
  117. // Returns: S_OK, S_FALSE or error codes
  118. //
  119. //---------------------------------------------------------------
  120. //Identifier for first bytes of Beta 2 Docfiles
  121. const BYTE SIGSTG_B2[] = {0x0e, 0x11, 0xfc, 0x0d, 0xd0, 0xcf, 0x11, 0xe0};
  122. const BYTE CBSIGSTG_B2 = sizeof(SIGSTG_B2);
  123. STDAPI StgIsStorageFile(TCHAR const *pwcsName)
  124. {
  125. SCODE sc;
  126. int i=0;
  127. CFileILB* pilb = NULL;
  128. olChk(ValidateNameW(pwcsName, CBMAXPATHCOMPLEN));
  129. olMem(pilb = new CFileILB(pwcsName, (DWORD)NULL, FALSE));
  130. olChk(pilb->Open(STGM_READ));
  131. sc = GetScode( StgIsStorageILockBytes(pilb) );
  132. EH_Err:
  133. if (pilb != NULL)
  134. i=pilb->Release();
  135. olAssert(i==0); // should not have any more reference to it
  136. olDebugOut((DEB_TRACE, "Out StgIsStorageFile: ret=> %lx\n", sc));
  137. return ResultFromScode(sc);
  138. }
  139. //+--------------------------------------------------------------
  140. //
  141. // Function: StgIsStorageILockBytes, public
  142. //
  143. // Synopsis: Determines whether the ILockBytes is a storage or not
  144. //
  145. // Arguments: [plkbyt] - The ILockBytes
  146. //
  147. // Returns: S_OK, S_FALSE or error codes
  148. //
  149. //---------------------------------------------------------------
  150. STDAPI StgIsStorageILockBytes(ILockBytes *plkbyt)
  151. {
  152. HRESULT hr;
  153. SCODE sc;
  154. SStorageFile stgfile;
  155. ULONG cbRead;
  156. ULARGE_INTEGER ulOffset;
  157. TRY
  158. {
  159. if (FAILED(sc = ValidateInterface(plkbyt, IID_ILockBytes)))
  160. return ResultFromScode(sc);
  161. ULISet32(ulOffset, 0);
  162. hr = plkbyt->ReadAt(ulOffset, &stgfile, sizeof(stgfile), &cbRead);
  163. if (FAILED(GetScode(hr)))
  164. return hr;
  165. }
  166. CATCH(CException, e)
  167. {
  168. return ResultFromScode(e.GetErrorCode());
  169. }
  170. END_CATCH
  171. if (cbRead == sizeof(stgfile))
  172. {
  173. if ((memcmp((void *)stgfile.abSig, SIGSTG, CBSIGSTG) == 0) ||
  174. (memcmp((void *)stgfile.abSig, SIGSTG_B2, CBSIGSTG_B2) == 0))
  175. return ResultFromScode(S_OK);
  176. }
  177. return ResultFromScode(S_FALSE);
  178. }