Fixed Makefile up-to-date check to avoid relinking all the object
[wine/wine-kai.git] / ole / storage32.c
blobece39a70548d75318ac7adf27286ff7e35c94c8e
1 /*
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
13 #include <assert.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
18 #include "winbase.h"
19 #include "winerror.h"
20 #include "wine/obj_storage.h"
21 #include "wine/winestring.h"
22 #include "crtdll.h"
23 #include "tchar.h"
24 #include "debug.h"
25 #include "windef.h"
27 #include "storage32.h"
29 #define FILE_BEGIN 0
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(
46 StorageImpl *storage,
47 ULONG propertyIndexToStore,
48 ULONG storagePropertyIndex,
49 INT typeOfRelation);
51 static HRESULT adjustPropertyChain(
52 StorageImpl *This,
53 StgProperty propertyToDelete,
54 StgProperty parentProperty,
55 ULONG parentPropertyId,
56 INT typeOfRelation);
58 /***********************************************************************
59 * Declaration of the functions used to manipulate StgProperty
62 static ULONG getFreeProperty(
63 StorageImpl *storage);
65 static void updatePropertyChain(
66 StorageImpl *storage,
67 ULONG newPropertyIndex,
68 StgProperty newProperty);
70 static LONG propertyNameCmp(
71 OLECHAR *newProperty,
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,
96 StorageImpl_CopyTo,
97 StorageImpl_MoveElementTo,
98 StorageImpl_Commit,
99 StorageImpl_Revert,
100 StorageBaseImpl_EnumElements,
101 StorageImpl_DestroyElement,
102 StorageBaseImpl_RenameElement,
103 StorageImpl_SetElementTimes,
104 StorageBaseImpl_SetClass,
105 StorageImpl_SetStateBits,
106 StorageBaseImpl_Stat
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,
121 StorageImpl_CopyTo,
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,
131 StorageBaseImpl_Stat
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(
165 IStorage* iface,
166 REFIID riid,
167 void** ppvObject)
169 ICOM_THIS(StorageBaseImpl,iface);
171 * Perform a sanity check on the parameters.
173 if ( (This==0) || (ppvObject==0) )
174 return E_INVALIDARG;
177 * Initialize the return parameter.
179 *ppvObject = 0;
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.
196 if ((*ppvObject)==0)
197 return E_NOINTERFACE;
200 * Query Interface always increases the reference count by one when it is
201 * successful
203 StorageBaseImpl_AddRef(iface);
205 return S_OK;
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(
217 IStorage* iface)
219 ICOM_THIS(StorageBaseImpl,iface);
220 This->ref++;
222 return This->ref;
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(
234 IStorage* iface)
236 ICOM_THIS(StorageBaseImpl,iface);
238 * Decrease the reference count on this object.
240 This->ref--;
243 * If the reference count goes down to 0, perform suicide.
245 if (This->ref==0)
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);
254 return 0;
257 return This->ref;
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(
268 IStorage* iface,
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) )
285 return E_INVALIDARG;
288 * Initialize the out parameter
290 *ppstm = 0;
293 * Validate the STGM flags
295 if ( FAILED( validateSTGM(grfMode) ))
296 return STG_E_INVALIDFLAG;
299 * As documented.
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(
317 propertyEnumeration,
318 pwcsName,
319 &currentProperty);
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);
334 if (newStream!=0)
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);
344 return S_OK;
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(
361 IStorage* iface,
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) )
379 return E_INVALIDARG;
382 * Validate the STGM flags
384 if ( FAILED( validateSTGM(grfMode) ))
385 return STG_E_INVALIDFLAG;
388 * As documented.
390 if ( !(grfMode & STGM_SHARE_EXCLUSIVE) ||
391 (grfMode & STGM_DELETEONRELEASE) ||
392 (grfMode & STGM_PRIORITY) )
393 return STG_E_INVALIDFUNCTION;
396 * Initialize the out parameter
398 *ppstg = 0;
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(
411 propertyEnumeration,
412 pwcsName,
413 &currentProperty);
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,
431 foundPropertyIndex);
433 if (newStorage != 0)
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);
443 return S_OK;
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(
461 IStorage* iface,
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))
474 return E_INVALIDARG;
477 * Construct the enumerator.
479 newEnum = IEnumSTATSTGImpl_Construct(
480 This->ancestorStorage,
481 This->rootPropertySetIndex);
483 if (newEnum!=0)
485 *ppenum = (IEnumSTATSTG*)newEnum;
488 * Don't forget to nail down a reference to the new object before
489 * returning it.
491 IEnumSTATSTGImpl_AddRef(*ppenum);
493 return S_OK;
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(
507 IStorage* iface,
508 STATSTG* pstatstg, /* [out] */
509 DWORD grfStatFlag) /* [in] */
511 ICOM_THIS(StorageBaseImpl,iface);
512 StgProperty curProperty;
513 BOOL readSucessful;
516 * Perform a sanity check on the parameters.
518 if ( (This==0) || (pstatstg==0))
519 return E_INVALIDARG;
522 * Read the information from the property.
524 readSucessful = StorageImpl_ReadProperty(
525 This->ancestorStorage,
526 This->rootPropertySetIndex,
527 &curProperty);
529 if (readSucessful)
531 StorageUtl_CopyPropertyToSTATSTG(
532 pstatstg,
533 &curProperty,
534 grfStatFlag);
536 return S_OK;
539 return E_FAIL;
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(
554 IStorage* iface,
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,
573 pwcsNewName,
574 &currentProperty);
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,
591 pwcsOldName,
592 &currentProperty);
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,
648 &renamedProperty);
651 * Find a spot in the property chain for our newly created property.
653 updatePropertyChain(
654 (StorageImpl*)This,
655 renamedPropertyIndex,
656 renamedProperty);
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
662 * this to happen.
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
667 * non empty storage.
669 currentProperty.dirProperty = PROPERTY_NULL;
670 currentProperty.propertyType = PROPTYPE_STORAGE;
671 StorageImpl_WriteProperty(
672 This->ancestorStorage,
673 foundPropertyIndex,
674 &currentProperty);
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);
683 else
686 * There is no property with the old name
688 return STG_E_FILENOTFOUND;
691 return S_OK;
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(
702 IStorage* iface,
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
718 if (ppstm == 0)
719 return STG_E_INVALIDPOINTER;
721 if (pwcsName == 0)
722 return STG_E_INVALIDNAME;
725 * Validate the STGM flags
727 if ( FAILED( validateSTGM(grfMode) ))
728 return STG_E_INVALIDFLAG;
731 * As documented.
733 if ( !(grfMode & STGM_SHARE_EXCLUSIVE) ||
734 (grfMode & STGM_DELETEONRELEASE) ||
735 (grfMode & STGM_TRANSACTED) )
736 return STG_E_INVALIDFUNCTION;
739 * Initialize the out parameter
741 *ppstm = 0;
744 * Create a property enumeration to search the properties
746 propertyEnumeration = IEnumSTATSTGImpl_Construct(This->ancestorStorage,
747 This->rootPropertySetIndex);
749 foundPropertyIndex = IEnumSTATSTGImpl_FindProperty(propertyEnumeration,
750 pwcsName,
751 &currentProperty);
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);
762 else
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,
807 newPropertyIndex,
808 &newStreamProperty);
811 * Find a spot in the property chain for our newly created property.
813 updatePropertyChain(
814 (StorageImpl*)This,
815 newPropertyIndex,
816 newStreamProperty);
819 * Open the stream to return it.
821 newStream = StgStreamImpl_Construct(This, newPropertyIndex);
823 if (newStream != 0)
825 *ppstm = (IStream*)newStream;
828 * Since we are returning a pointer to the interface, we have to nail down
829 * the reference.
831 StgStreamImpl_AddRef(*ppstm);
833 else
835 return STG_E_INSUFFICIENTMEMORY;
838 return S_OK;
841 /************************************************************************
842 * Storage32BaseImpl_SetClass (IStorage)
844 * This method will write the specified CLSID in the property of this
845 * storage.
847 * See Windows documentation for more details on IStorage methods.
849 HRESULT WINAPI StorageBaseImpl_SetClass(
850 IStorage* iface,
851 REFCLSID clsid) /* [in] */
853 ICOM_THIS(StorageBaseImpl,iface);
854 HRESULT hRes = E_FAIL;
855 StgProperty curProperty;
856 BOOL success;
858 success = StorageImpl_ReadProperty(This->ancestorStorage,
859 This->rootPropertySetIndex,
860 &curProperty);
861 if (success)
863 curProperty.propertyUniqueID = *clsid;
865 success = StorageImpl_WriteProperty(This->ancestorStorage,
866 This->rootPropertySetIndex,
867 &curProperty);
868 if (success)
869 hRes = S_OK;
872 return hRes;
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(
887 IStorage* iface,
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;
901 HRESULT hr;
904 * Validate parameters
906 if (ppstg == 0)
907 return STG_E_INVALIDPOINTER;
909 if (pwcsName == 0)
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
922 *ppstg = 0;
925 * Create a property enumeration and search the properties
927 propertyEnumeration = IEnumSTATSTGImpl_Construct( This->ancestorStorage,
928 This->rootPropertySetIndex);
930 foundPropertyIndex = IEnumSTATSTGImpl_FindProperty(propertyEnumeration,
931 pwcsName,
932 &currentProperty);
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);
942 else
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,
986 newPropertyIndex,
987 &newProperty);
990 * Find a spot in the property chain for our newly created property.
992 updatePropertyChain(
993 This,
994 newPropertyIndex,
995 newProperty);
998 * Open it to get a pointer to return.
1000 hr = StorageBaseImpl_OpenStorage(
1001 iface,
1002 (OLECHAR*)pwcsName,
1004 grfMode,
1007 ppstg);
1009 if( (hr != S_OK) || (*ppstg == NULL))
1011 return hr;
1014 return S_OK;
1018 /***************************************************************************
1020 * Internal Method
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,
1039 &currentProperty);
1040 if (readSucessful)
1042 if (currentProperty.sizeOfNameString == 0)
1045 * The property existis and is available, we found it.
1047 newPropertyIndex = currentPropertyIndex;
1050 else
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
1091 * created property
1093 memset(&emptyProperty, 0, sizeof(StgProperty));
1096 * initialize them
1098 lastProperty = storage->bigBlockSize / PROPSET_BLOCK_SIZE * blockCount;
1100 for(
1101 propertyIndex = newPropertyIndex;
1102 propertyIndex < lastProperty;
1103 propertyIndex++)
1105 StorageImpl_WriteProperty(
1106 storage->ancestorStorage,
1107 propertyIndex,
1108 &emptyProperty);
1112 return newPropertyIndex;
1115 /****************************************************************************
1117 * Internal Method
1119 * Case insensitive comparaison of StgProperty.name by first considering
1120 * their size.
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;
1134 if (diff == 0)
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)));
1148 return diff;
1151 /****************************************************************************
1153 * Internal Method
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,
1169 &currentProperty);
1171 if (currentProperty.dirProperty != PROPERTY_NULL)
1174 * The root storage contains some element, therefore, start the research
1175 * for the appropriate location.
1177 BOOL found = 0;
1178 ULONG current, next, previous, currentPropertyId;
1181 * Keep the StgProperty sequence number of the storage first property
1183 currentPropertyId = currentProperty.dirProperty;
1186 * Read
1188 StorageImpl_ReadProperty(storage->ancestorStorage,
1189 currentProperty.dirProperty,
1190 &currentProperty);
1192 previous = currentProperty.previousProperty;
1193 next = currentProperty.nextProperty;
1194 current = currentPropertyId;
1196 while (found == 0)
1198 LONG diff = propertyNameCmp( newProperty.name, currentProperty.name);
1200 if (diff < 0)
1202 if (previous != PROPERTY_NULL)
1204 StorageImpl_ReadProperty(storage->ancestorStorage,
1205 previous,
1206 &currentProperty);
1207 current = previous;
1209 else
1211 currentProperty.previousProperty = newPropertyIndex;
1212 StorageImpl_WriteProperty(storage->ancestorStorage,
1213 current,
1214 &currentProperty);
1215 found = 1;
1218 else
1220 if (next != PROPERTY_NULL)
1222 StorageImpl_ReadProperty(storage->ancestorStorage,
1223 next,
1224 &currentProperty);
1225 current = next;
1227 else
1229 currentProperty.nextProperty = newPropertyIndex;
1230 StorageImpl_WriteProperty(storage->ancestorStorage,
1231 current,
1232 &currentProperty);
1233 found = 1;
1237 previous = currentProperty.previousProperty;
1238 next = currentProperty.nextProperty;
1241 else
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,
1249 &currentProperty);
1254 /*************************************************************************
1255 * CopyTo (IStorage)
1257 HRESULT WINAPI StorageImpl_CopyTo(
1258 IStorage* iface,
1259 DWORD ciidExclude, /* [in] */
1260 const IID *rgiidExclude,/* [size_is][unique][in] */
1261 SNB snbExclude, /* [unique][in] */
1262 IStorage *pstgDest) /* [unique][in] */
1264 return E_NOTIMPL;
1267 /*************************************************************************
1268 * MoveElementTo (IStorage)
1270 HRESULT WINAPI StorageImpl_MoveElementTo(
1271 IStorage* iface,
1272 const OLECHAR *pwcsName, /* [string][in] */
1273 IStorage *pstgDest, /* [unique][in] */
1274 const OLECHAR *pwcsNewName,/* [string][in] */
1275 DWORD grfFlags) /* [in] */
1277 return E_NOTIMPL;
1280 /*************************************************************************
1281 * Commit (IStorage)
1283 HRESULT WINAPI StorageImpl_Commit(
1284 IStorage* iface,
1285 DWORD grfCommitFlags)/* [in] */
1287 FIXME(ole, "(%ld): stub!\n", grfCommitFlags);
1288 return S_OK;
1291 /*************************************************************************
1292 * Revert (IStorage)
1294 HRESULT WINAPI StorageImpl_Revert(
1295 IStorage* iface)
1297 return E_NOTIMPL;
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(
1312 IStorage* iface,
1313 const OLECHAR *pwcsName)/* [string][in] */
1315 StorageImpl* const This=(StorageImpl*)iface;
1317 IEnumSTATSTGImpl* propertyEnumeration;
1318 HRESULT hr = S_OK;
1319 BOOL res;
1320 StgProperty propertyToDelete;
1321 StgProperty parentProperty;
1322 ULONG foundPropertyIndexToDelete;
1323 ULONG typeOfRelation;
1324 ULONG parentPropertyId;
1327 * Perform a sanity check on the parameters.
1329 if (pwcsName==NULL)
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,
1341 pwcsName,
1342 &propertyToDelete);
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,
1363 &parentProperty);
1365 assert(res==TRUE);
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;
1379 else
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,
1394 &parentProperty,
1395 &parentPropertyId);
1397 IEnumSTATSTGImpl_Destroy(propertyEnumeration2);
1400 if ( propertyToDelete.propertyType == PROPTYPE_STORAGE )
1402 hr = deleteStorageProperty(
1403 This,
1404 propertyToDelete.name);
1406 else if ( propertyToDelete.propertyType == PROPTYPE_STREAM )
1408 hr = deleteStreamProperty(
1409 This,
1410 foundPropertyIndexToDelete,
1411 propertyToDelete);
1414 if (hr!=S_OK)
1415 return hr;
1418 * Adjust the property chain
1420 hr = adjustPropertyChain(
1421 This,
1422 propertyToDelete,
1423 parentProperty,
1424 parentPropertyId,
1425 typeOfRelation);
1427 return hr;
1431 /*********************************************************************
1433 * Internal Method
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;
1445 HRESULT hr;
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,
1458 &childStorage);
1460 if (hr != S_OK)
1462 return hr;
1466 * Enumerate the elements
1468 IStorage_EnumElements( childStorage, 0, 0, 0, &elements);
1473 * Obtain the next element
1475 hr = IEnumSTATSTG_Next(elements, 1, &currentElement, NULL);
1476 if (hr==S_OK)
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);
1496 return destroyHr;
1499 /*********************************************************************
1501 * Internal Method
1503 * Perform the deletion of a stream node
1506 static HRESULT deleteStreamProperty(
1507 StorageImpl *parentStorage,
1508 ULONG indexOfPropertyToDelete,
1509 StgProperty propertyToDelete)
1511 IStream *pis;
1512 HRESULT hr;
1513 ULARGE_INTEGER size;
1515 size.HighPart = 0;
1516 size.LowPart = 0;
1518 hr = StorageBaseImpl_OpenStream(
1519 (IStorage*)parentStorage,
1520 (OLECHAR*)propertyToDelete.name,
1521 NULL,
1522 STGM_SHARE_EXCLUSIVE,
1524 &pis);
1526 if (hr!=S_OK)
1528 return(hr);
1532 * Zap the stream
1534 hr = IStream_SetSize(pis, size);
1536 if(hr != S_OK)
1538 return hr;
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,
1554 &propertyToDelete);
1556 return S_OK;
1559 /*********************************************************************
1561 * Internal Method
1563 * Finds a placeholder for the StgProperty within the Storage
1566 static HRESULT findPlaceholder(
1567 StorageImpl *storage,
1568 ULONG propertyIndexToStore,
1569 ULONG storePropertyIndex,
1570 INT typeOfRelation)
1572 StgProperty storeProperty;
1573 HRESULT hr = S_OK;
1574 BOOL res = TRUE;
1577 * Read the storage property
1579 res = StorageImpl_ReadProperty(
1580 storage->ancestorStorage,
1581 storePropertyIndex,
1582 &storeProperty);
1584 if(! res)
1586 return E_FAIL;
1589 if (typeOfRelation == PROPERTY_RELATION_PREVIOUS)
1591 if (storeProperty.previousProperty != PROPERTY_NULL)
1593 return findPlaceholder(
1594 storage,
1595 propertyIndexToStore,
1596 storeProperty.previousProperty,
1597 typeOfRelation);
1599 else
1601 storeProperty.previousProperty = propertyIndexToStore;
1604 else if (typeOfRelation == PROPERTY_RELATION_NEXT)
1606 if (storeProperty.nextProperty != PROPERTY_NULL)
1608 return findPlaceholder(
1609 storage,
1610 propertyIndexToStore,
1611 storeProperty.nextProperty,
1612 typeOfRelation);
1614 else
1616 storeProperty.nextProperty = propertyIndexToStore;
1619 else if (typeOfRelation == PROPERTY_RELATION_DIR)
1621 if (storeProperty.dirProperty != PROPERTY_NULL)
1623 return findPlaceholder(
1624 storage,
1625 propertyIndexToStore,
1626 storeProperty.dirProperty,
1627 typeOfRelation);
1629 else
1631 storeProperty.dirProperty = propertyIndexToStore;
1635 hr = StorageImpl_WriteProperty(
1636 storage->ancestorStorage,
1637 storePropertyIndex,
1638 &storeProperty);
1640 if(! hr)
1642 return E_FAIL;
1645 return S_OK;
1648 /*************************************************************************
1650 * Internal Method
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(
1656 StorageImpl *This,
1657 StgProperty propertyToDelete,
1658 StgProperty parentProperty,
1659 ULONG parentPropertyId,
1660 INT typeOfRelation)
1662 ULONG newLinkProperty = PROPERTY_NULL;
1663 BOOL needToFindAPlaceholder = FALSE;
1664 ULONG storeNode = PROPERTY_NULL;
1665 ULONG toStoreNode = PROPERTY_NULL;
1666 INT relationType = 0;
1667 HRESULT hr = S_OK;
1668 BOOL res = TRUE;
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,
1779 parentPropertyId,
1780 &parentProperty);
1781 if(! res)
1783 return E_FAIL;
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(
1793 This,
1794 toStoreNode,
1795 storeNode,
1796 relationType);
1799 return hr;
1803 /******************************************************************************
1804 * SetElementTimes (IStorage)
1806 HRESULT WINAPI StorageImpl_SetElementTimes(
1807 IStorage* iface,
1808 const OLECHAR *pwcsName,/* [string][in] */
1809 const FILETIME *pctime, /* [in] */
1810 const FILETIME *patime, /* [in] */
1811 const FILETIME *pmtime) /* [in] */
1813 return E_NOTIMPL;
1816 /******************************************************************************
1817 * SetStateBits (IStorage)
1819 HRESULT WINAPI StorageImpl_SetStateBits(
1820 IStorage* iface,
1821 DWORD grfStateBits,/* [in] */
1822 DWORD grfMask) /* [in] */
1824 return E_NOTIMPL;
1827 HRESULT StorageImpl_Construct(
1828 StorageImpl* This,
1829 HANDLE hFile,
1830 DWORD openFlags)
1832 HRESULT hr = S_OK;
1833 StgProperty currentProperty;
1834 BOOL readSucessful;
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
1850 * to this.
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,
1865 openFlags,
1866 This->bigBlockSize);
1868 if (This->bigBlockFile == 0)
1869 return E_FAIL;
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,
1883 BLOCK_UNUSED,
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
1900 size.HighPart = 0;
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);
1913 else
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(
1938 This,
1939 &This->smallBlockDepotStart,
1940 PROPERTY_NULL);
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(
1974 This,
1975 currentPropertyIndex,
1976 &currentProperty);
1978 if (readSucessful)
1980 if ( (currentProperty.sizeOfNameString != 0 ) &&
1981 (currentProperty.propertyType == PROPTYPE_ROOT) )
1983 This->rootPropertySetIndex = currentPropertyIndex;
1987 currentPropertyIndex++;
1989 } while (readSucessful && (This->rootPropertySetIndex == PROPERTY_NULL) );
1991 if (!readSucessful)
1993 /* TODO CLEANUP */
1994 return E_FAIL;
1998 * Create the block chain abstraction for the small block root chain.
2000 This->smallBlockRootChain = BlockChainStream_Construct(
2001 This,
2002 NULL,
2003 This->rootPropertySetIndex);
2005 return hr;
2008 void StorageImpl_Destroy(
2009 StorageImpl* This)
2011 BlockChainStream_Destroy(This->smallBlockRootChain);
2012 BlockChainStream_Destroy(This->rootBlockChain);
2013 BlockChainStream_Destroy(This->smallBlockDepotChain);
2015 BIGBLOCKFILE_Destructor(This->bigBlockFile);
2016 return;
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(
2027 StorageImpl* This)
2029 ULONG depotBlockIndexPos;
2030 void *depotBuffer;
2031 ULONG depotBlockOffset;
2032 ULONG blocksPerDepot = This->bigBlockSize / sizeof(ULONG);
2033 ULONG nextBlockIndex = BLOCK_SPECIAL;
2034 int depotIndex = 0;
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,
2067 depotBlockIndexPos,
2068 BLOCK_SPECIAL);
2070 /* Save new header information.
2072 StorageImpl_SaveFileHeader(This);
2075 else
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;
2096 else
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,
2109 depotBlockIndexPos,
2110 BLOCK_SPECIAL);
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);
2144 depotIndex++;
2145 depotBlockOffset = 0;
2148 This->prevFreeBlock = freeBlock;
2150 return freeBlock;
2153 /******************************************************************************
2154 * Storage32Impl_AddBlockDepot
2156 * This will create a depot block, essentially it is a block initialized
2157 * to BLOCK_UNUSEDs.
2159 void Storage32Impl_AddBlockDepot(StorageImpl* This, ULONG blockIndex)
2161 BYTE* blockBuffer;
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);
2197 extBlockCount--;
2200 if (extBlockIndex != BLOCK_UNUSED)
2202 BYTE* depotBuffer;
2204 depotBuffer = StorageImpl_GetROBigBlock(This, extBlockIndex);
2206 if (depotBuffer != 0)
2208 StorageUtl_ReadDWord(depotBuffer,
2209 extBlockOffset * sizeof(ULONG),
2210 &blockIndex);
2212 StorageImpl_ReleaseBigBlock(This, depotBuffer);
2216 return blockIndex;
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,
2227 ULONG depotIndex,
2228 ULONG blockIndex)
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);
2241 extBlockCount--;
2244 if (extBlockIndex != BLOCK_UNUSED)
2246 BYTE* depotBuffer;
2248 depotBuffer = StorageImpl_GetBigBlock(This, extBlockIndex);
2250 if (depotBuffer != 0)
2252 StorageUtl_WriteDWord(depotBuffer,
2253 extBlockOffset * sizeof(ULONG),
2254 blockIndex);
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;
2286 else
2288 int i;
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);
2312 return index;
2315 /******************************************************************************
2316 * Storage32Impl_FreeBigBlock
2318 * This method will flag the specified block as free in the big block depot.
2320 void StorageImpl_FreeBigBlock(
2321 StorageImpl* This,
2322 ULONG blockIndex)
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
2334 * in the chain.
2336 * Params: This - Pointer to the Storage object.
2337 * blockIndex - Index of the block to retrieve the chain
2338 * for.
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
2343 * chain.
2344 * BLOCK_END_OF_CHAIN - If the block given was the last in
2345 * a chain.
2346 * BLOCK_UNUSED - If the block given was not past of a chain
2347 * and is available.
2348 * BLOCK_EXTBBDEPOT - This block is part of the extended
2349 * big block depot.
2351 * See Windows documentation for more details on IStorage methods.
2353 ULONG StorageImpl_GetNextBlockInChain(
2354 StorageImpl* This,
2355 ULONG blockIndex)
2357 ULONG offsetInDepot = blockIndex * sizeof (ULONG);
2358 ULONG depotBlockCount = offsetInDepot / This->bigBlockSize;
2359 ULONG depotBlockOffset = offsetInDepot % This->bigBlockSize;
2360 ULONG nextBlockIndex = BLOCK_SPECIAL;
2361 void* depotBuffer;
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];
2377 else
2380 * We have to look in the extended depot.
2382 depotBlockIndexPos = Storage32Impl_GetExtDepotBlock(This, depotBlockCount);
2385 depotBuffer = StorageImpl_GetROBigBlock(This, depotBlockIndexPos);
2387 if (depotBuffer!=0)
2389 int index;
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.
2411 * NOTES:
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
2414 * depot.
2416 * Return values:
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);
2425 void* depotBuffer;
2427 depotBuffer = StorageImpl_GetROBigBlock(This, blockIndex);
2429 if (depotBuffer!=0)
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
2446 * do the following
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(
2454 StorageImpl* This,
2455 ULONG blockIndex,
2456 ULONG nextBlock)
2458 ULONG offsetInDepot = blockIndex * sizeof (ULONG);
2459 ULONG depotBlockCount = offsetInDepot / This->bigBlockSize;
2460 ULONG depotBlockOffset = offsetInDepot % This->bigBlockSize;
2461 ULONG depotBlockIndexPos;
2462 void* depotBuffer;
2464 assert(depotBlockCount < This->bigBlockDepotCount);
2465 assert(blockIndex != nextBlock);
2467 if (depotBlockCount < COUNT_BBDEPOTINHEADER)
2469 depotBlockIndexPos = This->bigBlockDepotStart[depotBlockCount];
2471 else
2474 * We have to look in the extended depot.
2476 depotBlockIndexPos = Storage32Impl_GetExtDepotBlock(This, depotBlockCount);
2479 depotBuffer = StorageImpl_GetBigBlock(This, depotBlockIndexPos);
2481 if (depotBuffer!=0)
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(
2502 StorageImpl* This)
2504 HRESULT hr = STG_E_FILENOTFOUND;
2505 void* headerBigBlock = NULL;
2506 int index;
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
2520 * found.
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(
2535 headerBigBlock,
2536 OFFSET_BIGBLOCKSIZEBITS,
2537 &This->bigBlockSizeBits);
2539 StorageUtl_ReadWord(
2540 headerBigBlock,
2541 OFFSET_SMALLBLOCKSIZEBITS,
2542 &This->smallBlockSizeBits);
2544 StorageUtl_ReadDWord(
2545 headerBigBlock,
2546 OFFSET_BBDEPOTCOUNT,
2547 &This->bigBlockDepotCount);
2549 StorageUtl_ReadDWord(
2550 headerBigBlock,
2551 OFFSET_ROOTSTARTBLOCK,
2552 &This->rootStartBlock);
2554 StorageUtl_ReadDWord(
2555 headerBigBlock,
2556 OFFSET_SBDEPOTSTART,
2557 &This->smallBlockDepotStart);
2559 StorageUtl_ReadDWord(
2560 headerBigBlock,
2561 OFFSET_EXTBBDEPOTSTART,
2562 &This->extBigBlockDepotStart);
2564 StorageUtl_ReadDWord(
2565 headerBigBlock,
2566 OFFSET_EXTBBDEPOTCOUNT,
2567 &This->extBigBlockDepotCount);
2569 for (index = 0; index < COUNT_BBDEPOTINHEADER; index ++)
2571 StorageUtl_ReadDWord(
2572 headerBigBlock,
2573 OFFSET_BBDEPOTSTART + (sizeof(ULONG)*index),
2574 &(This->bigBlockDepotStart[index]));
2578 * Make the bitwise arithmetic to get the size of the blocks in bytes.
2580 if ((1 << 2) == 4)
2582 This->bigBlockSize = 0x000000001 << (DWORD)This->bigBlockSizeBits;
2583 This->smallBlockSize = 0x000000001 << (DWORD)This->smallBlockSizeBits;
2585 else
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);
2604 return hr;
2607 /******************************************************************************
2608 * Storage32Impl_SaveFileHeader
2610 * This method will save to the file the header, i.e. big block -1.
2612 void StorageImpl_SaveFileHeader(
2613 StorageImpl* This)
2615 BYTE headerBigBlock[BIG_BLOCK_SIZE];
2616 int index;
2617 BOOL success;
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.
2627 if (!success)
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(
2655 headerBigBlock,
2656 OFFSET_BIGBLOCKSIZEBITS,
2657 This->bigBlockSizeBits);
2659 StorageUtl_WriteWord(
2660 headerBigBlock,
2661 OFFSET_SMALLBLOCKSIZEBITS,
2662 This->smallBlockSizeBits);
2664 StorageUtl_WriteDWord(
2665 headerBigBlock,
2666 OFFSET_BBDEPOTCOUNT,
2667 This->bigBlockDepotCount);
2669 StorageUtl_WriteDWord(
2670 headerBigBlock,
2671 OFFSET_ROOTSTARTBLOCK,
2672 This->rootStartBlock);
2674 StorageUtl_WriteDWord(
2675 headerBigBlock,
2676 OFFSET_SBDEPOTSTART,
2677 This->smallBlockDepotStart);
2679 StorageUtl_WriteDWord(
2680 headerBigBlock,
2681 OFFSET_EXTBBDEPOTSTART,
2682 This->extBigBlockDepotStart);
2684 StorageUtl_WriteDWord(
2685 headerBigBlock,
2686 OFFSET_EXTBBDEPOTCOUNT,
2687 This->extBigBlockDepotCount);
2689 for (index = 0; index < COUNT_BBDEPOTINHEADER; index ++)
2691 StorageUtl_WriteDWord(
2692 headerBigBlock,
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(
2710 StorageImpl* This,
2711 ULONG index,
2712 StgProperty* buffer)
2714 BYTE currentProperty[PROPSET_BLOCK_SIZE];
2715 ULARGE_INTEGER offsetInPropSet;
2716 BOOL readSucessful;
2717 ULONG bytesRead;
2719 offsetInPropSet.HighPart = 0;
2720 offsetInPropSet.LowPart = index * PROPSET_BLOCK_SIZE;
2722 readSucessful = BlockChainStream_ReadAt(
2723 This->rootBlockChain,
2724 offsetInPropSet,
2725 PROPSET_BLOCK_SIZE,
2726 currentProperty,
2727 &bytesRead);
2729 if (readSucessful)
2731 memset(buffer->name, 0, sizeof(buffer->name));
2732 memcpy(
2733 buffer->name,
2734 currentProperty+OFFSET_PS_NAME,
2735 PROPERTY_NAME_BUFFER_LEN );
2737 memcpy(&buffer->propertyType, currentProperty + OFFSET_PS_PROPERTYTYPE, 1);
2739 StorageUtl_ReadWord(
2740 currentProperty,
2741 OFFSET_PS_NAMELENGTH,
2742 &buffer->sizeOfNameString);
2744 StorageUtl_ReadDWord(
2745 currentProperty,
2746 OFFSET_PS_PREVIOUSPROP,
2747 &buffer->previousProperty);
2749 StorageUtl_ReadDWord(
2750 currentProperty,
2751 OFFSET_PS_NEXTPROP,
2752 &buffer->nextProperty);
2754 StorageUtl_ReadDWord(
2755 currentProperty,
2756 OFFSET_PS_DIRPROP,
2757 &buffer->dirProperty);
2759 StorageUtl_ReadGUID(
2760 currentProperty,
2761 OFFSET_PS_GUID,
2762 &buffer->propertyUniqueID);
2764 StorageUtl_ReadDWord(
2765 currentProperty,
2766 OFFSET_PS_TSS1,
2767 &buffer->timeStampS1);
2769 StorageUtl_ReadDWord(
2770 currentProperty,
2771 OFFSET_PS_TSD1,
2772 &buffer->timeStampD1);
2774 StorageUtl_ReadDWord(
2775 currentProperty,
2776 OFFSET_PS_TSS2,
2777 &buffer->timeStampS2);
2779 StorageUtl_ReadDWord(
2780 currentProperty,
2781 OFFSET_PS_TSD2,
2782 &buffer->timeStampD2);
2784 StorageUtl_ReadDWord(
2785 currentProperty,
2786 OFFSET_PS_STARTBLOCK,
2787 &buffer->startingBlock);
2789 StorageUtl_ReadDWord(
2790 currentProperty,
2791 OFFSET_PS_SIZE,
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(
2804 StorageImpl* This,
2805 ULONG index,
2806 StgProperty* buffer)
2808 BYTE currentProperty[PROPSET_BLOCK_SIZE];
2809 ULARGE_INTEGER offsetInPropSet;
2810 BOOL writeSucessful;
2811 ULONG bytesWritten;
2813 offsetInPropSet.HighPart = 0;
2814 offsetInPropSet.LowPart = index * PROPSET_BLOCK_SIZE;
2816 memset(currentProperty, 0, PROPSET_BLOCK_SIZE);
2818 memcpy(
2819 currentProperty + OFFSET_PS_NAME,
2820 buffer->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(
2831 currentProperty,
2832 OFFSET_PS_NAMELENGTH,
2833 buffer->sizeOfNameString);
2835 StorageUtl_WriteDWord(
2836 currentProperty,
2837 OFFSET_PS_PREVIOUSPROP,
2838 buffer->previousProperty);
2840 StorageUtl_WriteDWord(
2841 currentProperty,
2842 OFFSET_PS_NEXTPROP,
2843 buffer->nextProperty);
2845 StorageUtl_WriteDWord(
2846 currentProperty,
2847 OFFSET_PS_DIRPROP,
2848 buffer->dirProperty);
2850 StorageUtl_WriteGUID(
2851 currentProperty,
2852 OFFSET_PS_GUID,
2853 &buffer->propertyUniqueID);
2855 StorageUtl_WriteDWord(
2856 currentProperty,
2857 OFFSET_PS_TSS1,
2858 buffer->timeStampS1);
2860 StorageUtl_WriteDWord(
2861 currentProperty,
2862 OFFSET_PS_TSD1,
2863 buffer->timeStampD1);
2865 StorageUtl_WriteDWord(
2866 currentProperty,
2867 OFFSET_PS_TSS2,
2868 buffer->timeStampS2);
2870 StorageUtl_WriteDWord(
2871 currentProperty,
2872 OFFSET_PS_TSD2,
2873 buffer->timeStampD2);
2875 StorageUtl_WriteDWord(
2876 currentProperty,
2877 OFFSET_PS_STARTBLOCK,
2878 buffer->startingBlock);
2880 StorageUtl_WriteDWord(
2881 currentProperty,
2882 OFFSET_PS_SIZE,
2883 buffer->size.LowPart);
2885 writeSucessful = BlockChainStream_WriteAt(This->rootBlockChain,
2886 offsetInPropSet,
2887 PROPSET_BLOCK_SIZE,
2888 currentProperty,
2889 &bytesWritten);
2890 return writeSucessful;
2893 BOOL StorageImpl_ReadBigBlock(
2894 StorageImpl* This,
2895 ULONG blockIndex,
2896 void* buffer)
2898 void* bigBlockBuffer;
2900 bigBlockBuffer = StorageImpl_GetROBigBlock(This, blockIndex);
2902 if (bigBlockBuffer!=0)
2904 memcpy(buffer, bigBlockBuffer, This->bigBlockSize);
2906 StorageImpl_ReleaseBigBlock(This, bigBlockBuffer);
2908 return TRUE;
2911 return FALSE;
2914 BOOL StorageImpl_WriteBigBlock(
2915 StorageImpl* This,
2916 ULONG blockIndex,
2917 void* buffer)
2919 void* bigBlockBuffer;
2921 bigBlockBuffer = StorageImpl_GetBigBlock(This, blockIndex);
2923 if (bigBlockBuffer!=0)
2925 memcpy(bigBlockBuffer, buffer, This->bigBlockSize);
2927 StorageImpl_ReleaseBigBlock(This, bigBlockBuffer);
2929 return TRUE;
2932 return FALSE;
2935 void* StorageImpl_GetROBigBlock(
2936 StorageImpl* This,
2937 ULONG blockIndex)
2939 return BIGBLOCKFILE_GetROBigBlock(This->bigBlockFile, blockIndex);
2942 void* StorageImpl_GetBigBlock(
2943 StorageImpl* This,
2944 ULONG blockIndex)
2946 return BIGBLOCKFILE_GetBigBlock(This->bigBlockFile, blockIndex);
2949 void StorageImpl_ReleaseBigBlock(
2950 StorageImpl* This,
2951 void* pBigBlock)
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(
2963 StorageImpl* This,
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,
2982 &bbHeadOfChain,
2983 PROPERTY_NULL);
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.
2995 offset.LowPart = 0;
2996 offset.HighPart = 0;
2997 cbTotalRead = 0;
2998 cbTotalWritten = 0;
3002 successRead = SmallBlockChainStream_ReadAt(*ppsbChain,
3003 offset,
3004 sizeof(buffer),
3005 buffer,
3006 &cbRead);
3007 cbTotalRead += cbRead;
3009 successWrite = BlockChainStream_WriteAt(bbTempChain,
3010 offset,
3011 cbRead,
3012 buffer,
3013 &cbWritten);
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;
3026 size.HighPart = 0;
3027 size.LowPart = 0;
3028 SmallBlockChainStream_SetSize(*ppsbChain, size);
3029 SmallBlockChainStream_Destroy(*ppsbChain);
3030 *ppsbChain = 0;
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,
3048 NULL,
3049 propertyIndex);
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));
3069 if (newStorage!=0)
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;
3090 return newStorage;
3093 return 0;
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
3108 ** does nothing.
3110 HRESULT WINAPI StorageInternalImpl_Commit(
3111 IStorage* iface,
3112 DWORD grfCommitFlags) /* [in] */
3114 return S_OK;
3117 /******************************************************************************
3119 ** Storage32InternalImpl_Revert
3121 ** The non-root storages cannot be opened in transacted mode thus this function
3122 ** does nothing.
3124 HRESULT WINAPI StorageInternalImpl_Revert(
3125 IStorage* iface)
3127 return S_OK;
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,
3185 REFIID riid,
3186 void** ppvObject)
3188 IEnumSTATSTGImpl* const This=(IEnumSTATSTGImpl*)iface;
3191 * Perform a sanity check on the parameters.
3193 if (ppvObject==0)
3194 return E_INVALIDARG;
3197 * Initialize the return parameter.
3199 *ppvObject = 0;
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
3221 * successful
3223 IEnumSTATSTGImpl_AddRef((IEnumSTATSTG*)This);
3225 return S_OK;
3228 ULONG WINAPI IEnumSTATSTGImpl_AddRef(
3229 IEnumSTATSTG* iface)
3231 IEnumSTATSTGImpl* const This=(IEnumSTATSTGImpl*)iface;
3233 This->ref++;
3234 return This->ref;
3237 ULONG WINAPI IEnumSTATSTGImpl_Release(
3238 IEnumSTATSTG* iface)
3240 IEnumSTATSTGImpl* const This=(IEnumSTATSTGImpl*)iface;
3242 ULONG newRef;
3244 This->ref--;
3245 newRef = This->ref;
3248 * If the reference count goes down to 0, perform suicide.
3250 if (newRef==0)
3252 IEnumSTATSTGImpl_Destroy(This);
3255 return newRef;;
3258 HRESULT WINAPI IEnumSTATSTGImpl_Next(
3259 IEnumSTATSTG* iface,
3260 ULONG celt,
3261 STATSTG* rgelt,
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
3288 *pceltFetched = 0;
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,
3307 currentSearchNode,
3308 &currentProperty);
3311 * Copy the information to the return buffer.
3313 StorageUtl_CopyPropertyToSTATSTG(currentReturnStruct,
3314 &currentProperty,
3315 STATFLAG_DEFAULT);
3318 * Step to the next item in the iteration
3320 (*pceltFetched)++;
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)
3335 return S_OK;
3337 return S_FALSE;
3341 HRESULT WINAPI IEnumSTATSTGImpl_Skip(
3342 IEnumSTATSTG* iface,
3343 ULONG celt)
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,
3368 currentSearchNode,
3369 &currentProperty);
3372 * Step to the next item in the iteration
3374 objectFetched++;
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)
3388 return S_OK;
3390 return S_FALSE;
3393 HRESULT WINAPI IEnumSTATSTGImpl_Reset(
3394 IEnumSTATSTG* iface)
3396 IEnumSTATSTGImpl* const This=(IEnumSTATSTGImpl*)iface;
3398 StgProperty rootProperty;
3399 BOOL readSucessful;
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,
3412 &rootProperty);
3414 if (readSucessful)
3416 assert(rootProperty.sizeOfNameString!=0);
3419 * Push the search node in the search stack.
3421 IEnumSTATSTGImpl_PushSearchNode(This, rootProperty.dirProperty);
3424 return S_OK;
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.
3438 if (ppenum==0)
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
3447 * the ole one.
3449 newClone->stackSize = This->stackSize ;
3450 newClone->stackMaxSize = This->stackMaxSize ;
3451 newClone->stackToVisit =
3452 HeapAlloc(GetProcessHeap(), 0, sizeof(ULONG) * newClone->stackMaxSize);
3454 memcpy(
3455 newClone->stackToVisit,
3456 This->stackToVisit,
3457 sizeof(ULONG) * newClone->stackSize);
3459 *ppenum = (IEnumSTATSTG*)newClone;
3462 * Don't forget to nail down a reference to the clone before
3463 * returning it.
3465 IEnumSTATSTGImpl_AddRef(*ppenum);
3467 return S_OK;
3470 INT IEnumSTATSTGImpl_FindParentProperty(
3471 IEnumSTATSTGImpl *This,
3472 ULONG childProperty,
3473 StgProperty *currentProperty,
3474 ULONG *thisNodeId)
3476 ULONG currentSearchNode;
3477 ULONG foundNode;
3480 * To avoid the special case, get another pointer to a ULONG value if
3481 * the caller didn't supply one.
3483 if (thisNodeId==0)
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,
3509 currentSearchNode,
3510 currentProperty);
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,
3558 currentSearchNode,
3559 currentProperty);
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,
3582 ULONG nodeToPush)
3584 StgProperty rootProperty;
3585 BOOL readSucessful;
3588 * First, make sure we're not trying to push an unexisting node.
3590 if (nodeToPush==PROPERTY_NULL)
3591 return;
3594 * First push the node to the stack
3596 if (This->stackSize == This->stackMaxSize)
3598 This->stackMaxSize += ENUMSTATSGT_SIZE_INCREMENT;
3600 This->stackToVisit = HeapReAlloc(
3601 GetProcessHeap(),
3603 This->stackToVisit,
3604 sizeof(ULONG) * This->stackMaxSize);
3607 This->stackToVisit[This->stackSize] = nodeToPush;
3608 This->stackSize++;
3611 * Read the root property from the storage.
3613 readSucessful = StorageImpl_ReadProperty(
3614 This->parentStorage,
3615 nodeToPush,
3616 &rootProperty);
3618 if (readSucessful)
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,
3631 BOOL remove)
3633 ULONG topNode;
3635 if (This->stackSize == 0)
3636 return PROPERTY_NULL;
3638 topNode = This->stackToVisit[This->stackSize-1];
3640 if (remove)
3641 This->stackSize--;
3643 return topNode;
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,
3691 int statFlags)
3694 * The copy of the string occurs only when the flag is not set
3696 if ((statFlags & STATFLAG_NONAME) != 0)
3698 destination->pwcsName = 0;
3700 else
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:
3711 case PROPTYPE_ROOT:
3712 destination->type = STGTY_STORAGE;
3713 break;
3714 case PROPTYPE_STREAM:
3715 destination->type = STGTY_STREAM;
3716 break;
3717 default:
3718 destination->type = STGTY_STREAM;
3719 break;
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;
3745 ULONG blockIndex;
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(
3764 parentStorage,
3765 blockIndex);
3768 return newStream;
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;
3787 BOOL readSucessful;
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,
3797 &chainProperty);
3799 if (readSucessful)
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)
3817 ULONG blockIndex;
3818 ULONG count = 0;
3820 blockIndex = BlockChainStream_GetHeadOfChain(This);
3822 while (blockIndex != BLOCK_END_OF_CHAIN)
3824 count++;
3826 blockIndex = StorageImpl_GetNextBlockInChain(
3827 This->parentStorage,
3828 blockIndex);
3831 return count;
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,
3843 ULONG size,
3844 void* buffer,
3845 ULONG* bytesRead)
3847 ULONG blockNoInSequence = offset.LowPart / This->parentStorage->bigBlockSize;
3848 ULONG offsetInBlock = offset.LowPart % This->parentStorage->bigBlockSize;
3849 ULONG bytesToReadInBuffer;
3850 ULONG blockIndex;
3851 BYTE* bufferWalker;
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;
3867 else
3869 blockIndex = BlockChainStream_GetHeadOfChain(This);
3870 This->lastBlockNoInSequence = blockNoInSequence;
3873 while ( (blockNoInSequence > 0) && (blockIndex != BLOCK_END_OF_CHAIN))
3875 blockIndex =
3876 StorageImpl_GetNextBlockInChain(This->parentStorage, blockIndex);
3878 blockNoInSequence--;
3881 This->lastBlockNoInSequenceIndex = blockIndex;
3884 * Start reading the buffer.
3886 *bytesRead = 0;
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
3900 bigBlockBuffer =
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.
3910 blockIndex =
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 */
3920 return (size == 0);
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,
3932 ULONG size,
3933 const void* buffer,
3934 ULONG* bytesWritten)
3936 ULONG blockNoInSequence = offset.LowPart / This->parentStorage->bigBlockSize;
3937 ULONG offsetInBlock = offset.LowPart % This->parentStorage->bigBlockSize;
3938 ULONG bytesToWrite;
3939 ULONG blockIndex;
3940 BYTE* bufferWalker;
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;
3957 else
3959 blockIndex = BlockChainStream_GetHeadOfChain(This);
3960 This->lastBlockNoInSequence = blockNoInSequence;
3963 while ( (blockNoInSequence > 0) && (blockIndex != BLOCK_END_OF_CHAIN))
3965 blockIndex =
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.
3977 *bytesWritten = 0;
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.
3985 bytesToWrite =
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.
4000 blockIndex =
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 */
4009 return (size == 0);
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;
4021 ULONG numBlocks;
4022 ULONG count = 1;
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)
4030 numBlocks++;
4032 blockIndex = BlockChainStream_GetHeadOfChain(This);
4035 * Go to the new end of chain
4037 while (count < numBlocks)
4039 blockIndex =
4040 StorageImpl_GetNextBlockInChain(This->parentStorage, blockIndex);
4042 count++;
4045 /* Get the next block before marking the new end */
4046 extraBlock =
4047 StorageImpl_GetNextBlockInChain(This->parentStorage, blockIndex);
4049 /* Mark the new end of chain */
4050 StorageImpl_SetNextBlockInChain(
4051 This->parentStorage,
4052 blockIndex,
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)
4063 blockIndex =
4064 StorageImpl_GetNextBlockInChain(This->parentStorage, extraBlock);
4066 StorageImpl_FreeBigBlock(This->parentStorage, extraBlock);
4067 extraBlock = blockIndex;
4070 return TRUE;
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;
4082 ULONG newNumBlocks;
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,
4094 blockIndex,
4095 BLOCK_END_OF_CHAIN);
4097 if (This->headOfStreamPlaceHolder != 0)
4099 *(This->headOfStreamPlaceHolder) = blockIndex;
4101 else
4103 StgProperty chainProp;
4104 assert(This->ownerPropertyIndex != PROPERTY_NULL);
4106 StorageImpl_ReadProperty(
4107 This->parentStorage,
4108 This->ownerPropertyIndex,
4109 &chainProp);
4111 chainProp.startingBlock = blockIndex;
4113 StorageImpl_WriteProperty(
4114 This->parentStorage,
4115 This->ownerPropertyIndex,
4116 &chainProp);
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)
4129 newNumBlocks++;
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)
4140 This->numBlocks++;
4141 currentBlock = blockIndex;
4143 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,
4162 currentBlock,
4163 blockIndex);
4165 StorageImpl_SetNextBlockInChain(
4166 This->parentStorage,
4167 blockIndex,
4168 BLOCK_END_OF_CHAIN);
4170 currentBlock = blockIndex;
4171 oldNumBlocks++;
4174 This->tailIndex = blockIndex;
4175 This->numBlocks = newNumBlocks;
4177 return TRUE;
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)
4197 return TRUE;
4199 if (newSize.LowPart < size.LowPart)
4201 BlockChainStream_Shrink(This, newSize);
4203 else
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);
4223 return TRUE;
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,
4245 &chainProperty);
4247 return chainProperty.size;
4249 else
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
4254 * size of them
4256 ULARGE_INTEGER result;
4257 result.HighPart = 0;
4259 result.LowPart =
4260 BlockChainStream_GetCount(This) *
4261 This->parentStorage->bigBlockSize;
4263 return result;
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;
4282 return newStream;
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;
4300 BOOL readSucessful;
4302 if (This->ownerPropertyIndex)
4304 readSucessful = StorageImpl_ReadProperty(
4305 This->parentStorage,
4306 This->ownerPropertyIndex,
4307 &chainProperty);
4309 if (readSucessful)
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.
4324 * Return Values:
4325 * - BLOCK_END_OF_CHAIN: end of this chain
4326 * - BLOCK_UNUSED: small block 'blockIndex' is free
4328 ULONG SmallBlockChainStream_GetNextBlockInChain(
4329 SmallBlockChainStream* This,
4330 ULONG blockIndex)
4332 ULARGE_INTEGER offsetOfBlockInDepot;
4333 DWORD buffer;
4334 ULONG nextBlockInChain = BLOCK_END_OF_CHAIN;
4335 ULONG bytesRead;
4336 BOOL success;
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,
4347 sizeof(DWORD),
4348 &buffer,
4349 &bytesRead);
4351 if (success)
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
4363 * block depot.
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,
4369 ULONG blockIndex,
4370 ULONG nextBlock)
4372 ULARGE_INTEGER offsetOfBlockInDepot;
4373 DWORD buffer;
4374 ULONG bytesWritten;
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,
4387 sizeof(DWORD),
4388 &buffer,
4389 &bytesWritten);
4392 /******************************************************************************
4393 * SmallBlockChainStream_FreeBlock
4395 * Flag small block 'blockIndex' as free in the small block depot.
4397 void SmallBlockChainStream_FreeBlock(
4398 SmallBlockChainStream* This,
4399 ULONG blockIndex)
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
4409 * necessary.
4411 ULONG SmallBlockChainStream_GetNextFreeBlock(
4412 SmallBlockChainStream* This)
4414 ULARGE_INTEGER offsetOfBlockInDepot;
4415 DWORD buffer;
4416 ULONG bytesRead;
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,
4434 sizeof(DWORD),
4435 &buffer,
4436 &bytesRead);
4439 * If we run out of space for the small block depot, enlarge it
4441 if (success)
4443 StorageUtl_ReadDWord(&buffer, 0, &nextBlockIndex);
4445 if (nextBlockIndex != BLOCK_UNUSED)
4446 blockIndex++;
4448 else
4450 ULONG count =
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;
4461 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,
4469 sbdIndex,
4470 newsbdIndex);
4472 StorageImpl_SetNextBlockInChain(
4473 This->parentStorage,
4474 newsbdIndex,
4475 BLOCK_END_OF_CHAIN);
4478 * Initialize all the small blocks to free
4480 smallBlockDepot =
4481 StorageImpl_GetBigBlock(This->parentStorage, newsbdIndex);
4483 memset(smallBlockDepot, BLOCK_UNUSED, This->parentStorage->bigBlockSize);
4484 StorageImpl_ReleaseBigBlock(This->parentStorage, smallBlockDepot);
4486 if (count == 0)
4489 * We have just created the small block depot.
4491 StgProperty rootProp;
4492 ULONG sbStartIndex;
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
4503 sbStartIndex =
4504 StorageImpl_GetNextFreeBigBlock(This->parentStorage);
4506 StorageImpl_SetNextBlockInChain(
4507 This->parentStorage,
4508 sbStartIndex,
4509 BLOCK_END_OF_CHAIN);
4511 StorageImpl_ReadProperty(
4512 This->parentStorage,
4513 This->parentStorage->rootPropertySetIndex,
4514 &rootProp);
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,
4523 &rootProp);
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,
4542 &rootProp);
4544 if (rootProp.size.LowPart <
4545 (blocksRequired * This->parentStorage->bigBlockSize))
4547 rootProp.size.LowPart += This->parentStorage->bigBlockSize;
4549 BlockChainStream_SetSize(
4550 This->parentStorage->smallBlockRootChain,
4551 rootProp.size);
4553 StorageImpl_WriteProperty(
4554 This->parentStorage,
4555 This->parentStorage->rootPropertySetIndex,
4556 &rootProp);
4560 return blockIndex;
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,
4573 ULONG size,
4574 void* buffer,
4575 ULONG* bytesRead)
4577 ULARGE_INTEGER offsetInBigBlockFile;
4578 ULONG blockNoInSequence =
4579 offset.LowPart / This->parentStorage->smallBlockSize;
4581 ULONG offsetInBlock = offset.LowPart % This->parentStorage->smallBlockSize;
4582 ULONG bytesToReadInBuffer;
4583 ULONG blockIndex;
4584 ULONG bytesReadFromBigBlockFile;
4585 BYTE* bufferWalker;
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.
4607 *bytesRead = 0;
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,
4633 bufferWalker,
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 */
4648 return (size == 0);
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,
4661 ULONG size,
4662 const void* buffer,
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;
4671 ULONG blockIndex;
4672 ULONG bytesWrittenFromBigBlockFile;
4673 BYTE* bufferWalker;
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.
4698 *bytesWritten = 0;
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,
4723 bufferWalker,
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 */
4738 return (size == 0);
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;
4751 ULONG numBlocks;
4752 ULONG count = 1;
4754 numBlocks = newSize.LowPart / This->parentStorage->smallBlockSize;
4756 if ((newSize.LowPart % This->parentStorage->smallBlockSize) != 0)
4757 numBlocks++;
4759 blockIndex = SmallBlockChainStream_GetHeadOfChain(This);
4762 * Go to the new end of chain
4764 while (count < numBlocks)
4766 blockIndex = SmallBlockChainStream_GetNextBlockInChain(This, blockIndex);
4767 count++;
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(
4775 This,
4776 blockIndex,
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;
4789 return TRUE;
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;
4802 ULONG newNumBlocks;
4803 ULONG oldNumBlocks = 0;
4805 blockIndex = SmallBlockChainStream_GetHeadOfChain(This);
4808 * Empty chain
4810 if (blockIndex == BLOCK_END_OF_CHAIN)
4812 StgProperty chainProp;
4814 StorageImpl_ReadProperty(This->parentStorage, This->ownerPropertyIndex,
4815 &chainProp);
4817 chainProp.startingBlock = SmallBlockChainStream_GetNextFreeBlock(This);
4819 StorageImpl_WriteProperty(This->parentStorage, This->ownerPropertyIndex,
4820 &chainProp);
4822 blockIndex = chainProp.startingBlock;
4823 SmallBlockChainStream_SetNextBlockInChain(
4824 This,
4825 blockIndex,
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)
4837 newNumBlocks++;
4840 * Go to the current end of chain
4842 while (blockIndex != BLOCK_END_OF_CHAIN)
4844 oldNumBlocks++;
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(
4858 This,
4859 blockIndex,
4860 BLOCK_END_OF_CHAIN);
4862 currentBlock = blockIndex;
4863 oldNumBlocks++;
4866 return TRUE;
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)
4877 ULONG blockIndex;
4878 ULONG count = 0;
4880 blockIndex = SmallBlockChainStream_GetHeadOfChain(This);
4882 while (blockIndex != BLOCK_END_OF_CHAIN)
4884 count++;
4886 blockIndex = SmallBlockChainStream_GetNextBlockInChain(This, blockIndex);
4889 return count;
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)
4909 return TRUE;
4911 if (newSize.LowPart < size.LowPart)
4913 SmallBlockChainStream_Shrink(This, newSize);
4915 else
4917 SmallBlockChainStream_Enlarge(This, newSize);
4920 return TRUE;
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,
4935 &chainProperty);
4937 return chainProperty.size;
4940 /******************************************************************************
4941 * StgCreateDocfile32 [OLE32.144]
4942 * TODO Validate grfMode (STGM)
4944 HRESULT WINAPI StgCreateDocfile(
4945 LPCOLESTR pwcsName,
4946 DWORD grfMode,
4947 DWORD reserved,
4948 IStorage **ppstgOpen)
4950 StorageImpl* newStorage = 0;
4951 HANDLE hFile = INVALID_HANDLE_VALUE;
4952 HRESULT hr = S_OK;
4953 DWORD shareMode;
4954 DWORD accessMode;
4955 DWORD creationMode;
4956 DWORD fileAttributes;
4957 WCHAR tempFileName[MAX_PATH];
4960 * Validate the parameters
4962 if (ppstgOpen == 0)
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.
4974 if (pwcsName == 0)
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 )
4983 tempPath[0] = '.';
4985 if (GetTempFileNameW(tempPath, prefix, 0, tempFileName) != 0)
4986 pwcsName = tempFileName;
4987 else
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;
5000 else
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.
5009 *ppstgOpen = 0;
5011 hFile = CreateFileW(pwcsName,
5012 accessMode,
5013 shareMode,
5014 NULL,
5015 creationMode,
5016 fileAttributes,
5019 if (hFile == INVALID_HANDLE_VALUE)
5021 return E_FAIL;
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(
5033 newStorage,
5034 hFile,
5035 grfMode);
5037 if (FAILED(hr))
5038 return hr;
5041 * Get an "out" pointer for the caller.
5043 hr = StorageBaseImpl_QueryInterface(
5044 (IStorage*)newStorage,
5045 (REFIID)&IID_IStorage,
5046 (void**)ppstgOpen);
5048 return hr;
5051 /******************************************************************************
5052 * StgOpenStorage32 [OLE32.148]
5054 HRESULT WINAPI StgOpenStorage(
5055 const OLECHAR *pwcsName,
5056 IStorage *pstgPriority,
5057 DWORD grfMode,
5058 SNB snbExclude,
5059 DWORD reserved,
5060 IStorage **ppstgOpen)
5062 StorageImpl* newStorage = 0;
5063 HRESULT hr = S_OK;
5064 HANDLE hFile = 0;
5065 DWORD shareMode;
5066 DWORD accessMode;
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.
5089 *ppstgOpen = 0;
5091 hFile = CreateFileW( pwcsName,
5092 accessMode,
5093 shareMode,
5094 NULL,
5095 OPEN_EXISTING,
5096 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
5100 if (hFile==INVALID_HANDLE_VALUE)
5102 return E_FAIL;
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(
5114 newStorage,
5115 hFile,
5116 grfMode);
5118 if (FAILED(hr))
5119 return hr;
5122 * Get an "out" pointer for the caller.
5124 hr = StorageBaseImpl_QueryInterface(
5125 (IStorage*)newStorage,
5126 (REFIID)&IID_IStorage,
5127 (void**)ppstgOpen);
5129 return hr;
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)
5139 HRESULT hRes;
5141 assert(pStg != 0);
5143 hRes = IStorage_SetClass(pStg, rclsid);
5145 return hRes;
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 )
5208 return E_FAIL;
5211 * STGM_WRITE | STGM_READWRITE | STGM_READ
5213 if ( ! bSTGM_READ )
5214 if( bSTGM_WRITE && bSTGM_READWRITE )
5215 return E_FAIL;
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)
5225 return E_FAIL;
5228 * STGM_CREATE | STGM_CONVERT
5229 * if both are false, STGM_FAILIFTHERE is set to TRUE
5231 if ( bSTGM_CREATE && bSTGM_CONVERT )
5232 return E_FAIL;
5235 * STGM_NOSCRATCH requires STGM_TRANSACTED
5237 if ( bSTGM_NOSCRATCH && ! bSTGM_TRANSACTED )
5238 return E_FAIL;
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)) )
5248 return E_FAIL;
5251 return S_OK;
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))
5276 dwShareMode = 0;
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;
5284 return dwShareMode;
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);
5300 if (bSTGM_READ)
5301 dwDesiredAccess = GENERIC_READ;
5303 if (bSTGM_WRITE)
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");
5324 return CREATE_NEW;
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));
5329 return CREATE_NEW;