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
20 #include "wine/obj_storage.h"
21 #include "wine/winestring.h"
27 #include "storage32.h"
31 static const char rootPropertyName
[] = "Root Entry";
33 /***********************************************************************
34 * Forward declaration of internal functions used by the method DestroyElement
36 static HRESULT
deleteStorageProperty(
37 StorageImpl
*parentStorage
,
38 OLECHAR
*propertyToDeleteName
);
40 static HRESULT
deleteStreamProperty(
41 StorageImpl
*parentStorage
,
42 ULONG foundPropertyIndexToDelete
,
43 StgProperty propertyToDelete
);
45 static HRESULT
findPlaceholder(
47 ULONG propertyIndexToStore
,
48 ULONG storagePropertyIndex
,
51 static HRESULT
adjustPropertyChain(
53 StgProperty propertyToDelete
,
54 StgProperty parentProperty
,
55 ULONG parentPropertyId
,
58 /***********************************************************************
59 * Declaration of the functions used to manipulate StgProperty
62 static ULONG
getFreeProperty(
63 StorageImpl
*storage
);
65 static void updatePropertyChain(
67 ULONG newPropertyIndex
,
68 StgProperty newProperty
);
70 static LONG
propertyNameCmp(
72 OLECHAR
*currentProperty
);
75 /***********************************************************************
76 * Declaration of miscellaneous functions...
78 static HRESULT
validateSTGM(DWORD stgmValue
);
80 static DWORD
GetShareModeFromSTGM(DWORD stgm
);
81 static DWORD
GetAccessModeFromSTGM(DWORD stgm
);
82 static DWORD
GetCreationModeFromSTGM(DWORD stgm
);
85 * Virtual function table for the IStorage32Impl class.
87 static ICOM_VTABLE(IStorage
) Storage32Impl_Vtbl
=
89 StorageBaseImpl_QueryInterface
,
90 StorageBaseImpl_AddRef
,
91 StorageBaseImpl_Release
,
92 StorageBaseImpl_CreateStream
,
93 StorageBaseImpl_OpenStream
,
94 StorageImpl_CreateStorage
,
95 StorageBaseImpl_OpenStorage
,
97 StorageImpl_MoveElementTo
,
100 StorageBaseImpl_EnumElements
,
101 StorageImpl_DestroyElement
,
102 StorageBaseImpl_RenameElement
,
103 StorageImpl_SetElementTimes
,
104 StorageBaseImpl_SetClass
,
105 StorageImpl_SetStateBits
,
110 * Virtual function table for the Storage32InternalImpl class.
112 static ICOM_VTABLE(IStorage
) Storage32InternalImpl_Vtbl
=
114 StorageBaseImpl_QueryInterface
,
115 StorageBaseImpl_AddRef
,
116 StorageBaseImpl_Release
,
117 StorageBaseImpl_CreateStream
,
118 StorageBaseImpl_OpenStream
,
119 StorageImpl_CreateStorage
,
120 StorageBaseImpl_OpenStorage
,
122 StorageImpl_MoveElementTo
,
123 StorageInternalImpl_Commit
,
124 StorageInternalImpl_Revert
,
125 StorageBaseImpl_EnumElements
,
126 StorageImpl_DestroyElement
,
127 StorageBaseImpl_RenameElement
,
128 StorageImpl_SetElementTimes
,
129 StorageBaseImpl_SetClass
,
130 StorageImpl_SetStateBits
,
135 * Virtual function table for the IEnumSTATSTGImpl class.
137 static ICOM_VTABLE(IEnumSTATSTG
) IEnumSTATSTGImpl_Vtbl
=
139 IEnumSTATSTGImpl_QueryInterface
,
140 IEnumSTATSTGImpl_AddRef
,
141 IEnumSTATSTGImpl_Release
,
142 IEnumSTATSTGImpl_Next
,
143 IEnumSTATSTGImpl_Skip
,
144 IEnumSTATSTGImpl_Reset
,
145 IEnumSTATSTGImpl_Clone
152 /************************************************************************
153 ** Storage32BaseImpl implementatiion
156 /************************************************************************
157 * Storage32BaseImpl_QueryInterface (IUnknown)
159 * This method implements the common QueryInterface for all IStorage32
160 * implementations contained in this file.
162 * See Windows documentation for more details on IUnknown methods.
164 HRESULT WINAPI
StorageBaseImpl_QueryInterface(
169 ICOM_THIS(StorageBaseImpl
,iface
);
171 * Perform a sanity check on the parameters.
173 if ( (This
==0) || (ppvObject
==0) )
177 * Initialize the return parameter.
182 * Compare the riid with the interface IDs implemented by this object.
184 if (memcmp(&IID_IUnknown
, riid
, sizeof(IID_IUnknown
)) == 0)
186 *ppvObject
= (IStorage
*)This
;
188 else if (memcmp(&IID_IStorage
, riid
, sizeof(IID_IStorage
)) == 0)
190 *ppvObject
= (IStorage
*)This
;
194 * Check that we obtained an interface.
197 return E_NOINTERFACE
;
200 * Query Interface always increases the reference count by one when it is
203 StorageBaseImpl_AddRef(iface
);
208 /************************************************************************
209 * Storage32BaseImpl_AddRef (IUnknown)
211 * This method implements the common AddRef for all IStorage32
212 * implementations contained in this file.
214 * See Windows documentation for more details on IUnknown methods.
216 ULONG WINAPI
StorageBaseImpl_AddRef(
219 ICOM_THIS(StorageBaseImpl
,iface
);
225 /************************************************************************
226 * Storage32BaseImpl_Release (IUnknown)
228 * This method implements the common Release for all IStorage32
229 * implementations contained in this file.
231 * See Windows documentation for more details on IUnknown methods.
233 ULONG WINAPI
StorageBaseImpl_Release(
236 ICOM_THIS(StorageBaseImpl
,iface
);
238 * Decrease the reference count on this object.
243 * If the reference count goes down to 0, perform suicide.
248 * Since we are using a system of base-classes, we want to call the
249 * destructor of the appropriate derived class. To do this, we are
250 * using virtual functions to implement the destructor.
252 This
->v_destructor(This
);
260 /************************************************************************
261 * Storage32BaseImpl_OpenStream (IStorage)
263 * This method will open the specified stream object from the current storage.
265 * See Windows documentation for more details on IStorage methods.
267 HRESULT WINAPI
StorageBaseImpl_OpenStream(
269 const OLECHAR
* pwcsName
, /* [string][in] */
270 void* reserved1
, /* [unique][in] */
271 DWORD grfMode
, /* [in] */
272 DWORD reserved2
, /* [in] */
273 IStream
** ppstm
) /* [out] */
275 ICOM_THIS(StorageBaseImpl
,iface
);
276 IEnumSTATSTGImpl
* propertyEnumeration
;
277 StgStreamImpl
* newStream
;
278 StgProperty currentProperty
;
279 ULONG foundPropertyIndex
;
282 * Perform a sanity check on the parameters.
284 if ( (pwcsName
==NULL
) || (ppstm
==0) )
288 * Initialize the out parameter
293 * Validate the STGM flags
295 if ( FAILED( validateSTGM(grfMode
) ))
296 return STG_E_INVALIDFLAG
;
301 if ( !(grfMode
& STGM_SHARE_EXCLUSIVE
) ||
302 (grfMode
& STGM_DELETEONRELEASE
) ||
303 (grfMode
& STGM_TRANSACTED
) )
304 return STG_E_INVALIDFUNCTION
;
307 * Create a property enumeration to search the properties
309 propertyEnumeration
= IEnumSTATSTGImpl_Construct(
310 This
->ancestorStorage
,
311 This
->rootPropertySetIndex
);
314 * Search the enumeration for the property with the given name
316 foundPropertyIndex
= IEnumSTATSTGImpl_FindProperty(
322 * Delete the property enumeration since we don't need it anymore
324 IEnumSTATSTGImpl_Destroy(propertyEnumeration
);
327 * If it was found, construct the stream object and return a pointer to it.
329 if ( (foundPropertyIndex
!=PROPERTY_NULL
) &&
330 (currentProperty
.propertyType
==PROPTYPE_STREAM
) )
332 newStream
= StgStreamImpl_Construct(This
, foundPropertyIndex
);
336 *ppstm
= (IStream
*)newStream
;
339 * Since we are returning a pointer to the interface, we have to
340 * nail down the reference.
342 StgStreamImpl_AddRef(*ppstm
);
347 return E_OUTOFMEMORY
;
350 return STG_E_FILENOTFOUND
;
353 /************************************************************************
354 * Storage32BaseImpl_OpenStorage (IStorage)
356 * This method will open a new storage object from the current storage.
358 * See Windows documentation for more details on IStorage methods.
360 HRESULT WINAPI
StorageBaseImpl_OpenStorage(
362 const OLECHAR
* pwcsName
, /* [string][unique][in] */
363 IStorage
* pstgPriority
, /* [unique][in] */
364 DWORD grfMode
, /* [in] */
365 SNB snbExclude
, /* [unique][in] */
366 DWORD reserved
, /* [in] */
367 IStorage
** ppstg
) /* [out] */
369 ICOM_THIS(StorageBaseImpl
,iface
);
370 StorageInternalImpl
* newStorage
;
371 IEnumSTATSTGImpl
* propertyEnumeration
;
372 StgProperty currentProperty
;
373 ULONG foundPropertyIndex
;
376 * Perform a sanity check on the parameters.
378 if ( (This
==0) || (pwcsName
==NULL
) || (ppstg
==0) )
382 * Validate the STGM flags
384 if ( FAILED( validateSTGM(grfMode
) ))
385 return STG_E_INVALIDFLAG
;
390 if ( !(grfMode
& STGM_SHARE_EXCLUSIVE
) ||
391 (grfMode
& STGM_DELETEONRELEASE
) ||
392 (grfMode
& STGM_PRIORITY
) )
393 return STG_E_INVALIDFUNCTION
;
396 * Initialize the out parameter
401 * Create a property enumeration to search the properties
403 propertyEnumeration
= IEnumSTATSTGImpl_Construct(
404 This
->ancestorStorage
,
405 This
->rootPropertySetIndex
);
408 * Search the enumeration for the property with the given name
410 foundPropertyIndex
= IEnumSTATSTGImpl_FindProperty(
416 * Delete the property enumeration since we don't need it anymore
418 IEnumSTATSTGImpl_Destroy(propertyEnumeration
);
421 * If it was found, construct the stream object and return a pointer to it.
423 if ( (foundPropertyIndex
!=PROPERTY_NULL
) &&
424 (currentProperty
.propertyType
==PROPTYPE_STORAGE
) )
427 * Construct a new Storage object
429 newStorage
= StorageInternalImpl_Construct(
430 This
->ancestorStorage
,
435 *ppstg
= (IStorage
*)newStorage
;
438 * Since we are returning a pointer to the interface,
439 * we have to nail down the reference.
441 StorageBaseImpl_AddRef(*ppstg
);
446 return STG_E_INSUFFICIENTMEMORY
;
449 return STG_E_FILENOTFOUND
;
452 /************************************************************************
453 * Storage32BaseImpl_EnumElements (IStorage)
455 * This method will create an enumerator object that can be used to
456 * retrieve informatino about all the properties in the storage object.
458 * See Windows documentation for more details on IStorage methods.
460 HRESULT WINAPI
StorageBaseImpl_EnumElements(
462 DWORD reserved1
, /* [in] */
463 void* reserved2
, /* [size_is][unique][in] */
464 DWORD reserved3
, /* [in] */
465 IEnumSTATSTG
** ppenum
) /* [out] */
467 ICOM_THIS(StorageBaseImpl
,iface
);
468 IEnumSTATSTGImpl
* newEnum
;
471 * Perform a sanity check on the parameters.
473 if ( (This
==0) || (ppenum
==0))
477 * Construct the enumerator.
479 newEnum
= IEnumSTATSTGImpl_Construct(
480 This
->ancestorStorage
,
481 This
->rootPropertySetIndex
);
485 *ppenum
= (IEnumSTATSTG
*)newEnum
;
488 * Don't forget to nail down a reference to the new object before
491 IEnumSTATSTGImpl_AddRef(*ppenum
);
496 return E_OUTOFMEMORY
;
499 /************************************************************************
500 * Storage32BaseImpl_Stat (IStorage)
502 * This method will retrieve information about this storage object.
504 * See Windows documentation for more details on IStorage methods.
506 HRESULT WINAPI
StorageBaseImpl_Stat(
508 STATSTG
* pstatstg
, /* [out] */
509 DWORD grfStatFlag
) /* [in] */
511 ICOM_THIS(StorageBaseImpl
,iface
);
512 StgProperty curProperty
;
516 * Perform a sanity check on the parameters.
518 if ( (This
==0) || (pstatstg
==0))
522 * Read the information from the property.
524 readSucessful
= StorageImpl_ReadProperty(
525 This
->ancestorStorage
,
526 This
->rootPropertySetIndex
,
531 StorageUtl_CopyPropertyToSTATSTG(
542 /************************************************************************
543 * Storage32BaseImpl_RenameElement (IStorage)
545 * This method will rename the specified element.
547 * See Windows documentation for more details on IStorage methods.
549 * Implementation notes: The method used to rename consists of creating a clone
550 * of the deleted StgProperty object setting it with the new name and to
551 * perform a DestroyElement of the old StgProperty.
553 HRESULT WINAPI
StorageBaseImpl_RenameElement(
555 const OLECHAR
* pwcsOldName
, /* [in] */
556 const OLECHAR
* pwcsNewName
) /* [in] */
558 ICOM_THIS(StorageBaseImpl
,iface
);
559 IEnumSTATSTGImpl
* propertyEnumeration
;
560 StgProperty currentProperty
;
561 ULONG foundPropertyIndex
;
564 * Create a property enumeration to search the properties
566 propertyEnumeration
= IEnumSTATSTGImpl_Construct(This
->ancestorStorage
,
567 This
->rootPropertySetIndex
);
570 * Search the enumeration for the new property name
572 foundPropertyIndex
= IEnumSTATSTGImpl_FindProperty(propertyEnumeration
,
576 if (foundPropertyIndex
!= PROPERTY_NULL
)
579 * There is already a property with the new name
581 IEnumSTATSTGImpl_Destroy(propertyEnumeration
);
582 return STG_E_FILEALREADYEXISTS
;
585 IEnumSTATSTGImpl_Reset((IEnumSTATSTG
*)propertyEnumeration
);
588 * Search the enumeration for the old property name
590 foundPropertyIndex
= IEnumSTATSTGImpl_FindProperty(propertyEnumeration
,
595 * Delete the property enumeration since we don't need it anymore
597 IEnumSTATSTGImpl_Destroy(propertyEnumeration
);
599 if (foundPropertyIndex
!= PROPERTY_NULL
)
601 StgProperty renamedProperty
;
602 ULONG renamedPropertyIndex
;
605 * Setup a new property for the renamed property
607 renamedProperty
.sizeOfNameString
=
608 ( lstrlenW(pwcsNewName
)+1 ) * sizeof(WCHAR
);
610 if (renamedProperty
.sizeOfNameString
> PROPERTY_NAME_BUFFER_LEN
)
611 return STG_E_INVALIDNAME
;
613 lstrcpyW(renamedProperty
.name
, pwcsNewName
);
615 renamedProperty
.propertyType
= currentProperty
.propertyType
;
616 renamedProperty
.startingBlock
= currentProperty
.startingBlock
;
617 renamedProperty
.size
.LowPart
= currentProperty
.size
.LowPart
;
618 renamedProperty
.size
.HighPart
= currentProperty
.size
.HighPart
;
620 renamedProperty
.previousProperty
= PROPERTY_NULL
;
621 renamedProperty
.nextProperty
= PROPERTY_NULL
;
624 * Bring the dirProperty link in case it is a storage and in which
625 * case the renamed storage elements don't require to be reorganized.
627 renamedProperty
.dirProperty
= currentProperty
.dirProperty
;
629 /* call CoFileTime to get the current time
630 renamedProperty.timeStampS1
631 renamedProperty.timeStampD1
632 renamedProperty.timeStampS2
633 renamedProperty.timeStampD2
634 renamedProperty.propertyUniqueID
638 * Obtain a free property in the property chain
640 renamedPropertyIndex
= getFreeProperty(This
->ancestorStorage
);
643 * Save the new property into the new property spot
645 StorageImpl_WriteProperty(
646 This
->ancestorStorage
,
647 renamedPropertyIndex
,
651 * Find a spot in the property chain for our newly created property.
655 renamedPropertyIndex
,
659 * At this point the renamed property has been inserted in the tree,
660 * now, before to Destroy the old property we must zeroed it's dirProperty
661 * otherwise the DestroyProperty below will zap it all and we do not want
663 * Also, we fake that the old property is a storage so the DestroyProperty
664 * will not do a SetSize(0) on the stream data.
666 * This means that we need to tweek the StgProperty if it is a stream or a
669 currentProperty
.dirProperty
= PROPERTY_NULL
;
670 currentProperty
.propertyType
= PROPTYPE_STORAGE
;
671 StorageImpl_WriteProperty(
672 This
->ancestorStorage
,
677 * Invoke Destroy to get rid of the ole property and automatically redo
678 * the linking of it's previous and next members...
680 StorageImpl_DestroyElement((IStorage
*)This
->ancestorStorage
, pwcsOldName
);
686 * There is no property with the old name
688 return STG_E_FILENOTFOUND
;
694 /************************************************************************
695 * Storage32BaseImpl_CreateStream (IStorage)
697 * This method will create a stream object within this storage
699 * See Windows documentation for more details on IStorage methods.
701 HRESULT WINAPI
StorageBaseImpl_CreateStream(
703 const OLECHAR
* pwcsName
, /* [string][in] */
704 DWORD grfMode
, /* [in] */
705 DWORD reserved1
, /* [in] */
706 DWORD reserved2
, /* [in] */
707 IStream
** ppstm
) /* [out] */
709 ICOM_THIS(StorageBaseImpl
,iface
);
710 IEnumSTATSTGImpl
* propertyEnumeration
;
711 StgStreamImpl
* newStream
;
712 StgProperty currentProperty
, newStreamProperty
;
713 ULONG foundPropertyIndex
, newPropertyIndex
;
716 * Validate parameters
719 return STG_E_INVALIDPOINTER
;
722 return STG_E_INVALIDNAME
;
725 * Validate the STGM flags
727 if ( FAILED( validateSTGM(grfMode
) ))
728 return STG_E_INVALIDFLAG
;
733 if ( !(grfMode
& STGM_SHARE_EXCLUSIVE
) ||
734 (grfMode
& STGM_DELETEONRELEASE
) ||
735 (grfMode
& STGM_TRANSACTED
) )
736 return STG_E_INVALIDFUNCTION
;
739 * Initialize the out parameter
744 * Create a property enumeration to search the properties
746 propertyEnumeration
= IEnumSTATSTGImpl_Construct(This
->ancestorStorage
,
747 This
->rootPropertySetIndex
);
749 foundPropertyIndex
= IEnumSTATSTGImpl_FindProperty(propertyEnumeration
,
753 IEnumSTATSTGImpl_Destroy(propertyEnumeration
);
755 if (foundPropertyIndex
!= PROPERTY_NULL
)
758 * An element with this name already exists
760 if (grfMode
& STGM_CREATE
)
761 StorageImpl_DestroyElement((IStorage
*)This
->ancestorStorage
, pwcsName
);
763 return STG_E_FILEALREADYEXISTS
;
767 * memset the empty property
769 memset(&newStreamProperty
, 0, sizeof(StgProperty
));
771 newStreamProperty
.sizeOfNameString
=
772 ( lstrlenW(pwcsName
)+1 ) * sizeof(WCHAR
);
774 if (newStreamProperty
.sizeOfNameString
> PROPERTY_NAME_BUFFER_LEN
)
775 return STG_E_INVALIDNAME
;
777 lstrcpyW(newStreamProperty
.name
, pwcsName
);
779 newStreamProperty
.propertyType
= PROPTYPE_STREAM
;
780 newStreamProperty
.startingBlock
= BLOCK_END_OF_CHAIN
;
781 newStreamProperty
.size
.LowPart
= 0;
782 newStreamProperty
.size
.HighPart
= 0;
784 newStreamProperty
.previousProperty
= PROPERTY_NULL
;
785 newStreamProperty
.nextProperty
= PROPERTY_NULL
;
786 newStreamProperty
.dirProperty
= PROPERTY_NULL
;
788 /* call CoFileTime to get the current time
789 newStreamProperty.timeStampS1
790 newStreamProperty.timeStampD1
791 newStreamProperty.timeStampS2
792 newStreamProperty.timeStampD2
795 /* newStreamProperty.propertyUniqueID */
798 * Get a free property or create a new one
800 newPropertyIndex
= getFreeProperty(This
->ancestorStorage
);
803 * Save the new property into the new property spot
805 StorageImpl_WriteProperty(
806 This
->ancestorStorage
,
811 * Find a spot in the property chain for our newly created property.
819 * Open the stream to return it.
821 newStream
= StgStreamImpl_Construct(This
, newPropertyIndex
);
825 *ppstm
= (IStream
*)newStream
;
828 * Since we are returning a pointer to the interface, we have to nail down
831 StgStreamImpl_AddRef(*ppstm
);
835 return STG_E_INSUFFICIENTMEMORY
;
841 /************************************************************************
842 * Storage32BaseImpl_SetClass (IStorage)
844 * This method will write the specified CLSID in the property of this
847 * See Windows documentation for more details on IStorage methods.
849 HRESULT WINAPI
StorageBaseImpl_SetClass(
851 REFCLSID clsid
) /* [in] */
853 ICOM_THIS(StorageBaseImpl
,iface
);
854 HRESULT hRes
= E_FAIL
;
855 StgProperty curProperty
;
858 success
= StorageImpl_ReadProperty(This
->ancestorStorage
,
859 This
->rootPropertySetIndex
,
863 curProperty
.propertyUniqueID
= *clsid
;
865 success
= StorageImpl_WriteProperty(This
->ancestorStorage
,
866 This
->rootPropertySetIndex
,
875 /************************************************************************
876 ** Storage32Impl implementation
879 /************************************************************************
880 * Storage32Impl_CreateStorage (IStorage)
882 * This method will create the storage object within the provided storage.
884 * See Windows documentation for more details on IStorage methods.
886 HRESULT WINAPI
StorageImpl_CreateStorage(
888 const OLECHAR
*pwcsName
, /* [string][in] */
889 DWORD grfMode
, /* [in] */
890 DWORD reserved1
, /* [in] */
891 DWORD reserved2
, /* [in] */
892 IStorage
**ppstg
) /* [out] */
894 StorageImpl
* const This
=(StorageImpl
*)iface
;
896 IEnumSTATSTGImpl
*propertyEnumeration
;
897 StgProperty currentProperty
;
898 StgProperty newProperty
;
899 ULONG foundPropertyIndex
;
900 ULONG newPropertyIndex
;
904 * Validate parameters
907 return STG_E_INVALIDPOINTER
;
910 return STG_E_INVALIDNAME
;
913 * Validate the STGM flags
915 if ( FAILED( validateSTGM(grfMode
) ) ||
916 (grfMode
& STGM_DELETEONRELEASE
) )
917 return STG_E_INVALIDFLAG
;
920 * Initialize the out parameter
925 * Create a property enumeration and search the properties
927 propertyEnumeration
= IEnumSTATSTGImpl_Construct( This
->ancestorStorage
,
928 This
->rootPropertySetIndex
);
930 foundPropertyIndex
= IEnumSTATSTGImpl_FindProperty(propertyEnumeration
,
933 IEnumSTATSTGImpl_Destroy(propertyEnumeration
);
935 if (foundPropertyIndex
!= PROPERTY_NULL
)
938 * An element with this name already exists
940 if (grfMode
& STGM_CREATE
)
941 StorageImpl_DestroyElement((IStorage
*)This
->ancestorStorage
, pwcsName
);
943 return STG_E_FILEALREADYEXISTS
;
947 * memset the empty property
949 memset(&newProperty
, 0, sizeof(StgProperty
));
951 newProperty
.sizeOfNameString
= (lstrlenW(pwcsName
)+1)*sizeof(WCHAR
);
953 if (newProperty
.sizeOfNameString
> PROPERTY_NAME_BUFFER_LEN
)
954 return STG_E_INVALIDNAME
;
956 lstrcpyW(newProperty
.name
, pwcsName
);
958 newProperty
.propertyType
= PROPTYPE_STORAGE
;
959 newProperty
.startingBlock
= BLOCK_END_OF_CHAIN
;
960 newProperty
.size
.LowPart
= 0;
961 newProperty
.size
.HighPart
= 0;
963 newProperty
.previousProperty
= PROPERTY_NULL
;
964 newProperty
.nextProperty
= PROPERTY_NULL
;
965 newProperty
.dirProperty
= PROPERTY_NULL
;
967 /* call CoFileTime to get the current time
968 newProperty.timeStampS1
969 newProperty.timeStampD1
970 newProperty.timeStampS2
971 newProperty.timeStampD2
974 /* newStorageProperty.propertyUniqueID */
977 * Obtain a free property in the property chain
979 newPropertyIndex
= getFreeProperty(This
->ancestorStorage
);
982 * Save the new property into the new property spot
984 StorageImpl_WriteProperty(
985 This
->ancestorStorage
,
990 * Find a spot in the property chain for our newly created property.
998 * Open it to get a pointer to return.
1000 hr
= StorageBaseImpl_OpenStorage(
1009 if( (hr
!= S_OK
) || (*ppstg
== NULL
))
1018 /***************************************************************************
1022 * Get a free property or create a new one.
1024 static ULONG
getFreeProperty(
1025 StorageImpl
*storage
)
1027 ULONG currentPropertyIndex
= 0;
1028 ULONG newPropertyIndex
= PROPERTY_NULL
;
1029 BOOL readSucessful
= TRUE
;
1030 StgProperty currentProperty
;
1035 * Start by reading the root property
1037 readSucessful
= StorageImpl_ReadProperty(storage
->ancestorStorage
,
1038 currentPropertyIndex
,
1042 if (currentProperty
.sizeOfNameString
== 0)
1045 * The property existis and is available, we found it.
1047 newPropertyIndex
= currentPropertyIndex
;
1053 * We exhausted the property list, we will create more space below
1055 newPropertyIndex
= currentPropertyIndex
;
1057 currentPropertyIndex
++;
1059 } while (newPropertyIndex
== PROPERTY_NULL
);
1062 * grow the property chain
1064 if (! readSucessful
)
1066 StgProperty emptyProperty
;
1067 ULARGE_INTEGER newSize
;
1068 ULONG propertyIndex
;
1069 ULONG lastProperty
= 0;
1070 ULONG blockCount
= 0;
1073 * obtain the new count of property blocks
1075 blockCount
= BlockChainStream_GetCount(
1076 storage
->ancestorStorage
->rootBlockChain
)+1;
1079 * initialize the size used by the property stream
1081 newSize
.HighPart
= 0;
1082 newSize
.LowPart
= storage
->bigBlockSize
* blockCount
;
1085 * add a property block to the property chain
1087 BlockChainStream_SetSize(storage
->ancestorStorage
->rootBlockChain
, newSize
);
1090 * memset the empty property in order to initialize the unused newly
1093 memset(&emptyProperty
, 0, sizeof(StgProperty
));
1098 lastProperty
= storage
->bigBlockSize
/ PROPSET_BLOCK_SIZE
* blockCount
;
1101 propertyIndex
= newPropertyIndex
;
1102 propertyIndex
< lastProperty
;
1105 StorageImpl_WriteProperty(
1106 storage
->ancestorStorage
,
1112 return newPropertyIndex
;
1115 /****************************************************************************
1119 * Case insensitive comparaison of StgProperty.name by first considering
1122 * Returns <0 when newPrpoerty < currentProperty
1123 * >0 when newPrpoerty > currentProperty
1124 * 0 when newPrpoerty == currentProperty
1126 static LONG
propertyNameCmp(
1127 OLECHAR
*newProperty
,
1128 OLECHAR
*currentProperty
)
1130 LONG sizeOfNew
= (lstrlenW(newProperty
) +1) * sizeof(WCHAR
);
1131 LONG sizeOfCur
= (lstrlenW(currentProperty
)+1) * sizeof(WCHAR
);
1132 LONG diff
= sizeOfNew
- sizeOfCur
;
1137 * We compare the string themselves only when they are of the same lenght
1139 WCHAR wsnew
[PROPERTY_NAME_MAX_LEN
];
1140 WCHAR wscur
[PROPERTY_NAME_MAX_LEN
];
1142 diff
= lstrcmpW( (LPCWSTR
)CRTDLL__wcsupr(
1143 lstrcpynW(wsnew
, newProperty
, sizeOfNew
)),
1144 (LPCWSTR
)CRTDLL__wcsupr(
1145 lstrcpynW(wscur
, currentProperty
, sizeOfCur
)));
1151 /****************************************************************************
1155 * Properly link this new element in the property chain.
1157 static void updatePropertyChain(
1158 StorageImpl
*storage
,
1159 ULONG newPropertyIndex
,
1160 StgProperty newProperty
)
1162 StgProperty currentProperty
;
1165 * Read the root property
1167 StorageImpl_ReadProperty(storage
->ancestorStorage
,
1168 storage
->rootPropertySetIndex
,
1171 if (currentProperty
.dirProperty
!= PROPERTY_NULL
)
1174 * The root storage contains some element, therefore, start the research
1175 * for the appropriate location.
1178 ULONG current
, next
, previous
, currentPropertyId
;
1181 * Keep the StgProperty sequence number of the storage first property
1183 currentPropertyId
= currentProperty
.dirProperty
;
1188 StorageImpl_ReadProperty(storage
->ancestorStorage
,
1189 currentProperty
.dirProperty
,
1192 previous
= currentProperty
.previousProperty
;
1193 next
= currentProperty
.nextProperty
;
1194 current
= currentPropertyId
;
1198 LONG diff
= propertyNameCmp( newProperty
.name
, currentProperty
.name
);
1202 if (previous
!= PROPERTY_NULL
)
1204 StorageImpl_ReadProperty(storage
->ancestorStorage
,
1211 currentProperty
.previousProperty
= newPropertyIndex
;
1212 StorageImpl_WriteProperty(storage
->ancestorStorage
,
1220 if (next
!= PROPERTY_NULL
)
1222 StorageImpl_ReadProperty(storage
->ancestorStorage
,
1229 currentProperty
.nextProperty
= newPropertyIndex
;
1230 StorageImpl_WriteProperty(storage
->ancestorStorage
,
1237 previous
= currentProperty
.previousProperty
;
1238 next
= currentProperty
.nextProperty
;
1244 * The root storage is empty, link the new property to it's dir property
1246 currentProperty
.dirProperty
= newPropertyIndex
;
1247 StorageImpl_WriteProperty(storage
->ancestorStorage
,
1248 storage
->rootPropertySetIndex
,
1254 /*************************************************************************
1257 HRESULT WINAPI
StorageImpl_CopyTo(
1259 DWORD ciidExclude
, /* [in] */
1260 const IID
*rgiidExclude
,/* [size_is][unique][in] */
1261 SNB snbExclude
, /* [unique][in] */
1262 IStorage
*pstgDest
) /* [unique][in] */
1267 /*************************************************************************
1268 * MoveElementTo (IStorage)
1270 HRESULT WINAPI
StorageImpl_MoveElementTo(
1272 const OLECHAR
*pwcsName
, /* [string][in] */
1273 IStorage
*pstgDest
, /* [unique][in] */
1274 const OLECHAR
*pwcsNewName
,/* [string][in] */
1275 DWORD grfFlags
) /* [in] */
1280 /*************************************************************************
1283 HRESULT WINAPI
StorageImpl_Commit(
1285 DWORD grfCommitFlags
)/* [in] */
1287 FIXME(ole
, "(%ld): stub!\n", grfCommitFlags
);
1291 /*************************************************************************
1294 HRESULT WINAPI
StorageImpl_Revert(
1300 /*************************************************************************
1301 * DestroyElement (IStorage)
1303 * Stategy: This implementation is build this way for simplicity not for speed.
1304 * I always delete the top most element of the enumeration and adjust
1305 * the deleted element pointer all the time. This takes longer to
1306 * do but allow to reinvoke DestroyElement whenever we encounter a
1307 * storage object. The optimisation reside in the usage of another
1308 * enumeration stategy that would give all the leaves of a storage
1309 * first. (postfix order)
1311 HRESULT WINAPI
StorageImpl_DestroyElement(
1313 const OLECHAR
*pwcsName
)/* [string][in] */
1315 StorageImpl
* const This
=(StorageImpl
*)iface
;
1317 IEnumSTATSTGImpl
* propertyEnumeration
;
1320 StgProperty propertyToDelete
;
1321 StgProperty parentProperty
;
1322 ULONG foundPropertyIndexToDelete
;
1323 ULONG typeOfRelation
;
1324 ULONG parentPropertyId
;
1327 * Perform a sanity check on the parameters.
1330 return STG_E_INVALIDPOINTER
;
1333 * Create a property enumeration to search the property with the given name
1335 propertyEnumeration
= IEnumSTATSTGImpl_Construct(
1336 This
->ancestorStorage
,
1337 This
->rootPropertySetIndex
);
1339 foundPropertyIndexToDelete
= IEnumSTATSTGImpl_FindProperty(
1340 propertyEnumeration
,
1344 IEnumSTATSTGImpl_Destroy(propertyEnumeration
);
1346 if ( foundPropertyIndexToDelete
== PROPERTY_NULL
)
1348 return STG_E_FILENOTFOUND
;
1352 * Find the parent property of the property to delete (the one that
1353 * link to it). If This->dirProperty == foundPropertyIndexToDelete,
1354 * the parent is This. Otherwise, the parent is one of it's sibling...
1358 * First, read This's StgProperty..
1360 res
= StorageImpl_ReadProperty(
1361 This
->ancestorStorage
,
1362 This
->rootPropertySetIndex
,
1368 * Second, check to see if by any chance the actual storage (This) is not
1369 * the parent of the property to delete... We never know...
1371 if ( parentProperty
.dirProperty
== foundPropertyIndexToDelete
)
1374 * Set data as it would have been done in the else part...
1376 typeOfRelation
= PROPERTY_RELATION_DIR
;
1377 parentPropertyId
= This
->rootPropertySetIndex
;
1382 * Create a property enumeration to search the parent properties, and
1383 * delete it once done.
1385 IEnumSTATSTGImpl
* propertyEnumeration2
;
1387 propertyEnumeration2
= IEnumSTATSTGImpl_Construct(
1388 This
->ancestorStorage
,
1389 This
->rootPropertySetIndex
);
1391 typeOfRelation
= IEnumSTATSTGImpl_FindParentProperty(
1392 propertyEnumeration2
,
1393 foundPropertyIndexToDelete
,
1397 IEnumSTATSTGImpl_Destroy(propertyEnumeration2
);
1400 if ( propertyToDelete
.propertyType
== PROPTYPE_STORAGE
)
1402 hr
= deleteStorageProperty(
1404 propertyToDelete
.name
);
1406 else if ( propertyToDelete
.propertyType
== PROPTYPE_STREAM
)
1408 hr
= deleteStreamProperty(
1410 foundPropertyIndexToDelete
,
1418 * Adjust the property chain
1420 hr
= adjustPropertyChain(
1431 /*********************************************************************
1435 * Perform the deletion of a complete storage node
1438 static HRESULT
deleteStorageProperty(
1439 StorageImpl
*parentStorage
,
1440 OLECHAR
*propertyToDeleteName
)
1442 IEnumSTATSTG
*elements
= 0;
1443 IStorage
*childStorage
= 0;
1444 STATSTG currentElement
;
1446 HRESULT destroyHr
= S_OK
;
1449 * Open the storage and enumerate it
1451 hr
= StorageBaseImpl_OpenStorage(
1452 (IStorage
*)parentStorage
,
1453 propertyToDeleteName
,
1455 STGM_SHARE_EXCLUSIVE
,
1466 * Enumerate the elements
1468 IStorage_EnumElements( childStorage
, 0, 0, 0, &elements
);
1473 * Obtain the next element
1475 hr
= IEnumSTATSTG_Next(elements
, 1, ¤tElement
, NULL
);
1478 destroyHr
= StorageImpl_DestroyElement(
1479 (IStorage
*)childStorage
,
1480 (OLECHAR
*)currentElement
.pwcsName
);
1482 CoTaskMemFree(currentElement
.pwcsName
);
1486 * We need to Reset the enumeration every time because we delete elements
1487 * and the enumeration could be invalid
1489 IEnumSTATSTG_Reset(elements
);
1491 } while ((hr
== S_OK
) && (destroyHr
== S_OK
));
1493 IStorage_Release(childStorage
);
1494 IEnumSTATSTG_Release(elements
);
1499 /*********************************************************************
1503 * Perform the deletion of a stream node
1506 static HRESULT
deleteStreamProperty(
1507 StorageImpl
*parentStorage
,
1508 ULONG indexOfPropertyToDelete
,
1509 StgProperty propertyToDelete
)
1513 ULARGE_INTEGER size
;
1518 hr
= StorageBaseImpl_OpenStream(
1519 (IStorage
*)parentStorage
,
1520 (OLECHAR
*)propertyToDelete
.name
,
1522 STGM_SHARE_EXCLUSIVE
,
1534 hr
= IStream_SetSize(pis
, size
);
1542 * Invalidate the property by zeroing it's name member.
1544 propertyToDelete
.sizeOfNameString
= 0;
1547 * Here we should re-read the property so we get the updated pointer
1548 * but since we are here to zap it, I don't do it...
1551 StorageImpl_WriteProperty(
1552 parentStorage
->ancestorStorage
,
1553 indexOfPropertyToDelete
,
1559 /*********************************************************************
1563 * Finds a placeholder for the StgProperty within the Storage
1566 static HRESULT
findPlaceholder(
1567 StorageImpl
*storage
,
1568 ULONG propertyIndexToStore
,
1569 ULONG storePropertyIndex
,
1572 StgProperty storeProperty
;
1577 * Read the storage property
1579 res
= StorageImpl_ReadProperty(
1580 storage
->ancestorStorage
,
1589 if (typeOfRelation
== PROPERTY_RELATION_PREVIOUS
)
1591 if (storeProperty
.previousProperty
!= PROPERTY_NULL
)
1593 return findPlaceholder(
1595 propertyIndexToStore
,
1596 storeProperty
.previousProperty
,
1601 storeProperty
.previousProperty
= propertyIndexToStore
;
1604 else if (typeOfRelation
== PROPERTY_RELATION_NEXT
)
1606 if (storeProperty
.nextProperty
!= PROPERTY_NULL
)
1608 return findPlaceholder(
1610 propertyIndexToStore
,
1611 storeProperty
.nextProperty
,
1616 storeProperty
.nextProperty
= propertyIndexToStore
;
1619 else if (typeOfRelation
== PROPERTY_RELATION_DIR
)
1621 if (storeProperty
.dirProperty
!= PROPERTY_NULL
)
1623 return findPlaceholder(
1625 propertyIndexToStore
,
1626 storeProperty
.dirProperty
,
1631 storeProperty
.dirProperty
= propertyIndexToStore
;
1635 hr
= StorageImpl_WriteProperty(
1636 storage
->ancestorStorage
,
1648 /*************************************************************************
1652 * This method takes the previous and the next property link of a property
1653 * to be deleted and find them a place in the Storage.
1655 static HRESULT
adjustPropertyChain(
1657 StgProperty propertyToDelete
,
1658 StgProperty parentProperty
,
1659 ULONG parentPropertyId
,
1662 ULONG newLinkProperty
= PROPERTY_NULL
;
1663 BOOL needToFindAPlaceholder
= FALSE
;
1664 ULONG storeNode
= PROPERTY_NULL
;
1665 ULONG toStoreNode
= PROPERTY_NULL
;
1666 INT relationType
= 0;
1670 if (typeOfRelation
== PROPERTY_RELATION_PREVIOUS
)
1672 if (propertyToDelete
.previousProperty
!= PROPERTY_NULL
)
1675 * Set the parent previous to the property to delete previous
1677 newLinkProperty
= propertyToDelete
.previousProperty
;
1679 if (propertyToDelete
.nextProperty
!= PROPERTY_NULL
)
1682 * We also need to find a storage for the other link, setup variables
1683 * to do this at the end...
1685 needToFindAPlaceholder
= TRUE
;
1686 storeNode
= propertyToDelete
.previousProperty
;
1687 toStoreNode
= propertyToDelete
.nextProperty
;
1688 relationType
= PROPERTY_RELATION_NEXT
;
1691 else if (propertyToDelete
.nextProperty
!= PROPERTY_NULL
)
1694 * Set the parent previous to the property to delete next
1696 newLinkProperty
= propertyToDelete
.nextProperty
;
1700 * Link it for real...
1702 parentProperty
.previousProperty
= newLinkProperty
;
1705 else if (typeOfRelation
== PROPERTY_RELATION_NEXT
)
1707 if (propertyToDelete
.previousProperty
!= PROPERTY_NULL
)
1710 * Set the parent next to the property to delete next previous
1712 newLinkProperty
= propertyToDelete
.previousProperty
;
1714 if (propertyToDelete
.nextProperty
!= PROPERTY_NULL
)
1717 * We also need to find a storage for the other link, setup variables
1718 * to do this at the end...
1720 needToFindAPlaceholder
= TRUE
;
1721 storeNode
= propertyToDelete
.previousProperty
;
1722 toStoreNode
= propertyToDelete
.nextProperty
;
1723 relationType
= PROPERTY_RELATION_NEXT
;
1726 else if (propertyToDelete
.nextProperty
!= PROPERTY_NULL
)
1729 * Set the parent next to the property to delete next
1731 newLinkProperty
= propertyToDelete
.nextProperty
;
1735 * Link it for real...
1737 parentProperty
.nextProperty
= newLinkProperty
;
1739 else /* (typeOfRelation == PROPERTY_RELATION_DIR) */
1741 if (propertyToDelete
.previousProperty
!= PROPERTY_NULL
)
1744 * Set the parent dir to the property to delete previous
1746 newLinkProperty
= propertyToDelete
.previousProperty
;
1748 if (propertyToDelete
.nextProperty
!= PROPERTY_NULL
)
1751 * We also need to find a storage for the other link, setup variables
1752 * to do this at the end...
1754 needToFindAPlaceholder
= TRUE
;
1755 storeNode
= propertyToDelete
.previousProperty
;
1756 toStoreNode
= propertyToDelete
.nextProperty
;
1757 relationType
= PROPERTY_RELATION_NEXT
;
1760 else if (propertyToDelete
.nextProperty
!= PROPERTY_NULL
)
1763 * Set the parent dir to the property to delete next
1765 newLinkProperty
= propertyToDelete
.nextProperty
;
1769 * Link it for real...
1771 parentProperty
.dirProperty
= newLinkProperty
;
1775 * Write back the parent property
1777 res
= StorageImpl_WriteProperty(
1778 This
->ancestorStorage
,
1787 * If a placeholder is required for the other link, then, find one and
1788 * get out of here...
1790 if (needToFindAPlaceholder
)
1792 hr
= findPlaceholder(
1803 /******************************************************************************
1804 * SetElementTimes (IStorage)
1806 HRESULT WINAPI
StorageImpl_SetElementTimes(
1808 const OLECHAR
*pwcsName
,/* [string][in] */
1809 const FILETIME
*pctime
, /* [in] */
1810 const FILETIME
*patime
, /* [in] */
1811 const FILETIME
*pmtime
) /* [in] */
1816 /******************************************************************************
1817 * SetStateBits (IStorage)
1819 HRESULT WINAPI
StorageImpl_SetStateBits(
1821 DWORD grfStateBits
,/* [in] */
1822 DWORD grfMask
) /* [in] */
1827 HRESULT
StorageImpl_Construct(
1833 StgProperty currentProperty
;
1835 ULONG currentPropertyIndex
;
1837 if ( FAILED( validateSTGM(openFlags
) ))
1838 return STG_E_INVALIDFLAG
;
1840 memset(This
, 0, sizeof(StorageImpl
));
1843 * Initialize the virtual fgunction table.
1845 This
->lpvtbl
= &Storage32Impl_Vtbl
;
1846 This
->v_destructor
= &StorageImpl_Destroy
;
1849 * This is the top-level storage so initialize the ancester pointer
1852 This
->ancestorStorage
= This
;
1855 * Initialize the physical support of the storage.
1857 This
->hFile
= hFile
;
1860 * Initialize the big block cache.
1862 This
->bigBlockSize
= DEF_BIG_BLOCK_SIZE
;
1863 This
->smallBlockSize
= DEF_SMALL_BLOCK_SIZE
;
1864 This
->bigBlockFile
= BIGBLOCKFILE_Construct(hFile
,
1866 This
->bigBlockSize
);
1868 if (This
->bigBlockFile
== 0)
1871 if (openFlags
& STGM_CREATE
)
1873 ULARGE_INTEGER size
;
1874 BYTE
* bigBlockBuffer
;
1877 * Initialize all header variables:
1878 * - The big block depot consists of one block and it is at block 0
1879 * - The properties start at block 1
1880 * - There is no small block depot
1882 memset( This
->bigBlockDepotStart
,
1884 sizeof(This
->bigBlockDepotStart
));
1886 This
->bigBlockDepotCount
= 1;
1887 This
->bigBlockDepotStart
[0] = 0;
1888 This
->rootStartBlock
= 1;
1889 This
->smallBlockDepotStart
= BLOCK_END_OF_CHAIN
;
1890 This
->bigBlockSizeBits
= DEF_BIG_BLOCK_SIZE_BITS
;
1891 This
->smallBlockSizeBits
= DEF_SMALL_BLOCK_SIZE_BITS
;
1892 This
->extBigBlockDepotStart
= BLOCK_END_OF_CHAIN
;
1893 This
->extBigBlockDepotCount
= 0;
1895 StorageImpl_SaveFileHeader(This
);
1898 * Add one block for the big block depot and one block for the properties
1901 size
.LowPart
= This
->bigBlockSize
* 3;
1902 BIGBLOCKFILE_SetSize(This
->bigBlockFile
, size
);
1905 * Initialize the big block depot
1907 bigBlockBuffer
= StorageImpl_GetBigBlock(This
, 0);
1908 memset(bigBlockBuffer
, BLOCK_UNUSED
, This
->bigBlockSize
);
1909 StorageUtl_WriteDWord(bigBlockBuffer
, 0, BLOCK_SPECIAL
);
1910 StorageUtl_WriteDWord(bigBlockBuffer
, sizeof(ULONG
), BLOCK_END_OF_CHAIN
);
1911 StorageImpl_ReleaseBigBlock(This
, bigBlockBuffer
);
1916 * Load the header for the file.
1918 StorageImpl_LoadFileHeader(This
);
1922 * There is no block depot cached yet.
1924 This
->indexBlockDepotCached
= 0xFFFFFFFF;
1927 * Start searching for free blocks with block 0.
1929 This
->prevFreeBlock
= 0;
1932 * Create the block chain abstractions.
1934 This
->rootBlockChain
=
1935 BlockChainStream_Construct(This
, &This
->rootStartBlock
, PROPERTY_NULL
);
1937 This
->smallBlockDepotChain
= BlockChainStream_Construct(
1939 &This
->smallBlockDepotStart
,
1943 * Write the root property
1945 if (openFlags
& STGM_CREATE
)
1947 StgProperty rootProp
;
1949 * Initialize the property chain
1951 memset(&rootProp
, 0, sizeof(rootProp
));
1952 lstrcpyAtoW(rootProp
.name
, rootPropertyName
);
1954 rootProp
.sizeOfNameString
= (lstrlenW(rootProp
.name
)+1) * sizeof(WCHAR
);
1955 rootProp
.propertyType
= PROPTYPE_ROOT
;
1956 rootProp
.previousProperty
= PROPERTY_NULL
;
1957 rootProp
.nextProperty
= PROPERTY_NULL
;
1958 rootProp
.dirProperty
= PROPERTY_NULL
;
1959 rootProp
.startingBlock
= BLOCK_END_OF_CHAIN
;
1960 rootProp
.size
.HighPart
= 0;
1961 rootProp
.size
.LowPart
= 0;
1963 StorageImpl_WriteProperty(This
, 0, &rootProp
);
1967 * Find the ID of the root int he property sets.
1969 currentPropertyIndex
= 0;
1973 readSucessful
= StorageImpl_ReadProperty(
1975 currentPropertyIndex
,
1980 if ( (currentProperty
.sizeOfNameString
!= 0 ) &&
1981 (currentProperty
.propertyType
== PROPTYPE_ROOT
) )
1983 This
->rootPropertySetIndex
= currentPropertyIndex
;
1987 currentPropertyIndex
++;
1989 } while (readSucessful
&& (This
->rootPropertySetIndex
== PROPERTY_NULL
) );
1998 * Create the block chain abstraction for the small block root chain.
2000 This
->smallBlockRootChain
= BlockChainStream_Construct(
2003 This
->rootPropertySetIndex
);
2008 void StorageImpl_Destroy(
2011 BlockChainStream_Destroy(This
->smallBlockRootChain
);
2012 BlockChainStream_Destroy(This
->rootBlockChain
);
2013 BlockChainStream_Destroy(This
->smallBlockDepotChain
);
2015 BIGBLOCKFILE_Destructor(This
->bigBlockFile
);
2019 /******************************************************************************
2020 * Storage32Impl_GetNextFreeBigBlock
2022 * Returns the index of the next free big block.
2023 * If the big block depot is filled, this method will enlarge it.
2026 ULONG
StorageImpl_GetNextFreeBigBlock(
2029 ULONG depotBlockIndexPos
;
2031 ULONG depotBlockOffset
;
2032 ULONG blocksPerDepot
= This
->bigBlockSize
/ sizeof(ULONG
);
2033 ULONG nextBlockIndex
= BLOCK_SPECIAL
;
2035 ULONG freeBlock
= BLOCK_UNUSED
;
2037 depotIndex
= This
->prevFreeBlock
/ blocksPerDepot
;
2038 depotBlockOffset
= (This
->prevFreeBlock
% blocksPerDepot
) * sizeof(ULONG
);
2041 * Scan the entire big block depot until we find a block marked free
2043 while (nextBlockIndex
!= BLOCK_UNUSED
)
2045 if (depotIndex
< COUNT_BBDEPOTINHEADER
)
2047 depotBlockIndexPos
= This
->bigBlockDepotStart
[depotIndex
];
2050 * Grow the primary depot.
2052 if (depotBlockIndexPos
== BLOCK_UNUSED
)
2054 depotBlockIndexPos
= depotIndex
*blocksPerDepot
;
2057 * Add a block depot.
2059 Storage32Impl_AddBlockDepot(This
, depotBlockIndexPos
);
2060 This
->bigBlockDepotCount
++;
2061 This
->bigBlockDepotStart
[depotIndex
] = depotBlockIndexPos
;
2064 * Flag it as a block depot.
2066 StorageImpl_SetNextBlockInChain(This
,
2070 /* Save new header information.
2072 StorageImpl_SaveFileHeader(This
);
2077 depotBlockIndexPos
= Storage32Impl_GetExtDepotBlock(This
, depotIndex
);
2079 if (depotBlockIndexPos
== BLOCK_UNUSED
)
2082 * Grow the extended depot.
2084 ULONG extIndex
= BLOCK_UNUSED
;
2085 ULONG numExtBlocks
= depotIndex
- COUNT_BBDEPOTINHEADER
;
2086 ULONG extBlockOffset
= numExtBlocks
% (blocksPerDepot
- 1);
2088 if (extBlockOffset
== 0)
2090 /* We need an extended block.
2092 extIndex
= Storage32Impl_AddExtBlockDepot(This
);
2093 This
->extBigBlockDepotCount
++;
2094 depotBlockIndexPos
= extIndex
+ 1;
2097 depotBlockIndexPos
= depotIndex
* blocksPerDepot
;
2100 * Add a block depot and mark it in the extended block.
2102 Storage32Impl_AddBlockDepot(This
, depotBlockIndexPos
);
2103 This
->bigBlockDepotCount
++;
2104 Storage32Impl_SetExtDepotBlock(This
, depotIndex
, depotBlockIndexPos
);
2106 /* Flag the block depot.
2108 StorageImpl_SetNextBlockInChain(This
,
2112 /* If necessary, flag the extended depot block.
2114 if (extIndex
!= BLOCK_UNUSED
)
2115 StorageImpl_SetNextBlockInChain(This
, extIndex
, BLOCK_EXTBBDEPOT
);
2117 /* Save header information.
2119 StorageImpl_SaveFileHeader(This
);
2123 depotBuffer
= StorageImpl_GetROBigBlock(This
, depotBlockIndexPos
);
2125 if (depotBuffer
!= 0)
2127 while ( ( (depotBlockOffset
/sizeof(ULONG
) ) < blocksPerDepot
) &&
2128 ( nextBlockIndex
!= BLOCK_UNUSED
))
2130 StorageUtl_ReadDWord(depotBuffer
, depotBlockOffset
, &nextBlockIndex
);
2132 if (nextBlockIndex
== BLOCK_UNUSED
)
2134 freeBlock
= (depotIndex
* blocksPerDepot
) +
2135 (depotBlockOffset
/sizeof(ULONG
));
2138 depotBlockOffset
+= sizeof(ULONG
);
2141 StorageImpl_ReleaseBigBlock(This
, depotBuffer
);
2145 depotBlockOffset
= 0;
2148 This
->prevFreeBlock
= freeBlock
;
2153 /******************************************************************************
2154 * Storage32Impl_AddBlockDepot
2156 * This will create a depot block, essentially it is a block initialized
2159 void Storage32Impl_AddBlockDepot(StorageImpl
* This
, ULONG blockIndex
)
2163 blockBuffer
= StorageImpl_GetBigBlock(This
, blockIndex
);
2166 * Initialize blocks as free
2168 memset(blockBuffer
, BLOCK_UNUSED
, This
->bigBlockSize
);
2170 StorageImpl_ReleaseBigBlock(This
, blockBuffer
);
2173 /******************************************************************************
2174 * Storage32Impl_GetExtDepotBlock
2176 * Returns the index of the block that corresponds to the specified depot
2177 * index. This method is only for depot indexes equal or greater than
2178 * COUNT_BBDEPOTINHEADER.
2180 ULONG
Storage32Impl_GetExtDepotBlock(StorageImpl
* This
, ULONG depotIndex
)
2182 ULONG depotBlocksPerExtBlock
= (This
->bigBlockSize
/ sizeof(ULONG
)) - 1;
2183 ULONG numExtBlocks
= depotIndex
- COUNT_BBDEPOTINHEADER
;
2184 ULONG extBlockCount
= numExtBlocks
/ depotBlocksPerExtBlock
;
2185 ULONG extBlockOffset
= numExtBlocks
% depotBlocksPerExtBlock
;
2186 ULONG blockIndex
= BLOCK_UNUSED
;
2187 ULONG extBlockIndex
= This
->extBigBlockDepotStart
;
2189 assert(depotIndex
>= COUNT_BBDEPOTINHEADER
);
2191 if (This
->extBigBlockDepotStart
== BLOCK_END_OF_CHAIN
)
2192 return BLOCK_UNUSED
;
2194 while (extBlockCount
> 0)
2196 extBlockIndex
= Storage32Impl_GetNextExtendedBlock(This
, extBlockIndex
);
2200 if (extBlockIndex
!= BLOCK_UNUSED
)
2204 depotBuffer
= StorageImpl_GetROBigBlock(This
, extBlockIndex
);
2206 if (depotBuffer
!= 0)
2208 StorageUtl_ReadDWord(depotBuffer
,
2209 extBlockOffset
* sizeof(ULONG
),
2212 StorageImpl_ReleaseBigBlock(This
, depotBuffer
);
2219 /******************************************************************************
2220 * Storage32Impl_SetExtDepotBlock
2222 * Associates the specified block index to the specified depot index.
2223 * This method is only for depot indexes equal or greater than
2224 * COUNT_BBDEPOTINHEADER.
2226 void Storage32Impl_SetExtDepotBlock(StorageImpl
* This
,
2230 ULONG depotBlocksPerExtBlock
= (This
->bigBlockSize
/ sizeof(ULONG
)) - 1;
2231 ULONG numExtBlocks
= depotIndex
- COUNT_BBDEPOTINHEADER
;
2232 ULONG extBlockCount
= numExtBlocks
/ depotBlocksPerExtBlock
;
2233 ULONG extBlockOffset
= numExtBlocks
% depotBlocksPerExtBlock
;
2234 ULONG extBlockIndex
= This
->extBigBlockDepotStart
;
2236 assert(depotIndex
>= COUNT_BBDEPOTINHEADER
);
2238 while (extBlockCount
> 0)
2240 extBlockIndex
= Storage32Impl_GetNextExtendedBlock(This
, extBlockIndex
);
2244 if (extBlockIndex
!= BLOCK_UNUSED
)
2248 depotBuffer
= StorageImpl_GetBigBlock(This
, extBlockIndex
);
2250 if (depotBuffer
!= 0)
2252 StorageUtl_WriteDWord(depotBuffer
,
2253 extBlockOffset
* sizeof(ULONG
),
2256 StorageImpl_ReleaseBigBlock(This
, depotBuffer
);
2261 /******************************************************************************
2262 * Storage32Impl_AddExtBlockDepot
2264 * Creates an extended depot block.
2266 ULONG
Storage32Impl_AddExtBlockDepot(StorageImpl
* This
)
2268 ULONG numExtBlocks
= This
->extBigBlockDepotCount
;
2269 ULONG nextExtBlock
= This
->extBigBlockDepotStart
;
2270 BYTE
* depotBuffer
= NULL
;
2271 ULONG index
= BLOCK_UNUSED
;
2272 ULONG nextBlockOffset
= This
->bigBlockSize
- sizeof(ULONG
);
2273 ULONG blocksPerDepotBlock
= This
->bigBlockSize
/ sizeof(ULONG
);
2274 ULONG depotBlocksPerExtBlock
= blocksPerDepotBlock
- 1;
2276 index
= (COUNT_BBDEPOTINHEADER
+ (numExtBlocks
* depotBlocksPerExtBlock
)) *
2277 blocksPerDepotBlock
;
2279 if ((numExtBlocks
== 0) && (nextExtBlock
== BLOCK_END_OF_CHAIN
))
2282 * The first extended block.
2284 This
->extBigBlockDepotStart
= index
;
2290 * Follow the chain to the last one.
2292 for (i
= 0; i
< (numExtBlocks
- 1); i
++)
2294 nextExtBlock
= Storage32Impl_GetNextExtendedBlock(This
, nextExtBlock
);
2298 * Add the new extended block to the chain.
2300 depotBuffer
= StorageImpl_GetBigBlock(This
, nextExtBlock
);
2301 StorageUtl_WriteDWord(depotBuffer
, nextBlockOffset
, index
);
2302 StorageImpl_ReleaseBigBlock(This
, depotBuffer
);
2306 * Initialize this block.
2308 depotBuffer
= StorageImpl_GetBigBlock(This
, index
);
2309 memset(depotBuffer
, BLOCK_UNUSED
, This
->bigBlockSize
);
2310 StorageImpl_ReleaseBigBlock(This
, depotBuffer
);
2315 /******************************************************************************
2316 * Storage32Impl_FreeBigBlock
2318 * This method will flag the specified block as free in the big block depot.
2320 void StorageImpl_FreeBigBlock(
2324 StorageImpl_SetNextBlockInChain(This
, blockIndex
, BLOCK_UNUSED
);
2326 if (blockIndex
< This
->prevFreeBlock
)
2327 This
->prevFreeBlock
= blockIndex
;
2330 /************************************************************************
2331 * Storage32Impl_GetNextBlockInChain
2333 * This method will retrieve the block index of the next big block in
2336 * Params: This - Pointer to the Storage object.
2337 * blockIndex - Index of the block to retrieve the chain
2340 * Returns: This method returns the index of the next block in the chain.
2341 * It will return the constants:
2342 * BLOCK_SPECIAL - If the block given was not part of a
2344 * BLOCK_END_OF_CHAIN - If the block given was the last in
2346 * BLOCK_UNUSED - If the block given was not past of a chain
2348 * BLOCK_EXTBBDEPOT - This block is part of the extended
2351 * See Windows documentation for more details on IStorage methods.
2353 ULONG
StorageImpl_GetNextBlockInChain(
2357 ULONG offsetInDepot
= blockIndex
* sizeof (ULONG
);
2358 ULONG depotBlockCount
= offsetInDepot
/ This
->bigBlockSize
;
2359 ULONG depotBlockOffset
= offsetInDepot
% This
->bigBlockSize
;
2360 ULONG nextBlockIndex
= BLOCK_SPECIAL
;
2362 ULONG depotBlockIndexPos
;
2364 assert(depotBlockCount
< This
->bigBlockDepotCount
);
2367 * Cache the currently accessed depot block.
2369 if (depotBlockCount
!= This
->indexBlockDepotCached
)
2371 This
->indexBlockDepotCached
= depotBlockCount
;
2373 if (depotBlockCount
< COUNT_BBDEPOTINHEADER
)
2375 depotBlockIndexPos
= This
->bigBlockDepotStart
[depotBlockCount
];
2380 * We have to look in the extended depot.
2382 depotBlockIndexPos
= Storage32Impl_GetExtDepotBlock(This
, depotBlockCount
);
2385 depotBuffer
= StorageImpl_GetROBigBlock(This
, depotBlockIndexPos
);
2391 for (index
= 0; index
< NUM_BLOCKS_PER_DEPOT_BLOCK
; index
++)
2393 StorageUtl_ReadDWord(depotBuffer
, index
*sizeof(ULONG
), &nextBlockIndex
);
2394 This
->blockDepotCached
[index
] = nextBlockIndex
;
2397 StorageImpl_ReleaseBigBlock(This
, depotBuffer
);
2401 nextBlockIndex
= This
->blockDepotCached
[depotBlockOffset
/sizeof(ULONG
)];
2403 return nextBlockIndex
;
2406 /******************************************************************************
2407 * Storage32Impl_GetNextExtendedBlock
2409 * Given an extended block this method will return the next extended block.
2412 * The last ULONG of an extended block is the block index of the next
2413 * extended block. Extended blocks are marked as BLOCK_EXTBBDEPOT in the
2417 * - The index of the next extended block
2418 * - BLOCK_UNUSED: there is no next extended block.
2419 * - Any other return values denotes failure.
2421 ULONG
Storage32Impl_GetNextExtendedBlock(StorageImpl
* This
, ULONG blockIndex
)
2423 ULONG nextBlockIndex
= BLOCK_SPECIAL
;
2424 ULONG depotBlockOffset
= This
->bigBlockSize
- sizeof(ULONG
);
2427 depotBuffer
= StorageImpl_GetROBigBlock(This
, blockIndex
);
2431 StorageUtl_ReadDWord(depotBuffer
, depotBlockOffset
, &nextBlockIndex
);
2433 StorageImpl_ReleaseBigBlock(This
, depotBuffer
);
2436 return nextBlockIndex
;
2439 /******************************************************************************
2440 * Storage32Impl_SetNextBlockInChain
2442 * This method will write the index of the specified block's next block
2443 * in the big block depot.
2445 * For example: to create the chain 3 -> 1 -> 7 -> End of Chain
2448 * Storage32Impl_SetNextBlockInChain(This, 3, 1);
2449 * Storage32Impl_SetNextBlockInChain(This, 1, 7);
2450 * Storage32Impl_SetNextBlockInChain(This, 7, BLOCK_END_OF_CHAIN);
2453 void StorageImpl_SetNextBlockInChain(
2458 ULONG offsetInDepot
= blockIndex
* sizeof (ULONG
);
2459 ULONG depotBlockCount
= offsetInDepot
/ This
->bigBlockSize
;
2460 ULONG depotBlockOffset
= offsetInDepot
% This
->bigBlockSize
;
2461 ULONG depotBlockIndexPos
;
2464 assert(depotBlockCount
< This
->bigBlockDepotCount
);
2465 assert(blockIndex
!= nextBlock
);
2467 if (depotBlockCount
< COUNT_BBDEPOTINHEADER
)
2469 depotBlockIndexPos
= This
->bigBlockDepotStart
[depotBlockCount
];
2474 * We have to look in the extended depot.
2476 depotBlockIndexPos
= Storage32Impl_GetExtDepotBlock(This
, depotBlockCount
);
2479 depotBuffer
= StorageImpl_GetBigBlock(This
, depotBlockIndexPos
);
2483 StorageUtl_WriteDWord(depotBuffer
, depotBlockOffset
, nextBlock
);
2484 StorageImpl_ReleaseBigBlock(This
, depotBuffer
);
2488 * Update the cached block depot, if necessary.
2490 if (depotBlockCount
== This
->indexBlockDepotCached
)
2492 This
->blockDepotCached
[depotBlockOffset
/sizeof(ULONG
)] = nextBlock
;
2496 /******************************************************************************
2497 * Storage32Impl_LoadFileHeader
2499 * This method will read in the file header, i.e. big block index -1.
2501 HRESULT
StorageImpl_LoadFileHeader(
2504 HRESULT hr
= STG_E_FILENOTFOUND
;
2505 void* headerBigBlock
= NULL
;
2509 * Get a pointer to the big block of data containing the header.
2511 headerBigBlock
= StorageImpl_GetROBigBlock(This
, -1);
2514 * Extract the information from the header.
2516 if (headerBigBlock
!=0)
2519 * Check for the "magic number" signature and return an error if it is not
2522 if (memcmp(headerBigBlock
, STORAGE_oldmagic
, sizeof(STORAGE_oldmagic
))==0)
2524 StorageImpl_ReleaseBigBlock(This
, headerBigBlock
);
2525 return STG_E_OLDFORMAT
;
2528 if (memcmp(headerBigBlock
, STORAGE_magic
, sizeof(STORAGE_magic
))!=0)
2530 StorageImpl_ReleaseBigBlock(This
, headerBigBlock
);
2531 return STG_E_INVALIDHEADER
;
2534 StorageUtl_ReadWord(
2536 OFFSET_BIGBLOCKSIZEBITS
,
2537 &This
->bigBlockSizeBits
);
2539 StorageUtl_ReadWord(
2541 OFFSET_SMALLBLOCKSIZEBITS
,
2542 &This
->smallBlockSizeBits
);
2544 StorageUtl_ReadDWord(
2546 OFFSET_BBDEPOTCOUNT
,
2547 &This
->bigBlockDepotCount
);
2549 StorageUtl_ReadDWord(
2551 OFFSET_ROOTSTARTBLOCK
,
2552 &This
->rootStartBlock
);
2554 StorageUtl_ReadDWord(
2556 OFFSET_SBDEPOTSTART
,
2557 &This
->smallBlockDepotStart
);
2559 StorageUtl_ReadDWord(
2561 OFFSET_EXTBBDEPOTSTART
,
2562 &This
->extBigBlockDepotStart
);
2564 StorageUtl_ReadDWord(
2566 OFFSET_EXTBBDEPOTCOUNT
,
2567 &This
->extBigBlockDepotCount
);
2569 for (index
= 0; index
< COUNT_BBDEPOTINHEADER
; index
++)
2571 StorageUtl_ReadDWord(
2573 OFFSET_BBDEPOTSTART
+ (sizeof(ULONG
)*index
),
2574 &(This
->bigBlockDepotStart
[index
]));
2578 * Make the bitwise arithmetic to get the size of the blocks in bytes.
2582 This
->bigBlockSize
= 0x000000001 << (DWORD
)This
->bigBlockSizeBits
;
2583 This
->smallBlockSize
= 0x000000001 << (DWORD
)This
->smallBlockSizeBits
;
2587 This
->bigBlockSize
= 0x000000001 >> (DWORD
)This
->bigBlockSizeBits
;
2588 This
->smallBlockSize
= 0x000000001 >> (DWORD
)This
->smallBlockSizeBits
;
2592 * Right now, the code is making some assumptions about the size of the
2593 * blocks, just make sure they are what we're expecting.
2595 assert( (This
->bigBlockSize
==DEF_BIG_BLOCK_SIZE
) &&
2596 (This
->smallBlockSize
==DEF_SMALL_BLOCK_SIZE
));
2599 * Release the block.
2601 StorageImpl_ReleaseBigBlock(This
, headerBigBlock
);
2607 /******************************************************************************
2608 * Storage32Impl_SaveFileHeader
2610 * This method will save to the file the header, i.e. big block -1.
2612 void StorageImpl_SaveFileHeader(
2615 BYTE headerBigBlock
[BIG_BLOCK_SIZE
];
2620 * Get a pointer to the big block of data containing the header.
2622 success
= StorageImpl_ReadBigBlock(This
, -1, headerBigBlock
);
2625 * If the block read failed, the file is probably new.
2630 * Initialize for all unknown fields.
2632 memset(headerBigBlock
, 0, BIG_BLOCK_SIZE
);
2635 * Initialize the magic number.
2637 memcpy(headerBigBlock
, STORAGE_magic
, sizeof(STORAGE_magic
));
2640 * And a bunch of things we don't know what they mean
2642 StorageUtl_WriteWord(headerBigBlock
, 0x18, 0x3b);
2643 StorageUtl_WriteWord(headerBigBlock
, 0x1a, 0x3);
2644 StorageUtl_WriteWord(headerBigBlock
, 0x1c, (WORD
)-2);
2645 StorageUtl_WriteDWord(headerBigBlock
, 0x38, (DWORD
)0x1000);
2646 StorageUtl_WriteDWord(headerBigBlock
, 0x40, (DWORD
)0x0001);
2650 * Write the information to the header.
2652 if (headerBigBlock
!=0)
2654 StorageUtl_WriteWord(
2656 OFFSET_BIGBLOCKSIZEBITS
,
2657 This
->bigBlockSizeBits
);
2659 StorageUtl_WriteWord(
2661 OFFSET_SMALLBLOCKSIZEBITS
,
2662 This
->smallBlockSizeBits
);
2664 StorageUtl_WriteDWord(
2666 OFFSET_BBDEPOTCOUNT
,
2667 This
->bigBlockDepotCount
);
2669 StorageUtl_WriteDWord(
2671 OFFSET_ROOTSTARTBLOCK
,
2672 This
->rootStartBlock
);
2674 StorageUtl_WriteDWord(
2676 OFFSET_SBDEPOTSTART
,
2677 This
->smallBlockDepotStart
);
2679 StorageUtl_WriteDWord(
2681 OFFSET_EXTBBDEPOTSTART
,
2682 This
->extBigBlockDepotStart
);
2684 StorageUtl_WriteDWord(
2686 OFFSET_EXTBBDEPOTCOUNT
,
2687 This
->extBigBlockDepotCount
);
2689 for (index
= 0; index
< COUNT_BBDEPOTINHEADER
; index
++)
2691 StorageUtl_WriteDWord(
2693 OFFSET_BBDEPOTSTART
+ (sizeof(ULONG
)*index
),
2694 (This
->bigBlockDepotStart
[index
]));
2699 * Write the big block back to the file.
2701 StorageImpl_WriteBigBlock(This
, -1, headerBigBlock
);
2704 /******************************************************************************
2705 * Storage32Impl_ReadProperty
2707 * This method will read the specified property from the property chain.
2709 BOOL
StorageImpl_ReadProperty(
2712 StgProperty
* buffer
)
2714 BYTE currentProperty
[PROPSET_BLOCK_SIZE
];
2715 ULARGE_INTEGER offsetInPropSet
;
2719 offsetInPropSet
.HighPart
= 0;
2720 offsetInPropSet
.LowPart
= index
* PROPSET_BLOCK_SIZE
;
2722 readSucessful
= BlockChainStream_ReadAt(
2723 This
->rootBlockChain
,
2731 memset(buffer
->name
, 0, sizeof(buffer
->name
));
2734 currentProperty
+OFFSET_PS_NAME
,
2735 PROPERTY_NAME_BUFFER_LEN
);
2737 memcpy(&buffer
->propertyType
, currentProperty
+ OFFSET_PS_PROPERTYTYPE
, 1);
2739 StorageUtl_ReadWord(
2741 OFFSET_PS_NAMELENGTH
,
2742 &buffer
->sizeOfNameString
);
2744 StorageUtl_ReadDWord(
2746 OFFSET_PS_PREVIOUSPROP
,
2747 &buffer
->previousProperty
);
2749 StorageUtl_ReadDWord(
2752 &buffer
->nextProperty
);
2754 StorageUtl_ReadDWord(
2757 &buffer
->dirProperty
);
2759 StorageUtl_ReadGUID(
2762 &buffer
->propertyUniqueID
);
2764 StorageUtl_ReadDWord(
2767 &buffer
->timeStampS1
);
2769 StorageUtl_ReadDWord(
2772 &buffer
->timeStampD1
);
2774 StorageUtl_ReadDWord(
2777 &buffer
->timeStampS2
);
2779 StorageUtl_ReadDWord(
2782 &buffer
->timeStampD2
);
2784 StorageUtl_ReadDWord(
2786 OFFSET_PS_STARTBLOCK
,
2787 &buffer
->startingBlock
);
2789 StorageUtl_ReadDWord(
2792 &buffer
->size
.LowPart
);
2794 buffer
->size
.HighPart
= 0;
2797 return readSucessful
;
2800 /*********************************************************************
2801 * Write the specified property into the property chain
2803 BOOL
StorageImpl_WriteProperty(
2806 StgProperty
* buffer
)
2808 BYTE currentProperty
[PROPSET_BLOCK_SIZE
];
2809 ULARGE_INTEGER offsetInPropSet
;
2810 BOOL writeSucessful
;
2813 offsetInPropSet
.HighPart
= 0;
2814 offsetInPropSet
.LowPart
= index
* PROPSET_BLOCK_SIZE
;
2816 memset(currentProperty
, 0, PROPSET_BLOCK_SIZE
);
2819 currentProperty
+ OFFSET_PS_NAME
,
2821 PROPERTY_NAME_BUFFER_LEN
);
2823 memcpy(currentProperty
+ OFFSET_PS_PROPERTYTYPE
, &buffer
->propertyType
, 1);
2826 * Reassign the size in case of mistake....
2828 buffer
->sizeOfNameString
= (lstrlenW(buffer
->name
)+1) * sizeof(WCHAR
);
2830 StorageUtl_WriteWord(
2832 OFFSET_PS_NAMELENGTH
,
2833 buffer
->sizeOfNameString
);
2835 StorageUtl_WriteDWord(
2837 OFFSET_PS_PREVIOUSPROP
,
2838 buffer
->previousProperty
);
2840 StorageUtl_WriteDWord(
2843 buffer
->nextProperty
);
2845 StorageUtl_WriteDWord(
2848 buffer
->dirProperty
);
2850 StorageUtl_WriteGUID(
2853 &buffer
->propertyUniqueID
);
2855 StorageUtl_WriteDWord(
2858 buffer
->timeStampS1
);
2860 StorageUtl_WriteDWord(
2863 buffer
->timeStampD1
);
2865 StorageUtl_WriteDWord(
2868 buffer
->timeStampS2
);
2870 StorageUtl_WriteDWord(
2873 buffer
->timeStampD2
);
2875 StorageUtl_WriteDWord(
2877 OFFSET_PS_STARTBLOCK
,
2878 buffer
->startingBlock
);
2880 StorageUtl_WriteDWord(
2883 buffer
->size
.LowPart
);
2885 writeSucessful
= BlockChainStream_WriteAt(This
->rootBlockChain
,
2890 return writeSucessful
;
2893 BOOL
StorageImpl_ReadBigBlock(
2898 void* bigBlockBuffer
;
2900 bigBlockBuffer
= StorageImpl_GetROBigBlock(This
, blockIndex
);
2902 if (bigBlockBuffer
!=0)
2904 memcpy(buffer
, bigBlockBuffer
, This
->bigBlockSize
);
2906 StorageImpl_ReleaseBigBlock(This
, bigBlockBuffer
);
2914 BOOL
StorageImpl_WriteBigBlock(
2919 void* bigBlockBuffer
;
2921 bigBlockBuffer
= StorageImpl_GetBigBlock(This
, blockIndex
);
2923 if (bigBlockBuffer
!=0)
2925 memcpy(bigBlockBuffer
, buffer
, This
->bigBlockSize
);
2927 StorageImpl_ReleaseBigBlock(This
, bigBlockBuffer
);
2935 void* StorageImpl_GetROBigBlock(
2939 return BIGBLOCKFILE_GetROBigBlock(This
->bigBlockFile
, blockIndex
);
2942 void* StorageImpl_GetBigBlock(
2946 return BIGBLOCKFILE_GetBigBlock(This
->bigBlockFile
, blockIndex
);
2949 void StorageImpl_ReleaseBigBlock(
2953 BIGBLOCKFILE_ReleaseBigBlock(This
->bigBlockFile
, pBigBlock
);
2956 /******************************************************************************
2957 * Storage32Impl_SmallBlocksToBigBlocks
2959 * This method will convert a small block chain to a big block chain.
2960 * The small block chain will be destroyed.
2962 BlockChainStream
* Storage32Impl_SmallBlocksToBigBlocks(
2964 SmallBlockChainStream
** ppsbChain
)
2966 ULONG bbHeadOfChain
= BLOCK_END_OF_CHAIN
;
2967 ULARGE_INTEGER size
, offset
;
2968 ULONG cbRead
, cbWritten
, cbTotalRead
, cbTotalWritten
;
2969 ULONG propertyIndex
;
2970 BOOL successRead
, successWrite
;
2971 StgProperty chainProperty
;
2972 BYTE buffer
[DEF_SMALL_BLOCK_SIZE
];
2973 BlockChainStream
*bbTempChain
= NULL
;
2974 BlockChainStream
*bigBlockChain
= NULL
;
2977 * Create a temporary big block chain that doesn't have
2978 * an associated property. This temporary chain will be
2979 * used to copy data from small blocks to big blocks.
2981 bbTempChain
= BlockChainStream_Construct(This
,
2986 * Grow the big block chain.
2988 size
= SmallBlockChainStream_GetSize(*ppsbChain
);
2989 BlockChainStream_SetSize(bbTempChain
, size
);
2992 * Copy the contents of the small block chain to the big block chain
2993 * by small block size increments.
2996 offset
.HighPart
= 0;
3002 successRead
= SmallBlockChainStream_ReadAt(*ppsbChain
,
3007 cbTotalRead
+= cbRead
;
3009 successWrite
= BlockChainStream_WriteAt(bbTempChain
,
3014 cbTotalWritten
+= cbWritten
;
3016 offset
.LowPart
+= This
->smallBlockSize
;
3018 } while (successRead
&& successWrite
);
3020 assert(cbTotalRead
== cbTotalWritten
);
3023 * Destroy the small block chain.
3025 propertyIndex
= (*ppsbChain
)->ownerPropertyIndex
;
3028 SmallBlockChainStream_SetSize(*ppsbChain
, size
);
3029 SmallBlockChainStream_Destroy(*ppsbChain
);
3033 * Change the property information. This chain is now a big block chain
3034 * and it doesn't reside in the small blocks chain anymore.
3036 StorageImpl_ReadProperty(This
, propertyIndex
, &chainProperty
);
3038 chainProperty
.startingBlock
= bbHeadOfChain
;
3040 StorageImpl_WriteProperty(This
, propertyIndex
, &chainProperty
);
3043 * Destroy the temporary propertyless big block chain.
3044 * Create a new big block chain associated with this property.
3046 BlockChainStream_Destroy(bbTempChain
);
3047 bigBlockChain
= BlockChainStream_Construct(This
,
3051 return bigBlockChain
;
3054 /******************************************************************************
3055 ** Storage32InternalImpl implementation
3058 StorageInternalImpl
* StorageInternalImpl_Construct(
3059 StorageImpl
* ancestorStorage
,
3060 ULONG rootPropertyIndex
)
3062 StorageInternalImpl
* newStorage
;
3065 * Allocate space for the new storage object
3067 newStorage
= HeapAlloc(GetProcessHeap(), 0, sizeof(StorageInternalImpl
));
3071 memset(newStorage
, 0, sizeof(StorageInternalImpl
));
3074 * Initialize the virtual function table.
3076 newStorage
->lpvtbl
= &Storage32InternalImpl_Vtbl
;
3077 newStorage
->v_destructor
= &StorageInternalImpl_Destroy
;
3080 * Keep the ancestor storage pointer and nail a reference to it.
3082 newStorage
->ancestorStorage
= ancestorStorage
;
3083 StorageBaseImpl_AddRef((IStorage
*)(newStorage
->ancestorStorage
));
3086 * Keep the index of the root property set for this storage,
3088 newStorage
->rootPropertySetIndex
= rootPropertyIndex
;
3096 void StorageInternalImpl_Destroy(
3097 StorageInternalImpl
* This
)
3099 StorageBaseImpl_Release((IStorage
*)This
->ancestorStorage
);
3100 HeapFree(GetProcessHeap(), 0, This
);
3103 /******************************************************************************
3105 ** Storage32InternalImpl_Commit
3107 ** The non-root storages cannot be opened in transacted mode thus this function
3110 HRESULT WINAPI
StorageInternalImpl_Commit(
3112 DWORD grfCommitFlags
) /* [in] */
3117 /******************************************************************************
3119 ** Storage32InternalImpl_Revert
3121 ** The non-root storages cannot be opened in transacted mode thus this function
3124 HRESULT WINAPI
StorageInternalImpl_Revert(
3130 /******************************************************************************
3131 ** IEnumSTATSTGImpl implementation
3134 IEnumSTATSTGImpl
* IEnumSTATSTGImpl_Construct(
3135 StorageImpl
* parentStorage
,
3136 ULONG firstPropertyNode
)
3138 IEnumSTATSTGImpl
* newEnumeration
;
3140 newEnumeration
= HeapAlloc(GetProcessHeap(), 0, sizeof(IEnumSTATSTGImpl
));
3142 if (newEnumeration
!=0)
3145 * Set-up the virtual function table and reference count.
3147 newEnumeration
->lpvtbl
= &IEnumSTATSTGImpl_Vtbl
;
3148 newEnumeration
->ref
= 0;
3151 * We want to nail-down the reference to the storage in case the
3152 * enumeration out-lives the storage in the client application.
3154 newEnumeration
->parentStorage
= parentStorage
;
3155 IStorage_AddRef((IStorage
*)newEnumeration
->parentStorage
);
3157 newEnumeration
->firstPropertyNode
= firstPropertyNode
;
3160 * Initialize the search stack
3162 newEnumeration
->stackSize
= 0;
3163 newEnumeration
->stackMaxSize
= ENUMSTATSGT_SIZE_INCREMENT
;
3164 newEnumeration
->stackToVisit
=
3165 HeapAlloc(GetProcessHeap(), 0, sizeof(ULONG
)*ENUMSTATSGT_SIZE_INCREMENT
);
3168 * Make sure the current node of the iterator is the first one.
3170 IEnumSTATSTGImpl_Reset((IEnumSTATSTG
*)newEnumeration
);
3173 return newEnumeration
;
3176 void IEnumSTATSTGImpl_Destroy(IEnumSTATSTGImpl
* This
)
3178 IStorage_Release((IStorage
*)This
->parentStorage
);
3179 HeapFree(GetProcessHeap(), 0, This
->stackToVisit
);
3180 HeapFree(GetProcessHeap(), 0, This
);
3183 HRESULT WINAPI
IEnumSTATSTGImpl_QueryInterface(
3184 IEnumSTATSTG
* iface
,
3188 IEnumSTATSTGImpl
* const This
=(IEnumSTATSTGImpl
*)iface
;
3191 * Perform a sanity check on the parameters.
3194 return E_INVALIDARG
;
3197 * Initialize the return parameter.
3202 * Compare the riid with the interface IDs implemented by this object.
3204 if (memcmp(&IID_IUnknown
, riid
, sizeof(IID_IUnknown
)) == 0)
3206 *ppvObject
= (IEnumSTATSTG
*)This
;
3208 else if (memcmp(&IID_IStorage
, riid
, sizeof(IID_IEnumSTATSTG
)) == 0)
3210 *ppvObject
= (IEnumSTATSTG
*)This
;
3214 * Check that we obtained an interface.
3216 if ((*ppvObject
)==0)
3217 return E_NOINTERFACE
;
3220 * Query Interface always increases the reference count by one when it is
3223 IEnumSTATSTGImpl_AddRef((IEnumSTATSTG
*)This
);
3228 ULONG WINAPI
IEnumSTATSTGImpl_AddRef(
3229 IEnumSTATSTG
* iface
)
3231 IEnumSTATSTGImpl
* const This
=(IEnumSTATSTGImpl
*)iface
;
3237 ULONG WINAPI
IEnumSTATSTGImpl_Release(
3238 IEnumSTATSTG
* iface
)
3240 IEnumSTATSTGImpl
* const This
=(IEnumSTATSTGImpl
*)iface
;
3248 * If the reference count goes down to 0, perform suicide.
3252 IEnumSTATSTGImpl_Destroy(This
);
3258 HRESULT WINAPI
IEnumSTATSTGImpl_Next(
3259 IEnumSTATSTG
* iface
,
3262 ULONG
* pceltFetched
)
3264 IEnumSTATSTGImpl
* const This
=(IEnumSTATSTGImpl
*)iface
;
3266 StgProperty currentProperty
;
3267 STATSTG
* currentReturnStruct
= rgelt
;
3268 ULONG objectFetched
= 0;
3269 ULONG currentSearchNode
;
3272 * Perform a sanity check on the parameters.
3274 if ( (rgelt
==0) || ( (celt
!=1) && (pceltFetched
==0) ) )
3275 return E_INVALIDARG
;
3278 * To avoid the special case, get another pointer to a ULONG value if
3279 * the caller didn't supply one.
3281 if (pceltFetched
==0)
3282 pceltFetched
= &objectFetched
;
3285 * Start the iteration, we will iterate until we hit the end of the
3286 * linked list or until we hit the number of items to iterate through
3291 * Start with the node at the top of the stack.
3293 currentSearchNode
= IEnumSTATSTGImpl_PopSearchNode(This
, FALSE
);
3295 while ( ( *pceltFetched
< celt
) &&
3296 ( currentSearchNode
!=PROPERTY_NULL
) )
3299 * Remove the top node from the stack
3301 IEnumSTATSTGImpl_PopSearchNode(This
, TRUE
);
3304 * Read the property from the storage.
3306 StorageImpl_ReadProperty(This
->parentStorage
,
3311 * Copy the information to the return buffer.
3313 StorageUtl_CopyPropertyToSTATSTG(currentReturnStruct
,
3318 * Step to the next item in the iteration
3321 currentReturnStruct
++;
3324 * Push the next search node in the search stack.
3326 IEnumSTATSTGImpl_PushSearchNode(This
, currentProperty
.nextProperty
);
3329 * continue the iteration.
3331 currentSearchNode
= IEnumSTATSTGImpl_PopSearchNode(This
, FALSE
);
3334 if (*pceltFetched
== celt
)
3341 HRESULT WINAPI
IEnumSTATSTGImpl_Skip(
3342 IEnumSTATSTG
* iface
,
3345 IEnumSTATSTGImpl
* const This
=(IEnumSTATSTGImpl
*)iface
;
3347 StgProperty currentProperty
;
3348 ULONG objectFetched
= 0;
3349 ULONG currentSearchNode
;
3352 * Start with the node at the top of the stack.
3354 currentSearchNode
= IEnumSTATSTGImpl_PopSearchNode(This
, FALSE
);
3356 while ( (objectFetched
< celt
) &&
3357 (currentSearchNode
!=PROPERTY_NULL
) )
3360 * Remove the top node from the stack
3362 IEnumSTATSTGImpl_PopSearchNode(This
, TRUE
);
3365 * Read the property from the storage.
3367 StorageImpl_ReadProperty(This
->parentStorage
,
3372 * Step to the next item in the iteration
3377 * Push the next search node in the search stack.
3379 IEnumSTATSTGImpl_PushSearchNode(This
, currentProperty
.nextProperty
);
3382 * continue the iteration.
3384 currentSearchNode
= IEnumSTATSTGImpl_PopSearchNode(This
, FALSE
);
3387 if (objectFetched
== celt
)
3393 HRESULT WINAPI
IEnumSTATSTGImpl_Reset(
3394 IEnumSTATSTG
* iface
)
3396 IEnumSTATSTGImpl
* const This
=(IEnumSTATSTGImpl
*)iface
;
3398 StgProperty rootProperty
;
3402 * Re-initialize the search stack to an empty stack
3404 This
->stackSize
= 0;
3407 * Read the root property from the storage.
3409 readSucessful
= StorageImpl_ReadProperty(
3410 This
->parentStorage
,
3411 This
->firstPropertyNode
,
3416 assert(rootProperty
.sizeOfNameString
!=0);
3419 * Push the search node in the search stack.
3421 IEnumSTATSTGImpl_PushSearchNode(This
, rootProperty
.dirProperty
);
3427 HRESULT WINAPI
IEnumSTATSTGImpl_Clone(
3428 IEnumSTATSTG
* iface
,
3429 IEnumSTATSTG
** ppenum
)
3431 IEnumSTATSTGImpl
* const This
=(IEnumSTATSTGImpl
*)iface
;
3433 IEnumSTATSTGImpl
* newClone
;
3436 * Perform a sanity check on the parameters.
3439 return E_INVALIDARG
;
3441 newClone
= IEnumSTATSTGImpl_Construct(This
->parentStorage
,
3442 This
->firstPropertyNode
);
3446 * The new clone enumeration must point to the same current node as
3449 newClone
->stackSize
= This
->stackSize
;
3450 newClone
->stackMaxSize
= This
->stackMaxSize
;
3451 newClone
->stackToVisit
=
3452 HeapAlloc(GetProcessHeap(), 0, sizeof(ULONG
) * newClone
->stackMaxSize
);
3455 newClone
->stackToVisit
,
3457 sizeof(ULONG
) * newClone
->stackSize
);
3459 *ppenum
= (IEnumSTATSTG
*)newClone
;
3462 * Don't forget to nail down a reference to the clone before
3465 IEnumSTATSTGImpl_AddRef(*ppenum
);
3470 INT
IEnumSTATSTGImpl_FindParentProperty(
3471 IEnumSTATSTGImpl
*This
,
3472 ULONG childProperty
,
3473 StgProperty
*currentProperty
,
3476 ULONG currentSearchNode
;
3480 * To avoid the special case, get another pointer to a ULONG value if
3481 * the caller didn't supply one.
3484 thisNodeId
= &foundNode
;
3487 * Start with the node at the top of the stack.
3489 currentSearchNode
= IEnumSTATSTGImpl_PopSearchNode(This
, FALSE
);
3492 while (currentSearchNode
!=PROPERTY_NULL
)
3495 * Store the current node in the returned parameters
3497 *thisNodeId
= currentSearchNode
;
3500 * Remove the top node from the stack
3502 IEnumSTATSTGImpl_PopSearchNode(This
, TRUE
);
3505 * Read the property from the storage.
3507 StorageImpl_ReadProperty(
3508 This
->parentStorage
,
3512 if (currentProperty
->previousProperty
== childProperty
)
3513 return PROPERTY_RELATION_PREVIOUS
;
3515 else if (currentProperty
->nextProperty
== childProperty
)
3516 return PROPERTY_RELATION_NEXT
;
3518 else if (currentProperty
->dirProperty
== childProperty
)
3519 return PROPERTY_RELATION_DIR
;
3522 * Push the next search node in the search stack.
3524 IEnumSTATSTGImpl_PushSearchNode(This
, currentProperty
->nextProperty
);
3527 * continue the iteration.
3529 currentSearchNode
= IEnumSTATSTGImpl_PopSearchNode(This
, FALSE
);
3532 return PROPERTY_NULL
;
3535 ULONG
IEnumSTATSTGImpl_FindProperty(
3536 IEnumSTATSTGImpl
* This
,
3537 const OLECHAR
* lpszPropName
,
3538 StgProperty
* currentProperty
)
3540 ULONG currentSearchNode
;
3543 * Start with the node at the top of the stack.
3545 currentSearchNode
= IEnumSTATSTGImpl_PopSearchNode(This
, FALSE
);
3547 while (currentSearchNode
!=PROPERTY_NULL
)
3550 * Remove the top node from the stack
3552 IEnumSTATSTGImpl_PopSearchNode(This
, TRUE
);
3555 * Read the property from the storage.
3557 StorageImpl_ReadProperty(This
->parentStorage
,
3561 if ( propertyNameCmp(
3562 (OLECHAR
*)currentProperty
->name
,
3563 (OLECHAR
*)lpszPropName
) == 0)
3564 return currentSearchNode
;
3567 * Push the next search node in the search stack.
3569 IEnumSTATSTGImpl_PushSearchNode(This
, currentProperty
->nextProperty
);
3572 * continue the iteration.
3574 currentSearchNode
= IEnumSTATSTGImpl_PopSearchNode(This
, FALSE
);
3577 return PROPERTY_NULL
;
3580 void IEnumSTATSTGImpl_PushSearchNode(
3581 IEnumSTATSTGImpl
* This
,
3584 StgProperty rootProperty
;
3588 * First, make sure we're not trying to push an unexisting node.
3590 if (nodeToPush
==PROPERTY_NULL
)
3594 * First push the node to the stack
3596 if (This
->stackSize
== This
->stackMaxSize
)
3598 This
->stackMaxSize
+= ENUMSTATSGT_SIZE_INCREMENT
;
3600 This
->stackToVisit
= HeapReAlloc(
3604 sizeof(ULONG
) * This
->stackMaxSize
);
3607 This
->stackToVisit
[This
->stackSize
] = nodeToPush
;
3611 * Read the root property from the storage.
3613 readSucessful
= StorageImpl_ReadProperty(
3614 This
->parentStorage
,
3620 assert(rootProperty
.sizeOfNameString
!=0);
3623 * Push the previous search node in the search stack.
3625 IEnumSTATSTGImpl_PushSearchNode(This
, rootProperty
.previousProperty
);
3629 ULONG
IEnumSTATSTGImpl_PopSearchNode(
3630 IEnumSTATSTGImpl
* This
,
3635 if (This
->stackSize
== 0)
3636 return PROPERTY_NULL
;
3638 topNode
= This
->stackToVisit
[This
->stackSize
-1];
3646 /******************************************************************************
3647 ** StorageUtl implementation
3650 void StorageUtl_ReadWord(void* buffer
, ULONG offset
, WORD
* value
)
3652 memcpy(value
, (BYTE
*)buffer
+offset
, sizeof(WORD
));
3655 void StorageUtl_WriteWord(void* buffer
, ULONG offset
, WORD value
)
3657 memcpy((BYTE
*)buffer
+offset
, &value
, sizeof(WORD
));
3660 void StorageUtl_ReadDWord(void* buffer
, ULONG offset
, DWORD
* value
)
3662 memcpy(value
, (BYTE
*)buffer
+offset
, sizeof(DWORD
));
3665 void StorageUtl_WriteDWord(void* buffer
, ULONG offset
, DWORD value
)
3667 memcpy((BYTE
*)buffer
+offset
, &value
, sizeof(DWORD
));
3670 void StorageUtl_ReadGUID(void* buffer
, ULONG offset
, GUID
* value
)
3672 StorageUtl_ReadDWord(buffer
, offset
, &(value
->Data1
));
3673 StorageUtl_ReadWord(buffer
, offset
+4, &(value
->Data2
));
3674 StorageUtl_ReadWord(buffer
, offset
+6, &(value
->Data3
));
3676 memcpy(value
->Data4
, (BYTE
*)buffer
+offset
+8, sizeof(value
->Data4
));
3679 void StorageUtl_WriteGUID(void* buffer
, ULONG offset
, GUID
* value
)
3681 StorageUtl_WriteDWord(buffer
, offset
, value
->Data1
);
3682 StorageUtl_WriteWord(buffer
, offset
+4, value
->Data2
);
3683 StorageUtl_WriteWord(buffer
, offset
+6, value
->Data3
);
3685 memcpy((BYTE
*)buffer
+offset
+8, value
->Data4
, sizeof(value
->Data4
));
3688 void StorageUtl_CopyPropertyToSTATSTG(
3689 STATSTG
* destination
,
3690 StgProperty
* source
,
3694 * The copy of the string occurs only when the flag is not set
3696 if ((statFlags
& STATFLAG_NONAME
) != 0)
3698 destination
->pwcsName
= 0;
3702 destination
->pwcsName
=
3703 CoTaskMemAlloc((lstrlenW(source
->name
)+1)*sizeof(WCHAR
));
3705 lstrcpyW((LPWSTR
)destination
->pwcsName
, source
->name
);
3708 switch (source
->propertyType
)
3710 case PROPTYPE_STORAGE
:
3712 destination
->type
= STGTY_STORAGE
;
3714 case PROPTYPE_STREAM
:
3715 destination
->type
= STGTY_STREAM
;
3718 destination
->type
= STGTY_STREAM
;
3722 destination
->cbSize
= source
->size
;
3724 currentReturnStruct->mtime = {0}; TODO
3725 currentReturnStruct->ctime = {0};
3726 currentReturnStruct->atime = {0};
3728 destination
->grfMode
= 0;
3729 destination
->grfLocksSupported
= 0;
3730 destination
->clsid
= source
->propertyUniqueID
;
3731 destination
->grfStateBits
= 0;
3732 destination
->reserved
= 0;
3735 /******************************************************************************
3736 ** BlockChainStream implementation
3739 BlockChainStream
* BlockChainStream_Construct(
3740 StorageImpl
* parentStorage
,
3741 ULONG
* headOfStreamPlaceHolder
,
3742 ULONG propertyIndex
)
3744 BlockChainStream
* newStream
;
3747 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(BlockChainStream
));
3749 newStream
->parentStorage
= parentStorage
;
3750 newStream
->headOfStreamPlaceHolder
= headOfStreamPlaceHolder
;
3751 newStream
->ownerPropertyIndex
= propertyIndex
;
3752 newStream
->lastBlockNoInSequence
= 0xFFFFFFFF;
3753 newStream
->tailIndex
= BLOCK_END_OF_CHAIN
;
3754 newStream
->numBlocks
= 0;
3756 blockIndex
= BlockChainStream_GetHeadOfChain(newStream
);
3758 while (blockIndex
!= BLOCK_END_OF_CHAIN
)
3760 newStream
->numBlocks
++;
3761 newStream
->tailIndex
= blockIndex
;
3763 blockIndex
= StorageImpl_GetNextBlockInChain(
3771 void BlockChainStream_Destroy(BlockChainStream
* This
)
3773 HeapFree(GetProcessHeap(), 0, This
);
3776 /******************************************************************************
3777 * BlockChainStream_GetHeadOfChain
3779 * Returns the head of this stream chain.
3780 * Some special chains don't have properties, their heads are kept in
3781 * This->headOfStreamPlaceHolder.
3784 ULONG
BlockChainStream_GetHeadOfChain(BlockChainStream
* This
)
3786 StgProperty chainProperty
;
3789 if (This
->headOfStreamPlaceHolder
!= 0)
3790 return *(This
->headOfStreamPlaceHolder
);
3792 if (This
->ownerPropertyIndex
!= PROPERTY_NULL
)
3794 readSucessful
= StorageImpl_ReadProperty(
3795 This
->parentStorage
,
3796 This
->ownerPropertyIndex
,
3801 return chainProperty
.startingBlock
;
3805 return BLOCK_END_OF_CHAIN
;
3808 /******************************************************************************
3809 * BlockChainStream_GetCount
3811 * Returns the number of blocks that comprises this chain.
3812 * This is not the size of the stream as the last block may not be full!
3815 ULONG
BlockChainStream_GetCount(BlockChainStream
* This
)
3820 blockIndex
= BlockChainStream_GetHeadOfChain(This
);
3822 while (blockIndex
!= BLOCK_END_OF_CHAIN
)
3826 blockIndex
= StorageImpl_GetNextBlockInChain(
3827 This
->parentStorage
,
3834 /******************************************************************************
3835 * BlockChainStream_ReadAt
3837 * Reads a specified number of bytes from this chain at the specified offset.
3838 * bytesRead may be NULL.
3839 * Failure will be returned if the specified number of bytes has not been read.
3841 BOOL
BlockChainStream_ReadAt(BlockChainStream
* This
,
3842 ULARGE_INTEGER offset
,
3847 ULONG blockNoInSequence
= offset
.LowPart
/ This
->parentStorage
->bigBlockSize
;
3848 ULONG offsetInBlock
= offset
.LowPart
% This
->parentStorage
->bigBlockSize
;
3849 ULONG bytesToReadInBuffer
;
3852 BYTE
* bigBlockBuffer
;
3854 if (This
->lastBlockNoInSequence
== 0xFFFFFFFF)
3855 This
->lastBlockNoInSequence
= blockNoInSequence
;
3857 * Find the first block in the stream that contains part of the buffer.
3859 if (blockNoInSequence
> This
->lastBlockNoInSequence
)
3861 ULONG temp
= blockNoInSequence
;
3863 blockIndex
= This
->lastBlockNoInSequenceIndex
;
3864 blockNoInSequence
-= This
->lastBlockNoInSequence
;
3865 This
->lastBlockNoInSequence
= temp
;
3869 blockIndex
= BlockChainStream_GetHeadOfChain(This
);
3870 This
->lastBlockNoInSequence
= blockNoInSequence
;
3873 while ( (blockNoInSequence
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
))
3876 StorageImpl_GetNextBlockInChain(This
->parentStorage
, blockIndex
);
3878 blockNoInSequence
--;
3881 This
->lastBlockNoInSequenceIndex
= blockIndex
;
3884 * Start reading the buffer.
3887 bufferWalker
= buffer
;
3889 while ( (size
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
) )
3892 * Calculate how many bytes we can copy from this big block.
3894 bytesToReadInBuffer
=
3895 MIN(This
->parentStorage
->bigBlockSize
- offsetInBlock
, size
);
3898 * Copy those bytes to the buffer
3901 StorageImpl_GetROBigBlock(This
->parentStorage
, blockIndex
);
3903 memcpy(bufferWalker
, bigBlockBuffer
+ offsetInBlock
, bytesToReadInBuffer
);
3905 StorageImpl_ReleaseBigBlock(This
->parentStorage
, bigBlockBuffer
);
3908 * Step to the next big block.
3911 StorageImpl_GetNextBlockInChain(This
->parentStorage
, blockIndex
);
3913 bufferWalker
+= bytesToReadInBuffer
;
3914 size
-= bytesToReadInBuffer
;
3915 *bytesRead
+= bytesToReadInBuffer
;
3916 offsetInBlock
= 0; /* There is no offset on the next block */
3923 /******************************************************************************
3924 * BlockChainStream_WriteAt
3926 * Writes the specified number of bytes to this chain at the specified offset.
3927 * bytesWritten may be NULL.
3928 * Will fail if not all specified number of bytes have been written.
3930 BOOL
BlockChainStream_WriteAt(BlockChainStream
* This
,
3931 ULARGE_INTEGER offset
,
3934 ULONG
* bytesWritten
)
3936 ULONG blockNoInSequence
= offset
.LowPart
/ This
->parentStorage
->bigBlockSize
;
3937 ULONG offsetInBlock
= offset
.LowPart
% This
->parentStorage
->bigBlockSize
;
3941 BYTE
* bigBlockBuffer
;
3943 if (This
->lastBlockNoInSequence
== 0xFFFFFFFF)
3944 This
->lastBlockNoInSequence
= blockNoInSequence
;
3947 * Find the first block in the stream that contains part of the buffer.
3949 if (blockNoInSequence
> This
->lastBlockNoInSequence
)
3951 ULONG temp
= blockNoInSequence
;
3953 blockIndex
= This
->lastBlockNoInSequenceIndex
;
3954 blockNoInSequence
-= This
->lastBlockNoInSequence
;
3955 This
->lastBlockNoInSequence
= temp
;
3959 blockIndex
= BlockChainStream_GetHeadOfChain(This
);
3960 This
->lastBlockNoInSequence
= blockNoInSequence
;
3963 while ( (blockNoInSequence
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
))
3966 StorageImpl_GetNextBlockInChain(This
->parentStorage
, blockIndex
);
3968 blockNoInSequence
--;
3971 This
->lastBlockNoInSequenceIndex
= blockIndex
;
3974 * Here, I'm casting away the constness on the buffer variable
3975 * This is OK since we don't intend to modify that buffer.
3978 bufferWalker
= (BYTE
*)buffer
;
3980 while ( (size
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
) )
3983 * Calculate how many bytes we can copy from this big block.
3986 MIN(This
->parentStorage
->bigBlockSize
- offsetInBlock
, size
);
3989 * Copy those bytes to the buffer
3991 bigBlockBuffer
= StorageImpl_GetBigBlock(This
->parentStorage
, blockIndex
);
3993 memcpy(bigBlockBuffer
+ offsetInBlock
, bufferWalker
, bytesToWrite
);
3995 StorageImpl_ReleaseBigBlock(This
->parentStorage
, bigBlockBuffer
);
3998 * Step to the next big block.
4001 StorageImpl_GetNextBlockInChain(This
->parentStorage
, blockIndex
);
4003 bufferWalker
+= bytesToWrite
;
4004 size
-= bytesToWrite
;
4005 *bytesWritten
+= bytesToWrite
;
4006 offsetInBlock
= 0; /* There is no offset on the next block */
4012 /******************************************************************************
4013 * BlockChainStream_Shrink
4015 * Shrinks this chain in the big block depot.
4017 BOOL
BlockChainStream_Shrink(BlockChainStream
* This
,
4018 ULARGE_INTEGER newSize
)
4020 ULONG blockIndex
, extraBlock
;
4025 * Figure out how many blocks are needed to contain the new size
4027 numBlocks
= newSize
.LowPart
/ This
->parentStorage
->bigBlockSize
;
4029 if ((newSize
.LowPart
% This
->parentStorage
->bigBlockSize
) != 0)
4032 blockIndex
= BlockChainStream_GetHeadOfChain(This
);
4035 * Go to the new end of chain
4037 while (count
< numBlocks
)
4040 StorageImpl_GetNextBlockInChain(This
->parentStorage
, blockIndex
);
4045 /* Get the next block before marking the new end */
4047 StorageImpl_GetNextBlockInChain(This
->parentStorage
, blockIndex
);
4049 /* Mark the new end of chain */
4050 StorageImpl_SetNextBlockInChain(
4051 This
->parentStorage
,
4053 BLOCK_END_OF_CHAIN
);
4055 This
->tailIndex
= blockIndex
;
4056 This
->numBlocks
= numBlocks
;
4059 * Mark the extra blocks as free
4061 while (extraBlock
!= BLOCK_END_OF_CHAIN
)
4064 StorageImpl_GetNextBlockInChain(This
->parentStorage
, extraBlock
);
4066 StorageImpl_FreeBigBlock(This
->parentStorage
, extraBlock
);
4067 extraBlock
= blockIndex
;
4073 /******************************************************************************
4074 * BlockChainStream_Enlarge
4076 * Grows this chain in the big block depot.
4078 BOOL
BlockChainStream_Enlarge(BlockChainStream
* This
,
4079 ULARGE_INTEGER newSize
)
4081 ULONG blockIndex
, currentBlock
;
4083 ULONG oldNumBlocks
= 0;
4085 blockIndex
= BlockChainStream_GetHeadOfChain(This
);
4088 * Empty chain. Create the head.
4090 if (blockIndex
== BLOCK_END_OF_CHAIN
)
4092 blockIndex
= StorageImpl_GetNextFreeBigBlock(This
->parentStorage
);
4093 StorageImpl_SetNextBlockInChain(This
->parentStorage
,
4095 BLOCK_END_OF_CHAIN
);
4097 if (This
->headOfStreamPlaceHolder
!= 0)
4099 *(This
->headOfStreamPlaceHolder
) = blockIndex
;
4103 StgProperty chainProp
;
4104 assert(This
->ownerPropertyIndex
!= PROPERTY_NULL
);
4106 StorageImpl_ReadProperty(
4107 This
->parentStorage
,
4108 This
->ownerPropertyIndex
,
4111 chainProp
.startingBlock
= blockIndex
;
4113 StorageImpl_WriteProperty(
4114 This
->parentStorage
,
4115 This
->ownerPropertyIndex
,
4119 This
->tailIndex
= blockIndex
;
4120 This
->numBlocks
= 1;
4124 * Figure out how many blocks are needed to contain this stream
4126 newNumBlocks
= newSize
.LowPart
/ This
->parentStorage
->bigBlockSize
;
4128 if ((newSize
.LowPart
% This
->parentStorage
->bigBlockSize
) != 0)
4132 * Go to the current end of chain
4134 if (This
->tailIndex
== BLOCK_END_OF_CHAIN
)
4136 currentBlock
= blockIndex
;
4138 while (blockIndex
!= BLOCK_END_OF_CHAIN
)
4141 currentBlock
= blockIndex
;
4144 StorageImpl_GetNextBlockInChain(This
->parentStorage
, currentBlock
);
4147 This
->tailIndex
= currentBlock
;
4150 currentBlock
= This
->tailIndex
;
4151 oldNumBlocks
= This
->numBlocks
;
4154 * Add new blocks to the chain
4156 while (oldNumBlocks
< newNumBlocks
)
4158 blockIndex
= StorageImpl_GetNextFreeBigBlock(This
->parentStorage
);
4160 StorageImpl_SetNextBlockInChain(
4161 This
->parentStorage
,
4165 StorageImpl_SetNextBlockInChain(
4166 This
->parentStorage
,
4168 BLOCK_END_OF_CHAIN
);
4170 currentBlock
= blockIndex
;
4174 This
->tailIndex
= blockIndex
;
4175 This
->numBlocks
= newNumBlocks
;
4180 /******************************************************************************
4181 * BlockChainStream_SetSize
4183 * Sets the size of this stream. The big block depot will be updated.
4184 * The file will grow if we grow the chain.
4186 * TODO: Free the actual blocks in the file when we shrink the chain.
4187 * Currently, the blocks are still in the file. So the file size
4188 * doesn't shrink even if we shrink streams.
4190 BOOL
BlockChainStream_SetSize(
4191 BlockChainStream
* This
,
4192 ULARGE_INTEGER newSize
)
4194 ULARGE_INTEGER size
= BlockChainStream_GetSize(This
);
4196 if (newSize
.LowPart
== size
.LowPart
)
4199 if (newSize
.LowPart
< size
.LowPart
)
4201 BlockChainStream_Shrink(This
, newSize
);
4205 ULARGE_INTEGER fileSize
=
4206 BIGBLOCKFILE_GetSize(This
->parentStorage
->bigBlockFile
);
4208 ULONG diff
= newSize
.LowPart
- size
.LowPart
;
4211 * Make sure the file stays a multiple of blocksize
4213 if ((diff
% This
->parentStorage
->bigBlockSize
) != 0)
4214 diff
+= (This
->parentStorage
->bigBlockSize
-
4215 (diff
% This
->parentStorage
->bigBlockSize
) );
4217 fileSize
.LowPart
+= diff
;
4218 BIGBLOCKFILE_SetSize(This
->parentStorage
->bigBlockFile
, fileSize
);
4220 BlockChainStream_Enlarge(This
, newSize
);
4226 /******************************************************************************
4227 * BlockChainStream_GetSize
4229 * Returns the size of this chain.
4230 * Will return the block count if this chain doesn't have a property.
4232 ULARGE_INTEGER
BlockChainStream_GetSize(BlockChainStream
* This
)
4234 StgProperty chainProperty
;
4236 if(This
->headOfStreamPlaceHolder
== NULL
)
4239 * This chain is a data stream read the property and return
4240 * the appropriate size
4242 StorageImpl_ReadProperty(
4243 This
->parentStorage
,
4244 This
->ownerPropertyIndex
,
4247 return chainProperty
.size
;
4252 * this chain is a chain that does not have a property, figure out the
4253 * size by making the product number of used blocks times the
4256 ULARGE_INTEGER result
;
4257 result
.HighPart
= 0;
4260 BlockChainStream_GetCount(This
) *
4261 This
->parentStorage
->bigBlockSize
;
4267 /******************************************************************************
4268 ** SmallBlockChainStream implementation
4271 SmallBlockChainStream
* SmallBlockChainStream_Construct(
4272 StorageImpl
* parentStorage
,
4273 ULONG propertyIndex
)
4275 SmallBlockChainStream
* newStream
;
4277 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(SmallBlockChainStream
));
4279 newStream
->parentStorage
= parentStorage
;
4280 newStream
->ownerPropertyIndex
= propertyIndex
;
4285 void SmallBlockChainStream_Destroy(
4286 SmallBlockChainStream
* This
)
4288 HeapFree(GetProcessHeap(), 0, This
);
4291 /******************************************************************************
4292 * SmallBlockChainStream_GetHeadOfChain
4294 * Returns the head of this chain of small blocks.
4296 ULONG
SmallBlockChainStream_GetHeadOfChain(
4297 SmallBlockChainStream
* This
)
4299 StgProperty chainProperty
;
4302 if (This
->ownerPropertyIndex
)
4304 readSucessful
= StorageImpl_ReadProperty(
4305 This
->parentStorage
,
4306 This
->ownerPropertyIndex
,
4311 return chainProperty
.startingBlock
;
4316 return BLOCK_END_OF_CHAIN
;
4319 /******************************************************************************
4320 * SmallBlockChainStream_GetNextBlockInChain
4322 * Returns the index of the next small block in this chain.
4325 * - BLOCK_END_OF_CHAIN: end of this chain
4326 * - BLOCK_UNUSED: small block 'blockIndex' is free
4328 ULONG
SmallBlockChainStream_GetNextBlockInChain(
4329 SmallBlockChainStream
* This
,
4332 ULARGE_INTEGER offsetOfBlockInDepot
;
4334 ULONG nextBlockInChain
= BLOCK_END_OF_CHAIN
;
4338 offsetOfBlockInDepot
.HighPart
= 0;
4339 offsetOfBlockInDepot
.LowPart
= blockIndex
* sizeof(ULONG
);
4342 * Read those bytes in the buffer from the small block file.
4344 success
= BlockChainStream_ReadAt(
4345 This
->parentStorage
->smallBlockDepotChain
,
4346 offsetOfBlockInDepot
,
4353 StorageUtl_ReadDWord(&buffer
, 0, &nextBlockInChain
);
4356 return nextBlockInChain
;
4359 /******************************************************************************
4360 * SmallBlockChainStream_SetNextBlockInChain
4362 * Writes the index of the next block of the specified block in the small
4364 * To set the end of chain use BLOCK_END_OF_CHAIN as nextBlock.
4365 * To flag a block as free use BLOCK_UNUSED as nextBlock.
4367 void SmallBlockChainStream_SetNextBlockInChain(
4368 SmallBlockChainStream
* This
,
4372 ULARGE_INTEGER offsetOfBlockInDepot
;
4376 offsetOfBlockInDepot
.HighPart
= 0;
4377 offsetOfBlockInDepot
.LowPart
= blockIndex
* sizeof(ULONG
);
4379 StorageUtl_WriteDWord(&buffer
, 0, nextBlock
);
4382 * Read those bytes in the buffer from the small block file.
4384 BlockChainStream_WriteAt(
4385 This
->parentStorage
->smallBlockDepotChain
,
4386 offsetOfBlockInDepot
,
4392 /******************************************************************************
4393 * SmallBlockChainStream_FreeBlock
4395 * Flag small block 'blockIndex' as free in the small block depot.
4397 void SmallBlockChainStream_FreeBlock(
4398 SmallBlockChainStream
* This
,
4401 SmallBlockChainStream_SetNextBlockInChain(This
, blockIndex
, BLOCK_UNUSED
);
4404 /******************************************************************************
4405 * SmallBlockChainStream_GetNextFreeBlock
4407 * Returns the index of a free small block. The small block depot will be
4408 * enlarged if necessary. The small block chain will also be enlarged if
4411 ULONG
SmallBlockChainStream_GetNextFreeBlock(
4412 SmallBlockChainStream
* This
)
4414 ULARGE_INTEGER offsetOfBlockInDepot
;
4417 ULONG blockIndex
= 0;
4418 ULONG nextBlockIndex
= BLOCK_END_OF_CHAIN
;
4419 BOOL success
= TRUE
;
4420 ULONG smallBlocksPerBigBlock
;
4422 offsetOfBlockInDepot
.HighPart
= 0;
4425 * Scan the small block depot for a free block
4427 while (nextBlockIndex
!= BLOCK_UNUSED
)
4429 offsetOfBlockInDepot
.LowPart
= blockIndex
* sizeof(ULONG
);
4431 success
= BlockChainStream_ReadAt(
4432 This
->parentStorage
->smallBlockDepotChain
,
4433 offsetOfBlockInDepot
,
4439 * If we run out of space for the small block depot, enlarge it
4443 StorageUtl_ReadDWord(&buffer
, 0, &nextBlockIndex
);
4445 if (nextBlockIndex
!= BLOCK_UNUSED
)
4451 BlockChainStream_GetCount(This
->parentStorage
->smallBlockDepotChain
);
4453 ULONG sbdIndex
= This
->parentStorage
->smallBlockDepotStart
;
4454 ULONG nextBlock
, newsbdIndex
;
4455 BYTE
* smallBlockDepot
;
4457 nextBlock
= sbdIndex
;
4458 while (nextBlock
!= BLOCK_END_OF_CHAIN
)
4460 sbdIndex
= nextBlock
;
4462 StorageImpl_GetNextBlockInChain(This
->parentStorage
, sbdIndex
);
4465 newsbdIndex
= StorageImpl_GetNextFreeBigBlock(This
->parentStorage
);
4466 if (sbdIndex
!= BLOCK_END_OF_CHAIN
)
4467 StorageImpl_SetNextBlockInChain(
4468 This
->parentStorage
,
4472 StorageImpl_SetNextBlockInChain(
4473 This
->parentStorage
,
4475 BLOCK_END_OF_CHAIN
);
4478 * Initialize all the small blocks to free
4481 StorageImpl_GetBigBlock(This
->parentStorage
, newsbdIndex
);
4483 memset(smallBlockDepot
, BLOCK_UNUSED
, This
->parentStorage
->bigBlockSize
);
4484 StorageImpl_ReleaseBigBlock(This
->parentStorage
, smallBlockDepot
);
4489 * We have just created the small block depot.
4491 StgProperty rootProp
;
4495 * Save it in the header
4497 This
->parentStorage
->smallBlockDepotStart
= newsbdIndex
;
4498 StorageImpl_SaveFileHeader(This
->parentStorage
);
4501 * And allocate the first big block that will contain small blocks
4504 StorageImpl_GetNextFreeBigBlock(This
->parentStorage
);
4506 StorageImpl_SetNextBlockInChain(
4507 This
->parentStorage
,
4509 BLOCK_END_OF_CHAIN
);
4511 StorageImpl_ReadProperty(
4512 This
->parentStorage
,
4513 This
->parentStorage
->rootPropertySetIndex
,
4516 rootProp
.startingBlock
= sbStartIndex
;
4517 rootProp
.size
.HighPart
= 0;
4518 rootProp
.size
.LowPart
= This
->parentStorage
->bigBlockSize
;
4520 StorageImpl_WriteProperty(
4521 This
->parentStorage
,
4522 This
->parentStorage
->rootPropertySetIndex
,
4528 smallBlocksPerBigBlock
=
4529 This
->parentStorage
->bigBlockSize
/ This
->parentStorage
->smallBlockSize
;
4532 * Verify if we have to allocate big blocks to contain small blocks
4534 if (blockIndex
% smallBlocksPerBigBlock
== 0)
4536 StgProperty rootProp
;
4537 ULONG blocksRequired
= (blockIndex
/ smallBlocksPerBigBlock
) + 1;
4539 StorageImpl_ReadProperty(
4540 This
->parentStorage
,
4541 This
->parentStorage
->rootPropertySetIndex
,
4544 if (rootProp
.size
.LowPart
<
4545 (blocksRequired
* This
->parentStorage
->bigBlockSize
))
4547 rootProp
.size
.LowPart
+= This
->parentStorage
->bigBlockSize
;
4549 BlockChainStream_SetSize(
4550 This
->parentStorage
->smallBlockRootChain
,
4553 StorageImpl_WriteProperty(
4554 This
->parentStorage
,
4555 This
->parentStorage
->rootPropertySetIndex
,
4563 /******************************************************************************
4564 * SmallBlockChainStream_ReadAt
4566 * Reads a specified number of bytes from this chain at the specified offset.
4567 * bytesRead may be NULL.
4568 * Failure will be returned if the specified number of bytes has not been read.
4570 BOOL
SmallBlockChainStream_ReadAt(
4571 SmallBlockChainStream
* This
,
4572 ULARGE_INTEGER offset
,
4577 ULARGE_INTEGER offsetInBigBlockFile
;
4578 ULONG blockNoInSequence
=
4579 offset
.LowPart
/ This
->parentStorage
->smallBlockSize
;
4581 ULONG offsetInBlock
= offset
.LowPart
% This
->parentStorage
->smallBlockSize
;
4582 ULONG bytesToReadInBuffer
;
4584 ULONG bytesReadFromBigBlockFile
;
4588 * This should never happen on a small block file.
4590 assert(offset
.HighPart
==0);
4593 * Find the first block in the stream that contains part of the buffer.
4595 blockIndex
= SmallBlockChainStream_GetHeadOfChain(This
);
4597 while ( (blockNoInSequence
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
))
4599 blockIndex
= SmallBlockChainStream_GetNextBlockInChain(This
, blockIndex
);
4601 blockNoInSequence
--;
4605 * Start reading the buffer.
4608 bufferWalker
= buffer
;
4610 while ( (size
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
) )
4613 * Calculate how many bytes we can copy from this small block.
4615 bytesToReadInBuffer
=
4616 MIN(This
->parentStorage
->smallBlockSize
- offsetInBlock
, size
);
4619 * Calculate the offset of the small block in the small block file.
4621 offsetInBigBlockFile
.HighPart
= 0;
4622 offsetInBigBlockFile
.LowPart
=
4623 blockIndex
* This
->parentStorage
->smallBlockSize
;
4625 offsetInBigBlockFile
.LowPart
+= offsetInBlock
;
4628 * Read those bytes in the buffer from the small block file.
4630 BlockChainStream_ReadAt(This
->parentStorage
->smallBlockRootChain
,
4631 offsetInBigBlockFile
,
4632 bytesToReadInBuffer
,
4634 &bytesReadFromBigBlockFile
);
4636 assert(bytesReadFromBigBlockFile
== bytesToReadInBuffer
);
4639 * Step to the next big block.
4641 blockIndex
= SmallBlockChainStream_GetNextBlockInChain(This
, blockIndex
);
4642 bufferWalker
+= bytesToReadInBuffer
;
4643 size
-= bytesToReadInBuffer
;
4644 *bytesRead
+= bytesToReadInBuffer
;
4645 offsetInBlock
= 0; /* There is no offset on the next block */
4651 /******************************************************************************
4652 * SmallBlockChainStream_WriteAt
4654 * Writes the specified number of bytes to this chain at the specified offset.
4655 * bytesWritten may be NULL.
4656 * Will fail if not all specified number of bytes have been written.
4658 BOOL
SmallBlockChainStream_WriteAt(
4659 SmallBlockChainStream
* This
,
4660 ULARGE_INTEGER offset
,
4663 ULONG
* bytesWritten
)
4665 ULARGE_INTEGER offsetInBigBlockFile
;
4666 ULONG blockNoInSequence
=
4667 offset
.LowPart
/ This
->parentStorage
->smallBlockSize
;
4669 ULONG offsetInBlock
= offset
.LowPart
% This
->parentStorage
->smallBlockSize
;
4670 ULONG bytesToWriteInBuffer
;
4672 ULONG bytesWrittenFromBigBlockFile
;
4676 * This should never happen on a small block file.
4678 assert(offset
.HighPart
==0);
4681 * Find the first block in the stream that contains part of the buffer.
4683 blockIndex
= SmallBlockChainStream_GetHeadOfChain(This
);
4685 while ( (blockNoInSequence
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
))
4687 blockIndex
= SmallBlockChainStream_GetNextBlockInChain(This
, blockIndex
);
4689 blockNoInSequence
--;
4693 * Start writing the buffer.
4695 * Here, I'm casting away the constness on the buffer variable
4696 * This is OK since we don't intend to modify that buffer.
4699 bufferWalker
= (BYTE
*)buffer
;
4700 while ( (size
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
) )
4703 * Calculate how many bytes we can copy to this small block.
4705 bytesToWriteInBuffer
=
4706 MIN(This
->parentStorage
->smallBlockSize
- offsetInBlock
, size
);
4709 * Calculate the offset of the small block in the small block file.
4711 offsetInBigBlockFile
.HighPart
= 0;
4712 offsetInBigBlockFile
.LowPart
=
4713 blockIndex
* This
->parentStorage
->smallBlockSize
;
4715 offsetInBigBlockFile
.LowPart
+= offsetInBlock
;
4718 * Write those bytes in the buffer to the small block file.
4720 BlockChainStream_WriteAt(This
->parentStorage
->smallBlockRootChain
,
4721 offsetInBigBlockFile
,
4722 bytesToWriteInBuffer
,
4724 &bytesWrittenFromBigBlockFile
);
4726 assert(bytesWrittenFromBigBlockFile
== bytesToWriteInBuffer
);
4729 * Step to the next big block.
4731 blockIndex
= SmallBlockChainStream_GetNextBlockInChain(This
, blockIndex
);
4732 bufferWalker
+= bytesToWriteInBuffer
;
4733 size
-= bytesToWriteInBuffer
;
4734 *bytesWritten
+= bytesToWriteInBuffer
;
4735 offsetInBlock
= 0; /* There is no offset on the next block */
4741 /******************************************************************************
4742 * SmallBlockChainStream_Shrink
4744 * Shrinks this chain in the small block depot.
4746 BOOL
SmallBlockChainStream_Shrink(
4747 SmallBlockChainStream
* This
,
4748 ULARGE_INTEGER newSize
)
4750 ULONG blockIndex
, extraBlock
;
4754 numBlocks
= newSize
.LowPart
/ This
->parentStorage
->smallBlockSize
;
4756 if ((newSize
.LowPart
% This
->parentStorage
->smallBlockSize
) != 0)
4759 blockIndex
= SmallBlockChainStream_GetHeadOfChain(This
);
4762 * Go to the new end of chain
4764 while (count
< numBlocks
)
4766 blockIndex
= SmallBlockChainStream_GetNextBlockInChain(This
, blockIndex
);
4770 /* Get the next block before marking the new end */
4771 extraBlock
= SmallBlockChainStream_GetNextBlockInChain(This
, blockIndex
);
4773 /* Mark the new end of chain */
4774 SmallBlockChainStream_SetNextBlockInChain(
4777 BLOCK_END_OF_CHAIN
);
4780 * Mark the extra blocks as free
4782 while (extraBlock
!= BLOCK_END_OF_CHAIN
)
4784 blockIndex
= SmallBlockChainStream_GetNextBlockInChain(This
, extraBlock
);
4785 SmallBlockChainStream_FreeBlock(This
, extraBlock
);
4786 extraBlock
= blockIndex
;
4792 /******************************************************************************
4793 * SmallBlockChainStream_Enlarge
4795 * Grows this chain in the small block depot.
4797 BOOL
SmallBlockChainStream_Enlarge(
4798 SmallBlockChainStream
* This
,
4799 ULARGE_INTEGER newSize
)
4801 ULONG blockIndex
, currentBlock
;
4803 ULONG oldNumBlocks
= 0;
4805 blockIndex
= SmallBlockChainStream_GetHeadOfChain(This
);
4810 if (blockIndex
== BLOCK_END_OF_CHAIN
)
4812 StgProperty chainProp
;
4814 StorageImpl_ReadProperty(This
->parentStorage
, This
->ownerPropertyIndex
,
4817 chainProp
.startingBlock
= SmallBlockChainStream_GetNextFreeBlock(This
);
4819 StorageImpl_WriteProperty(This
->parentStorage
, This
->ownerPropertyIndex
,
4822 blockIndex
= chainProp
.startingBlock
;
4823 SmallBlockChainStream_SetNextBlockInChain(
4826 BLOCK_END_OF_CHAIN
);
4829 currentBlock
= blockIndex
;
4832 * Figure out how many blocks are needed to contain this stream
4834 newNumBlocks
= newSize
.LowPart
/ This
->parentStorage
->smallBlockSize
;
4836 if ((newSize
.LowPart
% This
->parentStorage
->smallBlockSize
) != 0)
4840 * Go to the current end of chain
4842 while (blockIndex
!= BLOCK_END_OF_CHAIN
)
4845 currentBlock
= blockIndex
;
4846 blockIndex
= SmallBlockChainStream_GetNextBlockInChain(This
, currentBlock
);
4850 * Add new blocks to the chain
4852 while (oldNumBlocks
< newNumBlocks
)
4854 blockIndex
= SmallBlockChainStream_GetNextFreeBlock(This
);
4855 SmallBlockChainStream_SetNextBlockInChain(This
, currentBlock
, blockIndex
);
4857 SmallBlockChainStream_SetNextBlockInChain(
4860 BLOCK_END_OF_CHAIN
);
4862 currentBlock
= blockIndex
;
4869 /******************************************************************************
4870 * SmallBlockChainStream_GetCount
4872 * Returns the number of blocks that comprises this chain.
4873 * This is not the size of this chain as the last block may not be full!
4875 ULONG
SmallBlockChainStream_GetCount(SmallBlockChainStream
* This
)
4880 blockIndex
= SmallBlockChainStream_GetHeadOfChain(This
);
4882 while (blockIndex
!= BLOCK_END_OF_CHAIN
)
4886 blockIndex
= SmallBlockChainStream_GetNextBlockInChain(This
, blockIndex
);
4892 /******************************************************************************
4893 * SmallBlockChainStream_SetSize
4895 * Sets the size of this stream.
4896 * The file will grow if we grow the chain.
4898 * TODO: Free the actual blocks in the file when we shrink the chain.
4899 * Currently, the blocks are still in the file. So the file size
4900 * doesn't shrink even if we shrink streams.
4902 BOOL
SmallBlockChainStream_SetSize(
4903 SmallBlockChainStream
* This
,
4904 ULARGE_INTEGER newSize
)
4906 ULARGE_INTEGER size
= SmallBlockChainStream_GetSize(This
);
4908 if (newSize
.LowPart
== size
.LowPart
)
4911 if (newSize
.LowPart
< size
.LowPart
)
4913 SmallBlockChainStream_Shrink(This
, newSize
);
4917 SmallBlockChainStream_Enlarge(This
, newSize
);
4923 /******************************************************************************
4924 * SmallBlockChainStream_GetSize
4926 * Returns the size of this chain.
4928 ULARGE_INTEGER
SmallBlockChainStream_GetSize(SmallBlockChainStream
* This
)
4930 StgProperty chainProperty
;
4932 StorageImpl_ReadProperty(
4933 This
->parentStorage
,
4934 This
->ownerPropertyIndex
,
4937 return chainProperty
.size
;
4940 /******************************************************************************
4941 * StgCreateDocfile32 [OLE32.144]
4942 * TODO Validate grfMode (STGM)
4944 HRESULT WINAPI
StgCreateDocfile(
4948 IStorage
**ppstgOpen
)
4950 StorageImpl
* newStorage
= 0;
4951 HANDLE hFile
= INVALID_HANDLE_VALUE
;
4956 DWORD fileAttributes
;
4957 WCHAR tempFileName
[MAX_PATH
];
4960 * Validate the parameters
4963 return STG_E_INVALIDPOINTER
;
4966 * Validate the STGM flags
4968 if ( FAILED( validateSTGM(grfMode
) ))
4969 return STG_E_INVALIDFLAG
;
4972 * Generate a unique name.
4976 WCHAR tempPath
[MAX_PATH
];
4977 WCHAR prefix
[] = { 'S', 'T', 'O', 0 };
4979 memset(tempPath
, 0, sizeof(tempPath
));
4980 memset(tempFileName
, 0, sizeof(tempFileName
));
4982 if ((GetTempPathW(MAX_PATH
, tempPath
)) == 0 )
4985 if (GetTempFileNameW(tempPath
, prefix
, 0, tempFileName
) != 0)
4986 pwcsName
= tempFileName
;
4988 return STG_E_INSUFFICIENTMEMORY
;
4992 * Interpret the STGM value grfMode
4994 shareMode
= GetShareModeFromSTGM(grfMode
);
4995 accessMode
= GetAccessModeFromSTGM(grfMode
);
4996 creationMode
= GetCreationModeFromSTGM(grfMode
);
4998 if (grfMode
& STGM_DELETEONRELEASE
)
4999 fileAttributes
= FILE_FLAG_RANDOM_ACCESS
| FILE_FLAG_DELETE_ON_CLOSE
;
5001 fileAttributes
= FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_RANDOM_ACCESS
;
5003 if (grfMode
& STGM_TRANSACTED
)
5004 FIXME(ole
, "Transacted mode not implemented.\n");
5007 * Initialize the "out" parameter.
5011 hFile
= CreateFileW(pwcsName
,
5019 if (hFile
== INVALID_HANDLE_VALUE
)
5025 * Allocate and initialize the new IStorage32object.
5027 newStorage
= HeapAlloc(GetProcessHeap(), 0, sizeof(StorageImpl
));
5029 if (newStorage
== 0)
5030 return STG_E_INSUFFICIENTMEMORY
;
5032 hr
= StorageImpl_Construct(
5041 * Get an "out" pointer for the caller.
5043 hr
= StorageBaseImpl_QueryInterface(
5044 (IStorage
*)newStorage
,
5045 (REFIID
)&IID_IStorage
,
5051 /******************************************************************************
5052 * StgOpenStorage32 [OLE32.148]
5054 HRESULT WINAPI
StgOpenStorage(
5055 const OLECHAR
*pwcsName
,
5056 IStorage
*pstgPriority
,
5060 IStorage
**ppstgOpen
)
5062 StorageImpl
* newStorage
= 0;
5069 * Perform a sanity check
5071 if (( pwcsName
== 0) || (ppstgOpen
== 0) )
5072 return STG_E_INVALIDPOINTER
;
5075 * Validate the STGM flags
5077 if ( FAILED( validateSTGM(grfMode
) ))
5078 return STG_E_INVALIDFLAG
;
5081 * Interpret the STGM value grfMode
5083 shareMode
= GetShareModeFromSTGM(grfMode
);
5084 accessMode
= GetAccessModeFromSTGM(grfMode
);
5087 * Initialize the "out" parameter.
5091 hFile
= CreateFileW( pwcsName
,
5096 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_RANDOM_ACCESS
,
5100 if (hFile
==INVALID_HANDLE_VALUE
)
5106 * Allocate and initialize the new IStorage32object.
5108 newStorage
= HeapAlloc(GetProcessHeap(), 0, sizeof(StorageImpl
));
5110 if (newStorage
== 0)
5111 return STG_E_INSUFFICIENTMEMORY
;
5113 hr
= StorageImpl_Construct(
5122 * Get an "out" pointer for the caller.
5124 hr
= StorageBaseImpl_QueryInterface(
5125 (IStorage
*)newStorage
,
5126 (REFIID
)&IID_IStorage
,
5132 /******************************************************************************
5133 * WriteClassStg32 [OLE32.148]
5135 * This method will store the specified CLSID in the specified storage object
5137 HRESULT WINAPI
WriteClassStg(IStorage
* pStg
, REFCLSID rclsid
)
5143 hRes
= IStorage_SetClass(pStg
, rclsid
);
5149 /****************************************************************************
5150 * This method validate a STGM parameter that can contain the values below
5152 * STGM_DIRECT 0x00000000
5153 * STGM_TRANSACTED 0x00010000
5154 * STGM_SIMPLE 0x08000000
5156 * STGM_READ 0x00000000
5157 * STGM_WRITE 0x00000001
5158 * STGM_READWRITE 0x00000002
5160 * STGM_SHARE_DENY_NONE 0x00000040
5161 * STGM_SHARE_DENY_READ 0x00000030
5162 * STGM_SHARE_DENY_WRITE 0x00000020
5163 * STGM_SHARE_EXCLUSIVE 0x00000010
5165 * STGM_PRIORITY 0x00040000
5166 * STGM_DELETEONRELEASE 0x04000000
5168 * STGM_CREATE 0x00001000
5169 * STGM_CONVERT 0x00020000
5170 * STGM_FAILIFTHERE 0x00000000
5172 * STGM_NOSCRATCH 0x00100000
5173 * STGM_NOSNAPSHOT 0x00200000
5175 static HRESULT
validateSTGM(DWORD stgm
)
5177 BOOL bSTGM_TRANSACTED
= ((stgm
& STGM_TRANSACTED
) == STGM_TRANSACTED
);
5178 BOOL bSTGM_SIMPLE
= ((stgm
& STGM_SIMPLE
) == STGM_SIMPLE
);
5179 BOOL bSTGM_DIRECT
= ! (bSTGM_TRANSACTED
|| bSTGM_SIMPLE
);
5181 BOOL bSTGM_WRITE
= ((stgm
& STGM_WRITE
) == STGM_WRITE
);
5182 BOOL bSTGM_READWRITE
= ((stgm
& STGM_READWRITE
) == STGM_READWRITE
);
5183 BOOL bSTGM_READ
= ! (bSTGM_WRITE
|| bSTGM_READWRITE
);
5185 BOOL bSTGM_SHARE_DENY_NONE
=
5186 ((stgm
& STGM_SHARE_DENY_NONE
) == STGM_SHARE_DENY_NONE
);
5188 BOOL bSTGM_SHARE_DENY_READ
=
5189 ((stgm
& STGM_SHARE_DENY_READ
) == STGM_SHARE_DENY_READ
);
5191 BOOL bSTGM_SHARE_DENY_WRITE
=
5192 ((stgm
& STGM_SHARE_DENY_WRITE
) == STGM_SHARE_DENY_WRITE
);
5194 BOOL bSTGM_SHARE_EXCLUSIVE
=
5195 ((stgm
& STGM_SHARE_EXCLUSIVE
) == STGM_SHARE_EXCLUSIVE
);
5197 BOOL bSTGM_CREATE
= ((stgm
& STGM_CREATE
) == STGM_CREATE
);
5198 BOOL bSTGM_CONVERT
= ((stgm
& STGM_CONVERT
) == STGM_CONVERT
);
5200 BOOL bSTGM_NOSCRATCH
= ((stgm
& STGM_NOSCRATCH
) == STGM_NOSCRATCH
);
5201 BOOL bSTGM_NOSNAPSHOT
= ((stgm
& STGM_NOSNAPSHOT
) == STGM_NOSNAPSHOT
);
5204 * STGM_DIRECT | STGM_TRANSACTED | STGM_SIMPLE
5206 if ( ! bSTGM_DIRECT
)
5207 if( bSTGM_TRANSACTED
&& bSTGM_SIMPLE
)
5211 * STGM_WRITE | STGM_READWRITE | STGM_READ
5214 if( bSTGM_WRITE
&& bSTGM_READWRITE
)
5218 * STGM_SHARE_DENY_NONE | others
5219 * (I assume here that DENY_READ implies DENY_WRITE)
5221 if ( bSTGM_SHARE_DENY_NONE
)
5222 if ( bSTGM_SHARE_DENY_READ
||
5223 bSTGM_SHARE_DENY_WRITE
||
5224 bSTGM_SHARE_EXCLUSIVE
)
5228 * STGM_CREATE | STGM_CONVERT
5229 * if both are false, STGM_FAILIFTHERE is set to TRUE
5231 if ( bSTGM_CREATE
&& bSTGM_CONVERT
)
5235 * STGM_NOSCRATCH requires STGM_TRANSACTED
5237 if ( bSTGM_NOSCRATCH
&& ! bSTGM_TRANSACTED
)
5241 * STGM_NOSNAPSHOT requires STGM_TRANSACTED and
5242 * not STGM_SHARE_EXCLUSIVE or STGM_SHARE_DENY_WRITE`
5244 if (bSTGM_NOSNAPSHOT
)
5246 if ( ! ( bSTGM_TRANSACTED
&&
5247 !(bSTGM_SHARE_EXCLUSIVE
|| bSTGM_SHARE_DENY_WRITE
)) )
5254 /****************************************************************************
5255 * GetShareModeFromSTGM
5257 * This method will return a share mode flag from a STGM value.
5258 * The STGM value is assumed valid.
5260 static DWORD
GetShareModeFromSTGM(DWORD stgm
)
5262 DWORD dwShareMode
= 0;
5263 BOOL bSTGM_SHARE_DENY_NONE
=
5264 ((stgm
& STGM_SHARE_DENY_NONE
) == STGM_SHARE_DENY_NONE
);
5266 BOOL bSTGM_SHARE_DENY_READ
=
5267 ((stgm
& STGM_SHARE_DENY_READ
) == STGM_SHARE_DENY_READ
);
5269 BOOL bSTGM_SHARE_DENY_WRITE
=
5270 ((stgm
& STGM_SHARE_DENY_WRITE
) == STGM_SHARE_DENY_WRITE
);
5272 BOOL bSTGM_SHARE_EXCLUSIVE
=
5273 ((stgm
& STGM_SHARE_EXCLUSIVE
) == STGM_SHARE_EXCLUSIVE
);
5275 if ((bSTGM_SHARE_EXCLUSIVE
) || (bSTGM_SHARE_DENY_READ
))
5278 if (bSTGM_SHARE_DENY_NONE
)
5279 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
5281 if (bSTGM_SHARE_DENY_WRITE
)
5282 dwShareMode
= FILE_SHARE_READ
;
5287 /****************************************************************************
5288 * GetAccessModeFromSTGM
5290 * This method will return an access mode flag from a STGM value.
5291 * The STGM value is assumed valid.
5293 static DWORD
GetAccessModeFromSTGM(DWORD stgm
)
5295 DWORD dwDesiredAccess
= 0;
5296 BOOL bSTGM_WRITE
= ((stgm
& STGM_WRITE
) == STGM_WRITE
);
5297 BOOL bSTGM_READWRITE
= ((stgm
& STGM_READWRITE
) == STGM_READWRITE
);
5298 BOOL bSTGM_READ
= ! (bSTGM_WRITE
|| bSTGM_READWRITE
);
5301 dwDesiredAccess
= GENERIC_READ
;
5304 dwDesiredAccess
|= GENERIC_WRITE
;
5306 if (bSTGM_READWRITE
)
5307 dwDesiredAccess
= GENERIC_READ
| GENERIC_WRITE
;
5309 return dwDesiredAccess
;
5312 /****************************************************************************
5313 * GetCreationModeFromSTGM
5315 * This method will return a creation mode flag from a STGM value.
5316 * The STGM value is assumed valid.
5318 static DWORD
GetCreationModeFromSTGM(DWORD stgm
)
5320 if ( stgm
& STGM_CREATE
)
5321 return CREATE_ALWAYS
;
5322 if (stgm
& STGM_CONVERT
) {
5323 FIXME(ole
, "STGM_CONVERT not implemented!\n");
5326 /* All other cases */
5327 if (stgm
& ~ (STGM_CREATE
|STGM_CONVERT
))
5328 FIXME(ole
,"unhandled storage mode : 0x%08lx\n",stgm
& ~ (STGM_CREATE
|STGM_CONVERT
));