2 * Compound Storage (32 bit version)
3 * Storage implementation
5 * This file contains the compound file implementation
6 * of the storage interface.
8 * Copyright 1999 Francis Beaudet
9 * Copyright 1999 Sylvain St-Germain
10 * Copyright 1999 Thuy Nguyen
11 * Copyright 2005 Mike McCormack
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 * The compound file implementation of IStorage used for create
29 * and manage substorages and streams within a storage object
30 * residing in a compound file object.
40 #define NONAMELESSUNION
41 #define NONAMELESSSTRUCT
47 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 #include "storage32.h"
51 #include "ole2.h" /* For Write/ReadClassStm */
54 #include "wine/wingdi16.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(storage
);
58 /* Used for OleConvertIStorageToOLESTREAM and OleConvertOLESTREAMToIStorage */
59 #define OLESTREAM_ID 0x501
60 #define OLESTREAM_MAX_STR_LEN 255
63 * These are signatures to detect the type of Document file.
65 static const BYTE STORAGE_magic
[8] ={0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1};
66 static const BYTE STORAGE_oldmagic
[8] ={0xd0,0xcf,0x11,0xe0,0x0e,0x11,0xfc,0x0d};
68 static const char rootPropertyName
[] = "Root Entry";
70 /****************************************************************************
71 * Storage32InternalImpl definitions.
73 * Definition of the implementation structure for the IStorage32 interface.
74 * This one implements the IStorage32 interface for storage that are
75 * inside another storage.
77 struct StorageInternalImpl
79 struct StorageBaseImpl base
;
81 * There is no specific data for this class.
84 typedef struct StorageInternalImpl StorageInternalImpl
;
86 /* Method definitions for the Storage32InternalImpl class. */
87 static StorageInternalImpl
* StorageInternalImpl_Construct(StorageImpl
* ancestorStorage
,
88 DWORD openFlags
, ULONG rootTropertyIndex
);
89 static void StorageImpl_Destroy(StorageBaseImpl
* iface
);
90 static BOOL
StorageImpl_ReadBigBlock(StorageImpl
* This
, ULONG blockIndex
, void* buffer
);
91 static BOOL
StorageImpl_WriteBigBlock(StorageImpl
* This
, ULONG blockIndex
, const void* buffer
);
92 static void StorageImpl_SetNextBlockInChain(StorageImpl
* This
, ULONG blockIndex
, ULONG nextBlock
);
93 static HRESULT
StorageImpl_LoadFileHeader(StorageImpl
* This
);
94 static void StorageImpl_SaveFileHeader(StorageImpl
* This
);
96 static void Storage32Impl_AddBlockDepot(StorageImpl
* This
, ULONG blockIndex
);
97 static ULONG
Storage32Impl_AddExtBlockDepot(StorageImpl
* This
);
98 static ULONG
Storage32Impl_GetNextExtendedBlock(StorageImpl
* This
, ULONG blockIndex
);
99 static ULONG
Storage32Impl_GetExtDepotBlock(StorageImpl
* This
, ULONG depotIndex
);
100 static void Storage32Impl_SetExtDepotBlock(StorageImpl
* This
, ULONG depotIndex
, ULONG blockIndex
);
102 static ULONG
BlockChainStream_GetHeadOfChain(BlockChainStream
* This
);
103 static ULARGE_INTEGER
BlockChainStream_GetSize(BlockChainStream
* This
);
104 static ULONG
BlockChainStream_GetCount(BlockChainStream
* This
);
106 static ULARGE_INTEGER
SmallBlockChainStream_GetSize(SmallBlockChainStream
* This
);
107 static ULONG
SmallBlockChainStream_GetHeadOfChain(SmallBlockChainStream
* This
);
108 static BOOL
StorageImpl_WriteDWordToBigBlock( StorageImpl
* This
,
109 ULONG blockIndex
, ULONG offset
, DWORD value
);
110 static BOOL
StorageImpl_ReadDWordFromBigBlock( StorageImpl
* This
,
111 ULONG blockIndex
, ULONG offset
, DWORD
* value
);
113 /* OLESTREAM memory structure to use for Get and Put Routines */
114 /* Used for OleConvertIStorageToOLESTREAM and OleConvertOLESTREAMToIStorage */
119 DWORD dwOleTypeNameLength
;
120 CHAR strOleTypeName
[OLESTREAM_MAX_STR_LEN
];
121 CHAR
*pstrOleObjFileName
;
122 DWORD dwOleObjFileNameLength
;
123 DWORD dwMetaFileWidth
;
124 DWORD dwMetaFileHeight
;
125 CHAR strUnknown
[8]; /* don't know what this 8 byte information in OLE stream is. */
128 }OLECONVERT_OLESTREAM_DATA
;
130 /* CompObj Stream structure */
131 /* Used for OleConvertIStorageToOLESTREAM and OleConvertOLESTREAMToIStorage */
136 DWORD dwCLSIDNameLength
;
137 CHAR strCLSIDName
[OLESTREAM_MAX_STR_LEN
];
138 DWORD dwOleTypeNameLength
;
139 CHAR strOleTypeName
[OLESTREAM_MAX_STR_LEN
];
140 DWORD dwProgIDNameLength
;
141 CHAR strProgIDName
[OLESTREAM_MAX_STR_LEN
];
143 }OLECONVERT_ISTORAGE_COMPOBJ
;
146 /* Ole Presentation Stream structure */
147 /* Used for OleConvertIStorageToOLESTREAM and OleConvertOLESTREAMToIStorage */
155 }OLECONVERT_ISTORAGE_OLEPRES
;
159 /***********************************************************************
160 * Forward declaration of internal functions used by the method DestroyElement
162 static HRESULT
deleteStorageProperty(
163 StorageImpl
*parentStorage
,
164 ULONG foundPropertyIndexToDelete
,
165 StgProperty propertyToDelete
);
167 static HRESULT
deleteStreamProperty(
168 StorageImpl
*parentStorage
,
169 ULONG foundPropertyIndexToDelete
,
170 StgProperty propertyToDelete
);
172 static HRESULT
findPlaceholder(
173 StorageImpl
*storage
,
174 ULONG propertyIndexToStore
,
175 ULONG storagePropertyIndex
,
178 static HRESULT
adjustPropertyChain(
180 StgProperty propertyToDelete
,
181 StgProperty parentProperty
,
182 ULONG parentPropertyId
,
185 /***********************************************************************
186 * Declaration of the functions used to manipulate StgProperty
189 static ULONG
getFreeProperty(
190 StorageImpl
*storage
);
192 static void updatePropertyChain(
193 StorageImpl
*storage
,
194 ULONG newPropertyIndex
,
195 StgProperty newProperty
);
197 static LONG
propertyNameCmp(
198 const OLECHAR
*newProperty
,
199 const OLECHAR
*currentProperty
);
202 /***********************************************************************
203 * Declaration of miscellaneous functions...
205 static HRESULT
validateSTGM(DWORD stgmValue
);
207 static DWORD
GetShareModeFromSTGM(DWORD stgm
);
208 static DWORD
GetAccessModeFromSTGM(DWORD stgm
);
209 static DWORD
GetCreationModeFromSTGM(DWORD stgm
);
211 extern const IPropertySetStorageVtbl IPropertySetStorage_Vtbl
;
214 /****************************************************************************
215 * IEnumSTATSTGImpl definitions.
217 * Definition of the implementation structure for the IEnumSTATSTGImpl interface.
218 * This class allows iterating through the content of a storage and to find
219 * specific items inside it.
221 struct IEnumSTATSTGImpl
223 const IEnumSTATSTGVtbl
*lpVtbl
; /* Needs to be the first item in the struct
224 * since we want to cast this in an IEnumSTATSTG pointer */
226 LONG ref
; /* Reference count */
227 StorageImpl
* parentStorage
; /* Reference to the parent storage */
228 ULONG firstPropertyNode
; /* Index of the root of the storage to enumerate */
231 * The current implementation of the IEnumSTATSTGImpl class uses a stack
232 * to walk the property sets to get the content of a storage. This stack
233 * is implemented by the following 3 data members
239 #define ENUMSTATSGT_SIZE_INCREMENT 10
243 static IEnumSTATSTGImpl
* IEnumSTATSTGImpl_Construct(StorageImpl
* This
, ULONG firstPropertyNode
);
244 static void IEnumSTATSTGImpl_Destroy(IEnumSTATSTGImpl
* This
);
245 static void IEnumSTATSTGImpl_PushSearchNode(IEnumSTATSTGImpl
* This
, ULONG nodeToPush
);
246 static ULONG
IEnumSTATSTGImpl_PopSearchNode(IEnumSTATSTGImpl
* This
, BOOL remove
);
247 static ULONG
IEnumSTATSTGImpl_FindProperty(IEnumSTATSTGImpl
* This
, const OLECHAR
* lpszPropName
,
248 StgProperty
* buffer
);
249 static INT
IEnumSTATSTGImpl_FindParentProperty(IEnumSTATSTGImpl
*This
, ULONG childProperty
,
250 StgProperty
*currentProperty
, ULONG
*propertyId
);
252 /************************************************************************
256 static ULONG
BLOCK_GetBigBlockOffset(ULONG index
)
258 if (index
== 0xffffffff)
263 return index
* BIG_BLOCK_SIZE
;
266 /************************************************************************
267 ** Storage32BaseImpl implementation
269 static HRESULT
StorageImpl_ReadAt(StorageImpl
* This
,
270 ULARGE_INTEGER offset
,
275 return BIGBLOCKFILE_ReadAt(This
->bigBlockFile
,offset
,buffer
,size
,bytesRead
);
278 static HRESULT
StorageImpl_WriteAt(StorageImpl
* This
,
279 ULARGE_INTEGER offset
,
284 return BIGBLOCKFILE_WriteAt(This
->bigBlockFile
,offset
,buffer
,size
,bytesWritten
);
287 /************************************************************************
288 * Storage32BaseImpl_QueryInterface (IUnknown)
290 * This method implements the common QueryInterface for all IStorage32
291 * implementations contained in this file.
293 * See Windows documentation for more details on IUnknown methods.
295 static HRESULT WINAPI
StorageBaseImpl_QueryInterface(
300 StorageBaseImpl
*This
= (StorageBaseImpl
*)iface
;
302 * Perform a sanity check on the parameters.
304 if ( (This
==0) || (ppvObject
==0) )
308 * Initialize the return parameter.
313 * Compare the riid with the interface IDs implemented by this object.
315 if (IsEqualGUID(&IID_IUnknown
, riid
) ||
316 IsEqualGUID(&IID_IStorage
, riid
))
320 else if (IsEqualGUID(&IID_IPropertySetStorage
, riid
))
322 *ppvObject
= &This
->pssVtbl
;
326 * Check that we obtained an interface.
329 return E_NOINTERFACE
;
332 * Query Interface always increases the reference count by one when it is
335 IStorage_AddRef(iface
);
340 /************************************************************************
341 * Storage32BaseImpl_AddRef (IUnknown)
343 * This method implements the common AddRef for all IStorage32
344 * implementations contained in this file.
346 * See Windows documentation for more details on IUnknown methods.
348 static ULONG WINAPI
StorageBaseImpl_AddRef(
351 StorageBaseImpl
*This
= (StorageBaseImpl
*)iface
;
352 ULONG ref
= InterlockedIncrement(&This
->ref
);
354 TRACE("(%p) AddRef to %d\n", This
, ref
);
359 /************************************************************************
360 * Storage32BaseImpl_Release (IUnknown)
362 * This method implements the common Release for all IStorage32
363 * implementations contained in this file.
365 * See Windows documentation for more details on IUnknown methods.
367 static ULONG WINAPI
StorageBaseImpl_Release(
370 StorageBaseImpl
*This
= (StorageBaseImpl
*)iface
;
372 * Decrease the reference count on this object.
374 ULONG ref
= InterlockedDecrement(&This
->ref
);
376 TRACE("(%p) ReleaseRef to %d\n", This
, ref
);
379 * If the reference count goes down to 0, perform suicide.
384 * Since we are using a system of base-classes, we want to call the
385 * destructor of the appropriate derived class. To do this, we are
386 * using virtual functions to implement the destructor.
388 This
->v_destructor(This
);
394 /************************************************************************
395 * Storage32BaseImpl_OpenStream (IStorage)
397 * This method will open the specified stream object from the current storage.
399 * See Windows documentation for more details on IStorage methods.
401 static HRESULT WINAPI
StorageBaseImpl_OpenStream(
403 const OLECHAR
* pwcsName
, /* [string][in] */
404 void* reserved1
, /* [unique][in] */
405 DWORD grfMode
, /* [in] */
406 DWORD reserved2
, /* [in] */
407 IStream
** ppstm
) /* [out] */
409 StorageBaseImpl
*This
= (StorageBaseImpl
*)iface
;
410 IEnumSTATSTGImpl
* propertyEnumeration
;
411 StgStreamImpl
* newStream
;
412 StgProperty currentProperty
;
413 ULONG foundPropertyIndex
;
414 HRESULT res
= STG_E_UNKNOWN
;
416 TRACE("(%p, %s, %p, %x, %d, %p)\n",
417 iface
, debugstr_w(pwcsName
), reserved1
, grfMode
, reserved2
, ppstm
);
420 * Perform a sanity check on the parameters.
422 if ( (pwcsName
==NULL
) || (ppstm
==0) )
429 * Initialize the out parameter
434 * Validate the STGM flags
436 if ( FAILED( validateSTGM(grfMode
) ) ||
437 STGM_SHARE_MODE(grfMode
) != STGM_SHARE_EXCLUSIVE
)
439 res
= STG_E_INVALIDFLAG
;
446 if ( (grfMode
& STGM_DELETEONRELEASE
) || (grfMode
& STGM_TRANSACTED
) )
448 res
= STG_E_INVALIDFUNCTION
;
453 * Check that we're compatible with the parent's storage mode, but
454 * only if we are not in transacted mode
456 if(!(This
->ancestorStorage
->base
.openFlags
& STGM_TRANSACTED
)) {
457 if ( STGM_ACCESS_MODE( grfMode
) > STGM_ACCESS_MODE( This
->openFlags
) )
459 res
= STG_E_ACCESSDENIED
;
465 * Create a property enumeration to search the properties
467 propertyEnumeration
= IEnumSTATSTGImpl_Construct(
468 This
->ancestorStorage
,
469 This
->rootPropertySetIndex
);
472 * Search the enumeration for the property with the given name
474 foundPropertyIndex
= IEnumSTATSTGImpl_FindProperty(
480 * Delete the property enumeration since we don't need it anymore
482 IEnumSTATSTGImpl_Destroy(propertyEnumeration
);
485 * If it was found, construct the stream object and return a pointer to it.
487 if ( (foundPropertyIndex
!=PROPERTY_NULL
) &&
488 (currentProperty
.propertyType
==PROPTYPE_STREAM
) )
490 newStream
= StgStreamImpl_Construct(This
, grfMode
, foundPropertyIndex
);
494 newStream
->grfMode
= grfMode
;
495 *ppstm
= (IStream
*)newStream
;
498 * Since we are returning a pointer to the interface, we have to
499 * nail down the reference.
501 IStream_AddRef(*ppstm
);
511 res
= STG_E_FILENOTFOUND
;
515 TRACE("<-- IStream %p\n", *ppstm
);
516 TRACE("<-- %08x\n", res
);
520 /************************************************************************
521 * Storage32BaseImpl_OpenStorage (IStorage)
523 * This method will open a new storage object from the current storage.
525 * See Windows documentation for more details on IStorage methods.
527 static HRESULT WINAPI
StorageBaseImpl_OpenStorage(
529 const OLECHAR
* pwcsName
, /* [string][unique][in] */
530 IStorage
* pstgPriority
, /* [unique][in] */
531 DWORD grfMode
, /* [in] */
532 SNB snbExclude
, /* [unique][in] */
533 DWORD reserved
, /* [in] */
534 IStorage
** ppstg
) /* [out] */
536 StorageBaseImpl
*This
= (StorageBaseImpl
*)iface
;
537 StorageInternalImpl
* newStorage
;
538 IEnumSTATSTGImpl
* propertyEnumeration
;
539 StgProperty currentProperty
;
540 ULONG foundPropertyIndex
;
541 HRESULT res
= STG_E_UNKNOWN
;
543 TRACE("(%p, %s, %p, %x, %p, %d, %p)\n",
544 iface
, debugstr_w(pwcsName
), pstgPriority
,
545 grfMode
, snbExclude
, reserved
, ppstg
);
548 * Perform a sanity check on the parameters.
550 if ( (This
==0) || (pwcsName
==NULL
) || (ppstg
==0) )
557 if (snbExclude
!= NULL
)
559 res
= STG_E_INVALIDPARAMETER
;
564 * Validate the STGM flags
566 if ( FAILED( validateSTGM(grfMode
) ))
568 res
= STG_E_INVALIDFLAG
;
575 if ( STGM_SHARE_MODE(grfMode
) != STGM_SHARE_EXCLUSIVE
||
576 (grfMode
& STGM_DELETEONRELEASE
) ||
577 (grfMode
& STGM_PRIORITY
) )
579 res
= STG_E_INVALIDFUNCTION
;
584 * Check that we're compatible with the parent's storage mode,
585 * but only if we are not transacted
587 if(!(This
->ancestorStorage
->base
.openFlags
& STGM_TRANSACTED
)) {
588 if ( STGM_ACCESS_MODE( grfMode
) > STGM_ACCESS_MODE( This
->openFlags
) )
590 res
= STG_E_ACCESSDENIED
;
596 * Initialize the out parameter
601 * Create a property enumeration to search the properties
603 propertyEnumeration
= IEnumSTATSTGImpl_Construct(
604 This
->ancestorStorage
,
605 This
->rootPropertySetIndex
);
608 * Search the enumeration for the property with the given name
610 foundPropertyIndex
= IEnumSTATSTGImpl_FindProperty(
616 * Delete the property enumeration since we don't need it anymore
618 IEnumSTATSTGImpl_Destroy(propertyEnumeration
);
621 * If it was found, construct the stream object and return a pointer to it.
623 if ( (foundPropertyIndex
!=PROPERTY_NULL
) &&
624 (currentProperty
.propertyType
==PROPTYPE_STORAGE
) )
627 * Construct a new Storage object
629 newStorage
= StorageInternalImpl_Construct(
630 This
->ancestorStorage
,
636 *ppstg
= (IStorage
*)newStorage
;
639 * Since we are returning a pointer to the interface,
640 * we have to nail down the reference.
642 StorageBaseImpl_AddRef(*ppstg
);
648 res
= STG_E_INSUFFICIENTMEMORY
;
652 res
= STG_E_FILENOTFOUND
;
655 TRACE("<-- %08x\n", res
);
659 /************************************************************************
660 * Storage32BaseImpl_EnumElements (IStorage)
662 * This method will create an enumerator object that can be used to
663 * retrieve information about all the properties in the storage object.
665 * See Windows documentation for more details on IStorage methods.
667 static HRESULT WINAPI
StorageBaseImpl_EnumElements(
669 DWORD reserved1
, /* [in] */
670 void* reserved2
, /* [size_is][unique][in] */
671 DWORD reserved3
, /* [in] */
672 IEnumSTATSTG
** ppenum
) /* [out] */
674 StorageBaseImpl
*This
= (StorageBaseImpl
*)iface
;
675 IEnumSTATSTGImpl
* newEnum
;
677 TRACE("(%p, %d, %p, %d, %p)\n",
678 iface
, reserved1
, reserved2
, reserved3
, ppenum
);
681 * Perform a sanity check on the parameters.
683 if ( (This
==0) || (ppenum
==0))
687 * Construct the enumerator.
689 newEnum
= IEnumSTATSTGImpl_Construct(
690 This
->ancestorStorage
,
691 This
->rootPropertySetIndex
);
695 *ppenum
= (IEnumSTATSTG
*)newEnum
;
698 * Don't forget to nail down a reference to the new object before
701 IEnumSTATSTG_AddRef(*ppenum
);
706 return E_OUTOFMEMORY
;
709 /************************************************************************
710 * Storage32BaseImpl_Stat (IStorage)
712 * This method will retrieve information about this storage object.
714 * See Windows documentation for more details on IStorage methods.
716 static HRESULT WINAPI
StorageBaseImpl_Stat(
718 STATSTG
* pstatstg
, /* [out] */
719 DWORD grfStatFlag
) /* [in] */
721 StorageBaseImpl
*This
= (StorageBaseImpl
*)iface
;
722 StgProperty curProperty
;
724 HRESULT res
= STG_E_UNKNOWN
;
726 TRACE("(%p, %p, %x)\n",
727 iface
, pstatstg
, grfStatFlag
);
730 * Perform a sanity check on the parameters.
732 if ( (This
==0) || (pstatstg
==0))
739 * Read the information from the property.
741 readSuccessful
= StorageImpl_ReadProperty(
742 This
->ancestorStorage
,
743 This
->rootPropertySetIndex
,
748 StorageUtl_CopyPropertyToSTATSTG(
753 pstatstg
->grfMode
= This
->openFlags
;
754 pstatstg
->grfStateBits
= This
->stateBits
;
765 TRACE("<-- STATSTG: pwcsName: %s, type: %d, cbSize.Low/High: %d/%d, grfMode: %08x, grfLocksSupported: %d, grfStateBits: %08x\n", debugstr_w(pstatstg
->pwcsName
), pstatstg
->type
, pstatstg
->cbSize
.u
.LowPart
, pstatstg
->cbSize
.u
.HighPart
, pstatstg
->grfMode
, pstatstg
->grfLocksSupported
, pstatstg
->grfStateBits
);
767 TRACE("<-- %08x\n", res
);
771 /************************************************************************
772 * Storage32BaseImpl_RenameElement (IStorage)
774 * This method will rename the specified element.
776 * See Windows documentation for more details on IStorage methods.
778 * Implementation notes: The method used to rename consists of creating a clone
779 * of the deleted StgProperty object setting it with the new name and to
780 * perform a DestroyElement of the old StgProperty.
782 static HRESULT WINAPI
StorageBaseImpl_RenameElement(
784 const OLECHAR
* pwcsOldName
, /* [in] */
785 const OLECHAR
* pwcsNewName
) /* [in] */
787 StorageBaseImpl
*This
= (StorageBaseImpl
*)iface
;
788 IEnumSTATSTGImpl
* propertyEnumeration
;
789 StgProperty currentProperty
;
790 ULONG foundPropertyIndex
;
792 TRACE("(%p, %s, %s)\n",
793 iface
, debugstr_w(pwcsOldName
), debugstr_w(pwcsNewName
));
796 * Create a property enumeration to search the properties
798 propertyEnumeration
= IEnumSTATSTGImpl_Construct(This
->ancestorStorage
,
799 This
->rootPropertySetIndex
);
802 * Search the enumeration for the new property name
804 foundPropertyIndex
= IEnumSTATSTGImpl_FindProperty(propertyEnumeration
,
808 if (foundPropertyIndex
!= PROPERTY_NULL
)
811 * There is already a property with the new name
813 IEnumSTATSTGImpl_Destroy(propertyEnumeration
);
814 return STG_E_FILEALREADYEXISTS
;
817 IEnumSTATSTG_Reset((IEnumSTATSTG
*)propertyEnumeration
);
820 * Search the enumeration for the old property name
822 foundPropertyIndex
= IEnumSTATSTGImpl_FindProperty(propertyEnumeration
,
827 * Delete the property enumeration since we don't need it anymore
829 IEnumSTATSTGImpl_Destroy(propertyEnumeration
);
831 if (foundPropertyIndex
!= PROPERTY_NULL
)
833 StgProperty renamedProperty
;
834 ULONG renamedPropertyIndex
;
837 * Setup a new property for the renamed property
839 renamedProperty
.sizeOfNameString
=
840 ( lstrlenW(pwcsNewName
)+1 ) * sizeof(WCHAR
);
842 if (renamedProperty
.sizeOfNameString
> PROPERTY_NAME_BUFFER_LEN
)
843 return STG_E_INVALIDNAME
;
845 strcpyW(renamedProperty
.name
, pwcsNewName
);
847 renamedProperty
.propertyType
= currentProperty
.propertyType
;
848 renamedProperty
.startingBlock
= currentProperty
.startingBlock
;
849 renamedProperty
.size
.u
.LowPart
= currentProperty
.size
.u
.LowPart
;
850 renamedProperty
.size
.u
.HighPart
= currentProperty
.size
.u
.HighPart
;
852 renamedProperty
.previousProperty
= PROPERTY_NULL
;
853 renamedProperty
.nextProperty
= PROPERTY_NULL
;
856 * Bring the dirProperty link in case it is a storage and in which
857 * case the renamed storage elements don't require to be reorganized.
859 renamedProperty
.dirProperty
= currentProperty
.dirProperty
;
861 /* call CoFileTime to get the current time
862 renamedProperty.timeStampS1
863 renamedProperty.timeStampD1
864 renamedProperty.timeStampS2
865 renamedProperty.timeStampD2
866 renamedProperty.propertyUniqueID
870 * Obtain a free property in the property chain
872 renamedPropertyIndex
= getFreeProperty(This
->ancestorStorage
);
875 * Save the new property into the new property spot
877 StorageImpl_WriteProperty(
878 This
->ancestorStorage
,
879 renamedPropertyIndex
,
883 * Find a spot in the property chain for our newly created property.
887 renamedPropertyIndex
,
891 * At this point the renamed property has been inserted in the tree,
892 * now, before Destroying the old property we must zero its dirProperty
893 * otherwise the DestroyProperty below will zap it all and we do not want
895 * Also, we fake that the old property is a storage so the DestroyProperty
896 * will not do a SetSize(0) on the stream data.
898 * This means that we need to tweak the StgProperty if it is a stream or a
901 StorageImpl_ReadProperty(This
->ancestorStorage
,
905 currentProperty
.dirProperty
= PROPERTY_NULL
;
906 currentProperty
.propertyType
= PROPTYPE_STORAGE
;
907 StorageImpl_WriteProperty(
908 This
->ancestorStorage
,
913 * Invoke Destroy to get rid of the ole property and automatically redo
914 * the linking of its previous and next members...
916 IStorage_DestroyElement(iface
, pwcsOldName
);
922 * There is no property with the old name
924 return STG_E_FILENOTFOUND
;
930 /************************************************************************
931 * Storage32BaseImpl_CreateStream (IStorage)
933 * This method will create a stream object within this storage
935 * See Windows documentation for more details on IStorage methods.
937 static HRESULT WINAPI
StorageBaseImpl_CreateStream(
939 const OLECHAR
* pwcsName
, /* [string][in] */
940 DWORD grfMode
, /* [in] */
941 DWORD reserved1
, /* [in] */
942 DWORD reserved2
, /* [in] */
943 IStream
** ppstm
) /* [out] */
945 StorageBaseImpl
*This
= (StorageBaseImpl
*)iface
;
946 IEnumSTATSTGImpl
* propertyEnumeration
;
947 StgStreamImpl
* newStream
;
948 StgProperty currentProperty
, newStreamProperty
;
949 ULONG foundPropertyIndex
, newPropertyIndex
;
951 TRACE("(%p, %s, %x, %d, %d, %p)\n",
952 iface
, debugstr_w(pwcsName
), grfMode
,
953 reserved1
, reserved2
, ppstm
);
956 * Validate parameters
959 return STG_E_INVALIDPOINTER
;
962 return STG_E_INVALIDNAME
;
964 if (reserved1
|| reserved2
)
965 return STG_E_INVALIDPARAMETER
;
968 * Validate the STGM flags
970 if ( FAILED( validateSTGM(grfMode
) ))
971 return STG_E_INVALIDFLAG
;
973 if (STGM_SHARE_MODE(grfMode
) != STGM_SHARE_EXCLUSIVE
)
974 return STG_E_INVALIDFLAG
;
979 if ((grfMode
& STGM_DELETEONRELEASE
) ||
980 (grfMode
& STGM_TRANSACTED
))
981 return STG_E_INVALIDFUNCTION
;
983 /* Can't create a stream on read-only storage */
984 if ( STGM_ACCESS_MODE( This
->openFlags
) == STGM_READ
)
985 return STG_E_ACCESSDENIED
;
988 * Check that we're compatible with the parent's storage mode
989 * if not in transacted mode
991 if(!(This
->ancestorStorage
->base
.openFlags
& STGM_TRANSACTED
)) {
992 if ( STGM_ACCESS_MODE( grfMode
) > STGM_ACCESS_MODE( This
->openFlags
) )
993 return STG_E_ACCESSDENIED
;
996 if(This
->ancestorStorage
->base
.openFlags
& STGM_SIMPLE
)
997 if(grfMode
& STGM_CREATE
) return STG_E_INVALIDFLAG
;
1000 * Initialize the out parameter
1005 * Create a property enumeration to search the properties
1007 propertyEnumeration
= IEnumSTATSTGImpl_Construct(This
->ancestorStorage
,
1008 This
->rootPropertySetIndex
);
1010 foundPropertyIndex
= IEnumSTATSTGImpl_FindProperty(propertyEnumeration
,
1014 IEnumSTATSTGImpl_Destroy(propertyEnumeration
);
1016 if (foundPropertyIndex
!= PROPERTY_NULL
)
1019 * An element with this name already exists
1021 if (STGM_CREATE_MODE(grfMode
) == STGM_CREATE
)
1023 StgStreamImpl
*strm
;
1025 LIST_FOR_EACH_ENTRY(strm
, &This
->strmHead
, StgStreamImpl
, StrmListEntry
)
1027 if (strm
->ownerProperty
== foundPropertyIndex
)
1029 TRACE("Stream deleted %p\n", strm
);
1030 strm
->parentStorage
= NULL
;
1031 list_remove(&strm
->StrmListEntry
);
1034 IStorage_DestroyElement(iface
, pwcsName
);
1037 return STG_E_FILEALREADYEXISTS
;
1039 else if (STGM_ACCESS_MODE(This
->openFlags
) == STGM_READ
)
1041 WARN("read-only storage\n");
1042 return STG_E_ACCESSDENIED
;
1046 * memset the empty property
1048 memset(&newStreamProperty
, 0, sizeof(StgProperty
));
1050 newStreamProperty
.sizeOfNameString
=
1051 ( lstrlenW(pwcsName
)+1 ) * sizeof(WCHAR
);
1053 if (newStreamProperty
.sizeOfNameString
> PROPERTY_NAME_BUFFER_LEN
)
1054 return STG_E_INVALIDNAME
;
1056 strcpyW(newStreamProperty
.name
, pwcsName
);
1058 newStreamProperty
.propertyType
= PROPTYPE_STREAM
;
1059 newStreamProperty
.startingBlock
= BLOCK_END_OF_CHAIN
;
1060 newStreamProperty
.size
.u
.LowPart
= 0;
1061 newStreamProperty
.size
.u
.HighPart
= 0;
1063 newStreamProperty
.previousProperty
= PROPERTY_NULL
;
1064 newStreamProperty
.nextProperty
= PROPERTY_NULL
;
1065 newStreamProperty
.dirProperty
= PROPERTY_NULL
;
1067 /* call CoFileTime to get the current time
1068 newStreamProperty.timeStampS1
1069 newStreamProperty.timeStampD1
1070 newStreamProperty.timeStampS2
1071 newStreamProperty.timeStampD2
1074 /* newStreamProperty.propertyUniqueID */
1077 * Get a free property or create a new one
1079 newPropertyIndex
= getFreeProperty(This
->ancestorStorage
);
1082 * Save the new property into the new property spot
1084 StorageImpl_WriteProperty(
1085 This
->ancestorStorage
,
1087 &newStreamProperty
);
1090 * Find a spot in the property chain for our newly created property.
1092 updatePropertyChain(
1098 * Open the stream to return it.
1100 newStream
= StgStreamImpl_Construct(This
, grfMode
, newPropertyIndex
);
1104 *ppstm
= (IStream
*)newStream
;
1107 * Since we are returning a pointer to the interface, we have to nail down
1110 IStream_AddRef(*ppstm
);
1114 return STG_E_INSUFFICIENTMEMORY
;
1120 /************************************************************************
1121 * Storage32BaseImpl_SetClass (IStorage)
1123 * This method will write the specified CLSID in the property of this
1126 * See Windows documentation for more details on IStorage methods.
1128 static HRESULT WINAPI
StorageBaseImpl_SetClass(
1130 REFCLSID clsid
) /* [in] */
1132 StorageBaseImpl
*This
= (StorageBaseImpl
*)iface
;
1133 HRESULT hRes
= E_FAIL
;
1134 StgProperty curProperty
;
1137 TRACE("(%p, %p)\n", iface
, clsid
);
1139 success
= StorageImpl_ReadProperty(This
->ancestorStorage
,
1140 This
->rootPropertySetIndex
,
1144 curProperty
.propertyUniqueID
= *clsid
;
1146 success
= StorageImpl_WriteProperty(This
->ancestorStorage
,
1147 This
->rootPropertySetIndex
,
1156 /************************************************************************
1157 ** Storage32Impl implementation
1160 /************************************************************************
1161 * Storage32Impl_CreateStorage (IStorage)
1163 * This method will create the storage object within the provided storage.
1165 * See Windows documentation for more details on IStorage methods.
1167 static HRESULT WINAPI
StorageImpl_CreateStorage(
1169 const OLECHAR
*pwcsName
, /* [string][in] */
1170 DWORD grfMode
, /* [in] */
1171 DWORD reserved1
, /* [in] */
1172 DWORD reserved2
, /* [in] */
1173 IStorage
**ppstg
) /* [out] */
1175 StorageImpl
* const This
=(StorageImpl
*)iface
;
1177 IEnumSTATSTGImpl
*propertyEnumeration
;
1178 StgProperty currentProperty
;
1179 StgProperty newProperty
;
1180 ULONG foundPropertyIndex
;
1181 ULONG newPropertyIndex
;
1184 TRACE("(%p, %s, %x, %d, %d, %p)\n",
1185 iface
, debugstr_w(pwcsName
), grfMode
,
1186 reserved1
, reserved2
, ppstg
);
1189 * Validate parameters
1192 return STG_E_INVALIDPOINTER
;
1195 return STG_E_INVALIDNAME
;
1198 * Initialize the out parameter
1203 * Validate the STGM flags
1205 if ( FAILED( validateSTGM(grfMode
) ) ||
1206 (grfMode
& STGM_DELETEONRELEASE
) )
1208 WARN("bad grfMode: 0x%x\n", grfMode
);
1209 return STG_E_INVALIDFLAG
;
1213 * Check that we're compatible with the parent's storage mode
1215 if ( STGM_ACCESS_MODE( grfMode
) > STGM_ACCESS_MODE( This
->base
.openFlags
) )
1217 WARN("access denied\n");
1218 return STG_E_ACCESSDENIED
;
1222 * Create a property enumeration and search the properties
1224 propertyEnumeration
= IEnumSTATSTGImpl_Construct( This
->base
.ancestorStorage
,
1225 This
->base
.rootPropertySetIndex
);
1227 foundPropertyIndex
= IEnumSTATSTGImpl_FindProperty(propertyEnumeration
,
1230 IEnumSTATSTGImpl_Destroy(propertyEnumeration
);
1232 if (foundPropertyIndex
!= PROPERTY_NULL
)
1235 * An element with this name already exists
1237 if (STGM_CREATE_MODE(grfMode
) == STGM_CREATE
&&
1238 STGM_ACCESS_MODE(This
->base
.openFlags
) != STGM_READ
)
1240 hr
= IStorage_DestroyElement(iface
, pwcsName
);
1246 WARN("file already exists\n");
1247 return STG_E_FILEALREADYEXISTS
;
1250 else if (STGM_ACCESS_MODE(This
->base
.openFlags
) == STGM_READ
)
1252 WARN("read-only storage\n");
1253 return STG_E_ACCESSDENIED
;
1257 * memset the empty property
1259 memset(&newProperty
, 0, sizeof(StgProperty
));
1261 newProperty
.sizeOfNameString
= (lstrlenW(pwcsName
)+1)*sizeof(WCHAR
);
1263 if (newProperty
.sizeOfNameString
> PROPERTY_NAME_BUFFER_LEN
)
1265 FIXME("name too long\n");
1266 return STG_E_INVALIDNAME
;
1269 strcpyW(newProperty
.name
, pwcsName
);
1271 newProperty
.propertyType
= PROPTYPE_STORAGE
;
1272 newProperty
.startingBlock
= BLOCK_END_OF_CHAIN
;
1273 newProperty
.size
.u
.LowPart
= 0;
1274 newProperty
.size
.u
.HighPart
= 0;
1276 newProperty
.previousProperty
= PROPERTY_NULL
;
1277 newProperty
.nextProperty
= PROPERTY_NULL
;
1278 newProperty
.dirProperty
= PROPERTY_NULL
;
1280 /* call CoFileTime to get the current time
1281 newProperty.timeStampS1
1282 newProperty.timeStampD1
1283 newProperty.timeStampS2
1284 newProperty.timeStampD2
1287 /* newStorageProperty.propertyUniqueID */
1290 * Obtain a free property in the property chain
1292 newPropertyIndex
= getFreeProperty(This
->base
.ancestorStorage
);
1295 * Save the new property into the new property spot
1297 StorageImpl_WriteProperty(
1298 This
->base
.ancestorStorage
,
1303 * Find a spot in the property chain for our newly created property.
1305 updatePropertyChain(
1311 * Open it to get a pointer to return.
1313 hr
= IStorage_OpenStorage(iface
, pwcsName
, 0, grfMode
, 0, 0, ppstg
);
1315 if( (hr
!= S_OK
) || (*ppstg
== NULL
))
1325 /***************************************************************************
1329 * Get a free property or create a new one.
1331 static ULONG
getFreeProperty(
1332 StorageImpl
*storage
)
1334 ULONG currentPropertyIndex
= 0;
1335 ULONG newPropertyIndex
= PROPERTY_NULL
;
1336 BOOL readSuccessful
= TRUE
;
1337 StgProperty currentProperty
;
1342 * Start by reading the root property
1344 readSuccessful
= StorageImpl_ReadProperty(storage
->base
.ancestorStorage
,
1345 currentPropertyIndex
,
1349 if (currentProperty
.sizeOfNameString
== 0)
1352 * The property existis and is available, we found it.
1354 newPropertyIndex
= currentPropertyIndex
;
1360 * We exhausted the property list, we will create more space below
1362 newPropertyIndex
= currentPropertyIndex
;
1364 currentPropertyIndex
++;
1366 } while (newPropertyIndex
== PROPERTY_NULL
);
1369 * grow the property chain
1371 if (! readSuccessful
)
1373 StgProperty emptyProperty
;
1374 ULARGE_INTEGER newSize
;
1375 ULONG propertyIndex
;
1376 ULONG lastProperty
= 0;
1377 ULONG blockCount
= 0;
1380 * obtain the new count of property blocks
1382 blockCount
= BlockChainStream_GetCount(
1383 storage
->base
.ancestorStorage
->rootBlockChain
)+1;
1386 * initialize the size used by the property stream
1388 newSize
.u
.HighPart
= 0;
1389 newSize
.u
.LowPart
= storage
->bigBlockSize
* blockCount
;
1392 * add a property block to the property chain
1394 BlockChainStream_SetSize(storage
->base
.ancestorStorage
->rootBlockChain
, newSize
);
1397 * memset the empty property in order to initialize the unused newly
1400 memset(&emptyProperty
, 0, sizeof(StgProperty
));
1405 lastProperty
= storage
->bigBlockSize
/ PROPSET_BLOCK_SIZE
* blockCount
;
1408 propertyIndex
= newPropertyIndex
;
1409 propertyIndex
< lastProperty
;
1412 StorageImpl_WriteProperty(
1413 storage
->base
.ancestorStorage
,
1419 return newPropertyIndex
;
1422 /****************************************************************************
1426 * Case insensitive comparison of StgProperty.name by first considering
1429 * Returns <0 when newProperty < currentProperty
1430 * >0 when newProperty > currentProperty
1431 * 0 when newProperty == currentProperty
1433 static LONG
propertyNameCmp(
1434 const OLECHAR
*newProperty
,
1435 const OLECHAR
*currentProperty
)
1437 LONG diff
= lstrlenW(newProperty
) - lstrlenW(currentProperty
);
1442 * We compare the string themselves only when they are of the same length
1444 diff
= lstrcmpiW( newProperty
, currentProperty
);
1450 /****************************************************************************
1454 * Properly link this new element in the property chain.
1456 static void updatePropertyChain(
1457 StorageImpl
*storage
,
1458 ULONG newPropertyIndex
,
1459 StgProperty newProperty
)
1461 StgProperty currentProperty
;
1464 * Read the root property
1466 StorageImpl_ReadProperty(storage
->base
.ancestorStorage
,
1467 storage
->base
.rootPropertySetIndex
,
1470 if (currentProperty
.dirProperty
!= PROPERTY_NULL
)
1473 * The root storage contains some element, therefore, start the research
1474 * for the appropriate location.
1477 ULONG current
, next
, previous
, currentPropertyId
;
1480 * Keep the StgProperty sequence number of the storage first property
1482 currentPropertyId
= currentProperty
.dirProperty
;
1487 StorageImpl_ReadProperty(storage
->base
.ancestorStorage
,
1488 currentProperty
.dirProperty
,
1491 previous
= currentProperty
.previousProperty
;
1492 next
= currentProperty
.nextProperty
;
1493 current
= currentPropertyId
;
1497 LONG diff
= propertyNameCmp( newProperty
.name
, currentProperty
.name
);
1501 if (previous
!= PROPERTY_NULL
)
1503 StorageImpl_ReadProperty(storage
->base
.ancestorStorage
,
1510 currentProperty
.previousProperty
= newPropertyIndex
;
1511 StorageImpl_WriteProperty(storage
->base
.ancestorStorage
,
1519 if (next
!= PROPERTY_NULL
)
1521 StorageImpl_ReadProperty(storage
->base
.ancestorStorage
,
1528 currentProperty
.nextProperty
= newPropertyIndex
;
1529 StorageImpl_WriteProperty(storage
->base
.ancestorStorage
,
1538 * Trying to insert an item with the same name in the
1539 * subtree structure.
1544 previous
= currentProperty
.previousProperty
;
1545 next
= currentProperty
.nextProperty
;
1551 * The root storage is empty, link the new property to its dir property
1553 currentProperty
.dirProperty
= newPropertyIndex
;
1554 StorageImpl_WriteProperty(storage
->base
.ancestorStorage
,
1555 storage
->base
.rootPropertySetIndex
,
1561 /*************************************************************************
1564 static HRESULT WINAPI
StorageImpl_CopyTo(
1566 DWORD ciidExclude
, /* [in] */
1567 const IID
* rgiidExclude
, /* [size_is][unique][in] */
1568 SNB snbExclude
, /* [unique][in] */
1569 IStorage
* pstgDest
) /* [unique][in] */
1571 IEnumSTATSTG
*elements
= 0;
1572 STATSTG curElement
, strStat
;
1574 IStorage
*pstgTmp
, *pstgChild
;
1575 IStream
*pstrTmp
, *pstrChild
;
1577 if ((ciidExclude
!= 0) || (rgiidExclude
!= NULL
) || (snbExclude
!= NULL
))
1578 FIXME("Exclude option not implemented\n");
1580 TRACE("(%p, %d, %p, %p, %p)\n",
1581 iface
, ciidExclude
, rgiidExclude
,
1582 snbExclude
, pstgDest
);
1585 * Perform a sanity check
1587 if ( pstgDest
== 0 )
1588 return STG_E_INVALIDPOINTER
;
1591 * Enumerate the elements
1593 hr
= IStorage_EnumElements( iface
, 0, 0, 0, &elements
);
1601 IStorage_Stat( iface
, &curElement
, STATFLAG_NONAME
);
1602 IStorage_SetClass( pstgDest
, &curElement
.clsid
);
1607 * Obtain the next element
1609 hr
= IEnumSTATSTG_Next( elements
, 1, &curElement
, NULL
);
1611 if ( hr
== S_FALSE
)
1613 hr
= S_OK
; /* done, every element has been copied */
1617 if (curElement
.type
== STGTY_STORAGE
)
1620 * open child source storage
1622 hr
= IStorage_OpenStorage( iface
, curElement
.pwcsName
, NULL
,
1623 STGM_READ
|STGM_SHARE_EXCLUSIVE
,
1624 NULL
, 0, &pstgChild
);
1630 * Check if destination storage is not a child of the source
1631 * storage, which will cause an infinite loop
1633 if (pstgChild
== pstgDest
)
1635 IEnumSTATSTG_Release(elements
);
1637 return STG_E_ACCESSDENIED
;
1641 * create a new storage in destination storage
1643 hr
= IStorage_CreateStorage( pstgDest
, curElement
.pwcsName
,
1644 STGM_FAILIFTHERE
|STGM_WRITE
|STGM_SHARE_EXCLUSIVE
,
1648 * if it already exist, don't create a new one use this one
1650 if (hr
== STG_E_FILEALREADYEXISTS
)
1652 hr
= IStorage_OpenStorage( pstgDest
, curElement
.pwcsName
, NULL
,
1653 STGM_WRITE
|STGM_SHARE_EXCLUSIVE
,
1654 NULL
, 0, &pstgTmp
);
1662 * do the copy recursively
1664 hr
= IStorage_CopyTo( pstgChild
, ciidExclude
, rgiidExclude
,
1665 snbExclude
, pstgTmp
);
1667 IStorage_Release( pstgTmp
);
1668 IStorage_Release( pstgChild
);
1670 else if (curElement
.type
== STGTY_STREAM
)
1673 * create a new stream in destination storage. If the stream already
1674 * exist, it will be deleted and a new one will be created.
1676 hr
= IStorage_CreateStream( pstgDest
, curElement
.pwcsName
,
1677 STGM_CREATE
|STGM_WRITE
|STGM_SHARE_EXCLUSIVE
,
1684 * open child stream storage
1686 hr
= IStorage_OpenStream( iface
, curElement
.pwcsName
, NULL
,
1687 STGM_READ
|STGM_SHARE_EXCLUSIVE
,
1694 * Get the size of the source stream
1696 IStream_Stat( pstrChild
, &strStat
, STATFLAG_NONAME
);
1699 * Set the size of the destination stream.
1701 IStream_SetSize(pstrTmp
, strStat
.cbSize
);
1706 hr
= IStream_CopyTo( pstrChild
, pstrTmp
, strStat
.cbSize
,
1709 IStream_Release( pstrTmp
);
1710 IStream_Release( pstrChild
);
1714 WARN("unknown element type: %d\n", curElement
.type
);
1717 } while (hr
== S_OK
);
1722 IEnumSTATSTG_Release(elements
);
1727 /*************************************************************************
1728 * MoveElementTo (IStorage)
1730 static HRESULT WINAPI
StorageImpl_MoveElementTo(
1732 const OLECHAR
*pwcsName
, /* [string][in] */
1733 IStorage
*pstgDest
, /* [unique][in] */
1734 const OLECHAR
*pwcsNewName
,/* [string][in] */
1735 DWORD grfFlags
) /* [in] */
1737 FIXME("(%p %s %p %s %u): stub\n", iface
,
1738 debugstr_w(pwcsName
), pstgDest
,
1739 debugstr_w(pwcsNewName
), grfFlags
);
1743 /*************************************************************************
1746 * Ensures that any changes made to a storage object open in transacted mode
1747 * are reflected in the parent storage
1750 * Wine doesn't implement transacted mode, which seems to be a basic
1751 * optimization, so we can ignore this stub for now.
1753 static HRESULT WINAPI
StorageImpl_Commit(
1755 DWORD grfCommitFlags
)/* [in] */
1757 FIXME("(%p %d): stub\n", iface
, grfCommitFlags
);
1761 /*************************************************************************
1764 * Discard all changes that have been made since the last commit operation
1766 static HRESULT WINAPI
StorageImpl_Revert(
1769 FIXME("(%p): stub\n", iface
);
1773 /*************************************************************************
1774 * DestroyElement (IStorage)
1776 * Strategy: This implementation is built this way for simplicity not for speed.
1777 * I always delete the topmost element of the enumeration and adjust
1778 * the deleted element pointer all the time. This takes longer to
1779 * do but allow to reinvoke DestroyElement whenever we encounter a
1780 * storage object. The optimisation resides in the usage of another
1781 * enumeration strategy that would give all the leaves of a storage
1782 * first. (postfix order)
1784 static HRESULT WINAPI
StorageImpl_DestroyElement(
1786 const OLECHAR
*pwcsName
)/* [string][in] */
1788 StorageImpl
* const This
=(StorageImpl
*)iface
;
1790 IEnumSTATSTGImpl
* propertyEnumeration
;
1793 StgProperty propertyToDelete
;
1794 StgProperty parentProperty
;
1795 ULONG foundPropertyIndexToDelete
;
1796 ULONG typeOfRelation
;
1797 ULONG parentPropertyId
= 0;
1800 iface
, debugstr_w(pwcsName
));
1803 * Perform a sanity check on the parameters.
1806 return STG_E_INVALIDPOINTER
;
1808 if ( STGM_ACCESS_MODE( This
->base
.openFlags
) == STGM_READ
)
1809 return STG_E_ACCESSDENIED
;
1812 * Create a property enumeration to search the property with the given name
1814 propertyEnumeration
= IEnumSTATSTGImpl_Construct(
1815 This
->base
.ancestorStorage
,
1816 This
->base
.rootPropertySetIndex
);
1818 foundPropertyIndexToDelete
= IEnumSTATSTGImpl_FindProperty(
1819 propertyEnumeration
,
1823 IEnumSTATSTGImpl_Destroy(propertyEnumeration
);
1825 if ( foundPropertyIndexToDelete
== PROPERTY_NULL
)
1827 return STG_E_FILENOTFOUND
;
1831 * Find the parent property of the property to delete (the one that
1832 * link to it). If This->dirProperty == foundPropertyIndexToDelete,
1833 * the parent is This. Otherwise, the parent is one of its sibling...
1837 * First, read This's StgProperty..
1839 res
= StorageImpl_ReadProperty(
1840 This
->base
.ancestorStorage
,
1841 This
->base
.rootPropertySetIndex
,
1847 * Second, check to see if by any chance the actual storage (This) is not
1848 * the parent of the property to delete... We never know...
1850 if ( parentProperty
.dirProperty
== foundPropertyIndexToDelete
)
1853 * Set data as it would have been done in the else part...
1855 typeOfRelation
= PROPERTY_RELATION_DIR
;
1856 parentPropertyId
= This
->base
.rootPropertySetIndex
;
1861 * Create a property enumeration to search the parent properties, and
1862 * delete it once done.
1864 IEnumSTATSTGImpl
* propertyEnumeration2
;
1866 propertyEnumeration2
= IEnumSTATSTGImpl_Construct(
1867 This
->base
.ancestorStorage
,
1868 This
->base
.rootPropertySetIndex
);
1870 typeOfRelation
= IEnumSTATSTGImpl_FindParentProperty(
1871 propertyEnumeration2
,
1872 foundPropertyIndexToDelete
,
1876 IEnumSTATSTGImpl_Destroy(propertyEnumeration2
);
1879 if ( propertyToDelete
.propertyType
== PROPTYPE_STORAGE
)
1881 hr
= deleteStorageProperty(
1883 foundPropertyIndexToDelete
,
1886 else if ( propertyToDelete
.propertyType
== PROPTYPE_STREAM
)
1888 hr
= deleteStreamProperty(
1890 foundPropertyIndexToDelete
,
1898 * Adjust the property chain
1900 hr
= adjustPropertyChain(
1911 /************************************************************************
1912 * StorageImpl_Stat (IStorage)
1914 * This method will retrieve information about this storage object.
1916 * See Windows documentation for more details on IStorage methods.
1918 static HRESULT WINAPI
StorageImpl_Stat( IStorage
* iface
,
1919 STATSTG
* pstatstg
, /* [out] */
1920 DWORD grfStatFlag
) /* [in] */
1922 StorageImpl
* const This
= (StorageImpl
*)iface
;
1923 HRESULT result
= StorageBaseImpl_Stat( iface
, pstatstg
, grfStatFlag
);
1925 if ( SUCCEEDED(result
) && ((grfStatFlag
& STATFLAG_NONAME
) == 0) && This
->pwcsName
)
1927 CoTaskMemFree(pstatstg
->pwcsName
);
1928 pstatstg
->pwcsName
= CoTaskMemAlloc((lstrlenW(This
->pwcsName
)+1)*sizeof(WCHAR
));
1929 strcpyW(pstatstg
->pwcsName
, This
->pwcsName
);
1935 /******************************************************************************
1936 * Internal stream list handlers
1939 void StorageBaseImpl_AddStream(StorageBaseImpl
* stg
, StgStreamImpl
* strm
)
1941 TRACE("Stream added (stg=%p strm=%p)\n", stg
, strm
);
1942 list_add_tail(&stg
->strmHead
,&strm
->StrmListEntry
);
1945 void StorageBaseImpl_RemoveStream(StorageBaseImpl
* stg
, StgStreamImpl
* strm
)
1947 TRACE("Stream removed (stg=%p strm=%p)\n", stg
,strm
);
1948 list_remove(&(strm
->StrmListEntry
));
1951 static void StorageBaseImpl_DeleteAll(StorageBaseImpl
* stg
)
1953 struct list
*cur
, *cur2
;
1954 StgStreamImpl
*strm
=NULL
;
1956 LIST_FOR_EACH_SAFE(cur
, cur2
, &stg
->strmHead
) {
1957 strm
= LIST_ENTRY(cur
,StgStreamImpl
,StrmListEntry
);
1958 TRACE("Streams deleted (stg=%p strm=%p next=%p prev=%p)\n", stg
,strm
,cur
->next
,cur
->prev
);
1959 strm
->parentStorage
= NULL
;
1965 /*********************************************************************
1969 * Perform the deletion of a complete storage node
1972 static HRESULT
deleteStorageProperty(
1973 StorageImpl
*parentStorage
,
1974 ULONG indexOfPropertyToDelete
,
1975 StgProperty propertyToDelete
)
1977 IEnumSTATSTG
*elements
= 0;
1978 IStorage
*childStorage
= 0;
1979 STATSTG currentElement
;
1981 HRESULT destroyHr
= S_OK
;
1984 * Open the storage and enumerate it
1986 hr
= StorageBaseImpl_OpenStorage(
1987 (IStorage
*)parentStorage
,
1988 propertyToDelete
.name
,
1990 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
,
2001 * Enumerate the elements
2003 IStorage_EnumElements( childStorage
, 0, 0, 0, &elements
);
2008 * Obtain the next element
2010 hr
= IEnumSTATSTG_Next(elements
, 1, ¤tElement
, NULL
);
2013 destroyHr
= StorageImpl_DestroyElement(childStorage
, currentElement
.pwcsName
);
2015 CoTaskMemFree(currentElement
.pwcsName
);
2019 * We need to Reset the enumeration every time because we delete elements
2020 * and the enumeration could be invalid
2022 IEnumSTATSTG_Reset(elements
);
2024 } while ((hr
== S_OK
) && (destroyHr
== S_OK
));
2027 * Invalidate the property by zeroing its name member.
2029 propertyToDelete
.sizeOfNameString
= 0;
2031 StorageImpl_WriteProperty(parentStorage
->base
.ancestorStorage
,
2032 indexOfPropertyToDelete
,
2035 IStorage_Release(childStorage
);
2036 IEnumSTATSTG_Release(elements
);
2041 /*********************************************************************
2045 * Perform the deletion of a stream node
2048 static HRESULT
deleteStreamProperty(
2049 StorageImpl
*parentStorage
,
2050 ULONG indexOfPropertyToDelete
,
2051 StgProperty propertyToDelete
)
2055 ULARGE_INTEGER size
;
2057 size
.u
.HighPart
= 0;
2060 hr
= StorageBaseImpl_OpenStream((IStorage
*)parentStorage
,
2061 propertyToDelete
.name
, NULL
, STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &pis
);
2071 hr
= IStream_SetSize(pis
, size
);
2079 * Release the stream object.
2081 IStream_Release(pis
);
2084 * Invalidate the property by zeroing its name member.
2086 propertyToDelete
.sizeOfNameString
= 0;
2089 * Here we should re-read the property so we get the updated pointer
2090 * but since we are here to zap it, I don't do it...
2092 StorageImpl_WriteProperty(
2093 parentStorage
->base
.ancestorStorage
,
2094 indexOfPropertyToDelete
,
2100 /*********************************************************************
2104 * Finds a placeholder for the StgProperty within the Storage
2107 static HRESULT
findPlaceholder(
2108 StorageImpl
*storage
,
2109 ULONG propertyIndexToStore
,
2110 ULONG storePropertyIndex
,
2113 StgProperty storeProperty
;
2117 * Read the storage property
2119 res
= StorageImpl_ReadProperty(
2120 storage
->base
.ancestorStorage
,
2129 if (typeOfRelation
== PROPERTY_RELATION_PREVIOUS
)
2131 if (storeProperty
.previousProperty
!= PROPERTY_NULL
)
2133 return findPlaceholder(
2135 propertyIndexToStore
,
2136 storeProperty
.previousProperty
,
2141 storeProperty
.previousProperty
= propertyIndexToStore
;
2144 else if (typeOfRelation
== PROPERTY_RELATION_NEXT
)
2146 if (storeProperty
.nextProperty
!= PROPERTY_NULL
)
2148 return findPlaceholder(
2150 propertyIndexToStore
,
2151 storeProperty
.nextProperty
,
2156 storeProperty
.nextProperty
= propertyIndexToStore
;
2159 else if (typeOfRelation
== PROPERTY_RELATION_DIR
)
2161 if (storeProperty
.dirProperty
!= PROPERTY_NULL
)
2163 return findPlaceholder(
2165 propertyIndexToStore
,
2166 storeProperty
.dirProperty
,
2171 storeProperty
.dirProperty
= propertyIndexToStore
;
2175 res
= StorageImpl_WriteProperty(
2176 storage
->base
.ancestorStorage
,
2188 /*************************************************************************
2192 * This method takes the previous and the next property link of a property
2193 * to be deleted and find them a place in the Storage.
2195 static HRESULT
adjustPropertyChain(
2197 StgProperty propertyToDelete
,
2198 StgProperty parentProperty
,
2199 ULONG parentPropertyId
,
2202 ULONG newLinkProperty
= PROPERTY_NULL
;
2203 BOOL needToFindAPlaceholder
= FALSE
;
2204 ULONG storeNode
= PROPERTY_NULL
;
2205 ULONG toStoreNode
= PROPERTY_NULL
;
2206 INT relationType
= 0;
2210 if (typeOfRelation
== PROPERTY_RELATION_PREVIOUS
)
2212 if (propertyToDelete
.previousProperty
!= PROPERTY_NULL
)
2215 * Set the parent previous to the property to delete previous
2217 newLinkProperty
= propertyToDelete
.previousProperty
;
2219 if (propertyToDelete
.nextProperty
!= PROPERTY_NULL
)
2222 * We also need to find a storage for the other link, setup variables
2223 * to do this at the end...
2225 needToFindAPlaceholder
= TRUE
;
2226 storeNode
= propertyToDelete
.previousProperty
;
2227 toStoreNode
= propertyToDelete
.nextProperty
;
2228 relationType
= PROPERTY_RELATION_NEXT
;
2231 else if (propertyToDelete
.nextProperty
!= PROPERTY_NULL
)
2234 * Set the parent previous to the property to delete next
2236 newLinkProperty
= propertyToDelete
.nextProperty
;
2240 * Link it for real...
2242 parentProperty
.previousProperty
= newLinkProperty
;
2245 else if (typeOfRelation
== PROPERTY_RELATION_NEXT
)
2247 if (propertyToDelete
.previousProperty
!= PROPERTY_NULL
)
2250 * Set the parent next to the property to delete next previous
2252 newLinkProperty
= propertyToDelete
.previousProperty
;
2254 if (propertyToDelete
.nextProperty
!= PROPERTY_NULL
)
2257 * We also need to find a storage for the other link, setup variables
2258 * to do this at the end...
2260 needToFindAPlaceholder
= TRUE
;
2261 storeNode
= propertyToDelete
.previousProperty
;
2262 toStoreNode
= propertyToDelete
.nextProperty
;
2263 relationType
= PROPERTY_RELATION_NEXT
;
2266 else if (propertyToDelete
.nextProperty
!= PROPERTY_NULL
)
2269 * Set the parent next to the property to delete next
2271 newLinkProperty
= propertyToDelete
.nextProperty
;
2275 * Link it for real...
2277 parentProperty
.nextProperty
= newLinkProperty
;
2279 else /* (typeOfRelation == PROPERTY_RELATION_DIR) */
2281 if (propertyToDelete
.previousProperty
!= PROPERTY_NULL
)
2284 * Set the parent dir to the property to delete previous
2286 newLinkProperty
= propertyToDelete
.previousProperty
;
2288 if (propertyToDelete
.nextProperty
!= PROPERTY_NULL
)
2291 * We also need to find a storage for the other link, setup variables
2292 * to do this at the end...
2294 needToFindAPlaceholder
= TRUE
;
2295 storeNode
= propertyToDelete
.previousProperty
;
2296 toStoreNode
= propertyToDelete
.nextProperty
;
2297 relationType
= PROPERTY_RELATION_NEXT
;
2300 else if (propertyToDelete
.nextProperty
!= PROPERTY_NULL
)
2303 * Set the parent dir to the property to delete next
2305 newLinkProperty
= propertyToDelete
.nextProperty
;
2309 * Link it for real...
2311 parentProperty
.dirProperty
= newLinkProperty
;
2315 * Write back the parent property
2317 res
= StorageImpl_WriteProperty(
2318 This
->base
.ancestorStorage
,
2327 * If a placeholder is required for the other link, then, find one and
2328 * get out of here...
2330 if (needToFindAPlaceholder
)
2332 hr
= findPlaceholder(
2343 /******************************************************************************
2344 * SetElementTimes (IStorage)
2346 static HRESULT WINAPI
StorageImpl_SetElementTimes(
2348 const OLECHAR
*pwcsName
,/* [string][in] */
2349 const FILETIME
*pctime
, /* [in] */
2350 const FILETIME
*patime
, /* [in] */
2351 const FILETIME
*pmtime
) /* [in] */
2353 FIXME("(%s,...), stub!\n",debugstr_w(pwcsName
));
2357 /******************************************************************************
2358 * SetStateBits (IStorage)
2360 static HRESULT WINAPI
StorageImpl_SetStateBits(
2362 DWORD grfStateBits
,/* [in] */
2363 DWORD grfMask
) /* [in] */
2365 StorageImpl
* const This
= (StorageImpl
*)iface
;
2366 This
->base
.stateBits
= (This
->base
.stateBits
& ~grfMask
) | (grfStateBits
& grfMask
);
2371 * Virtual function table for the IStorage32Impl class.
2373 static const IStorageVtbl Storage32Impl_Vtbl
=
2375 StorageBaseImpl_QueryInterface
,
2376 StorageBaseImpl_AddRef
,
2377 StorageBaseImpl_Release
,
2378 StorageBaseImpl_CreateStream
,
2379 StorageBaseImpl_OpenStream
,
2380 StorageImpl_CreateStorage
,
2381 StorageBaseImpl_OpenStorage
,
2383 StorageImpl_MoveElementTo
,
2386 StorageBaseImpl_EnumElements
,
2387 StorageImpl_DestroyElement
,
2388 StorageBaseImpl_RenameElement
,
2389 StorageImpl_SetElementTimes
,
2390 StorageBaseImpl_SetClass
,
2391 StorageImpl_SetStateBits
,
2395 static HRESULT
StorageImpl_Construct(
2405 StgProperty currentProperty
;
2406 BOOL readSuccessful
;
2407 ULONG currentPropertyIndex
;
2409 if ( FAILED( validateSTGM(openFlags
) ))
2410 return STG_E_INVALIDFLAG
;
2412 memset(This
, 0, sizeof(StorageImpl
));
2414 list_init(&This
->base
.strmHead
);
2416 This
->base
.lpVtbl
= &Storage32Impl_Vtbl
;
2417 This
->base
.pssVtbl
= &IPropertySetStorage_Vtbl
;
2418 This
->base
.v_destructor
= StorageImpl_Destroy
;
2419 This
->base
.openFlags
= (openFlags
& ~STGM_CREATE
);
2420 This
->create
= create
;
2423 * This is the top-level storage so initialize the ancestor pointer
2426 This
->base
.ancestorStorage
= This
;
2428 This
->hFile
= hFile
;
2431 This
->pwcsName
= HeapAlloc(GetProcessHeap(), 0,
2432 (lstrlenW(pwcsName
)+1)*sizeof(WCHAR
));
2433 if (!This
->pwcsName
)
2434 return STG_E_INSUFFICIENTMEMORY
;
2435 strcpyW(This
->pwcsName
, pwcsName
);
2439 * Initialize the big block cache.
2441 This
->bigBlockSize
= DEF_BIG_BLOCK_SIZE
;
2442 This
->smallBlockSize
= DEF_SMALL_BLOCK_SIZE
;
2443 This
->bigBlockFile
= BIGBLOCKFILE_Construct(hFile
,
2449 if (This
->bigBlockFile
== 0)
2454 ULARGE_INTEGER size
;
2455 BYTE bigBlockBuffer
[BIG_BLOCK_SIZE
];
2458 * Initialize all header variables:
2459 * - The big block depot consists of one block and it is at block 0
2460 * - The properties start at block 1
2461 * - There is no small block depot
2463 memset( This
->bigBlockDepotStart
,
2465 sizeof(This
->bigBlockDepotStart
));
2467 This
->bigBlockDepotCount
= 1;
2468 This
->bigBlockDepotStart
[0] = 0;
2469 This
->rootStartBlock
= 1;
2470 This
->smallBlockDepotStart
= BLOCK_END_OF_CHAIN
;
2471 This
->bigBlockSizeBits
= DEF_BIG_BLOCK_SIZE_BITS
;
2472 This
->smallBlockSizeBits
= DEF_SMALL_BLOCK_SIZE_BITS
;
2473 This
->extBigBlockDepotStart
= BLOCK_END_OF_CHAIN
;
2474 This
->extBigBlockDepotCount
= 0;
2476 StorageImpl_SaveFileHeader(This
);
2479 * Add one block for the big block depot and one block for the properties
2481 size
.u
.HighPart
= 0;
2482 size
.u
.LowPart
= This
->bigBlockSize
* 3;
2483 BIGBLOCKFILE_SetSize(This
->bigBlockFile
, size
);
2486 * Initialize the big block depot
2488 memset(bigBlockBuffer
, BLOCK_UNUSED
, This
->bigBlockSize
);
2489 StorageUtl_WriteDWord(bigBlockBuffer
, 0, BLOCK_SPECIAL
);
2490 StorageUtl_WriteDWord(bigBlockBuffer
, sizeof(ULONG
), BLOCK_END_OF_CHAIN
);
2491 StorageImpl_WriteBigBlock(This
, 0, bigBlockBuffer
);
2496 * Load the header for the file.
2498 hr
= StorageImpl_LoadFileHeader(This
);
2502 BIGBLOCKFILE_Destructor(This
->bigBlockFile
);
2509 * There is no block depot cached yet.
2511 This
->indexBlockDepotCached
= 0xFFFFFFFF;
2514 * Start searching for free blocks with block 0.
2516 This
->prevFreeBlock
= 0;
2519 * Create the block chain abstractions.
2521 if(!(This
->rootBlockChain
=
2522 BlockChainStream_Construct(This
, &This
->rootStartBlock
, PROPERTY_NULL
)))
2523 return STG_E_READFAULT
;
2525 if(!(This
->smallBlockDepotChain
=
2526 BlockChainStream_Construct(This
, &This
->smallBlockDepotStart
,
2528 return STG_E_READFAULT
;
2531 * Write the root property (memory only)
2535 StgProperty rootProp
;
2537 * Initialize the property chain
2539 memset(&rootProp
, 0, sizeof(rootProp
));
2540 MultiByteToWideChar( CP_ACP
, 0, rootPropertyName
, -1, rootProp
.name
,
2541 sizeof(rootProp
.name
)/sizeof(WCHAR
) );
2542 rootProp
.sizeOfNameString
= (strlenW(rootProp
.name
)+1) * sizeof(WCHAR
);
2543 rootProp
.propertyType
= PROPTYPE_ROOT
;
2544 rootProp
.previousProperty
= PROPERTY_NULL
;
2545 rootProp
.nextProperty
= PROPERTY_NULL
;
2546 rootProp
.dirProperty
= PROPERTY_NULL
;
2547 rootProp
.startingBlock
= BLOCK_END_OF_CHAIN
;
2548 rootProp
.size
.u
.HighPart
= 0;
2549 rootProp
.size
.u
.LowPart
= 0;
2551 StorageImpl_WriteProperty(This
, 0, &rootProp
);
2555 * Find the ID of the root in the property sets.
2557 currentPropertyIndex
= 0;
2561 readSuccessful
= StorageImpl_ReadProperty(
2563 currentPropertyIndex
,
2568 if ( (currentProperty
.sizeOfNameString
!= 0 ) &&
2569 (currentProperty
.propertyType
== PROPTYPE_ROOT
) )
2571 This
->base
.rootPropertySetIndex
= currentPropertyIndex
;
2575 currentPropertyIndex
++;
2577 } while (readSuccessful
&& (This
->base
.rootPropertySetIndex
== PROPERTY_NULL
) );
2579 if (!readSuccessful
)
2582 return STG_E_READFAULT
;
2586 * Create the block chain abstraction for the small block root chain.
2588 if(!(This
->smallBlockRootChain
=
2589 BlockChainStream_Construct(This
, NULL
, This
->base
.rootPropertySetIndex
)))
2590 return STG_E_READFAULT
;
2595 static void StorageImpl_Destroy(StorageBaseImpl
* iface
)
2597 StorageImpl
*This
= (StorageImpl
*) iface
;
2598 TRACE("(%p)\n", This
);
2600 StorageBaseImpl_DeleteAll(&This
->base
);
2602 HeapFree(GetProcessHeap(), 0, This
->pwcsName
);
2604 BlockChainStream_Destroy(This
->smallBlockRootChain
);
2605 BlockChainStream_Destroy(This
->rootBlockChain
);
2606 BlockChainStream_Destroy(This
->smallBlockDepotChain
);
2608 BIGBLOCKFILE_Destructor(This
->bigBlockFile
);
2609 HeapFree(GetProcessHeap(), 0, This
);
2612 /******************************************************************************
2613 * Storage32Impl_GetNextFreeBigBlock
2615 * Returns the index of the next free big block.
2616 * If the big block depot is filled, this method will enlarge it.
2619 static ULONG
StorageImpl_GetNextFreeBigBlock(
2622 ULONG depotBlockIndexPos
;
2623 BYTE depotBuffer
[BIG_BLOCK_SIZE
];
2625 ULONG depotBlockOffset
;
2626 ULONG blocksPerDepot
= This
->bigBlockSize
/ sizeof(ULONG
);
2627 ULONG nextBlockIndex
= BLOCK_SPECIAL
;
2629 ULONG freeBlock
= BLOCK_UNUSED
;
2631 depotIndex
= This
->prevFreeBlock
/ blocksPerDepot
;
2632 depotBlockOffset
= (This
->prevFreeBlock
% blocksPerDepot
) * sizeof(ULONG
);
2635 * Scan the entire big block depot until we find a block marked free
2637 while (nextBlockIndex
!= BLOCK_UNUSED
)
2639 if (depotIndex
< COUNT_BBDEPOTINHEADER
)
2641 depotBlockIndexPos
= This
->bigBlockDepotStart
[depotIndex
];
2644 * Grow the primary depot.
2646 if (depotBlockIndexPos
== BLOCK_UNUSED
)
2648 depotBlockIndexPos
= depotIndex
*blocksPerDepot
;
2651 * Add a block depot.
2653 Storage32Impl_AddBlockDepot(This
, depotBlockIndexPos
);
2654 This
->bigBlockDepotCount
++;
2655 This
->bigBlockDepotStart
[depotIndex
] = depotBlockIndexPos
;
2658 * Flag it as a block depot.
2660 StorageImpl_SetNextBlockInChain(This
,
2664 /* Save new header information.
2666 StorageImpl_SaveFileHeader(This
);
2671 depotBlockIndexPos
= Storage32Impl_GetExtDepotBlock(This
, depotIndex
);
2673 if (depotBlockIndexPos
== BLOCK_UNUSED
)
2676 * Grow the extended depot.
2678 ULONG extIndex
= BLOCK_UNUSED
;
2679 ULONG numExtBlocks
= depotIndex
- COUNT_BBDEPOTINHEADER
;
2680 ULONG extBlockOffset
= numExtBlocks
% (blocksPerDepot
- 1);
2682 if (extBlockOffset
== 0)
2684 /* We need an extended block.
2686 extIndex
= Storage32Impl_AddExtBlockDepot(This
);
2687 This
->extBigBlockDepotCount
++;
2688 depotBlockIndexPos
= extIndex
+ 1;
2691 depotBlockIndexPos
= depotIndex
* blocksPerDepot
;
2694 * Add a block depot and mark it in the extended block.
2696 Storage32Impl_AddBlockDepot(This
, depotBlockIndexPos
);
2697 This
->bigBlockDepotCount
++;
2698 Storage32Impl_SetExtDepotBlock(This
, depotIndex
, depotBlockIndexPos
);
2700 /* Flag the block depot.
2702 StorageImpl_SetNextBlockInChain(This
,
2706 /* If necessary, flag the extended depot block.
2708 if (extIndex
!= BLOCK_UNUSED
)
2709 StorageImpl_SetNextBlockInChain(This
, extIndex
, BLOCK_EXTBBDEPOT
);
2711 /* Save header information.
2713 StorageImpl_SaveFileHeader(This
);
2717 success
= StorageImpl_ReadBigBlock(This
, depotBlockIndexPos
, depotBuffer
);
2721 while ( ( (depotBlockOffset
/sizeof(ULONG
) ) < blocksPerDepot
) &&
2722 ( nextBlockIndex
!= BLOCK_UNUSED
))
2724 StorageUtl_ReadDWord(depotBuffer
, depotBlockOffset
, &nextBlockIndex
);
2726 if (nextBlockIndex
== BLOCK_UNUSED
)
2728 freeBlock
= (depotIndex
* blocksPerDepot
) +
2729 (depotBlockOffset
/sizeof(ULONG
));
2732 depotBlockOffset
+= sizeof(ULONG
);
2737 depotBlockOffset
= 0;
2741 * make sure that the block physically exists before using it
2743 BIGBLOCKFILE_EnsureExists(This
->bigBlockFile
, freeBlock
);
2745 This
->prevFreeBlock
= freeBlock
;
2750 /******************************************************************************
2751 * Storage32Impl_AddBlockDepot
2753 * This will create a depot block, essentially it is a block initialized
2756 static void Storage32Impl_AddBlockDepot(StorageImpl
* This
, ULONG blockIndex
)
2758 BYTE blockBuffer
[BIG_BLOCK_SIZE
];
2761 * Initialize blocks as free
2763 memset(blockBuffer
, BLOCK_UNUSED
, This
->bigBlockSize
);
2764 StorageImpl_WriteBigBlock(This
, blockIndex
, blockBuffer
);
2767 /******************************************************************************
2768 * Storage32Impl_GetExtDepotBlock
2770 * Returns the index of the block that corresponds to the specified depot
2771 * index. This method is only for depot indexes equal or greater than
2772 * COUNT_BBDEPOTINHEADER.
2774 static ULONG
Storage32Impl_GetExtDepotBlock(StorageImpl
* This
, ULONG depotIndex
)
2776 ULONG depotBlocksPerExtBlock
= (This
->bigBlockSize
/ sizeof(ULONG
)) - 1;
2777 ULONG numExtBlocks
= depotIndex
- COUNT_BBDEPOTINHEADER
;
2778 ULONG extBlockCount
= numExtBlocks
/ depotBlocksPerExtBlock
;
2779 ULONG extBlockOffset
= numExtBlocks
% depotBlocksPerExtBlock
;
2780 ULONG blockIndex
= BLOCK_UNUSED
;
2781 ULONG extBlockIndex
= This
->extBigBlockDepotStart
;
2783 assert(depotIndex
>= COUNT_BBDEPOTINHEADER
);
2785 if (This
->extBigBlockDepotStart
== BLOCK_END_OF_CHAIN
)
2786 return BLOCK_UNUSED
;
2788 while (extBlockCount
> 0)
2790 extBlockIndex
= Storage32Impl_GetNextExtendedBlock(This
, extBlockIndex
);
2794 if (extBlockIndex
!= BLOCK_UNUSED
)
2795 StorageImpl_ReadDWordFromBigBlock(This
, extBlockIndex
,
2796 extBlockOffset
* sizeof(ULONG
), &blockIndex
);
2801 /******************************************************************************
2802 * Storage32Impl_SetExtDepotBlock
2804 * Associates the specified block index to the specified depot index.
2805 * This method is only for depot indexes equal or greater than
2806 * COUNT_BBDEPOTINHEADER.
2808 static void Storage32Impl_SetExtDepotBlock(StorageImpl
* This
, ULONG depotIndex
, ULONG blockIndex
)
2810 ULONG depotBlocksPerExtBlock
= (This
->bigBlockSize
/ sizeof(ULONG
)) - 1;
2811 ULONG numExtBlocks
= depotIndex
- COUNT_BBDEPOTINHEADER
;
2812 ULONG extBlockCount
= numExtBlocks
/ depotBlocksPerExtBlock
;
2813 ULONG extBlockOffset
= numExtBlocks
% depotBlocksPerExtBlock
;
2814 ULONG extBlockIndex
= This
->extBigBlockDepotStart
;
2816 assert(depotIndex
>= COUNT_BBDEPOTINHEADER
);
2818 while (extBlockCount
> 0)
2820 extBlockIndex
= Storage32Impl_GetNextExtendedBlock(This
, extBlockIndex
);
2824 if (extBlockIndex
!= BLOCK_UNUSED
)
2826 StorageImpl_WriteDWordToBigBlock(This
, extBlockIndex
,
2827 extBlockOffset
* sizeof(ULONG
),
2832 /******************************************************************************
2833 * Storage32Impl_AddExtBlockDepot
2835 * Creates an extended depot block.
2837 static ULONG
Storage32Impl_AddExtBlockDepot(StorageImpl
* This
)
2839 ULONG numExtBlocks
= This
->extBigBlockDepotCount
;
2840 ULONG nextExtBlock
= This
->extBigBlockDepotStart
;
2841 BYTE depotBuffer
[BIG_BLOCK_SIZE
];
2842 ULONG index
= BLOCK_UNUSED
;
2843 ULONG nextBlockOffset
= This
->bigBlockSize
- sizeof(ULONG
);
2844 ULONG blocksPerDepotBlock
= This
->bigBlockSize
/ sizeof(ULONG
);
2845 ULONG depotBlocksPerExtBlock
= blocksPerDepotBlock
- 1;
2847 index
= (COUNT_BBDEPOTINHEADER
+ (numExtBlocks
* depotBlocksPerExtBlock
)) *
2848 blocksPerDepotBlock
;
2850 if ((numExtBlocks
== 0) && (nextExtBlock
== BLOCK_END_OF_CHAIN
))
2853 * The first extended block.
2855 This
->extBigBlockDepotStart
= index
;
2861 * Follow the chain to the last one.
2863 for (i
= 0; i
< (numExtBlocks
- 1); i
++)
2865 nextExtBlock
= Storage32Impl_GetNextExtendedBlock(This
, nextExtBlock
);
2869 * Add the new extended block to the chain.
2871 StorageImpl_WriteDWordToBigBlock(This
, nextExtBlock
, nextBlockOffset
,
2876 * Initialize this block.
2878 memset(depotBuffer
, BLOCK_UNUSED
, This
->bigBlockSize
);
2879 StorageImpl_WriteBigBlock(This
, index
, depotBuffer
);
2884 /******************************************************************************
2885 * Storage32Impl_FreeBigBlock
2887 * This method will flag the specified block as free in the big block depot.
2889 static void StorageImpl_FreeBigBlock(
2893 StorageImpl_SetNextBlockInChain(This
, blockIndex
, BLOCK_UNUSED
);
2895 if (blockIndex
< This
->prevFreeBlock
)
2896 This
->prevFreeBlock
= blockIndex
;
2899 /************************************************************************
2900 * Storage32Impl_GetNextBlockInChain
2902 * This method will retrieve the block index of the next big block in
2905 * Params: This - Pointer to the Storage object.
2906 * blockIndex - Index of the block to retrieve the chain
2908 * nextBlockIndex - receives the return value.
2910 * Returns: This method returns the index of the next block in the chain.
2911 * It will return the constants:
2912 * BLOCK_SPECIAL - If the block given was not part of a
2914 * BLOCK_END_OF_CHAIN - If the block given was the last in
2916 * BLOCK_UNUSED - If the block given was not past of a chain
2918 * BLOCK_EXTBBDEPOT - This block is part of the extended
2921 * See Windows documentation for more details on IStorage methods.
2923 static HRESULT
StorageImpl_GetNextBlockInChain(
2926 ULONG
* nextBlockIndex
)
2928 ULONG offsetInDepot
= blockIndex
* sizeof (ULONG
);
2929 ULONG depotBlockCount
= offsetInDepot
/ This
->bigBlockSize
;
2930 ULONG depotBlockOffset
= offsetInDepot
% This
->bigBlockSize
;
2931 BYTE depotBuffer
[BIG_BLOCK_SIZE
];
2933 ULONG depotBlockIndexPos
;
2936 *nextBlockIndex
= BLOCK_SPECIAL
;
2938 if(depotBlockCount
>= This
->bigBlockDepotCount
)
2940 WARN("depotBlockCount %d, bigBlockDepotCount %d\n", depotBlockCount
,
2941 This
->bigBlockDepotCount
);
2942 return STG_E_READFAULT
;
2946 * Cache the currently accessed depot block.
2948 if (depotBlockCount
!= This
->indexBlockDepotCached
)
2950 This
->indexBlockDepotCached
= depotBlockCount
;
2952 if (depotBlockCount
< COUNT_BBDEPOTINHEADER
)
2954 depotBlockIndexPos
= This
->bigBlockDepotStart
[depotBlockCount
];
2959 * We have to look in the extended depot.
2961 depotBlockIndexPos
= Storage32Impl_GetExtDepotBlock(This
, depotBlockCount
);
2964 success
= StorageImpl_ReadBigBlock(This
, depotBlockIndexPos
, depotBuffer
);
2967 return STG_E_READFAULT
;
2969 for (index
= 0; index
< NUM_BLOCKS_PER_DEPOT_BLOCK
; index
++)
2971 StorageUtl_ReadDWord(depotBuffer
, index
*sizeof(ULONG
), nextBlockIndex
);
2972 This
->blockDepotCached
[index
] = *nextBlockIndex
;
2976 *nextBlockIndex
= This
->blockDepotCached
[depotBlockOffset
/sizeof(ULONG
)];
2981 /******************************************************************************
2982 * Storage32Impl_GetNextExtendedBlock
2984 * Given an extended block this method will return the next extended block.
2987 * The last ULONG of an extended block is the block index of the next
2988 * extended block. Extended blocks are marked as BLOCK_EXTBBDEPOT in the
2992 * - The index of the next extended block
2993 * - BLOCK_UNUSED: there is no next extended block.
2994 * - Any other return values denotes failure.
2996 static ULONG
Storage32Impl_GetNextExtendedBlock(StorageImpl
* This
, ULONG blockIndex
)
2998 ULONG nextBlockIndex
= BLOCK_SPECIAL
;
2999 ULONG depotBlockOffset
= This
->bigBlockSize
- sizeof(ULONG
);
3001 StorageImpl_ReadDWordFromBigBlock(This
, blockIndex
, depotBlockOffset
,
3004 return nextBlockIndex
;
3007 /******************************************************************************
3008 * Storage32Impl_SetNextBlockInChain
3010 * This method will write the index of the specified block's next block
3011 * in the big block depot.
3013 * For example: to create the chain 3 -> 1 -> 7 -> End of Chain
3016 * Storage32Impl_SetNextBlockInChain(This, 3, 1);
3017 * Storage32Impl_SetNextBlockInChain(This, 1, 7);
3018 * Storage32Impl_SetNextBlockInChain(This, 7, BLOCK_END_OF_CHAIN);
3021 static void StorageImpl_SetNextBlockInChain(
3026 ULONG offsetInDepot
= blockIndex
* sizeof (ULONG
);
3027 ULONG depotBlockCount
= offsetInDepot
/ This
->bigBlockSize
;
3028 ULONG depotBlockOffset
= offsetInDepot
% This
->bigBlockSize
;
3029 ULONG depotBlockIndexPos
;
3031 assert(depotBlockCount
< This
->bigBlockDepotCount
);
3032 assert(blockIndex
!= nextBlock
);
3034 if (depotBlockCount
< COUNT_BBDEPOTINHEADER
)
3036 depotBlockIndexPos
= This
->bigBlockDepotStart
[depotBlockCount
];
3041 * We have to look in the extended depot.
3043 depotBlockIndexPos
= Storage32Impl_GetExtDepotBlock(This
, depotBlockCount
);
3046 StorageImpl_WriteDWordToBigBlock(This
, depotBlockIndexPos
, depotBlockOffset
,
3049 * Update the cached block depot, if necessary.
3051 if (depotBlockCount
== This
->indexBlockDepotCached
)
3053 This
->blockDepotCached
[depotBlockOffset
/sizeof(ULONG
)] = nextBlock
;
3057 /******************************************************************************
3058 * Storage32Impl_LoadFileHeader
3060 * This method will read in the file header, i.e. big block index -1.
3062 static HRESULT
StorageImpl_LoadFileHeader(
3065 HRESULT hr
= STG_E_FILENOTFOUND
;
3066 BYTE headerBigBlock
[BIG_BLOCK_SIZE
];
3072 * Get a pointer to the big block of data containing the header.
3074 success
= StorageImpl_ReadBigBlock(This
, -1, headerBigBlock
);
3077 * Extract the information from the header.
3082 * Check for the "magic number" signature and return an error if it is not
3085 if (memcmp(headerBigBlock
, STORAGE_oldmagic
, sizeof(STORAGE_oldmagic
))==0)
3087 return STG_E_OLDFORMAT
;
3090 if (memcmp(headerBigBlock
, STORAGE_magic
, sizeof(STORAGE_magic
))!=0)
3092 return STG_E_INVALIDHEADER
;
3095 StorageUtl_ReadWord(
3097 OFFSET_BIGBLOCKSIZEBITS
,
3098 &This
->bigBlockSizeBits
);
3100 StorageUtl_ReadWord(
3102 OFFSET_SMALLBLOCKSIZEBITS
,
3103 &This
->smallBlockSizeBits
);
3105 StorageUtl_ReadDWord(
3107 OFFSET_BBDEPOTCOUNT
,
3108 &This
->bigBlockDepotCount
);
3110 StorageUtl_ReadDWord(
3112 OFFSET_ROOTSTARTBLOCK
,
3113 &This
->rootStartBlock
);
3115 StorageUtl_ReadDWord(
3117 OFFSET_SBDEPOTSTART
,
3118 &This
->smallBlockDepotStart
);
3120 StorageUtl_ReadDWord(
3122 OFFSET_EXTBBDEPOTSTART
,
3123 &This
->extBigBlockDepotStart
);
3125 StorageUtl_ReadDWord(
3127 OFFSET_EXTBBDEPOTCOUNT
,
3128 &This
->extBigBlockDepotCount
);
3130 for (index
= 0; index
< COUNT_BBDEPOTINHEADER
; index
++)
3132 StorageUtl_ReadDWord(
3134 OFFSET_BBDEPOTSTART
+ (sizeof(ULONG
)*index
),
3135 &(This
->bigBlockDepotStart
[index
]));
3139 * Make the bitwise arithmetic to get the size of the blocks in bytes.
3141 This
->bigBlockSize
= 0x000000001 << (DWORD
)This
->bigBlockSizeBits
;
3142 This
->smallBlockSize
= 0x000000001 << (DWORD
)This
->smallBlockSizeBits
;
3145 * Right now, the code is making some assumptions about the size of the
3146 * blocks, just make sure they are what we're expecting.
3148 if (This
->bigBlockSize
!= DEF_BIG_BLOCK_SIZE
||
3149 This
->smallBlockSize
!= DEF_SMALL_BLOCK_SIZE
)
3151 WARN("Broken OLE storage file\n");
3152 hr
= STG_E_INVALIDHEADER
;
3161 /******************************************************************************
3162 * Storage32Impl_SaveFileHeader
3164 * This method will save to the file the header, i.e. big block -1.
3166 static void StorageImpl_SaveFileHeader(
3169 BYTE headerBigBlock
[BIG_BLOCK_SIZE
];
3174 * Get a pointer to the big block of data containing the header.
3176 success
= StorageImpl_ReadBigBlock(This
, -1, headerBigBlock
);
3179 * If the block read failed, the file is probably new.
3184 * Initialize for all unknown fields.
3186 memset(headerBigBlock
, 0, BIG_BLOCK_SIZE
);
3189 * Initialize the magic number.
3191 memcpy(headerBigBlock
, STORAGE_magic
, sizeof(STORAGE_magic
));
3194 * And a bunch of things we don't know what they mean
3196 StorageUtl_WriteWord(headerBigBlock
, 0x18, 0x3b);
3197 StorageUtl_WriteWord(headerBigBlock
, 0x1a, 0x3);
3198 StorageUtl_WriteWord(headerBigBlock
, 0x1c, (WORD
)-2);
3199 StorageUtl_WriteDWord(headerBigBlock
, 0x38, (DWORD
)0x1000);
3203 * Write the information to the header.
3205 StorageUtl_WriteWord(
3207 OFFSET_BIGBLOCKSIZEBITS
,
3208 This
->bigBlockSizeBits
);
3210 StorageUtl_WriteWord(
3212 OFFSET_SMALLBLOCKSIZEBITS
,
3213 This
->smallBlockSizeBits
);
3215 StorageUtl_WriteDWord(
3217 OFFSET_BBDEPOTCOUNT
,
3218 This
->bigBlockDepotCount
);
3220 StorageUtl_WriteDWord(
3222 OFFSET_ROOTSTARTBLOCK
,
3223 This
->rootStartBlock
);
3225 StorageUtl_WriteDWord(
3227 OFFSET_SBDEPOTSTART
,
3228 This
->smallBlockDepotStart
);
3230 StorageUtl_WriteDWord(
3232 OFFSET_SBDEPOTCOUNT
,
3233 This
->smallBlockDepotChain
?
3234 BlockChainStream_GetCount(This
->smallBlockDepotChain
) : 0);
3236 StorageUtl_WriteDWord(
3238 OFFSET_EXTBBDEPOTSTART
,
3239 This
->extBigBlockDepotStart
);
3241 StorageUtl_WriteDWord(
3243 OFFSET_EXTBBDEPOTCOUNT
,
3244 This
->extBigBlockDepotCount
);
3246 for (index
= 0; index
< COUNT_BBDEPOTINHEADER
; index
++)
3248 StorageUtl_WriteDWord(
3250 OFFSET_BBDEPOTSTART
+ (sizeof(ULONG
)*index
),
3251 (This
->bigBlockDepotStart
[index
]));
3255 * Write the big block back to the file.
3257 StorageImpl_WriteBigBlock(This
, -1, headerBigBlock
);
3260 /******************************************************************************
3261 * Storage32Impl_ReadProperty
3263 * This method will read the specified property from the property chain.
3265 BOOL
StorageImpl_ReadProperty(
3268 StgProperty
* buffer
)
3270 BYTE currentProperty
[PROPSET_BLOCK_SIZE
];
3271 ULARGE_INTEGER offsetInPropSet
;
3275 offsetInPropSet
.u
.HighPart
= 0;
3276 offsetInPropSet
.u
.LowPart
= index
* PROPSET_BLOCK_SIZE
;
3278 readRes
= BlockChainStream_ReadAt(
3279 This
->rootBlockChain
,
3285 if (SUCCEEDED(readRes
))
3287 /* replace the name of root entry (often "Root Entry") by the file name */
3288 WCHAR
*propName
= (index
== This
->base
.rootPropertySetIndex
) ?
3289 This
->filename
: (WCHAR
*)currentProperty
+OFFSET_PS_NAME
;
3291 memset(buffer
->name
, 0, sizeof(buffer
->name
));
3295 PROPERTY_NAME_BUFFER_LEN
);
3296 TRACE("storage name: %s\n", debugstr_w(buffer
->name
));
3298 memcpy(&buffer
->propertyType
, currentProperty
+ OFFSET_PS_PROPERTYTYPE
, 1);
3300 StorageUtl_ReadWord(
3302 OFFSET_PS_NAMELENGTH
,
3303 &buffer
->sizeOfNameString
);
3305 StorageUtl_ReadDWord(
3307 OFFSET_PS_PREVIOUSPROP
,
3308 &buffer
->previousProperty
);
3310 StorageUtl_ReadDWord(
3313 &buffer
->nextProperty
);
3315 StorageUtl_ReadDWord(
3318 &buffer
->dirProperty
);
3320 StorageUtl_ReadGUID(
3323 &buffer
->propertyUniqueID
);
3325 StorageUtl_ReadDWord(
3328 &buffer
->timeStampS1
);
3330 StorageUtl_ReadDWord(
3333 &buffer
->timeStampD1
);
3335 StorageUtl_ReadDWord(
3338 &buffer
->timeStampS2
);
3340 StorageUtl_ReadDWord(
3343 &buffer
->timeStampD2
);
3345 StorageUtl_ReadDWord(
3347 OFFSET_PS_STARTBLOCK
,
3348 &buffer
->startingBlock
);
3350 StorageUtl_ReadDWord(
3353 &buffer
->size
.u
.LowPart
);
3355 buffer
->size
.u
.HighPart
= 0;
3358 return SUCCEEDED(readRes
) ? TRUE
: FALSE
;
3361 /*********************************************************************
3362 * Write the specified property into the property chain
3364 BOOL
StorageImpl_WriteProperty(
3367 const StgProperty
* buffer
)
3369 BYTE currentProperty
[PROPSET_BLOCK_SIZE
];
3370 ULARGE_INTEGER offsetInPropSet
;
3374 offsetInPropSet
.u
.HighPart
= 0;
3375 offsetInPropSet
.u
.LowPart
= index
* PROPSET_BLOCK_SIZE
;
3377 memset(currentProperty
, 0, PROPSET_BLOCK_SIZE
);
3380 currentProperty
+ OFFSET_PS_NAME
,
3382 PROPERTY_NAME_BUFFER_LEN
);
3384 memcpy(currentProperty
+ OFFSET_PS_PROPERTYTYPE
, &buffer
->propertyType
, 1);
3386 StorageUtl_WriteWord(
3388 OFFSET_PS_NAMELENGTH
,
3389 buffer
->sizeOfNameString
);
3391 StorageUtl_WriteDWord(
3393 OFFSET_PS_PREVIOUSPROP
,
3394 buffer
->previousProperty
);
3396 StorageUtl_WriteDWord(
3399 buffer
->nextProperty
);
3401 StorageUtl_WriteDWord(
3404 buffer
->dirProperty
);
3406 StorageUtl_WriteGUID(
3409 &buffer
->propertyUniqueID
);
3411 StorageUtl_WriteDWord(
3414 buffer
->timeStampS1
);
3416 StorageUtl_WriteDWord(
3419 buffer
->timeStampD1
);
3421 StorageUtl_WriteDWord(
3424 buffer
->timeStampS2
);
3426 StorageUtl_WriteDWord(
3429 buffer
->timeStampD2
);
3431 StorageUtl_WriteDWord(
3433 OFFSET_PS_STARTBLOCK
,
3434 buffer
->startingBlock
);
3436 StorageUtl_WriteDWord(
3439 buffer
->size
.u
.LowPart
);
3441 writeRes
= BlockChainStream_WriteAt(This
->rootBlockChain
,
3446 return SUCCEEDED(writeRes
) ? TRUE
: FALSE
;
3449 static BOOL
StorageImpl_ReadBigBlock(
3454 ULARGE_INTEGER ulOffset
;
3457 ulOffset
.u
.HighPart
= 0;
3458 ulOffset
.u
.LowPart
= BLOCK_GetBigBlockOffset(blockIndex
);
3460 StorageImpl_ReadAt(This
, ulOffset
, buffer
, This
->bigBlockSize
, &read
);
3461 return (read
== This
->bigBlockSize
);
3464 static BOOL
StorageImpl_ReadDWordFromBigBlock(
3470 ULARGE_INTEGER ulOffset
;
3474 ulOffset
.u
.HighPart
= 0;
3475 ulOffset
.u
.LowPart
= BLOCK_GetBigBlockOffset(blockIndex
);
3476 ulOffset
.u
.LowPart
+= offset
;
3478 StorageImpl_ReadAt(This
, ulOffset
, &tmp
, sizeof(DWORD
), &read
);
3479 *value
= lendian32toh(tmp
);
3480 return (read
== sizeof(DWORD
));
3483 static BOOL
StorageImpl_WriteBigBlock(
3488 ULARGE_INTEGER ulOffset
;
3491 ulOffset
.u
.HighPart
= 0;
3492 ulOffset
.u
.LowPart
= BLOCK_GetBigBlockOffset(blockIndex
);
3494 StorageImpl_WriteAt(This
, ulOffset
, buffer
, This
->bigBlockSize
, &wrote
);
3495 return (wrote
== This
->bigBlockSize
);
3498 static BOOL
StorageImpl_WriteDWordToBigBlock(
3504 ULARGE_INTEGER ulOffset
;
3507 ulOffset
.u
.HighPart
= 0;
3508 ulOffset
.u
.LowPart
= BLOCK_GetBigBlockOffset(blockIndex
);
3509 ulOffset
.u
.LowPart
+= offset
;
3511 value
= htole32(value
);
3512 StorageImpl_WriteAt(This
, ulOffset
, &value
, sizeof(DWORD
), &wrote
);
3513 return (wrote
== sizeof(DWORD
));
3516 /******************************************************************************
3517 * Storage32Impl_SmallBlocksToBigBlocks
3519 * This method will convert a small block chain to a big block chain.
3520 * The small block chain will be destroyed.
3522 BlockChainStream
* Storage32Impl_SmallBlocksToBigBlocks(
3524 SmallBlockChainStream
** ppsbChain
)
3526 ULONG bbHeadOfChain
= BLOCK_END_OF_CHAIN
;
3527 ULARGE_INTEGER size
, offset
;
3528 ULONG cbRead
, cbWritten
;
3529 ULARGE_INTEGER cbTotalRead
;
3530 ULONG propertyIndex
;
3531 HRESULT resWrite
= S_OK
;
3533 StgProperty chainProperty
;
3535 BlockChainStream
*bbTempChain
= NULL
;
3536 BlockChainStream
*bigBlockChain
= NULL
;
3539 * Create a temporary big block chain that doesn't have
3540 * an associated property. This temporary chain will be
3541 * used to copy data from small blocks to big blocks.
3543 bbTempChain
= BlockChainStream_Construct(This
,
3546 if(!bbTempChain
) return NULL
;
3548 * Grow the big block chain.
3550 size
= SmallBlockChainStream_GetSize(*ppsbChain
);
3551 BlockChainStream_SetSize(bbTempChain
, size
);
3554 * Copy the contents of the small block chain to the big block chain
3555 * by small block size increments.
3557 offset
.u
.LowPart
= 0;
3558 offset
.u
.HighPart
= 0;
3559 cbTotalRead
.QuadPart
= 0;
3561 buffer
= HeapAlloc(GetProcessHeap(),0,DEF_SMALL_BLOCK_SIZE
);
3564 resRead
= SmallBlockChainStream_ReadAt(*ppsbChain
,
3566 min(This
->smallBlockSize
, size
.u
.LowPart
- offset
.u
.LowPart
),
3569 if (FAILED(resRead
))
3574 cbTotalRead
.QuadPart
+= cbRead
;
3576 resWrite
= BlockChainStream_WriteAt(bbTempChain
,
3582 if (FAILED(resWrite
))
3585 offset
.u
.LowPart
+= cbRead
;
3587 } while (cbTotalRead
.QuadPart
< size
.QuadPart
);
3588 HeapFree(GetProcessHeap(),0,buffer
);
3590 size
.u
.HighPart
= 0;
3593 if (FAILED(resRead
) || FAILED(resWrite
))
3595 ERR("conversion failed: resRead = 0x%08x, resWrite = 0x%08x\n", resRead
, resWrite
);
3596 BlockChainStream_SetSize(bbTempChain
, size
);
3597 BlockChainStream_Destroy(bbTempChain
);
3602 * Destroy the small block chain.
3604 propertyIndex
= (*ppsbChain
)->ownerPropertyIndex
;
3605 SmallBlockChainStream_SetSize(*ppsbChain
, size
);
3606 SmallBlockChainStream_Destroy(*ppsbChain
);
3610 * Change the property information. This chain is now a big block chain
3611 * and it doesn't reside in the small blocks chain anymore.
3613 StorageImpl_ReadProperty(This
, propertyIndex
, &chainProperty
);
3615 chainProperty
.startingBlock
= bbHeadOfChain
;
3617 StorageImpl_WriteProperty(This
, propertyIndex
, &chainProperty
);
3620 * Destroy the temporary propertyless big block chain.
3621 * Create a new big block chain associated with this property.
3623 BlockChainStream_Destroy(bbTempChain
);
3624 bigBlockChain
= BlockChainStream_Construct(This
,
3628 return bigBlockChain
;
3631 /******************************************************************************
3632 * Storage32Impl_BigBlocksToSmallBlocks
3634 * This method will convert a big block chain to a small block chain.
3635 * The big block chain will be destroyed on success.
3637 SmallBlockChainStream
* Storage32Impl_BigBlocksToSmallBlocks(
3639 BlockChainStream
** ppbbChain
)
3641 ULARGE_INTEGER size
, offset
, cbTotalRead
;
3642 ULONG cbRead
, cbWritten
, propertyIndex
, sbHeadOfChain
= BLOCK_END_OF_CHAIN
;
3643 HRESULT resWrite
= S_OK
, resRead
;
3644 StgProperty chainProperty
;
3646 SmallBlockChainStream
* sbTempChain
;
3648 TRACE("%p %p\n", This
, ppbbChain
);
3650 sbTempChain
= SmallBlockChainStream_Construct(This
, &sbHeadOfChain
,
3656 size
= BlockChainStream_GetSize(*ppbbChain
);
3657 SmallBlockChainStream_SetSize(sbTempChain
, size
);
3659 offset
.u
.HighPart
= 0;
3660 offset
.u
.LowPart
= 0;
3661 cbTotalRead
.QuadPart
= 0;
3662 buffer
= HeapAlloc(GetProcessHeap(), 0, This
->bigBlockSize
);
3665 resRead
= BlockChainStream_ReadAt(*ppbbChain
, offset
,
3666 min(This
->bigBlockSize
, size
.u
.LowPart
- offset
.u
.LowPart
),
3674 cbTotalRead
.QuadPart
+= cbRead
;
3676 resWrite
= SmallBlockChainStream_WriteAt(sbTempChain
, offset
,
3677 cbRead
, buffer
, &cbWritten
);
3679 if(FAILED(resWrite
))
3682 offset
.u
.LowPart
+= cbRead
;
3684 }while(cbTotalRead
.QuadPart
< size
.QuadPart
);
3685 HeapFree(GetProcessHeap(), 0, buffer
);
3687 size
.u
.HighPart
= 0;
3690 if(FAILED(resRead
) || FAILED(resWrite
))
3692 ERR("conversion failed: resRead = 0x%08x, resWrite = 0x%08x\n", resRead
, resWrite
);
3693 SmallBlockChainStream_SetSize(sbTempChain
, size
);
3694 SmallBlockChainStream_Destroy(sbTempChain
);
3698 /* destroy the original big block chain */
3699 propertyIndex
= (*ppbbChain
)->ownerPropertyIndex
;
3700 BlockChainStream_SetSize(*ppbbChain
, size
);
3701 BlockChainStream_Destroy(*ppbbChain
);
3704 StorageImpl_ReadProperty(This
, propertyIndex
, &chainProperty
);
3705 chainProperty
.startingBlock
= sbHeadOfChain
;
3706 StorageImpl_WriteProperty(This
, propertyIndex
, &chainProperty
);
3708 SmallBlockChainStream_Destroy(sbTempChain
);
3709 return SmallBlockChainStream_Construct(This
, NULL
, propertyIndex
);
3712 static void StorageInternalImpl_Destroy( StorageBaseImpl
*iface
)
3714 StorageInternalImpl
* This
= (StorageInternalImpl
*) iface
;
3716 StorageBaseImpl_Release((IStorage
*)This
->base
.ancestorStorage
);
3717 HeapFree(GetProcessHeap(), 0, This
);
3720 /******************************************************************************
3722 ** Storage32InternalImpl_Commit
3724 ** The non-root storages cannot be opened in transacted mode thus this function
3727 static HRESULT WINAPI
StorageInternalImpl_Commit(
3729 DWORD grfCommitFlags
) /* [in] */
3734 /******************************************************************************
3736 ** Storage32InternalImpl_Revert
3738 ** The non-root storages cannot be opened in transacted mode thus this function
3741 static HRESULT WINAPI
StorageInternalImpl_Revert(
3747 static void IEnumSTATSTGImpl_Destroy(IEnumSTATSTGImpl
* This
)
3749 IStorage_Release((IStorage
*)This
->parentStorage
);
3750 HeapFree(GetProcessHeap(), 0, This
->stackToVisit
);
3751 HeapFree(GetProcessHeap(), 0, This
);
3754 static HRESULT WINAPI
IEnumSTATSTGImpl_QueryInterface(
3755 IEnumSTATSTG
* iface
,
3759 IEnumSTATSTGImpl
* const This
=(IEnumSTATSTGImpl
*)iface
;
3762 * Perform a sanity check on the parameters.
3765 return E_INVALIDARG
;
3768 * Initialize the return parameter.
3773 * Compare the riid with the interface IDs implemented by this object.
3775 if (IsEqualGUID(&IID_IUnknown
, riid
) ||
3776 IsEqualGUID(&IID_IEnumSTATSTG
, riid
))
3779 IEnumSTATSTG_AddRef((IEnumSTATSTG
*)This
);
3783 return E_NOINTERFACE
;
3786 static ULONG WINAPI
IEnumSTATSTGImpl_AddRef(
3787 IEnumSTATSTG
* iface
)
3789 IEnumSTATSTGImpl
* const This
=(IEnumSTATSTGImpl
*)iface
;
3790 return InterlockedIncrement(&This
->ref
);
3793 static ULONG WINAPI
IEnumSTATSTGImpl_Release(
3794 IEnumSTATSTG
* iface
)
3796 IEnumSTATSTGImpl
* const This
=(IEnumSTATSTGImpl
*)iface
;
3800 newRef
= InterlockedDecrement(&This
->ref
);
3803 * If the reference count goes down to 0, perform suicide.
3807 IEnumSTATSTGImpl_Destroy(This
);
3813 static HRESULT WINAPI
IEnumSTATSTGImpl_Next(
3814 IEnumSTATSTG
* iface
,
3817 ULONG
* pceltFetched
)
3819 IEnumSTATSTGImpl
* const This
=(IEnumSTATSTGImpl
*)iface
;
3821 StgProperty currentProperty
;
3822 STATSTG
* currentReturnStruct
= rgelt
;
3823 ULONG objectFetched
= 0;
3824 ULONG currentSearchNode
;
3827 * Perform a sanity check on the parameters.
3829 if ( (rgelt
==0) || ( (celt
!=1) && (pceltFetched
==0) ) )
3830 return E_INVALIDARG
;
3833 * To avoid the special case, get another pointer to a ULONG value if
3834 * the caller didn't supply one.
3836 if (pceltFetched
==0)
3837 pceltFetched
= &objectFetched
;
3840 * Start the iteration, we will iterate until we hit the end of the
3841 * linked list or until we hit the number of items to iterate through
3846 * Start with the node at the top of the stack.
3848 currentSearchNode
= IEnumSTATSTGImpl_PopSearchNode(This
, FALSE
);
3850 while ( ( *pceltFetched
< celt
) &&
3851 ( currentSearchNode
!=PROPERTY_NULL
) )
3854 * Remove the top node from the stack
3856 IEnumSTATSTGImpl_PopSearchNode(This
, TRUE
);
3859 * Read the property from the storage.
3861 StorageImpl_ReadProperty(This
->parentStorage
,
3866 * Copy the information to the return buffer.
3868 StorageUtl_CopyPropertyToSTATSTG(currentReturnStruct
,
3873 * Step to the next item in the iteration
3876 currentReturnStruct
++;
3879 * Push the next search node in the search stack.
3881 IEnumSTATSTGImpl_PushSearchNode(This
, currentProperty
.nextProperty
);
3884 * continue the iteration.
3886 currentSearchNode
= IEnumSTATSTGImpl_PopSearchNode(This
, FALSE
);
3889 if (*pceltFetched
== celt
)
3896 static HRESULT WINAPI
IEnumSTATSTGImpl_Skip(
3897 IEnumSTATSTG
* iface
,
3900 IEnumSTATSTGImpl
* const This
=(IEnumSTATSTGImpl
*)iface
;
3902 StgProperty currentProperty
;
3903 ULONG objectFetched
= 0;
3904 ULONG currentSearchNode
;
3907 * Start with the node at the top of the stack.
3909 currentSearchNode
= IEnumSTATSTGImpl_PopSearchNode(This
, FALSE
);
3911 while ( (objectFetched
< celt
) &&
3912 (currentSearchNode
!=PROPERTY_NULL
) )
3915 * Remove the top node from the stack
3917 IEnumSTATSTGImpl_PopSearchNode(This
, TRUE
);
3920 * Read the property from the storage.
3922 StorageImpl_ReadProperty(This
->parentStorage
,
3927 * Step to the next item in the iteration
3932 * Push the next search node in the search stack.
3934 IEnumSTATSTGImpl_PushSearchNode(This
, currentProperty
.nextProperty
);
3937 * continue the iteration.
3939 currentSearchNode
= IEnumSTATSTGImpl_PopSearchNode(This
, FALSE
);
3942 if (objectFetched
== celt
)
3948 static HRESULT WINAPI
IEnumSTATSTGImpl_Reset(
3949 IEnumSTATSTG
* iface
)
3951 IEnumSTATSTGImpl
* const This
=(IEnumSTATSTGImpl
*)iface
;
3953 StgProperty rootProperty
;
3954 BOOL readSuccessful
;
3957 * Re-initialize the search stack to an empty stack
3959 This
->stackSize
= 0;
3962 * Read the root property from the storage.
3964 readSuccessful
= StorageImpl_ReadProperty(
3965 This
->parentStorage
,
3966 This
->firstPropertyNode
,
3971 assert(rootProperty
.sizeOfNameString
!=0);
3974 * Push the search node in the search stack.
3976 IEnumSTATSTGImpl_PushSearchNode(This
, rootProperty
.dirProperty
);
3982 static HRESULT WINAPI
IEnumSTATSTGImpl_Clone(
3983 IEnumSTATSTG
* iface
,
3984 IEnumSTATSTG
** ppenum
)
3986 IEnumSTATSTGImpl
* const This
=(IEnumSTATSTGImpl
*)iface
;
3988 IEnumSTATSTGImpl
* newClone
;
3991 * Perform a sanity check on the parameters.
3994 return E_INVALIDARG
;
3996 newClone
= IEnumSTATSTGImpl_Construct(This
->parentStorage
,
3997 This
->firstPropertyNode
);
4001 * The new clone enumeration must point to the same current node as
4004 newClone
->stackSize
= This
->stackSize
;
4005 newClone
->stackMaxSize
= This
->stackMaxSize
;
4006 newClone
->stackToVisit
=
4007 HeapAlloc(GetProcessHeap(), 0, sizeof(ULONG
) * newClone
->stackMaxSize
);
4010 newClone
->stackToVisit
,
4012 sizeof(ULONG
) * newClone
->stackSize
);
4014 *ppenum
= (IEnumSTATSTG
*)newClone
;
4017 * Don't forget to nail down a reference to the clone before
4020 IEnumSTATSTGImpl_AddRef(*ppenum
);
4025 static INT
IEnumSTATSTGImpl_FindParentProperty(
4026 IEnumSTATSTGImpl
*This
,
4027 ULONG childProperty
,
4028 StgProperty
*currentProperty
,
4031 ULONG currentSearchNode
;
4035 * To avoid the special case, get another pointer to a ULONG value if
4036 * the caller didn't supply one.
4039 thisNodeId
= &foundNode
;
4042 * Start with the node at the top of the stack.
4044 currentSearchNode
= IEnumSTATSTGImpl_PopSearchNode(This
, FALSE
);
4047 while (currentSearchNode
!=PROPERTY_NULL
)
4050 * Store the current node in the returned parameters
4052 *thisNodeId
= currentSearchNode
;
4055 * Remove the top node from the stack
4057 IEnumSTATSTGImpl_PopSearchNode(This
, TRUE
);
4060 * Read the property from the storage.
4062 StorageImpl_ReadProperty(
4063 This
->parentStorage
,
4067 if (currentProperty
->previousProperty
== childProperty
)
4068 return PROPERTY_RELATION_PREVIOUS
;
4070 else if (currentProperty
->nextProperty
== childProperty
)
4071 return PROPERTY_RELATION_NEXT
;
4073 else if (currentProperty
->dirProperty
== childProperty
)
4074 return PROPERTY_RELATION_DIR
;
4077 * Push the next search node in the search stack.
4079 IEnumSTATSTGImpl_PushSearchNode(This
, currentProperty
->nextProperty
);
4082 * continue the iteration.
4084 currentSearchNode
= IEnumSTATSTGImpl_PopSearchNode(This
, FALSE
);
4087 return PROPERTY_NULL
;
4090 static ULONG
IEnumSTATSTGImpl_FindProperty(
4091 IEnumSTATSTGImpl
* This
,
4092 const OLECHAR
* lpszPropName
,
4093 StgProperty
* currentProperty
)
4095 ULONG currentSearchNode
;
4098 * Start with the node at the top of the stack.
4100 currentSearchNode
= IEnumSTATSTGImpl_PopSearchNode(This
, FALSE
);
4102 while (currentSearchNode
!=PROPERTY_NULL
)
4105 * Remove the top node from the stack
4107 IEnumSTATSTGImpl_PopSearchNode(This
, TRUE
);
4110 * Read the property from the storage.
4112 StorageImpl_ReadProperty(This
->parentStorage
,
4116 if (propertyNameCmp(currentProperty
->name
, lpszPropName
) == 0)
4117 return currentSearchNode
;
4120 * Push the next search node in the search stack.
4122 IEnumSTATSTGImpl_PushSearchNode(This
, currentProperty
->nextProperty
);
4125 * continue the iteration.
4127 currentSearchNode
= IEnumSTATSTGImpl_PopSearchNode(This
, FALSE
);
4130 return PROPERTY_NULL
;
4133 static void IEnumSTATSTGImpl_PushSearchNode(
4134 IEnumSTATSTGImpl
* This
,
4137 StgProperty rootProperty
;
4138 BOOL readSuccessful
;
4141 * First, make sure we're not trying to push an unexisting node.
4143 if (nodeToPush
==PROPERTY_NULL
)
4147 * First push the node to the stack
4149 if (This
->stackSize
== This
->stackMaxSize
)
4151 This
->stackMaxSize
+= ENUMSTATSGT_SIZE_INCREMENT
;
4153 This
->stackToVisit
= HeapReAlloc(
4157 sizeof(ULONG
) * This
->stackMaxSize
);
4160 This
->stackToVisit
[This
->stackSize
] = nodeToPush
;
4164 * Read the root property from the storage.
4166 readSuccessful
= StorageImpl_ReadProperty(
4167 This
->parentStorage
,
4173 assert(rootProperty
.sizeOfNameString
!=0);
4176 * Push the previous search node in the search stack.
4178 IEnumSTATSTGImpl_PushSearchNode(This
, rootProperty
.previousProperty
);
4182 static ULONG
IEnumSTATSTGImpl_PopSearchNode(
4183 IEnumSTATSTGImpl
* This
,
4188 if (This
->stackSize
== 0)
4189 return PROPERTY_NULL
;
4191 topNode
= This
->stackToVisit
[This
->stackSize
-1];
4200 * Virtual function table for the IEnumSTATSTGImpl class.
4202 static const IEnumSTATSTGVtbl IEnumSTATSTGImpl_Vtbl
=
4204 IEnumSTATSTGImpl_QueryInterface
,
4205 IEnumSTATSTGImpl_AddRef
,
4206 IEnumSTATSTGImpl_Release
,
4207 IEnumSTATSTGImpl_Next
,
4208 IEnumSTATSTGImpl_Skip
,
4209 IEnumSTATSTGImpl_Reset
,
4210 IEnumSTATSTGImpl_Clone
4213 /******************************************************************************
4214 ** IEnumSTATSTGImpl implementation
4217 static IEnumSTATSTGImpl
* IEnumSTATSTGImpl_Construct(
4218 StorageImpl
* parentStorage
,
4219 ULONG firstPropertyNode
)
4221 IEnumSTATSTGImpl
* newEnumeration
;
4223 newEnumeration
= HeapAlloc(GetProcessHeap(), 0, sizeof(IEnumSTATSTGImpl
));
4225 if (newEnumeration
!=0)
4228 * Set-up the virtual function table and reference count.
4230 newEnumeration
->lpVtbl
= &IEnumSTATSTGImpl_Vtbl
;
4231 newEnumeration
->ref
= 0;
4234 * We want to nail-down the reference to the storage in case the
4235 * enumeration out-lives the storage in the client application.
4237 newEnumeration
->parentStorage
= parentStorage
;
4238 IStorage_AddRef((IStorage
*)newEnumeration
->parentStorage
);
4240 newEnumeration
->firstPropertyNode
= firstPropertyNode
;
4243 * Initialize the search stack
4245 newEnumeration
->stackSize
= 0;
4246 newEnumeration
->stackMaxSize
= ENUMSTATSGT_SIZE_INCREMENT
;
4247 newEnumeration
->stackToVisit
=
4248 HeapAlloc(GetProcessHeap(), 0, sizeof(ULONG
)*ENUMSTATSGT_SIZE_INCREMENT
);
4251 * Make sure the current node of the iterator is the first one.
4253 IEnumSTATSTGImpl_Reset((IEnumSTATSTG
*)newEnumeration
);
4256 return newEnumeration
;
4260 * Virtual function table for the Storage32InternalImpl class.
4262 static const IStorageVtbl Storage32InternalImpl_Vtbl
=
4264 StorageBaseImpl_QueryInterface
,
4265 StorageBaseImpl_AddRef
,
4266 StorageBaseImpl_Release
,
4267 StorageBaseImpl_CreateStream
,
4268 StorageBaseImpl_OpenStream
,
4269 StorageImpl_CreateStorage
,
4270 StorageBaseImpl_OpenStorage
,
4272 StorageImpl_MoveElementTo
,
4273 StorageInternalImpl_Commit
,
4274 StorageInternalImpl_Revert
,
4275 StorageBaseImpl_EnumElements
,
4276 StorageImpl_DestroyElement
,
4277 StorageBaseImpl_RenameElement
,
4278 StorageImpl_SetElementTimes
,
4279 StorageBaseImpl_SetClass
,
4280 StorageImpl_SetStateBits
,
4281 StorageBaseImpl_Stat
4284 /******************************************************************************
4285 ** Storage32InternalImpl implementation
4288 static StorageInternalImpl
* StorageInternalImpl_Construct(
4289 StorageImpl
* ancestorStorage
,
4291 ULONG rootPropertyIndex
)
4293 StorageInternalImpl
* newStorage
;
4296 * Allocate space for the new storage object
4298 newStorage
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(StorageInternalImpl
));
4303 * Initialize the stream list
4305 list_init(&newStorage
->base
.strmHead
);
4308 * Initialize the virtual function table.
4310 newStorage
->base
.lpVtbl
= &Storage32InternalImpl_Vtbl
;
4311 newStorage
->base
.v_destructor
= StorageInternalImpl_Destroy
;
4312 newStorage
->base
.openFlags
= (openFlags
& ~STGM_CREATE
);
4315 * Keep the ancestor storage pointer and nail a reference to it.
4317 newStorage
->base
.ancestorStorage
= ancestorStorage
;
4318 StorageBaseImpl_AddRef((IStorage
*)(newStorage
->base
.ancestorStorage
));
4321 * Keep the index of the root property set for this storage,
4323 newStorage
->base
.rootPropertySetIndex
= rootPropertyIndex
;
4331 /******************************************************************************
4332 ** StorageUtl implementation
4335 void StorageUtl_ReadWord(const BYTE
* buffer
, ULONG offset
, WORD
* value
)
4339 memcpy(&tmp
, buffer
+offset
, sizeof(WORD
));
4340 *value
= lendian16toh(tmp
);
4343 void StorageUtl_WriteWord(BYTE
* buffer
, ULONG offset
, WORD value
)
4345 value
= htole16(value
);
4346 memcpy(buffer
+offset
, &value
, sizeof(WORD
));
4349 void StorageUtl_ReadDWord(const BYTE
* buffer
, ULONG offset
, DWORD
* value
)
4353 memcpy(&tmp
, buffer
+offset
, sizeof(DWORD
));
4354 *value
= lendian32toh(tmp
);
4357 void StorageUtl_WriteDWord(BYTE
* buffer
, ULONG offset
, DWORD value
)
4359 value
= htole32(value
);
4360 memcpy(buffer
+offset
, &value
, sizeof(DWORD
));
4363 void StorageUtl_ReadULargeInteger(const BYTE
* buffer
, ULONG offset
,
4364 ULARGE_INTEGER
* value
)
4366 #ifdef WORDS_BIGENDIAN
4369 memcpy(&tmp
, buffer
+ offset
, sizeof(ULARGE_INTEGER
));
4370 value
->u
.LowPart
= htole32(tmp
.u
.HighPart
);
4371 value
->u
.HighPart
= htole32(tmp
.u
.LowPart
);
4373 memcpy(value
, buffer
+ offset
, sizeof(ULARGE_INTEGER
));
4377 void StorageUtl_WriteULargeInteger(BYTE
* buffer
, ULONG offset
,
4378 const ULARGE_INTEGER
*value
)
4380 #ifdef WORDS_BIGENDIAN
4383 tmp
.u
.LowPart
= htole32(value
->u
.HighPart
);
4384 tmp
.u
.HighPart
= htole32(value
->u
.LowPart
);
4385 memcpy(buffer
+ offset
, &tmp
, sizeof(ULARGE_INTEGER
));
4387 memcpy(buffer
+ offset
, value
, sizeof(ULARGE_INTEGER
));
4391 void StorageUtl_ReadGUID(const BYTE
* buffer
, ULONG offset
, GUID
* value
)
4393 StorageUtl_ReadDWord(buffer
, offset
, &(value
->Data1
));
4394 StorageUtl_ReadWord(buffer
, offset
+4, &(value
->Data2
));
4395 StorageUtl_ReadWord(buffer
, offset
+6, &(value
->Data3
));
4397 memcpy(value
->Data4
, buffer
+offset
+8, sizeof(value
->Data4
));
4400 void StorageUtl_WriteGUID(BYTE
* buffer
, ULONG offset
, const GUID
* value
)
4402 StorageUtl_WriteDWord(buffer
, offset
, value
->Data1
);
4403 StorageUtl_WriteWord(buffer
, offset
+4, value
->Data2
);
4404 StorageUtl_WriteWord(buffer
, offset
+6, value
->Data3
);
4406 memcpy(buffer
+offset
+8, value
->Data4
, sizeof(value
->Data4
));
4409 void StorageUtl_CopyPropertyToSTATSTG(
4410 STATSTG
* destination
,
4411 const StgProperty
* source
,
4415 * The copy of the string occurs only when the flag is not set
4417 if( ((statFlags
& STATFLAG_NONAME
) != 0) ||
4418 (source
->name
== NULL
) ||
4419 (source
->name
[0] == 0) )
4421 destination
->pwcsName
= 0;
4425 destination
->pwcsName
=
4426 CoTaskMemAlloc((lstrlenW(source
->name
)+1)*sizeof(WCHAR
));
4428 strcpyW(destination
->pwcsName
, source
->name
);
4431 switch (source
->propertyType
)
4433 case PROPTYPE_STORAGE
:
4435 destination
->type
= STGTY_STORAGE
;
4437 case PROPTYPE_STREAM
:
4438 destination
->type
= STGTY_STREAM
;
4441 destination
->type
= STGTY_STREAM
;
4445 destination
->cbSize
= source
->size
;
4447 currentReturnStruct->mtime = {0}; TODO
4448 currentReturnStruct->ctime = {0};
4449 currentReturnStruct->atime = {0};
4451 destination
->grfMode
= 0;
4452 destination
->grfLocksSupported
= 0;
4453 destination
->clsid
= source
->propertyUniqueID
;
4454 destination
->grfStateBits
= 0;
4455 destination
->reserved
= 0;
4458 /******************************************************************************
4459 ** BlockChainStream implementation
4462 BlockChainStream
* BlockChainStream_Construct(
4463 StorageImpl
* parentStorage
,
4464 ULONG
* headOfStreamPlaceHolder
,
4465 ULONG propertyIndex
)
4467 BlockChainStream
* newStream
;
4470 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(BlockChainStream
));
4472 newStream
->parentStorage
= parentStorage
;
4473 newStream
->headOfStreamPlaceHolder
= headOfStreamPlaceHolder
;
4474 newStream
->ownerPropertyIndex
= propertyIndex
;
4475 newStream
->lastBlockNoInSequence
= 0xFFFFFFFF;
4476 newStream
->tailIndex
= BLOCK_END_OF_CHAIN
;
4477 newStream
->numBlocks
= 0;
4479 blockIndex
= BlockChainStream_GetHeadOfChain(newStream
);
4481 while (blockIndex
!= BLOCK_END_OF_CHAIN
)
4483 newStream
->numBlocks
++;
4484 newStream
->tailIndex
= blockIndex
;
4486 if(FAILED(StorageImpl_GetNextBlockInChain(
4491 HeapFree(GetProcessHeap(), 0, newStream
);
4499 void BlockChainStream_Destroy(BlockChainStream
* This
)
4501 HeapFree(GetProcessHeap(), 0, This
);
4504 /******************************************************************************
4505 * BlockChainStream_GetHeadOfChain
4507 * Returns the head of this stream chain.
4508 * Some special chains don't have properties, their heads are kept in
4509 * This->headOfStreamPlaceHolder.
4512 static ULONG
BlockChainStream_GetHeadOfChain(BlockChainStream
* This
)
4514 StgProperty chainProperty
;
4515 BOOL readSuccessful
;
4517 if (This
->headOfStreamPlaceHolder
!= 0)
4518 return *(This
->headOfStreamPlaceHolder
);
4520 if (This
->ownerPropertyIndex
!= PROPERTY_NULL
)
4522 readSuccessful
= StorageImpl_ReadProperty(
4523 This
->parentStorage
,
4524 This
->ownerPropertyIndex
,
4529 return chainProperty
.startingBlock
;
4533 return BLOCK_END_OF_CHAIN
;
4536 /******************************************************************************
4537 * BlockChainStream_GetCount
4539 * Returns the number of blocks that comprises this chain.
4540 * This is not the size of the stream as the last block may not be full!
4543 static ULONG
BlockChainStream_GetCount(BlockChainStream
* This
)
4548 blockIndex
= BlockChainStream_GetHeadOfChain(This
);
4550 while (blockIndex
!= BLOCK_END_OF_CHAIN
)
4554 if(FAILED(StorageImpl_GetNextBlockInChain(
4555 This
->parentStorage
,
4564 /******************************************************************************
4565 * BlockChainStream_ReadAt
4567 * Reads a specified number of bytes from this chain at the specified offset.
4568 * bytesRead may be NULL.
4569 * Failure will be returned if the specified number of bytes has not been read.
4571 HRESULT
BlockChainStream_ReadAt(BlockChainStream
* This
,
4572 ULARGE_INTEGER offset
,
4577 ULONG blockNoInSequence
= offset
.u
.LowPart
/ This
->parentStorage
->bigBlockSize
;
4578 ULONG offsetInBlock
= offset
.u
.LowPart
% This
->parentStorage
->bigBlockSize
;
4579 ULONG bytesToReadInBuffer
;
4583 TRACE("(%p)-> %i %p %i %p\n",This
, offset
.u
.LowPart
, buffer
, size
, bytesRead
);
4586 * Find the first block in the stream that contains part of the buffer.
4588 if ( (This
->lastBlockNoInSequence
== 0xFFFFFFFF) ||
4589 (This
->lastBlockNoInSequenceIndex
== BLOCK_END_OF_CHAIN
) ||
4590 (blockNoInSequence
< This
->lastBlockNoInSequence
) )
4592 blockIndex
= BlockChainStream_GetHeadOfChain(This
);
4593 This
->lastBlockNoInSequence
= blockNoInSequence
;
4597 ULONG temp
= blockNoInSequence
;
4599 blockIndex
= This
->lastBlockNoInSequenceIndex
;
4600 blockNoInSequence
-= This
->lastBlockNoInSequence
;
4601 This
->lastBlockNoInSequence
= temp
;
4604 while ( (blockNoInSequence
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
))
4606 if(FAILED(StorageImpl_GetNextBlockInChain(This
->parentStorage
, blockIndex
, &blockIndex
)))
4607 return STG_E_DOCFILECORRUPT
;
4608 blockNoInSequence
--;
4611 if ((blockNoInSequence
> 0) && (blockIndex
== BLOCK_END_OF_CHAIN
))
4612 return STG_E_DOCFILECORRUPT
; /* We failed to find the starting block */
4614 This
->lastBlockNoInSequenceIndex
= blockIndex
;
4617 * Start reading the buffer.
4620 bufferWalker
= buffer
;
4622 while ( (size
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
) )
4624 ULARGE_INTEGER ulOffset
;
4627 * Calculate how many bytes we can copy from this big block.
4629 bytesToReadInBuffer
=
4630 min(This
->parentStorage
->bigBlockSize
- offsetInBlock
, size
);
4632 TRACE("block %i\n",blockIndex
);
4633 ulOffset
.u
.HighPart
= 0;
4634 ulOffset
.u
.LowPart
= BLOCK_GetBigBlockOffset(blockIndex
) +
4637 StorageImpl_ReadAt(This
->parentStorage
,
4640 bytesToReadInBuffer
,
4643 * Step to the next big block.
4645 if( size
> bytesReadAt
&& FAILED(StorageImpl_GetNextBlockInChain(This
->parentStorage
, blockIndex
, &blockIndex
)))
4646 return STG_E_DOCFILECORRUPT
;
4648 bufferWalker
+= bytesReadAt
;
4649 size
-= bytesReadAt
;
4650 *bytesRead
+= bytesReadAt
;
4651 offsetInBlock
= 0; /* There is no offset on the next block */
4653 if (bytesToReadInBuffer
!= bytesReadAt
)
4657 return (size
== 0) ? S_OK
: STG_E_READFAULT
;
4660 /******************************************************************************
4661 * BlockChainStream_WriteAt
4663 * Writes the specified number of bytes to this chain at the specified offset.
4664 * Will fail if not all specified number of bytes have been written.
4666 HRESULT
BlockChainStream_WriteAt(BlockChainStream
* This
,
4667 ULARGE_INTEGER offset
,
4670 ULONG
* bytesWritten
)
4672 ULONG blockNoInSequence
= offset
.u
.LowPart
/ This
->parentStorage
->bigBlockSize
;
4673 ULONG offsetInBlock
= offset
.u
.LowPart
% This
->parentStorage
->bigBlockSize
;
4676 const BYTE
* bufferWalker
;
4679 * Find the first block in the stream that contains part of the buffer.
4681 if ( (This
->lastBlockNoInSequence
== 0xFFFFFFFF) ||
4682 (This
->lastBlockNoInSequenceIndex
== BLOCK_END_OF_CHAIN
) ||
4683 (blockNoInSequence
< This
->lastBlockNoInSequence
) )
4685 blockIndex
= BlockChainStream_GetHeadOfChain(This
);
4686 This
->lastBlockNoInSequence
= blockNoInSequence
;
4690 ULONG temp
= blockNoInSequence
;
4692 blockIndex
= This
->lastBlockNoInSequenceIndex
;
4693 blockNoInSequence
-= This
->lastBlockNoInSequence
;
4694 This
->lastBlockNoInSequence
= temp
;
4697 while ( (blockNoInSequence
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
))
4699 if(FAILED(StorageImpl_GetNextBlockInChain(This
->parentStorage
, blockIndex
,
4701 return STG_E_DOCFILECORRUPT
;
4702 blockNoInSequence
--;
4705 This
->lastBlockNoInSequenceIndex
= blockIndex
;
4707 /* BlockChainStream_SetSize should have already been called to ensure we have
4708 * enough blocks in the chain to write into */
4709 if (blockIndex
== BLOCK_END_OF_CHAIN
)
4711 ERR("not enough blocks in chain to write data\n");
4712 return STG_E_DOCFILECORRUPT
;
4716 bufferWalker
= buffer
;
4718 while ( (size
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
) )
4720 ULARGE_INTEGER ulOffset
;
4721 DWORD bytesWrittenAt
;
4723 * Calculate how many bytes we can copy from this big block.
4726 min(This
->parentStorage
->bigBlockSize
- offsetInBlock
, size
);
4728 TRACE("block %i\n",blockIndex
);
4729 ulOffset
.u
.HighPart
= 0;
4730 ulOffset
.u
.LowPart
= BLOCK_GetBigBlockOffset(blockIndex
) +
4733 StorageImpl_WriteAt(This
->parentStorage
,
4740 * Step to the next big block.
4742 if(size
> bytesWrittenAt
&& FAILED(StorageImpl_GetNextBlockInChain(This
->parentStorage
, blockIndex
,
4744 return STG_E_DOCFILECORRUPT
;
4746 bufferWalker
+= bytesWrittenAt
;
4747 size
-= bytesWrittenAt
;
4748 *bytesWritten
+= bytesWrittenAt
;
4749 offsetInBlock
= 0; /* There is no offset on the next block */
4751 if (bytesWrittenAt
!= bytesToWrite
)
4755 return (size
== 0) ? S_OK
: STG_E_WRITEFAULT
;
4758 /******************************************************************************
4759 * BlockChainStream_Shrink
4761 * Shrinks this chain in the big block depot.
4763 static BOOL
BlockChainStream_Shrink(BlockChainStream
* This
,
4764 ULARGE_INTEGER newSize
)
4766 ULONG blockIndex
, extraBlock
;
4771 * Reset the last accessed block cache.
4773 This
->lastBlockNoInSequence
= 0xFFFFFFFF;
4774 This
->lastBlockNoInSequenceIndex
= BLOCK_END_OF_CHAIN
;
4777 * Figure out how many blocks are needed to contain the new size
4779 numBlocks
= newSize
.u
.LowPart
/ This
->parentStorage
->bigBlockSize
;
4781 if ((newSize
.u
.LowPart
% This
->parentStorage
->bigBlockSize
) != 0)
4784 blockIndex
= BlockChainStream_GetHeadOfChain(This
);
4787 * Go to the new end of chain
4789 while (count
< numBlocks
)
4791 if(FAILED(StorageImpl_GetNextBlockInChain(This
->parentStorage
, blockIndex
,
4797 /* Get the next block before marking the new end */
4798 if(FAILED(StorageImpl_GetNextBlockInChain(This
->parentStorage
, blockIndex
,
4802 /* Mark the new end of chain */
4803 StorageImpl_SetNextBlockInChain(
4804 This
->parentStorage
,
4806 BLOCK_END_OF_CHAIN
);
4808 This
->tailIndex
= blockIndex
;
4809 This
->numBlocks
= numBlocks
;
4812 * Mark the extra blocks as free
4814 while (extraBlock
!= BLOCK_END_OF_CHAIN
)
4816 if(FAILED(StorageImpl_GetNextBlockInChain(This
->parentStorage
, extraBlock
,
4819 StorageImpl_FreeBigBlock(This
->parentStorage
, extraBlock
);
4820 extraBlock
= blockIndex
;
4826 /******************************************************************************
4827 * BlockChainStream_Enlarge
4829 * Grows this chain in the big block depot.
4831 static BOOL
BlockChainStream_Enlarge(BlockChainStream
* This
,
4832 ULARGE_INTEGER newSize
)
4834 ULONG blockIndex
, currentBlock
;
4836 ULONG oldNumBlocks
= 0;
4838 blockIndex
= BlockChainStream_GetHeadOfChain(This
);
4841 * Empty chain. Create the head.
4843 if (blockIndex
== BLOCK_END_OF_CHAIN
)
4845 blockIndex
= StorageImpl_GetNextFreeBigBlock(This
->parentStorage
);
4846 StorageImpl_SetNextBlockInChain(This
->parentStorage
,
4848 BLOCK_END_OF_CHAIN
);
4850 if (This
->headOfStreamPlaceHolder
!= 0)
4852 *(This
->headOfStreamPlaceHolder
) = blockIndex
;
4856 StgProperty chainProp
;
4857 assert(This
->ownerPropertyIndex
!= PROPERTY_NULL
);
4859 StorageImpl_ReadProperty(
4860 This
->parentStorage
,
4861 This
->ownerPropertyIndex
,
4864 chainProp
.startingBlock
= blockIndex
;
4866 StorageImpl_WriteProperty(
4867 This
->parentStorage
,
4868 This
->ownerPropertyIndex
,
4872 This
->tailIndex
= blockIndex
;
4873 This
->numBlocks
= 1;
4877 * Figure out how many blocks are needed to contain this stream
4879 newNumBlocks
= newSize
.u
.LowPart
/ This
->parentStorage
->bigBlockSize
;
4881 if ((newSize
.u
.LowPart
% This
->parentStorage
->bigBlockSize
) != 0)
4885 * Go to the current end of chain
4887 if (This
->tailIndex
== BLOCK_END_OF_CHAIN
)
4889 currentBlock
= blockIndex
;
4891 while (blockIndex
!= BLOCK_END_OF_CHAIN
)
4894 currentBlock
= blockIndex
;
4896 if(FAILED(StorageImpl_GetNextBlockInChain(This
->parentStorage
, currentBlock
,
4901 This
->tailIndex
= currentBlock
;
4904 currentBlock
= This
->tailIndex
;
4905 oldNumBlocks
= This
->numBlocks
;
4908 * Add new blocks to the chain
4910 if (oldNumBlocks
< newNumBlocks
)
4912 while (oldNumBlocks
< newNumBlocks
)
4914 blockIndex
= StorageImpl_GetNextFreeBigBlock(This
->parentStorage
);
4916 StorageImpl_SetNextBlockInChain(
4917 This
->parentStorage
,
4921 StorageImpl_SetNextBlockInChain(
4922 This
->parentStorage
,
4924 BLOCK_END_OF_CHAIN
);
4926 currentBlock
= blockIndex
;
4930 This
->tailIndex
= blockIndex
;
4931 This
->numBlocks
= newNumBlocks
;
4937 /******************************************************************************
4938 * BlockChainStream_SetSize
4940 * Sets the size of this stream. The big block depot will be updated.
4941 * The file will grow if we grow the chain.
4943 * TODO: Free the actual blocks in the file when we shrink the chain.
4944 * Currently, the blocks are still in the file. So the file size
4945 * doesn't shrink even if we shrink streams.
4947 BOOL
BlockChainStream_SetSize(
4948 BlockChainStream
* This
,
4949 ULARGE_INTEGER newSize
)
4951 ULARGE_INTEGER size
= BlockChainStream_GetSize(This
);
4953 if (newSize
.u
.LowPart
== size
.u
.LowPart
)
4956 if (newSize
.u
.LowPart
< size
.u
.LowPart
)
4958 BlockChainStream_Shrink(This
, newSize
);
4962 BlockChainStream_Enlarge(This
, newSize
);
4968 /******************************************************************************
4969 * BlockChainStream_GetSize
4971 * Returns the size of this chain.
4972 * Will return the block count if this chain doesn't have a property.
4974 static ULARGE_INTEGER
BlockChainStream_GetSize(BlockChainStream
* This
)
4976 StgProperty chainProperty
;
4978 if(This
->headOfStreamPlaceHolder
== NULL
)
4981 * This chain is a data stream read the property and return
4982 * the appropriate size
4984 StorageImpl_ReadProperty(
4985 This
->parentStorage
,
4986 This
->ownerPropertyIndex
,
4989 return chainProperty
.size
;
4994 * this chain is a chain that does not have a property, figure out the
4995 * size by making the product number of used blocks times the
4998 ULARGE_INTEGER result
;
4999 result
.u
.HighPart
= 0;
5002 BlockChainStream_GetCount(This
) *
5003 This
->parentStorage
->bigBlockSize
;
5009 /******************************************************************************
5010 ** SmallBlockChainStream implementation
5013 SmallBlockChainStream
* SmallBlockChainStream_Construct(
5014 StorageImpl
* parentStorage
,
5015 ULONG
* headOfStreamPlaceHolder
,
5016 ULONG propertyIndex
)
5018 SmallBlockChainStream
* newStream
;
5020 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(SmallBlockChainStream
));
5022 newStream
->parentStorage
= parentStorage
;
5023 newStream
->headOfStreamPlaceHolder
= headOfStreamPlaceHolder
;
5024 newStream
->ownerPropertyIndex
= propertyIndex
;
5029 void SmallBlockChainStream_Destroy(
5030 SmallBlockChainStream
* This
)
5032 HeapFree(GetProcessHeap(), 0, This
);
5035 /******************************************************************************
5036 * SmallBlockChainStream_GetHeadOfChain
5038 * Returns the head of this chain of small blocks.
5040 static ULONG
SmallBlockChainStream_GetHeadOfChain(
5041 SmallBlockChainStream
* This
)
5043 StgProperty chainProperty
;
5044 BOOL readSuccessful
;
5046 if (This
->headOfStreamPlaceHolder
!= NULL
)
5047 return *(This
->headOfStreamPlaceHolder
);
5049 if (This
->ownerPropertyIndex
)
5051 readSuccessful
= StorageImpl_ReadProperty(
5052 This
->parentStorage
,
5053 This
->ownerPropertyIndex
,
5058 return chainProperty
.startingBlock
;
5063 return BLOCK_END_OF_CHAIN
;
5066 /******************************************************************************
5067 * SmallBlockChainStream_GetNextBlockInChain
5069 * Returns the index of the next small block in this chain.
5072 * - BLOCK_END_OF_CHAIN: end of this chain
5073 * - BLOCK_UNUSED: small block 'blockIndex' is free
5075 static HRESULT
SmallBlockChainStream_GetNextBlockInChain(
5076 SmallBlockChainStream
* This
,
5078 ULONG
* nextBlockInChain
)
5080 ULARGE_INTEGER offsetOfBlockInDepot
;
5085 *nextBlockInChain
= BLOCK_END_OF_CHAIN
;
5087 offsetOfBlockInDepot
.u
.HighPart
= 0;
5088 offsetOfBlockInDepot
.u
.LowPart
= blockIndex
* sizeof(ULONG
);
5091 * Read those bytes in the buffer from the small block file.
5093 res
= BlockChainStream_ReadAt(
5094 This
->parentStorage
->smallBlockDepotChain
,
5095 offsetOfBlockInDepot
,
5102 StorageUtl_ReadDWord((BYTE
*)&buffer
, 0, nextBlockInChain
);
5109 /******************************************************************************
5110 * SmallBlockChainStream_SetNextBlockInChain
5112 * Writes the index of the next block of the specified block in the small
5114 * To set the end of chain use BLOCK_END_OF_CHAIN as nextBlock.
5115 * To flag a block as free use BLOCK_UNUSED as nextBlock.
5117 static void SmallBlockChainStream_SetNextBlockInChain(
5118 SmallBlockChainStream
* This
,
5122 ULARGE_INTEGER offsetOfBlockInDepot
;
5126 offsetOfBlockInDepot
.u
.HighPart
= 0;
5127 offsetOfBlockInDepot
.u
.LowPart
= blockIndex
* sizeof(ULONG
);
5129 StorageUtl_WriteDWord((BYTE
*)&buffer
, 0, nextBlock
);
5132 * Read those bytes in the buffer from the small block file.
5134 BlockChainStream_WriteAt(
5135 This
->parentStorage
->smallBlockDepotChain
,
5136 offsetOfBlockInDepot
,
5142 /******************************************************************************
5143 * SmallBlockChainStream_FreeBlock
5145 * Flag small block 'blockIndex' as free in the small block depot.
5147 static void SmallBlockChainStream_FreeBlock(
5148 SmallBlockChainStream
* This
,
5151 SmallBlockChainStream_SetNextBlockInChain(This
, blockIndex
, BLOCK_UNUSED
);
5154 /******************************************************************************
5155 * SmallBlockChainStream_GetNextFreeBlock
5157 * Returns the index of a free small block. The small block depot will be
5158 * enlarged if necessary. The small block chain will also be enlarged if
5161 static ULONG
SmallBlockChainStream_GetNextFreeBlock(
5162 SmallBlockChainStream
* This
)
5164 ULARGE_INTEGER offsetOfBlockInDepot
;
5167 ULONG blockIndex
= 0;
5168 ULONG nextBlockIndex
= BLOCK_END_OF_CHAIN
;
5170 ULONG smallBlocksPerBigBlock
;
5172 offsetOfBlockInDepot
.u
.HighPart
= 0;
5175 * Scan the small block depot for a free block
5177 while (nextBlockIndex
!= BLOCK_UNUSED
)
5179 offsetOfBlockInDepot
.u
.LowPart
= blockIndex
* sizeof(ULONG
);
5181 res
= BlockChainStream_ReadAt(
5182 This
->parentStorage
->smallBlockDepotChain
,
5183 offsetOfBlockInDepot
,
5189 * If we run out of space for the small block depot, enlarge it
5193 StorageUtl_ReadDWord((BYTE
*)&buffer
, 0, &nextBlockIndex
);
5195 if (nextBlockIndex
!= BLOCK_UNUSED
)
5201 BlockChainStream_GetCount(This
->parentStorage
->smallBlockDepotChain
);
5203 ULONG sbdIndex
= This
->parentStorage
->smallBlockDepotStart
;
5204 ULONG nextBlock
, newsbdIndex
;
5205 BYTE smallBlockDepot
[BIG_BLOCK_SIZE
];
5207 nextBlock
= sbdIndex
;
5208 while (nextBlock
!= BLOCK_END_OF_CHAIN
)
5210 sbdIndex
= nextBlock
;
5211 StorageImpl_GetNextBlockInChain(This
->parentStorage
, sbdIndex
, &nextBlock
);
5214 newsbdIndex
= StorageImpl_GetNextFreeBigBlock(This
->parentStorage
);
5215 if (sbdIndex
!= BLOCK_END_OF_CHAIN
)
5216 StorageImpl_SetNextBlockInChain(
5217 This
->parentStorage
,
5221 StorageImpl_SetNextBlockInChain(
5222 This
->parentStorage
,
5224 BLOCK_END_OF_CHAIN
);
5227 * Initialize all the small blocks to free
5229 memset(smallBlockDepot
, BLOCK_UNUSED
, This
->parentStorage
->bigBlockSize
);
5230 StorageImpl_WriteBigBlock(This
->parentStorage
, newsbdIndex
, smallBlockDepot
);
5235 * We have just created the small block depot.
5237 StgProperty rootProp
;
5241 * Save it in the header
5243 This
->parentStorage
->smallBlockDepotStart
= newsbdIndex
;
5244 StorageImpl_SaveFileHeader(This
->parentStorage
);
5247 * And allocate the first big block that will contain small blocks
5250 StorageImpl_GetNextFreeBigBlock(This
->parentStorage
);
5252 StorageImpl_SetNextBlockInChain(
5253 This
->parentStorage
,
5255 BLOCK_END_OF_CHAIN
);
5257 StorageImpl_ReadProperty(
5258 This
->parentStorage
,
5259 This
->parentStorage
->base
.rootPropertySetIndex
,
5262 rootProp
.startingBlock
= sbStartIndex
;
5263 rootProp
.size
.u
.HighPart
= 0;
5264 rootProp
.size
.u
.LowPart
= This
->parentStorage
->bigBlockSize
;
5266 StorageImpl_WriteProperty(
5267 This
->parentStorage
,
5268 This
->parentStorage
->base
.rootPropertySetIndex
,
5272 StorageImpl_SaveFileHeader(This
->parentStorage
);
5276 smallBlocksPerBigBlock
=
5277 This
->parentStorage
->bigBlockSize
/ This
->parentStorage
->smallBlockSize
;
5280 * Verify if we have to allocate big blocks to contain small blocks
5282 if (blockIndex
% smallBlocksPerBigBlock
== 0)
5284 StgProperty rootProp
;
5285 ULONG blocksRequired
= (blockIndex
/ smallBlocksPerBigBlock
) + 1;
5287 StorageImpl_ReadProperty(
5288 This
->parentStorage
,
5289 This
->parentStorage
->base
.rootPropertySetIndex
,
5292 if (rootProp
.size
.u
.LowPart
<
5293 (blocksRequired
* This
->parentStorage
->bigBlockSize
))
5295 rootProp
.size
.u
.LowPart
+= This
->parentStorage
->bigBlockSize
;
5297 BlockChainStream_SetSize(
5298 This
->parentStorage
->smallBlockRootChain
,
5301 StorageImpl_WriteProperty(
5302 This
->parentStorage
,
5303 This
->parentStorage
->base
.rootPropertySetIndex
,
5311 /******************************************************************************
5312 * SmallBlockChainStream_ReadAt
5314 * Reads a specified number of bytes from this chain at the specified offset.
5315 * bytesRead may be NULL.
5316 * Failure will be returned if the specified number of bytes has not been read.
5318 HRESULT
SmallBlockChainStream_ReadAt(
5319 SmallBlockChainStream
* This
,
5320 ULARGE_INTEGER offset
,
5326 ULARGE_INTEGER offsetInBigBlockFile
;
5327 ULONG blockNoInSequence
=
5328 offset
.u
.LowPart
/ This
->parentStorage
->smallBlockSize
;
5330 ULONG offsetInBlock
= offset
.u
.LowPart
% This
->parentStorage
->smallBlockSize
;
5331 ULONG bytesToReadInBuffer
;
5333 ULONG bytesReadFromBigBlockFile
;
5337 * This should never happen on a small block file.
5339 assert(offset
.u
.HighPart
==0);
5342 * Find the first block in the stream that contains part of the buffer.
5344 blockIndex
= SmallBlockChainStream_GetHeadOfChain(This
);
5346 while ( (blockNoInSequence
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
))
5348 rc
= SmallBlockChainStream_GetNextBlockInChain(This
, blockIndex
, &blockIndex
);
5351 blockNoInSequence
--;
5355 * Start reading the buffer.
5358 bufferWalker
= buffer
;
5360 while ( (size
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
) )
5363 * Calculate how many bytes we can copy from this small block.
5365 bytesToReadInBuffer
=
5366 min(This
->parentStorage
->smallBlockSize
- offsetInBlock
, size
);
5369 * Calculate the offset of the small block in the small block file.
5371 offsetInBigBlockFile
.u
.HighPart
= 0;
5372 offsetInBigBlockFile
.u
.LowPart
=
5373 blockIndex
* This
->parentStorage
->smallBlockSize
;
5375 offsetInBigBlockFile
.u
.LowPart
+= offsetInBlock
;
5378 * Read those bytes in the buffer from the small block file.
5379 * The small block has already been identified so it shouldn't fail
5380 * unless the file is corrupt.
5382 rc
= BlockChainStream_ReadAt(This
->parentStorage
->smallBlockRootChain
,
5383 offsetInBigBlockFile
,
5384 bytesToReadInBuffer
,
5386 &bytesReadFromBigBlockFile
);
5392 * Step to the next big block.
5394 rc
= SmallBlockChainStream_GetNextBlockInChain(This
, blockIndex
, &blockIndex
);
5396 return STG_E_DOCFILECORRUPT
;
5398 bufferWalker
+= bytesReadFromBigBlockFile
;
5399 size
-= bytesReadFromBigBlockFile
;
5400 *bytesRead
+= bytesReadFromBigBlockFile
;
5401 offsetInBlock
= (offsetInBlock
+ bytesReadFromBigBlockFile
) % This
->parentStorage
->smallBlockSize
;
5404 return (size
== 0) ? S_OK
: STG_E_READFAULT
;
5407 /******************************************************************************
5408 * SmallBlockChainStream_WriteAt
5410 * Writes the specified number of bytes to this chain at the specified offset.
5411 * Will fail if not all specified number of bytes have been written.
5413 HRESULT
SmallBlockChainStream_WriteAt(
5414 SmallBlockChainStream
* This
,
5415 ULARGE_INTEGER offset
,
5418 ULONG
* bytesWritten
)
5420 ULARGE_INTEGER offsetInBigBlockFile
;
5421 ULONG blockNoInSequence
=
5422 offset
.u
.LowPart
/ This
->parentStorage
->smallBlockSize
;
5424 ULONG offsetInBlock
= offset
.u
.LowPart
% This
->parentStorage
->smallBlockSize
;
5425 ULONG bytesToWriteInBuffer
;
5427 ULONG bytesWrittenToBigBlockFile
;
5428 const BYTE
* bufferWalker
;
5432 * This should never happen on a small block file.
5434 assert(offset
.u
.HighPart
==0);
5437 * Find the first block in the stream that contains part of the buffer.
5439 blockIndex
= SmallBlockChainStream_GetHeadOfChain(This
);
5441 while ( (blockNoInSequence
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
))
5443 if(FAILED(SmallBlockChainStream_GetNextBlockInChain(This
, blockIndex
, &blockIndex
)))
5444 return STG_E_DOCFILECORRUPT
;
5445 blockNoInSequence
--;
5449 * Start writing the buffer.
5452 bufferWalker
= buffer
;
5453 while ( (size
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
) )
5456 * Calculate how many bytes we can copy to this small block.
5458 bytesToWriteInBuffer
=
5459 min(This
->parentStorage
->smallBlockSize
- offsetInBlock
, size
);
5462 * Calculate the offset of the small block in the small block file.
5464 offsetInBigBlockFile
.u
.HighPart
= 0;
5465 offsetInBigBlockFile
.u
.LowPart
=
5466 blockIndex
* This
->parentStorage
->smallBlockSize
;
5468 offsetInBigBlockFile
.u
.LowPart
+= offsetInBlock
;
5471 * Write those bytes in the buffer to the small block file.
5473 res
= BlockChainStream_WriteAt(
5474 This
->parentStorage
->smallBlockRootChain
,
5475 offsetInBigBlockFile
,
5476 bytesToWriteInBuffer
,
5478 &bytesWrittenToBigBlockFile
);
5483 * Step to the next big block.
5485 if(FAILED(SmallBlockChainStream_GetNextBlockInChain(This
, blockIndex
,
5488 bufferWalker
+= bytesWrittenToBigBlockFile
;
5489 size
-= bytesWrittenToBigBlockFile
;
5490 *bytesWritten
+= bytesWrittenToBigBlockFile
;
5491 offsetInBlock
= (offsetInBlock
+ bytesWrittenToBigBlockFile
) % This
->parentStorage
->smallBlockSize
;
5494 return (size
== 0) ? S_OK
: STG_E_WRITEFAULT
;
5497 /******************************************************************************
5498 * SmallBlockChainStream_Shrink
5500 * Shrinks this chain in the small block depot.
5502 static BOOL
SmallBlockChainStream_Shrink(
5503 SmallBlockChainStream
* This
,
5504 ULARGE_INTEGER newSize
)
5506 ULONG blockIndex
, extraBlock
;
5510 numBlocks
= newSize
.u
.LowPart
/ This
->parentStorage
->smallBlockSize
;
5512 if ((newSize
.u
.LowPart
% This
->parentStorage
->smallBlockSize
) != 0)
5515 blockIndex
= SmallBlockChainStream_GetHeadOfChain(This
);
5518 * Go to the new end of chain
5520 while (count
< numBlocks
)
5522 if(FAILED(SmallBlockChainStream_GetNextBlockInChain(This
, blockIndex
,
5529 * If the count is 0, we have a special case, the head of the chain was
5534 StgProperty chainProp
;
5536 StorageImpl_ReadProperty(This
->parentStorage
,
5537 This
->ownerPropertyIndex
,
5540 chainProp
.startingBlock
= BLOCK_END_OF_CHAIN
;
5542 StorageImpl_WriteProperty(This
->parentStorage
,
5543 This
->ownerPropertyIndex
,
5547 * We start freeing the chain at the head block.
5549 extraBlock
= blockIndex
;
5553 /* Get the next block before marking the new end */
5554 if(FAILED(SmallBlockChainStream_GetNextBlockInChain(This
, blockIndex
,
5558 /* Mark the new end of chain */
5559 SmallBlockChainStream_SetNextBlockInChain(
5562 BLOCK_END_OF_CHAIN
);
5566 * Mark the extra blocks as free
5568 while (extraBlock
!= BLOCK_END_OF_CHAIN
)
5570 if(FAILED(SmallBlockChainStream_GetNextBlockInChain(This
, extraBlock
,
5573 SmallBlockChainStream_FreeBlock(This
, extraBlock
);
5574 extraBlock
= blockIndex
;
5580 /******************************************************************************
5581 * SmallBlockChainStream_Enlarge
5583 * Grows this chain in the small block depot.
5585 static BOOL
SmallBlockChainStream_Enlarge(
5586 SmallBlockChainStream
* This
,
5587 ULARGE_INTEGER newSize
)
5589 ULONG blockIndex
, currentBlock
;
5591 ULONG oldNumBlocks
= 0;
5593 blockIndex
= SmallBlockChainStream_GetHeadOfChain(This
);
5596 * Empty chain. Create the head.
5598 if (blockIndex
== BLOCK_END_OF_CHAIN
)
5600 blockIndex
= SmallBlockChainStream_GetNextFreeBlock(This
);
5601 SmallBlockChainStream_SetNextBlockInChain(
5604 BLOCK_END_OF_CHAIN
);
5606 if (This
->headOfStreamPlaceHolder
!= NULL
)
5608 *(This
->headOfStreamPlaceHolder
) = blockIndex
;
5612 StgProperty chainProp
;
5614 StorageImpl_ReadProperty(This
->parentStorage
, This
->ownerPropertyIndex
,
5617 chainProp
.startingBlock
= blockIndex
;
5619 StorageImpl_WriteProperty(This
->parentStorage
, This
->ownerPropertyIndex
,
5624 currentBlock
= blockIndex
;
5627 * Figure out how many blocks are needed to contain this stream
5629 newNumBlocks
= newSize
.u
.LowPart
/ This
->parentStorage
->smallBlockSize
;
5631 if ((newSize
.u
.LowPart
% This
->parentStorage
->smallBlockSize
) != 0)
5635 * Go to the current end of chain
5637 while (blockIndex
!= BLOCK_END_OF_CHAIN
)
5640 currentBlock
= blockIndex
;
5641 if(FAILED(SmallBlockChainStream_GetNextBlockInChain(This
, currentBlock
, &blockIndex
)))
5646 * Add new blocks to the chain
5648 while (oldNumBlocks
< newNumBlocks
)
5650 blockIndex
= SmallBlockChainStream_GetNextFreeBlock(This
);
5651 SmallBlockChainStream_SetNextBlockInChain(This
, currentBlock
, blockIndex
);
5653 SmallBlockChainStream_SetNextBlockInChain(
5656 BLOCK_END_OF_CHAIN
);
5658 currentBlock
= blockIndex
;
5665 /******************************************************************************
5666 * SmallBlockChainStream_SetSize
5668 * Sets the size of this stream.
5669 * The file will grow if we grow the chain.
5671 * TODO: Free the actual blocks in the file when we shrink the chain.
5672 * Currently, the blocks are still in the file. So the file size
5673 * doesn't shrink even if we shrink streams.
5675 BOOL
SmallBlockChainStream_SetSize(
5676 SmallBlockChainStream
* This
,
5677 ULARGE_INTEGER newSize
)
5679 ULARGE_INTEGER size
= SmallBlockChainStream_GetSize(This
);
5681 if (newSize
.u
.LowPart
== size
.u
.LowPart
)
5684 if (newSize
.u
.LowPart
< size
.u
.LowPart
)
5686 SmallBlockChainStream_Shrink(This
, newSize
);
5690 SmallBlockChainStream_Enlarge(This
, newSize
);
5696 /******************************************************************************
5697 * SmallBlockChainStream_GetCount
5699 * Returns the number of small blocks that comprises this chain.
5700 * This is not the size of the stream as the last block may not be full!
5703 static ULONG
SmallBlockChainStream_GetCount(SmallBlockChainStream
* This
)
5708 blockIndex
= SmallBlockChainStream_GetHeadOfChain(This
);
5710 while(blockIndex
!= BLOCK_END_OF_CHAIN
)
5714 if(FAILED(SmallBlockChainStream_GetNextBlockInChain(This
,
5715 blockIndex
, &blockIndex
)))
5722 /******************************************************************************
5723 * SmallBlockChainStream_GetSize
5725 * Returns the size of this chain.
5727 static ULARGE_INTEGER
SmallBlockChainStream_GetSize(SmallBlockChainStream
* This
)
5729 StgProperty chainProperty
;
5731 if(This
->headOfStreamPlaceHolder
!= NULL
)
5733 ULARGE_INTEGER result
;
5734 result
.u
.HighPart
= 0;
5736 result
.u
.LowPart
= SmallBlockChainStream_GetCount(This
) *
5737 This
->parentStorage
->smallBlockSize
;
5742 StorageImpl_ReadProperty(
5743 This
->parentStorage
,
5744 This
->ownerPropertyIndex
,
5747 return chainProperty
.size
;
5750 /******************************************************************************
5751 * StgCreateDocfile [OLE32.@]
5752 * Creates a new compound file storage object
5755 * pwcsName [ I] Unicode string with filename (can be relative or NULL)
5756 * grfMode [ I] Access mode for opening the new storage object (see STGM_ constants)
5757 * reserved [ ?] unused?, usually 0
5758 * ppstgOpen [IO] A pointer to IStorage pointer to the new onject
5761 * S_OK if the file was successfully created
5762 * some STG_E_ value if error
5764 * if pwcsName is NULL, create file with new unique name
5765 * the function can returns
5766 * STG_S_CONVERTED if the specified file was successfully converted to storage format
5769 HRESULT WINAPI
StgCreateDocfile(
5773 IStorage
**ppstgOpen
)
5775 StorageImpl
* newStorage
= 0;
5776 HANDLE hFile
= INVALID_HANDLE_VALUE
;
5777 HRESULT hr
= STG_E_INVALIDFLAG
;
5781 DWORD fileAttributes
;
5782 WCHAR tempFileName
[MAX_PATH
];
5784 TRACE("(%s, %x, %d, %p)\n",
5785 debugstr_w(pwcsName
), grfMode
,
5786 reserved
, ppstgOpen
);
5789 * Validate the parameters
5792 return STG_E_INVALIDPOINTER
;
5794 return STG_E_INVALIDPARAMETER
;
5796 /* if no share mode given then DENY_NONE is the default */
5797 if (STGM_SHARE_MODE(grfMode
) == 0)
5798 grfMode
|= STGM_SHARE_DENY_NONE
;
5801 * Validate the STGM flags
5803 if ( FAILED( validateSTGM(grfMode
) ))
5806 /* StgCreateDocFile seems to refuse readonly access, despite MSDN */
5807 switch(STGM_ACCESS_MODE(grfMode
))
5810 case STGM_READWRITE
:
5816 /* in direct mode, can only use SHARE_EXCLUSIVE */
5817 if (!(grfMode
& STGM_TRANSACTED
) && (STGM_SHARE_MODE(grfMode
) != STGM_SHARE_EXCLUSIVE
))
5820 /* but in transacted mode, any share mode is valid */
5823 * Generate a unique name.
5827 WCHAR tempPath
[MAX_PATH
];
5828 static const WCHAR prefix
[] = { 'S', 'T', 'O', 0 };
5830 memset(tempPath
, 0, sizeof(tempPath
));
5831 memset(tempFileName
, 0, sizeof(tempFileName
));
5833 if ((GetTempPathW(MAX_PATH
, tempPath
)) == 0 )
5836 if (GetTempFileNameW(tempPath
, prefix
, 0, tempFileName
) != 0)
5837 pwcsName
= tempFileName
;
5840 hr
= STG_E_INSUFFICIENTMEMORY
;
5844 creationMode
= TRUNCATE_EXISTING
;
5848 creationMode
= GetCreationModeFromSTGM(grfMode
);
5852 * Interpret the STGM value grfMode
5854 shareMode
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
5855 accessMode
= GetAccessModeFromSTGM(grfMode
);
5857 if (grfMode
& STGM_DELETEONRELEASE
)
5858 fileAttributes
= FILE_FLAG_RANDOM_ACCESS
| FILE_FLAG_DELETE_ON_CLOSE
;
5860 fileAttributes
= FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_RANDOM_ACCESS
;
5862 if (STGM_SHARE_MODE(grfMode
) && !(grfMode
& STGM_SHARE_DENY_NONE
))
5863 FIXME("Storage share mode not implemented.\n");
5865 if (grfMode
& STGM_TRANSACTED
)
5866 FIXME("Transacted mode not implemented.\n");
5869 * Initialize the "out" parameter.
5873 hFile
= CreateFileW(pwcsName
,
5881 if (hFile
== INVALID_HANDLE_VALUE
)
5883 if(GetLastError() == ERROR_FILE_EXISTS
)
5884 hr
= STG_E_FILEALREADYEXISTS
;
5891 * Allocate and initialize the new IStorage32object.
5893 newStorage
= HeapAlloc(GetProcessHeap(), 0, sizeof(StorageImpl
));
5895 if (newStorage
== 0)
5897 hr
= STG_E_INSUFFICIENTMEMORY
;
5901 hr
= StorageImpl_Construct(
5912 HeapFree(GetProcessHeap(), 0, newStorage
);
5917 * Get an "out" pointer for the caller.
5919 hr
= StorageBaseImpl_QueryInterface(
5920 (IStorage
*)newStorage
,
5921 (REFIID
)&IID_IStorage
,
5924 TRACE("<-- %p r = %08x\n", *ppstgOpen
, hr
);
5929 /******************************************************************************
5930 * StgCreateStorageEx [OLE32.@]
5932 HRESULT WINAPI
StgCreateStorageEx(const WCHAR
* pwcsName
, DWORD grfMode
, DWORD stgfmt
, DWORD grfAttrs
, STGOPTIONS
* pStgOptions
, void* reserved
, REFIID riid
, void** ppObjectOpen
)
5934 TRACE("(%s, %x, %x, %x, %p, %p, %p, %p)\n", debugstr_w(pwcsName
),
5935 grfMode
, stgfmt
, grfAttrs
, pStgOptions
, reserved
, riid
, ppObjectOpen
);
5937 if (stgfmt
!= STGFMT_FILE
&& grfAttrs
!= 0)
5939 ERR("grfAttrs must be 0 if stgfmt != STGFMT_FILE\n");
5940 return STG_E_INVALIDPARAMETER
;
5943 if (stgfmt
== STGFMT_FILE
&& grfAttrs
!= 0 && grfAttrs
!= FILE_FLAG_NO_BUFFERING
)
5945 ERR("grfAttrs must be 0 or FILE_FLAG_NO_BUFFERING if stgfmt == STGFMT_FILE\n");
5946 return STG_E_INVALIDPARAMETER
;
5949 if (stgfmt
== STGFMT_FILE
)
5951 ERR("Cannot use STGFMT_FILE - this is NTFS only\n");
5952 return STG_E_INVALIDPARAMETER
;
5955 if (stgfmt
== STGFMT_STORAGE
|| stgfmt
== STGFMT_DOCFILE
)
5957 FIXME("Stub: calling StgCreateDocfile, but ignoring pStgOptions and grfAttrs\n");
5958 return StgCreateDocfile(pwcsName
, grfMode
, 0, (IStorage
**)ppObjectOpen
);
5961 ERR("Invalid stgfmt argument\n");
5962 return STG_E_INVALIDPARAMETER
;
5965 /******************************************************************************
5966 * StgCreatePropSetStg [OLE32.@]
5968 HRESULT WINAPI
StgCreatePropSetStg(IStorage
*pstg
, DWORD reserved
,
5969 IPropertySetStorage
**ppPropSetStg
)
5973 TRACE("(%p, 0x%x, %p)\n", pstg
, reserved
, ppPropSetStg
);
5975 hr
= STG_E_INVALIDPARAMETER
;
5977 hr
= StorageBaseImpl_QueryInterface(pstg
, &IID_IPropertySetStorage
,
5978 (void**)ppPropSetStg
);
5982 /******************************************************************************
5983 * StgOpenStorageEx [OLE32.@]
5985 HRESULT WINAPI
StgOpenStorageEx(const WCHAR
* pwcsName
, DWORD grfMode
, DWORD stgfmt
, DWORD grfAttrs
, STGOPTIONS
* pStgOptions
, void* reserved
, REFIID riid
, void** ppObjectOpen
)
5987 TRACE("(%s, %x, %x, %x, %p, %p, %p, %p)\n", debugstr_w(pwcsName
),
5988 grfMode
, stgfmt
, grfAttrs
, pStgOptions
, reserved
, riid
, ppObjectOpen
);
5990 if (stgfmt
!= STGFMT_DOCFILE
&& grfAttrs
!= 0)
5992 ERR("grfAttrs must be 0 if stgfmt != STGFMT_DOCFILE\n");
5993 return STG_E_INVALIDPARAMETER
;
5999 ERR("Cannot use STGFMT_FILE - this is NTFS only\n");
6000 return STG_E_INVALIDPARAMETER
;
6002 case STGFMT_STORAGE
:
6005 case STGFMT_DOCFILE
:
6006 if (grfAttrs
&& grfAttrs
!= FILE_FLAG_NO_BUFFERING
)
6008 ERR("grfAttrs must be 0 or FILE_FLAG_NO_BUFFERING if stgfmt == STGFMT_DOCFILE\n");
6009 return STG_E_INVALIDPARAMETER
;
6011 FIXME("Stub: calling StgOpenStorage, but ignoring pStgOptions and grfAttrs\n");
6015 WARN("STGFMT_ANY assuming storage\n");
6019 return STG_E_INVALIDPARAMETER
;
6022 return StgOpenStorage(pwcsName
, NULL
, grfMode
, NULL
, 0, (IStorage
**)ppObjectOpen
);
6026 /******************************************************************************
6027 * StgOpenStorage [OLE32.@]
6029 HRESULT WINAPI
StgOpenStorage(
6030 const OLECHAR
*pwcsName
,
6031 IStorage
*pstgPriority
,
6035 IStorage
**ppstgOpen
)
6037 StorageImpl
* newStorage
= 0;
6042 WCHAR fullname
[MAX_PATH
];
6044 TRACE("(%s, %p, %x, %p, %d, %p)\n",
6045 debugstr_w(pwcsName
), pstgPriority
, grfMode
,
6046 snbExclude
, reserved
, ppstgOpen
);
6049 * Perform sanity checks
6053 hr
= STG_E_INVALIDNAME
;
6059 hr
= STG_E_INVALIDPOINTER
;
6065 hr
= STG_E_INVALIDPARAMETER
;
6069 if (grfMode
& STGM_PRIORITY
)
6071 if (grfMode
& (STGM_TRANSACTED
|STGM_SIMPLE
|STGM_NOSCRATCH
|STGM_NOSNAPSHOT
))
6072 return STG_E_INVALIDFLAG
;
6073 if (grfMode
& STGM_DELETEONRELEASE
)
6074 return STG_E_INVALIDFUNCTION
;
6075 if(STGM_ACCESS_MODE(grfMode
) != STGM_READ
)
6076 return STG_E_INVALIDFLAG
;
6077 grfMode
&= ~0xf0; /* remove the existing sharing mode */
6078 grfMode
|= STGM_SHARE_DENY_NONE
;
6080 /* STGM_PRIORITY stops other IStorage objects on the same file from
6081 * committing until the STGM_PRIORITY IStorage is closed. it also
6082 * stops non-transacted mode StgOpenStorage calls with write access from
6083 * succeeding. obviously, both of these cannot be achieved through just
6084 * file share flags */
6085 FIXME("STGM_PRIORITY mode not implemented correctly\n");
6089 * Validate the sharing mode
6091 if (!(grfMode
& (STGM_TRANSACTED
|STGM_PRIORITY
)))
6092 switch(STGM_SHARE_MODE(grfMode
))
6094 case STGM_SHARE_EXCLUSIVE
:
6095 case STGM_SHARE_DENY_WRITE
:
6098 hr
= STG_E_INVALIDFLAG
;
6103 * Validate the STGM flags
6105 if ( FAILED( validateSTGM(grfMode
) ) ||
6106 (grfMode
&STGM_CREATE
))
6108 hr
= STG_E_INVALIDFLAG
;
6112 /* shared reading requires transacted mode */
6113 if( STGM_SHARE_MODE(grfMode
) == STGM_SHARE_DENY_WRITE
&&
6114 STGM_ACCESS_MODE(grfMode
) == STGM_READWRITE
&&
6115 !(grfMode
&STGM_TRANSACTED
) )
6117 hr
= STG_E_INVALIDFLAG
;
6122 * Interpret the STGM value grfMode
6124 shareMode
= GetShareModeFromSTGM(grfMode
);
6125 accessMode
= GetAccessModeFromSTGM(grfMode
);
6128 * Initialize the "out" parameter.
6132 hFile
= CreateFileW( pwcsName
,
6137 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_RANDOM_ACCESS
,
6140 if (hFile
==INVALID_HANDLE_VALUE
)
6142 DWORD last_error
= GetLastError();
6148 case ERROR_FILE_NOT_FOUND
:
6149 hr
= STG_E_FILENOTFOUND
;
6152 case ERROR_PATH_NOT_FOUND
:
6153 hr
= STG_E_PATHNOTFOUND
;
6156 case ERROR_ACCESS_DENIED
:
6157 case ERROR_WRITE_PROTECT
:
6158 hr
= STG_E_ACCESSDENIED
;
6161 case ERROR_SHARING_VIOLATION
:
6162 hr
= STG_E_SHAREVIOLATION
;
6173 * Refuse to open the file if it's too small to be a structured storage file
6174 * FIXME: verify the file when reading instead of here
6176 if (GetFileSize(hFile
, NULL
) < 0x100)
6179 hr
= STG_E_FILEALREADYEXISTS
;
6184 * Allocate and initialize the new IStorage32object.
6186 newStorage
= HeapAlloc(GetProcessHeap(), 0, sizeof(StorageImpl
));
6188 if (newStorage
== 0)
6190 hr
= STG_E_INSUFFICIENTMEMORY
;
6194 /* Initialize the storage */
6195 hr
= StorageImpl_Construct(
6206 HeapFree(GetProcessHeap(), 0, newStorage
);
6208 * According to the docs if the file is not a storage, return STG_E_FILEALREADYEXISTS
6210 if(hr
== STG_E_INVALIDHEADER
)
6211 hr
= STG_E_FILEALREADYEXISTS
;
6215 /* prepare the file name string given in lieu of the root property name */
6216 GetFullPathNameW(pwcsName
, MAX_PATH
, fullname
, NULL
);
6217 memcpy(newStorage
->filename
, fullname
, PROPERTY_NAME_BUFFER_LEN
);
6218 newStorage
->filename
[PROPERTY_NAME_BUFFER_LEN
-1] = '\0';
6221 * Get an "out" pointer for the caller.
6223 hr
= StorageBaseImpl_QueryInterface(
6224 (IStorage
*)newStorage
,
6225 (REFIID
)&IID_IStorage
,
6229 TRACE("<-- %08x, IStorage %p\n", hr
, ppstgOpen
? *ppstgOpen
: NULL
);
6233 /******************************************************************************
6234 * StgCreateDocfileOnILockBytes [OLE32.@]
6236 HRESULT WINAPI
StgCreateDocfileOnILockBytes(
6240 IStorage
** ppstgOpen
)
6242 StorageImpl
* newStorage
= 0;
6246 * Validate the parameters
6248 if ((ppstgOpen
== 0) || (plkbyt
== 0))
6249 return STG_E_INVALIDPOINTER
;
6252 * Allocate and initialize the new IStorage object.
6254 newStorage
= HeapAlloc(GetProcessHeap(), 0, sizeof(StorageImpl
));
6256 if (newStorage
== 0)
6257 return STG_E_INSUFFICIENTMEMORY
;
6259 hr
= StorageImpl_Construct(
6270 HeapFree(GetProcessHeap(), 0, newStorage
);
6275 * Get an "out" pointer for the caller.
6277 hr
= StorageBaseImpl_QueryInterface(
6278 (IStorage
*)newStorage
,
6279 (REFIID
)&IID_IStorage
,
6285 /******************************************************************************
6286 * StgOpenStorageOnILockBytes [OLE32.@]
6288 HRESULT WINAPI
StgOpenStorageOnILockBytes(
6290 IStorage
*pstgPriority
,
6294 IStorage
**ppstgOpen
)
6296 StorageImpl
* newStorage
= 0;
6300 * Perform a sanity check
6302 if ((plkbyt
== 0) || (ppstgOpen
== 0))
6303 return STG_E_INVALIDPOINTER
;
6306 * Validate the STGM flags
6308 if ( FAILED( validateSTGM(grfMode
) ))
6309 return STG_E_INVALIDFLAG
;
6312 * Initialize the "out" parameter.
6317 * Allocate and initialize the new IStorage object.
6319 newStorage
= HeapAlloc(GetProcessHeap(), 0, sizeof(StorageImpl
));
6321 if (newStorage
== 0)
6322 return STG_E_INSUFFICIENTMEMORY
;
6324 hr
= StorageImpl_Construct(
6335 HeapFree(GetProcessHeap(), 0, newStorage
);
6340 * Get an "out" pointer for the caller.
6342 hr
= StorageBaseImpl_QueryInterface(
6343 (IStorage
*)newStorage
,
6344 (REFIID
)&IID_IStorage
,
6350 /******************************************************************************
6351 * StgSetTimes [ole32.@]
6352 * StgSetTimes [OLE32.@]
6356 HRESULT WINAPI
StgSetTimes(OLECHAR
const *str
, FILETIME
const *pctime
,
6357 FILETIME
const *patime
, FILETIME
const *pmtime
)
6359 IStorage
*stg
= NULL
;
6362 TRACE("%s %p %p %p\n", debugstr_w(str
), pctime
, patime
, pmtime
);
6364 r
= StgOpenStorage(str
, NULL
, STGM_READWRITE
| STGM_SHARE_DENY_WRITE
,
6368 r
= IStorage_SetElementTimes(stg
, NULL
, pctime
, patime
, pmtime
);
6369 IStorage_Release(stg
);
6375 /******************************************************************************
6376 * StgIsStorageILockBytes [OLE32.@]
6378 * Determines if the ILockBytes contains a storage object.
6380 HRESULT WINAPI
StgIsStorageILockBytes(ILockBytes
*plkbyt
)
6383 ULARGE_INTEGER offset
;
6385 offset
.u
.HighPart
= 0;
6386 offset
.u
.LowPart
= 0;
6388 ILockBytes_ReadAt(plkbyt
, offset
, sig
, sizeof(sig
), NULL
);
6390 if (memcmp(sig
, STORAGE_magic
, sizeof(STORAGE_magic
)) == 0)
6396 /******************************************************************************
6397 * WriteClassStg [OLE32.@]
6399 * This method will store the specified CLSID in the specified storage object
6401 HRESULT WINAPI
WriteClassStg(IStorage
* pStg
, REFCLSID rclsid
)
6406 return E_INVALIDARG
;
6409 return STG_E_INVALIDPOINTER
;
6411 hRes
= IStorage_SetClass(pStg
, rclsid
);
6416 /***********************************************************************
6417 * ReadClassStg (OLE32.@)
6419 * This method reads the CLSID previously written to a storage object with
6420 * the WriteClassStg.
6423 * pstg [I] IStorage pointer
6424 * pclsid [O] Pointer to where the CLSID is written
6428 * Failure: HRESULT code.
6430 HRESULT WINAPI
ReadClassStg(IStorage
*pstg
,CLSID
*pclsid
){
6435 TRACE("(%p, %p)\n", pstg
, pclsid
);
6437 if(!pstg
|| !pclsid
)
6438 return E_INVALIDARG
;
6441 * read a STATSTG structure (contains the clsid) from the storage
6443 hRes
=IStorage_Stat(pstg
,&pstatstg
,STATFLAG_NONAME
);
6446 *pclsid
=pstatstg
.clsid
;
6451 /***********************************************************************
6452 * OleLoadFromStream (OLE32.@)
6454 * This function loads an object from stream
6456 HRESULT WINAPI
OleLoadFromStream(IStream
*pStm
,REFIID iidInterface
,void** ppvObj
)
6460 LPPERSISTSTREAM xstm
;
6462 TRACE("(%p,%s,%p)\n",pStm
,debugstr_guid(iidInterface
),ppvObj
);
6464 res
=ReadClassStm(pStm
,&clsid
);
6467 res
=CoCreateInstance(&clsid
,NULL
,CLSCTX_INPROC_SERVER
,iidInterface
,ppvObj
);
6470 res
=IUnknown_QueryInterface((IUnknown
*)*ppvObj
,&IID_IPersistStream
,(LPVOID
*)&xstm
);
6472 IUnknown_Release((IUnknown
*)*ppvObj
);
6475 res
=IPersistStream_Load(xstm
,pStm
);
6476 IPersistStream_Release(xstm
);
6477 /* FIXME: all refcounts ok at this point? I think they should be:
6480 * xstm : 0 (released)
6485 /***********************************************************************
6486 * OleSaveToStream (OLE32.@)
6488 * This function saves an object with the IPersistStream interface on it
6489 * to the specified stream.
6491 HRESULT WINAPI
OleSaveToStream(IPersistStream
*pPStm
,IStream
*pStm
)
6497 TRACE("(%p,%p)\n",pPStm
,pStm
);
6499 res
=IPersistStream_GetClassID(pPStm
,&clsid
);
6501 if (SUCCEEDED(res
)){
6503 res
=WriteClassStm(pStm
,&clsid
);
6507 res
=IPersistStream_Save(pPStm
,pStm
,TRUE
);
6510 TRACE("Finished Save\n");
6514 /****************************************************************************
6515 * This method validate a STGM parameter that can contain the values below
6517 * The stgm modes in 0x0000ffff are not bit masks, but distinct 4 bit values.
6518 * The stgm values contained in 0xffff0000 are bitmasks.
6520 * STGM_DIRECT 0x00000000
6521 * STGM_TRANSACTED 0x00010000
6522 * STGM_SIMPLE 0x08000000
6524 * STGM_READ 0x00000000
6525 * STGM_WRITE 0x00000001
6526 * STGM_READWRITE 0x00000002
6528 * STGM_SHARE_DENY_NONE 0x00000040
6529 * STGM_SHARE_DENY_READ 0x00000030
6530 * STGM_SHARE_DENY_WRITE 0x00000020
6531 * STGM_SHARE_EXCLUSIVE 0x00000010
6533 * STGM_PRIORITY 0x00040000
6534 * STGM_DELETEONRELEASE 0x04000000
6536 * STGM_CREATE 0x00001000
6537 * STGM_CONVERT 0x00020000
6538 * STGM_FAILIFTHERE 0x00000000
6540 * STGM_NOSCRATCH 0x00100000
6541 * STGM_NOSNAPSHOT 0x00200000
6543 static HRESULT
validateSTGM(DWORD stgm
)
6545 DWORD access
= STGM_ACCESS_MODE(stgm
);
6546 DWORD share
= STGM_SHARE_MODE(stgm
);
6547 DWORD create
= STGM_CREATE_MODE(stgm
);
6549 if (stgm
&~STGM_KNOWN_FLAGS
)
6551 ERR("unknown flags %08x\n", stgm
);
6559 case STGM_READWRITE
:
6567 case STGM_SHARE_DENY_NONE
:
6568 case STGM_SHARE_DENY_READ
:
6569 case STGM_SHARE_DENY_WRITE
:
6570 case STGM_SHARE_EXCLUSIVE
:
6579 case STGM_FAILIFTHERE
:
6586 * STGM_DIRECT | STGM_TRANSACTED | STGM_SIMPLE
6588 if ( (stgm
& STGM_TRANSACTED
) && (stgm
& STGM_SIMPLE
) )
6592 * STGM_CREATE | STGM_CONVERT
6593 * if both are false, STGM_FAILIFTHERE is set to TRUE
6595 if ( create
== STGM_CREATE
&& (stgm
& STGM_CONVERT
) )
6599 * STGM_NOSCRATCH requires STGM_TRANSACTED
6601 if ( (stgm
& STGM_NOSCRATCH
) && !(stgm
& STGM_TRANSACTED
) )
6605 * STGM_NOSNAPSHOT requires STGM_TRANSACTED and
6606 * not STGM_SHARE_EXCLUSIVE or STGM_SHARE_DENY_WRITE`
6608 if ( (stgm
& STGM_NOSNAPSHOT
) &&
6609 (!(stgm
& STGM_TRANSACTED
) ||
6610 share
== STGM_SHARE_EXCLUSIVE
||
6611 share
== STGM_SHARE_DENY_WRITE
) )
6617 /****************************************************************************
6618 * GetShareModeFromSTGM
6620 * This method will return a share mode flag from a STGM value.
6621 * The STGM value is assumed valid.
6623 static DWORD
GetShareModeFromSTGM(DWORD stgm
)
6625 switch (STGM_SHARE_MODE(stgm
))
6627 case STGM_SHARE_DENY_NONE
:
6628 return FILE_SHARE_READ
| FILE_SHARE_WRITE
;
6629 case STGM_SHARE_DENY_READ
:
6630 return FILE_SHARE_WRITE
;
6631 case STGM_SHARE_DENY_WRITE
:
6632 return FILE_SHARE_READ
;
6633 case STGM_SHARE_EXCLUSIVE
:
6636 ERR("Invalid share mode!\n");
6641 /****************************************************************************
6642 * GetAccessModeFromSTGM
6644 * This method will return an access mode flag from a STGM value.
6645 * The STGM value is assumed valid.
6647 static DWORD
GetAccessModeFromSTGM(DWORD stgm
)
6649 switch (STGM_ACCESS_MODE(stgm
))
6652 return GENERIC_READ
;
6654 case STGM_READWRITE
:
6655 return GENERIC_READ
| GENERIC_WRITE
;
6657 ERR("Invalid access mode!\n");
6662 /****************************************************************************
6663 * GetCreationModeFromSTGM
6665 * This method will return a creation mode flag from a STGM value.
6666 * The STGM value is assumed valid.
6668 static DWORD
GetCreationModeFromSTGM(DWORD stgm
)
6670 switch(STGM_CREATE_MODE(stgm
))
6673 return CREATE_ALWAYS
;
6675 FIXME("STGM_CONVERT not implemented!\n");
6677 case STGM_FAILIFTHERE
:
6680 ERR("Invalid create mode!\n");
6686 /*************************************************************************
6687 * OLECONVERT_LoadOLE10 [Internal]
6689 * Loads the OLE10 STREAM to memory
6692 * pOleStream [I] The OLESTREAM
6693 * pData [I] Data Structure for the OLESTREAM Data
6697 * Failure: CONVERT10_E_OLESTREAM_GET for invalid Get
6698 * CONVERT10_E_OLESTREAM_FMT if the OLEID is invalid
6701 * This function is used by OleConvertOLESTREAMToIStorage only.
6703 * Memory allocated for pData must be freed by the caller
6705 static HRESULT
OLECONVERT_LoadOLE10(LPOLESTREAM pOleStream
, OLECONVERT_OLESTREAM_DATA
*pData
, BOOL bStrem1
)
6708 HRESULT hRes
= S_OK
;
6712 pData
->pData
= NULL
;
6713 pData
->pstrOleObjFileName
= NULL
;
6715 for( nTryCnt
=0;nTryCnt
< max_try
; nTryCnt
++)
6718 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, (void *)&(pData
->dwOleID
), sizeof(pData
->dwOleID
));
6719 if(dwSize
!= sizeof(pData
->dwOleID
))
6721 hRes
= CONVERT10_E_OLESTREAM_GET
;
6723 else if(pData
->dwOleID
!= OLESTREAM_ID
)
6725 hRes
= CONVERT10_E_OLESTREAM_FMT
;
6736 /* Get the TypeID... more info needed for this field */
6737 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, (void *)&(pData
->dwTypeID
), sizeof(pData
->dwTypeID
));
6738 if(dwSize
!= sizeof(pData
->dwTypeID
))
6740 hRes
= CONVERT10_E_OLESTREAM_GET
;
6745 if(pData
->dwTypeID
!= 0)
6747 /* Get the length of the OleTypeName */
6748 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, (void *) &(pData
->dwOleTypeNameLength
), sizeof(pData
->dwOleTypeNameLength
));
6749 if(dwSize
!= sizeof(pData
->dwOleTypeNameLength
))
6751 hRes
= CONVERT10_E_OLESTREAM_GET
;
6756 if(pData
->dwOleTypeNameLength
> 0)
6758 /* Get the OleTypeName */
6759 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, pData
->strOleTypeName
, pData
->dwOleTypeNameLength
);
6760 if(dwSize
!= pData
->dwOleTypeNameLength
)
6762 hRes
= CONVERT10_E_OLESTREAM_GET
;
6768 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, (void *)&(pData
->dwOleObjFileNameLength
), sizeof(pData
->dwOleObjFileNameLength
));
6769 if(dwSize
!= sizeof(pData
->dwOleObjFileNameLength
))
6771 hRes
= CONVERT10_E_OLESTREAM_GET
;
6775 if(pData
->dwOleObjFileNameLength
< 1) /* there is no file name exist */
6776 pData
->dwOleObjFileNameLength
= sizeof(pData
->dwOleObjFileNameLength
);
6777 pData
->pstrOleObjFileName
= HeapAlloc(GetProcessHeap(), 0, pData
->dwOleObjFileNameLength
);
6778 if(pData
->pstrOleObjFileName
)
6780 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, pData
->pstrOleObjFileName
, pData
->dwOleObjFileNameLength
);
6781 if(dwSize
!= pData
->dwOleObjFileNameLength
)
6783 hRes
= CONVERT10_E_OLESTREAM_GET
;
6787 hRes
= CONVERT10_E_OLESTREAM_GET
;
6792 /* Get the Width of the Metafile */
6793 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, (void *)&(pData
->dwMetaFileWidth
), sizeof(pData
->dwMetaFileWidth
));
6794 if(dwSize
!= sizeof(pData
->dwMetaFileWidth
))
6796 hRes
= CONVERT10_E_OLESTREAM_GET
;
6800 /* Get the Height of the Metafile */
6801 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, (void *)&(pData
->dwMetaFileHeight
), sizeof(pData
->dwMetaFileHeight
));
6802 if(dwSize
!= sizeof(pData
->dwMetaFileHeight
))
6804 hRes
= CONVERT10_E_OLESTREAM_GET
;
6810 /* Get the Length of the Data */
6811 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, (void *)&(pData
->dwDataLength
), sizeof(pData
->dwDataLength
));
6812 if(dwSize
!= sizeof(pData
->dwDataLength
))
6814 hRes
= CONVERT10_E_OLESTREAM_GET
;
6818 if(hRes
== S_OK
) /* I don't know what this 8 byte information is. We have to figure out */
6820 if(!bStrem1
) /* if it is a second OLE stream data */
6822 pData
->dwDataLength
-= 8;
6823 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, pData
->strUnknown
, sizeof(pData
->strUnknown
));
6824 if(dwSize
!= sizeof(pData
->strUnknown
))
6826 hRes
= CONVERT10_E_OLESTREAM_GET
;
6832 if(pData
->dwDataLength
> 0)
6834 pData
->pData
= HeapAlloc(GetProcessHeap(),0,pData
->dwDataLength
);
6836 /* Get Data (ex. IStorage, Metafile, or BMP) */
6839 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, (void *)pData
->pData
, pData
->dwDataLength
);
6840 if(dwSize
!= pData
->dwDataLength
)
6842 hRes
= CONVERT10_E_OLESTREAM_GET
;
6847 hRes
= CONVERT10_E_OLESTREAM_GET
;
6856 /*************************************************************************
6857 * OLECONVERT_SaveOLE10 [Internal]
6859 * Saves the OLE10 STREAM From memory
6862 * pData [I] Data Structure for the OLESTREAM Data
6863 * pOleStream [I] The OLESTREAM to save
6867 * Failure: CONVERT10_E_OLESTREAM_PUT for invalid Put
6870 * This function is used by OleConvertIStorageToOLESTREAM only.
6873 static HRESULT
OLECONVERT_SaveOLE10(OLECONVERT_OLESTREAM_DATA
*pData
, LPOLESTREAM pOleStream
)
6876 HRESULT hRes
= S_OK
;
6880 dwSize
= pOleStream
->lpstbl
->Put(pOleStream
, (void *)&(pData
->dwOleID
), sizeof(pData
->dwOleID
));
6881 if(dwSize
!= sizeof(pData
->dwOleID
))
6883 hRes
= CONVERT10_E_OLESTREAM_PUT
;
6888 /* Set the TypeID */
6889 dwSize
= pOleStream
->lpstbl
->Put(pOleStream
, (void *)&(pData
->dwTypeID
), sizeof(pData
->dwTypeID
));
6890 if(dwSize
!= sizeof(pData
->dwTypeID
))
6892 hRes
= CONVERT10_E_OLESTREAM_PUT
;
6896 if(pData
->dwOleID
== OLESTREAM_ID
&& pData
->dwTypeID
!= 0 && hRes
== S_OK
)
6898 /* Set the Length of the OleTypeName */
6899 dwSize
= pOleStream
->lpstbl
->Put(pOleStream
, (void *)&(pData
->dwOleTypeNameLength
), sizeof(pData
->dwOleTypeNameLength
));
6900 if(dwSize
!= sizeof(pData
->dwOleTypeNameLength
))
6902 hRes
= CONVERT10_E_OLESTREAM_PUT
;
6907 if(pData
->dwOleTypeNameLength
> 0)
6909 /* Set the OleTypeName */
6910 dwSize
= pOleStream
->lpstbl
->Put(pOleStream
, pData
->strOleTypeName
, pData
->dwOleTypeNameLength
);
6911 if(dwSize
!= pData
->dwOleTypeNameLength
)
6913 hRes
= CONVERT10_E_OLESTREAM_PUT
;
6920 /* Set the width of the Metafile */
6921 dwSize
= pOleStream
->lpstbl
->Put(pOleStream
, (void *)&(pData
->dwMetaFileWidth
), sizeof(pData
->dwMetaFileWidth
));
6922 if(dwSize
!= sizeof(pData
->dwMetaFileWidth
))
6924 hRes
= CONVERT10_E_OLESTREAM_PUT
;
6930 /* Set the height of the Metafile */
6931 dwSize
= pOleStream
->lpstbl
->Put(pOleStream
, (void *)&(pData
->dwMetaFileHeight
), sizeof(pData
->dwMetaFileHeight
));
6932 if(dwSize
!= sizeof(pData
->dwMetaFileHeight
))
6934 hRes
= CONVERT10_E_OLESTREAM_PUT
;
6940 /* Set the length of the Data */
6941 dwSize
= pOleStream
->lpstbl
->Put(pOleStream
, (void *)&(pData
->dwDataLength
), sizeof(pData
->dwDataLength
));
6942 if(dwSize
!= sizeof(pData
->dwDataLength
))
6944 hRes
= CONVERT10_E_OLESTREAM_PUT
;
6950 if(pData
->dwDataLength
> 0)
6952 /* Set the Data (eg. IStorage, Metafile, Bitmap) */
6953 dwSize
= pOleStream
->lpstbl
->Put(pOleStream
, (void *) pData
->pData
, pData
->dwDataLength
);
6954 if(dwSize
!= pData
->dwDataLength
)
6956 hRes
= CONVERT10_E_OLESTREAM_PUT
;
6964 /*************************************************************************
6965 * OLECONVERT_GetOLE20FromOLE10[Internal]
6967 * This function copies OLE10 Data (the IStorage in the OLESTREAM) to disk,
6968 * opens it, and copies the content to the dest IStorage for
6969 * OleConvertOLESTREAMToIStorage
6973 * pDestStorage [I] The IStorage to copy the data to
6974 * pBuffer [I] Buffer that contains the IStorage from the OLESTREAM
6975 * nBufferLength [I] The size of the buffer
6984 static void OLECONVERT_GetOLE20FromOLE10(LPSTORAGE pDestStorage
, const BYTE
*pBuffer
, DWORD nBufferLength
)
6988 IStorage
*pTempStorage
;
6989 DWORD dwNumOfBytesWritten
;
6990 WCHAR wstrTempDir
[MAX_PATH
], wstrTempFile
[MAX_PATH
];
6991 static const WCHAR wstrPrefix
[] = {'s', 'i', 's', 0};
6993 /* Create a temp File */
6994 GetTempPathW(MAX_PATH
, wstrTempDir
);
6995 GetTempFileNameW(wstrTempDir
, wstrPrefix
, 0, wstrTempFile
);
6996 hFile
= CreateFileW(wstrTempFile
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
6998 if(hFile
!= INVALID_HANDLE_VALUE
)
7000 /* Write IStorage Data to File */
7001 WriteFile(hFile
, pBuffer
, nBufferLength
, &dwNumOfBytesWritten
, NULL
);
7004 /* Open and copy temp storage to the Dest Storage */
7005 hRes
= StgOpenStorage(wstrTempFile
, NULL
, STGM_READ
, NULL
, 0, &pTempStorage
);
7008 hRes
= StorageImpl_CopyTo(pTempStorage
, 0, NULL
, NULL
, pDestStorage
);
7009 StorageBaseImpl_Release(pTempStorage
);
7011 DeleteFileW(wstrTempFile
);
7016 /*************************************************************************
7017 * OLECONVERT_WriteOLE20ToBuffer [Internal]
7019 * Saves the OLE10 STREAM From memory
7022 * pStorage [I] The Src IStorage to copy
7023 * pData [I] The Dest Memory to write to.
7026 * The size in bytes allocated for pData
7029 * Memory allocated for pData must be freed by the caller
7031 * Used by OleConvertIStorageToOLESTREAM only.
7034 static DWORD
OLECONVERT_WriteOLE20ToBuffer(LPSTORAGE pStorage
, BYTE
**pData
)
7038 DWORD nDataLength
= 0;
7039 IStorage
*pTempStorage
;
7040 WCHAR wstrTempDir
[MAX_PATH
], wstrTempFile
[MAX_PATH
];
7041 static const WCHAR wstrPrefix
[] = {'s', 'i', 's', 0};
7045 /* Create temp Storage */
7046 GetTempPathW(MAX_PATH
, wstrTempDir
);
7047 GetTempFileNameW(wstrTempDir
, wstrPrefix
, 0, wstrTempFile
);
7048 hRes
= StgCreateDocfile(wstrTempFile
, STGM_CREATE
| STGM_READWRITE
| STGM_SHARE_EXCLUSIVE
, 0, &pTempStorage
);
7052 /* Copy Src Storage to the Temp Storage */
7053 StorageImpl_CopyTo(pStorage
, 0, NULL
, NULL
, pTempStorage
);
7054 StorageBaseImpl_Release(pTempStorage
);
7056 /* Open Temp Storage as a file and copy to memory */
7057 hFile
= CreateFileW(wstrTempFile
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
7058 if(hFile
!= INVALID_HANDLE_VALUE
)
7060 nDataLength
= GetFileSize(hFile
, NULL
);
7061 *pData
= HeapAlloc(GetProcessHeap(),0,nDataLength
);
7062 ReadFile(hFile
, *pData
, nDataLength
, &nDataLength
, 0);
7065 DeleteFileW(wstrTempFile
);
7070 /*************************************************************************
7071 * OLECONVERT_CreateOleStream [Internal]
7073 * Creates the "\001OLE" stream in the IStorage if necessary.
7076 * pStorage [I] Dest storage to create the stream in
7082 * This function is used by OleConvertOLESTREAMToIStorage only.
7084 * This stream is still unknown, MS Word seems to have extra data
7085 * but since the data is stored in the OLESTREAM there should be
7086 * no need to recreate the stream. If the stream is manually
7087 * deleted it will create it with this default data.
7090 void OLECONVERT_CreateOleStream(LPSTORAGE pStorage
)
7094 static const WCHAR wstrStreamName
[] = {1,'O', 'l', 'e', 0};
7095 BYTE pOleStreamHeader
[] =
7097 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
7098 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7099 0x00, 0x00, 0x00, 0x00
7102 /* Create stream if not present */
7103 hRes
= IStorage_CreateStream(pStorage
, wstrStreamName
,
7104 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &pStream
);
7108 /* Write default Data */
7109 hRes
= IStream_Write(pStream
, pOleStreamHeader
, sizeof(pOleStreamHeader
), NULL
);
7110 IStream_Release(pStream
);
7114 /* write a string to a stream, preceded by its length */
7115 static HRESULT
STREAM_WriteString( IStream
*stm
, LPCWSTR string
)
7122 len
= WideCharToMultiByte( CP_ACP
, 0, string
, -1, NULL
, 0, NULL
, NULL
);
7123 r
= IStream_Write( stm
, &len
, sizeof(len
), NULL
);
7128 str
= CoTaskMemAlloc( len
);
7129 WideCharToMultiByte( CP_ACP
, 0, string
, -1, str
, len
, NULL
, NULL
);
7130 r
= IStream_Write( stm
, str
, len
, NULL
);
7131 CoTaskMemFree( str
);
7135 /* read a string preceded by its length from a stream */
7136 static HRESULT
STREAM_ReadString( IStream
*stm
, LPWSTR
*string
)
7139 DWORD len
, count
= 0;
7143 r
= IStream_Read( stm
, &len
, sizeof(len
), &count
);
7146 if( count
!= sizeof(len
) )
7147 return E_OUTOFMEMORY
;
7149 TRACE("%d bytes\n",len
);
7151 str
= CoTaskMemAlloc( len
);
7153 return E_OUTOFMEMORY
;
7155 r
= IStream_Read( stm
, str
, len
, &count
);
7160 CoTaskMemFree( str
);
7161 return E_OUTOFMEMORY
;
7164 TRACE("Read string %s\n",debugstr_an(str
,len
));
7166 len
= MultiByteToWideChar( CP_ACP
, 0, str
, count
, NULL
, 0 );
7167 wstr
= CoTaskMemAlloc( (len
+ 1)*sizeof (WCHAR
) );
7169 MultiByteToWideChar( CP_ACP
, 0, str
, count
, wstr
, len
);
7170 CoTaskMemFree( str
);
7178 static HRESULT
STORAGE_WriteCompObj( LPSTORAGE pstg
, CLSID
*clsid
,
7179 LPCWSTR lpszUserType
, LPCWSTR szClipName
, LPCWSTR szProgIDName
)
7183 static const WCHAR szwStreamName
[] = {1, 'C', 'o', 'm', 'p', 'O', 'b', 'j', 0};
7185 static const BYTE unknown1
[12] =
7186 { 0x01, 0x00, 0xFE, 0xFF, 0x03, 0x0A, 0x00, 0x00,
7187 0xFF, 0xFF, 0xFF, 0xFF};
7188 static const BYTE unknown2
[16] =
7189 { 0xF4, 0x39, 0xB2, 0x71, 0x00, 0x00, 0x00, 0x00,
7190 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
7192 TRACE("%p %s %s %s %s\n", pstg
, debugstr_guid(clsid
),
7193 debugstr_w(lpszUserType
), debugstr_w(szClipName
),
7194 debugstr_w(szProgIDName
));
7196 /* Create a CompObj stream */
7197 r
= IStorage_CreateStream(pstg
, szwStreamName
,
7198 STGM_CREATE
| STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &pstm
);
7202 /* Write CompObj Structure to stream */
7203 r
= IStream_Write(pstm
, unknown1
, sizeof(unknown1
), NULL
);
7205 if( SUCCEEDED( r
) )
7206 r
= WriteClassStm( pstm
, clsid
);
7208 if( SUCCEEDED( r
) )
7209 r
= STREAM_WriteString( pstm
, lpszUserType
);
7210 if( SUCCEEDED( r
) )
7211 r
= STREAM_WriteString( pstm
, szClipName
);
7212 if( SUCCEEDED( r
) )
7213 r
= STREAM_WriteString( pstm
, szProgIDName
);
7214 if( SUCCEEDED( r
) )
7215 r
= IStream_Write(pstm
, unknown2
, sizeof(unknown2
), NULL
);
7217 IStream_Release( pstm
);
7222 /***********************************************************************
7223 * WriteFmtUserTypeStg (OLE32.@)
7225 HRESULT WINAPI
WriteFmtUserTypeStg(
7226 LPSTORAGE pstg
, CLIPFORMAT cf
, LPOLESTR lpszUserType
)
7229 WCHAR szwClipName
[0x40];
7230 CLSID clsid
= CLSID_NULL
;
7231 LPWSTR wstrProgID
= NULL
;
7234 TRACE("(%p,%x,%s)\n",pstg
,cf
,debugstr_w(lpszUserType
));
7236 /* get the clipboard format name */
7237 n
= GetClipboardFormatNameW( cf
, szwClipName
, sizeof(szwClipName
)/sizeof(szwClipName
[0]) );
7240 TRACE("Clipboard name is %s\n", debugstr_w(szwClipName
));
7242 /* FIXME: There's room to save a CLSID and its ProgID, but
7243 the CLSID is not looked up in the registry and in all the
7244 tests I wrote it was CLSID_NULL. Where does it come from?
7247 /* get the real program ID. This may fail, but that's fine */
7248 ProgIDFromCLSID(&clsid
, &wstrProgID
);
7250 TRACE("progid is %s\n",debugstr_w(wstrProgID
));
7252 r
= STORAGE_WriteCompObj( pstg
, &clsid
,
7253 lpszUserType
, szwClipName
, wstrProgID
);
7255 CoTaskMemFree(wstrProgID
);
7261 /******************************************************************************
7262 * ReadFmtUserTypeStg [OLE32.@]
7264 HRESULT WINAPI
ReadFmtUserTypeStg (LPSTORAGE pstg
, CLIPFORMAT
* pcf
, LPOLESTR
* lplpszUserType
)
7268 static const WCHAR szCompObj
[] = { 1, 'C','o','m','p','O','b','j', 0 };
7269 unsigned char unknown1
[12];
7270 unsigned char unknown2
[16];
7272 LPWSTR szProgIDName
= NULL
, szCLSIDName
= NULL
, szOleTypeName
= NULL
;
7275 TRACE("(%p,%p,%p)\n", pstg
, pcf
, lplpszUserType
);
7277 r
= IStorage_OpenStream( pstg
, szCompObj
, NULL
,
7278 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
7281 WARN("Failed to open stream r = %08x\n", r
);
7285 /* read the various parts of the structure */
7286 r
= IStream_Read( stm
, unknown1
, sizeof(unknown1
), &count
);
7287 if( FAILED( r
) || ( count
!= sizeof(unknown1
) ) )
7289 r
= ReadClassStm( stm
, &clsid
);
7293 r
= STREAM_ReadString( stm
, &szCLSIDName
);
7297 r
= STREAM_ReadString( stm
, &szOleTypeName
);
7301 r
= STREAM_ReadString( stm
, &szProgIDName
);
7305 r
= IStream_Read( stm
, unknown2
, sizeof(unknown2
), &count
);
7306 if( FAILED( r
) || ( count
!= sizeof(unknown2
) ) )
7309 /* ok, success... now we just need to store what we found */
7311 *pcf
= RegisterClipboardFormatW( szOleTypeName
);
7312 CoTaskMemFree( szOleTypeName
);
7314 if( lplpszUserType
)
7315 *lplpszUserType
= szCLSIDName
;
7316 CoTaskMemFree( szProgIDName
);
7319 IStream_Release( stm
);
7325 /*************************************************************************
7326 * OLECONVERT_CreateCompObjStream [Internal]
7328 * Creates a "\001CompObj" is the destination IStorage if necessary.
7331 * pStorage [I] The dest IStorage to create the CompObj Stream
7333 * strOleTypeName [I] The ProgID
7337 * Failure: REGDB_E_CLASSNOTREG if cannot reconstruct the stream
7340 * This function is used by OleConvertOLESTREAMToIStorage only.
7342 * The stream data is stored in the OLESTREAM and there should be
7343 * no need to recreate the stream. If the stream is manually
7344 * deleted it will attempt to create it by querying the registry.
7348 HRESULT
OLECONVERT_CreateCompObjStream(LPSTORAGE pStorage
, LPCSTR strOleTypeName
)
7351 HRESULT hStorageRes
, hRes
= S_OK
;
7352 OLECONVERT_ISTORAGE_COMPOBJ IStorageCompObj
;
7353 static const WCHAR wstrStreamName
[] = {1,'C', 'o', 'm', 'p', 'O', 'b', 'j', 0};
7354 WCHAR bufferW
[OLESTREAM_MAX_STR_LEN
];
7356 BYTE pCompObjUnknown1
[] = {0x01, 0x00, 0xFE, 0xFF, 0x03, 0x0A, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF};
7357 BYTE pCompObjUnknown2
[] = {0xF4, 0x39, 0xB2, 0x71};
7359 /* Initialize the CompObj structure */
7360 memset(&IStorageCompObj
, 0, sizeof(IStorageCompObj
));
7361 memcpy(IStorageCompObj
.byUnknown1
, pCompObjUnknown1
, sizeof(pCompObjUnknown1
));
7362 memcpy(IStorageCompObj
.byUnknown2
, pCompObjUnknown2
, sizeof(pCompObjUnknown2
));
7365 /* Create a CompObj stream if it doesn't exist */
7366 hStorageRes
= IStorage_CreateStream(pStorage
, wstrStreamName
,
7367 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &pStream
);
7368 if(hStorageRes
== S_OK
)
7370 /* copy the OleTypeName to the compobj struct */
7371 IStorageCompObj
.dwOleTypeNameLength
= strlen(strOleTypeName
)+1;
7372 strcpy(IStorageCompObj
.strOleTypeName
, strOleTypeName
);
7374 /* copy the OleTypeName to the compobj struct */
7375 /* Note: in the test made, these were Identical */
7376 IStorageCompObj
.dwProgIDNameLength
= strlen(strOleTypeName
)+1;
7377 strcpy(IStorageCompObj
.strProgIDName
, strOleTypeName
);
7380 MultiByteToWideChar( CP_ACP
, 0, IStorageCompObj
.strProgIDName
, -1,
7381 bufferW
, OLESTREAM_MAX_STR_LEN
);
7382 hRes
= CLSIDFromProgID(bufferW
, &(IStorageCompObj
.clsid
));
7388 /* Get the CLSID Default Name from the Registry */
7389 hErr
= RegOpenKeyA(HKEY_CLASSES_ROOT
, IStorageCompObj
.strProgIDName
, &hKey
);
7390 if(hErr
== ERROR_SUCCESS
)
7392 char strTemp
[OLESTREAM_MAX_STR_LEN
];
7393 IStorageCompObj
.dwCLSIDNameLength
= OLESTREAM_MAX_STR_LEN
;
7394 hErr
= RegQueryValueA(hKey
, NULL
, strTemp
, (LONG
*) &(IStorageCompObj
.dwCLSIDNameLength
));
7395 if(hErr
== ERROR_SUCCESS
)
7397 strcpy(IStorageCompObj
.strCLSIDName
, strTemp
);
7403 /* Write CompObj Structure to stream */
7404 hRes
= IStream_Write(pStream
, IStorageCompObj
.byUnknown1
, sizeof(IStorageCompObj
.byUnknown1
), NULL
);
7406 WriteClassStm(pStream
,&(IStorageCompObj
.clsid
));
7408 hRes
= IStream_Write(pStream
, &(IStorageCompObj
.dwCLSIDNameLength
), sizeof(IStorageCompObj
.dwCLSIDNameLength
), NULL
);
7409 if(IStorageCompObj
.dwCLSIDNameLength
> 0)
7411 hRes
= IStream_Write(pStream
, IStorageCompObj
.strCLSIDName
, IStorageCompObj
.dwCLSIDNameLength
, NULL
);
7413 hRes
= IStream_Write(pStream
, &(IStorageCompObj
.dwOleTypeNameLength
) , sizeof(IStorageCompObj
.dwOleTypeNameLength
), NULL
);
7414 if(IStorageCompObj
.dwOleTypeNameLength
> 0)
7416 hRes
= IStream_Write(pStream
, IStorageCompObj
.strOleTypeName
, IStorageCompObj
.dwOleTypeNameLength
, NULL
);
7418 hRes
= IStream_Write(pStream
, &(IStorageCompObj
.dwProgIDNameLength
) , sizeof(IStorageCompObj
.dwProgIDNameLength
), NULL
);
7419 if(IStorageCompObj
.dwProgIDNameLength
> 0)
7421 hRes
= IStream_Write(pStream
, IStorageCompObj
.strProgIDName
, IStorageCompObj
.dwProgIDNameLength
, NULL
);
7423 hRes
= IStream_Write(pStream
, IStorageCompObj
.byUnknown2
, sizeof(IStorageCompObj
.byUnknown2
), NULL
);
7424 IStream_Release(pStream
);
7430 /*************************************************************************
7431 * OLECONVERT_CreateOlePresStream[Internal]
7433 * Creates the "\002OlePres000" Stream with the Metafile data
7436 * pStorage [I] The dest IStorage to create \002OLEPres000 stream in.
7437 * dwExtentX [I] Width of the Metafile
7438 * dwExtentY [I] Height of the Metafile
7439 * pData [I] Metafile data
7440 * dwDataLength [I] Size of the Metafile data
7444 * Failure: CONVERT10_E_OLESTREAM_PUT for invalid Put
7447 * This function is used by OleConvertOLESTREAMToIStorage only.
7450 static void OLECONVERT_CreateOlePresStream(LPSTORAGE pStorage
, DWORD dwExtentX
, DWORD dwExtentY
, BYTE
*pData
, DWORD dwDataLength
)
7454 static const WCHAR wstrStreamName
[] = {2, 'O', 'l', 'e', 'P', 'r', 'e', 's', '0', '0', '0', 0};
7455 BYTE pOlePresStreamHeader
[] =
7457 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0x00, 0x00, 0x00,
7458 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
7459 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
7460 0x00, 0x00, 0x00, 0x00
7463 BYTE pOlePresStreamHeaderEmpty
[] =
7465 0x00, 0x00, 0x00, 0x00,
7466 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
7467 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
7468 0x00, 0x00, 0x00, 0x00
7471 /* Create the OlePres000 Stream */
7472 hRes
= IStorage_CreateStream(pStorage
, wstrStreamName
,
7473 STGM_CREATE
| STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &pStream
);
7478 OLECONVERT_ISTORAGE_OLEPRES OlePres
;
7480 memset(&OlePres
, 0, sizeof(OlePres
));
7481 /* Do we have any metafile data to save */
7482 if(dwDataLength
> 0)
7484 memcpy(OlePres
.byUnknown1
, pOlePresStreamHeader
, sizeof(pOlePresStreamHeader
));
7485 nHeaderSize
= sizeof(pOlePresStreamHeader
);
7489 memcpy(OlePres
.byUnknown1
, pOlePresStreamHeaderEmpty
, sizeof(pOlePresStreamHeaderEmpty
));
7490 nHeaderSize
= sizeof(pOlePresStreamHeaderEmpty
);
7492 /* Set width and height of the metafile */
7493 OlePres
.dwExtentX
= dwExtentX
;
7494 OlePres
.dwExtentY
= -dwExtentY
;
7496 /* Set Data and Length */
7497 if(dwDataLength
> sizeof(METAFILEPICT16
))
7499 OlePres
.dwSize
= dwDataLength
- sizeof(METAFILEPICT16
);
7500 OlePres
.pData
= &(pData
[8]);
7502 /* Save OlePres000 Data to Stream */
7503 hRes
= IStream_Write(pStream
, OlePres
.byUnknown1
, nHeaderSize
, NULL
);
7504 hRes
= IStream_Write(pStream
, &(OlePres
.dwExtentX
), sizeof(OlePres
.dwExtentX
), NULL
);
7505 hRes
= IStream_Write(pStream
, &(OlePres
.dwExtentY
), sizeof(OlePres
.dwExtentY
), NULL
);
7506 hRes
= IStream_Write(pStream
, &(OlePres
.dwSize
), sizeof(OlePres
.dwSize
), NULL
);
7507 if(OlePres
.dwSize
> 0)
7509 hRes
= IStream_Write(pStream
, OlePres
.pData
, OlePres
.dwSize
, NULL
);
7511 IStream_Release(pStream
);
7515 /*************************************************************************
7516 * OLECONVERT_CreateOle10NativeStream [Internal]
7518 * Creates the "\001Ole10Native" Stream (should contain a BMP)
7521 * pStorage [I] Dest storage to create the stream in
7522 * pData [I] Ole10 Native Data (ex. bmp)
7523 * dwDataLength [I] Size of the Ole10 Native Data
7529 * This function is used by OleConvertOLESTREAMToIStorage only.
7531 * Might need to verify the data and return appropriate error message
7534 static void OLECONVERT_CreateOle10NativeStream(LPSTORAGE pStorage
, const BYTE
*pData
, DWORD dwDataLength
)
7538 static const WCHAR wstrStreamName
[] = {1, 'O', 'l', 'e', '1', '0', 'N', 'a', 't', 'i', 'v', 'e', 0};
7540 /* Create the Ole10Native Stream */
7541 hRes
= IStorage_CreateStream(pStorage
, wstrStreamName
,
7542 STGM_CREATE
| STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &pStream
);
7546 /* Write info to stream */
7547 hRes
= IStream_Write(pStream
, &dwDataLength
, sizeof(dwDataLength
), NULL
);
7548 hRes
= IStream_Write(pStream
, pData
, dwDataLength
, NULL
);
7549 IStream_Release(pStream
);
7554 /*************************************************************************
7555 * OLECONVERT_GetOLE10ProgID [Internal]
7557 * Finds the ProgID (or OleTypeID) from the IStorage
7560 * pStorage [I] The Src IStorage to get the ProgID
7561 * strProgID [I] the ProgID string to get
7562 * dwSize [I] the size of the string
7566 * Failure: REGDB_E_CLASSNOTREG if cannot reconstruct the stream
7569 * This function is used by OleConvertIStorageToOLESTREAM only.
7573 static HRESULT
OLECONVERT_GetOLE10ProgID(LPSTORAGE pStorage
, char *strProgID
, DWORD
*dwSize
)
7577 LARGE_INTEGER iSeekPos
;
7578 OLECONVERT_ISTORAGE_COMPOBJ CompObj
;
7579 static const WCHAR wstrStreamName
[] = {1,'C', 'o', 'm', 'p', 'O', 'b', 'j', 0};
7581 /* Open the CompObj Stream */
7582 hRes
= IStorage_OpenStream(pStorage
, wstrStreamName
, NULL
,
7583 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &pStream
);
7587 /*Get the OleType from the CompObj Stream */
7588 iSeekPos
.u
.LowPart
= sizeof(CompObj
.byUnknown1
) + sizeof(CompObj
.clsid
);
7589 iSeekPos
.u
.HighPart
= 0;
7591 IStream_Seek(pStream
, iSeekPos
, STREAM_SEEK_SET
, NULL
);
7592 IStream_Read(pStream
, &CompObj
.dwCLSIDNameLength
, sizeof(CompObj
.dwCLSIDNameLength
), NULL
);
7593 iSeekPos
.u
.LowPart
= CompObj
.dwCLSIDNameLength
;
7594 IStream_Seek(pStream
, iSeekPos
, STREAM_SEEK_CUR
, NULL
);
7595 IStream_Read(pStream
, &CompObj
.dwOleTypeNameLength
, sizeof(CompObj
.dwOleTypeNameLength
), NULL
);
7596 iSeekPos
.u
.LowPart
= CompObj
.dwOleTypeNameLength
;
7597 IStream_Seek(pStream
, iSeekPos
, STREAM_SEEK_CUR
, NULL
);
7599 IStream_Read(pStream
, dwSize
, sizeof(*dwSize
), NULL
);
7602 IStream_Read(pStream
, strProgID
, *dwSize
, NULL
);
7604 IStream_Release(pStream
);
7609 LPOLESTR wstrProgID
;
7611 /* Get the OleType from the registry */
7612 REFCLSID clsid
= &(stat
.clsid
);
7613 IStorage_Stat(pStorage
, &stat
, STATFLAG_NONAME
);
7614 hRes
= ProgIDFromCLSID(clsid
, &wstrProgID
);
7617 *dwSize
= WideCharToMultiByte(CP_ACP
, 0, wstrProgID
, -1, strProgID
, *dwSize
, NULL
, FALSE
);
7624 /*************************************************************************
7625 * OLECONVERT_GetOle10PresData [Internal]
7627 * Converts IStorage "/001Ole10Native" stream to a OLE10 Stream
7630 * pStorage [I] Src IStroage
7631 * pOleStream [I] Dest OleStream Mem Struct
7637 * This function is used by OleConvertIStorageToOLESTREAM only.
7639 * Memory allocated for pData must be freed by the caller
7643 static void OLECONVERT_GetOle10PresData(LPSTORAGE pStorage
, OLECONVERT_OLESTREAM_DATA
*pOleStreamData
)
7648 static const WCHAR wstrStreamName
[] = {1, 'O', 'l', 'e', '1', '0', 'N', 'a', 't', 'i', 'v', 'e', 0};
7650 /* Initialize Default data for OLESTREAM */
7651 pOleStreamData
[0].dwOleID
= OLESTREAM_ID
;
7652 pOleStreamData
[0].dwTypeID
= 2;
7653 pOleStreamData
[1].dwOleID
= OLESTREAM_ID
;
7654 pOleStreamData
[1].dwTypeID
= 0;
7655 pOleStreamData
[0].dwMetaFileWidth
= 0;
7656 pOleStreamData
[0].dwMetaFileHeight
= 0;
7657 pOleStreamData
[0].pData
= NULL
;
7658 pOleStreamData
[1].pData
= NULL
;
7660 /* Open Ole10Native Stream */
7661 hRes
= IStorage_OpenStream(pStorage
, wstrStreamName
, NULL
,
7662 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &pStream
);
7666 /* Read Size and Data */
7667 IStream_Read(pStream
, &(pOleStreamData
->dwDataLength
), sizeof(pOleStreamData
->dwDataLength
), NULL
);
7668 if(pOleStreamData
->dwDataLength
> 0)
7670 pOleStreamData
->pData
= HeapAlloc(GetProcessHeap(),0,pOleStreamData
->dwDataLength
);
7671 IStream_Read(pStream
, pOleStreamData
->pData
, pOleStreamData
->dwDataLength
, NULL
);
7673 IStream_Release(pStream
);
7679 /*************************************************************************
7680 * OLECONVERT_GetOle20PresData[Internal]
7682 * Converts IStorage "/002OlePres000" stream to a OLE10 Stream
7685 * pStorage [I] Src IStroage
7686 * pOleStreamData [I] Dest OleStream Mem Struct
7692 * This function is used by OleConvertIStorageToOLESTREAM only.
7694 * Memory allocated for pData must be freed by the caller
7696 static void OLECONVERT_GetOle20PresData(LPSTORAGE pStorage
, OLECONVERT_OLESTREAM_DATA
*pOleStreamData
)
7700 OLECONVERT_ISTORAGE_OLEPRES olePress
;
7701 static const WCHAR wstrStreamName
[] = {2, 'O', 'l', 'e', 'P', 'r', 'e', 's', '0', '0', '0', 0};
7703 /* Initialize Default data for OLESTREAM */
7704 pOleStreamData
[0].dwOleID
= OLESTREAM_ID
;
7705 pOleStreamData
[0].dwTypeID
= 2;
7706 pOleStreamData
[0].dwMetaFileWidth
= 0;
7707 pOleStreamData
[0].dwMetaFileHeight
= 0;
7708 pOleStreamData
[0].dwDataLength
= OLECONVERT_WriteOLE20ToBuffer(pStorage
, &(pOleStreamData
[0].pData
));
7709 pOleStreamData
[1].dwOleID
= OLESTREAM_ID
;
7710 pOleStreamData
[1].dwTypeID
= 0;
7711 pOleStreamData
[1].dwOleTypeNameLength
= 0;
7712 pOleStreamData
[1].strOleTypeName
[0] = 0;
7713 pOleStreamData
[1].dwMetaFileWidth
= 0;
7714 pOleStreamData
[1].dwMetaFileHeight
= 0;
7715 pOleStreamData
[1].pData
= NULL
;
7716 pOleStreamData
[1].dwDataLength
= 0;
7719 /* Open OlePress000 stream */
7720 hRes
= IStorage_OpenStream(pStorage
, wstrStreamName
, NULL
,
7721 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &pStream
);
7724 LARGE_INTEGER iSeekPos
;
7725 METAFILEPICT16 MetaFilePict
;
7726 static const char strMetafilePictName
[] = "METAFILEPICT";
7728 /* Set the TypeID for a Metafile */
7729 pOleStreamData
[1].dwTypeID
= 5;
7731 /* Set the OleTypeName to Metafile */
7732 pOleStreamData
[1].dwOleTypeNameLength
= strlen(strMetafilePictName
) +1;
7733 strcpy(pOleStreamData
[1].strOleTypeName
, strMetafilePictName
);
7735 iSeekPos
.u
.HighPart
= 0;
7736 iSeekPos
.u
.LowPart
= sizeof(olePress
.byUnknown1
);
7738 /* Get Presentation Data */
7739 IStream_Seek(pStream
, iSeekPos
, STREAM_SEEK_SET
, NULL
);
7740 IStream_Read(pStream
, &(olePress
.dwExtentX
), sizeof(olePress
.dwExtentX
), NULL
);
7741 IStream_Read(pStream
, &(olePress
.dwExtentY
), sizeof(olePress
.dwExtentY
), NULL
);
7742 IStream_Read(pStream
, &(olePress
.dwSize
), sizeof(olePress
.dwSize
), NULL
);
7744 /*Set width and Height */
7745 pOleStreamData
[1].dwMetaFileWidth
= olePress
.dwExtentX
;
7746 pOleStreamData
[1].dwMetaFileHeight
= -olePress
.dwExtentY
;
7747 if(olePress
.dwSize
> 0)
7750 pOleStreamData
[1].dwDataLength
= olePress
.dwSize
+ sizeof(METAFILEPICT16
);
7752 /* Set MetaFilePict struct */
7753 MetaFilePict
.mm
= 8;
7754 MetaFilePict
.xExt
= olePress
.dwExtentX
;
7755 MetaFilePict
.yExt
= olePress
.dwExtentY
;
7756 MetaFilePict
.hMF
= 0;
7758 /* Get Metafile Data */
7759 pOleStreamData
[1].pData
= HeapAlloc(GetProcessHeap(),0,pOleStreamData
[1].dwDataLength
);
7760 memcpy(pOleStreamData
[1].pData
, &MetaFilePict
, sizeof(MetaFilePict
));
7761 IStream_Read(pStream
, &(pOleStreamData
[1].pData
[sizeof(MetaFilePict
)]), pOleStreamData
[1].dwDataLength
-sizeof(METAFILEPICT16
), NULL
);
7763 IStream_Release(pStream
);
7767 /*************************************************************************
7768 * OleConvertOLESTREAMToIStorage [OLE32.@]
7773 * DVTARGETDEVICE parameter is not handled
7774 * Still unsure of some mem fields for OLE 10 Stream
7775 * Still some unknowns for the IStorage: "\002OlePres000", "\001CompObj",
7776 * and "\001OLE" streams
7779 HRESULT WINAPI
OleConvertOLESTREAMToIStorage (
7780 LPOLESTREAM pOleStream
,
7782 const DVTARGETDEVICE
* ptd
)
7786 OLECONVERT_OLESTREAM_DATA pOleStreamData
[2];
7788 TRACE("%p %p %p\n", pOleStream
, pstg
, ptd
);
7790 memset(pOleStreamData
, 0, sizeof(pOleStreamData
));
7794 FIXME("DVTARGETDEVICE is not NULL, unhandled parameter\n");
7797 if(pstg
== NULL
|| pOleStream
== NULL
)
7799 hRes
= E_INVALIDARG
;
7804 /* Load the OLESTREAM to Memory */
7805 hRes
= OLECONVERT_LoadOLE10(pOleStream
, &pOleStreamData
[0], TRUE
);
7810 /* Load the OLESTREAM to Memory (part 2)*/
7811 hRes
= OLECONVERT_LoadOLE10(pOleStream
, &pOleStreamData
[1], FALSE
);
7817 if(pOleStreamData
[0].dwDataLength
> sizeof(STORAGE_magic
))
7819 /* Do we have the IStorage Data in the OLESTREAM */
7820 if(memcmp(pOleStreamData
[0].pData
, STORAGE_magic
, sizeof(STORAGE_magic
)) ==0)
7822 OLECONVERT_GetOLE20FromOLE10(pstg
, pOleStreamData
[0].pData
, pOleStreamData
[0].dwDataLength
);
7823 OLECONVERT_CreateOlePresStream(pstg
, pOleStreamData
[1].dwMetaFileWidth
, pOleStreamData
[1].dwMetaFileHeight
, pOleStreamData
[1].pData
, pOleStreamData
[1].dwDataLength
);
7827 /* It must be an original OLE 1.0 source */
7828 OLECONVERT_CreateOle10NativeStream(pstg
, pOleStreamData
[0].pData
, pOleStreamData
[0].dwDataLength
);
7833 /* It must be an original OLE 1.0 source */
7834 OLECONVERT_CreateOle10NativeStream(pstg
, pOleStreamData
[0].pData
, pOleStreamData
[0].dwDataLength
);
7837 /* Create CompObj Stream if necessary */
7838 hRes
= OLECONVERT_CreateCompObjStream(pstg
, pOleStreamData
[0].strOleTypeName
);
7841 /*Create the Ole Stream if necessary */
7842 OLECONVERT_CreateOleStream(pstg
);
7847 /* Free allocated memory */
7848 for(i
=0; i
< 2; i
++)
7850 HeapFree(GetProcessHeap(),0,pOleStreamData
[i
].pData
);
7851 HeapFree(GetProcessHeap(),0,pOleStreamData
[i
].pstrOleObjFileName
);
7852 pOleStreamData
[i
].pstrOleObjFileName
= NULL
;
7857 /*************************************************************************
7858 * OleConvertIStorageToOLESTREAM [OLE32.@]
7865 * Still unsure of some mem fields for OLE 10 Stream
7866 * Still some unknowns for the IStorage: "\002OlePres000", "\001CompObj",
7867 * and "\001OLE" streams.
7870 HRESULT WINAPI
OleConvertIStorageToOLESTREAM (
7872 LPOLESTREAM pOleStream
)
7875 HRESULT hRes
= S_OK
;
7877 OLECONVERT_OLESTREAM_DATA pOleStreamData
[2];
7878 static const WCHAR wstrStreamName
[] = {1, 'O', 'l', 'e', '1', '0', 'N', 'a', 't', 'i', 'v', 'e', 0};
7880 TRACE("%p %p\n", pstg
, pOleStream
);
7882 memset(pOleStreamData
, 0, sizeof(pOleStreamData
));
7884 if(pstg
== NULL
|| pOleStream
== NULL
)
7886 hRes
= E_INVALIDARG
;
7890 /* Get the ProgID */
7891 pOleStreamData
[0].dwOleTypeNameLength
= OLESTREAM_MAX_STR_LEN
;
7892 hRes
= OLECONVERT_GetOLE10ProgID(pstg
, pOleStreamData
[0].strOleTypeName
, &(pOleStreamData
[0].dwOleTypeNameLength
));
7896 /* Was it originally Ole10 */
7897 hRes
= IStorage_OpenStream(pstg
, wstrStreamName
, 0, STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &pStream
);
7900 IStream_Release(pStream
);
7901 /* Get Presentation Data for Ole10Native */
7902 OLECONVERT_GetOle10PresData(pstg
, pOleStreamData
);
7906 /* Get Presentation Data (OLE20) */
7907 OLECONVERT_GetOle20PresData(pstg
, pOleStreamData
);
7910 /* Save OLESTREAM */
7911 hRes
= OLECONVERT_SaveOLE10(&(pOleStreamData
[0]), pOleStream
);
7914 hRes
= OLECONVERT_SaveOLE10(&(pOleStreamData
[1]), pOleStream
);
7919 /* Free allocated memory */
7920 for(i
=0; i
< 2; i
++)
7922 HeapFree(GetProcessHeap(),0,pOleStreamData
[i
].pData
);
7928 /***********************************************************************
7929 * GetConvertStg (OLE32.@)
7931 HRESULT WINAPI
GetConvertStg(IStorage
*stg
) {
7932 FIXME("unimplemented stub!\n");
7936 /******************************************************************************
7937 * StgIsStorageFile [OLE32.@]
7938 * Verify if the file contains a storage object
7944 * S_OK if file has magic bytes as a storage object
7945 * S_FALSE if file is not storage
7948 StgIsStorageFile(LPCOLESTR fn
)
7954 TRACE("%s\n", debugstr_w(fn
));
7955 hf
= CreateFileW(fn
, GENERIC_READ
,
7956 FILE_SHARE_DELETE
| FILE_SHARE_READ
| FILE_SHARE_WRITE
,
7957 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
7959 if (hf
== INVALID_HANDLE_VALUE
)
7960 return STG_E_FILENOTFOUND
;
7962 if (!ReadFile(hf
, magic
, 8, &bytes_read
, NULL
))
7964 WARN(" unable to read file\n");
7971 if (bytes_read
!= 8) {
7972 WARN(" too short\n");
7976 if (!memcmp(magic
,STORAGE_magic
,8)) {
7981 WARN(" -> Invalid header.\n");
7985 /***********************************************************************
7986 * WriteClassStm (OLE32.@)
7988 * Writes a CLSID to a stream.
7991 * pStm [I] Stream to write to.
7992 * rclsid [I] CLSID to write.
7996 * Failure: HRESULT code.
7998 HRESULT WINAPI
WriteClassStm(IStream
*pStm
,REFCLSID rclsid
)
8000 TRACE("(%p,%p)\n",pStm
,rclsid
);
8002 if (!pStm
|| !rclsid
)
8003 return E_INVALIDARG
;
8005 return IStream_Write(pStm
,rclsid
,sizeof(CLSID
),NULL
);
8008 /***********************************************************************
8009 * ReadClassStm (OLE32.@)
8011 * Reads a CLSID from a stream.
8014 * pStm [I] Stream to read from.
8015 * rclsid [O] CLSID to read.
8019 * Failure: HRESULT code.
8021 HRESULT WINAPI
ReadClassStm(IStream
*pStm
,CLSID
*pclsid
)
8026 TRACE("(%p,%p)\n",pStm
,pclsid
);
8028 if (!pStm
|| !pclsid
)
8029 return E_INVALIDARG
;
8031 /* clear the output args */
8032 *pclsid
= CLSID_NULL
;
8034 res
= IStream_Read(pStm
,(void*)pclsid
,sizeof(CLSID
),&nbByte
);
8039 if (nbByte
!= sizeof(CLSID
))
8040 return STG_E_READFAULT
;