2 * Compound Storage (32 bit version)
3 * Storage implementation
5 * This file contains the compound file implementation
6 * of the storage interface.
8 * Copyright 1999 Francis Beaudet
9 * Copyright 1999 Sylvain St-Germain
10 * Copyright 1999 Thuy Nguyen
11 * Copyright 2005 Mike McCormack
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 * The compound file implementation of IStorage used for create
29 * and manage substorages and streams within a storage object
30 * residing in a compound file object.
40 #define NONAMELESSUNION
46 #include "wine/unicode.h"
47 #include "wine/debug.h"
49 #include "storage32.h"
50 #include "ole2.h" /* For Write/ReadClassStm */
53 #include "wine/wingdi16.h"
54 #include "compobj_private.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(storage
);
60 * These are signatures to detect the type of Document file.
62 static const BYTE STORAGE_magic
[8] ={0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1};
63 static const BYTE STORAGE_oldmagic
[8] ={0xd0,0xcf,0x11,0xe0,0x0e,0x11,0xfc,0x0d};
65 extern const IPropertySetStorageVtbl IPropertySetStorage_Vtbl
;
68 /****************************************************************************
69 * StorageInternalImpl definitions.
71 * Definition of the implementation structure for the IStorage interface.
72 * This one implements the IStorage interface for storage that are
73 * inside another storage.
75 typedef struct StorageInternalImpl
77 struct StorageBaseImpl base
;
80 * Entry in the parent's stream tracking list
82 struct list ParentListEntry
;
84 StorageBaseImpl
*parentStorage
;
85 } StorageInternalImpl
;
87 static const IStorageVtbl StorageInternalImpl_Vtbl
;
88 static StorageInternalImpl
* StorageInternalImpl_Construct(StorageBaseImpl
*,DWORD
,DirRef
);
90 typedef struct TransactedDirEntry
92 /* If applicable, a reference to the original DirEntry in the transacted
93 * parent. If this is a newly-created entry, DIRENTRY_NULL. */
94 DirRef transactedParentEntry
;
96 /* True if this entry is being used. */
99 /* True if data is up to date. */
102 /* True if this entry has been modified. */
105 /* True if this entry's stream has been modified. */
108 /* True if this entry has been deleted in the transacted storage, but the
109 * delete has not yet been committed. */
112 /* If this entry's stream has been modified, a reference to where the stream
113 * is stored in the snapshot file. */
116 /* This directory entry's data, including any changes that have been made. */
119 /* A reference to the parent of this node. This is only valid while we are
120 * committing changes. */
123 /* A reference to a newly-created entry in the transacted parent. This is
124 * always equal to transactedParentEntry except when committing changes. */
125 DirRef newTransactedParentEntry
;
126 } TransactedDirEntry
;
129 /****************************************************************************
130 * Transacted storage object.
132 typedef struct TransactedSnapshotImpl
134 struct StorageBaseImpl base
;
137 * Modified streams are temporarily saved to the scratch file.
139 StorageBaseImpl
*scratch
;
141 /* The directory structure is kept here, so that we can track how these
142 * entries relate to those in the parent storage. */
143 TransactedDirEntry
*entries
;
145 ULONG firstFreeEntry
;
148 * Changes are committed to the transacted parent.
150 StorageBaseImpl
*transactedParent
;
152 /* The transaction signature from when we last committed */
153 ULONG lastTransactionSig
;
154 } TransactedSnapshotImpl
;
156 static const IStorageVtbl TransactedSnapshotImpl_Vtbl
;
157 static HRESULT
Storage_ConstructTransacted(StorageBaseImpl
*,BOOL
,StorageBaseImpl
**);
159 typedef struct TransactedSharedImpl
161 struct StorageBaseImpl base
;
164 * Snapshot and uncommitted changes go here.
166 TransactedSnapshotImpl
*scratch
;
169 * Changes are committed to the transacted parent.
171 StorageBaseImpl
*transactedParent
;
173 /* The transaction signature from when we last committed */
174 ULONG lastTransactionSig
;
175 } TransactedSharedImpl
;
178 /****************************************************************************
179 * BlockChainStream definitions.
181 * The BlockChainStream class is a utility class that is used to create an
182 * abstraction of the big block chains in the storage file.
187 /* This represents a range of blocks that happen reside in consecutive sectors. */
193 typedef struct BlockChainBlock
199 BYTE data
[MAX_BIG_BLOCK_SIZE
];
202 struct BlockChainStream
204 StorageImpl
* parentStorage
;
205 ULONG
* headOfStreamPlaceHolder
;
206 DirRef ownerDirEntry
;
207 struct BlockChainRun
* indexCache
;
209 ULONG indexCacheSize
;
210 BlockChainBlock cachedBlocks
[2];
216 /* Returns the number of blocks that comprises this chain.
217 * This is not the size of the stream as the last block may not be full!
219 static inline ULONG
BlockChainStream_GetCount(BlockChainStream
* This
)
221 return This
->numBlocks
;
224 static BlockChainStream
* BlockChainStream_Construct(StorageImpl
*,ULONG
*,DirRef
);
225 static void BlockChainStream_Destroy(BlockChainStream
*);
226 static HRESULT
BlockChainStream_ReadAt(BlockChainStream
*,ULARGE_INTEGER
,ULONG
,void*,ULONG
*);
227 static HRESULT
BlockChainStream_WriteAt(BlockChainStream
*,ULARGE_INTEGER
,ULONG
,const void*,ULONG
*);
228 static HRESULT
BlockChainStream_Flush(BlockChainStream
*);
229 static ULARGE_INTEGER
BlockChainStream_GetSize(BlockChainStream
*);
230 static BOOL
BlockChainStream_SetSize(BlockChainStream
*,ULARGE_INTEGER
);
233 /****************************************************************************
234 * SmallBlockChainStream definitions.
236 * The SmallBlockChainStream class is a utility class that is used to create an
237 * abstraction of the small block chains in the storage file.
240 struct SmallBlockChainStream
242 StorageImpl
* parentStorage
;
243 DirRef ownerDirEntry
;
244 ULONG
* headOfStreamPlaceHolder
;
247 static SmallBlockChainStream
* SmallBlockChainStream_Construct(StorageImpl
*,ULONG
*,DirRef
);
248 static void SmallBlockChainStream_Destroy(SmallBlockChainStream
*);
249 static HRESULT
SmallBlockChainStream_ReadAt(SmallBlockChainStream
*,ULARGE_INTEGER
,ULONG
,void*,ULONG
*);
250 static HRESULT
SmallBlockChainStream_WriteAt(SmallBlockChainStream
*,ULARGE_INTEGER
,ULONG
,const void*,ULONG
*);
251 static ULARGE_INTEGER
SmallBlockChainStream_GetSize(SmallBlockChainStream
*);
252 static BOOL
SmallBlockChainStream_SetSize(SmallBlockChainStream
*,ULARGE_INTEGER
);
255 /************************************************************************
257 ***********************************************************************/
259 /************************************************************************
260 * This method validates an STGM parameter that can contain the values below
262 * The stgm modes in 0x0000ffff are not bit masks, but distinct 4 bit values.
263 * The stgm values contained in 0xffff0000 are bitmasks.
265 * STGM_DIRECT 0x00000000
266 * STGM_TRANSACTED 0x00010000
267 * STGM_SIMPLE 0x08000000
269 * STGM_READ 0x00000000
270 * STGM_WRITE 0x00000001
271 * STGM_READWRITE 0x00000002
273 * STGM_SHARE_DENY_NONE 0x00000040
274 * STGM_SHARE_DENY_READ 0x00000030
275 * STGM_SHARE_DENY_WRITE 0x00000020
276 * STGM_SHARE_EXCLUSIVE 0x00000010
278 * STGM_PRIORITY 0x00040000
279 * STGM_DELETEONRELEASE 0x04000000
281 * STGM_CREATE 0x00001000
282 * STGM_CONVERT 0x00020000
283 * STGM_FAILIFTHERE 0x00000000
285 * STGM_NOSCRATCH 0x00100000
286 * STGM_NOSNAPSHOT 0x00200000
288 static HRESULT
validateSTGM(DWORD stgm
)
290 DWORD access
= STGM_ACCESS_MODE(stgm
);
291 DWORD share
= STGM_SHARE_MODE(stgm
);
292 DWORD create
= STGM_CREATE_MODE(stgm
);
294 if (stgm
&~STGM_KNOWN_FLAGS
)
296 ERR("unknown flags %08x\n", stgm
);
312 case STGM_SHARE_DENY_NONE
:
313 case STGM_SHARE_DENY_READ
:
314 case STGM_SHARE_DENY_WRITE
:
315 case STGM_SHARE_EXCLUSIVE
:
318 if (!(stgm
& STGM_TRANSACTED
))
328 case STGM_FAILIFTHERE
:
335 * STGM_DIRECT | STGM_TRANSACTED | STGM_SIMPLE
337 if ( (stgm
& STGM_TRANSACTED
) && (stgm
& STGM_SIMPLE
) )
341 * STGM_CREATE | STGM_CONVERT
342 * if both are false, STGM_FAILIFTHERE is set to TRUE
344 if ( create
== STGM_CREATE
&& (stgm
& STGM_CONVERT
) )
348 * STGM_NOSCRATCH requires STGM_TRANSACTED
350 if ( (stgm
& STGM_NOSCRATCH
) && !(stgm
& STGM_TRANSACTED
) )
354 * STGM_NOSNAPSHOT requires STGM_TRANSACTED and
355 * not STGM_SHARE_EXCLUSIVE or STGM_SHARE_DENY_WRITE`
357 if ( (stgm
& STGM_NOSNAPSHOT
) &&
358 (!(stgm
& STGM_TRANSACTED
) ||
359 share
== STGM_SHARE_EXCLUSIVE
||
360 share
== STGM_SHARE_DENY_WRITE
) )
366 /************************************************************************
367 * GetShareModeFromSTGM
369 * This method will return a share mode flag from a STGM value.
370 * The STGM value is assumed valid.
372 static DWORD
GetShareModeFromSTGM(DWORD stgm
)
374 switch (STGM_SHARE_MODE(stgm
))
377 assert(stgm
& STGM_TRANSACTED
);
379 case STGM_SHARE_DENY_NONE
:
380 return FILE_SHARE_READ
| FILE_SHARE_WRITE
;
381 case STGM_SHARE_DENY_READ
:
382 return FILE_SHARE_WRITE
;
383 case STGM_SHARE_DENY_WRITE
:
384 case STGM_SHARE_EXCLUSIVE
:
385 return FILE_SHARE_READ
;
387 ERR("Invalid share mode!\n");
392 /************************************************************************
393 * GetAccessModeFromSTGM
395 * This method will return an access mode flag from a STGM value.
396 * The STGM value is assumed valid.
398 static DWORD
GetAccessModeFromSTGM(DWORD stgm
)
400 switch (STGM_ACCESS_MODE(stgm
))
406 return GENERIC_READ
| GENERIC_WRITE
;
408 ERR("Invalid access mode!\n");
413 /************************************************************************
414 * GetCreationModeFromSTGM
416 * This method will return a creation mode flag from a STGM value.
417 * The STGM value is assumed valid.
419 static DWORD
GetCreationModeFromSTGM(DWORD stgm
)
421 switch(STGM_CREATE_MODE(stgm
))
424 return CREATE_ALWAYS
;
426 FIXME("STGM_CONVERT not implemented!\n");
428 case STGM_FAILIFTHERE
:
431 ERR("Invalid create mode!\n");
437 /************************************************************************
438 * IDirectWriterLock implementation
439 ***********************************************************************/
441 static inline StorageBaseImpl
*impl_from_IDirectWriterLock( IDirectWriterLock
*iface
)
443 return CONTAINING_RECORD(iface
, StorageBaseImpl
, IDirectWriterLock_iface
);
446 static HRESULT WINAPI
directwriterlock_QueryInterface(IDirectWriterLock
*iface
, REFIID riid
, void **obj
)
448 StorageBaseImpl
*This
= impl_from_IDirectWriterLock(iface
);
449 return IStorage_QueryInterface(&This
->IStorage_iface
, riid
, obj
);
452 static ULONG WINAPI
directwriterlock_AddRef(IDirectWriterLock
*iface
)
454 StorageBaseImpl
*This
= impl_from_IDirectWriterLock(iface
);
455 return IStorage_AddRef(&This
->IStorage_iface
);
458 static ULONG WINAPI
directwriterlock_Release(IDirectWriterLock
*iface
)
460 StorageBaseImpl
*This
= impl_from_IDirectWriterLock(iface
);
461 return IStorage_Release(&This
->IStorage_iface
);
464 static HRESULT WINAPI
directwriterlock_WaitForWriteAccess(IDirectWriterLock
*iface
, DWORD timeout
)
466 StorageBaseImpl
*This
= impl_from_IDirectWriterLock(iface
);
467 FIXME("(%p)->(%d): stub\n", This
, timeout
);
471 static HRESULT WINAPI
directwriterlock_ReleaseWriteAccess(IDirectWriterLock
*iface
)
473 StorageBaseImpl
*This
= impl_from_IDirectWriterLock(iface
);
474 FIXME("(%p): stub\n", This
);
478 static HRESULT WINAPI
directwriterlock_HaveWriteAccess(IDirectWriterLock
*iface
)
480 StorageBaseImpl
*This
= impl_from_IDirectWriterLock(iface
);
481 FIXME("(%p): stub\n", This
);
485 static const IDirectWriterLockVtbl DirectWriterLockVtbl
=
487 directwriterlock_QueryInterface
,
488 directwriterlock_AddRef
,
489 directwriterlock_Release
,
490 directwriterlock_WaitForWriteAccess
,
491 directwriterlock_ReleaseWriteAccess
,
492 directwriterlock_HaveWriteAccess
496 /************************************************************************
497 * StorageBaseImpl implementation : Tree helper functions
498 ***********************************************************************/
500 /****************************************************************************
504 * Case insensitive comparison of DirEntry.name by first considering
507 * Returns <0 when name1 < name2
508 * >0 when name1 > name2
509 * 0 when name1 == name2
511 static LONG
entryNameCmp(
512 const OLECHAR
*name1
,
513 const OLECHAR
*name2
)
515 LONG diff
= lstrlenW(name1
) - lstrlenW(name2
);
517 while (diff
== 0 && *name1
!= 0)
520 * We compare the string themselves only when they are of the same length
522 diff
= toupperW(*name1
++) - toupperW(*name2
++);
528 /****************************************************************************
532 * Find and read the element of a storage with the given name.
534 static DirRef
findElement(StorageBaseImpl
*storage
, DirRef storageEntry
,
535 const OLECHAR
*name
, DirEntry
*data
)
539 /* Read the storage entry to find the root of the tree. */
540 StorageBaseImpl_ReadDirEntry(storage
, storageEntry
, data
);
542 currentEntry
= data
->dirRootEntry
;
544 while (currentEntry
!= DIRENTRY_NULL
)
548 StorageBaseImpl_ReadDirEntry(storage
, currentEntry
, data
);
550 cmp
= entryNameCmp(name
, data
->name
);
557 currentEntry
= data
->leftChild
;
560 currentEntry
= data
->rightChild
;
566 /****************************************************************************
570 * Find and read the binary tree parent of the element with the given name.
572 * If there is no such element, find a place where it could be inserted and
573 * return STG_E_FILENOTFOUND.
575 static HRESULT
findTreeParent(StorageBaseImpl
*storage
, DirRef storageEntry
,
576 const OLECHAR
*childName
, DirEntry
*parentData
, DirRef
*parentEntry
,
582 /* Read the storage entry to find the root of the tree. */
583 StorageBaseImpl_ReadDirEntry(storage
, storageEntry
, parentData
);
585 *parentEntry
= storageEntry
;
586 *relation
= DIRENTRY_RELATION_DIR
;
588 childEntry
= parentData
->dirRootEntry
;
590 while (childEntry
!= DIRENTRY_NULL
)
594 StorageBaseImpl_ReadDirEntry(storage
, childEntry
, &childData
);
596 cmp
= entryNameCmp(childName
, childData
.name
);
604 *parentData
= childData
;
605 *parentEntry
= childEntry
;
606 *relation
= DIRENTRY_RELATION_PREVIOUS
;
608 childEntry
= parentData
->leftChild
;
613 *parentData
= childData
;
614 *parentEntry
= childEntry
;
615 *relation
= DIRENTRY_RELATION_NEXT
;
617 childEntry
= parentData
->rightChild
;
621 if (childEntry
== DIRENTRY_NULL
)
622 return STG_E_FILENOTFOUND
;
627 static void setEntryLink(DirEntry
*entry
, ULONG relation
, DirRef new_target
)
631 case DIRENTRY_RELATION_PREVIOUS
:
632 entry
->leftChild
= new_target
;
634 case DIRENTRY_RELATION_NEXT
:
635 entry
->rightChild
= new_target
;
637 case DIRENTRY_RELATION_DIR
:
638 entry
->dirRootEntry
= new_target
;
645 /****************************************************************************
649 * Add a directory entry to a storage
651 static HRESULT
insertIntoTree(
652 StorageBaseImpl
*This
,
653 DirRef parentStorageIndex
,
654 DirRef newEntryIndex
)
656 DirEntry currentEntry
;
660 * Read the inserted entry
662 StorageBaseImpl_ReadDirEntry(This
,
667 * Read the storage entry
669 StorageBaseImpl_ReadDirEntry(This
,
673 if (currentEntry
.dirRootEntry
!= DIRENTRY_NULL
)
676 * The root storage contains some element, therefore, start the research
677 * for the appropriate location.
680 DirRef current
, next
, previous
, currentEntryId
;
683 * Keep a reference to the root of the storage's element tree
685 currentEntryId
= currentEntry
.dirRootEntry
;
690 StorageBaseImpl_ReadDirEntry(This
,
691 currentEntry
.dirRootEntry
,
694 previous
= currentEntry
.leftChild
;
695 next
= currentEntry
.rightChild
;
696 current
= currentEntryId
;
700 LONG diff
= entryNameCmp( newEntry
.name
, currentEntry
.name
);
704 if (previous
!= DIRENTRY_NULL
)
706 StorageBaseImpl_ReadDirEntry(This
,
713 currentEntry
.leftChild
= newEntryIndex
;
714 StorageBaseImpl_WriteDirEntry(This
,
722 if (next
!= DIRENTRY_NULL
)
724 StorageBaseImpl_ReadDirEntry(This
,
731 currentEntry
.rightChild
= newEntryIndex
;
732 StorageBaseImpl_WriteDirEntry(This
,
741 * Trying to insert an item with the same name in the
744 return STG_E_FILEALREADYEXISTS
;
747 previous
= currentEntry
.leftChild
;
748 next
= currentEntry
.rightChild
;
754 * The storage is empty, make the new entry the root of its element tree
756 currentEntry
.dirRootEntry
= newEntryIndex
;
757 StorageBaseImpl_WriteDirEntry(This
,
765 /*************************************************************************
769 * This method removes a directory entry from its parent storage tree without
770 * freeing any resources attached to it.
772 static HRESULT
removeFromTree(
773 StorageBaseImpl
*This
,
774 DirRef parentStorageIndex
,
777 DirEntry entryToDelete
;
778 DirEntry parentEntry
;
779 DirRef parentEntryRef
;
780 ULONG typeOfRelation
;
783 hr
= StorageBaseImpl_ReadDirEntry(This
, deletedIndex
, &entryToDelete
);
789 * Find the element that links to the one we want to delete.
791 hr
= findTreeParent(This
, parentStorageIndex
, entryToDelete
.name
,
792 &parentEntry
, &parentEntryRef
, &typeOfRelation
);
797 if (entryToDelete
.leftChild
!= DIRENTRY_NULL
)
800 * Replace the deleted entry with its left child
802 setEntryLink(&parentEntry
, typeOfRelation
, entryToDelete
.leftChild
);
804 hr
= StorageBaseImpl_WriteDirEntry(
813 if (entryToDelete
.rightChild
!= DIRENTRY_NULL
)
816 * We need to reinsert the right child somewhere. We already know it and
817 * its children are greater than everything in the left tree, so we
818 * insert it at the rightmost point in the left tree.
820 DirRef newRightChildParent
= entryToDelete
.leftChild
;
821 DirEntry newRightChildParentEntry
;
825 hr
= StorageBaseImpl_ReadDirEntry(
828 &newRightChildParentEntry
);
834 if (newRightChildParentEntry
.rightChild
!= DIRENTRY_NULL
)
835 newRightChildParent
= newRightChildParentEntry
.rightChild
;
836 } while (newRightChildParentEntry
.rightChild
!= DIRENTRY_NULL
);
838 newRightChildParentEntry
.rightChild
= entryToDelete
.rightChild
;
840 hr
= StorageBaseImpl_WriteDirEntry(
843 &newRightChildParentEntry
);
853 * Replace the deleted entry with its right child
855 setEntryLink(&parentEntry
, typeOfRelation
, entryToDelete
.rightChild
);
857 hr
= StorageBaseImpl_WriteDirEntry(
871 /************************************************************************
872 * IEnumSTATSTGImpl implementation for StorageBaseImpl_EnumElements
873 ***********************************************************************/
876 * IEnumSTATSTGImpl definitions.
878 * Definition of the implementation structure for the IEnumSTATSTGImpl interface.
879 * This class allows iterating through the content of a storage and finding
880 * specific items inside it.
882 struct IEnumSTATSTGImpl
884 IEnumSTATSTG IEnumSTATSTG_iface
;
886 LONG ref
; /* Reference count */
887 StorageBaseImpl
* parentStorage
; /* Reference to the parent storage */
888 DirRef storageDirEntry
; /* Directory entry of the storage to enumerate */
890 WCHAR name
[DIRENTRY_NAME_MAX_LEN
]; /* The most recent name visited */
893 static inline IEnumSTATSTGImpl
*impl_from_IEnumSTATSTG(IEnumSTATSTG
*iface
)
895 return CONTAINING_RECORD(iface
, IEnumSTATSTGImpl
, IEnumSTATSTG_iface
);
898 static void IEnumSTATSTGImpl_Destroy(IEnumSTATSTGImpl
* This
)
900 IStorage_Release(&This
->parentStorage
->IStorage_iface
);
901 HeapFree(GetProcessHeap(), 0, This
);
904 static HRESULT WINAPI
IEnumSTATSTGImpl_QueryInterface(
909 IEnumSTATSTGImpl
* const This
= impl_from_IEnumSTATSTG(iface
);
911 TRACE("%p,%s,%p\n", iface
, debugstr_guid(riid
), ppvObject
);
918 if (IsEqualGUID(&IID_IUnknown
, riid
) ||
919 IsEqualGUID(&IID_IEnumSTATSTG
, riid
))
921 *ppvObject
= &This
->IEnumSTATSTG_iface
;
922 IEnumSTATSTG_AddRef(&This
->IEnumSTATSTG_iface
);
923 TRACE("<-- %p\n", *ppvObject
);
927 TRACE("<-- E_NOINTERFACE\n");
928 return E_NOINTERFACE
;
931 static ULONG WINAPI
IEnumSTATSTGImpl_AddRef(
934 IEnumSTATSTGImpl
* const This
= impl_from_IEnumSTATSTG(iface
);
935 return InterlockedIncrement(&This
->ref
);
938 static ULONG WINAPI
IEnumSTATSTGImpl_Release(
941 IEnumSTATSTGImpl
* const This
= impl_from_IEnumSTATSTG(iface
);
945 newRef
= InterlockedDecrement(&This
->ref
);
949 IEnumSTATSTGImpl_Destroy(This
);
955 static HRESULT
IEnumSTATSTGImpl_GetNextRef(
956 IEnumSTATSTGImpl
* This
,
959 DirRef result
= DIRENTRY_NULL
;
963 WCHAR result_name
[DIRENTRY_NAME_MAX_LEN
];
965 TRACE("%p,%p\n", This
, ref
);
967 hr
= StorageBaseImpl_ReadDirEntry(This
->parentStorage
,
968 This
->parentStorage
->storageDirEntry
, &entry
);
969 searchNode
= entry
.dirRootEntry
;
971 while (SUCCEEDED(hr
) && searchNode
!= DIRENTRY_NULL
)
973 hr
= StorageBaseImpl_ReadDirEntry(This
->parentStorage
, searchNode
, &entry
);
977 LONG diff
= entryNameCmp( entry
.name
, This
->name
);
981 searchNode
= entry
.rightChild
;
986 memcpy(result_name
, entry
.name
, sizeof(result_name
));
987 searchNode
= entry
.leftChild
;
995 if (result
!= DIRENTRY_NULL
)
996 memcpy(This
->name
, result_name
, sizeof(result_name
));
999 TRACE("<-- %08x\n", hr
);
1003 static HRESULT WINAPI
IEnumSTATSTGImpl_Next(
1004 IEnumSTATSTG
* iface
,
1007 ULONG
* pceltFetched
)
1009 IEnumSTATSTGImpl
* const This
= impl_from_IEnumSTATSTG(iface
);
1011 DirEntry currentEntry
;
1012 STATSTG
* currentReturnStruct
= rgelt
;
1013 ULONG objectFetched
= 0;
1014 DirRef currentSearchNode
;
1017 TRACE("%p,%u,%p,%p\n", iface
, celt
, rgelt
, pceltFetched
);
1019 if ( (rgelt
==0) || ( (celt
!=1) && (pceltFetched
==0) ) )
1020 return E_INVALIDARG
;
1022 if (This
->parentStorage
->reverted
)
1024 TRACE("<-- STG_E_REVERTED\n");
1025 return STG_E_REVERTED
;
1029 * To avoid the special case, get another pointer to a ULONG value if
1030 * the caller didn't supply one.
1032 if (pceltFetched
==0)
1033 pceltFetched
= &objectFetched
;
1036 * Start the iteration, we will iterate until we hit the end of the
1037 * linked list or until we hit the number of items to iterate through
1041 while ( *pceltFetched
< celt
)
1043 hr
= IEnumSTATSTGImpl_GetNextRef(This
, ¤tSearchNode
);
1045 if (FAILED(hr
) || currentSearchNode
== DIRENTRY_NULL
)
1047 memset(currentReturnStruct
, 0, sizeof(*currentReturnStruct
));
1052 * Read the entry from the storage.
1054 hr
= StorageBaseImpl_ReadDirEntry(This
->parentStorage
,
1057 if (FAILED(hr
)) break;
1060 * Copy the information to the return buffer.
1062 StorageUtl_CopyDirEntryToSTATSTG(This
->parentStorage
,
1063 currentReturnStruct
,
1068 * Step to the next item in the iteration
1071 currentReturnStruct
++;
1074 if (SUCCEEDED(hr
) && *pceltFetched
!= celt
)
1077 TRACE("<-- %08x (asked %u, got %u)\n", hr
, celt
, *pceltFetched
);
1082 static HRESULT WINAPI
IEnumSTATSTGImpl_Skip(
1083 IEnumSTATSTG
* iface
,
1086 IEnumSTATSTGImpl
* const This
= impl_from_IEnumSTATSTG(iface
);
1088 ULONG objectFetched
= 0;
1089 DirRef currentSearchNode
;
1092 TRACE("%p,%u\n", iface
, celt
);
1094 if (This
->parentStorage
->reverted
)
1096 TRACE("<-- STG_E_REVERTED\n");
1097 return STG_E_REVERTED
;
1100 while ( (objectFetched
< celt
) )
1102 hr
= IEnumSTATSTGImpl_GetNextRef(This
, ¤tSearchNode
);
1104 if (FAILED(hr
) || currentSearchNode
== DIRENTRY_NULL
)
1110 if (SUCCEEDED(hr
) && objectFetched
!= celt
)
1113 TRACE("<-- %08x\n", hr
);
1117 static HRESULT WINAPI
IEnumSTATSTGImpl_Reset(
1118 IEnumSTATSTG
* iface
)
1120 IEnumSTATSTGImpl
* const This
= impl_from_IEnumSTATSTG(iface
);
1122 TRACE("%p\n", iface
);
1124 if (This
->parentStorage
->reverted
)
1126 TRACE("<-- STG_E_REVERTED\n");
1127 return STG_E_REVERTED
;
1135 static IEnumSTATSTGImpl
* IEnumSTATSTGImpl_Construct(StorageBaseImpl
*,DirRef
);
1137 static HRESULT WINAPI
IEnumSTATSTGImpl_Clone(
1138 IEnumSTATSTG
* iface
,
1139 IEnumSTATSTG
** ppenum
)
1141 IEnumSTATSTGImpl
* const This
= impl_from_IEnumSTATSTG(iface
);
1142 IEnumSTATSTGImpl
* newClone
;
1144 TRACE("%p,%p\n", iface
, ppenum
);
1146 if (This
->parentStorage
->reverted
)
1148 TRACE("<-- STG_E_REVERTED\n");
1149 return STG_E_REVERTED
;
1153 return E_INVALIDARG
;
1155 newClone
= IEnumSTATSTGImpl_Construct(This
->parentStorage
,
1156 This
->storageDirEntry
);
1160 return E_OUTOFMEMORY
;
1164 * The new clone enumeration must point to the same current node as
1167 memcpy(newClone
->name
, This
->name
, sizeof(newClone
->name
));
1169 *ppenum
= &newClone
->IEnumSTATSTG_iface
;
1175 * Virtual function table for the IEnumSTATSTGImpl class.
1177 static const IEnumSTATSTGVtbl IEnumSTATSTGImpl_Vtbl
=
1179 IEnumSTATSTGImpl_QueryInterface
,
1180 IEnumSTATSTGImpl_AddRef
,
1181 IEnumSTATSTGImpl_Release
,
1182 IEnumSTATSTGImpl_Next
,
1183 IEnumSTATSTGImpl_Skip
,
1184 IEnumSTATSTGImpl_Reset
,
1185 IEnumSTATSTGImpl_Clone
1188 static IEnumSTATSTGImpl
* IEnumSTATSTGImpl_Construct(
1189 StorageBaseImpl
* parentStorage
,
1190 DirRef storageDirEntry
)
1192 IEnumSTATSTGImpl
* newEnumeration
;
1194 newEnumeration
= HeapAlloc(GetProcessHeap(), 0, sizeof(IEnumSTATSTGImpl
));
1198 newEnumeration
->IEnumSTATSTG_iface
.lpVtbl
= &IEnumSTATSTGImpl_Vtbl
;
1199 newEnumeration
->ref
= 1;
1200 newEnumeration
->name
[0] = 0;
1203 * We want to nail-down the reference to the storage in case the
1204 * enumeration out-lives the storage in the client application.
1206 newEnumeration
->parentStorage
= parentStorage
;
1207 IStorage_AddRef(&newEnumeration
->parentStorage
->IStorage_iface
);
1209 newEnumeration
->storageDirEntry
= storageDirEntry
;
1212 return newEnumeration
;
1216 /************************************************************************
1217 * StorageBaseImpl implementation
1218 ***********************************************************************/
1220 static inline StorageBaseImpl
*impl_from_IStorage( IStorage
*iface
)
1222 return CONTAINING_RECORD(iface
, StorageBaseImpl
, IStorage_iface
);
1225 /************************************************************************
1226 * StorageBaseImpl_QueryInterface (IUnknown)
1228 * This method implements the common QueryInterface for all IStorage
1229 * implementations contained in this file.
1231 * See Windows documentation for more details on IUnknown methods.
1233 static HRESULT WINAPI
StorageBaseImpl_QueryInterface(
1238 StorageBaseImpl
*This
= impl_from_IStorage(iface
);
1240 TRACE("%p,%s,%p\n", iface
, debugstr_guid(riid
), ppvObject
);
1243 return E_INVALIDARG
;
1247 if (IsEqualGUID(&IID_IUnknown
, riid
) ||
1248 IsEqualGUID(&IID_IStorage
, riid
))
1250 *ppvObject
= &This
->IStorage_iface
;
1252 else if (IsEqualGUID(&IID_IPropertySetStorage
, riid
))
1254 *ppvObject
= &This
->IPropertySetStorage_iface
;
1256 /* locking interface is reported for writer only */
1257 else if (IsEqualGUID(&IID_IDirectWriterLock
, riid
) && This
->lockingrole
== SWMR_Writer
)
1259 *ppvObject
= &This
->IDirectWriterLock_iface
;
1263 TRACE("<-- E_NOINTERFACE\n");
1264 return E_NOINTERFACE
;
1267 IStorage_AddRef(iface
);
1268 TRACE("<-- %p\n", *ppvObject
);
1272 /************************************************************************
1273 * StorageBaseImpl_AddRef (IUnknown)
1275 * This method implements the common AddRef for all IStorage
1276 * implementations contained in this file.
1278 * See Windows documentation for more details on IUnknown methods.
1280 static ULONG WINAPI
StorageBaseImpl_AddRef(
1283 StorageBaseImpl
*This
= impl_from_IStorage(iface
);
1284 ULONG ref
= InterlockedIncrement(&This
->ref
);
1286 TRACE("(%p) AddRef to %d\n", This
, ref
);
1291 /************************************************************************
1292 * StorageBaseImpl_Release (IUnknown)
1294 * This method implements the common Release for all IStorage
1295 * implementations contained in this file.
1297 * See Windows documentation for more details on IUnknown methods.
1299 static ULONG WINAPI
StorageBaseImpl_Release(
1302 StorageBaseImpl
*This
= impl_from_IStorage(iface
);
1304 ULONG ref
= InterlockedDecrement(&This
->ref
);
1306 TRACE("(%p) ReleaseRef to %d\n", This
, ref
);
1311 * Since we are using a system of base-classes, we want to call the
1312 * destructor of the appropriate derived class. To do this, we are
1313 * using virtual functions to implement the destructor.
1315 StorageBaseImpl_Destroy(This
);
1321 static HRESULT
StorageBaseImpl_CopyStorageEntryTo(StorageBaseImpl
*This
,
1322 DirRef srcEntry
, BOOL skip_storage
, BOOL skip_stream
,
1323 SNB snbExclude
, IStorage
*pstgDest
);
1325 static HRESULT
StorageBaseImpl_CopyChildEntryTo(StorageBaseImpl
*This
,
1326 DirRef srcEntry
, BOOL skip_storage
, BOOL skip_stream
,
1327 SNB snbExclude
, IStorage
*pstgDest
)
1333 IStream
*pstrChild
, *pstrTmp
;
1336 if (srcEntry
== DIRENTRY_NULL
)
1339 hr
= StorageBaseImpl_ReadDirEntry( This
, srcEntry
, &data
);
1346 WCHAR
**snb
= snbExclude
;
1348 while ( *snb
!= NULL
&& !skip
)
1350 if ( lstrcmpW(data
.name
, *snb
) == 0 )
1358 if (data
.stgType
== STGTY_STORAGE
&& !skip_storage
)
1361 * create a new storage in destination storage
1363 hr
= IStorage_CreateStorage( pstgDest
, data
.name
,
1364 STGM_FAILIFTHERE
|STGM_WRITE
|STGM_SHARE_EXCLUSIVE
,
1369 * if it already exist, don't create a new one use this one
1371 if (hr
== STG_E_FILEALREADYEXISTS
)
1373 hr
= IStorage_OpenStorage( pstgDest
, data
.name
, NULL
,
1374 STGM_WRITE
|STGM_SHARE_EXCLUSIVE
,
1375 NULL
, 0, &pstgTmp
);
1380 hr
= StorageBaseImpl_CopyStorageEntryTo( This
, srcEntry
, skip_storage
,
1381 skip_stream
, NULL
, pstgTmp
);
1383 IStorage_Release(pstgTmp
);
1386 else if (data
.stgType
== STGTY_STREAM
&& !skip_stream
)
1389 * create a new stream in destination storage. If the stream already
1390 * exist, it will be deleted and a new one will be created.
1392 hr
= IStorage_CreateStream( pstgDest
, data
.name
,
1393 STGM_CREATE
|STGM_WRITE
|STGM_SHARE_EXCLUSIVE
,
1397 * open child stream storage. This operation must succeed even if the
1398 * stream is already open, so we use internal functions to do it.
1402 StgStreamImpl
*streamimpl
= StgStreamImpl_Construct(This
, STGM_READ
|STGM_SHARE_EXCLUSIVE
, srcEntry
);
1406 pstrChild
= &streamimpl
->IStream_iface
;
1408 IStream_AddRef(pstrChild
);
1420 * Get the size of the source stream
1422 IStream_Stat( pstrChild
, &strStat
, STATFLAG_NONAME
);
1425 * Set the size of the destination stream.
1427 IStream_SetSize(pstrTmp
, strStat
.cbSize
);
1432 hr
= IStream_CopyTo( pstrChild
, pstrTmp
, strStat
.cbSize
,
1435 IStream_Release( pstrChild
);
1438 IStream_Release( pstrTmp
);
1444 hr
= StorageBaseImpl_CopyChildEntryTo( This
, data
.leftChild
, skip_storage
,
1445 skip_stream
, snbExclude
, pstgDest
);
1448 hr
= StorageBaseImpl_CopyChildEntryTo( This
, data
.rightChild
, skip_storage
,
1449 skip_stream
, snbExclude
, pstgDest
);
1451 TRACE("<-- %08x\n", hr
);
1455 static BOOL
StorageBaseImpl_IsStreamOpen(StorageBaseImpl
* stg
, DirRef streamEntry
)
1457 StgStreamImpl
*strm
;
1459 TRACE("%p,%d\n", stg
, streamEntry
);
1461 LIST_FOR_EACH_ENTRY(strm
, &stg
->strmHead
, StgStreamImpl
, StrmListEntry
)
1463 if (strm
->dirEntry
== streamEntry
)
1472 static BOOL
StorageBaseImpl_IsStorageOpen(StorageBaseImpl
* stg
, DirRef storageEntry
)
1474 StorageInternalImpl
*childstg
;
1476 TRACE("%p,%d\n", stg
, storageEntry
);
1478 LIST_FOR_EACH_ENTRY(childstg
, &stg
->storageHead
, StorageInternalImpl
, ParentListEntry
)
1480 if (childstg
->base
.storageDirEntry
== storageEntry
)
1489 /************************************************************************
1490 * StorageBaseImpl_OpenStream (IStorage)
1492 * This method will open the specified stream object from the current storage.
1494 * See Windows documentation for more details on IStorage methods.
1496 static HRESULT WINAPI
StorageBaseImpl_OpenStream(
1498 const OLECHAR
* pwcsName
, /* [string][in] */
1499 void* reserved1
, /* [unique][in] */
1500 DWORD grfMode
, /* [in] */
1501 DWORD reserved2
, /* [in] */
1502 IStream
** ppstm
) /* [out] */
1504 StorageBaseImpl
*This
= impl_from_IStorage(iface
);
1505 StgStreamImpl
* newStream
;
1506 DirEntry currentEntry
;
1507 DirRef streamEntryRef
;
1508 HRESULT res
= STG_E_UNKNOWN
;
1510 TRACE("(%p, %s, %p, %x, %d, %p)\n",
1511 iface
, debugstr_w(pwcsName
), reserved1
, grfMode
, reserved2
, ppstm
);
1513 if ( (pwcsName
==NULL
) || (ppstm
==0) )
1521 if ( FAILED( validateSTGM(grfMode
) ) ||
1522 STGM_SHARE_MODE(grfMode
) != STGM_SHARE_EXCLUSIVE
)
1524 res
= STG_E_INVALIDFLAG
;
1531 if ( (grfMode
& STGM_DELETEONRELEASE
) || (grfMode
& STGM_TRANSACTED
) )
1533 res
= STG_E_INVALIDFUNCTION
;
1539 res
= STG_E_REVERTED
;
1544 * Check that we're compatible with the parent's storage mode, but
1545 * only if we are not in transacted mode
1547 if(!(This
->openFlags
& STGM_TRANSACTED
)) {
1548 if ( STGM_ACCESS_MODE( grfMode
) > STGM_ACCESS_MODE( This
->openFlags
) )
1550 res
= STG_E_INVALIDFLAG
;
1556 * Search for the element with the given name
1558 streamEntryRef
= findElement(
1560 This
->storageDirEntry
,
1565 * If it was found, construct the stream object and return a pointer to it.
1567 if ( (streamEntryRef
!=DIRENTRY_NULL
) &&
1568 (currentEntry
.stgType
==STGTY_STREAM
) )
1570 if (StorageBaseImpl_IsStreamOpen(This
, streamEntryRef
))
1572 /* A single stream cannot be opened a second time. */
1573 res
= STG_E_ACCESSDENIED
;
1577 newStream
= StgStreamImpl_Construct(This
, grfMode
, streamEntryRef
);
1581 newStream
->grfMode
= grfMode
;
1582 *ppstm
= &newStream
->IStream_iface
;
1584 IStream_AddRef(*ppstm
);
1590 res
= E_OUTOFMEMORY
;
1594 res
= STG_E_FILENOTFOUND
;
1598 TRACE("<-- IStream %p\n", *ppstm
);
1599 TRACE("<-- %08x\n", res
);
1603 /************************************************************************
1604 * StorageBaseImpl_OpenStorage (IStorage)
1606 * This method will open a new storage object from the current storage.
1608 * See Windows documentation for more details on IStorage methods.
1610 static HRESULT WINAPI
StorageBaseImpl_OpenStorage(
1612 const OLECHAR
* pwcsName
, /* [string][unique][in] */
1613 IStorage
* pstgPriority
, /* [unique][in] */
1614 DWORD grfMode
, /* [in] */
1615 SNB snbExclude
, /* [unique][in] */
1616 DWORD reserved
, /* [in] */
1617 IStorage
** ppstg
) /* [out] */
1619 StorageBaseImpl
*This
= impl_from_IStorage(iface
);
1620 StorageInternalImpl
* newStorage
;
1621 StorageBaseImpl
* newTransactedStorage
;
1622 DirEntry currentEntry
;
1623 DirRef storageEntryRef
;
1624 HRESULT res
= STG_E_UNKNOWN
;
1626 TRACE("(%p, %s, %p, %x, %p, %d, %p)\n",
1627 iface
, debugstr_w(pwcsName
), pstgPriority
,
1628 grfMode
, snbExclude
, reserved
, ppstg
);
1630 if ((pwcsName
==NULL
) || (ppstg
==0) )
1636 if (This
->openFlags
& STGM_SIMPLE
)
1638 res
= STG_E_INVALIDFUNCTION
;
1643 if (snbExclude
!= NULL
)
1645 res
= STG_E_INVALIDPARAMETER
;
1649 if ( FAILED( validateSTGM(grfMode
) ))
1651 res
= STG_E_INVALIDFLAG
;
1658 if ( STGM_SHARE_MODE(grfMode
) != STGM_SHARE_EXCLUSIVE
||
1659 (grfMode
& STGM_DELETEONRELEASE
) ||
1660 (grfMode
& STGM_PRIORITY
) )
1662 res
= STG_E_INVALIDFUNCTION
;
1667 return STG_E_REVERTED
;
1670 * Check that we're compatible with the parent's storage mode,
1671 * but only if we are not transacted
1673 if(!(This
->openFlags
& STGM_TRANSACTED
)) {
1674 if ( STGM_ACCESS_MODE( grfMode
) > STGM_ACCESS_MODE( This
->openFlags
) )
1676 res
= STG_E_ACCESSDENIED
;
1683 storageEntryRef
= findElement(
1685 This
->storageDirEntry
,
1689 if ( (storageEntryRef
!=DIRENTRY_NULL
) &&
1690 (currentEntry
.stgType
==STGTY_STORAGE
) )
1692 if (StorageBaseImpl_IsStorageOpen(This
, storageEntryRef
))
1694 /* A single storage cannot be opened a second time. */
1695 res
= STG_E_ACCESSDENIED
;
1699 newStorage
= StorageInternalImpl_Construct(
1704 if (newStorage
!= 0)
1706 if (grfMode
& STGM_TRANSACTED
)
1708 res
= Storage_ConstructTransacted(&newStorage
->base
, FALSE
, &newTransactedStorage
);
1712 HeapFree(GetProcessHeap(), 0, newStorage
);
1716 *ppstg
= &newTransactedStorage
->IStorage_iface
;
1720 *ppstg
= &newStorage
->base
.IStorage_iface
;
1723 list_add_tail(&This
->storageHead
, &newStorage
->ParentListEntry
);
1729 res
= STG_E_INSUFFICIENTMEMORY
;
1733 res
= STG_E_FILENOTFOUND
;
1736 TRACE("<-- %08x\n", res
);
1740 /************************************************************************
1741 * StorageBaseImpl_EnumElements (IStorage)
1743 * This method will create an enumerator object that can be used to
1744 * retrieve information about all the elements in the storage object.
1746 * See Windows documentation for more details on IStorage methods.
1748 static HRESULT WINAPI
StorageBaseImpl_EnumElements(
1750 DWORD reserved1
, /* [in] */
1751 void* reserved2
, /* [size_is][unique][in] */
1752 DWORD reserved3
, /* [in] */
1753 IEnumSTATSTG
** ppenum
) /* [out] */
1755 StorageBaseImpl
*This
= impl_from_IStorage(iface
);
1756 IEnumSTATSTGImpl
* newEnum
;
1758 TRACE("(%p, %d, %p, %d, %p)\n",
1759 iface
, reserved1
, reserved2
, reserved3
, ppenum
);
1762 return E_INVALIDARG
;
1765 return STG_E_REVERTED
;
1767 newEnum
= IEnumSTATSTGImpl_Construct(
1769 This
->storageDirEntry
);
1773 *ppenum
= &newEnum
->IEnumSTATSTG_iface
;
1777 return E_OUTOFMEMORY
;
1780 /************************************************************************
1781 * StorageBaseImpl_Stat (IStorage)
1783 * This method will retrieve information about this storage object.
1785 * See Windows documentation for more details on IStorage methods.
1787 static HRESULT WINAPI
StorageBaseImpl_Stat(
1789 STATSTG
* pstatstg
, /* [out] */
1790 DWORD grfStatFlag
) /* [in] */
1792 StorageBaseImpl
*This
= impl_from_IStorage(iface
);
1793 DirEntry currentEntry
;
1794 HRESULT res
= STG_E_UNKNOWN
;
1796 TRACE("(%p, %p, %x)\n",
1797 iface
, pstatstg
, grfStatFlag
);
1807 res
= STG_E_REVERTED
;
1811 res
= StorageBaseImpl_ReadDirEntry(
1813 This
->storageDirEntry
,
1818 StorageUtl_CopyDirEntryToSTATSTG(
1824 pstatstg
->grfMode
= This
->openFlags
;
1825 pstatstg
->grfStateBits
= This
->stateBits
;
1831 TRACE("<-- STATSTG: pwcsName: %s, type: %d, cbSize.Low/High: %d/%d, grfMode: %08x, grfLocksSupported: %d, grfStateBits: %08x\n", debugstr_w(pstatstg
->pwcsName
), pstatstg
->type
, pstatstg
->cbSize
.u
.LowPart
, pstatstg
->cbSize
.u
.HighPart
, pstatstg
->grfMode
, pstatstg
->grfLocksSupported
, pstatstg
->grfStateBits
);
1833 TRACE("<-- %08x\n", res
);
1837 /************************************************************************
1838 * StorageBaseImpl_RenameElement (IStorage)
1840 * This method will rename the specified element.
1842 * See Windows documentation for more details on IStorage methods.
1844 static HRESULT WINAPI
StorageBaseImpl_RenameElement(
1846 const OLECHAR
* pwcsOldName
, /* [in] */
1847 const OLECHAR
* pwcsNewName
) /* [in] */
1849 StorageBaseImpl
*This
= impl_from_IStorage(iface
);
1850 DirEntry currentEntry
;
1851 DirRef currentEntryRef
;
1853 TRACE("(%p, %s, %s)\n",
1854 iface
, debugstr_w(pwcsOldName
), debugstr_w(pwcsNewName
));
1857 return STG_E_REVERTED
;
1859 currentEntryRef
= findElement(This
,
1860 This
->storageDirEntry
,
1864 if (currentEntryRef
!= DIRENTRY_NULL
)
1867 * There is already an element with the new name
1869 return STG_E_FILEALREADYEXISTS
;
1873 * Search for the old element name
1875 currentEntryRef
= findElement(This
,
1876 This
->storageDirEntry
,
1880 if (currentEntryRef
!= DIRENTRY_NULL
)
1882 if (StorageBaseImpl_IsStreamOpen(This
, currentEntryRef
) ||
1883 StorageBaseImpl_IsStorageOpen(This
, currentEntryRef
))
1885 WARN("Element is already open; cannot rename.\n");
1886 return STG_E_ACCESSDENIED
;
1889 /* Remove the element from its current position in the tree */
1890 removeFromTree(This
, This
->storageDirEntry
,
1893 /* Change the name of the element */
1894 strcpyW(currentEntry
.name
, pwcsNewName
);
1896 /* Delete any sibling links */
1897 currentEntry
.leftChild
= DIRENTRY_NULL
;
1898 currentEntry
.rightChild
= DIRENTRY_NULL
;
1900 StorageBaseImpl_WriteDirEntry(This
, currentEntryRef
,
1903 /* Insert the element in a new position in the tree */
1904 insertIntoTree(This
, This
->storageDirEntry
,
1910 * There is no element with the old name
1912 return STG_E_FILENOTFOUND
;
1915 return StorageBaseImpl_Flush(This
);
1918 /************************************************************************
1919 * StorageBaseImpl_CreateStream (IStorage)
1921 * This method will create a stream object within this storage
1923 * See Windows documentation for more details on IStorage methods.
1925 static HRESULT WINAPI
StorageBaseImpl_CreateStream(
1927 const OLECHAR
* pwcsName
, /* [string][in] */
1928 DWORD grfMode
, /* [in] */
1929 DWORD reserved1
, /* [in] */
1930 DWORD reserved2
, /* [in] */
1931 IStream
** ppstm
) /* [out] */
1933 StorageBaseImpl
*This
= impl_from_IStorage(iface
);
1934 StgStreamImpl
* newStream
;
1935 DirEntry currentEntry
, newStreamEntry
;
1936 DirRef currentEntryRef
, newStreamEntryRef
;
1939 TRACE("(%p, %s, %x, %d, %d, %p)\n",
1940 iface
, debugstr_w(pwcsName
), grfMode
,
1941 reserved1
, reserved2
, ppstm
);
1944 return STG_E_INVALIDPOINTER
;
1947 return STG_E_INVALIDNAME
;
1949 if (reserved1
|| reserved2
)
1950 return STG_E_INVALIDPARAMETER
;
1952 if ( FAILED( validateSTGM(grfMode
) ))
1953 return STG_E_INVALIDFLAG
;
1955 if (STGM_SHARE_MODE(grfMode
) != STGM_SHARE_EXCLUSIVE
)
1956 return STG_E_INVALIDFLAG
;
1959 return STG_E_REVERTED
;
1964 if ((grfMode
& STGM_DELETEONRELEASE
) ||
1965 (grfMode
& STGM_TRANSACTED
))
1966 return STG_E_INVALIDFUNCTION
;
1969 * Don't worry about permissions in transacted mode, as we can always write
1970 * changes; we just can't always commit them.
1972 if(!(This
->openFlags
& STGM_TRANSACTED
)) {
1973 /* Can't create a stream on read-only storage */
1974 if ( STGM_ACCESS_MODE( This
->openFlags
) == STGM_READ
)
1975 return STG_E_ACCESSDENIED
;
1977 /* Can't create a stream with greater access than the parent. */
1978 if ( STGM_ACCESS_MODE( grfMode
) > STGM_ACCESS_MODE( This
->openFlags
) )
1979 return STG_E_ACCESSDENIED
;
1982 if(This
->openFlags
& STGM_SIMPLE
)
1983 if(grfMode
& STGM_CREATE
) return STG_E_INVALIDFLAG
;
1987 currentEntryRef
= findElement(This
,
1988 This
->storageDirEntry
,
1992 if (currentEntryRef
!= DIRENTRY_NULL
)
1995 * An element with this name already exists
1997 if (STGM_CREATE_MODE(grfMode
) == STGM_CREATE
)
1999 IStorage_DestroyElement(iface
, pwcsName
);
2002 return STG_E_FILEALREADYEXISTS
;
2006 * memset the empty entry
2008 memset(&newStreamEntry
, 0, sizeof(DirEntry
));
2010 newStreamEntry
.sizeOfNameString
=
2011 ( lstrlenW(pwcsName
)+1 ) * sizeof(WCHAR
);
2013 if (newStreamEntry
.sizeOfNameString
> DIRENTRY_NAME_BUFFER_LEN
)
2014 return STG_E_INVALIDNAME
;
2016 strcpyW(newStreamEntry
.name
, pwcsName
);
2018 newStreamEntry
.stgType
= STGTY_STREAM
;
2019 newStreamEntry
.startingBlock
= BLOCK_END_OF_CHAIN
;
2020 newStreamEntry
.size
.u
.LowPart
= 0;
2021 newStreamEntry
.size
.u
.HighPart
= 0;
2023 newStreamEntry
.leftChild
= DIRENTRY_NULL
;
2024 newStreamEntry
.rightChild
= DIRENTRY_NULL
;
2025 newStreamEntry
.dirRootEntry
= DIRENTRY_NULL
;
2027 /* call CoFileTime to get the current time
2028 newStreamEntry.ctime
2029 newStreamEntry.mtime
2032 /* newStreamEntry.clsid */
2035 * Create an entry with the new data
2037 hr
= StorageBaseImpl_CreateDirEntry(This
, &newStreamEntry
, &newStreamEntryRef
);
2042 * Insert the new entry in the parent storage's tree.
2044 hr
= insertIntoTree(
2046 This
->storageDirEntry
,
2050 StorageBaseImpl_DestroyDirEntry(This
, newStreamEntryRef
);
2055 * Open the stream to return it.
2057 newStream
= StgStreamImpl_Construct(This
, grfMode
, newStreamEntryRef
);
2061 *ppstm
= &newStream
->IStream_iface
;
2062 IStream_AddRef(*ppstm
);
2066 return STG_E_INSUFFICIENTMEMORY
;
2069 return StorageBaseImpl_Flush(This
);
2072 /************************************************************************
2073 * StorageBaseImpl_SetClass (IStorage)
2075 * This method will write the specified CLSID in the directory entry of this
2078 * See Windows documentation for more details on IStorage methods.
2080 static HRESULT WINAPI
StorageBaseImpl_SetClass(
2082 REFCLSID clsid
) /* [in] */
2084 StorageBaseImpl
*This
= impl_from_IStorage(iface
);
2086 DirEntry currentEntry
;
2088 TRACE("(%p, %s)\n", iface
, wine_dbgstr_guid(clsid
));
2091 return STG_E_REVERTED
;
2093 hRes
= StorageBaseImpl_ReadDirEntry(This
,
2094 This
->storageDirEntry
,
2096 if (SUCCEEDED(hRes
))
2098 currentEntry
.clsid
= *clsid
;
2100 hRes
= StorageBaseImpl_WriteDirEntry(This
,
2101 This
->storageDirEntry
,
2105 if (SUCCEEDED(hRes
))
2106 hRes
= StorageBaseImpl_Flush(This
);
2111 /************************************************************************
2112 * StorageBaseImpl_CreateStorage (IStorage)
2114 * This method will create the storage object within the provided storage.
2116 * See Windows documentation for more details on IStorage methods.
2118 static HRESULT WINAPI
StorageBaseImpl_CreateStorage(
2120 const OLECHAR
*pwcsName
, /* [string][in] */
2121 DWORD grfMode
, /* [in] */
2122 DWORD reserved1
, /* [in] */
2123 DWORD reserved2
, /* [in] */
2124 IStorage
**ppstg
) /* [out] */
2126 StorageBaseImpl
* This
= impl_from_IStorage(iface
);
2128 DirEntry currentEntry
;
2130 DirRef currentEntryRef
;
2134 TRACE("(%p, %s, %x, %d, %d, %p)\n",
2135 iface
, debugstr_w(pwcsName
), grfMode
,
2136 reserved1
, reserved2
, ppstg
);
2139 return STG_E_INVALIDPOINTER
;
2141 if (This
->openFlags
& STGM_SIMPLE
)
2143 return STG_E_INVALIDFUNCTION
;
2147 return STG_E_INVALIDNAME
;
2151 if ( FAILED( validateSTGM(grfMode
) ) ||
2152 (grfMode
& STGM_DELETEONRELEASE
) )
2154 WARN("bad grfMode: 0x%x\n", grfMode
);
2155 return STG_E_INVALIDFLAG
;
2159 return STG_E_REVERTED
;
2162 * Check that we're compatible with the parent's storage mode
2164 if ( !(This
->openFlags
& STGM_TRANSACTED
) &&
2165 STGM_ACCESS_MODE( grfMode
) > STGM_ACCESS_MODE( This
->openFlags
) )
2167 WARN("access denied\n");
2168 return STG_E_ACCESSDENIED
;
2171 currentEntryRef
= findElement(This
,
2172 This
->storageDirEntry
,
2176 if (currentEntryRef
!= DIRENTRY_NULL
)
2179 * An element with this name already exists
2181 if (STGM_CREATE_MODE(grfMode
) == STGM_CREATE
&&
2182 ((This
->openFlags
& STGM_TRANSACTED
) ||
2183 STGM_ACCESS_MODE(This
->openFlags
) != STGM_READ
))
2185 hr
= IStorage_DestroyElement(iface
, pwcsName
);
2191 WARN("file already exists\n");
2192 return STG_E_FILEALREADYEXISTS
;
2195 else if (!(This
->openFlags
& STGM_TRANSACTED
) &&
2196 STGM_ACCESS_MODE(This
->openFlags
) == STGM_READ
)
2198 WARN("read-only storage\n");
2199 return STG_E_ACCESSDENIED
;
2202 memset(&newEntry
, 0, sizeof(DirEntry
));
2204 newEntry
.sizeOfNameString
= (lstrlenW(pwcsName
)+1)*sizeof(WCHAR
);
2206 if (newEntry
.sizeOfNameString
> DIRENTRY_NAME_BUFFER_LEN
)
2208 FIXME("name too long\n");
2209 return STG_E_INVALIDNAME
;
2212 strcpyW(newEntry
.name
, pwcsName
);
2214 newEntry
.stgType
= STGTY_STORAGE
;
2215 newEntry
.startingBlock
= BLOCK_END_OF_CHAIN
;
2216 newEntry
.size
.u
.LowPart
= 0;
2217 newEntry
.size
.u
.HighPart
= 0;
2219 newEntry
.leftChild
= DIRENTRY_NULL
;
2220 newEntry
.rightChild
= DIRENTRY_NULL
;
2221 newEntry
.dirRootEntry
= DIRENTRY_NULL
;
2223 /* call CoFileTime to get the current time
2228 /* newEntry.clsid */
2231 * Create a new directory entry for the storage
2233 hr
= StorageBaseImpl_CreateDirEntry(This
, &newEntry
, &newEntryRef
);
2238 * Insert the new directory entry into the parent storage's tree
2240 hr
= insertIntoTree(
2242 This
->storageDirEntry
,
2246 StorageBaseImpl_DestroyDirEntry(This
, newEntryRef
);
2251 * Open it to get a pointer to return.
2253 hr
= IStorage_OpenStorage(iface
, pwcsName
, 0, grfMode
, 0, 0, ppstg
);
2255 if( (hr
!= S_OK
) || (*ppstg
== NULL
))
2261 hr
= StorageBaseImpl_Flush(This
);
2266 static HRESULT
StorageBaseImpl_CopyStorageEntryTo(StorageBaseImpl
*This
,
2267 DirRef srcEntry
, BOOL skip_storage
, BOOL skip_stream
,
2268 SNB snbExclude
, IStorage
*pstgDest
)
2273 hr
= StorageBaseImpl_ReadDirEntry( This
, srcEntry
, &data
);
2276 hr
= IStorage_SetClass( pstgDest
, &data
.clsid
);
2279 hr
= StorageBaseImpl_CopyChildEntryTo( This
, data
.dirRootEntry
, skip_storage
,
2280 skip_stream
, snbExclude
, pstgDest
);
2282 TRACE("<-- %08x\n", hr
);
2286 /*************************************************************************
2289 static HRESULT WINAPI
StorageBaseImpl_CopyTo(
2291 DWORD ciidExclude
, /* [in] */
2292 const IID
* rgiidExclude
, /* [size_is][unique][in] */
2293 SNB snbExclude
, /* [unique][in] */
2294 IStorage
* pstgDest
) /* [unique][in] */
2296 StorageBaseImpl
*This
= impl_from_IStorage(iface
);
2298 BOOL skip_storage
= FALSE
, skip_stream
= FALSE
;
2301 TRACE("(%p, %d, %p, %p, %p)\n",
2302 iface
, ciidExclude
, rgiidExclude
,
2303 snbExclude
, pstgDest
);
2305 if ( pstgDest
== 0 )
2306 return STG_E_INVALIDPOINTER
;
2308 for(i
= 0; i
< ciidExclude
; ++i
)
2310 if(IsEqualGUID(&IID_IStorage
, &rgiidExclude
[i
]))
2311 skip_storage
= TRUE
;
2312 else if(IsEqualGUID(&IID_IStream
, &rgiidExclude
[i
]))
2315 WARN("Unknown excluded GUID: %s\n", debugstr_guid(&rgiidExclude
[i
]));
2320 /* Give up early if it looks like this would be infinitely recursive.
2321 * Oddly enough, this includes some cases that aren't really recursive, like
2322 * copying to a transacted child. */
2323 IStorage
*pstgDestAncestor
= pstgDest
;
2324 IStorage
*pstgDestAncestorChild
= NULL
;
2326 /* Go up the chain from the destination until we find the source storage. */
2327 while (pstgDestAncestor
!= iface
) {
2328 pstgDestAncestorChild
= pstgDest
;
2330 if (pstgDestAncestor
->lpVtbl
== &TransactedSnapshotImpl_Vtbl
)
2332 TransactedSnapshotImpl
*snapshot
= (TransactedSnapshotImpl
*) pstgDestAncestor
;
2334 pstgDestAncestor
= &snapshot
->transactedParent
->IStorage_iface
;
2336 else if (pstgDestAncestor
->lpVtbl
== &StorageInternalImpl_Vtbl
)
2338 StorageInternalImpl
*internal
= (StorageInternalImpl
*) pstgDestAncestor
;
2340 pstgDestAncestor
= &internal
->parentStorage
->IStorage_iface
;
2346 if (pstgDestAncestor
== iface
)
2350 if (pstgDestAncestorChild
&& snbExclude
)
2352 StorageBaseImpl
*ancestorChildBase
= (StorageBaseImpl
*)pstgDestAncestorChild
;
2354 WCHAR
**snb
= snbExclude
;
2356 StorageBaseImpl_ReadDirEntry(ancestorChildBase
, ancestorChildBase
->storageDirEntry
, &data
);
2358 while ( *snb
!= NULL
&& fail
)
2360 if ( lstrcmpW(data
.name
, *snb
) == 0 )
2367 return STG_E_ACCESSDENIED
;
2371 return StorageBaseImpl_CopyStorageEntryTo( This
, This
->storageDirEntry
,
2372 skip_storage
, skip_stream
, snbExclude
, pstgDest
);
2375 /*************************************************************************
2376 * MoveElementTo (IStorage)
2378 static HRESULT WINAPI
StorageBaseImpl_MoveElementTo(
2380 const OLECHAR
*pwcsName
, /* [string][in] */
2381 IStorage
*pstgDest
, /* [unique][in] */
2382 const OLECHAR
*pwcsNewName
,/* [string][in] */
2383 DWORD grfFlags
) /* [in] */
2385 FIXME("(%p %s %p %s %u): stub\n", iface
,
2386 debugstr_w(pwcsName
), pstgDest
,
2387 debugstr_w(pwcsNewName
), grfFlags
);
2391 /*************************************************************************
2394 * Ensures that any changes made to a storage object open in transacted mode
2395 * are reflected in the parent storage
2397 * In a non-transacted mode, this ensures all cached writes are completed.
2399 static HRESULT WINAPI
StorageBaseImpl_Commit(
2401 DWORD grfCommitFlags
)/* [in] */
2403 StorageBaseImpl
* This
= impl_from_IStorage(iface
);
2404 TRACE("(%p %d)\n", iface
, grfCommitFlags
);
2405 return StorageBaseImpl_Flush(This
);
2408 /*************************************************************************
2411 * Discard all changes that have been made since the last commit operation
2413 static HRESULT WINAPI
StorageBaseImpl_Revert(
2416 TRACE("(%p)\n", iface
);
2420 /*********************************************************************
2422 * Internal helper function for StorageBaseImpl_DestroyElement()
2424 * Delete the contents of a storage entry.
2427 static HRESULT
deleteStorageContents(
2428 StorageBaseImpl
*parentStorage
,
2429 DirRef indexToDelete
,
2430 DirEntry entryDataToDelete
)
2432 IEnumSTATSTG
*elements
= 0;
2433 IStorage
*childStorage
= 0;
2434 STATSTG currentElement
;
2436 HRESULT destroyHr
= S_OK
;
2437 StorageInternalImpl
*stg
, *stg2
;
2439 TRACE("%p,%d\n", parentStorage
, indexToDelete
);
2441 /* Invalidate any open storage objects. */
2442 LIST_FOR_EACH_ENTRY_SAFE(stg
, stg2
, &parentStorage
->storageHead
, StorageInternalImpl
, ParentListEntry
)
2444 if (stg
->base
.storageDirEntry
== indexToDelete
)
2446 StorageBaseImpl_Invalidate(&stg
->base
);
2451 * Open the storage and enumerate it
2453 hr
= IStorage_OpenStorage(
2454 &parentStorage
->IStorage_iface
,
2455 entryDataToDelete
.name
,
2457 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
,
2464 TRACE("<-- %08x\n", hr
);
2469 * Enumerate the elements
2471 hr
= IStorage_EnumElements(childStorage
, 0, 0, 0, &elements
);
2474 IStorage_Release(childStorage
);
2475 TRACE("<-- %08x\n", hr
);
2482 * Obtain the next element
2484 hr
= IEnumSTATSTG_Next(elements
, 1, ¤tElement
, NULL
);
2487 destroyHr
= IStorage_DestroyElement(childStorage
, currentElement
.pwcsName
);
2489 CoTaskMemFree(currentElement
.pwcsName
);
2493 * We need to Reset the enumeration every time because we delete elements
2494 * and the enumeration could be invalid
2496 IEnumSTATSTG_Reset(elements
);
2498 } while ((hr
== S_OK
) && (destroyHr
== S_OK
));
2500 IStorage_Release(childStorage
);
2501 IEnumSTATSTG_Release(elements
);
2503 TRACE("%08x\n", hr
);
2507 /*********************************************************************
2509 * Internal helper function for StorageBaseImpl_DestroyElement()
2511 * Perform the deletion of a stream's data
2514 static HRESULT
deleteStreamContents(
2515 StorageBaseImpl
*parentStorage
,
2516 DirRef indexToDelete
,
2517 DirEntry entryDataToDelete
)
2521 ULARGE_INTEGER size
;
2522 StgStreamImpl
*strm
, *strm2
;
2524 /* Invalidate any open stream objects. */
2525 LIST_FOR_EACH_ENTRY_SAFE(strm
, strm2
, &parentStorage
->strmHead
, StgStreamImpl
, StrmListEntry
)
2527 if (strm
->dirEntry
== indexToDelete
)
2529 TRACE("Stream deleted %p\n", strm
);
2530 strm
->parentStorage
= NULL
;
2531 list_remove(&strm
->StrmListEntry
);
2535 size
.u
.HighPart
= 0;
2538 hr
= IStorage_OpenStream(&parentStorage
->IStorage_iface
,
2539 entryDataToDelete
.name
, NULL
, STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &pis
);
2543 TRACE("<-- %08x\n", hr
);
2550 hr
= IStream_SetSize(pis
, size
);
2554 TRACE("<-- %08x\n", hr
);
2559 * Release the stream object.
2561 IStream_Release(pis
);
2562 TRACE("<-- %08x\n", hr
);
2566 /*************************************************************************
2567 * DestroyElement (IStorage)
2569 * Strategy: This implementation is built this way for simplicity not for speed.
2570 * I always delete the topmost element of the enumeration and adjust
2571 * the deleted element pointer all the time. This takes longer to
2572 * do but allows reinvoking DestroyElement whenever we encounter a
2573 * storage object. The optimisation resides in the usage of another
2574 * enumeration strategy that would give all the leaves of a storage
2575 * first. (postfix order)
2577 static HRESULT WINAPI
StorageBaseImpl_DestroyElement(
2579 const OLECHAR
*pwcsName
)/* [string][in] */
2581 StorageBaseImpl
*This
= impl_from_IStorage(iface
);
2584 DirEntry entryToDelete
;
2585 DirRef entryToDeleteRef
;
2588 iface
, debugstr_w(pwcsName
));
2591 return STG_E_INVALIDPOINTER
;
2594 return STG_E_REVERTED
;
2596 if ( !(This
->openFlags
& STGM_TRANSACTED
) &&
2597 STGM_ACCESS_MODE( This
->openFlags
) == STGM_READ
)
2598 return STG_E_ACCESSDENIED
;
2600 entryToDeleteRef
= findElement(
2602 This
->storageDirEntry
,
2606 if ( entryToDeleteRef
== DIRENTRY_NULL
)
2608 TRACE("<-- STG_E_FILENOTFOUND\n");
2609 return STG_E_FILENOTFOUND
;
2612 if ( entryToDelete
.stgType
== STGTY_STORAGE
)
2614 hr
= deleteStorageContents(
2619 else if ( entryToDelete
.stgType
== STGTY_STREAM
)
2621 hr
= deleteStreamContents(
2629 TRACE("<-- %08x\n", hr
);
2634 * Remove the entry from its parent storage
2636 hr
= removeFromTree(
2638 This
->storageDirEntry
,
2642 * Invalidate the entry
2645 StorageBaseImpl_DestroyDirEntry(This
, entryToDeleteRef
);
2648 hr
= StorageBaseImpl_Flush(This
);
2650 TRACE("<-- %08x\n", hr
);
2654 static void StorageBaseImpl_DeleteAll(StorageBaseImpl
* stg
)
2656 struct list
*cur
, *cur2
;
2657 StgStreamImpl
*strm
=NULL
;
2658 StorageInternalImpl
*childstg
=NULL
;
2660 LIST_FOR_EACH_SAFE(cur
, cur2
, &stg
->strmHead
) {
2661 strm
= LIST_ENTRY(cur
,StgStreamImpl
,StrmListEntry
);
2662 TRACE("Streams invalidated (stg=%p strm=%p next=%p prev=%p)\n", stg
,strm
,cur
->next
,cur
->prev
);
2663 strm
->parentStorage
= NULL
;
2667 LIST_FOR_EACH_SAFE(cur
, cur2
, &stg
->storageHead
) {
2668 childstg
= LIST_ENTRY(cur
,StorageInternalImpl
,ParentListEntry
);
2669 StorageBaseImpl_Invalidate( &childstg
->base
);
2672 if (stg
->transactedChild
)
2674 StorageBaseImpl_Invalidate(stg
->transactedChild
);
2676 stg
->transactedChild
= NULL
;
2680 /******************************************************************************
2681 * SetElementTimes (IStorage)
2683 static HRESULT WINAPI
StorageBaseImpl_SetElementTimes(
2685 const OLECHAR
*pwcsName
,/* [string][in] */
2686 const FILETIME
*pctime
, /* [in] */
2687 const FILETIME
*patime
, /* [in] */
2688 const FILETIME
*pmtime
) /* [in] */
2690 FIXME("(%s,...), stub!\n",debugstr_w(pwcsName
));
2694 /******************************************************************************
2695 * SetStateBits (IStorage)
2697 static HRESULT WINAPI
StorageBaseImpl_SetStateBits(
2699 DWORD grfStateBits
,/* [in] */
2700 DWORD grfMask
) /* [in] */
2702 StorageBaseImpl
*This
= impl_from_IStorage(iface
);
2705 return STG_E_REVERTED
;
2707 This
->stateBits
= (This
->stateBits
& ~grfMask
) | (grfStateBits
& grfMask
);
2711 /******************************************************************************
2712 * Internal stream list handlers
2715 void StorageBaseImpl_AddStream(StorageBaseImpl
* stg
, StgStreamImpl
* strm
)
2717 TRACE("Stream added (stg=%p strm=%p)\n", stg
, strm
);
2718 list_add_tail(&stg
->strmHead
,&strm
->StrmListEntry
);
2721 void StorageBaseImpl_RemoveStream(StorageBaseImpl
* stg
, StgStreamImpl
* strm
)
2723 TRACE("Stream removed (stg=%p strm=%p)\n", stg
,strm
);
2724 list_remove(&(strm
->StrmListEntry
));
2727 static HRESULT
StorageBaseImpl_CopyStream(
2728 StorageBaseImpl
*dst
, DirRef dst_entry
,
2729 StorageBaseImpl
*src
, DirRef src_entry
)
2734 ULARGE_INTEGER bytes_copied
;
2735 ULONG bytestocopy
, bytesread
, byteswritten
;
2737 hr
= StorageBaseImpl_ReadDirEntry(src
, src_entry
, &srcdata
);
2741 hr
= StorageBaseImpl_StreamSetSize(dst
, dst_entry
, srcdata
.size
);
2743 bytes_copied
.QuadPart
= 0;
2744 while (bytes_copied
.QuadPart
< srcdata
.size
.QuadPart
&& SUCCEEDED(hr
))
2746 bytestocopy
= min(4096, srcdata
.size
.QuadPart
- bytes_copied
.QuadPart
);
2748 hr
= StorageBaseImpl_StreamReadAt(src
, src_entry
, bytes_copied
, bytestocopy
,
2750 if (SUCCEEDED(hr
) && bytesread
!= bytestocopy
) hr
= STG_E_READFAULT
;
2753 hr
= StorageBaseImpl_StreamWriteAt(dst
, dst_entry
, bytes_copied
, bytestocopy
,
2754 data
, &byteswritten
);
2757 if (byteswritten
!= bytestocopy
) hr
= STG_E_WRITEFAULT
;
2758 bytes_copied
.QuadPart
+= byteswritten
;
2766 static HRESULT
StorageBaseImpl_DupStorageTree(
2767 StorageBaseImpl
*dst
, DirRef
*dst_entry
,
2768 StorageBaseImpl
*src
, DirRef src_entry
)
2772 BOOL has_stream
=FALSE
;
2774 if (src_entry
== DIRENTRY_NULL
)
2776 *dst_entry
= DIRENTRY_NULL
;
2780 hr
= StorageBaseImpl_ReadDirEntry(src
, src_entry
, &data
);
2783 has_stream
= (data
.stgType
== STGTY_STREAM
&& data
.size
.QuadPart
!= 0);
2784 data
.startingBlock
= BLOCK_END_OF_CHAIN
;
2785 data
.size
.QuadPart
= 0;
2787 hr
= StorageBaseImpl_DupStorageTree(dst
, &data
.leftChild
, src
, data
.leftChild
);
2791 hr
= StorageBaseImpl_DupStorageTree(dst
, &data
.rightChild
, src
, data
.rightChild
);
2794 hr
= StorageBaseImpl_DupStorageTree(dst
, &data
.dirRootEntry
, src
, data
.dirRootEntry
);
2797 hr
= StorageBaseImpl_CreateDirEntry(dst
, &data
, dst_entry
);
2799 if (SUCCEEDED(hr
) && has_stream
)
2800 hr
= StorageBaseImpl_CopyStream(dst
, *dst_entry
, src
, src_entry
);
2805 static HRESULT
StorageBaseImpl_CopyStorageTree(
2806 StorageBaseImpl
*dst
, DirRef dst_entry
,
2807 StorageBaseImpl
*src
, DirRef src_entry
)
2810 DirEntry src_data
, dst_data
;
2811 DirRef new_root_entry
;
2813 hr
= StorageBaseImpl_ReadDirEntry(src
, src_entry
, &src_data
);
2817 hr
= StorageBaseImpl_DupStorageTree(dst
, &new_root_entry
, src
, src_data
.dirRootEntry
);
2822 hr
= StorageBaseImpl_ReadDirEntry(dst
, dst_entry
, &dst_data
);
2823 dst_data
.clsid
= src_data
.clsid
;
2824 dst_data
.ctime
= src_data
.ctime
;
2825 dst_data
.mtime
= src_data
.mtime
;
2826 dst_data
.dirRootEntry
= new_root_entry
;
2830 hr
= StorageBaseImpl_WriteDirEntry(dst
, dst_entry
, &dst_data
);
2835 static HRESULT
StorageBaseImpl_DeleteStorageTree(StorageBaseImpl
*This
, DirRef entry
, BOOL include_siblings
)
2839 ULARGE_INTEGER zero
;
2841 if (entry
== DIRENTRY_NULL
)
2846 hr
= StorageBaseImpl_ReadDirEntry(This
, entry
, &data
);
2848 if (SUCCEEDED(hr
) && include_siblings
)
2849 hr
= StorageBaseImpl_DeleteStorageTree(This
, data
.leftChild
, TRUE
);
2851 if (SUCCEEDED(hr
) && include_siblings
)
2852 hr
= StorageBaseImpl_DeleteStorageTree(This
, data
.rightChild
, TRUE
);
2855 hr
= StorageBaseImpl_DeleteStorageTree(This
, data
.dirRootEntry
, TRUE
);
2857 if (SUCCEEDED(hr
) && data
.stgType
== STGTY_STREAM
)
2858 hr
= StorageBaseImpl_StreamSetSize(This
, entry
, zero
);
2861 hr
= StorageBaseImpl_DestroyDirEntry(This
, entry
);
2867 /************************************************************************
2868 * StorageImpl implementation
2869 ***********************************************************************/
2871 static HRESULT
StorageImpl_ReadAt(StorageImpl
* This
,
2872 ULARGE_INTEGER offset
,
2877 return ILockBytes_ReadAt(This
->lockBytes
,offset
,buffer
,size
,bytesRead
);
2880 static HRESULT
StorageImpl_WriteAt(StorageImpl
* This
,
2881 ULARGE_INTEGER offset
,
2884 ULONG
* bytesWritten
)
2886 return ILockBytes_WriteAt(This
->lockBytes
,offset
,buffer
,size
,bytesWritten
);
2889 /******************************************************************************
2890 * StorageImpl_LoadFileHeader
2892 * This method will read in the file header
2894 static HRESULT
StorageImpl_LoadFileHeader(
2898 BYTE headerBigBlock
[HEADER_SIZE
];
2900 ULARGE_INTEGER offset
;
2905 * Get a pointer to the big block of data containing the header.
2907 offset
.u
.HighPart
= 0;
2908 offset
.u
.LowPart
= 0;
2909 hr
= StorageImpl_ReadAt(This
, offset
, headerBigBlock
, HEADER_SIZE
, &bytes_read
);
2910 if (SUCCEEDED(hr
) && bytes_read
!= HEADER_SIZE
)
2911 hr
= STG_E_FILENOTFOUND
;
2914 * Extract the information from the header.
2919 * Check for the "magic number" signature and return an error if it is not
2922 if (memcmp(headerBigBlock
, STORAGE_oldmagic
, sizeof(STORAGE_oldmagic
))==0)
2924 return STG_E_OLDFORMAT
;
2927 if (memcmp(headerBigBlock
, STORAGE_magic
, sizeof(STORAGE_magic
))!=0)
2929 return STG_E_INVALIDHEADER
;
2932 StorageUtl_ReadWord(
2934 OFFSET_BIGBLOCKSIZEBITS
,
2935 &This
->bigBlockSizeBits
);
2937 StorageUtl_ReadWord(
2939 OFFSET_SMALLBLOCKSIZEBITS
,
2940 &This
->smallBlockSizeBits
);
2942 StorageUtl_ReadDWord(
2944 OFFSET_BBDEPOTCOUNT
,
2945 &This
->bigBlockDepotCount
);
2947 StorageUtl_ReadDWord(
2949 OFFSET_ROOTSTARTBLOCK
,
2950 &This
->rootStartBlock
);
2952 StorageUtl_ReadDWord(
2954 OFFSET_TRANSACTIONSIG
,
2955 &This
->transactionSig
);
2957 StorageUtl_ReadDWord(
2959 OFFSET_SMALLBLOCKLIMIT
,
2960 &This
->smallBlockLimit
);
2962 StorageUtl_ReadDWord(
2964 OFFSET_SBDEPOTSTART
,
2965 &This
->smallBlockDepotStart
);
2967 StorageUtl_ReadDWord(
2969 OFFSET_EXTBBDEPOTSTART
,
2970 &This
->extBigBlockDepotStart
);
2972 StorageUtl_ReadDWord(
2974 OFFSET_EXTBBDEPOTCOUNT
,
2975 &This
->extBigBlockDepotCount
);
2977 for (index
= 0; index
< COUNT_BBDEPOTINHEADER
; index
++)
2979 StorageUtl_ReadDWord(
2981 OFFSET_BBDEPOTSTART
+ (sizeof(ULONG
)*index
),
2982 &(This
->bigBlockDepotStart
[index
]));
2986 * Make the bitwise arithmetic to get the size of the blocks in bytes.
2988 This
->bigBlockSize
= 0x000000001 << (DWORD
)This
->bigBlockSizeBits
;
2989 This
->smallBlockSize
= 0x000000001 << (DWORD
)This
->smallBlockSizeBits
;
2992 * Right now, the code is making some assumptions about the size of the
2993 * blocks, just make sure they are what we're expecting.
2995 if ((This
->bigBlockSize
!= MIN_BIG_BLOCK_SIZE
&& This
->bigBlockSize
!= MAX_BIG_BLOCK_SIZE
) ||
2996 This
->smallBlockSize
!= DEF_SMALL_BLOCK_SIZE
||
2997 This
->smallBlockLimit
!= LIMIT_TO_USE_SMALL_BLOCK
)
2999 FIXME("Broken OLE storage file? bigblock=0x%x, smallblock=0x%x, sblimit=0x%x\n",
3000 This
->bigBlockSize
, This
->smallBlockSize
, This
->smallBlockLimit
);
3001 hr
= STG_E_INVALIDHEADER
;
3010 /******************************************************************************
3011 * StorageImpl_SaveFileHeader
3013 * This method will save to the file the header
3015 static void StorageImpl_SaveFileHeader(
3018 BYTE headerBigBlock
[HEADER_SIZE
];
3021 ULARGE_INTEGER offset
;
3022 DWORD bytes_read
, bytes_written
;
3023 DWORD major_version
, dirsectorcount
;
3026 * Get a pointer to the big block of data containing the header.
3028 offset
.u
.HighPart
= 0;
3029 offset
.u
.LowPart
= 0;
3030 hr
= StorageImpl_ReadAt(This
, offset
, headerBigBlock
, HEADER_SIZE
, &bytes_read
);
3031 if (SUCCEEDED(hr
) && bytes_read
!= HEADER_SIZE
)
3032 hr
= STG_E_FILENOTFOUND
;
3034 if (This
->bigBlockSizeBits
== 0x9)
3036 else if (This
->bigBlockSizeBits
== 0xc)
3040 ERR("invalid big block shift 0x%x\n", This
->bigBlockSizeBits
);
3045 * If the block read failed, the file is probably new.
3050 * Initialize for all unknown fields.
3052 memset(headerBigBlock
, 0, HEADER_SIZE
);
3055 * Initialize the magic number.
3057 memcpy(headerBigBlock
, STORAGE_magic
, sizeof(STORAGE_magic
));
3061 * Write the information to the header.
3063 StorageUtl_WriteWord(
3065 OFFSET_MINORVERSION
,
3068 StorageUtl_WriteWord(
3070 OFFSET_MAJORVERSION
,
3073 StorageUtl_WriteWord(
3075 OFFSET_BYTEORDERMARKER
,
3078 StorageUtl_WriteWord(
3080 OFFSET_BIGBLOCKSIZEBITS
,
3081 This
->bigBlockSizeBits
);
3083 StorageUtl_WriteWord(
3085 OFFSET_SMALLBLOCKSIZEBITS
,
3086 This
->smallBlockSizeBits
);
3088 if (major_version
>= 4)
3090 if (This
->rootBlockChain
)
3091 dirsectorcount
= BlockChainStream_GetCount(This
->rootBlockChain
);
3093 /* This file is being created, and it will start out with one block. */
3097 /* This field must be 0 in versions older than 4 */
3100 StorageUtl_WriteDWord(
3102 OFFSET_DIRSECTORCOUNT
,
3105 StorageUtl_WriteDWord(
3107 OFFSET_BBDEPOTCOUNT
,
3108 This
->bigBlockDepotCount
);
3110 StorageUtl_WriteDWord(
3112 OFFSET_ROOTSTARTBLOCK
,
3113 This
->rootStartBlock
);
3115 StorageUtl_WriteDWord(
3117 OFFSET_TRANSACTIONSIG
,
3118 This
->transactionSig
);
3120 StorageUtl_WriteDWord(
3122 OFFSET_SMALLBLOCKLIMIT
,
3123 This
->smallBlockLimit
);
3125 StorageUtl_WriteDWord(
3127 OFFSET_SBDEPOTSTART
,
3128 This
->smallBlockDepotStart
);
3130 StorageUtl_WriteDWord(
3132 OFFSET_SBDEPOTCOUNT
,
3133 This
->smallBlockDepotChain
?
3134 BlockChainStream_GetCount(This
->smallBlockDepotChain
) : 0);
3136 StorageUtl_WriteDWord(
3138 OFFSET_EXTBBDEPOTSTART
,
3139 This
->extBigBlockDepotStart
);
3141 StorageUtl_WriteDWord(
3143 OFFSET_EXTBBDEPOTCOUNT
,
3144 This
->extBigBlockDepotCount
);
3146 for (index
= 0; index
< COUNT_BBDEPOTINHEADER
; index
++)
3148 StorageUtl_WriteDWord(
3150 OFFSET_BBDEPOTSTART
+ (sizeof(ULONG
)*index
),
3151 (This
->bigBlockDepotStart
[index
]));
3155 * Write the big block back to the file.
3157 StorageImpl_WriteAt(This
, offset
, headerBigBlock
, HEADER_SIZE
, &bytes_written
);
3161 /************************************************************************
3162 * StorageImpl implementation : DirEntry methods
3163 ***********************************************************************/
3165 /******************************************************************************
3166 * StorageImpl_ReadRawDirEntry
3168 * This method will read the raw data from a directory entry in the file.
3170 * buffer must be RAW_DIRENTRY_SIZE bytes long.
3172 static HRESULT
StorageImpl_ReadRawDirEntry(StorageImpl
*This
, ULONG index
, BYTE
*buffer
)
3174 ULARGE_INTEGER offset
;
3178 offset
.QuadPart
= (ULONGLONG
)index
* RAW_DIRENTRY_SIZE
;
3180 hr
= BlockChainStream_ReadAt(
3181 This
->rootBlockChain
,
3187 if (bytesRead
!= RAW_DIRENTRY_SIZE
)
3188 return STG_E_READFAULT
;
3193 /******************************************************************************
3194 * StorageImpl_WriteRawDirEntry
3196 * This method will write the raw data from a directory entry in the file.
3198 * buffer must be RAW_DIRENTRY_SIZE bytes long.
3200 static HRESULT
StorageImpl_WriteRawDirEntry(StorageImpl
*This
, ULONG index
, const BYTE
*buffer
)
3202 ULARGE_INTEGER offset
;
3205 offset
.QuadPart
= (ULONGLONG
)index
* RAW_DIRENTRY_SIZE
;
3207 return BlockChainStream_WriteAt(
3208 This
->rootBlockChain
,
3215 /***************************************************************************
3219 * Mark a directory entry in the file as free.
3221 static HRESULT
StorageImpl_DestroyDirEntry(
3222 StorageBaseImpl
*base
,
3225 BYTE emptyData
[RAW_DIRENTRY_SIZE
];
3226 StorageImpl
*storage
= (StorageImpl
*)base
;
3228 memset(emptyData
, 0, RAW_DIRENTRY_SIZE
);
3230 return StorageImpl_WriteRawDirEntry(storage
, index
, emptyData
);
3233 /******************************************************************************
3236 * Update raw directory entry data from the fields in newData.
3238 * buffer must be RAW_DIRENTRY_SIZE bytes long.
3240 static void UpdateRawDirEntry(BYTE
*buffer
, const DirEntry
*newData
)
3242 memset(buffer
, 0, RAW_DIRENTRY_SIZE
);
3245 buffer
+ OFFSET_PS_NAME
,
3247 DIRENTRY_NAME_BUFFER_LEN
);
3249 memcpy(buffer
+ OFFSET_PS_STGTYPE
, &newData
->stgType
, 1);
3251 StorageUtl_WriteWord(
3253 OFFSET_PS_NAMELENGTH
,
3254 newData
->sizeOfNameString
);
3256 StorageUtl_WriteDWord(
3258 OFFSET_PS_LEFTCHILD
,
3259 newData
->leftChild
);
3261 StorageUtl_WriteDWord(
3263 OFFSET_PS_RIGHTCHILD
,
3264 newData
->rightChild
);
3266 StorageUtl_WriteDWord(
3269 newData
->dirRootEntry
);
3271 StorageUtl_WriteGUID(
3276 StorageUtl_WriteDWord(
3279 newData
->ctime
.dwLowDateTime
);
3281 StorageUtl_WriteDWord(
3283 OFFSET_PS_CTIMEHIGH
,
3284 newData
->ctime
.dwHighDateTime
);
3286 StorageUtl_WriteDWord(
3289 newData
->mtime
.dwLowDateTime
);
3291 StorageUtl_WriteDWord(
3293 OFFSET_PS_MTIMEHIGH
,
3294 newData
->ctime
.dwHighDateTime
);
3296 StorageUtl_WriteDWord(
3298 OFFSET_PS_STARTBLOCK
,
3299 newData
->startingBlock
);
3301 StorageUtl_WriteDWord(
3304 newData
->size
.u
.LowPart
);
3306 StorageUtl_WriteDWord(
3308 OFFSET_PS_SIZE_HIGH
,
3309 newData
->size
.u
.HighPart
);
3312 /***************************************************************************
3316 * Reserve a directory entry in the file and initialize it.
3318 static HRESULT
StorageImpl_CreateDirEntry(
3319 StorageBaseImpl
*base
,
3320 const DirEntry
*newData
,
3323 StorageImpl
*storage
= (StorageImpl
*)base
;
3324 ULONG currentEntryIndex
= 0;
3325 ULONG newEntryIndex
= DIRENTRY_NULL
;
3327 BYTE currentData
[RAW_DIRENTRY_SIZE
];
3328 WORD sizeOfNameString
;
3332 hr
= StorageImpl_ReadRawDirEntry(storage
,
3338 StorageUtl_ReadWord(
3340 OFFSET_PS_NAMELENGTH
,
3343 if (sizeOfNameString
== 0)
3346 * The entry exists and is available, we found it.
3348 newEntryIndex
= currentEntryIndex
;
3354 * We exhausted the directory entries, we will create more space below
3356 newEntryIndex
= currentEntryIndex
;
3358 currentEntryIndex
++;
3360 } while (newEntryIndex
== DIRENTRY_NULL
);
3363 * grow the directory stream
3367 BYTE emptyData
[RAW_DIRENTRY_SIZE
];
3368 ULARGE_INTEGER newSize
;
3370 ULONG lastEntry
= 0;
3371 ULONG blockCount
= 0;
3374 * obtain the new count of blocks in the directory stream
3376 blockCount
= BlockChainStream_GetCount(
3377 storage
->rootBlockChain
)+1;
3380 * initialize the size used by the directory stream
3382 newSize
.QuadPart
= (ULONGLONG
)storage
->bigBlockSize
* blockCount
;
3385 * add a block to the directory stream
3387 BlockChainStream_SetSize(storage
->rootBlockChain
, newSize
);
3390 * memset the empty entry in order to initialize the unused newly
3393 memset(emptyData
, 0, RAW_DIRENTRY_SIZE
);
3398 lastEntry
= storage
->bigBlockSize
/ RAW_DIRENTRY_SIZE
* blockCount
;
3401 entryIndex
= newEntryIndex
+ 1;
3402 entryIndex
< lastEntry
;
3405 StorageImpl_WriteRawDirEntry(
3411 StorageImpl_SaveFileHeader(storage
);
3414 UpdateRawDirEntry(currentData
, newData
);
3416 hr
= StorageImpl_WriteRawDirEntry(storage
, newEntryIndex
, currentData
);
3419 *index
= newEntryIndex
;
3424 /******************************************************************************
3425 * StorageImpl_ReadDirEntry
3427 * This method will read the specified directory entry.
3429 static HRESULT
StorageImpl_ReadDirEntry(
3434 BYTE currentEntry
[RAW_DIRENTRY_SIZE
];
3437 readRes
= StorageImpl_ReadRawDirEntry(This
, index
, currentEntry
);
3439 if (SUCCEEDED(readRes
))
3441 memset(buffer
->name
, 0, sizeof(buffer
->name
));
3444 (WCHAR
*)currentEntry
+OFFSET_PS_NAME
,
3445 DIRENTRY_NAME_BUFFER_LEN
);
3446 TRACE("storage name: %s\n", debugstr_w(buffer
->name
));
3448 memcpy(&buffer
->stgType
, currentEntry
+ OFFSET_PS_STGTYPE
, 1);
3450 StorageUtl_ReadWord(
3452 OFFSET_PS_NAMELENGTH
,
3453 &buffer
->sizeOfNameString
);
3455 StorageUtl_ReadDWord(
3457 OFFSET_PS_LEFTCHILD
,
3458 &buffer
->leftChild
);
3460 StorageUtl_ReadDWord(
3462 OFFSET_PS_RIGHTCHILD
,
3463 &buffer
->rightChild
);
3465 StorageUtl_ReadDWord(
3468 &buffer
->dirRootEntry
);
3470 StorageUtl_ReadGUID(
3475 StorageUtl_ReadDWord(
3478 &buffer
->ctime
.dwLowDateTime
);
3480 StorageUtl_ReadDWord(
3482 OFFSET_PS_CTIMEHIGH
,
3483 &buffer
->ctime
.dwHighDateTime
);
3485 StorageUtl_ReadDWord(
3488 &buffer
->mtime
.dwLowDateTime
);
3490 StorageUtl_ReadDWord(
3492 OFFSET_PS_MTIMEHIGH
,
3493 &buffer
->mtime
.dwHighDateTime
);
3495 StorageUtl_ReadDWord(
3497 OFFSET_PS_STARTBLOCK
,
3498 &buffer
->startingBlock
);
3500 StorageUtl_ReadDWord(
3503 &buffer
->size
.u
.LowPart
);
3505 if (This
->bigBlockSize
< 4096)
3507 /* Version 3 files may have junk in the high part of size. */
3508 buffer
->size
.u
.HighPart
= 0;
3512 StorageUtl_ReadDWord(
3514 OFFSET_PS_SIZE_HIGH
,
3515 &buffer
->size
.u
.HighPart
);
3522 /*********************************************************************
3523 * Write the specified directory entry to the file
3525 static HRESULT
StorageImpl_WriteDirEntry(
3528 const DirEntry
* buffer
)
3530 BYTE currentEntry
[RAW_DIRENTRY_SIZE
];
3532 UpdateRawDirEntry(currentEntry
, buffer
);
3534 return StorageImpl_WriteRawDirEntry(This
, index
, currentEntry
);
3538 /************************************************************************
3539 * StorageImpl implementation : Block methods
3540 ***********************************************************************/
3542 static ULONGLONG
StorageImpl_GetBigBlockOffset(StorageImpl
* This
, ULONG index
)
3544 return (ULONGLONG
)(index
+1) * This
->bigBlockSize
;
3547 static HRESULT
StorageImpl_ReadBigBlock(
3553 ULARGE_INTEGER ulOffset
;
3557 ulOffset
.QuadPart
= StorageImpl_GetBigBlockOffset(This
, blockIndex
);
3559 hr
= StorageImpl_ReadAt(This
, ulOffset
, buffer
, This
->bigBlockSize
, &read
);
3561 if (SUCCEEDED(hr
) && read
< This
->bigBlockSize
)
3563 /* File ends during this block; fill the rest with 0's. */
3564 memset((LPBYTE
)buffer
+read
, 0, This
->bigBlockSize
-read
);
3567 if (out_read
) *out_read
= read
;
3572 static BOOL
StorageImpl_ReadDWordFromBigBlock(
3578 ULARGE_INTEGER ulOffset
;
3582 ulOffset
.QuadPart
= StorageImpl_GetBigBlockOffset(This
, blockIndex
);
3583 ulOffset
.QuadPart
+= offset
;
3585 StorageImpl_ReadAt(This
, ulOffset
, &tmp
, sizeof(DWORD
), &read
);
3586 *value
= lendian32toh(tmp
);
3587 return (read
== sizeof(DWORD
));
3590 static BOOL
StorageImpl_WriteBigBlock(
3595 ULARGE_INTEGER ulOffset
;
3598 ulOffset
.QuadPart
= StorageImpl_GetBigBlockOffset(This
, blockIndex
);
3600 StorageImpl_WriteAt(This
, ulOffset
, buffer
, This
->bigBlockSize
, &wrote
);
3601 return (wrote
== This
->bigBlockSize
);
3604 static BOOL
StorageImpl_WriteDWordToBigBlock(
3610 ULARGE_INTEGER ulOffset
;
3613 ulOffset
.QuadPart
= StorageImpl_GetBigBlockOffset(This
, blockIndex
);
3614 ulOffset
.QuadPart
+= offset
;
3616 value
= htole32(value
);
3617 StorageImpl_WriteAt(This
, ulOffset
, &value
, sizeof(DWORD
), &wrote
);
3618 return (wrote
== sizeof(DWORD
));
3621 /******************************************************************************
3622 * Storage32Impl_SmallBlocksToBigBlocks
3624 * This method will convert a small block chain to a big block chain.
3625 * The small block chain will be destroyed.
3627 static BlockChainStream
* Storage32Impl_SmallBlocksToBigBlocks(
3629 SmallBlockChainStream
** ppsbChain
)
3631 ULONG bbHeadOfChain
= BLOCK_END_OF_CHAIN
;
3632 ULARGE_INTEGER size
, offset
;
3633 ULONG cbRead
, cbWritten
;
3634 ULARGE_INTEGER cbTotalRead
;
3635 DirRef streamEntryRef
;
3636 HRESULT resWrite
= S_OK
;
3638 DirEntry streamEntry
;
3640 BlockChainStream
*bbTempChain
= NULL
;
3641 BlockChainStream
*bigBlockChain
= NULL
;
3644 * Create a temporary big block chain that doesn't have
3645 * an associated directory entry. This temporary chain will be
3646 * used to copy data from small blocks to big blocks.
3648 bbTempChain
= BlockChainStream_Construct(This
,
3651 if(!bbTempChain
) return NULL
;
3653 * Grow the big block chain.
3655 size
= SmallBlockChainStream_GetSize(*ppsbChain
);
3656 BlockChainStream_SetSize(bbTempChain
, size
);
3659 * Copy the contents of the small block chain to the big block chain
3660 * by small block size increments.
3662 offset
.u
.LowPart
= 0;
3663 offset
.u
.HighPart
= 0;
3664 cbTotalRead
.QuadPart
= 0;
3666 buffer
= HeapAlloc(GetProcessHeap(),0,DEF_SMALL_BLOCK_SIZE
);
3669 resRead
= SmallBlockChainStream_ReadAt(*ppsbChain
,
3671 min(This
->smallBlockSize
, size
.u
.LowPart
- offset
.u
.LowPart
),
3674 if (FAILED(resRead
))
3679 cbTotalRead
.QuadPart
+= cbRead
;
3681 resWrite
= BlockChainStream_WriteAt(bbTempChain
,
3687 if (FAILED(resWrite
))
3690 offset
.u
.LowPart
+= cbRead
;
3694 resRead
= STG_E_READFAULT
;
3697 } while (cbTotalRead
.QuadPart
< size
.QuadPart
);
3698 HeapFree(GetProcessHeap(),0,buffer
);
3700 size
.u
.HighPart
= 0;
3703 if (FAILED(resRead
) || FAILED(resWrite
))
3705 ERR("conversion failed: resRead = 0x%08x, resWrite = 0x%08x\n", resRead
, resWrite
);
3706 BlockChainStream_SetSize(bbTempChain
, size
);
3707 BlockChainStream_Destroy(bbTempChain
);
3712 * Destroy the small block chain.
3714 streamEntryRef
= (*ppsbChain
)->ownerDirEntry
;
3715 SmallBlockChainStream_SetSize(*ppsbChain
, size
);
3716 SmallBlockChainStream_Destroy(*ppsbChain
);
3720 * Change the directory entry. This chain is now a big block chain
3721 * and it doesn't reside in the small blocks chain anymore.
3723 StorageImpl_ReadDirEntry(This
, streamEntryRef
, &streamEntry
);
3725 streamEntry
.startingBlock
= bbHeadOfChain
;
3727 StorageImpl_WriteDirEntry(This
, streamEntryRef
, &streamEntry
);
3730 * Destroy the temporary entryless big block chain.
3731 * Create a new big block chain associated with this entry.
3733 BlockChainStream_Destroy(bbTempChain
);
3734 bigBlockChain
= BlockChainStream_Construct(This
,
3738 return bigBlockChain
;
3741 /******************************************************************************
3742 * Storage32Impl_BigBlocksToSmallBlocks
3744 * This method will convert a big block chain to a small block chain.
3745 * The big block chain will be destroyed on success.
3747 static SmallBlockChainStream
* Storage32Impl_BigBlocksToSmallBlocks(
3749 BlockChainStream
** ppbbChain
,
3750 ULARGE_INTEGER newSize
)
3752 ULARGE_INTEGER size
, offset
, cbTotalRead
;
3753 ULONG cbRead
, cbWritten
, sbHeadOfChain
= BLOCK_END_OF_CHAIN
;
3754 DirRef streamEntryRef
;
3755 HRESULT resWrite
= S_OK
, resRead
= S_OK
;
3756 DirEntry streamEntry
;
3758 SmallBlockChainStream
* sbTempChain
;
3760 TRACE("%p %p\n", This
, ppbbChain
);
3762 sbTempChain
= SmallBlockChainStream_Construct(This
, &sbHeadOfChain
,
3768 SmallBlockChainStream_SetSize(sbTempChain
, newSize
);
3769 size
= BlockChainStream_GetSize(*ppbbChain
);
3770 size
.QuadPart
= min(size
.QuadPart
, newSize
.QuadPart
);
3772 offset
.u
.HighPart
= 0;
3773 offset
.u
.LowPart
= 0;
3774 cbTotalRead
.QuadPart
= 0;
3775 buffer
= HeapAlloc(GetProcessHeap(), 0, This
->bigBlockSize
);
3776 while(cbTotalRead
.QuadPart
< size
.QuadPart
)
3778 resRead
= BlockChainStream_ReadAt(*ppbbChain
, offset
,
3779 min(This
->bigBlockSize
, size
.u
.LowPart
- offset
.u
.LowPart
),
3787 cbTotalRead
.QuadPart
+= cbRead
;
3789 resWrite
= SmallBlockChainStream_WriteAt(sbTempChain
, offset
,
3790 cbRead
, buffer
, &cbWritten
);
3792 if(FAILED(resWrite
))
3795 offset
.u
.LowPart
+= cbRead
;
3799 resRead
= STG_E_READFAULT
;
3803 HeapFree(GetProcessHeap(), 0, buffer
);
3805 size
.u
.HighPart
= 0;
3808 if(FAILED(resRead
) || FAILED(resWrite
))
3810 ERR("conversion failed: resRead = 0x%08x, resWrite = 0x%08x\n", resRead
, resWrite
);
3811 SmallBlockChainStream_SetSize(sbTempChain
, size
);
3812 SmallBlockChainStream_Destroy(sbTempChain
);
3816 /* destroy the original big block chain */
3817 streamEntryRef
= (*ppbbChain
)->ownerDirEntry
;
3818 BlockChainStream_SetSize(*ppbbChain
, size
);
3819 BlockChainStream_Destroy(*ppbbChain
);
3822 StorageImpl_ReadDirEntry(This
, streamEntryRef
, &streamEntry
);
3823 streamEntry
.startingBlock
= sbHeadOfChain
;
3824 StorageImpl_WriteDirEntry(This
, streamEntryRef
, &streamEntry
);
3826 SmallBlockChainStream_Destroy(sbTempChain
);
3827 return SmallBlockChainStream_Construct(This
, NULL
, streamEntryRef
);
3830 /******************************************************************************
3831 * Storage32Impl_AddBlockDepot
3833 * This will create a depot block, essentially it is a block initialized
3836 static void Storage32Impl_AddBlockDepot(StorageImpl
* This
, ULONG blockIndex
, ULONG depotIndex
)
3838 BYTE blockBuffer
[MAX_BIG_BLOCK_SIZE
];
3839 ULONG rangeLockIndex
= RANGELOCK_FIRST
/ This
->bigBlockSize
- 1;
3840 ULONG blocksPerDepot
= This
->bigBlockSize
/ sizeof(ULONG
);
3841 ULONG rangeLockDepot
= rangeLockIndex
/ blocksPerDepot
;
3844 * Initialize blocks as free
3846 memset(blockBuffer
, BLOCK_UNUSED
, This
->bigBlockSize
);
3848 /* Reserve the range lock sector */
3849 if (depotIndex
== rangeLockDepot
)
3851 ((ULONG
*)blockBuffer
)[rangeLockIndex
% blocksPerDepot
] = BLOCK_END_OF_CHAIN
;
3854 StorageImpl_WriteBigBlock(This
, blockIndex
, blockBuffer
);
3857 /******************************************************************************
3858 * Storage32Impl_GetExtDepotBlock
3860 * Returns the index of the block that corresponds to the specified depot
3861 * index. This method is only for depot indexes equal or greater than
3862 * COUNT_BBDEPOTINHEADER.
3864 static ULONG
Storage32Impl_GetExtDepotBlock(StorageImpl
* This
, ULONG depotIndex
)
3866 ULONG depotBlocksPerExtBlock
= (This
->bigBlockSize
/ sizeof(ULONG
)) - 1;
3867 ULONG numExtBlocks
= depotIndex
- COUNT_BBDEPOTINHEADER
;
3868 ULONG extBlockCount
= numExtBlocks
/ depotBlocksPerExtBlock
;
3869 ULONG extBlockOffset
= numExtBlocks
% depotBlocksPerExtBlock
;
3870 ULONG blockIndex
= BLOCK_UNUSED
;
3871 ULONG extBlockIndex
;
3872 BYTE depotBuffer
[MAX_BIG_BLOCK_SIZE
];
3873 int index
, num_blocks
;
3875 assert(depotIndex
>= COUNT_BBDEPOTINHEADER
);
3877 if (extBlockCount
>= This
->extBigBlockDepotCount
)
3878 return BLOCK_UNUSED
;
3880 if (This
->indexExtBlockDepotCached
!= extBlockCount
)
3882 extBlockIndex
= This
->extBigBlockDepotLocations
[extBlockCount
];
3884 StorageImpl_ReadBigBlock(This
, extBlockIndex
, depotBuffer
, NULL
);
3886 num_blocks
= This
->bigBlockSize
/ 4;
3888 for (index
= 0; index
< num_blocks
; index
++)
3890 StorageUtl_ReadDWord(depotBuffer
, index
*sizeof(ULONG
), &blockIndex
);
3891 This
->extBlockDepotCached
[index
] = blockIndex
;
3894 This
->indexExtBlockDepotCached
= extBlockCount
;
3897 blockIndex
= This
->extBlockDepotCached
[extBlockOffset
];
3902 /******************************************************************************
3903 * Storage32Impl_SetExtDepotBlock
3905 * Associates the specified block index to the specified depot index.
3906 * This method is only for depot indexes equal or greater than
3907 * COUNT_BBDEPOTINHEADER.
3909 static void Storage32Impl_SetExtDepotBlock(StorageImpl
* This
, ULONG depotIndex
, ULONG blockIndex
)
3911 ULONG depotBlocksPerExtBlock
= (This
->bigBlockSize
/ sizeof(ULONG
)) - 1;
3912 ULONG numExtBlocks
= depotIndex
- COUNT_BBDEPOTINHEADER
;
3913 ULONG extBlockCount
= numExtBlocks
/ depotBlocksPerExtBlock
;
3914 ULONG extBlockOffset
= numExtBlocks
% depotBlocksPerExtBlock
;
3915 ULONG extBlockIndex
;
3917 assert(depotIndex
>= COUNT_BBDEPOTINHEADER
);
3919 assert(extBlockCount
< This
->extBigBlockDepotCount
);
3921 extBlockIndex
= This
->extBigBlockDepotLocations
[extBlockCount
];
3923 if (extBlockIndex
!= BLOCK_UNUSED
)
3925 StorageImpl_WriteDWordToBigBlock(This
, extBlockIndex
,
3926 extBlockOffset
* sizeof(ULONG
),
3930 if (This
->indexExtBlockDepotCached
== extBlockCount
)
3932 This
->extBlockDepotCached
[extBlockOffset
] = blockIndex
;
3936 /******************************************************************************
3937 * Storage32Impl_AddExtBlockDepot
3939 * Creates an extended depot block.
3941 static ULONG
Storage32Impl_AddExtBlockDepot(StorageImpl
* This
)
3943 ULONG numExtBlocks
= This
->extBigBlockDepotCount
;
3944 ULONG nextExtBlock
= This
->extBigBlockDepotStart
;
3945 BYTE depotBuffer
[MAX_BIG_BLOCK_SIZE
];
3946 ULONG index
= BLOCK_UNUSED
;
3947 ULONG nextBlockOffset
= This
->bigBlockSize
- sizeof(ULONG
);
3948 ULONG blocksPerDepotBlock
= This
->bigBlockSize
/ sizeof(ULONG
);
3949 ULONG depotBlocksPerExtBlock
= blocksPerDepotBlock
- 1;
3951 index
= (COUNT_BBDEPOTINHEADER
+ (numExtBlocks
* depotBlocksPerExtBlock
)) *
3952 blocksPerDepotBlock
;
3954 if ((numExtBlocks
== 0) && (nextExtBlock
== BLOCK_END_OF_CHAIN
))
3957 * The first extended block.
3959 This
->extBigBlockDepotStart
= index
;
3964 * Find the last existing extended block.
3966 nextExtBlock
= This
->extBigBlockDepotLocations
[This
->extBigBlockDepotCount
-1];
3969 * Add the new extended block to the chain.
3971 StorageImpl_WriteDWordToBigBlock(This
, nextExtBlock
, nextBlockOffset
,
3976 * Initialize this block.
3978 memset(depotBuffer
, BLOCK_UNUSED
, This
->bigBlockSize
);
3979 StorageImpl_WriteBigBlock(This
, index
, depotBuffer
);
3981 /* Add the block to our cache. */
3982 if (This
->extBigBlockDepotLocationsSize
== numExtBlocks
)
3984 ULONG new_cache_size
= (This
->extBigBlockDepotLocationsSize
+1)*2;
3985 ULONG
*new_cache
= HeapAlloc(GetProcessHeap(), 0, sizeof(ULONG
) * new_cache_size
);
3987 memcpy(new_cache
, This
->extBigBlockDepotLocations
, sizeof(ULONG
) * This
->extBigBlockDepotLocationsSize
);
3988 HeapFree(GetProcessHeap(), 0, This
->extBigBlockDepotLocations
);
3990 This
->extBigBlockDepotLocations
= new_cache
;
3991 This
->extBigBlockDepotLocationsSize
= new_cache_size
;
3993 This
->extBigBlockDepotLocations
[numExtBlocks
] = index
;
3998 /************************************************************************
3999 * StorageImpl_GetNextBlockInChain
4001 * This method will retrieve the block index of the next big block in
4004 * Params: This - Pointer to the Storage object.
4005 * blockIndex - Index of the block to retrieve the chain
4007 * nextBlockIndex - receives the return value.
4009 * Returns: This method returns the index of the next block in the chain.
4010 * It will return the constants:
4011 * BLOCK_SPECIAL - If the block given was not part of a
4013 * BLOCK_END_OF_CHAIN - If the block given was the last in
4015 * BLOCK_UNUSED - If the block given was not past of a chain
4017 * BLOCK_EXTBBDEPOT - This block is part of the extended
4020 * See Windows documentation for more details on IStorage methods.
4022 static HRESULT
StorageImpl_GetNextBlockInChain(
4025 ULONG
* nextBlockIndex
)
4027 ULONG offsetInDepot
= blockIndex
* sizeof (ULONG
);
4028 ULONG depotBlockCount
= offsetInDepot
/ This
->bigBlockSize
;
4029 ULONG depotBlockOffset
= offsetInDepot
% This
->bigBlockSize
;
4030 BYTE depotBuffer
[MAX_BIG_BLOCK_SIZE
];
4032 ULONG depotBlockIndexPos
;
4033 int index
, num_blocks
;
4035 *nextBlockIndex
= BLOCK_SPECIAL
;
4037 if(depotBlockCount
>= This
->bigBlockDepotCount
)
4039 WARN("depotBlockCount %d, bigBlockDepotCount %d\n", depotBlockCount
,
4040 This
->bigBlockDepotCount
);
4041 return STG_E_READFAULT
;
4045 * Cache the currently accessed depot block.
4047 if (depotBlockCount
!= This
->indexBlockDepotCached
)
4049 This
->indexBlockDepotCached
= depotBlockCount
;
4051 if (depotBlockCount
< COUNT_BBDEPOTINHEADER
)
4053 depotBlockIndexPos
= This
->bigBlockDepotStart
[depotBlockCount
];
4058 * We have to look in the extended depot.
4060 depotBlockIndexPos
= Storage32Impl_GetExtDepotBlock(This
, depotBlockCount
);
4063 StorageImpl_ReadBigBlock(This
, depotBlockIndexPos
, depotBuffer
, &read
);
4066 return STG_E_READFAULT
;
4068 num_blocks
= This
->bigBlockSize
/ 4;
4070 for (index
= 0; index
< num_blocks
; index
++)
4072 StorageUtl_ReadDWord(depotBuffer
, index
*sizeof(ULONG
), nextBlockIndex
);
4073 This
->blockDepotCached
[index
] = *nextBlockIndex
;
4077 *nextBlockIndex
= This
->blockDepotCached
[depotBlockOffset
/sizeof(ULONG
)];
4082 /******************************************************************************
4083 * Storage32Impl_GetNextExtendedBlock
4085 * Given an extended block this method will return the next extended block.
4088 * The last ULONG of an extended block is the block index of the next
4089 * extended block. Extended blocks are marked as BLOCK_EXTBBDEPOT in the
4093 * - The index of the next extended block
4094 * - BLOCK_UNUSED: there is no next extended block.
4095 * - Any other return values denotes failure.
4097 static ULONG
Storage32Impl_GetNextExtendedBlock(StorageImpl
* This
, ULONG blockIndex
)
4099 ULONG nextBlockIndex
= BLOCK_SPECIAL
;
4100 ULONG depotBlockOffset
= This
->bigBlockSize
- sizeof(ULONG
);
4102 StorageImpl_ReadDWordFromBigBlock(This
, blockIndex
, depotBlockOffset
,
4105 return nextBlockIndex
;
4108 /******************************************************************************
4109 * StorageImpl_SetNextBlockInChain
4111 * This method will write the index of the specified block's next block
4112 * in the big block depot.
4114 * For example: to create the chain 3 -> 1 -> 7 -> End of Chain
4117 * StorageImpl_SetNextBlockInChain(This, 3, 1);
4118 * StorageImpl_SetNextBlockInChain(This, 1, 7);
4119 * StorageImpl_SetNextBlockInChain(This, 7, BLOCK_END_OF_CHAIN);
4122 static void StorageImpl_SetNextBlockInChain(
4127 ULONG offsetInDepot
= blockIndex
* sizeof (ULONG
);
4128 ULONG depotBlockCount
= offsetInDepot
/ This
->bigBlockSize
;
4129 ULONG depotBlockOffset
= offsetInDepot
% This
->bigBlockSize
;
4130 ULONG depotBlockIndexPos
;
4132 assert(depotBlockCount
< This
->bigBlockDepotCount
);
4133 assert(blockIndex
!= nextBlock
);
4135 if (blockIndex
== (RANGELOCK_FIRST
/ This
->bigBlockSize
) - 1)
4136 /* This should never happen (storage file format spec forbids it), but
4137 * older versions of Wine may have generated broken files. We don't want to
4138 * assert and potentially lose data, but we do want to know if this ever
4139 * happens in a newly-created file. */
4140 ERR("Using range lock page\n");
4142 if (depotBlockCount
< COUNT_BBDEPOTINHEADER
)
4144 depotBlockIndexPos
= This
->bigBlockDepotStart
[depotBlockCount
];
4149 * We have to look in the extended depot.
4151 depotBlockIndexPos
= Storage32Impl_GetExtDepotBlock(This
, depotBlockCount
);
4154 StorageImpl_WriteDWordToBigBlock(This
, depotBlockIndexPos
, depotBlockOffset
,
4157 * Update the cached block depot, if necessary.
4159 if (depotBlockCount
== This
->indexBlockDepotCached
)
4161 This
->blockDepotCached
[depotBlockOffset
/sizeof(ULONG
)] = nextBlock
;
4165 /******************************************************************************
4166 * StorageImpl_GetNextFreeBigBlock
4168 * Returns the index of the next free big block.
4169 * If the big block depot is filled, this method will enlarge it.
4172 static ULONG
StorageImpl_GetNextFreeBigBlock(
4175 ULONG depotBlockIndexPos
;
4176 BYTE depotBuffer
[MAX_BIG_BLOCK_SIZE
];
4177 ULONG depotBlockOffset
;
4178 ULONG blocksPerDepot
= This
->bigBlockSize
/ sizeof(ULONG
);
4179 ULONG nextBlockIndex
= BLOCK_SPECIAL
;
4181 ULONG freeBlock
= BLOCK_UNUSED
;
4183 ULARGE_INTEGER neededSize
;
4186 depotIndex
= This
->prevFreeBlock
/ blocksPerDepot
;
4187 depotBlockOffset
= (This
->prevFreeBlock
% blocksPerDepot
) * sizeof(ULONG
);
4190 * Scan the entire big block depot until we find a block marked free
4192 while (nextBlockIndex
!= BLOCK_UNUSED
)
4194 if (depotIndex
< COUNT_BBDEPOTINHEADER
)
4196 depotBlockIndexPos
= This
->bigBlockDepotStart
[depotIndex
];
4199 * Grow the primary depot.
4201 if (depotBlockIndexPos
== BLOCK_UNUSED
)
4203 depotBlockIndexPos
= depotIndex
*blocksPerDepot
;
4206 * Add a block depot.
4208 Storage32Impl_AddBlockDepot(This
, depotBlockIndexPos
, depotIndex
);
4209 This
->bigBlockDepotCount
++;
4210 This
->bigBlockDepotStart
[depotIndex
] = depotBlockIndexPos
;
4213 * Flag it as a block depot.
4215 StorageImpl_SetNextBlockInChain(This
,
4219 /* Save new header information.
4221 StorageImpl_SaveFileHeader(This
);
4226 depotBlockIndexPos
= Storage32Impl_GetExtDepotBlock(This
, depotIndex
);
4228 if (depotBlockIndexPos
== BLOCK_UNUSED
)
4231 * Grow the extended depot.
4233 ULONG extIndex
= BLOCK_UNUSED
;
4234 ULONG numExtBlocks
= depotIndex
- COUNT_BBDEPOTINHEADER
;
4235 ULONG extBlockOffset
= numExtBlocks
% (blocksPerDepot
- 1);
4237 if (extBlockOffset
== 0)
4239 /* We need an extended block.
4241 extIndex
= Storage32Impl_AddExtBlockDepot(This
);
4242 This
->extBigBlockDepotCount
++;
4243 depotBlockIndexPos
= extIndex
+ 1;
4246 depotBlockIndexPos
= depotIndex
* blocksPerDepot
;
4249 * Add a block depot and mark it in the extended block.
4251 Storage32Impl_AddBlockDepot(This
, depotBlockIndexPos
, depotIndex
);
4252 This
->bigBlockDepotCount
++;
4253 Storage32Impl_SetExtDepotBlock(This
, depotIndex
, depotBlockIndexPos
);
4255 /* Flag the block depot.
4257 StorageImpl_SetNextBlockInChain(This
,
4261 /* If necessary, flag the extended depot block.
4263 if (extIndex
!= BLOCK_UNUSED
)
4264 StorageImpl_SetNextBlockInChain(This
, extIndex
, BLOCK_EXTBBDEPOT
);
4266 /* Save header information.
4268 StorageImpl_SaveFileHeader(This
);
4272 StorageImpl_ReadBigBlock(This
, depotBlockIndexPos
, depotBuffer
, &read
);
4276 while ( ( (depotBlockOffset
/sizeof(ULONG
) ) < blocksPerDepot
) &&
4277 ( nextBlockIndex
!= BLOCK_UNUSED
))
4279 StorageUtl_ReadDWord(depotBuffer
, depotBlockOffset
, &nextBlockIndex
);
4281 if (nextBlockIndex
== BLOCK_UNUSED
)
4283 freeBlock
= (depotIndex
* blocksPerDepot
) +
4284 (depotBlockOffset
/sizeof(ULONG
));
4287 depotBlockOffset
+= sizeof(ULONG
);
4292 depotBlockOffset
= 0;
4296 * make sure that the block physically exists before using it
4298 neededSize
.QuadPart
= StorageImpl_GetBigBlockOffset(This
, freeBlock
)+This
->bigBlockSize
;
4300 ILockBytes_Stat(This
->lockBytes
, &statstg
, STATFLAG_NONAME
);
4302 if (neededSize
.QuadPart
> statstg
.cbSize
.QuadPart
)
4303 ILockBytes_SetSize(This
->lockBytes
, neededSize
);
4305 This
->prevFreeBlock
= freeBlock
;
4310 /******************************************************************************
4311 * StorageImpl_FreeBigBlock
4313 * This method will flag the specified block as free in the big block depot.
4315 static void StorageImpl_FreeBigBlock(
4319 StorageImpl_SetNextBlockInChain(This
, blockIndex
, BLOCK_UNUSED
);
4321 if (blockIndex
< This
->prevFreeBlock
)
4322 This
->prevFreeBlock
= blockIndex
;
4326 static HRESULT
StorageImpl_BaseWriteDirEntry(StorageBaseImpl
*base
,
4327 DirRef index
, const DirEntry
*data
)
4329 StorageImpl
*This
= (StorageImpl
*)base
;
4330 return StorageImpl_WriteDirEntry(This
, index
, data
);
4333 static HRESULT
StorageImpl_BaseReadDirEntry(StorageBaseImpl
*base
,
4334 DirRef index
, DirEntry
*data
)
4336 StorageImpl
*This
= (StorageImpl
*)base
;
4337 return StorageImpl_ReadDirEntry(This
, index
, data
);
4340 static BlockChainStream
**StorageImpl_GetFreeBlockChainCacheEntry(StorageImpl
* This
)
4344 for (i
=0; i
<BLOCKCHAIN_CACHE_SIZE
; i
++)
4346 if (!This
->blockChainCache
[i
])
4348 return &This
->blockChainCache
[i
];
4352 i
= This
->blockChainToEvict
;
4354 BlockChainStream_Destroy(This
->blockChainCache
[i
]);
4355 This
->blockChainCache
[i
] = NULL
;
4357 This
->blockChainToEvict
++;
4358 if (This
->blockChainToEvict
== BLOCKCHAIN_CACHE_SIZE
)
4359 This
->blockChainToEvict
= 0;
4361 return &This
->blockChainCache
[i
];
4364 static BlockChainStream
**StorageImpl_GetCachedBlockChainStream(StorageImpl
*This
,
4367 int i
, free_index
=-1;
4369 for (i
=0; i
<BLOCKCHAIN_CACHE_SIZE
; i
++)
4371 if (!This
->blockChainCache
[i
])
4373 if (free_index
== -1) free_index
= i
;
4375 else if (This
->blockChainCache
[i
]->ownerDirEntry
== index
)
4377 return &This
->blockChainCache
[i
];
4381 if (free_index
== -1)
4383 free_index
= This
->blockChainToEvict
;
4385 BlockChainStream_Destroy(This
->blockChainCache
[free_index
]);
4386 This
->blockChainCache
[free_index
] = NULL
;
4388 This
->blockChainToEvict
++;
4389 if (This
->blockChainToEvict
== BLOCKCHAIN_CACHE_SIZE
)
4390 This
->blockChainToEvict
= 0;
4393 This
->blockChainCache
[free_index
] = BlockChainStream_Construct(This
, NULL
, index
);
4394 return &This
->blockChainCache
[free_index
];
4397 static void StorageImpl_DeleteCachedBlockChainStream(StorageImpl
*This
, DirRef index
)
4401 for (i
=0; i
<BLOCKCHAIN_CACHE_SIZE
; i
++)
4403 if (This
->blockChainCache
[i
] && This
->blockChainCache
[i
]->ownerDirEntry
== index
)
4405 BlockChainStream_Destroy(This
->blockChainCache
[i
]);
4406 This
->blockChainCache
[i
] = NULL
;
4412 static HRESULT
StorageImpl_StreamReadAt(StorageBaseImpl
*base
, DirRef index
,
4413 ULARGE_INTEGER offset
, ULONG size
, void *buffer
, ULONG
*bytesRead
)
4415 StorageImpl
*This
= (StorageImpl
*)base
;
4420 hr
= StorageImpl_ReadDirEntry(This
, index
, &data
);
4421 if (FAILED(hr
)) return hr
;
4423 if (data
.size
.QuadPart
== 0)
4429 if (offset
.QuadPart
+ size
> data
.size
.QuadPart
)
4431 bytesToRead
= data
.size
.QuadPart
- offset
.QuadPart
;
4438 if (data
.size
.QuadPart
< LIMIT_TO_USE_SMALL_BLOCK
)
4440 SmallBlockChainStream
*stream
;
4442 stream
= SmallBlockChainStream_Construct(This
, NULL
, index
);
4443 if (!stream
) return E_OUTOFMEMORY
;
4445 hr
= SmallBlockChainStream_ReadAt(stream
, offset
, bytesToRead
, buffer
, bytesRead
);
4447 SmallBlockChainStream_Destroy(stream
);
4453 BlockChainStream
*stream
= NULL
;
4455 stream
= *StorageImpl_GetCachedBlockChainStream(This
, index
);
4456 if (!stream
) return E_OUTOFMEMORY
;
4458 hr
= BlockChainStream_ReadAt(stream
, offset
, bytesToRead
, buffer
, bytesRead
);
4464 static HRESULT
StorageImpl_StreamSetSize(StorageBaseImpl
*base
, DirRef index
,
4465 ULARGE_INTEGER newsize
)
4467 StorageImpl
*This
= (StorageImpl
*)base
;
4470 SmallBlockChainStream
*smallblock
=NULL
;
4471 BlockChainStream
**pbigblock
=NULL
, *bigblock
=NULL
;
4473 hr
= StorageImpl_ReadDirEntry(This
, index
, &data
);
4474 if (FAILED(hr
)) return hr
;
4476 /* In simple mode keep the stream size above the small block limit */
4477 if (This
->base
.openFlags
& STGM_SIMPLE
)
4478 newsize
.QuadPart
= max(newsize
.QuadPart
, LIMIT_TO_USE_SMALL_BLOCK
);
4480 if (data
.size
.QuadPart
== newsize
.QuadPart
)
4483 /* Create a block chain object of the appropriate type */
4484 if (data
.size
.QuadPart
== 0)
4486 if (newsize
.QuadPart
< LIMIT_TO_USE_SMALL_BLOCK
)
4488 smallblock
= SmallBlockChainStream_Construct(This
, NULL
, index
);
4489 if (!smallblock
) return E_OUTOFMEMORY
;
4493 pbigblock
= StorageImpl_GetCachedBlockChainStream(This
, index
);
4494 bigblock
= *pbigblock
;
4495 if (!bigblock
) return E_OUTOFMEMORY
;
4498 else if (data
.size
.QuadPart
< LIMIT_TO_USE_SMALL_BLOCK
)
4500 smallblock
= SmallBlockChainStream_Construct(This
, NULL
, index
);
4501 if (!smallblock
) return E_OUTOFMEMORY
;
4505 pbigblock
= StorageImpl_GetCachedBlockChainStream(This
, index
);
4506 bigblock
= *pbigblock
;
4507 if (!bigblock
) return E_OUTOFMEMORY
;
4510 /* Change the block chain type if necessary. */
4511 if (smallblock
&& newsize
.QuadPart
>= LIMIT_TO_USE_SMALL_BLOCK
)
4513 bigblock
= Storage32Impl_SmallBlocksToBigBlocks(This
, &smallblock
);
4516 SmallBlockChainStream_Destroy(smallblock
);
4520 pbigblock
= StorageImpl_GetFreeBlockChainCacheEntry(This
);
4521 *pbigblock
= bigblock
;
4523 else if (bigblock
&& newsize
.QuadPart
< LIMIT_TO_USE_SMALL_BLOCK
)
4525 smallblock
= Storage32Impl_BigBlocksToSmallBlocks(This
, pbigblock
, newsize
);
4530 /* Set the size of the block chain. */
4533 SmallBlockChainStream_SetSize(smallblock
, newsize
);
4534 SmallBlockChainStream_Destroy(smallblock
);
4538 BlockChainStream_SetSize(bigblock
, newsize
);
4541 /* Set the size in the directory entry. */
4542 hr
= StorageImpl_ReadDirEntry(This
, index
, &data
);
4545 data
.size
= newsize
;
4547 hr
= StorageImpl_WriteDirEntry(This
, index
, &data
);
4552 static HRESULT
StorageImpl_StreamWriteAt(StorageBaseImpl
*base
, DirRef index
,
4553 ULARGE_INTEGER offset
, ULONG size
, const void *buffer
, ULONG
*bytesWritten
)
4555 StorageImpl
*This
= (StorageImpl
*)base
;
4558 ULARGE_INTEGER newSize
;
4560 hr
= StorageImpl_ReadDirEntry(This
, index
, &data
);
4561 if (FAILED(hr
)) return hr
;
4563 /* Grow the stream if necessary */
4564 newSize
.QuadPart
= offset
.QuadPart
+ size
;
4566 if (newSize
.QuadPart
> data
.size
.QuadPart
)
4568 hr
= StorageImpl_StreamSetSize(base
, index
, newSize
);
4572 hr
= StorageImpl_ReadDirEntry(This
, index
, &data
);
4573 if (FAILED(hr
)) return hr
;
4576 if (data
.size
.QuadPart
< LIMIT_TO_USE_SMALL_BLOCK
)
4578 SmallBlockChainStream
*stream
;
4580 stream
= SmallBlockChainStream_Construct(This
, NULL
, index
);
4581 if (!stream
) return E_OUTOFMEMORY
;
4583 hr
= SmallBlockChainStream_WriteAt(stream
, offset
, size
, buffer
, bytesWritten
);
4585 SmallBlockChainStream_Destroy(stream
);
4591 BlockChainStream
*stream
;
4593 stream
= *StorageImpl_GetCachedBlockChainStream(This
, index
);
4594 if (!stream
) return E_OUTOFMEMORY
;
4596 return BlockChainStream_WriteAt(stream
, offset
, size
, buffer
, bytesWritten
);
4600 static HRESULT
StorageImpl_StreamLink(StorageBaseImpl
*base
, DirRef dst
,
4603 StorageImpl
*This
= (StorageImpl
*)base
;
4604 DirEntry dst_data
, src_data
;
4607 hr
= StorageImpl_ReadDirEntry(This
, dst
, &dst_data
);
4610 hr
= StorageImpl_ReadDirEntry(This
, src
, &src_data
);
4614 StorageImpl_DeleteCachedBlockChainStream(This
, src
);
4615 dst_data
.startingBlock
= src_data
.startingBlock
;
4616 dst_data
.size
= src_data
.size
;
4618 hr
= StorageImpl_WriteDirEntry(This
, dst
, &dst_data
);
4624 static HRESULT
StorageImpl_Refresh(StorageImpl
*This
, BOOL new_object
, BOOL create
)
4627 DirEntry currentEntry
;
4628 DirRef currentEntryRef
;
4629 BlockChainStream
*blockChainStream
;
4633 ULARGE_INTEGER size
;
4634 BYTE bigBlockBuffer
[MAX_BIG_BLOCK_SIZE
];
4636 /* Discard any existing data. */
4638 ILockBytes_SetSize(This
->lockBytes
, size
);
4641 * Initialize all header variables:
4642 * - The big block depot consists of one block and it is at block 0
4643 * - The directory table starts at block 1
4644 * - There is no small block depot
4646 memset( This
->bigBlockDepotStart
,
4648 sizeof(This
->bigBlockDepotStart
));
4650 This
->bigBlockDepotCount
= 1;
4651 This
->bigBlockDepotStart
[0] = 0;
4652 This
->rootStartBlock
= 1;
4653 This
->smallBlockLimit
= LIMIT_TO_USE_SMALL_BLOCK
;
4654 This
->smallBlockDepotStart
= BLOCK_END_OF_CHAIN
;
4655 if (This
->bigBlockSize
== 4096)
4656 This
->bigBlockSizeBits
= MAX_BIG_BLOCK_SIZE_BITS
;
4658 This
->bigBlockSizeBits
= MIN_BIG_BLOCK_SIZE_BITS
;
4659 This
->smallBlockSizeBits
= DEF_SMALL_BLOCK_SIZE_BITS
;
4660 This
->extBigBlockDepotStart
= BLOCK_END_OF_CHAIN
;
4661 This
->extBigBlockDepotCount
= 0;
4663 StorageImpl_SaveFileHeader(This
);
4666 * Add one block for the big block depot and one block for the directory table
4668 size
.u
.HighPart
= 0;
4669 size
.u
.LowPart
= This
->bigBlockSize
* 3;
4670 ILockBytes_SetSize(This
->lockBytes
, size
);
4673 * Initialize the big block depot
4675 memset(bigBlockBuffer
, BLOCK_UNUSED
, This
->bigBlockSize
);
4676 StorageUtl_WriteDWord(bigBlockBuffer
, 0, BLOCK_SPECIAL
);
4677 StorageUtl_WriteDWord(bigBlockBuffer
, sizeof(ULONG
), BLOCK_END_OF_CHAIN
);
4678 StorageImpl_WriteBigBlock(This
, 0, bigBlockBuffer
);
4683 * Load the header for the file.
4685 hr
= StorageImpl_LoadFileHeader(This
);
4694 * There is no block depot cached yet.
4696 This
->indexBlockDepotCached
= 0xFFFFFFFF;
4697 This
->indexExtBlockDepotCached
= 0xFFFFFFFF;
4700 * Start searching for free blocks with block 0.
4702 This
->prevFreeBlock
= 0;
4704 This
->firstFreeSmallBlock
= 0;
4706 /* Read the extended big block depot locations. */
4707 if (This
->extBigBlockDepotCount
!= 0)
4709 ULONG current_block
= This
->extBigBlockDepotStart
;
4710 ULONG cache_size
= This
->extBigBlockDepotCount
* 2;
4713 This
->extBigBlockDepotLocations
= HeapAlloc(GetProcessHeap(), 0, sizeof(ULONG
) * cache_size
);
4714 if (!This
->extBigBlockDepotLocations
)
4716 return E_OUTOFMEMORY
;
4719 This
->extBigBlockDepotLocationsSize
= cache_size
;
4721 for (i
=0; i
<This
->extBigBlockDepotCount
; i
++)
4723 if (current_block
== BLOCK_END_OF_CHAIN
)
4725 WARN("File has too few extended big block depot blocks.\n");
4726 return STG_E_DOCFILECORRUPT
;
4728 This
->extBigBlockDepotLocations
[i
] = current_block
;
4729 current_block
= Storage32Impl_GetNextExtendedBlock(This
, current_block
);
4734 This
->extBigBlockDepotLocations
= NULL
;
4735 This
->extBigBlockDepotLocationsSize
= 0;
4739 * Create the block chain abstractions.
4741 if(!(blockChainStream
=
4742 BlockChainStream_Construct(This
, &This
->rootStartBlock
, DIRENTRY_NULL
)))
4744 return STG_E_READFAULT
;
4747 BlockChainStream_Destroy(This
->rootBlockChain
);
4748 This
->rootBlockChain
= blockChainStream
;
4750 if(!(blockChainStream
=
4751 BlockChainStream_Construct(This
, &This
->smallBlockDepotStart
,
4754 return STG_E_READFAULT
;
4757 BlockChainStream_Destroy(This
->smallBlockDepotChain
);
4758 This
->smallBlockDepotChain
= blockChainStream
;
4761 * Write the root storage entry (memory only)
4765 static const WCHAR rootentryW
[] = {'R','o','o','t',' ','E','n','t','r','y',0};
4768 * Initialize the directory table
4770 memset(&rootEntry
, 0, sizeof(rootEntry
));
4771 strcpyW(rootEntry
.name
, rootentryW
);
4772 rootEntry
.sizeOfNameString
= sizeof(rootentryW
);
4773 rootEntry
.stgType
= STGTY_ROOT
;
4774 rootEntry
.leftChild
= DIRENTRY_NULL
;
4775 rootEntry
.rightChild
= DIRENTRY_NULL
;
4776 rootEntry
.dirRootEntry
= DIRENTRY_NULL
;
4777 rootEntry
.startingBlock
= BLOCK_END_OF_CHAIN
;
4778 rootEntry
.size
.u
.HighPart
= 0;
4779 rootEntry
.size
.u
.LowPart
= 0;
4781 StorageImpl_WriteDirEntry(This
, 0, &rootEntry
);
4785 * Find the ID of the root storage.
4787 currentEntryRef
= 0;
4791 hr
= StorageImpl_ReadDirEntry(
4798 if ( (currentEntry
.sizeOfNameString
!= 0 ) &&
4799 (currentEntry
.stgType
== STGTY_ROOT
) )
4801 This
->base
.storageDirEntry
= currentEntryRef
;
4807 } while (SUCCEEDED(hr
) && (This
->base
.storageDirEntry
== DIRENTRY_NULL
) );
4811 return STG_E_READFAULT
;
4815 * Create the block chain abstraction for the small block root chain.
4817 if(!(blockChainStream
=
4818 BlockChainStream_Construct(This
, NULL
, This
->base
.storageDirEntry
)))
4820 return STG_E_READFAULT
;
4823 BlockChainStream_Destroy(This
->smallBlockRootChain
);
4824 This
->smallBlockRootChain
= blockChainStream
;
4829 for (i
=0; i
<BLOCKCHAIN_CACHE_SIZE
; i
++)
4831 BlockChainStream_Destroy(This
->blockChainCache
[i
]);
4832 This
->blockChainCache
[i
] = NULL
;
4839 static HRESULT
StorageImpl_GetTransactionSig(StorageBaseImpl
*base
,
4840 ULONG
* result
, BOOL refresh
)
4842 StorageImpl
*This
= (StorageImpl
*)base
;
4844 DWORD oldTransactionSig
= This
->transactionSig
;
4848 ULARGE_INTEGER offset
;
4852 offset
.u
.HighPart
= 0;
4853 offset
.u
.LowPart
= OFFSET_TRANSACTIONSIG
;
4854 hr
= StorageImpl_ReadAt(This
, offset
, data
, 4, &bytes_read
);
4858 StorageUtl_ReadDWord(data
, 0, &This
->transactionSig
);
4860 if (oldTransactionSig
!= This
->transactionSig
)
4862 /* Someone else wrote to this, so toss all cached information. */
4863 TRACE("signature changed\n");
4865 hr
= StorageImpl_Refresh(This
, FALSE
, FALSE
);
4869 This
->transactionSig
= oldTransactionSig
;
4873 *result
= This
->transactionSig
;
4878 static HRESULT
StorageImpl_SetTransactionSig(StorageBaseImpl
*base
,
4881 StorageImpl
*This
= (StorageImpl
*)base
;
4883 This
->transactionSig
= value
;
4884 StorageImpl_SaveFileHeader(This
);
4889 static HRESULT
StorageImpl_LockRegion(StorageImpl
*This
, ULARGE_INTEGER offset
,
4890 ULARGE_INTEGER cb
, DWORD dwLockType
, BOOL
*supported
)
4892 if ((dwLockType
& This
->locks_supported
) == 0)
4894 if (supported
) *supported
= FALSE
;
4898 if (supported
) *supported
= TRUE
;
4899 return ILockBytes_LockRegion(This
->lockBytes
, offset
, cb
, dwLockType
);
4902 static HRESULT
StorageImpl_UnlockRegion(StorageImpl
*This
, ULARGE_INTEGER offset
,
4903 ULARGE_INTEGER cb
, DWORD dwLockType
)
4905 if ((dwLockType
& This
->locks_supported
) == 0)
4908 return ILockBytes_UnlockRegion(This
->lockBytes
, offset
, cb
, dwLockType
);
4911 /* Internal function */
4912 static HRESULT
StorageImpl_LockRegionSync(StorageImpl
*This
, ULARGE_INTEGER offset
,
4913 ULARGE_INTEGER cb
, DWORD dwLockType
, BOOL
*supported
)
4917 DWORD start_time
= GetTickCount();
4918 DWORD last_sanity_check
= start_time
;
4919 ULARGE_INTEGER sanity_offset
, sanity_cb
;
4921 sanity_offset
.QuadPart
= RANGELOCK_UNK1_FIRST
;
4922 sanity_cb
.QuadPart
= RANGELOCK_UNK1_LAST
- RANGELOCK_UNK1_FIRST
+ 1;
4926 hr
= StorageImpl_LockRegion(This
, offset
, cb
, dwLockType
, supported
);
4928 if (hr
== STG_E_ACCESSDENIED
|| hr
== STG_E_LOCKVIOLATION
)
4930 DWORD current_time
= GetTickCount();
4931 if (current_time
- start_time
>= 20000)
4936 if (current_time
- last_sanity_check
>= 500)
4938 /* Any storage implementation with the file open in a
4939 * shared mode should not lock these bytes for writing. However,
4940 * some programs (LibreOffice Writer) will keep ALL bytes locked
4941 * when opening in exclusive mode. We can use a read lock to
4942 * detect this case early, and not hang a full 20 seconds.
4944 * This can collide with another attempt to open the file in
4945 * exclusive mode, but it's unlikely, and someone would fail anyway. */
4946 hr
= StorageImpl_LockRegion(This
, sanity_offset
, sanity_cb
, WINE_LOCK_READ
, NULL
);
4947 if (hr
== STG_E_ACCESSDENIED
|| hr
== STG_E_LOCKVIOLATION
)
4951 StorageImpl_UnlockRegion(This
, sanity_offset
, sanity_cb
, WINE_LOCK_READ
);
4952 hr
= STG_E_ACCESSDENIED
;
4955 last_sanity_check
= current_time
;
4958 if (delay
< 150) delay
++;
4960 } while (hr
== STG_E_ACCESSDENIED
|| hr
== STG_E_LOCKVIOLATION
);
4965 static HRESULT
StorageImpl_LockTransaction(StorageBaseImpl
*base
, BOOL write
)
4967 StorageImpl
*This
= (StorageImpl
*)base
;
4969 ULARGE_INTEGER offset
, cb
;
4973 /* Synchronous grab of second priority range, the commit lock, and the
4974 * lock-checking lock. */
4975 offset
.QuadPart
= RANGELOCK_TRANSACTION_FIRST
;
4976 cb
.QuadPart
= RANGELOCK_TRANSACTION_LAST
- RANGELOCK_TRANSACTION_FIRST
+ 1;
4980 offset
.QuadPart
= RANGELOCK_COMMIT
;
4984 hr
= StorageImpl_LockRegionSync(This
, offset
, cb
, LOCK_ONLYONCE
, NULL
);
4989 static HRESULT
StorageImpl_UnlockTransaction(StorageBaseImpl
*base
, BOOL write
)
4991 StorageImpl
*This
= (StorageImpl
*)base
;
4993 ULARGE_INTEGER offset
, cb
;
4997 offset
.QuadPart
= RANGELOCK_TRANSACTION_FIRST
;
4998 cb
.QuadPart
= RANGELOCK_TRANSACTION_LAST
- RANGELOCK_TRANSACTION_FIRST
+ 1;
5002 offset
.QuadPart
= RANGELOCK_COMMIT
;
5006 hr
= StorageImpl_UnlockRegion(This
, offset
, cb
, LOCK_ONLYONCE
);
5011 static HRESULT
StorageImpl_GetFilename(StorageBaseImpl
* iface
, LPWSTR
*result
)
5013 StorageImpl
*This
= (StorageImpl
*) iface
;
5017 hr
= ILockBytes_Stat(This
->lockBytes
, &statstg
, 0);
5019 *result
= statstg
.pwcsName
;
5024 static HRESULT
StorageImpl_CheckLockRange(StorageImpl
*This
, ULONG start
,
5025 ULONG end
, HRESULT fail_hr
)
5028 ULARGE_INTEGER offset
, cb
;
5030 offset
.QuadPart
= start
;
5031 cb
.QuadPart
= 1 + end
- start
;
5033 hr
= StorageImpl_LockRegion(This
, offset
, cb
, LOCK_ONLYONCE
, NULL
);
5034 if (SUCCEEDED(hr
)) StorageImpl_UnlockRegion(This
, offset
, cb
, LOCK_ONLYONCE
);
5042 static HRESULT
StorageImpl_LockOne(StorageImpl
*This
, ULONG start
, ULONG end
)
5046 ULARGE_INTEGER offset
, cb
;
5050 for (i
=start
; i
<=end
; i
++)
5052 offset
.QuadPart
= i
;
5053 hr
= StorageImpl_LockRegion(This
, offset
, cb
, LOCK_ONLYONCE
, NULL
);
5054 if (hr
!= STG_E_ACCESSDENIED
&& hr
!= STG_E_LOCKVIOLATION
)
5060 for (j
= 0; j
< ARRAY_SIZE(This
->locked_bytes
); j
++)
5062 if (This
->locked_bytes
[j
] == 0)
5064 This
->locked_bytes
[j
] = i
;
5073 static HRESULT
StorageImpl_GrabLocks(StorageImpl
*This
, DWORD openFlags
)
5076 ULARGE_INTEGER offset
;
5078 DWORD share_mode
= STGM_SHARE_MODE(openFlags
);
5081 if (openFlags
& STGM_NOSNAPSHOT
)
5083 /* STGM_NOSNAPSHOT implies deny write */
5084 if (share_mode
== STGM_SHARE_DENY_READ
) share_mode
= STGM_SHARE_EXCLUSIVE
;
5085 else if (share_mode
!= STGM_SHARE_EXCLUSIVE
) share_mode
= STGM_SHARE_DENY_WRITE
;
5088 /* Wrap all other locking inside a single lock so we can check ranges safely */
5089 offset
.QuadPart
= RANGELOCK_CHECKLOCKS
;
5091 hr
= StorageImpl_LockRegionSync(This
, offset
, cb
, LOCK_ONLYONCE
, &supported
);
5093 /* If the ILockBytes doesn't support locking that's ok. */
5094 if (!supported
) return S_OK
;
5095 else if (FAILED(hr
)) return hr
;
5099 /* First check for any conflicting locks. */
5100 if ((openFlags
& STGM_PRIORITY
) == STGM_PRIORITY
)
5101 hr
= StorageImpl_CheckLockRange(This
, RANGELOCK_COMMIT
, RANGELOCK_COMMIT
, STG_E_LOCKVIOLATION
);
5103 if (SUCCEEDED(hr
) && (STGM_ACCESS_MODE(openFlags
) != STGM_WRITE
))
5104 hr
= StorageImpl_CheckLockRange(This
, RANGELOCK_DENY_READ_FIRST
, RANGELOCK_DENY_READ_LAST
, STG_E_SHAREVIOLATION
);
5106 if (SUCCEEDED(hr
) && (STGM_ACCESS_MODE(openFlags
) != STGM_READ
))
5107 hr
= StorageImpl_CheckLockRange(This
, RANGELOCK_DENY_WRITE_FIRST
, RANGELOCK_DENY_WRITE_LAST
, STG_E_SHAREVIOLATION
);
5109 if (SUCCEEDED(hr
) && (share_mode
== STGM_SHARE_DENY_READ
|| share_mode
== STGM_SHARE_EXCLUSIVE
))
5110 hr
= StorageImpl_CheckLockRange(This
, RANGELOCK_READ_FIRST
, RANGELOCK_READ_LAST
, STG_E_LOCKVIOLATION
);
5112 if (SUCCEEDED(hr
) && (share_mode
== STGM_SHARE_DENY_WRITE
|| share_mode
== STGM_SHARE_EXCLUSIVE
))
5113 hr
= StorageImpl_CheckLockRange(This
, RANGELOCK_WRITE_FIRST
, RANGELOCK_WRITE_LAST
, STG_E_LOCKVIOLATION
);
5115 if (SUCCEEDED(hr
) && STGM_ACCESS_MODE(openFlags
) == STGM_READ
&& share_mode
== STGM_SHARE_EXCLUSIVE
)
5117 hr
= StorageImpl_CheckLockRange(This
, 0, RANGELOCK_CHECKLOCKS
-1, STG_E_LOCKVIOLATION
);
5120 hr
= StorageImpl_CheckLockRange(This
, RANGELOCK_CHECKLOCKS
+1, RANGELOCK_LAST
, STG_E_LOCKVIOLATION
);
5123 /* Then grab our locks. */
5124 if (SUCCEEDED(hr
) && (openFlags
& STGM_PRIORITY
) == STGM_PRIORITY
)
5126 hr
= StorageImpl_LockOne(This
, RANGELOCK_PRIORITY1_FIRST
, RANGELOCK_PRIORITY1_LAST
);
5128 hr
= StorageImpl_LockOne(This
, RANGELOCK_PRIORITY2_FIRST
, RANGELOCK_PRIORITY2_LAST
);
5131 if (SUCCEEDED(hr
) && (STGM_ACCESS_MODE(openFlags
) != STGM_WRITE
))
5132 hr
= StorageImpl_LockOne(This
, RANGELOCK_READ_FIRST
, RANGELOCK_READ_LAST
);
5134 if (SUCCEEDED(hr
) && (STGM_ACCESS_MODE(openFlags
) != STGM_READ
))
5135 hr
= StorageImpl_LockOne(This
, RANGELOCK_WRITE_FIRST
, RANGELOCK_WRITE_LAST
);
5137 if (SUCCEEDED(hr
) && (share_mode
== STGM_SHARE_DENY_READ
|| share_mode
== STGM_SHARE_EXCLUSIVE
))
5138 hr
= StorageImpl_LockOne(This
, RANGELOCK_DENY_READ_FIRST
, RANGELOCK_DENY_READ_LAST
);
5140 if (SUCCEEDED(hr
) && (share_mode
== STGM_SHARE_DENY_WRITE
|| share_mode
== STGM_SHARE_EXCLUSIVE
))
5141 hr
= StorageImpl_LockOne(This
, RANGELOCK_DENY_WRITE_FIRST
, RANGELOCK_DENY_WRITE_LAST
);
5143 if (SUCCEEDED(hr
) && (openFlags
& STGM_NOSNAPSHOT
) == STGM_NOSNAPSHOT
)
5144 hr
= StorageImpl_LockOne(This
, RANGELOCK_NOSNAPSHOT_FIRST
, RANGELOCK_NOSNAPSHOT_LAST
);
5146 offset
.QuadPart
= RANGELOCK_CHECKLOCKS
;
5148 StorageImpl_UnlockRegion(This
, offset
, cb
, LOCK_ONLYONCE
);
5153 static HRESULT
StorageImpl_Flush(StorageBaseImpl
*storage
)
5155 StorageImpl
*This
= (StorageImpl
*)storage
;
5158 TRACE("(%p)\n", This
);
5160 hr
= BlockChainStream_Flush(This
->smallBlockRootChain
);
5163 hr
= BlockChainStream_Flush(This
->rootBlockChain
);
5166 hr
= BlockChainStream_Flush(This
->smallBlockDepotChain
);
5168 for (i
=0; SUCCEEDED(hr
) && i
<BLOCKCHAIN_CACHE_SIZE
; i
++)
5169 if (This
->blockChainCache
[i
])
5170 hr
= BlockChainStream_Flush(This
->blockChainCache
[i
]);
5173 hr
= ILockBytes_Flush(This
->lockBytes
);
5178 static void StorageImpl_Invalidate(StorageBaseImpl
* iface
)
5180 StorageImpl
*This
= (StorageImpl
*) iface
;
5182 StorageBaseImpl_DeleteAll(&This
->base
);
5184 This
->base
.reverted
= TRUE
;
5187 static void StorageImpl_Destroy(StorageBaseImpl
* iface
)
5189 StorageImpl
*This
= (StorageImpl
*) iface
;
5191 TRACE("(%p)\n", This
);
5193 StorageImpl_Flush(iface
);
5195 StorageImpl_Invalidate(iface
);
5197 HeapFree(GetProcessHeap(), 0, This
->extBigBlockDepotLocations
);
5199 BlockChainStream_Destroy(This
->smallBlockRootChain
);
5200 BlockChainStream_Destroy(This
->rootBlockChain
);
5201 BlockChainStream_Destroy(This
->smallBlockDepotChain
);
5203 for (i
= 0; i
< BLOCKCHAIN_CACHE_SIZE
; i
++)
5204 BlockChainStream_Destroy(This
->blockChainCache
[i
]);
5206 for (i
= 0; i
< ARRAY_SIZE(This
->locked_bytes
); i
++)
5208 ULARGE_INTEGER offset
, cb
;
5210 if (This
->locked_bytes
[i
] != 0)
5212 offset
.QuadPart
= This
->locked_bytes
[i
];
5213 StorageImpl_UnlockRegion(This
, offset
, cb
, LOCK_ONLYONCE
);
5217 if (This
->lockBytes
)
5218 ILockBytes_Release(This
->lockBytes
);
5219 HeapFree(GetProcessHeap(), 0, This
);
5223 static const StorageBaseImplVtbl StorageImpl_BaseVtbl
=
5225 StorageImpl_Destroy
,
5226 StorageImpl_Invalidate
,
5228 StorageImpl_GetFilename
,
5229 StorageImpl_CreateDirEntry
,
5230 StorageImpl_BaseWriteDirEntry
,
5231 StorageImpl_BaseReadDirEntry
,
5232 StorageImpl_DestroyDirEntry
,
5233 StorageImpl_StreamReadAt
,
5234 StorageImpl_StreamWriteAt
,
5235 StorageImpl_StreamSetSize
,
5236 StorageImpl_StreamLink
,
5237 StorageImpl_GetTransactionSig
,
5238 StorageImpl_SetTransactionSig
,
5239 StorageImpl_LockTransaction
,
5240 StorageImpl_UnlockTransaction
5245 * Virtual function table for the IStorageBaseImpl class.
5247 static const IStorageVtbl StorageImpl_Vtbl
=
5249 StorageBaseImpl_QueryInterface
,
5250 StorageBaseImpl_AddRef
,
5251 StorageBaseImpl_Release
,
5252 StorageBaseImpl_CreateStream
,
5253 StorageBaseImpl_OpenStream
,
5254 StorageBaseImpl_CreateStorage
,
5255 StorageBaseImpl_OpenStorage
,
5256 StorageBaseImpl_CopyTo
,
5257 StorageBaseImpl_MoveElementTo
,
5258 StorageBaseImpl_Commit
,
5259 StorageBaseImpl_Revert
,
5260 StorageBaseImpl_EnumElements
,
5261 StorageBaseImpl_DestroyElement
,
5262 StorageBaseImpl_RenameElement
,
5263 StorageBaseImpl_SetElementTimes
,
5264 StorageBaseImpl_SetClass
,
5265 StorageBaseImpl_SetStateBits
,
5266 StorageBaseImpl_Stat
5269 static HRESULT
StorageImpl_Construct(
5277 StorageImpl
** result
)
5283 if ( FAILED( validateSTGM(openFlags
) ))
5284 return STG_E_INVALIDFLAG
;
5286 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(StorageImpl
));
5288 return E_OUTOFMEMORY
;
5290 memset(This
, 0, sizeof(StorageImpl
));
5292 list_init(&This
->base
.strmHead
);
5294 list_init(&This
->base
.storageHead
);
5296 This
->base
.IStorage_iface
.lpVtbl
= &StorageImpl_Vtbl
;
5297 This
->base
.IPropertySetStorage_iface
.lpVtbl
= &IPropertySetStorage_Vtbl
;
5298 This
->base
.IDirectWriterLock_iface
.lpVtbl
= &DirectWriterLockVtbl
;
5299 This
->base
.baseVtbl
= &StorageImpl_BaseVtbl
;
5300 This
->base
.openFlags
= (openFlags
& ~STGM_CREATE
);
5302 This
->base
.create
= create
;
5304 if (openFlags
== (STGM_DIRECT_SWMR
|STGM_READWRITE
|STGM_SHARE_DENY_WRITE
))
5305 This
->base
.lockingrole
= SWMR_Writer
;
5306 else if (openFlags
== (STGM_DIRECT_SWMR
|STGM_READ
|STGM_SHARE_DENY_NONE
))
5307 This
->base
.lockingrole
= SWMR_Reader
;
5309 This
->base
.lockingrole
= SWMR_None
;
5311 This
->base
.reverted
= FALSE
;
5314 * Initialize the big block cache.
5316 This
->bigBlockSize
= sector_size
;
5317 This
->smallBlockSize
= DEF_SMALL_BLOCK_SIZE
;
5319 hr
= FileLockBytesImpl_Construct(hFile
, openFlags
, pwcsName
, &This
->lockBytes
);
5322 This
->lockBytes
= pLkbyt
;
5323 ILockBytes_AddRef(pLkbyt
);
5327 hr
= ILockBytes_Stat(This
->lockBytes
, &stat
, STATFLAG_NONAME
);
5331 This
->locks_supported
= stat
.grfLocksSupported
;
5333 /* Don't try to use wine-internal locking flag with custom ILockBytes */
5334 This
->locks_supported
&= ~WINE_LOCK_READ
;
5336 hr
= StorageImpl_GrabLocks(This
, openFlags
);
5340 hr
= StorageImpl_Refresh(This
, TRUE
, create
);
5344 IStorage_Release(&This
->base
.IStorage_iface
);
5349 StorageImpl_Flush(&This
->base
);
5357 /************************************************************************
5358 * StorageInternalImpl implementation
5359 ***********************************************************************/
5361 static void StorageInternalImpl_Invalidate( StorageBaseImpl
*base
)
5363 StorageInternalImpl
* This
= (StorageInternalImpl
*) base
;
5365 if (!This
->base
.reverted
)
5367 TRACE("Storage invalidated (stg=%p)\n", This
);
5369 This
->base
.reverted
= TRUE
;
5371 This
->parentStorage
= NULL
;
5373 StorageBaseImpl_DeleteAll(&This
->base
);
5375 list_remove(&This
->ParentListEntry
);
5379 static void StorageInternalImpl_Destroy( StorageBaseImpl
*iface
)
5381 StorageInternalImpl
* This
= (StorageInternalImpl
*) iface
;
5383 StorageInternalImpl_Invalidate(&This
->base
);
5385 HeapFree(GetProcessHeap(), 0, This
);
5388 static HRESULT
StorageInternalImpl_Flush(StorageBaseImpl
* iface
)
5390 StorageInternalImpl
* This
= (StorageInternalImpl
*) iface
;
5392 return StorageBaseImpl_Flush(This
->parentStorage
);
5395 static HRESULT
StorageInternalImpl_GetFilename(StorageBaseImpl
* iface
, LPWSTR
*result
)
5397 StorageInternalImpl
* This
= (StorageInternalImpl
*) iface
;
5399 return StorageBaseImpl_GetFilename(This
->parentStorage
, result
);
5402 static HRESULT
StorageInternalImpl_CreateDirEntry(StorageBaseImpl
*base
,
5403 const DirEntry
*newData
, DirRef
*index
)
5405 StorageInternalImpl
* This
= (StorageInternalImpl
*) base
;
5407 return StorageBaseImpl_CreateDirEntry(This
->parentStorage
,
5411 static HRESULT
StorageInternalImpl_WriteDirEntry(StorageBaseImpl
*base
,
5412 DirRef index
, const DirEntry
*data
)
5414 StorageInternalImpl
* This
= (StorageInternalImpl
*) base
;
5416 return StorageBaseImpl_WriteDirEntry(This
->parentStorage
,
5420 static HRESULT
StorageInternalImpl_ReadDirEntry(StorageBaseImpl
*base
,
5421 DirRef index
, DirEntry
*data
)
5423 StorageInternalImpl
* This
= (StorageInternalImpl
*) base
;
5425 return StorageBaseImpl_ReadDirEntry(This
->parentStorage
,
5429 static HRESULT
StorageInternalImpl_DestroyDirEntry(StorageBaseImpl
*base
,
5432 StorageInternalImpl
* This
= (StorageInternalImpl
*) base
;
5434 return StorageBaseImpl_DestroyDirEntry(This
->parentStorage
,
5438 static HRESULT
StorageInternalImpl_StreamReadAt(StorageBaseImpl
*base
,
5439 DirRef index
, ULARGE_INTEGER offset
, ULONG size
, void *buffer
, ULONG
*bytesRead
)
5441 StorageInternalImpl
* This
= (StorageInternalImpl
*) base
;
5443 return StorageBaseImpl_StreamReadAt(This
->parentStorage
,
5444 index
, offset
, size
, buffer
, bytesRead
);
5447 static HRESULT
StorageInternalImpl_StreamWriteAt(StorageBaseImpl
*base
,
5448 DirRef index
, ULARGE_INTEGER offset
, ULONG size
, const void *buffer
, ULONG
*bytesWritten
)
5450 StorageInternalImpl
* This
= (StorageInternalImpl
*) base
;
5452 return StorageBaseImpl_StreamWriteAt(This
->parentStorage
,
5453 index
, offset
, size
, buffer
, bytesWritten
);
5456 static HRESULT
StorageInternalImpl_StreamSetSize(StorageBaseImpl
*base
,
5457 DirRef index
, ULARGE_INTEGER newsize
)
5459 StorageInternalImpl
* This
= (StorageInternalImpl
*) base
;
5461 return StorageBaseImpl_StreamSetSize(This
->parentStorage
,
5465 static HRESULT
StorageInternalImpl_StreamLink(StorageBaseImpl
*base
,
5466 DirRef dst
, DirRef src
)
5468 StorageInternalImpl
* This
= (StorageInternalImpl
*) base
;
5470 return StorageBaseImpl_StreamLink(This
->parentStorage
,
5474 static HRESULT
StorageInternalImpl_GetTransactionSig(StorageBaseImpl
*base
,
5475 ULONG
* result
, BOOL refresh
)
5480 static HRESULT
StorageInternalImpl_SetTransactionSig(StorageBaseImpl
*base
,
5486 static HRESULT
StorageInternalImpl_LockTransaction(StorageBaseImpl
*base
, BOOL write
)
5491 static HRESULT
StorageInternalImpl_UnlockTransaction(StorageBaseImpl
*base
, BOOL write
)
5496 /******************************************************************************
5498 ** StorageInternalImpl_Commit
5501 static HRESULT WINAPI
StorageInternalImpl_Commit(
5503 DWORD grfCommitFlags
) /* [in] */
5505 StorageBaseImpl
* This
= impl_from_IStorage(iface
);
5506 TRACE("(%p,%x)\n", iface
, grfCommitFlags
);
5507 return StorageBaseImpl_Flush(This
);
5510 /******************************************************************************
5512 ** StorageInternalImpl_Revert
5515 static HRESULT WINAPI
StorageInternalImpl_Revert(
5518 FIXME("(%p): stub\n", iface
);
5523 * Virtual function table for the StorageInternalImpl class.
5525 static const IStorageVtbl StorageInternalImpl_Vtbl
=
5527 StorageBaseImpl_QueryInterface
,
5528 StorageBaseImpl_AddRef
,
5529 StorageBaseImpl_Release
,
5530 StorageBaseImpl_CreateStream
,
5531 StorageBaseImpl_OpenStream
,
5532 StorageBaseImpl_CreateStorage
,
5533 StorageBaseImpl_OpenStorage
,
5534 StorageBaseImpl_CopyTo
,
5535 StorageBaseImpl_MoveElementTo
,
5536 StorageInternalImpl_Commit
,
5537 StorageInternalImpl_Revert
,
5538 StorageBaseImpl_EnumElements
,
5539 StorageBaseImpl_DestroyElement
,
5540 StorageBaseImpl_RenameElement
,
5541 StorageBaseImpl_SetElementTimes
,
5542 StorageBaseImpl_SetClass
,
5543 StorageBaseImpl_SetStateBits
,
5544 StorageBaseImpl_Stat
5547 static const StorageBaseImplVtbl StorageInternalImpl_BaseVtbl
=
5549 StorageInternalImpl_Destroy
,
5550 StorageInternalImpl_Invalidate
,
5551 StorageInternalImpl_Flush
,
5552 StorageInternalImpl_GetFilename
,
5553 StorageInternalImpl_CreateDirEntry
,
5554 StorageInternalImpl_WriteDirEntry
,
5555 StorageInternalImpl_ReadDirEntry
,
5556 StorageInternalImpl_DestroyDirEntry
,
5557 StorageInternalImpl_StreamReadAt
,
5558 StorageInternalImpl_StreamWriteAt
,
5559 StorageInternalImpl_StreamSetSize
,
5560 StorageInternalImpl_StreamLink
,
5561 StorageInternalImpl_GetTransactionSig
,
5562 StorageInternalImpl_SetTransactionSig
,
5563 StorageInternalImpl_LockTransaction
,
5564 StorageInternalImpl_UnlockTransaction
5567 static StorageInternalImpl
* StorageInternalImpl_Construct(
5568 StorageBaseImpl
* parentStorage
,
5570 DirRef storageDirEntry
)
5572 StorageInternalImpl
* newStorage
;
5574 newStorage
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(StorageInternalImpl
));
5578 list_init(&newStorage
->base
.strmHead
);
5580 list_init(&newStorage
->base
.storageHead
);
5583 * Initialize the virtual function table.
5585 newStorage
->base
.IStorage_iface
.lpVtbl
= &StorageInternalImpl_Vtbl
;
5586 newStorage
->base
.IPropertySetStorage_iface
.lpVtbl
= &IPropertySetStorage_Vtbl
;
5587 newStorage
->base
.baseVtbl
= &StorageInternalImpl_BaseVtbl
;
5588 newStorage
->base
.openFlags
= (openFlags
& ~STGM_CREATE
);
5590 newStorage
->base
.reverted
= FALSE
;
5592 newStorage
->base
.ref
= 1;
5594 newStorage
->parentStorage
= parentStorage
;
5597 * Keep a reference to the directory entry of this storage
5599 newStorage
->base
.storageDirEntry
= storageDirEntry
;
5601 newStorage
->base
.create
= FALSE
;
5610 /************************************************************************
5611 * TransactedSnapshotImpl implementation
5612 ***********************************************************************/
5614 static DirRef
TransactedSnapshotImpl_FindFreeEntry(TransactedSnapshotImpl
*This
)
5616 DirRef result
=This
->firstFreeEntry
;
5618 while (result
< This
->entries_size
&& This
->entries
[result
].inuse
)
5621 if (result
== This
->entries_size
)
5623 ULONG new_size
= This
->entries_size
* 2;
5624 TransactedDirEntry
*new_entries
;
5626 new_entries
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(TransactedDirEntry
) * new_size
);
5627 if (!new_entries
) return DIRENTRY_NULL
;
5629 memcpy(new_entries
, This
->entries
, sizeof(TransactedDirEntry
) * This
->entries_size
);
5630 HeapFree(GetProcessHeap(), 0, This
->entries
);
5632 This
->entries
= new_entries
;
5633 This
->entries_size
= new_size
;
5636 This
->entries
[result
].inuse
= TRUE
;
5638 This
->firstFreeEntry
= result
+1;
5643 static DirRef
TransactedSnapshotImpl_CreateStubEntry(
5644 TransactedSnapshotImpl
*This
, DirRef parentEntryRef
)
5646 DirRef stubEntryRef
;
5647 TransactedDirEntry
*entry
;
5649 stubEntryRef
= TransactedSnapshotImpl_FindFreeEntry(This
);
5651 if (stubEntryRef
!= DIRENTRY_NULL
)
5653 entry
= &This
->entries
[stubEntryRef
];
5655 entry
->newTransactedParentEntry
= entry
->transactedParentEntry
= parentEntryRef
;
5657 entry
->read
= FALSE
;
5660 return stubEntryRef
;
5663 static HRESULT
TransactedSnapshotImpl_EnsureReadEntry(
5664 TransactedSnapshotImpl
*This
, DirRef entry
)
5669 if (!This
->entries
[entry
].read
)
5671 hr
= StorageBaseImpl_ReadDirEntry(This
->transactedParent
,
5672 This
->entries
[entry
].transactedParentEntry
,
5675 if (SUCCEEDED(hr
) && data
.leftChild
!= DIRENTRY_NULL
)
5677 data
.leftChild
= TransactedSnapshotImpl_CreateStubEntry(This
, data
.leftChild
);
5679 if (data
.leftChild
== DIRENTRY_NULL
)
5683 if (SUCCEEDED(hr
) && data
.rightChild
!= DIRENTRY_NULL
)
5685 data
.rightChild
= TransactedSnapshotImpl_CreateStubEntry(This
, data
.rightChild
);
5687 if (data
.rightChild
== DIRENTRY_NULL
)
5691 if (SUCCEEDED(hr
) && data
.dirRootEntry
!= DIRENTRY_NULL
)
5693 data
.dirRootEntry
= TransactedSnapshotImpl_CreateStubEntry(This
, data
.dirRootEntry
);
5695 if (data
.dirRootEntry
== DIRENTRY_NULL
)
5701 memcpy(&This
->entries
[entry
].data
, &data
, sizeof(DirEntry
));
5702 This
->entries
[entry
].read
= TRUE
;
5709 static HRESULT
TransactedSnapshotImpl_MakeStreamDirty(
5710 TransactedSnapshotImpl
*This
, DirRef entry
)
5714 if (!This
->entries
[entry
].stream_dirty
)
5716 DirEntry new_entrydata
;
5718 memset(&new_entrydata
, 0, sizeof(DirEntry
));
5719 new_entrydata
.name
[0] = 'S';
5720 new_entrydata
.sizeOfNameString
= 1;
5721 new_entrydata
.stgType
= STGTY_STREAM
;
5722 new_entrydata
.startingBlock
= BLOCK_END_OF_CHAIN
;
5723 new_entrydata
.leftChild
= DIRENTRY_NULL
;
5724 new_entrydata
.rightChild
= DIRENTRY_NULL
;
5725 new_entrydata
.dirRootEntry
= DIRENTRY_NULL
;
5727 hr
= StorageBaseImpl_CreateDirEntry(This
->scratch
, &new_entrydata
,
5728 &This
->entries
[entry
].stream_entry
);
5730 if (SUCCEEDED(hr
) && This
->entries
[entry
].transactedParentEntry
!= DIRENTRY_NULL
)
5732 hr
= StorageBaseImpl_CopyStream(
5733 This
->scratch
, This
->entries
[entry
].stream_entry
,
5734 This
->transactedParent
, This
->entries
[entry
].transactedParentEntry
);
5737 StorageBaseImpl_DestroyDirEntry(This
->scratch
, This
->entries
[entry
].stream_entry
);
5741 This
->entries
[entry
].stream_dirty
= TRUE
;
5743 if (This
->entries
[entry
].transactedParentEntry
!= DIRENTRY_NULL
)
5745 /* Since this entry is modified, and we aren't using its stream data, we
5746 * no longer care about the original entry. */
5748 delete_ref
= TransactedSnapshotImpl_CreateStubEntry(This
, This
->entries
[entry
].transactedParentEntry
);
5750 if (delete_ref
!= DIRENTRY_NULL
)
5751 This
->entries
[delete_ref
].deleted
= TRUE
;
5753 This
->entries
[entry
].transactedParentEntry
= This
->entries
[entry
].newTransactedParentEntry
= DIRENTRY_NULL
;
5760 /* Find the first entry in a depth-first traversal. */
5761 static DirRef
TransactedSnapshotImpl_FindFirstChild(
5762 TransactedSnapshotImpl
* This
, DirRef parent
)
5764 DirRef cursor
, prev
;
5765 TransactedDirEntry
*entry
;
5768 entry
= &This
->entries
[cursor
];
5771 if (entry
->data
.leftChild
!= DIRENTRY_NULL
)
5774 cursor
= entry
->data
.leftChild
;
5775 entry
= &This
->entries
[cursor
];
5776 entry
->parent
= prev
;
5778 else if (entry
->data
.rightChild
!= DIRENTRY_NULL
)
5781 cursor
= entry
->data
.rightChild
;
5782 entry
= &This
->entries
[cursor
];
5783 entry
->parent
= prev
;
5785 else if (entry
->data
.dirRootEntry
!= DIRENTRY_NULL
)
5788 cursor
= entry
->data
.dirRootEntry
;
5789 entry
= &This
->entries
[cursor
];
5790 entry
->parent
= prev
;
5799 /* Find the next entry in a depth-first traversal. */
5800 static DirRef
TransactedSnapshotImpl_FindNextChild(
5801 TransactedSnapshotImpl
* This
, DirRef current
)
5804 TransactedDirEntry
*parent_entry
;
5806 parent
= This
->entries
[current
].parent
;
5807 parent_entry
= &This
->entries
[parent
];
5809 if (parent
!= DIRENTRY_NULL
&& parent_entry
->data
.dirRootEntry
!= current
)
5811 if (parent_entry
->data
.rightChild
!= current
&& parent_entry
->data
.rightChild
!= DIRENTRY_NULL
)
5813 This
->entries
[parent_entry
->data
.rightChild
].parent
= parent
;
5814 return TransactedSnapshotImpl_FindFirstChild(This
, parent_entry
->data
.rightChild
);
5817 if (parent_entry
->data
.dirRootEntry
!= DIRENTRY_NULL
)
5819 This
->entries
[parent_entry
->data
.dirRootEntry
].parent
= parent
;
5820 return TransactedSnapshotImpl_FindFirstChild(This
, parent_entry
->data
.dirRootEntry
);
5827 /* Return TRUE if we've made a copy of this entry for committing to the parent. */
5828 static inline BOOL
TransactedSnapshotImpl_MadeCopy(
5829 TransactedSnapshotImpl
* This
, DirRef entry
)
5831 return entry
!= DIRENTRY_NULL
&&
5832 This
->entries
[entry
].newTransactedParentEntry
!= This
->entries
[entry
].transactedParentEntry
;
5835 /* Destroy the entries created by CopyTree. */
5836 static void TransactedSnapshotImpl_DestroyTemporaryCopy(
5837 TransactedSnapshotImpl
* This
, DirRef stop
)
5840 TransactedDirEntry
*entry
;
5841 ULARGE_INTEGER zero
;
5845 if (!This
->entries
[This
->base
.storageDirEntry
].read
)
5848 cursor
= This
->entries
[This
->base
.storageDirEntry
].data
.dirRootEntry
;
5850 if (cursor
== DIRENTRY_NULL
)
5853 cursor
= TransactedSnapshotImpl_FindFirstChild(This
, cursor
);
5855 while (cursor
!= DIRENTRY_NULL
&& cursor
!= stop
)
5857 if (TransactedSnapshotImpl_MadeCopy(This
, cursor
))
5859 entry
= &This
->entries
[cursor
];
5861 if (entry
->stream_dirty
)
5862 StorageBaseImpl_StreamSetSize(This
->transactedParent
,
5863 entry
->newTransactedParentEntry
, zero
);
5865 StorageBaseImpl_DestroyDirEntry(This
->transactedParent
,
5866 entry
->newTransactedParentEntry
);
5868 entry
->newTransactedParentEntry
= entry
->transactedParentEntry
;
5871 cursor
= TransactedSnapshotImpl_FindNextChild(This
, cursor
);
5875 /* Make a copy of our edited tree that we can use in the parent. */
5876 static HRESULT
TransactedSnapshotImpl_CopyTree(TransactedSnapshotImpl
* This
)
5879 TransactedDirEntry
*entry
;
5882 cursor
= This
->base
.storageDirEntry
;
5883 entry
= &This
->entries
[cursor
];
5884 entry
->parent
= DIRENTRY_NULL
;
5885 entry
->newTransactedParentEntry
= entry
->transactedParentEntry
;
5887 if (entry
->data
.dirRootEntry
== DIRENTRY_NULL
)
5890 This
->entries
[entry
->data
.dirRootEntry
].parent
= DIRENTRY_NULL
;
5892 cursor
= TransactedSnapshotImpl_FindFirstChild(This
, entry
->data
.dirRootEntry
);
5893 entry
= &This
->entries
[cursor
];
5895 while (cursor
!= DIRENTRY_NULL
)
5897 /* Make a copy of this entry in the transacted parent. */
5899 (!entry
->dirty
&& !entry
->stream_dirty
&&
5900 !TransactedSnapshotImpl_MadeCopy(This
, entry
->data
.leftChild
) &&
5901 !TransactedSnapshotImpl_MadeCopy(This
, entry
->data
.rightChild
) &&
5902 !TransactedSnapshotImpl_MadeCopy(This
, entry
->data
.dirRootEntry
)))
5903 entry
->newTransactedParentEntry
= entry
->transactedParentEntry
;
5908 memcpy(&newData
, &entry
->data
, sizeof(DirEntry
));
5910 newData
.size
.QuadPart
= 0;
5911 newData
.startingBlock
= BLOCK_END_OF_CHAIN
;
5913 if (newData
.leftChild
!= DIRENTRY_NULL
)
5914 newData
.leftChild
= This
->entries
[newData
.leftChild
].newTransactedParentEntry
;
5916 if (newData
.rightChild
!= DIRENTRY_NULL
)
5917 newData
.rightChild
= This
->entries
[newData
.rightChild
].newTransactedParentEntry
;
5919 if (newData
.dirRootEntry
!= DIRENTRY_NULL
)
5920 newData
.dirRootEntry
= This
->entries
[newData
.dirRootEntry
].newTransactedParentEntry
;
5922 hr
= StorageBaseImpl_CreateDirEntry(This
->transactedParent
, &newData
,
5923 &entry
->newTransactedParentEntry
);
5926 TransactedSnapshotImpl_DestroyTemporaryCopy(This
, cursor
);
5930 if (entry
->stream_dirty
)
5932 hr
= StorageBaseImpl_CopyStream(
5933 This
->transactedParent
, entry
->newTransactedParentEntry
,
5934 This
->scratch
, entry
->stream_entry
);
5936 else if (entry
->data
.size
.QuadPart
)
5938 hr
= StorageBaseImpl_StreamLink(
5939 This
->transactedParent
, entry
->newTransactedParentEntry
,
5940 entry
->transactedParentEntry
);
5945 cursor
= TransactedSnapshotImpl_FindNextChild(This
, cursor
);
5946 TransactedSnapshotImpl_DestroyTemporaryCopy(This
, cursor
);
5951 cursor
= TransactedSnapshotImpl_FindNextChild(This
, cursor
);
5952 entry
= &This
->entries
[cursor
];
5958 static HRESULT WINAPI
TransactedSnapshotImpl_Commit(
5960 DWORD grfCommitFlags
) /* [in] */
5962 TransactedSnapshotImpl
* This
= (TransactedSnapshotImpl
*)impl_from_IStorage(iface
);
5963 TransactedDirEntry
*root_entry
;
5964 DirRef i
, dir_root_ref
;
5966 ULARGE_INTEGER zero
;
5968 ULONG transactionSig
;
5972 TRACE("(%p,%x)\n", iface
, grfCommitFlags
);
5974 /* Cannot commit a read-only transacted storage */
5975 if ( STGM_ACCESS_MODE( This
->base
.openFlags
) == STGM_READ
)
5976 return STG_E_ACCESSDENIED
;
5978 hr
= StorageBaseImpl_LockTransaction(This
->transactedParent
, TRUE
);
5979 if (hr
== E_NOTIMPL
) hr
= S_OK
;
5982 hr
= StorageBaseImpl_GetTransactionSig(This
->transactedParent
, &transactionSig
, TRUE
);
5985 if (transactionSig
!= This
->lastTransactionSig
)
5987 ERR("file was externally modified\n");
5988 hr
= STG_E_NOTCURRENT
;
5993 This
->lastTransactionSig
= transactionSig
+1;
5994 hr
= StorageBaseImpl_SetTransactionSig(This
->transactedParent
, This
->lastTransactionSig
);
5997 else if (hr
== E_NOTIMPL
)
6000 if (FAILED(hr
)) goto end
;
6002 /* To prevent data loss, we create the new structure in the file before we
6003 * delete the old one, so that in case of errors the old data is intact. We
6004 * shouldn't do this if STGC_OVERWRITE is set, but that flag should only be
6005 * needed in the rare situation where we have just enough free disk space to
6006 * overwrite the existing data. */
6008 root_entry
= &This
->entries
[This
->base
.storageDirEntry
];
6010 if (!root_entry
->read
)
6013 hr
= TransactedSnapshotImpl_CopyTree(This
);
6014 if (FAILED(hr
)) goto end
;
6016 if (root_entry
->data
.dirRootEntry
== DIRENTRY_NULL
)
6017 dir_root_ref
= DIRENTRY_NULL
;
6019 dir_root_ref
= This
->entries
[root_entry
->data
.dirRootEntry
].newTransactedParentEntry
;
6021 hr
= StorageBaseImpl_Flush(This
->transactedParent
);
6023 /* Update the storage to use the new data in one step. */
6025 hr
= StorageBaseImpl_ReadDirEntry(This
->transactedParent
,
6026 root_entry
->transactedParentEntry
, &data
);
6030 data
.dirRootEntry
= dir_root_ref
;
6031 data
.clsid
= root_entry
->data
.clsid
;
6032 data
.ctime
= root_entry
->data
.ctime
;
6033 data
.mtime
= root_entry
->data
.mtime
;
6035 hr
= StorageBaseImpl_WriteDirEntry(This
->transactedParent
,
6036 root_entry
->transactedParentEntry
, &data
);
6039 /* Try to flush after updating the root storage, but if the flush fails, keep
6040 * going, on the theory that it'll either succeed later or the subsequent
6041 * writes will fail. */
6042 StorageBaseImpl_Flush(This
->transactedParent
);
6046 /* Destroy the old now-orphaned data. */
6047 for (i
=0; i
<This
->entries_size
; i
++)
6049 TransactedDirEntry
*entry
= &This
->entries
[i
];
6054 StorageBaseImpl_StreamSetSize(This
->transactedParent
,
6055 entry
->transactedParentEntry
, zero
);
6056 StorageBaseImpl_DestroyDirEntry(This
->transactedParent
,
6057 entry
->transactedParentEntry
);
6058 memset(entry
, 0, sizeof(TransactedDirEntry
));
6059 This
->firstFreeEntry
= min(i
, This
->firstFreeEntry
);
6061 else if (entry
->read
&& entry
->transactedParentEntry
!= entry
->newTransactedParentEntry
)
6063 if (entry
->transactedParentEntry
!= DIRENTRY_NULL
)
6064 StorageBaseImpl_DestroyDirEntry(This
->transactedParent
,
6065 entry
->transactedParentEntry
);
6066 if (entry
->stream_dirty
)
6068 StorageBaseImpl_StreamSetSize(This
->scratch
, entry
->stream_entry
, zero
);
6069 StorageBaseImpl_DestroyDirEntry(This
->scratch
, entry
->stream_entry
);
6070 entry
->stream_dirty
= FALSE
;
6072 entry
->dirty
= FALSE
;
6073 entry
->transactedParentEntry
= entry
->newTransactedParentEntry
;
6080 TransactedSnapshotImpl_DestroyTemporaryCopy(This
, DIRENTRY_NULL
);
6084 hr
= StorageBaseImpl_Flush(This
->transactedParent
);
6086 StorageBaseImpl_UnlockTransaction(This
->transactedParent
, TRUE
);
6089 TRACE("<-- %08x\n", hr
);
6093 static HRESULT WINAPI
TransactedSnapshotImpl_Revert(
6096 TransactedSnapshotImpl
* This
= (TransactedSnapshotImpl
*)impl_from_IStorage(iface
);
6097 ULARGE_INTEGER zero
;
6100 TRACE("(%p)\n", iface
);
6102 /* Destroy the open objects. */
6103 StorageBaseImpl_DeleteAll(&This
->base
);
6105 /* Clear out the scratch file. */
6107 for (i
=0; i
<This
->entries_size
; i
++)
6109 if (This
->entries
[i
].stream_dirty
)
6111 StorageBaseImpl_StreamSetSize(This
->scratch
, This
->entries
[i
].stream_entry
,
6114 StorageBaseImpl_DestroyDirEntry(This
->scratch
, This
->entries
[i
].stream_entry
);
6118 memset(This
->entries
, 0, sizeof(TransactedDirEntry
) * This
->entries_size
);
6120 This
->firstFreeEntry
= 0;
6121 This
->base
.storageDirEntry
= TransactedSnapshotImpl_CreateStubEntry(This
, This
->transactedParent
->storageDirEntry
);
6126 static void TransactedSnapshotImpl_Invalidate(StorageBaseImpl
* This
)
6128 if (!This
->reverted
)
6130 TRACE("Storage invalidated (stg=%p)\n", This
);
6132 This
->reverted
= TRUE
;
6134 StorageBaseImpl_DeleteAll(This
);
6138 static void TransactedSnapshotImpl_Destroy( StorageBaseImpl
*iface
)
6140 TransactedSnapshotImpl
* This
= (TransactedSnapshotImpl
*) iface
;
6142 IStorage_Revert(&This
->base
.IStorage_iface
);
6143 IStorage_Release(&This
->transactedParent
->IStorage_iface
);
6144 IStorage_Release(&This
->scratch
->IStorage_iface
);
6145 HeapFree(GetProcessHeap(), 0, This
->entries
);
6146 HeapFree(GetProcessHeap(), 0, This
);
6149 static HRESULT
TransactedSnapshotImpl_Flush(StorageBaseImpl
* iface
)
6151 /* We only need to flush when committing. */
6155 static HRESULT
TransactedSnapshotImpl_GetFilename(StorageBaseImpl
* iface
, LPWSTR
*result
)
6157 TransactedSnapshotImpl
* This
= (TransactedSnapshotImpl
*) iface
;
6159 return StorageBaseImpl_GetFilename(This
->transactedParent
, result
);
6162 static HRESULT
TransactedSnapshotImpl_CreateDirEntry(StorageBaseImpl
*base
,
6163 const DirEntry
*newData
, DirRef
*index
)
6165 TransactedSnapshotImpl
* This
= (TransactedSnapshotImpl
*) base
;
6167 TransactedDirEntry
*new_entry
;
6169 new_ref
= TransactedSnapshotImpl_FindFreeEntry(This
);
6170 if (new_ref
== DIRENTRY_NULL
)
6171 return E_OUTOFMEMORY
;
6173 new_entry
= &This
->entries
[new_ref
];
6175 new_entry
->newTransactedParentEntry
= new_entry
->transactedParentEntry
= DIRENTRY_NULL
;
6176 new_entry
->read
= TRUE
;
6177 new_entry
->dirty
= TRUE
;
6178 memcpy(&new_entry
->data
, newData
, sizeof(DirEntry
));
6182 TRACE("%s l=%x r=%x d=%x <-- %x\n", debugstr_w(newData
->name
), newData
->leftChild
, newData
->rightChild
, newData
->dirRootEntry
, *index
);
6187 static HRESULT
TransactedSnapshotImpl_WriteDirEntry(StorageBaseImpl
*base
,
6188 DirRef index
, const DirEntry
*data
)
6190 TransactedSnapshotImpl
* This
= (TransactedSnapshotImpl
*) base
;
6193 TRACE("%x %s l=%x r=%x d=%x\n", index
, debugstr_w(data
->name
), data
->leftChild
, data
->rightChild
, data
->dirRootEntry
);
6195 hr
= TransactedSnapshotImpl_EnsureReadEntry(This
, index
);
6198 TRACE("<-- %08x\n", hr
);
6202 memcpy(&This
->entries
[index
].data
, data
, sizeof(DirEntry
));
6204 if (index
!= This
->base
.storageDirEntry
)
6206 This
->entries
[index
].dirty
= TRUE
;
6208 if (data
->size
.QuadPart
== 0 &&
6209 This
->entries
[index
].transactedParentEntry
!= DIRENTRY_NULL
)
6211 /* Since this entry is modified, and we aren't using its stream data, we
6212 * no longer care about the original entry. */
6214 delete_ref
= TransactedSnapshotImpl_CreateStubEntry(This
, This
->entries
[index
].transactedParentEntry
);
6216 if (delete_ref
!= DIRENTRY_NULL
)
6217 This
->entries
[delete_ref
].deleted
= TRUE
;
6219 This
->entries
[index
].transactedParentEntry
= This
->entries
[index
].newTransactedParentEntry
= DIRENTRY_NULL
;
6222 TRACE("<-- S_OK\n");
6226 static HRESULT
TransactedSnapshotImpl_ReadDirEntry(StorageBaseImpl
*base
,
6227 DirRef index
, DirEntry
*data
)
6229 TransactedSnapshotImpl
* This
= (TransactedSnapshotImpl
*) base
;
6232 hr
= TransactedSnapshotImpl_EnsureReadEntry(This
, index
);
6235 TRACE("<-- %08x\n", hr
);
6239 memcpy(data
, &This
->entries
[index
].data
, sizeof(DirEntry
));
6241 TRACE("%x %s l=%x r=%x d=%x\n", index
, debugstr_w(data
->name
), data
->leftChild
, data
->rightChild
, data
->dirRootEntry
);
6246 static HRESULT
TransactedSnapshotImpl_DestroyDirEntry(StorageBaseImpl
*base
,
6249 TransactedSnapshotImpl
* This
= (TransactedSnapshotImpl
*) base
;
6251 if (This
->entries
[index
].transactedParentEntry
== DIRENTRY_NULL
||
6252 This
->entries
[index
].data
.size
.QuadPart
!= 0)
6254 /* If we deleted this entry while it has stream data. We must have left the
6255 * data because some other entry is using it, and we need to leave the
6256 * original entry alone. */
6257 memset(&This
->entries
[index
], 0, sizeof(TransactedDirEntry
));
6258 This
->firstFreeEntry
= min(index
, This
->firstFreeEntry
);
6262 This
->entries
[index
].deleted
= TRUE
;
6268 static HRESULT
TransactedSnapshotImpl_StreamReadAt(StorageBaseImpl
*base
,
6269 DirRef index
, ULARGE_INTEGER offset
, ULONG size
, void *buffer
, ULONG
*bytesRead
)
6271 TransactedSnapshotImpl
* This
= (TransactedSnapshotImpl
*) base
;
6273 if (This
->entries
[index
].stream_dirty
)
6275 return StorageBaseImpl_StreamReadAt(This
->scratch
,
6276 This
->entries
[index
].stream_entry
, offset
, size
, buffer
, bytesRead
);
6278 else if (This
->entries
[index
].transactedParentEntry
== DIRENTRY_NULL
)
6280 /* This stream doesn't live in the parent, and we haven't allocated storage
6287 return StorageBaseImpl_StreamReadAt(This
->transactedParent
,
6288 This
->entries
[index
].transactedParentEntry
, offset
, size
, buffer
, bytesRead
);
6292 static HRESULT
TransactedSnapshotImpl_StreamWriteAt(StorageBaseImpl
*base
,
6293 DirRef index
, ULARGE_INTEGER offset
, ULONG size
, const void *buffer
, ULONG
*bytesWritten
)
6295 TransactedSnapshotImpl
* This
= (TransactedSnapshotImpl
*) base
;
6298 hr
= TransactedSnapshotImpl_EnsureReadEntry(This
, index
);
6301 TRACE("<-- %08x\n", hr
);
6305 hr
= TransactedSnapshotImpl_MakeStreamDirty(This
, index
);
6308 TRACE("<-- %08x\n", hr
);
6312 hr
= StorageBaseImpl_StreamWriteAt(This
->scratch
,
6313 This
->entries
[index
].stream_entry
, offset
, size
, buffer
, bytesWritten
);
6315 if (SUCCEEDED(hr
) && size
!= 0)
6316 This
->entries
[index
].data
.size
.QuadPart
= max(
6317 This
->entries
[index
].data
.size
.QuadPart
,
6318 offset
.QuadPart
+ size
);
6320 TRACE("<-- %08x\n", hr
);
6324 static HRESULT
TransactedSnapshotImpl_StreamSetSize(StorageBaseImpl
*base
,
6325 DirRef index
, ULARGE_INTEGER newsize
)
6327 TransactedSnapshotImpl
* This
= (TransactedSnapshotImpl
*) base
;
6330 hr
= TransactedSnapshotImpl_EnsureReadEntry(This
, index
);
6333 TRACE("<-- %08x\n", hr
);
6337 if (This
->entries
[index
].data
.size
.QuadPart
== newsize
.QuadPart
)
6340 if (newsize
.QuadPart
== 0)
6342 /* Destroy any parent references or entries in the scratch file. */
6343 if (This
->entries
[index
].stream_dirty
)
6345 ULARGE_INTEGER zero
;
6347 StorageBaseImpl_StreamSetSize(This
->scratch
,
6348 This
->entries
[index
].stream_entry
, zero
);
6349 StorageBaseImpl_DestroyDirEntry(This
->scratch
,
6350 This
->entries
[index
].stream_entry
);
6351 This
->entries
[index
].stream_dirty
= FALSE
;
6353 else if (This
->entries
[index
].transactedParentEntry
!= DIRENTRY_NULL
)
6356 delete_ref
= TransactedSnapshotImpl_CreateStubEntry(This
, This
->entries
[index
].transactedParentEntry
);
6358 if (delete_ref
!= DIRENTRY_NULL
)
6359 This
->entries
[delete_ref
].deleted
= TRUE
;
6361 This
->entries
[index
].transactedParentEntry
= This
->entries
[index
].newTransactedParentEntry
= DIRENTRY_NULL
;
6366 hr
= TransactedSnapshotImpl_MakeStreamDirty(This
, index
);
6367 if (FAILED(hr
)) return hr
;
6369 hr
= StorageBaseImpl_StreamSetSize(This
->scratch
,
6370 This
->entries
[index
].stream_entry
, newsize
);
6374 This
->entries
[index
].data
.size
= newsize
;
6376 TRACE("<-- %08x\n", hr
);
6380 static HRESULT
TransactedSnapshotImpl_StreamLink(StorageBaseImpl
*base
,
6381 DirRef dst
, DirRef src
)
6383 TransactedSnapshotImpl
* This
= (TransactedSnapshotImpl
*) base
;
6385 TransactedDirEntry
*dst_entry
, *src_entry
;
6387 hr
= TransactedSnapshotImpl_EnsureReadEntry(This
, src
);
6390 TRACE("<-- %08x\n", hr
);
6394 hr
= TransactedSnapshotImpl_EnsureReadEntry(This
, dst
);
6397 TRACE("<-- %08x\n", hr
);
6401 dst_entry
= &This
->entries
[dst
];
6402 src_entry
= &This
->entries
[src
];
6404 dst_entry
->stream_dirty
= src_entry
->stream_dirty
;
6405 dst_entry
->stream_entry
= src_entry
->stream_entry
;
6406 dst_entry
->transactedParentEntry
= src_entry
->transactedParentEntry
;
6407 dst_entry
->newTransactedParentEntry
= src_entry
->newTransactedParentEntry
;
6408 dst_entry
->data
.size
= src_entry
->data
.size
;
6413 static HRESULT
TransactedSnapshotImpl_GetTransactionSig(StorageBaseImpl
*base
,
6414 ULONG
* result
, BOOL refresh
)
6419 static HRESULT
TransactedSnapshotImpl_SetTransactionSig(StorageBaseImpl
*base
,
6425 static HRESULT
TransactedSnapshotImpl_LockTransaction(StorageBaseImpl
*base
, BOOL write
)
6430 static HRESULT
TransactedSnapshotImpl_UnlockTransaction(StorageBaseImpl
*base
, BOOL write
)
6435 static const IStorageVtbl TransactedSnapshotImpl_Vtbl
=
6437 StorageBaseImpl_QueryInterface
,
6438 StorageBaseImpl_AddRef
,
6439 StorageBaseImpl_Release
,
6440 StorageBaseImpl_CreateStream
,
6441 StorageBaseImpl_OpenStream
,
6442 StorageBaseImpl_CreateStorage
,
6443 StorageBaseImpl_OpenStorage
,
6444 StorageBaseImpl_CopyTo
,
6445 StorageBaseImpl_MoveElementTo
,
6446 TransactedSnapshotImpl_Commit
,
6447 TransactedSnapshotImpl_Revert
,
6448 StorageBaseImpl_EnumElements
,
6449 StorageBaseImpl_DestroyElement
,
6450 StorageBaseImpl_RenameElement
,
6451 StorageBaseImpl_SetElementTimes
,
6452 StorageBaseImpl_SetClass
,
6453 StorageBaseImpl_SetStateBits
,
6454 StorageBaseImpl_Stat
6457 static const StorageBaseImplVtbl TransactedSnapshotImpl_BaseVtbl
=
6459 TransactedSnapshotImpl_Destroy
,
6460 TransactedSnapshotImpl_Invalidate
,
6461 TransactedSnapshotImpl_Flush
,
6462 TransactedSnapshotImpl_GetFilename
,
6463 TransactedSnapshotImpl_CreateDirEntry
,
6464 TransactedSnapshotImpl_WriteDirEntry
,
6465 TransactedSnapshotImpl_ReadDirEntry
,
6466 TransactedSnapshotImpl_DestroyDirEntry
,
6467 TransactedSnapshotImpl_StreamReadAt
,
6468 TransactedSnapshotImpl_StreamWriteAt
,
6469 TransactedSnapshotImpl_StreamSetSize
,
6470 TransactedSnapshotImpl_StreamLink
,
6471 TransactedSnapshotImpl_GetTransactionSig
,
6472 TransactedSnapshotImpl_SetTransactionSig
,
6473 TransactedSnapshotImpl_LockTransaction
,
6474 TransactedSnapshotImpl_UnlockTransaction
6477 static HRESULT
TransactedSnapshotImpl_Construct(StorageBaseImpl
*parentStorage
,
6478 TransactedSnapshotImpl
** result
)
6482 *result
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(TransactedSnapshotImpl
));
6487 (*result
)->base
.IStorage_iface
.lpVtbl
= &TransactedSnapshotImpl_Vtbl
;
6489 /* This is OK because the property set storage functions use the IStorage functions. */
6490 (*result
)->base
.IPropertySetStorage_iface
.lpVtbl
= parentStorage
->IPropertySetStorage_iface
.lpVtbl
;
6491 (*result
)->base
.baseVtbl
= &TransactedSnapshotImpl_BaseVtbl
;
6493 list_init(&(*result
)->base
.strmHead
);
6495 list_init(&(*result
)->base
.storageHead
);
6497 (*result
)->base
.ref
= 1;
6499 (*result
)->base
.openFlags
= parentStorage
->openFlags
;
6501 /* This cannot fail, except with E_NOTIMPL in which case we don't care */
6502 StorageBaseImpl_GetTransactionSig(parentStorage
, &(*result
)->lastTransactionSig
, FALSE
);
6504 /* Create a new temporary storage to act as the scratch file. */
6505 hr
= StgCreateDocfile(NULL
, STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
|STGM_CREATE
|STGM_DELETEONRELEASE
,
6507 (*result
)->scratch
= impl_from_IStorage(scratch
);
6511 ULONG num_entries
= 20;
6513 (*result
)->entries
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(TransactedDirEntry
) * num_entries
);
6514 (*result
)->entries_size
= num_entries
;
6515 (*result
)->firstFreeEntry
= 0;
6517 if ((*result
)->entries
)
6519 /* parentStorage already has 1 reference, which we take over here. */
6520 (*result
)->transactedParent
= parentStorage
;
6522 parentStorage
->transactedChild
= &(*result
)->base
;
6524 (*result
)->base
.storageDirEntry
= TransactedSnapshotImpl_CreateStubEntry(*result
, parentStorage
->storageDirEntry
);
6528 IStorage_Release(scratch
);
6534 if (FAILED(hr
)) HeapFree(GetProcessHeap(), 0, *result
);
6539 return E_OUTOFMEMORY
;
6543 /************************************************************************
6544 * TransactedSharedImpl implementation
6545 ***********************************************************************/
6547 static void TransactedSharedImpl_Invalidate(StorageBaseImpl
* This
)
6549 if (!This
->reverted
)
6551 TRACE("Storage invalidated (stg=%p)\n", This
);
6553 This
->reverted
= TRUE
;
6555 StorageBaseImpl_DeleteAll(This
);
6559 static void TransactedSharedImpl_Destroy( StorageBaseImpl
*iface
)
6561 TransactedSharedImpl
* This
= (TransactedSharedImpl
*) iface
;
6563 TransactedSharedImpl_Invalidate(&This
->base
);
6564 IStorage_Release(&This
->transactedParent
->IStorage_iface
);
6565 IStorage_Release(&This
->scratch
->base
.IStorage_iface
);
6566 HeapFree(GetProcessHeap(), 0, This
);
6569 static HRESULT
TransactedSharedImpl_Flush(StorageBaseImpl
* iface
)
6571 /* We only need to flush when committing. */
6575 static HRESULT
TransactedSharedImpl_GetFilename(StorageBaseImpl
* iface
, LPWSTR
*result
)
6577 TransactedSharedImpl
* This
= (TransactedSharedImpl
*) iface
;
6579 return StorageBaseImpl_GetFilename(This
->transactedParent
, result
);
6582 static HRESULT
TransactedSharedImpl_CreateDirEntry(StorageBaseImpl
*base
,
6583 const DirEntry
*newData
, DirRef
*index
)
6585 TransactedSharedImpl
* This
= (TransactedSharedImpl
*) base
;
6587 return StorageBaseImpl_CreateDirEntry(&This
->scratch
->base
,
6591 static HRESULT
TransactedSharedImpl_WriteDirEntry(StorageBaseImpl
*base
,
6592 DirRef index
, const DirEntry
*data
)
6594 TransactedSharedImpl
* This
= (TransactedSharedImpl
*) base
;
6596 return StorageBaseImpl_WriteDirEntry(&This
->scratch
->base
,
6600 static HRESULT
TransactedSharedImpl_ReadDirEntry(StorageBaseImpl
*base
,
6601 DirRef index
, DirEntry
*data
)
6603 TransactedSharedImpl
* This
= (TransactedSharedImpl
*) base
;
6605 return StorageBaseImpl_ReadDirEntry(&This
->scratch
->base
,
6609 static HRESULT
TransactedSharedImpl_DestroyDirEntry(StorageBaseImpl
*base
,
6612 TransactedSharedImpl
* This
= (TransactedSharedImpl
*) base
;
6614 return StorageBaseImpl_DestroyDirEntry(&This
->scratch
->base
,
6618 static HRESULT
TransactedSharedImpl_StreamReadAt(StorageBaseImpl
*base
,
6619 DirRef index
, ULARGE_INTEGER offset
, ULONG size
, void *buffer
, ULONG
*bytesRead
)
6621 TransactedSharedImpl
* This
= (TransactedSharedImpl
*) base
;
6623 return StorageBaseImpl_StreamReadAt(&This
->scratch
->base
,
6624 index
, offset
, size
, buffer
, bytesRead
);
6627 static HRESULT
TransactedSharedImpl_StreamWriteAt(StorageBaseImpl
*base
,
6628 DirRef index
, ULARGE_INTEGER offset
, ULONG size
, const void *buffer
, ULONG
*bytesWritten
)
6630 TransactedSharedImpl
* This
= (TransactedSharedImpl
*) base
;
6632 return StorageBaseImpl_StreamWriteAt(&This
->scratch
->base
,
6633 index
, offset
, size
, buffer
, bytesWritten
);
6636 static HRESULT
TransactedSharedImpl_StreamSetSize(StorageBaseImpl
*base
,
6637 DirRef index
, ULARGE_INTEGER newsize
)
6639 TransactedSharedImpl
* This
= (TransactedSharedImpl
*) base
;
6641 return StorageBaseImpl_StreamSetSize(&This
->scratch
->base
,
6645 static HRESULT
TransactedSharedImpl_StreamLink(StorageBaseImpl
*base
,
6646 DirRef dst
, DirRef src
)
6648 TransactedSharedImpl
* This
= (TransactedSharedImpl
*) base
;
6650 return StorageBaseImpl_StreamLink(&This
->scratch
->base
,
6654 static HRESULT
TransactedSharedImpl_GetTransactionSig(StorageBaseImpl
*base
,
6655 ULONG
* result
, BOOL refresh
)
6660 static HRESULT
TransactedSharedImpl_SetTransactionSig(StorageBaseImpl
*base
,
6666 static HRESULT
TransactedSharedImpl_LockTransaction(StorageBaseImpl
*base
, BOOL write
)
6671 static HRESULT
TransactedSharedImpl_UnlockTransaction(StorageBaseImpl
*base
, BOOL write
)
6676 static HRESULT WINAPI
TransactedSharedImpl_Commit(
6678 DWORD grfCommitFlags
) /* [in] */
6680 TransactedSharedImpl
* This
= (TransactedSharedImpl
*)impl_from_IStorage(iface
);
6681 DirRef new_storage_ref
, prev_storage_ref
;
6682 DirEntry src_data
, dst_data
;
6684 ULONG transactionSig
;
6686 TRACE("(%p,%x)\n", iface
, grfCommitFlags
);
6688 /* Cannot commit a read-only transacted storage */
6689 if ( STGM_ACCESS_MODE( This
->base
.openFlags
) == STGM_READ
)
6690 return STG_E_ACCESSDENIED
;
6692 hr
= StorageBaseImpl_LockTransaction(This
->transactedParent
, TRUE
);
6693 if (hr
== E_NOTIMPL
) hr
= S_OK
;
6696 hr
= StorageBaseImpl_GetTransactionSig(This
->transactedParent
, &transactionSig
, TRUE
);
6699 if ((grfCommitFlags
& STGC_ONLYIFCURRENT
) && transactionSig
!= This
->lastTransactionSig
)
6700 hr
= STG_E_NOTCURRENT
;
6703 hr
= StorageBaseImpl_SetTransactionSig(This
->transactedParent
, transactionSig
+1);
6705 else if (hr
== E_NOTIMPL
)
6709 hr
= StorageBaseImpl_ReadDirEntry(&This
->scratch
->base
, This
->scratch
->base
.storageDirEntry
, &src_data
);
6711 /* FIXME: If we're current, we should be able to copy only the changes in scratch. */
6713 hr
= StorageBaseImpl_DupStorageTree(This
->transactedParent
, &new_storage_ref
, &This
->scratch
->base
, src_data
.dirRootEntry
);
6716 hr
= StorageBaseImpl_Flush(This
->transactedParent
);
6719 hr
= StorageBaseImpl_ReadDirEntry(This
->transactedParent
, This
->transactedParent
->storageDirEntry
, &dst_data
);
6723 prev_storage_ref
= dst_data
.dirRootEntry
;
6724 dst_data
.dirRootEntry
= new_storage_ref
;
6725 dst_data
.clsid
= src_data
.clsid
;
6726 dst_data
.ctime
= src_data
.ctime
;
6727 dst_data
.mtime
= src_data
.mtime
;
6728 hr
= StorageBaseImpl_WriteDirEntry(This
->transactedParent
, This
->transactedParent
->storageDirEntry
, &dst_data
);
6733 /* Try to flush after updating the root storage, but if the flush fails, keep
6734 * going, on the theory that it'll either succeed later or the subsequent
6735 * writes will fail. */
6736 StorageBaseImpl_Flush(This
->transactedParent
);
6738 hr
= StorageBaseImpl_DeleteStorageTree(This
->transactedParent
, prev_storage_ref
, TRUE
);
6742 hr
= StorageBaseImpl_Flush(This
->transactedParent
);
6744 StorageBaseImpl_UnlockTransaction(This
->transactedParent
, TRUE
);
6747 hr
= IStorage_Commit(&This
->scratch
->base
.IStorage_iface
, STGC_DEFAULT
);
6751 This
->lastTransactionSig
= transactionSig
+1;
6754 TRACE("<-- %08x\n", hr
);
6758 static HRESULT WINAPI
TransactedSharedImpl_Revert(
6761 TransactedSharedImpl
* This
= (TransactedSharedImpl
*)impl_from_IStorage(iface
);
6763 TRACE("(%p)\n", iface
);
6765 /* Destroy the open objects. */
6766 StorageBaseImpl_DeleteAll(&This
->base
);
6768 return IStorage_Revert(&This
->scratch
->base
.IStorage_iface
);
6771 static const IStorageVtbl TransactedSharedImpl_Vtbl
=
6773 StorageBaseImpl_QueryInterface
,
6774 StorageBaseImpl_AddRef
,
6775 StorageBaseImpl_Release
,
6776 StorageBaseImpl_CreateStream
,
6777 StorageBaseImpl_OpenStream
,
6778 StorageBaseImpl_CreateStorage
,
6779 StorageBaseImpl_OpenStorage
,
6780 StorageBaseImpl_CopyTo
,
6781 StorageBaseImpl_MoveElementTo
,
6782 TransactedSharedImpl_Commit
,
6783 TransactedSharedImpl_Revert
,
6784 StorageBaseImpl_EnumElements
,
6785 StorageBaseImpl_DestroyElement
,
6786 StorageBaseImpl_RenameElement
,
6787 StorageBaseImpl_SetElementTimes
,
6788 StorageBaseImpl_SetClass
,
6789 StorageBaseImpl_SetStateBits
,
6790 StorageBaseImpl_Stat
6793 static const StorageBaseImplVtbl TransactedSharedImpl_BaseVtbl
=
6795 TransactedSharedImpl_Destroy
,
6796 TransactedSharedImpl_Invalidate
,
6797 TransactedSharedImpl_Flush
,
6798 TransactedSharedImpl_GetFilename
,
6799 TransactedSharedImpl_CreateDirEntry
,
6800 TransactedSharedImpl_WriteDirEntry
,
6801 TransactedSharedImpl_ReadDirEntry
,
6802 TransactedSharedImpl_DestroyDirEntry
,
6803 TransactedSharedImpl_StreamReadAt
,
6804 TransactedSharedImpl_StreamWriteAt
,
6805 TransactedSharedImpl_StreamSetSize
,
6806 TransactedSharedImpl_StreamLink
,
6807 TransactedSharedImpl_GetTransactionSig
,
6808 TransactedSharedImpl_SetTransactionSig
,
6809 TransactedSharedImpl_LockTransaction
,
6810 TransactedSharedImpl_UnlockTransaction
6813 static HRESULT
TransactedSharedImpl_Construct(StorageBaseImpl
*parentStorage
,
6814 TransactedSharedImpl
** result
)
6818 *result
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(TransactedSharedImpl
));
6823 (*result
)->base
.IStorage_iface
.lpVtbl
= &TransactedSharedImpl_Vtbl
;
6825 /* This is OK because the property set storage functions use the IStorage functions. */
6826 (*result
)->base
.IPropertySetStorage_iface
.lpVtbl
= parentStorage
->IPropertySetStorage_iface
.lpVtbl
;
6827 (*result
)->base
.baseVtbl
= &TransactedSharedImpl_BaseVtbl
;
6829 list_init(&(*result
)->base
.strmHead
);
6831 list_init(&(*result
)->base
.storageHead
);
6833 (*result
)->base
.ref
= 1;
6835 (*result
)->base
.openFlags
= parentStorage
->openFlags
;
6837 hr
= StorageBaseImpl_LockTransaction(parentStorage
, FALSE
);
6843 /* This cannot fail, except with E_NOTIMPL in which case we don't care */
6844 StorageBaseImpl_GetTransactionSig(parentStorage
, &(*result
)->lastTransactionSig
, FALSE
);
6848 stgo
.ulSectorSize
= 4096;
6849 stgo
.pwcsTemplateFile
= NULL
;
6851 /* Create a new temporary storage to act as the scratch file. */
6852 hr
= StgCreateStorageEx(NULL
, STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
|STGM_CREATE
|STGM_DELETEONRELEASE
|STGM_TRANSACTED
,
6853 STGFMT_DOCFILE
, 0, &stgo
, NULL
, &IID_IStorage
, (void**)&scratch
);
6854 (*result
)->scratch
= (TransactedSnapshotImpl
*)impl_from_IStorage(scratch
);
6858 hr
= StorageBaseImpl_CopyStorageTree(&(*result
)->scratch
->base
, (*result
)->scratch
->base
.storageDirEntry
,
6859 parentStorage
, parentStorage
->storageDirEntry
);
6863 hr
= IStorage_Commit(scratch
, STGC_DEFAULT
);
6865 (*result
)->base
.storageDirEntry
= (*result
)->scratch
->base
.storageDirEntry
;
6866 (*result
)->transactedParent
= parentStorage
;
6870 IStorage_Release(scratch
);
6873 StorageBaseImpl_UnlockTransaction(parentStorage
, FALSE
);
6876 if (FAILED(hr
)) HeapFree(GetProcessHeap(), 0, *result
);
6881 return E_OUTOFMEMORY
;
6884 static HRESULT
Storage_ConstructTransacted(StorageBaseImpl
*parentStorage
,
6885 BOOL toplevel
, StorageBaseImpl
** result
)
6887 static int fixme_flags
=STGM_NOSCRATCH
|STGM_NOSNAPSHOT
;
6889 if (parentStorage
->openFlags
& fixme_flags
)
6891 fixme_flags
&= ~parentStorage
->openFlags
;
6892 FIXME("Unimplemented flags %x\n", parentStorage
->openFlags
);
6895 if (toplevel
&& !(parentStorage
->openFlags
& STGM_NOSNAPSHOT
) &&
6896 STGM_SHARE_MODE(parentStorage
->openFlags
) != STGM_SHARE_DENY_WRITE
&&
6897 STGM_SHARE_MODE(parentStorage
->openFlags
) != STGM_SHARE_EXCLUSIVE
)
6899 /* Need to create a temp file for the snapshot */
6900 return TransactedSharedImpl_Construct(parentStorage
, (TransactedSharedImpl
**)result
);
6903 return TransactedSnapshotImpl_Construct(parentStorage
,
6904 (TransactedSnapshotImpl
**)result
);
6907 static HRESULT
Storage_Construct(
6915 StorageBaseImpl
** result
)
6917 StorageImpl
*newStorage
;
6918 StorageBaseImpl
*newTransactedStorage
;
6921 hr
= StorageImpl_Construct(hFile
, pwcsName
, pLkbyt
, openFlags
, fileBased
, create
, sector_size
, &newStorage
);
6922 if (FAILED(hr
)) goto end
;
6924 if (openFlags
& STGM_TRANSACTED
)
6926 hr
= Storage_ConstructTransacted(&newStorage
->base
, TRUE
, &newTransactedStorage
);
6928 IStorage_Release(&newStorage
->base
.IStorage_iface
);
6930 *result
= newTransactedStorage
;
6933 *result
= &newStorage
->base
;
6940 /************************************************************************
6941 * StorageUtl helper functions
6942 ***********************************************************************/
6944 void StorageUtl_ReadWord(const BYTE
* buffer
, ULONG offset
, WORD
* value
)
6948 memcpy(&tmp
, buffer
+offset
, sizeof(WORD
));
6949 *value
= lendian16toh(tmp
);
6952 void StorageUtl_WriteWord(BYTE
* buffer
, ULONG offset
, WORD value
)
6954 value
= htole16(value
);
6955 memcpy(buffer
+offset
, &value
, sizeof(WORD
));
6958 void StorageUtl_ReadDWord(const BYTE
* buffer
, ULONG offset
, DWORD
* value
)
6962 memcpy(&tmp
, buffer
+offset
, sizeof(DWORD
));
6963 *value
= lendian32toh(tmp
);
6966 void StorageUtl_WriteDWord(BYTE
* buffer
, ULONG offset
, DWORD value
)
6968 value
= htole32(value
);
6969 memcpy(buffer
+offset
, &value
, sizeof(DWORD
));
6972 void StorageUtl_ReadULargeInteger(const BYTE
* buffer
, ULONG offset
,
6973 ULARGE_INTEGER
* value
)
6975 #ifdef WORDS_BIGENDIAN
6978 memcpy(&tmp
, buffer
+ offset
, sizeof(ULARGE_INTEGER
));
6979 value
->u
.LowPart
= htole32(tmp
.u
.HighPart
);
6980 value
->u
.HighPart
= htole32(tmp
.u
.LowPart
);
6982 memcpy(value
, buffer
+ offset
, sizeof(ULARGE_INTEGER
));
6986 void StorageUtl_WriteULargeInteger(BYTE
* buffer
, ULONG offset
,
6987 const ULARGE_INTEGER
*value
)
6989 #ifdef WORDS_BIGENDIAN
6992 tmp
.u
.LowPart
= htole32(value
->u
.HighPart
);
6993 tmp
.u
.HighPart
= htole32(value
->u
.LowPart
);
6994 memcpy(buffer
+ offset
, &tmp
, sizeof(ULARGE_INTEGER
));
6996 memcpy(buffer
+ offset
, value
, sizeof(ULARGE_INTEGER
));
7000 void StorageUtl_ReadGUID(const BYTE
* buffer
, ULONG offset
, GUID
* value
)
7002 StorageUtl_ReadDWord(buffer
, offset
, &(value
->Data1
));
7003 StorageUtl_ReadWord(buffer
, offset
+4, &(value
->Data2
));
7004 StorageUtl_ReadWord(buffer
, offset
+6, &(value
->Data3
));
7006 memcpy(value
->Data4
, buffer
+offset
+8, sizeof(value
->Data4
));
7009 void StorageUtl_WriteGUID(BYTE
* buffer
, ULONG offset
, const GUID
* value
)
7011 StorageUtl_WriteDWord(buffer
, offset
, value
->Data1
);
7012 StorageUtl_WriteWord(buffer
, offset
+4, value
->Data2
);
7013 StorageUtl_WriteWord(buffer
, offset
+6, value
->Data3
);
7015 memcpy(buffer
+offset
+8, value
->Data4
, sizeof(value
->Data4
));
7018 void StorageUtl_CopyDirEntryToSTATSTG(
7019 StorageBaseImpl
* storage
,
7020 STATSTG
* destination
,
7021 const DirEntry
* source
,
7025 * The copy of the string occurs only when the flag is not set
7027 if (!(statFlags
& STATFLAG_NONAME
) && source
->stgType
== STGTY_ROOT
)
7029 /* Use the filename for the root storage. */
7030 destination
->pwcsName
= 0;
7031 StorageBaseImpl_GetFilename(storage
, &destination
->pwcsName
);
7033 else if( ((statFlags
& STATFLAG_NONAME
) != 0) ||
7034 (source
->name
[0] == 0) )
7036 destination
->pwcsName
= 0;
7040 destination
->pwcsName
=
7041 CoTaskMemAlloc((lstrlenW(source
->name
)+1)*sizeof(WCHAR
));
7043 strcpyW(destination
->pwcsName
, source
->name
);
7046 switch (source
->stgType
)
7050 destination
->type
= STGTY_STORAGE
;
7053 destination
->type
= STGTY_STREAM
;
7056 destination
->type
= STGTY_STREAM
;
7060 destination
->cbSize
= source
->size
;
7062 currentReturnStruct->mtime = {0}; TODO
7063 currentReturnStruct->ctime = {0};
7064 currentReturnStruct->atime = {0};
7066 destination
->grfMode
= 0;
7067 destination
->grfLocksSupported
= 0;
7068 destination
->clsid
= source
->clsid
;
7069 destination
->grfStateBits
= 0;
7070 destination
->reserved
= 0;
7074 /************************************************************************
7075 * BlockChainStream implementation
7076 ***********************************************************************/
7078 /******************************************************************************
7079 * BlockChainStream_GetHeadOfChain
7081 * Returns the head of this stream chain.
7082 * Some special chains don't have directory entries, their heads are kept in
7083 * This->headOfStreamPlaceHolder.
7086 static ULONG
BlockChainStream_GetHeadOfChain(BlockChainStream
* This
)
7088 DirEntry chainEntry
;
7091 if (This
->headOfStreamPlaceHolder
!= 0)
7092 return *(This
->headOfStreamPlaceHolder
);
7094 if (This
->ownerDirEntry
!= DIRENTRY_NULL
)
7096 hr
= StorageImpl_ReadDirEntry(
7097 This
->parentStorage
,
7098 This
->ownerDirEntry
,
7101 if (SUCCEEDED(hr
) && chainEntry
.startingBlock
< BLOCK_FIRST_SPECIAL
)
7102 return chainEntry
.startingBlock
;
7105 return BLOCK_END_OF_CHAIN
;
7108 /* Read and save the index of all blocks in this stream. */
7109 static HRESULT
BlockChainStream_UpdateIndexCache(BlockChainStream
* This
)
7111 ULONG next_sector
, next_offset
;
7113 struct BlockChainRun
*last_run
;
7115 if (This
->indexCacheLen
== 0)
7119 next_sector
= BlockChainStream_GetHeadOfChain(This
);
7123 last_run
= &This
->indexCache
[This
->indexCacheLen
-1];
7124 next_offset
= last_run
->lastOffset
+1;
7125 hr
= StorageImpl_GetNextBlockInChain(This
->parentStorage
,
7126 last_run
->firstSector
+ last_run
->lastOffset
- last_run
->firstOffset
,
7128 if (FAILED(hr
)) return hr
;
7131 while (next_sector
!= BLOCK_END_OF_CHAIN
)
7133 if (!last_run
|| next_sector
!= last_run
->firstSector
+ next_offset
- last_run
->firstOffset
)
7135 /* Add the current block to the cache. */
7136 if (This
->indexCacheSize
== 0)
7138 This
->indexCache
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct BlockChainRun
)*16);
7139 if (!This
->indexCache
) return E_OUTOFMEMORY
;
7140 This
->indexCacheSize
= 16;
7142 else if (This
->indexCacheSize
== This
->indexCacheLen
)
7144 struct BlockChainRun
*new_cache
;
7147 new_size
= This
->indexCacheSize
* 2;
7148 new_cache
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct BlockChainRun
)*new_size
);
7149 if (!new_cache
) return E_OUTOFMEMORY
;
7150 memcpy(new_cache
, This
->indexCache
, sizeof(struct BlockChainRun
)*This
->indexCacheLen
);
7152 HeapFree(GetProcessHeap(), 0, This
->indexCache
);
7153 This
->indexCache
= new_cache
;
7154 This
->indexCacheSize
= new_size
;
7157 This
->indexCacheLen
++;
7158 last_run
= &This
->indexCache
[This
->indexCacheLen
-1];
7159 last_run
->firstSector
= next_sector
;
7160 last_run
->firstOffset
= next_offset
;
7163 last_run
->lastOffset
= next_offset
;
7165 /* Find the next block. */
7167 hr
= StorageImpl_GetNextBlockInChain(This
->parentStorage
, next_sector
, &next_sector
);
7168 if (FAILED(hr
)) return hr
;
7171 if (This
->indexCacheLen
)
7173 This
->tailIndex
= last_run
->firstSector
+ last_run
->lastOffset
- last_run
->firstOffset
;
7174 This
->numBlocks
= last_run
->lastOffset
+1;
7178 This
->tailIndex
= BLOCK_END_OF_CHAIN
;
7179 This
->numBlocks
= 0;
7185 /* Locate the nth block in this stream. */
7186 static ULONG
BlockChainStream_GetSectorOfOffset(BlockChainStream
*This
, ULONG offset
)
7188 ULONG min_offset
= 0, max_offset
= This
->numBlocks
-1;
7189 ULONG min_run
= 0, max_run
= This
->indexCacheLen
-1;
7191 if (offset
>= This
->numBlocks
)
7192 return BLOCK_END_OF_CHAIN
;
7194 while (min_run
< max_run
)
7196 ULONG run_to_check
= min_run
+ (offset
- min_offset
) * (max_run
- min_run
) / (max_offset
- min_offset
);
7197 if (offset
< This
->indexCache
[run_to_check
].firstOffset
)
7199 max_offset
= This
->indexCache
[run_to_check
].firstOffset
-1;
7200 max_run
= run_to_check
-1;
7202 else if (offset
> This
->indexCache
[run_to_check
].lastOffset
)
7204 min_offset
= This
->indexCache
[run_to_check
].lastOffset
+1;
7205 min_run
= run_to_check
+1;
7208 /* Block is in this run. */
7209 min_run
= max_run
= run_to_check
;
7212 return This
->indexCache
[min_run
].firstSector
+ offset
- This
->indexCache
[min_run
].firstOffset
;
7215 static HRESULT
BlockChainStream_GetBlockAtOffset(BlockChainStream
*This
,
7216 ULONG index
, BlockChainBlock
**block
, ULONG
*sector
, BOOL create
)
7218 BlockChainBlock
*result
=NULL
;
7222 if (This
->cachedBlocks
[i
].index
== index
)
7224 *sector
= This
->cachedBlocks
[i
].sector
;
7225 *block
= &This
->cachedBlocks
[i
];
7229 *sector
= BlockChainStream_GetSectorOfOffset(This
, index
);
7230 if (*sector
== BLOCK_END_OF_CHAIN
)
7231 return STG_E_DOCFILECORRUPT
;
7235 if (This
->cachedBlocks
[0].index
== 0xffffffff)
7236 result
= &This
->cachedBlocks
[0];
7237 else if (This
->cachedBlocks
[1].index
== 0xffffffff)
7238 result
= &This
->cachedBlocks
[1];
7241 result
= &This
->cachedBlocks
[This
->blockToEvict
++];
7242 if (This
->blockToEvict
== 2)
7243 This
->blockToEvict
= 0;
7248 if (!StorageImpl_WriteBigBlock(This
->parentStorage
, result
->sector
, result
->data
))
7249 return STG_E_WRITEFAULT
;
7250 result
->dirty
= FALSE
;
7253 result
->read
= FALSE
;
7254 result
->index
= index
;
7255 result
->sector
= *sector
;
7262 BlockChainStream
* BlockChainStream_Construct(
7263 StorageImpl
* parentStorage
,
7264 ULONG
* headOfStreamPlaceHolder
,
7267 BlockChainStream
* newStream
;
7269 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(BlockChainStream
));
7273 newStream
->parentStorage
= parentStorage
;
7274 newStream
->headOfStreamPlaceHolder
= headOfStreamPlaceHolder
;
7275 newStream
->ownerDirEntry
= dirEntry
;
7276 newStream
->indexCache
= NULL
;
7277 newStream
->indexCacheLen
= 0;
7278 newStream
->indexCacheSize
= 0;
7279 newStream
->cachedBlocks
[0].index
= 0xffffffff;
7280 newStream
->cachedBlocks
[0].dirty
= FALSE
;
7281 newStream
->cachedBlocks
[1].index
= 0xffffffff;
7282 newStream
->cachedBlocks
[1].dirty
= FALSE
;
7283 newStream
->blockToEvict
= 0;
7285 if (FAILED(BlockChainStream_UpdateIndexCache(newStream
)))
7287 HeapFree(GetProcessHeap(), 0, newStream
->indexCache
);
7288 HeapFree(GetProcessHeap(), 0, newStream
);
7295 HRESULT
BlockChainStream_Flush(BlockChainStream
* This
)
7298 if (!This
) return S_OK
;
7301 if (This
->cachedBlocks
[i
].dirty
)
7303 if (StorageImpl_WriteBigBlock(This
->parentStorage
, This
->cachedBlocks
[i
].sector
, This
->cachedBlocks
[i
].data
))
7304 This
->cachedBlocks
[i
].dirty
= FALSE
;
7306 return STG_E_WRITEFAULT
;
7312 void BlockChainStream_Destroy(BlockChainStream
* This
)
7316 BlockChainStream_Flush(This
);
7317 HeapFree(GetProcessHeap(), 0, This
->indexCache
);
7319 HeapFree(GetProcessHeap(), 0, This
);
7322 /******************************************************************************
7323 * BlockChainStream_Shrink
7325 * Shrinks this chain in the big block depot.
7327 static BOOL
BlockChainStream_Shrink(BlockChainStream
* This
,
7328 ULARGE_INTEGER newSize
)
7335 * Figure out how many blocks are needed to contain the new size
7337 numBlocks
= newSize
.QuadPart
/ This
->parentStorage
->bigBlockSize
;
7339 if ((newSize
.QuadPart
% This
->parentStorage
->bigBlockSize
) != 0)
7345 * Go to the new end of chain
7347 blockIndex
= BlockChainStream_GetSectorOfOffset(This
, numBlocks
-1);
7349 /* Mark the new end of chain */
7350 StorageImpl_SetNextBlockInChain(
7351 This
->parentStorage
,
7353 BLOCK_END_OF_CHAIN
);
7355 This
->tailIndex
= blockIndex
;
7359 if (This
->headOfStreamPlaceHolder
!= 0)
7361 *This
->headOfStreamPlaceHolder
= BLOCK_END_OF_CHAIN
;
7365 DirEntry chainEntry
;
7366 assert(This
->ownerDirEntry
!= DIRENTRY_NULL
);
7368 StorageImpl_ReadDirEntry(
7369 This
->parentStorage
,
7370 This
->ownerDirEntry
,
7373 chainEntry
.startingBlock
= BLOCK_END_OF_CHAIN
;
7375 StorageImpl_WriteDirEntry(
7376 This
->parentStorage
,
7377 This
->ownerDirEntry
,
7381 This
->tailIndex
= BLOCK_END_OF_CHAIN
;
7384 This
->numBlocks
= numBlocks
;
7387 * Mark the extra blocks as free
7389 while (This
->indexCacheLen
&& This
->indexCache
[This
->indexCacheLen
-1].lastOffset
>= numBlocks
)
7391 struct BlockChainRun
*last_run
= &This
->indexCache
[This
->indexCacheLen
-1];
7392 StorageImpl_FreeBigBlock(This
->parentStorage
,
7393 last_run
->firstSector
+ last_run
->lastOffset
- last_run
->firstOffset
);
7394 if (last_run
->lastOffset
== last_run
->firstOffset
)
7395 This
->indexCacheLen
--;
7397 last_run
->lastOffset
--;
7401 * Reset the last accessed block cache.
7405 if (This
->cachedBlocks
[i
].index
>= numBlocks
)
7407 This
->cachedBlocks
[i
].index
= 0xffffffff;
7408 This
->cachedBlocks
[i
].dirty
= FALSE
;
7415 /******************************************************************************
7416 * BlockChainStream_Enlarge
7418 * Grows this chain in the big block depot.
7420 static BOOL
BlockChainStream_Enlarge(BlockChainStream
* This
,
7421 ULARGE_INTEGER newSize
)
7423 ULONG blockIndex
, currentBlock
;
7425 ULONG oldNumBlocks
= 0;
7427 blockIndex
= BlockChainStream_GetHeadOfChain(This
);
7430 * Empty chain. Create the head.
7432 if (blockIndex
== BLOCK_END_OF_CHAIN
)
7434 blockIndex
= StorageImpl_GetNextFreeBigBlock(This
->parentStorage
);
7435 StorageImpl_SetNextBlockInChain(This
->parentStorage
,
7437 BLOCK_END_OF_CHAIN
);
7439 if (This
->headOfStreamPlaceHolder
!= 0)
7441 *(This
->headOfStreamPlaceHolder
) = blockIndex
;
7445 DirEntry chainEntry
;
7446 assert(This
->ownerDirEntry
!= DIRENTRY_NULL
);
7448 StorageImpl_ReadDirEntry(
7449 This
->parentStorage
,
7450 This
->ownerDirEntry
,
7453 chainEntry
.startingBlock
= blockIndex
;
7455 StorageImpl_WriteDirEntry(
7456 This
->parentStorage
,
7457 This
->ownerDirEntry
,
7461 This
->tailIndex
= blockIndex
;
7462 This
->numBlocks
= 1;
7466 * Figure out how many blocks are needed to contain this stream
7468 newNumBlocks
= newSize
.QuadPart
/ This
->parentStorage
->bigBlockSize
;
7470 if ((newSize
.QuadPart
% This
->parentStorage
->bigBlockSize
) != 0)
7474 * Go to the current end of chain
7476 if (This
->tailIndex
== BLOCK_END_OF_CHAIN
)
7478 currentBlock
= blockIndex
;
7480 while (blockIndex
!= BLOCK_END_OF_CHAIN
)
7483 currentBlock
= blockIndex
;
7485 if(FAILED(StorageImpl_GetNextBlockInChain(This
->parentStorage
, currentBlock
,
7490 This
->tailIndex
= currentBlock
;
7493 currentBlock
= This
->tailIndex
;
7494 oldNumBlocks
= This
->numBlocks
;
7497 * Add new blocks to the chain
7499 if (oldNumBlocks
< newNumBlocks
)
7501 while (oldNumBlocks
< newNumBlocks
)
7503 blockIndex
= StorageImpl_GetNextFreeBigBlock(This
->parentStorage
);
7505 StorageImpl_SetNextBlockInChain(
7506 This
->parentStorage
,
7510 StorageImpl_SetNextBlockInChain(
7511 This
->parentStorage
,
7513 BLOCK_END_OF_CHAIN
);
7515 currentBlock
= blockIndex
;
7519 This
->tailIndex
= blockIndex
;
7520 This
->numBlocks
= newNumBlocks
;
7523 if (FAILED(BlockChainStream_UpdateIndexCache(This
)))
7530 /******************************************************************************
7531 * BlockChainStream_GetSize
7533 * Returns the size of this chain.
7534 * Will return the block count if this chain doesn't have a directory entry.
7536 static ULARGE_INTEGER
BlockChainStream_GetSize(BlockChainStream
* This
)
7538 DirEntry chainEntry
;
7540 if(This
->headOfStreamPlaceHolder
== NULL
)
7543 * This chain has a directory entry so use the size value from there.
7545 StorageImpl_ReadDirEntry(
7546 This
->parentStorage
,
7547 This
->ownerDirEntry
,
7550 return chainEntry
.size
;
7555 * this chain is a chain that does not have a directory entry, figure out the
7556 * size by making the product number of used blocks times the
7559 ULARGE_INTEGER result
;
7561 (ULONGLONG
)BlockChainStream_GetCount(This
) *
7562 This
->parentStorage
->bigBlockSize
;
7568 /******************************************************************************
7569 * BlockChainStream_SetSize
7571 * Sets the size of this stream. The big block depot will be updated.
7572 * The file will grow if we grow the chain.
7574 * TODO: Free the actual blocks in the file when we shrink the chain.
7575 * Currently, the blocks are still in the file. So the file size
7576 * doesn't shrink even if we shrink streams.
7578 BOOL
BlockChainStream_SetSize(
7579 BlockChainStream
* This
,
7580 ULARGE_INTEGER newSize
)
7582 ULARGE_INTEGER size
= BlockChainStream_GetSize(This
);
7584 if (newSize
.QuadPart
== size
.QuadPart
)
7587 if (newSize
.QuadPart
< size
.QuadPart
)
7589 BlockChainStream_Shrink(This
, newSize
);
7593 BlockChainStream_Enlarge(This
, newSize
);
7599 /******************************************************************************
7600 * BlockChainStream_ReadAt
7602 * Reads a specified number of bytes from this chain at the specified offset.
7603 * bytesRead may be NULL.
7604 * Failure will be returned if the specified number of bytes has not been read.
7606 HRESULT
BlockChainStream_ReadAt(BlockChainStream
* This
,
7607 ULARGE_INTEGER offset
,
7612 ULONG blockNoInSequence
= offset
.QuadPart
/ This
->parentStorage
->bigBlockSize
;
7613 ULONG offsetInBlock
= offset
.QuadPart
% This
->parentStorage
->bigBlockSize
;
7614 ULONG bytesToReadInBuffer
;
7617 ULARGE_INTEGER stream_size
;
7619 BlockChainBlock
*cachedBlock
;
7621 TRACE("(%p)-> %i %p %i %p\n",This
, offset
.u
.LowPart
, buffer
, size
, bytesRead
);
7624 * Find the first block in the stream that contains part of the buffer.
7626 blockIndex
= BlockChainStream_GetSectorOfOffset(This
, blockNoInSequence
);
7630 stream_size
= BlockChainStream_GetSize(This
);
7631 if (stream_size
.QuadPart
> offset
.QuadPart
)
7632 size
= min(stream_size
.QuadPart
- offset
.QuadPart
, size
);
7637 * Start reading the buffer.
7639 bufferWalker
= buffer
;
7643 ULARGE_INTEGER ulOffset
;
7647 * Calculate how many bytes we can copy from this big block.
7649 bytesToReadInBuffer
=
7650 min(This
->parentStorage
->bigBlockSize
- offsetInBlock
, size
);
7652 hr
= BlockChainStream_GetBlockAtOffset(This
, blockNoInSequence
, &cachedBlock
, &blockIndex
, size
== bytesToReadInBuffer
);
7659 /* Not in cache, and we're going to read past the end of the block. */
7660 ulOffset
.QuadPart
= StorageImpl_GetBigBlockOffset(This
->parentStorage
, blockIndex
) +
7663 StorageImpl_ReadAt(This
->parentStorage
,
7666 bytesToReadInBuffer
,
7671 if (!cachedBlock
->read
)
7674 if (FAILED(StorageImpl_ReadBigBlock(This
->parentStorage
, cachedBlock
->sector
, cachedBlock
->data
, &read
)) && !read
)
7675 return STG_E_READFAULT
;
7677 cachedBlock
->read
= TRUE
;
7680 memcpy(bufferWalker
, cachedBlock
->data
+offsetInBlock
, bytesToReadInBuffer
);
7681 bytesReadAt
= bytesToReadInBuffer
;
7684 blockNoInSequence
++;
7685 bufferWalker
+= bytesReadAt
;
7686 size
-= bytesReadAt
;
7687 *bytesRead
+= bytesReadAt
;
7688 offsetInBlock
= 0; /* There is no offset on the next block */
7690 if (bytesToReadInBuffer
!= bytesReadAt
)
7697 /******************************************************************************
7698 * BlockChainStream_WriteAt
7700 * Writes the specified number of bytes to this chain at the specified offset.
7701 * Will fail if not all specified number of bytes have been written.
7703 HRESULT
BlockChainStream_WriteAt(BlockChainStream
* This
,
7704 ULARGE_INTEGER offset
,
7707 ULONG
* bytesWritten
)
7709 ULONG blockNoInSequence
= offset
.QuadPart
/ This
->parentStorage
->bigBlockSize
;
7710 ULONG offsetInBlock
= offset
.QuadPart
% This
->parentStorage
->bigBlockSize
;
7713 const BYTE
* bufferWalker
;
7715 BlockChainBlock
*cachedBlock
;
7718 bufferWalker
= buffer
;
7722 ULARGE_INTEGER ulOffset
;
7723 DWORD bytesWrittenAt
;
7726 * Calculate how many bytes we can copy to this big block.
7729 min(This
->parentStorage
->bigBlockSize
- offsetInBlock
, size
);
7731 hr
= BlockChainStream_GetBlockAtOffset(This
, blockNoInSequence
, &cachedBlock
, &blockIndex
, size
== bytesToWrite
);
7733 /* BlockChainStream_SetSize should have already been called to ensure we have
7734 * enough blocks in the chain to write into */
7737 ERR("not enough blocks in chain to write data\n");
7743 /* Not in cache, and we're going to write past the end of the block. */
7744 ulOffset
.QuadPart
= StorageImpl_GetBigBlockOffset(This
->parentStorage
, blockIndex
) +
7747 StorageImpl_WriteAt(This
->parentStorage
,
7755 if (!cachedBlock
->read
&& bytesToWrite
!= This
->parentStorage
->bigBlockSize
)
7758 if (FAILED(StorageImpl_ReadBigBlock(This
->parentStorage
, cachedBlock
->sector
, cachedBlock
->data
, &read
)) && !read
)
7759 return STG_E_READFAULT
;
7762 memcpy(cachedBlock
->data
+offsetInBlock
, bufferWalker
, bytesToWrite
);
7763 bytesWrittenAt
= bytesToWrite
;
7764 cachedBlock
->read
= TRUE
;
7765 cachedBlock
->dirty
= TRUE
;
7768 blockNoInSequence
++;
7769 bufferWalker
+= bytesWrittenAt
;
7770 size
-= bytesWrittenAt
;
7771 *bytesWritten
+= bytesWrittenAt
;
7772 offsetInBlock
= 0; /* There is no offset on the next block */
7774 if (bytesWrittenAt
!= bytesToWrite
)
7778 return (size
== 0) ? S_OK
: STG_E_WRITEFAULT
;
7782 /************************************************************************
7783 * SmallBlockChainStream implementation
7784 ***********************************************************************/
7786 SmallBlockChainStream
* SmallBlockChainStream_Construct(
7787 StorageImpl
* parentStorage
,
7788 ULONG
* headOfStreamPlaceHolder
,
7791 SmallBlockChainStream
* newStream
;
7793 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(SmallBlockChainStream
));
7795 newStream
->parentStorage
= parentStorage
;
7796 newStream
->headOfStreamPlaceHolder
= headOfStreamPlaceHolder
;
7797 newStream
->ownerDirEntry
= dirEntry
;
7802 void SmallBlockChainStream_Destroy(
7803 SmallBlockChainStream
* This
)
7805 HeapFree(GetProcessHeap(), 0, This
);
7808 /******************************************************************************
7809 * SmallBlockChainStream_GetHeadOfChain
7811 * Returns the head of this chain of small blocks.
7813 static ULONG
SmallBlockChainStream_GetHeadOfChain(
7814 SmallBlockChainStream
* This
)
7816 DirEntry chainEntry
;
7819 if (This
->headOfStreamPlaceHolder
!= NULL
)
7820 return *(This
->headOfStreamPlaceHolder
);
7822 if (This
->ownerDirEntry
)
7824 hr
= StorageImpl_ReadDirEntry(
7825 This
->parentStorage
,
7826 This
->ownerDirEntry
,
7829 if (SUCCEEDED(hr
) && chainEntry
.startingBlock
< BLOCK_FIRST_SPECIAL
)
7830 return chainEntry
.startingBlock
;
7833 return BLOCK_END_OF_CHAIN
;
7836 /******************************************************************************
7837 * SmallBlockChainStream_GetNextBlockInChain
7839 * Returns the index of the next small block in this chain.
7842 * - BLOCK_END_OF_CHAIN: end of this chain
7843 * - BLOCK_UNUSED: small block 'blockIndex' is free
7845 static HRESULT
SmallBlockChainStream_GetNextBlockInChain(
7846 SmallBlockChainStream
* This
,
7848 ULONG
* nextBlockInChain
)
7850 ULARGE_INTEGER offsetOfBlockInDepot
;
7855 *nextBlockInChain
= BLOCK_END_OF_CHAIN
;
7857 offsetOfBlockInDepot
.QuadPart
= (ULONGLONG
)blockIndex
* sizeof(ULONG
);
7860 * Read those bytes in the buffer from the small block file.
7862 res
= BlockChainStream_ReadAt(
7863 This
->parentStorage
->smallBlockDepotChain
,
7864 offsetOfBlockInDepot
,
7869 if (SUCCEEDED(res
) && bytesRead
!= sizeof(DWORD
))
7870 res
= STG_E_READFAULT
;
7874 StorageUtl_ReadDWord((BYTE
*)&buffer
, 0, nextBlockInChain
);
7881 /******************************************************************************
7882 * SmallBlockChainStream_SetNextBlockInChain
7884 * Writes the index of the next block of the specified block in the small
7886 * To set the end of chain use BLOCK_END_OF_CHAIN as nextBlock.
7887 * To flag a block as free use BLOCK_UNUSED as nextBlock.
7889 static void SmallBlockChainStream_SetNextBlockInChain(
7890 SmallBlockChainStream
* This
,
7894 ULARGE_INTEGER offsetOfBlockInDepot
;
7898 offsetOfBlockInDepot
.QuadPart
= (ULONGLONG
)blockIndex
* sizeof(ULONG
);
7900 StorageUtl_WriteDWord((BYTE
*)&buffer
, 0, nextBlock
);
7903 * Read those bytes in the buffer from the small block file.
7905 BlockChainStream_WriteAt(
7906 This
->parentStorage
->smallBlockDepotChain
,
7907 offsetOfBlockInDepot
,
7913 /******************************************************************************
7914 * SmallBlockChainStream_FreeBlock
7916 * Flag small block 'blockIndex' as free in the small block depot.
7918 static void SmallBlockChainStream_FreeBlock(
7919 SmallBlockChainStream
* This
,
7922 SmallBlockChainStream_SetNextBlockInChain(This
, blockIndex
, BLOCK_UNUSED
);
7925 /******************************************************************************
7926 * SmallBlockChainStream_GetNextFreeBlock
7928 * Returns the index of a free small block. The small block depot will be
7929 * enlarged if necessary. The small block chain will also be enlarged if
7932 static ULONG
SmallBlockChainStream_GetNextFreeBlock(
7933 SmallBlockChainStream
* This
)
7935 ULARGE_INTEGER offsetOfBlockInDepot
;
7938 ULONG blockIndex
= This
->parentStorage
->firstFreeSmallBlock
;
7939 ULONG nextBlockIndex
= BLOCK_END_OF_CHAIN
;
7941 ULONG smallBlocksPerBigBlock
;
7943 ULONG blocksRequired
;
7944 ULARGE_INTEGER old_size
, size_required
;
7946 offsetOfBlockInDepot
.u
.HighPart
= 0;
7949 * Scan the small block depot for a free block
7951 while (nextBlockIndex
!= BLOCK_UNUSED
)
7953 offsetOfBlockInDepot
.QuadPart
= (ULONGLONG
)blockIndex
* sizeof(ULONG
);
7955 res
= BlockChainStream_ReadAt(
7956 This
->parentStorage
->smallBlockDepotChain
,
7957 offsetOfBlockInDepot
,
7963 * If we run out of space for the small block depot, enlarge it
7965 if (SUCCEEDED(res
) && bytesRead
== sizeof(DWORD
))
7967 StorageUtl_ReadDWord((BYTE
*)&buffer
, 0, &nextBlockIndex
);
7969 if (nextBlockIndex
!= BLOCK_UNUSED
)
7975 BlockChainStream_GetCount(This
->parentStorage
->smallBlockDepotChain
);
7977 BYTE smallBlockDepot
[MAX_BIG_BLOCK_SIZE
];
7978 ULARGE_INTEGER newSize
, offset
;
7981 newSize
.QuadPart
= (ULONGLONG
)(count
+ 1) * This
->parentStorage
->bigBlockSize
;
7982 BlockChainStream_Enlarge(This
->parentStorage
->smallBlockDepotChain
, newSize
);
7985 * Initialize all the small blocks to free
7987 memset(smallBlockDepot
, BLOCK_UNUSED
, This
->parentStorage
->bigBlockSize
);
7988 offset
.QuadPart
= (ULONGLONG
)count
* This
->parentStorage
->bigBlockSize
;
7989 BlockChainStream_WriteAt(This
->parentStorage
->smallBlockDepotChain
,
7990 offset
, This
->parentStorage
->bigBlockSize
, smallBlockDepot
, &bytesWritten
);
7992 StorageImpl_SaveFileHeader(This
->parentStorage
);
7996 This
->parentStorage
->firstFreeSmallBlock
= blockIndex
+1;
7998 smallBlocksPerBigBlock
=
7999 This
->parentStorage
->bigBlockSize
/ This
->parentStorage
->smallBlockSize
;
8002 * Verify if we have to allocate big blocks to contain small blocks
8004 blocksRequired
= (blockIndex
/ smallBlocksPerBigBlock
) + 1;
8006 size_required
.QuadPart
= (ULONGLONG
)blocksRequired
* This
->parentStorage
->bigBlockSize
;
8008 old_size
= BlockChainStream_GetSize(This
->parentStorage
->smallBlockRootChain
);
8010 if (size_required
.QuadPart
> old_size
.QuadPart
)
8012 BlockChainStream_SetSize(
8013 This
->parentStorage
->smallBlockRootChain
,
8016 StorageImpl_ReadDirEntry(
8017 This
->parentStorage
,
8018 This
->parentStorage
->base
.storageDirEntry
,
8021 rootEntry
.size
= size_required
;
8023 StorageImpl_WriteDirEntry(
8024 This
->parentStorage
,
8025 This
->parentStorage
->base
.storageDirEntry
,
8032 /******************************************************************************
8033 * SmallBlockChainStream_ReadAt
8035 * Reads a specified number of bytes from this chain at the specified offset.
8036 * bytesRead may be NULL.
8037 * Failure will be returned if the specified number of bytes has not been read.
8039 HRESULT
SmallBlockChainStream_ReadAt(
8040 SmallBlockChainStream
* This
,
8041 ULARGE_INTEGER offset
,
8047 ULARGE_INTEGER offsetInBigBlockFile
;
8048 ULONG blockNoInSequence
=
8049 offset
.u
.LowPart
/ This
->parentStorage
->smallBlockSize
;
8051 ULONG offsetInBlock
= offset
.u
.LowPart
% This
->parentStorage
->smallBlockSize
;
8052 ULONG bytesToReadInBuffer
;
8054 ULONG bytesReadFromBigBlockFile
;
8056 ULARGE_INTEGER stream_size
;
8059 * This should never happen on a small block file.
8061 assert(offset
.u
.HighPart
==0);
8065 stream_size
= SmallBlockChainStream_GetSize(This
);
8066 if (stream_size
.QuadPart
> offset
.QuadPart
)
8067 size
= min(stream_size
.QuadPart
- offset
.QuadPart
, size
);
8072 * Find the first block in the stream that contains part of the buffer.
8074 blockIndex
= SmallBlockChainStream_GetHeadOfChain(This
);
8076 while ( (blockNoInSequence
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
))
8078 rc
= SmallBlockChainStream_GetNextBlockInChain(This
, blockIndex
, &blockIndex
);
8081 blockNoInSequence
--;
8085 * Start reading the buffer.
8087 bufferWalker
= buffer
;
8089 while ( (size
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
) )
8092 * Calculate how many bytes we can copy from this small block.
8094 bytesToReadInBuffer
=
8095 min(This
->parentStorage
->smallBlockSize
- offsetInBlock
, size
);
8098 * Calculate the offset of the small block in the small block file.
8100 offsetInBigBlockFile
.QuadPart
=
8101 (ULONGLONG
)blockIndex
* This
->parentStorage
->smallBlockSize
;
8103 offsetInBigBlockFile
.QuadPart
+= offsetInBlock
;
8106 * Read those bytes in the buffer from the small block file.
8107 * The small block has already been identified so it shouldn't fail
8108 * unless the file is corrupt.
8110 rc
= BlockChainStream_ReadAt(This
->parentStorage
->smallBlockRootChain
,
8111 offsetInBigBlockFile
,
8112 bytesToReadInBuffer
,
8114 &bytesReadFromBigBlockFile
);
8119 if (!bytesReadFromBigBlockFile
)
8120 return STG_E_DOCFILECORRUPT
;
8123 * Step to the next big block.
8125 rc
= SmallBlockChainStream_GetNextBlockInChain(This
, blockIndex
, &blockIndex
);
8127 return STG_E_DOCFILECORRUPT
;
8129 bufferWalker
+= bytesReadFromBigBlockFile
;
8130 size
-= bytesReadFromBigBlockFile
;
8131 *bytesRead
+= bytesReadFromBigBlockFile
;
8132 offsetInBlock
= (offsetInBlock
+ bytesReadFromBigBlockFile
) % This
->parentStorage
->smallBlockSize
;
8138 /******************************************************************************
8139 * SmallBlockChainStream_WriteAt
8141 * Writes the specified number of bytes to this chain at the specified offset.
8142 * Will fail if not all specified number of bytes have been written.
8144 HRESULT
SmallBlockChainStream_WriteAt(
8145 SmallBlockChainStream
* This
,
8146 ULARGE_INTEGER offset
,
8149 ULONG
* bytesWritten
)
8151 ULARGE_INTEGER offsetInBigBlockFile
;
8152 ULONG blockNoInSequence
=
8153 offset
.u
.LowPart
/ This
->parentStorage
->smallBlockSize
;
8155 ULONG offsetInBlock
= offset
.u
.LowPart
% This
->parentStorage
->smallBlockSize
;
8156 ULONG bytesToWriteInBuffer
;
8158 ULONG bytesWrittenToBigBlockFile
;
8159 const BYTE
* bufferWalker
;
8163 * This should never happen on a small block file.
8165 assert(offset
.u
.HighPart
==0);
8168 * Find the first block in the stream that contains part of the buffer.
8170 blockIndex
= SmallBlockChainStream_GetHeadOfChain(This
);
8172 while ( (blockNoInSequence
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
))
8174 if(FAILED(SmallBlockChainStream_GetNextBlockInChain(This
, blockIndex
, &blockIndex
)))
8175 return STG_E_DOCFILECORRUPT
;
8176 blockNoInSequence
--;
8180 * Start writing the buffer.
8183 bufferWalker
= buffer
;
8184 while ( (size
> 0) && (blockIndex
!= BLOCK_END_OF_CHAIN
) )
8187 * Calculate how many bytes we can copy to this small block.
8189 bytesToWriteInBuffer
=
8190 min(This
->parentStorage
->smallBlockSize
- offsetInBlock
, size
);
8193 * Calculate the offset of the small block in the small block file.
8195 offsetInBigBlockFile
.QuadPart
=
8196 (ULONGLONG
)blockIndex
* This
->parentStorage
->smallBlockSize
;
8198 offsetInBigBlockFile
.QuadPart
+= offsetInBlock
;
8201 * Write those bytes in the buffer to the small block file.
8203 res
= BlockChainStream_WriteAt(
8204 This
->parentStorage
->smallBlockRootChain
,
8205 offsetInBigBlockFile
,
8206 bytesToWriteInBuffer
,
8208 &bytesWrittenToBigBlockFile
);
8213 * Step to the next big block.
8215 res
= SmallBlockChainStream_GetNextBlockInChain(This
, blockIndex
, &blockIndex
);
8218 bufferWalker
+= bytesWrittenToBigBlockFile
;
8219 size
-= bytesWrittenToBigBlockFile
;
8220 *bytesWritten
+= bytesWrittenToBigBlockFile
;
8221 offsetInBlock
= (offsetInBlock
+ bytesWrittenToBigBlockFile
) % This
->parentStorage
->smallBlockSize
;
8224 return (size
== 0) ? S_OK
: STG_E_WRITEFAULT
;
8227 /******************************************************************************
8228 * SmallBlockChainStream_Shrink
8230 * Shrinks this chain in the small block depot.
8232 static BOOL
SmallBlockChainStream_Shrink(
8233 SmallBlockChainStream
* This
,
8234 ULARGE_INTEGER newSize
)
8236 ULONG blockIndex
, extraBlock
;
8240 numBlocks
= newSize
.u
.LowPart
/ This
->parentStorage
->smallBlockSize
;
8242 if ((newSize
.u
.LowPart
% This
->parentStorage
->smallBlockSize
) != 0)
8245 blockIndex
= SmallBlockChainStream_GetHeadOfChain(This
);
8248 * Go to the new end of chain
8250 while (count
< numBlocks
)
8252 if(FAILED(SmallBlockChainStream_GetNextBlockInChain(This
, blockIndex
,
8259 * If the count is 0, we have a special case, the head of the chain was
8264 DirEntry chainEntry
;
8266 StorageImpl_ReadDirEntry(This
->parentStorage
,
8267 This
->ownerDirEntry
,
8270 chainEntry
.startingBlock
= BLOCK_END_OF_CHAIN
;
8272 StorageImpl_WriteDirEntry(This
->parentStorage
,
8273 This
->ownerDirEntry
,
8277 * We start freeing the chain at the head block.
8279 extraBlock
= blockIndex
;
8283 /* Get the next block before marking the new end */
8284 if(FAILED(SmallBlockChainStream_GetNextBlockInChain(This
, blockIndex
,
8288 /* Mark the new end of chain */
8289 SmallBlockChainStream_SetNextBlockInChain(
8292 BLOCK_END_OF_CHAIN
);
8296 * Mark the extra blocks as free
8298 while (extraBlock
!= BLOCK_END_OF_CHAIN
)
8300 if(FAILED(SmallBlockChainStream_GetNextBlockInChain(This
, extraBlock
,
8303 SmallBlockChainStream_FreeBlock(This
, extraBlock
);
8304 This
->parentStorage
->firstFreeSmallBlock
= min(This
->parentStorage
->firstFreeSmallBlock
, extraBlock
);
8305 extraBlock
= blockIndex
;
8311 /******************************************************************************
8312 * SmallBlockChainStream_Enlarge
8314 * Grows this chain in the small block depot.
8316 static BOOL
SmallBlockChainStream_Enlarge(
8317 SmallBlockChainStream
* This
,
8318 ULARGE_INTEGER newSize
)
8320 ULONG blockIndex
, currentBlock
;
8322 ULONG oldNumBlocks
= 0;
8324 blockIndex
= SmallBlockChainStream_GetHeadOfChain(This
);
8327 * Empty chain. Create the head.
8329 if (blockIndex
== BLOCK_END_OF_CHAIN
)
8331 blockIndex
= SmallBlockChainStream_GetNextFreeBlock(This
);
8332 SmallBlockChainStream_SetNextBlockInChain(
8335 BLOCK_END_OF_CHAIN
);
8337 if (This
->headOfStreamPlaceHolder
!= NULL
)
8339 *(This
->headOfStreamPlaceHolder
) = blockIndex
;
8343 DirEntry chainEntry
;
8345 StorageImpl_ReadDirEntry(This
->parentStorage
, This
->ownerDirEntry
,
8348 chainEntry
.startingBlock
= blockIndex
;
8350 StorageImpl_WriteDirEntry(This
->parentStorage
, This
->ownerDirEntry
,
8355 currentBlock
= blockIndex
;
8358 * Figure out how many blocks are needed to contain this stream
8360 newNumBlocks
= newSize
.u
.LowPart
/ This
->parentStorage
->smallBlockSize
;
8362 if ((newSize
.u
.LowPart
% This
->parentStorage
->smallBlockSize
) != 0)
8366 * Go to the current end of chain
8368 while (blockIndex
!= BLOCK_END_OF_CHAIN
)
8371 currentBlock
= blockIndex
;
8372 if(FAILED(SmallBlockChainStream_GetNextBlockInChain(This
, currentBlock
, &blockIndex
)))
8377 * Add new blocks to the chain
8379 while (oldNumBlocks
< newNumBlocks
)
8381 blockIndex
= SmallBlockChainStream_GetNextFreeBlock(This
);
8382 SmallBlockChainStream_SetNextBlockInChain(This
, currentBlock
, blockIndex
);
8384 SmallBlockChainStream_SetNextBlockInChain(
8387 BLOCK_END_OF_CHAIN
);
8389 currentBlock
= blockIndex
;
8396 /******************************************************************************
8397 * SmallBlockChainStream_SetSize
8399 * Sets the size of this stream.
8400 * The file will grow if we grow the chain.
8402 * TODO: Free the actual blocks in the file when we shrink the chain.
8403 * Currently, the blocks are still in the file. So the file size
8404 * doesn't shrink even if we shrink streams.
8406 BOOL
SmallBlockChainStream_SetSize(
8407 SmallBlockChainStream
* This
,
8408 ULARGE_INTEGER newSize
)
8410 ULARGE_INTEGER size
= SmallBlockChainStream_GetSize(This
);
8412 if (newSize
.u
.LowPart
== size
.u
.LowPart
)
8415 if (newSize
.u
.LowPart
< size
.u
.LowPart
)
8417 SmallBlockChainStream_Shrink(This
, newSize
);
8421 SmallBlockChainStream_Enlarge(This
, newSize
);
8427 /******************************************************************************
8428 * SmallBlockChainStream_GetCount
8430 * Returns the number of small blocks that comprises this chain.
8431 * This is not the size of the stream as the last block may not be full!
8434 static ULONG
SmallBlockChainStream_GetCount(SmallBlockChainStream
* This
)
8439 blockIndex
= SmallBlockChainStream_GetHeadOfChain(This
);
8441 while(blockIndex
!= BLOCK_END_OF_CHAIN
)
8445 if(FAILED(SmallBlockChainStream_GetNextBlockInChain(This
,
8446 blockIndex
, &blockIndex
)))
8453 /******************************************************************************
8454 * SmallBlockChainStream_GetSize
8456 * Returns the size of this chain.
8458 static ULARGE_INTEGER
SmallBlockChainStream_GetSize(SmallBlockChainStream
* This
)
8460 DirEntry chainEntry
;
8462 if(This
->headOfStreamPlaceHolder
!= NULL
)
8464 ULARGE_INTEGER result
;
8465 result
.u
.HighPart
= 0;
8467 result
.u
.LowPart
= SmallBlockChainStream_GetCount(This
) *
8468 This
->parentStorage
->smallBlockSize
;
8473 StorageImpl_ReadDirEntry(
8474 This
->parentStorage
,
8475 This
->ownerDirEntry
,
8478 return chainEntry
.size
;
8482 /************************************************************************
8483 * Miscellaneous storage functions
8484 ***********************************************************************/
8486 static HRESULT
create_storagefile(
8490 STGOPTIONS
* pStgOptions
,
8494 StorageBaseImpl
* newStorage
= 0;
8495 HANDLE hFile
= INVALID_HANDLE_VALUE
;
8496 HRESULT hr
= STG_E_INVALIDFLAG
;
8500 DWORD fileAttributes
;
8501 WCHAR tempFileName
[MAX_PATH
];
8504 return STG_E_INVALIDPOINTER
;
8506 if (pStgOptions
->ulSectorSize
!= MIN_BIG_BLOCK_SIZE
&& pStgOptions
->ulSectorSize
!= MAX_BIG_BLOCK_SIZE
)
8507 return STG_E_INVALIDPARAMETER
;
8509 /* if no share mode given then DENY_NONE is the default */
8510 if (STGM_SHARE_MODE(grfMode
) == 0)
8511 grfMode
|= STGM_SHARE_DENY_NONE
;
8513 if ( FAILED( validateSTGM(grfMode
) ))
8516 /* StgCreateDocFile seems to refuse readonly access, despite MSDN */
8517 switch(STGM_ACCESS_MODE(grfMode
))
8520 case STGM_READWRITE
:
8526 /* in direct mode, can only use SHARE_EXCLUSIVE */
8527 if (!(grfMode
& STGM_TRANSACTED
) && (STGM_SHARE_MODE(grfMode
) != STGM_SHARE_EXCLUSIVE
))
8530 /* but in transacted mode, any share mode is valid */
8533 * Generate a unique name.
8537 WCHAR tempPath
[MAX_PATH
];
8538 static const WCHAR prefix
[] = { 'S', 'T', 'O', 0 };
8540 memset(tempPath
, 0, sizeof(tempPath
));
8541 memset(tempFileName
, 0, sizeof(tempFileName
));
8543 if ((GetTempPathW(MAX_PATH
, tempPath
)) == 0 )
8546 if (GetTempFileNameW(tempPath
, prefix
, 0, tempFileName
) != 0)
8547 pwcsName
= tempFileName
;
8550 hr
= STG_E_INSUFFICIENTMEMORY
;
8554 creationMode
= TRUNCATE_EXISTING
;
8558 creationMode
= GetCreationModeFromSTGM(grfMode
);
8562 * Interpret the STGM value grfMode
8564 shareMode
= GetShareModeFromSTGM(grfMode
);
8565 accessMode
= GetAccessModeFromSTGM(grfMode
);
8567 if (grfMode
& STGM_DELETEONRELEASE
)
8568 fileAttributes
= FILE_FLAG_RANDOM_ACCESS
| FILE_FLAG_DELETE_ON_CLOSE
;
8570 fileAttributes
= FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_RANDOM_ACCESS
;
8574 hFile
= CreateFileW(pwcsName
,
8582 if (hFile
== INVALID_HANDLE_VALUE
)
8584 if(GetLastError() == ERROR_FILE_EXISTS
)
8585 hr
= STG_E_FILEALREADYEXISTS
;
8592 * Allocate and initialize the new IStorage object.
8594 hr
= Storage_Construct(
8601 pStgOptions
->ulSectorSize
,
8609 hr
= IStorage_QueryInterface(&newStorage
->IStorage_iface
, riid
, ppstgOpen
);
8610 IStorage_Release(&newStorage
->IStorage_iface
);
8613 TRACE("<-- %p r = %08x\n", *ppstgOpen
, hr
);
8618 /******************************************************************************
8619 * StgCreateDocfile [OLE32.@]
8620 * Creates a new compound file storage object
8623 * pwcsName [ I] Unicode string with filename (can be relative or NULL)
8624 * grfMode [ I] Access mode for opening the new storage object (see STGM_ constants)
8625 * reserved [ ?] unused?, usually 0
8626 * ppstgOpen [IO] A pointer to IStorage pointer to the new onject
8629 * S_OK if the file was successfully created
8630 * some STG_E_ value if error
8632 * if pwcsName is NULL, create file with new unique name
8633 * the function can returns
8634 * STG_S_CONVERTED if the specified file was successfully converted to storage format
8637 HRESULT WINAPI
StgCreateDocfile(
8641 IStorage
**ppstgOpen
)
8643 STGOPTIONS stgoptions
= {1, 0, 512};
8645 TRACE("(%s, %x, %d, %p)\n",
8646 debugstr_w(pwcsName
), grfMode
,
8647 reserved
, ppstgOpen
);
8650 return STG_E_INVALIDPOINTER
;
8652 return STG_E_INVALIDPARAMETER
;
8654 return create_storagefile(pwcsName
, grfMode
, 0, &stgoptions
, &IID_IStorage
, (void**)ppstgOpen
);
8657 /******************************************************************************
8658 * StgCreateStorageEx [OLE32.@]
8660 HRESULT WINAPI
StgCreateStorageEx(const WCHAR
* pwcsName
, DWORD grfMode
, DWORD stgfmt
, DWORD grfAttrs
, STGOPTIONS
* pStgOptions
, void* reserved
, REFIID riid
, void** ppObjectOpen
)
8662 TRACE("(%s, %x, %x, %x, %p, %p, %p, %p)\n", debugstr_w(pwcsName
),
8663 grfMode
, stgfmt
, grfAttrs
, pStgOptions
, reserved
, riid
, ppObjectOpen
);
8665 if (stgfmt
!= STGFMT_FILE
&& grfAttrs
!= 0)
8667 ERR("grfAttrs must be 0 if stgfmt != STGFMT_FILE\n");
8668 return STG_E_INVALIDPARAMETER
;
8671 if (stgfmt
== STGFMT_FILE
&& grfAttrs
!= 0 && grfAttrs
!= FILE_FLAG_NO_BUFFERING
)
8673 ERR("grfAttrs must be 0 or FILE_FLAG_NO_BUFFERING if stgfmt == STGFMT_FILE\n");
8674 return STG_E_INVALIDPARAMETER
;
8677 if (stgfmt
== STGFMT_FILE
)
8679 ERR("Cannot use STGFMT_FILE - this is NTFS only\n");
8680 return STG_E_INVALIDPARAMETER
;
8683 if (stgfmt
== STGFMT_STORAGE
|| stgfmt
== STGFMT_DOCFILE
)
8685 STGOPTIONS defaultOptions
= {1, 0, 512};
8687 if (!pStgOptions
) pStgOptions
= &defaultOptions
;
8688 return create_storagefile(pwcsName
, grfMode
, grfAttrs
, pStgOptions
, riid
, ppObjectOpen
);
8692 ERR("Invalid stgfmt argument\n");
8693 return STG_E_INVALIDPARAMETER
;
8696 /******************************************************************************
8697 * StgCreatePropSetStg [OLE32.@]
8699 HRESULT WINAPI
StgCreatePropSetStg(IStorage
*pstg
, DWORD reserved
,
8700 IPropertySetStorage
**propset
)
8702 TRACE("(%p, 0x%x, %p)\n", pstg
, reserved
, propset
);
8704 return STG_E_INVALIDPARAMETER
;
8706 return IStorage_QueryInterface(pstg
, &IID_IPropertySetStorage
, (void**)propset
);
8709 /******************************************************************************
8710 * StgOpenStorageEx [OLE32.@]
8712 HRESULT WINAPI
StgOpenStorageEx(const WCHAR
* pwcsName
, DWORD grfMode
, DWORD stgfmt
, DWORD grfAttrs
, STGOPTIONS
* pStgOptions
, void* reserved
, REFIID riid
, void** ppObjectOpen
)
8714 TRACE("(%s, %x, %x, %x, %p, %p, %p, %p)\n", debugstr_w(pwcsName
),
8715 grfMode
, stgfmt
, grfAttrs
, pStgOptions
, reserved
, riid
, ppObjectOpen
);
8717 if (stgfmt
!= STGFMT_DOCFILE
&& grfAttrs
!= 0)
8719 ERR("grfAttrs must be 0 if stgfmt != STGFMT_DOCFILE\n");
8720 return STG_E_INVALIDPARAMETER
;
8726 ERR("Cannot use STGFMT_FILE - this is NTFS only\n");
8727 return STG_E_INVALIDPARAMETER
;
8729 case STGFMT_STORAGE
:
8732 case STGFMT_DOCFILE
:
8733 if (grfAttrs
&& grfAttrs
!= FILE_FLAG_NO_BUFFERING
)
8735 ERR("grfAttrs must be 0 or FILE_FLAG_NO_BUFFERING if stgfmt == STGFMT_DOCFILE\n");
8736 return STG_E_INVALIDPARAMETER
;
8738 FIXME("Stub: calling StgOpenStorage, but ignoring pStgOptions and grfAttrs\n");
8742 WARN("STGFMT_ANY assuming storage\n");
8746 return STG_E_INVALIDPARAMETER
;
8749 return StgOpenStorage(pwcsName
, NULL
, grfMode
, NULL
, 0, (IStorage
**)ppObjectOpen
);
8753 /******************************************************************************
8754 * StgOpenStorage [OLE32.@]
8756 HRESULT WINAPI
StgOpenStorage(
8757 const OLECHAR
*pwcsName
,
8758 IStorage
*pstgPriority
,
8762 IStorage
**ppstgOpen
)
8764 StorageBaseImpl
* newStorage
= 0;
8769 LPWSTR temp_name
= NULL
;
8771 TRACE("(%s, %p, %x, %p, %d, %p)\n",
8772 debugstr_w(pwcsName
), pstgPriority
, grfMode
,
8773 snbExclude
, reserved
, ppstgOpen
);
8777 /* FIXME: Copy ILockBytes instead? But currently for STGM_PRIORITY it'll be read-only. */
8778 hr
= StorageBaseImpl_GetFilename((StorageBaseImpl
*)pstgPriority
, &temp_name
);
8779 if (FAILED(hr
)) goto end
;
8780 pwcsName
= temp_name
;
8781 TRACE("using filename %s\n", debugstr_w(temp_name
));
8786 hr
= STG_E_INVALIDNAME
;
8792 hr
= STG_E_INVALIDPOINTER
;
8798 hr
= STG_E_INVALIDPARAMETER
;
8802 if (grfMode
& STGM_PRIORITY
)
8804 if (grfMode
& (STGM_TRANSACTED
|STGM_SIMPLE
|STGM_NOSCRATCH
|STGM_NOSNAPSHOT
))
8805 return STG_E_INVALIDFLAG
;
8806 if (grfMode
& STGM_DELETEONRELEASE
)
8807 return STG_E_INVALIDFUNCTION
;
8808 if(STGM_ACCESS_MODE(grfMode
) != STGM_READ
)
8809 return STG_E_INVALIDFLAG
;
8810 grfMode
&= ~0xf0; /* remove the existing sharing mode */
8811 grfMode
|= STGM_SHARE_DENY_NONE
;
8815 * Validate the sharing mode
8817 if (grfMode
& STGM_DIRECT_SWMR
)
8819 if ((STGM_SHARE_MODE(grfMode
) != STGM_SHARE_DENY_WRITE
) &&
8820 (STGM_SHARE_MODE(grfMode
) != STGM_SHARE_DENY_NONE
))
8822 hr
= STG_E_INVALIDFLAG
;
8826 else if (!(grfMode
& (STGM_TRANSACTED
|STGM_PRIORITY
)))
8827 switch(STGM_SHARE_MODE(grfMode
))
8829 case STGM_SHARE_EXCLUSIVE
:
8830 case STGM_SHARE_DENY_WRITE
:
8833 hr
= STG_E_INVALIDFLAG
;
8837 if ( FAILED( validateSTGM(grfMode
) ) ||
8838 (grfMode
&STGM_CREATE
))
8840 hr
= STG_E_INVALIDFLAG
;
8844 /* shared reading requires transacted or single writer mode */
8845 if( STGM_SHARE_MODE(grfMode
) == STGM_SHARE_DENY_WRITE
&&
8846 STGM_ACCESS_MODE(grfMode
) == STGM_READWRITE
&&
8847 !(grfMode
& STGM_TRANSACTED
) && !(grfMode
& STGM_DIRECT_SWMR
))
8849 hr
= STG_E_INVALIDFLAG
;
8854 * Interpret the STGM value grfMode
8856 shareMode
= GetShareModeFromSTGM(grfMode
);
8857 accessMode
= GetAccessModeFromSTGM(grfMode
);
8861 hFile
= CreateFileW( pwcsName
,
8866 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_RANDOM_ACCESS
,
8869 if (hFile
==INVALID_HANDLE_VALUE
)
8871 DWORD last_error
= GetLastError();
8877 case ERROR_FILE_NOT_FOUND
:
8878 hr
= STG_E_FILENOTFOUND
;
8881 case ERROR_PATH_NOT_FOUND
:
8882 hr
= STG_E_PATHNOTFOUND
;
8885 case ERROR_ACCESS_DENIED
:
8886 case ERROR_WRITE_PROTECT
:
8887 hr
= STG_E_ACCESSDENIED
;
8890 case ERROR_SHARING_VIOLATION
:
8891 hr
= STG_E_SHAREVIOLATION
;
8902 * Refuse to open the file if it's too small to be a structured storage file
8903 * FIXME: verify the file when reading instead of here
8905 if (GetFileSize(hFile
, NULL
) < HEADER_SIZE
)
8908 hr
= STG_E_FILEALREADYEXISTS
;
8913 * Allocate and initialize the new IStorage object.
8915 hr
= Storage_Construct(
8928 * According to the docs if the file is not a storage, return STG_E_FILEALREADYEXISTS
8930 if(hr
== STG_E_INVALIDHEADER
)
8931 hr
= STG_E_FILEALREADYEXISTS
;
8935 *ppstgOpen
= &newStorage
->IStorage_iface
;
8938 CoTaskMemFree(temp_name
);
8939 if (pstgPriority
) IStorage_Release(pstgPriority
);
8940 TRACE("<-- %08x, IStorage %p\n", hr
, ppstgOpen
? *ppstgOpen
: NULL
);
8944 /******************************************************************************
8945 * StgCreateDocfileOnILockBytes [OLE32.@]
8947 HRESULT WINAPI
StgCreateDocfileOnILockBytes(
8951 IStorage
** ppstgOpen
)
8953 StorageBaseImpl
* newStorage
= 0;
8956 if ((ppstgOpen
== 0) || (plkbyt
== 0))
8957 return STG_E_INVALIDPOINTER
;
8960 * Allocate and initialize the new IStorage object.
8962 hr
= Storage_Construct(
8977 *ppstgOpen
= &newStorage
->IStorage_iface
;
8982 /******************************************************************************
8983 * StgOpenStorageOnILockBytes [OLE32.@]
8985 HRESULT WINAPI
StgOpenStorageOnILockBytes(
8987 IStorage
*pstgPriority
,
8991 IStorage
**ppstgOpen
)
8993 StorageBaseImpl
* newStorage
= 0;
8996 if ((plkbyt
== 0) || (ppstgOpen
== 0))
8997 return STG_E_INVALIDPOINTER
;
8999 if ( FAILED( validateSTGM(grfMode
) ))
9000 return STG_E_INVALIDFLAG
;
9005 * Allocate and initialize the new IStorage object.
9007 hr
= Storage_Construct(
9022 *ppstgOpen
= &newStorage
->IStorage_iface
;
9027 /******************************************************************************
9028 * StgSetTimes [ole32.@]
9029 * StgSetTimes [OLE32.@]
9033 HRESULT WINAPI
StgSetTimes(OLECHAR
const *str
, FILETIME
const *pctime
,
9034 FILETIME
const *patime
, FILETIME
const *pmtime
)
9036 IStorage
*stg
= NULL
;
9039 TRACE("%s %p %p %p\n", debugstr_w(str
), pctime
, patime
, pmtime
);
9041 r
= StgOpenStorage(str
, NULL
, STGM_READWRITE
| STGM_SHARE_DENY_WRITE
,
9045 r
= IStorage_SetElementTimes(stg
, NULL
, pctime
, patime
, pmtime
);
9046 IStorage_Release(stg
);
9052 /******************************************************************************
9053 * StgIsStorageILockBytes [OLE32.@]
9055 * Determines if the ILockBytes contains a storage object.
9057 HRESULT WINAPI
StgIsStorageILockBytes(ILockBytes
*plkbyt
)
9059 BYTE sig
[sizeof(STORAGE_magic
)];
9060 ULARGE_INTEGER offset
;
9063 offset
.u
.HighPart
= 0;
9064 offset
.u
.LowPart
= 0;
9066 ILockBytes_ReadAt(plkbyt
, offset
, sig
, sizeof(sig
), &read
);
9068 if (read
== sizeof(sig
) && memcmp(sig
, STORAGE_magic
, sizeof(sig
)) == 0)
9074 /******************************************************************************
9075 * WriteClassStg [OLE32.@]
9077 * This method will store the specified CLSID in the specified storage object
9079 HRESULT WINAPI
WriteClassStg(IStorage
* pStg
, REFCLSID rclsid
)
9082 return E_INVALIDARG
;
9085 return STG_E_INVALIDPOINTER
;
9087 return IStorage_SetClass(pStg
, rclsid
);
9090 /***********************************************************************
9091 * ReadClassStg (OLE32.@)
9093 * This method reads the CLSID previously written to a storage object with
9094 * the WriteClassStg.
9097 * pstg [I] IStorage pointer
9098 * pclsid [O] Pointer to where the CLSID is written
9102 * Failure: HRESULT code.
9104 HRESULT WINAPI
ReadClassStg(IStorage
*pstg
,CLSID
*pclsid
){
9109 TRACE("(%p, %p)\n", pstg
, pclsid
);
9111 if(!pstg
|| !pclsid
)
9112 return E_INVALIDARG
;
9115 * read a STATSTG structure (contains the clsid) from the storage
9117 hRes
=IStorage_Stat(pstg
,&pstatstg
,STATFLAG_NONAME
);
9120 *pclsid
=pstatstg
.clsid
;
9125 /***********************************************************************
9126 * OleLoadFromStream (OLE32.@)
9128 * This function loads an object from stream
9130 HRESULT WINAPI
OleLoadFromStream(IStream
*pStm
,REFIID iidInterface
,void** ppvObj
)
9134 LPPERSISTSTREAM xstm
;
9136 TRACE("(%p,%s,%p)\n",pStm
,debugstr_guid(iidInterface
),ppvObj
);
9138 res
=ReadClassStm(pStm
,&clsid
);
9141 res
=CoCreateInstance(&clsid
,NULL
,CLSCTX_INPROC_SERVER
,iidInterface
,ppvObj
);
9144 res
=IUnknown_QueryInterface((IUnknown
*)*ppvObj
,&IID_IPersistStream
,(LPVOID
*)&xstm
);
9146 IUnknown_Release((IUnknown
*)*ppvObj
);
9149 res
=IPersistStream_Load(xstm
,pStm
);
9150 IPersistStream_Release(xstm
);
9151 /* FIXME: all refcounts ok at this point? I think they should be:
9154 * xstm : 0 (released)
9159 /***********************************************************************
9160 * OleSaveToStream (OLE32.@)
9162 * This function saves an object with the IPersistStream interface on it
9163 * to the specified stream.
9165 HRESULT WINAPI
OleSaveToStream(IPersistStream
*pPStm
,IStream
*pStm
)
9171 TRACE("(%p,%p)\n",pPStm
,pStm
);
9173 res
=IPersistStream_GetClassID(pPStm
,&clsid
);
9175 if (SUCCEEDED(res
)){
9177 res
=WriteClassStm(pStm
,&clsid
);
9181 res
=IPersistStream_Save(pPStm
,pStm
,TRUE
);
9184 TRACE("Finished Save\n");
9188 /*************************************************************************
9189 * STORAGE_CreateOleStream [Internal]
9191 * Creates the "\001OLE" stream in the IStorage if necessary.
9194 * storage [I] Dest storage to create the stream in
9195 * flags [I] flags to be set for newly created stream
9198 * HRESULT return value
9202 * This stream is still unknown, MS Word seems to have extra data
9203 * but since the data is stored in the OLESTREAM there should be
9204 * no need to recreate the stream. If the stream is manually
9205 * deleted it will create it with this default data.
9208 HRESULT
STORAGE_CreateOleStream(IStorage
*storage
, DWORD flags
)
9210 static const WCHAR stream_1oleW
[] = {1,'O','l','e',0};
9211 static const DWORD version_magic
= 0x02000001;
9215 hr
= IStorage_CreateStream(storage
, stream_1oleW
, STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stream
);
9218 struct empty_1ole_stream
{
9219 DWORD version_magic
;
9221 DWORD update_options
;
9223 DWORD mon_stream_size
;
9225 struct empty_1ole_stream stream_data
;
9227 stream_data
.version_magic
= version_magic
;
9228 stream_data
.flags
= flags
;
9229 stream_data
.update_options
= 0;
9230 stream_data
.reserved
= 0;
9231 stream_data
.mon_stream_size
= 0;
9233 hr
= IStream_Write(stream
, &stream_data
, sizeof(stream_data
), NULL
);
9234 IStream_Release(stream
);
9240 /* write a string to a stream, preceded by its length */
9241 static HRESULT
STREAM_WriteString( IStream
*stm
, LPCWSTR string
)
9248 len
= WideCharToMultiByte( CP_ACP
, 0, string
, -1, NULL
, 0, NULL
, NULL
);
9249 r
= IStream_Write( stm
, &len
, sizeof(len
), NULL
);
9254 str
= CoTaskMemAlloc( len
);
9255 WideCharToMultiByte( CP_ACP
, 0, string
, -1, str
, len
, NULL
, NULL
);
9256 r
= IStream_Write( stm
, str
, len
, NULL
);
9257 CoTaskMemFree( str
);
9261 /* read a string preceded by its length from a stream */
9262 static HRESULT
STREAM_ReadString( IStream
*stm
, LPWSTR
*string
)
9265 DWORD len
, count
= 0;
9269 r
= IStream_Read( stm
, &len
, sizeof(len
), &count
);
9272 if( count
!= sizeof(len
) )
9273 return E_OUTOFMEMORY
;
9275 TRACE("%d bytes\n",len
);
9277 str
= CoTaskMemAlloc( len
);
9279 return E_OUTOFMEMORY
;
9281 r
= IStream_Read( stm
, str
, len
, &count
);
9284 CoTaskMemFree( str
);
9289 CoTaskMemFree( str
);
9290 return E_OUTOFMEMORY
;
9293 TRACE("Read string %s\n",debugstr_an(str
,len
));
9295 len
= MultiByteToWideChar( CP_ACP
, 0, str
, count
, NULL
, 0 );
9296 wstr
= CoTaskMemAlloc( (len
+ 1)*sizeof (WCHAR
) );
9299 MultiByteToWideChar( CP_ACP
, 0, str
, count
, wstr
, len
);
9302 CoTaskMemFree( str
);
9310 static HRESULT
STORAGE_WriteCompObj( LPSTORAGE pstg
, CLSID
*clsid
,
9311 LPCWSTR lpszUserType
, LPCWSTR szClipName
, LPCWSTR szProgIDName
)
9315 static const WCHAR szwStreamName
[] = {1, 'C', 'o', 'm', 'p', 'O', 'b', 'j', 0};
9317 static const BYTE unknown1
[12] =
9318 { 0x01, 0x00, 0xFE, 0xFF, 0x03, 0x0A, 0x00, 0x00,
9319 0xFF, 0xFF, 0xFF, 0xFF};
9320 static const BYTE unknown2
[16] =
9321 { 0xF4, 0x39, 0xB2, 0x71, 0x00, 0x00, 0x00, 0x00,
9322 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
9324 TRACE("%p %s %s %s %s\n", pstg
, debugstr_guid(clsid
),
9325 debugstr_w(lpszUserType
), debugstr_w(szClipName
),
9326 debugstr_w(szProgIDName
));
9328 /* Create a CompObj stream */
9329 r
= IStorage_CreateStream(pstg
, szwStreamName
,
9330 STGM_CREATE
| STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &pstm
);
9334 /* Write CompObj Structure to stream */
9335 r
= IStream_Write(pstm
, unknown1
, sizeof(unknown1
), NULL
);
9337 if( SUCCEEDED( r
) )
9338 r
= WriteClassStm( pstm
, clsid
);
9340 if( SUCCEEDED( r
) )
9341 r
= STREAM_WriteString( pstm
, lpszUserType
);
9342 if( SUCCEEDED( r
) )
9343 r
= STREAM_WriteString( pstm
, szClipName
);
9344 if( SUCCEEDED( r
) )
9345 r
= STREAM_WriteString( pstm
, szProgIDName
);
9346 if( SUCCEEDED( r
) )
9347 r
= IStream_Write(pstm
, unknown2
, sizeof(unknown2
), NULL
);
9349 IStream_Release( pstm
);
9354 /***********************************************************************
9355 * WriteFmtUserTypeStg (OLE32.@)
9357 HRESULT WINAPI
WriteFmtUserTypeStg(
9358 LPSTORAGE pstg
, CLIPFORMAT cf
, LPOLESTR lpszUserType
)
9362 WCHAR szwClipName
[0x40];
9364 LPWSTR wstrProgID
= NULL
;
9367 TRACE("(%p,%x,%s)\n",pstg
,cf
,debugstr_w(lpszUserType
));
9369 /* get the clipboard format name */
9372 n
= GetClipboardFormatNameW(cf
, szwClipName
, ARRAY_SIZE(szwClipName
));
9376 TRACE("Clipboard name is %s\n", debugstr_w(szwClipName
));
9378 r
= IStorage_Stat(pstg
, &stat
, STATFLAG_NONAME
);
9384 ProgIDFromCLSID(&clsid
, &wstrProgID
);
9386 TRACE("progid is %s\n",debugstr_w(wstrProgID
));
9388 r
= STORAGE_WriteCompObj( pstg
, &clsid
, lpszUserType
,
9389 cf
? szwClipName
: NULL
, wstrProgID
);
9391 CoTaskMemFree(wstrProgID
);
9397 /******************************************************************************
9398 * ReadFmtUserTypeStg [OLE32.@]
9400 HRESULT WINAPI
ReadFmtUserTypeStg (LPSTORAGE pstg
, CLIPFORMAT
* pcf
, LPOLESTR
* lplpszUserType
)
9404 static const WCHAR szCompObj
[] = { 1, 'C','o','m','p','O','b','j', 0 };
9405 unsigned char unknown1
[12];
9406 unsigned char unknown2
[16];
9408 LPWSTR szProgIDName
= NULL
, szCLSIDName
= NULL
, szOleTypeName
= NULL
;
9411 TRACE("(%p,%p,%p)\n", pstg
, pcf
, lplpszUserType
);
9413 r
= IStorage_OpenStream( pstg
, szCompObj
, NULL
,
9414 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
9417 WARN("Failed to open stream r = %08x\n", r
);
9421 /* read the various parts of the structure */
9422 r
= IStream_Read( stm
, unknown1
, sizeof(unknown1
), &count
);
9423 if( FAILED( r
) || ( count
!= sizeof(unknown1
) ) )
9425 r
= ReadClassStm( stm
, &clsid
);
9429 r
= STREAM_ReadString( stm
, &szCLSIDName
);
9433 r
= STREAM_ReadString( stm
, &szOleTypeName
);
9437 r
= STREAM_ReadString( stm
, &szProgIDName
);
9441 r
= IStream_Read( stm
, unknown2
, sizeof(unknown2
), &count
);
9442 if( FAILED( r
) || ( count
!= sizeof(unknown2
) ) )
9445 /* ok, success... now we just need to store what we found */
9447 *pcf
= RegisterClipboardFormatW( szOleTypeName
);
9449 if( lplpszUserType
)
9451 *lplpszUserType
= szCLSIDName
;
9456 CoTaskMemFree( szCLSIDName
);
9457 CoTaskMemFree( szOleTypeName
);
9458 CoTaskMemFree( szProgIDName
);
9459 IStream_Release( stm
);
9464 /******************************************************************************
9465 * StgIsStorageFile [OLE32.@]
9466 * Verify if the file contains a storage object
9472 * S_OK if file has magic bytes as a storage object
9473 * S_FALSE if file is not storage
9476 StgIsStorageFile(LPCOLESTR fn
)
9482 TRACE("%s\n", debugstr_w(fn
));
9483 hf
= CreateFileW(fn
, GENERIC_READ
,
9484 FILE_SHARE_DELETE
| FILE_SHARE_READ
| FILE_SHARE_WRITE
,
9485 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
9487 if (hf
== INVALID_HANDLE_VALUE
)
9488 return STG_E_FILENOTFOUND
;
9490 if (!ReadFile(hf
, magic
, 8, &bytes_read
, NULL
))
9492 WARN(" unable to read file\n");
9499 if (bytes_read
!= 8) {
9500 TRACE(" too short\n");
9504 if (!memcmp(magic
,STORAGE_magic
,8)) {
9509 TRACE(" -> Invalid header.\n");
9513 /***********************************************************************
9514 * WriteClassStm (OLE32.@)
9516 * Writes a CLSID to a stream.
9519 * pStm [I] Stream to write to.
9520 * rclsid [I] CLSID to write.
9524 * Failure: HRESULT code.
9526 HRESULT WINAPI
WriteClassStm(IStream
*pStm
,REFCLSID rclsid
)
9528 TRACE("(%p,%p)\n",pStm
,rclsid
);
9530 if (!pStm
|| !rclsid
)
9531 return E_INVALIDARG
;
9533 return IStream_Write(pStm
,rclsid
,sizeof(CLSID
),NULL
);
9536 /***********************************************************************
9537 * ReadClassStm (OLE32.@)
9539 * Reads a CLSID from a stream.
9542 * pStm [I] Stream to read from.
9543 * rclsid [O] CLSID to read.
9547 * Failure: HRESULT code.
9549 HRESULT WINAPI
ReadClassStm(IStream
*pStm
,CLSID
*pclsid
)
9554 TRACE("(%p,%p)\n",pStm
,pclsid
);
9556 if (!pStm
|| !pclsid
)
9557 return E_INVALIDARG
;
9559 /* clear the output args */
9560 *pclsid
= CLSID_NULL
;
9562 res
= IStream_Read(pStm
, pclsid
, sizeof(CLSID
), &nbByte
);
9567 if (nbByte
!= sizeof(CLSID
))
9568 return STG_E_READFAULT
;
9574 /************************************************************************
9575 * OleConvert Functions
9576 ***********************************************************************/
9578 #define OLESTREAM_ID 0x501
9579 #define OLESTREAM_MAX_STR_LEN 255
9581 /* OLESTREAM memory structure to use for Get and Put Routines */
9586 DWORD dwOleTypeNameLength
;
9587 CHAR strOleTypeName
[OLESTREAM_MAX_STR_LEN
];
9588 CHAR
*pstrOleObjFileName
;
9589 DWORD dwOleObjFileNameLength
;
9590 DWORD dwMetaFileWidth
;
9591 DWORD dwMetaFileHeight
;
9592 CHAR strUnknown
[8]; /* don't know what this 8 byte information in OLE stream is. */
9595 } OLECONVERT_OLESTREAM_DATA
;
9597 /* CompObj Stream structure */
9600 BYTE byUnknown1
[12];
9602 DWORD dwCLSIDNameLength
;
9603 CHAR strCLSIDName
[OLESTREAM_MAX_STR_LEN
];
9604 DWORD dwOleTypeNameLength
;
9605 CHAR strOleTypeName
[OLESTREAM_MAX_STR_LEN
];
9606 DWORD dwProgIDNameLength
;
9607 CHAR strProgIDName
[OLESTREAM_MAX_STR_LEN
];
9608 BYTE byUnknown2
[16];
9609 } OLECONVERT_ISTORAGE_COMPOBJ
;
9611 /* Ole Presentation Stream structure */
9614 BYTE byUnknown1
[28];
9619 } OLECONVERT_ISTORAGE_OLEPRES
;
9622 /*************************************************************************
9623 * OLECONVERT_LoadOLE10 [Internal]
9625 * Loads the OLE10 STREAM to memory
9628 * pOleStream [I] The OLESTREAM
9629 * pData [I] Data Structure for the OLESTREAM Data
9633 * Failure: CONVERT10_E_OLESTREAM_GET for invalid Get
9634 * CONVERT10_E_OLESTREAM_FMT if the OLEID is invalid
9637 * This function is used by OleConvertOLESTREAMToIStorage only.
9639 * Memory allocated for pData must be freed by the caller
9641 static HRESULT
OLECONVERT_LoadOLE10(LPOLESTREAM pOleStream
, OLECONVERT_OLESTREAM_DATA
*pData
, BOOL bStrem1
)
9644 HRESULT hRes
= S_OK
;
9648 pData
->pData
= NULL
;
9649 pData
->pstrOleObjFileName
= NULL
;
9651 for( nTryCnt
=0;nTryCnt
< max_try
; nTryCnt
++)
9654 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, (void *)&(pData
->dwOleID
), sizeof(pData
->dwOleID
));
9655 if(dwSize
!= sizeof(pData
->dwOleID
))
9657 hRes
= CONVERT10_E_OLESTREAM_GET
;
9659 else if(pData
->dwOleID
!= OLESTREAM_ID
)
9661 hRes
= CONVERT10_E_OLESTREAM_FMT
;
9672 /* Get the TypeID... more info needed for this field */
9673 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, (void *)&(pData
->dwTypeID
), sizeof(pData
->dwTypeID
));
9674 if(dwSize
!= sizeof(pData
->dwTypeID
))
9676 hRes
= CONVERT10_E_OLESTREAM_GET
;
9681 if(pData
->dwTypeID
!= 0)
9683 /* Get the length of the OleTypeName */
9684 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, (void *) &(pData
->dwOleTypeNameLength
), sizeof(pData
->dwOleTypeNameLength
));
9685 if(dwSize
!= sizeof(pData
->dwOleTypeNameLength
))
9687 hRes
= CONVERT10_E_OLESTREAM_GET
;
9692 if(pData
->dwOleTypeNameLength
> 0)
9694 /* Get the OleTypeName */
9695 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, pData
->strOleTypeName
, pData
->dwOleTypeNameLength
);
9696 if(dwSize
!= pData
->dwOleTypeNameLength
)
9698 hRes
= CONVERT10_E_OLESTREAM_GET
;
9704 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, (void *)&(pData
->dwOleObjFileNameLength
), sizeof(pData
->dwOleObjFileNameLength
));
9705 if(dwSize
!= sizeof(pData
->dwOleObjFileNameLength
))
9707 hRes
= CONVERT10_E_OLESTREAM_GET
;
9711 if(pData
->dwOleObjFileNameLength
< 1) /* there is no file name exist */
9712 pData
->dwOleObjFileNameLength
= sizeof(pData
->dwOleObjFileNameLength
);
9713 pData
->pstrOleObjFileName
= HeapAlloc(GetProcessHeap(), 0, pData
->dwOleObjFileNameLength
);
9714 if(pData
->pstrOleObjFileName
)
9716 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, pData
->pstrOleObjFileName
, pData
->dwOleObjFileNameLength
);
9717 if(dwSize
!= pData
->dwOleObjFileNameLength
)
9719 hRes
= CONVERT10_E_OLESTREAM_GET
;
9723 hRes
= CONVERT10_E_OLESTREAM_GET
;
9728 /* Get the Width of the Metafile */
9729 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, (void *)&(pData
->dwMetaFileWidth
), sizeof(pData
->dwMetaFileWidth
));
9730 if(dwSize
!= sizeof(pData
->dwMetaFileWidth
))
9732 hRes
= CONVERT10_E_OLESTREAM_GET
;
9736 /* Get the Height of the Metafile */
9737 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, (void *)&(pData
->dwMetaFileHeight
), sizeof(pData
->dwMetaFileHeight
));
9738 if(dwSize
!= sizeof(pData
->dwMetaFileHeight
))
9740 hRes
= CONVERT10_E_OLESTREAM_GET
;
9746 /* Get the Length of the Data */
9747 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, (void *)&(pData
->dwDataLength
), sizeof(pData
->dwDataLength
));
9748 if(dwSize
!= sizeof(pData
->dwDataLength
))
9750 hRes
= CONVERT10_E_OLESTREAM_GET
;
9754 if(hRes
== S_OK
) /* I don't know what this 8 byte information is. We have to figure out */
9756 if(!bStrem1
) /* if it is a second OLE stream data */
9758 pData
->dwDataLength
-= 8;
9759 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, pData
->strUnknown
, sizeof(pData
->strUnknown
));
9760 if(dwSize
!= sizeof(pData
->strUnknown
))
9762 hRes
= CONVERT10_E_OLESTREAM_GET
;
9768 if(pData
->dwDataLength
> 0)
9770 pData
->pData
= HeapAlloc(GetProcessHeap(),0,pData
->dwDataLength
);
9772 /* Get Data (ex. IStorage, Metafile, or BMP) */
9775 dwSize
= pOleStream
->lpstbl
->Get(pOleStream
, (void *)pData
->pData
, pData
->dwDataLength
);
9776 if(dwSize
!= pData
->dwDataLength
)
9778 hRes
= CONVERT10_E_OLESTREAM_GET
;
9783 hRes
= CONVERT10_E_OLESTREAM_GET
;
9792 /*************************************************************************
9793 * OLECONVERT_SaveOLE10 [Internal]
9795 * Saves the OLE10 STREAM From memory
9798 * pData [I] Data Structure for the OLESTREAM Data
9799 * pOleStream [I] The OLESTREAM to save
9803 * Failure: CONVERT10_E_OLESTREAM_PUT for invalid Put
9806 * This function is used by OleConvertIStorageToOLESTREAM only.
9809 static HRESULT
OLECONVERT_SaveOLE10(OLECONVERT_OLESTREAM_DATA
*pData
, LPOLESTREAM pOleStream
)
9812 HRESULT hRes
= S_OK
;
9816 dwSize
= pOleStream
->lpstbl
->Put(pOleStream
, (void *)&(pData
->dwOleID
), sizeof(pData
->dwOleID
));
9817 if(dwSize
!= sizeof(pData
->dwOleID
))
9819 hRes
= CONVERT10_E_OLESTREAM_PUT
;
9824 /* Set the TypeID */
9825 dwSize
= pOleStream
->lpstbl
->Put(pOleStream
, (void *)&(pData
->dwTypeID
), sizeof(pData
->dwTypeID
));
9826 if(dwSize
!= sizeof(pData
->dwTypeID
))
9828 hRes
= CONVERT10_E_OLESTREAM_PUT
;
9832 if(pData
->dwOleID
== OLESTREAM_ID
&& pData
->dwTypeID
!= 0 && hRes
== S_OK
)
9834 /* Set the Length of the OleTypeName */
9835 dwSize
= pOleStream
->lpstbl
->Put(pOleStream
, (void *)&(pData
->dwOleTypeNameLength
), sizeof(pData
->dwOleTypeNameLength
));
9836 if(dwSize
!= sizeof(pData
->dwOleTypeNameLength
))
9838 hRes
= CONVERT10_E_OLESTREAM_PUT
;
9843 if(pData
->dwOleTypeNameLength
> 0)
9845 /* Set the OleTypeName */
9846 dwSize
= pOleStream
->lpstbl
->Put(pOleStream
, pData
->strOleTypeName
, pData
->dwOleTypeNameLength
);
9847 if(dwSize
!= pData
->dwOleTypeNameLength
)
9849 hRes
= CONVERT10_E_OLESTREAM_PUT
;
9856 /* Set the width of the Metafile */
9857 dwSize
= pOleStream
->lpstbl
->Put(pOleStream
, (void *)&(pData
->dwMetaFileWidth
), sizeof(pData
->dwMetaFileWidth
));
9858 if(dwSize
!= sizeof(pData
->dwMetaFileWidth
))
9860 hRes
= CONVERT10_E_OLESTREAM_PUT
;
9866 /* Set the height of the Metafile */
9867 dwSize
= pOleStream
->lpstbl
->Put(pOleStream
, (void *)&(pData
->dwMetaFileHeight
), sizeof(pData
->dwMetaFileHeight
));
9868 if(dwSize
!= sizeof(pData
->dwMetaFileHeight
))
9870 hRes
= CONVERT10_E_OLESTREAM_PUT
;
9876 /* Set the length of the Data */
9877 dwSize
= pOleStream
->lpstbl
->Put(pOleStream
, (void *)&(pData
->dwDataLength
), sizeof(pData
->dwDataLength
));
9878 if(dwSize
!= sizeof(pData
->dwDataLength
))
9880 hRes
= CONVERT10_E_OLESTREAM_PUT
;
9886 if(pData
->dwDataLength
> 0)
9888 /* Set the Data (eg. IStorage, Metafile, Bitmap) */
9889 dwSize
= pOleStream
->lpstbl
->Put(pOleStream
, (void *) pData
->pData
, pData
->dwDataLength
);
9890 if(dwSize
!= pData
->dwDataLength
)
9892 hRes
= CONVERT10_E_OLESTREAM_PUT
;
9900 /*************************************************************************
9901 * OLECONVERT_GetOLE20FromOLE10[Internal]
9903 * This function copies OLE10 Data (the IStorage in the OLESTREAM) to disk,
9904 * opens it, and copies the content to the dest IStorage for
9905 * OleConvertOLESTREAMToIStorage
9909 * pDestStorage [I] The IStorage to copy the data to
9910 * pBuffer [I] Buffer that contains the IStorage from the OLESTREAM
9911 * nBufferLength [I] The size of the buffer
9920 static void OLECONVERT_GetOLE20FromOLE10(LPSTORAGE pDestStorage
, const BYTE
*pBuffer
, DWORD nBufferLength
)
9924 IStorage
*pTempStorage
;
9925 DWORD dwNumOfBytesWritten
;
9926 WCHAR wstrTempDir
[MAX_PATH
], wstrTempFile
[MAX_PATH
];
9927 static const WCHAR wstrPrefix
[] = {'s', 'i', 's', 0};
9929 /* Create a temp File */
9930 GetTempPathW(MAX_PATH
, wstrTempDir
);
9931 GetTempFileNameW(wstrTempDir
, wstrPrefix
, 0, wstrTempFile
);
9932 hFile
= CreateFileW(wstrTempFile
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
9934 if(hFile
!= INVALID_HANDLE_VALUE
)
9936 /* Write IStorage Data to File */
9937 WriteFile(hFile
, pBuffer
, nBufferLength
, &dwNumOfBytesWritten
, NULL
);
9940 /* Open and copy temp storage to the Dest Storage */
9941 hRes
= StgOpenStorage(wstrTempFile
, NULL
, STGM_READ
, NULL
, 0, &pTempStorage
);
9944 hRes
= IStorage_CopyTo(pTempStorage
, 0, NULL
, NULL
, pDestStorage
);
9945 IStorage_Release(pTempStorage
);
9947 DeleteFileW(wstrTempFile
);
9952 /*************************************************************************
9953 * OLECONVERT_WriteOLE20ToBuffer [Internal]
9955 * Saves the OLE10 STREAM From memory
9958 * pStorage [I] The Src IStorage to copy
9959 * pData [I] The Dest Memory to write to.
9962 * The size in bytes allocated for pData
9965 * Memory allocated for pData must be freed by the caller
9967 * Used by OleConvertIStorageToOLESTREAM only.
9970 static DWORD
OLECONVERT_WriteOLE20ToBuffer(LPSTORAGE pStorage
, BYTE
**pData
)
9974 DWORD nDataLength
= 0;
9975 IStorage
*pTempStorage
;
9976 WCHAR wstrTempDir
[MAX_PATH
], wstrTempFile
[MAX_PATH
];
9977 static const WCHAR wstrPrefix
[] = {'s', 'i', 's', 0};
9981 /* Create temp Storage */
9982 GetTempPathW(MAX_PATH
, wstrTempDir
);
9983 GetTempFileNameW(wstrTempDir
, wstrPrefix
, 0, wstrTempFile
);
9984 hRes
= StgCreateDocfile(wstrTempFile
, STGM_CREATE
| STGM_READWRITE
| STGM_SHARE_EXCLUSIVE
, 0, &pTempStorage
);
9988 /* Copy Src Storage to the Temp Storage */
9989 IStorage_CopyTo(pStorage
, 0, NULL
, NULL
, pTempStorage
);
9990 IStorage_Release(pTempStorage
);
9992 /* Open Temp Storage as a file and copy to memory */
9993 hFile
= CreateFileW(wstrTempFile
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
9994 if(hFile
!= INVALID_HANDLE_VALUE
)
9996 nDataLength
= GetFileSize(hFile
, NULL
);
9997 *pData
= HeapAlloc(GetProcessHeap(),0,nDataLength
);
9998 ReadFile(hFile
, *pData
, nDataLength
, &nDataLength
, 0);
10001 DeleteFileW(wstrTempFile
);
10003 return nDataLength
;
10006 /*************************************************************************
10007 * OLECONVERT_CreateCompObjStream [Internal]
10009 * Creates a "\001CompObj" is the destination IStorage if necessary.
10012 * pStorage [I] The dest IStorage to create the CompObj Stream
10014 * strOleTypeName [I] The ProgID
10018 * Failure: REGDB_E_CLASSNOTREG if cannot reconstruct the stream
10021 * This function is used by OleConvertOLESTREAMToIStorage only.
10023 * The stream data is stored in the OLESTREAM and there should be
10024 * no need to recreate the stream. If the stream is manually
10025 * deleted it will attempt to create it by querying the registry.
10029 HRESULT
OLECONVERT_CreateCompObjStream(LPSTORAGE pStorage
, LPCSTR strOleTypeName
)
10032 HRESULT hStorageRes
, hRes
= S_OK
;
10033 OLECONVERT_ISTORAGE_COMPOBJ IStorageCompObj
;
10034 static const WCHAR wstrStreamName
[] = {1,'C', 'o', 'm', 'p', 'O', 'b', 'j', 0};
10035 WCHAR bufferW
[OLESTREAM_MAX_STR_LEN
];
10037 BYTE pCompObjUnknown1
[] = {0x01, 0x00, 0xFE, 0xFF, 0x03, 0x0A, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF};
10038 BYTE pCompObjUnknown2
[] = {0xF4, 0x39, 0xB2, 0x71};
10040 /* Initialize the CompObj structure */
10041 memset(&IStorageCompObj
, 0, sizeof(IStorageCompObj
));
10042 memcpy(IStorageCompObj
.byUnknown1
, pCompObjUnknown1
, sizeof(pCompObjUnknown1
));
10043 memcpy(IStorageCompObj
.byUnknown2
, pCompObjUnknown2
, sizeof(pCompObjUnknown2
));
10046 /* Create a CompObj stream if it doesn't exist */
10047 hStorageRes
= IStorage_CreateStream(pStorage
, wstrStreamName
,
10048 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &pStream
);
10049 if(hStorageRes
== S_OK
)
10051 /* copy the OleTypeName to the compobj struct */
10052 IStorageCompObj
.dwOleTypeNameLength
= strlen(strOleTypeName
)+1;
10053 strcpy(IStorageCompObj
.strOleTypeName
, strOleTypeName
);
10055 /* copy the OleTypeName to the compobj struct */
10056 /* Note: in the test made, these were Identical */
10057 IStorageCompObj
.dwProgIDNameLength
= strlen(strOleTypeName
)+1;
10058 strcpy(IStorageCompObj
.strProgIDName
, strOleTypeName
);
10060 /* Get the CLSID */
10061 MultiByteToWideChar( CP_ACP
, 0, IStorageCompObj
.strProgIDName
, -1,
10062 bufferW
, OLESTREAM_MAX_STR_LEN
);
10063 hRes
= CLSIDFromProgID(bufferW
, &(IStorageCompObj
.clsid
));
10069 /* Get the CLSID Default Name from the Registry */
10070 hErr
= open_classes_key(HKEY_CLASSES_ROOT
, bufferW
, MAXIMUM_ALLOWED
, &hKey
);
10071 if(hErr
== ERROR_SUCCESS
)
10073 char strTemp
[OLESTREAM_MAX_STR_LEN
];
10074 IStorageCompObj
.dwCLSIDNameLength
= OLESTREAM_MAX_STR_LEN
;
10075 hErr
= RegQueryValueA(hKey
, NULL
, strTemp
, (LONG
*) &(IStorageCompObj
.dwCLSIDNameLength
));
10076 if(hErr
== ERROR_SUCCESS
)
10078 strcpy(IStorageCompObj
.strCLSIDName
, strTemp
);
10084 /* Write CompObj Structure to stream */
10085 hRes
= IStream_Write(pStream
, IStorageCompObj
.byUnknown1
, sizeof(IStorageCompObj
.byUnknown1
), NULL
);
10087 WriteClassStm(pStream
,&(IStorageCompObj
.clsid
));
10089 hRes
= IStream_Write(pStream
, &(IStorageCompObj
.dwCLSIDNameLength
), sizeof(IStorageCompObj
.dwCLSIDNameLength
), NULL
);
10090 if(IStorageCompObj
.dwCLSIDNameLength
> 0)
10092 hRes
= IStream_Write(pStream
, IStorageCompObj
.strCLSIDName
, IStorageCompObj
.dwCLSIDNameLength
, NULL
);
10094 hRes
= IStream_Write(pStream
, &(IStorageCompObj
.dwOleTypeNameLength
) , sizeof(IStorageCompObj
.dwOleTypeNameLength
), NULL
);
10095 if(IStorageCompObj
.dwOleTypeNameLength
> 0)
10097 hRes
= IStream_Write(pStream
, IStorageCompObj
.strOleTypeName
, IStorageCompObj
.dwOleTypeNameLength
, NULL
);
10099 hRes
= IStream_Write(pStream
, &(IStorageCompObj
.dwProgIDNameLength
) , sizeof(IStorageCompObj
.dwProgIDNameLength
), NULL
);
10100 if(IStorageCompObj
.dwProgIDNameLength
> 0)
10102 hRes
= IStream_Write(pStream
, IStorageCompObj
.strProgIDName
, IStorageCompObj
.dwProgIDNameLength
, NULL
);
10104 hRes
= IStream_Write(pStream
, IStorageCompObj
.byUnknown2
, sizeof(IStorageCompObj
.byUnknown2
), NULL
);
10105 IStream_Release(pStream
);
10111 /*************************************************************************
10112 * OLECONVERT_CreateOlePresStream[Internal]
10114 * Creates the "\002OlePres000" Stream with the Metafile data
10117 * pStorage [I] The dest IStorage to create \002OLEPres000 stream in.
10118 * dwExtentX [I] Width of the Metafile
10119 * dwExtentY [I] Height of the Metafile
10120 * pData [I] Metafile data
10121 * dwDataLength [I] Size of the Metafile data
10125 * Failure: CONVERT10_E_OLESTREAM_PUT for invalid Put
10128 * This function is used by OleConvertOLESTREAMToIStorage only.
10131 static void OLECONVERT_CreateOlePresStream(LPSTORAGE pStorage
, DWORD dwExtentX
, DWORD dwExtentY
, BYTE
*pData
, DWORD dwDataLength
)
10135 static const WCHAR wstrStreamName
[] = {2, 'O', 'l', 'e', 'P', 'r', 'e', 's', '0', '0', '0', 0};
10136 BYTE pOlePresStreamHeader
[] =
10138 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0x00, 0x00, 0x00,
10139 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
10140 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
10141 0x00, 0x00, 0x00, 0x00
10144 BYTE pOlePresStreamHeaderEmpty
[] =
10146 0x00, 0x00, 0x00, 0x00,
10147 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
10148 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
10149 0x00, 0x00, 0x00, 0x00
10152 /* Create the OlePres000 Stream */
10153 hRes
= IStorage_CreateStream(pStorage
, wstrStreamName
,
10154 STGM_CREATE
| STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &pStream
);
10159 OLECONVERT_ISTORAGE_OLEPRES OlePres
;
10161 memset(&OlePres
, 0, sizeof(OlePres
));
10162 /* Do we have any metafile data to save */
10163 if(dwDataLength
> 0)
10165 memcpy(OlePres
.byUnknown1
, pOlePresStreamHeader
, sizeof(pOlePresStreamHeader
));
10166 nHeaderSize
= sizeof(pOlePresStreamHeader
);
10170 memcpy(OlePres
.byUnknown1
, pOlePresStreamHeaderEmpty
, sizeof(pOlePresStreamHeaderEmpty
));
10171 nHeaderSize
= sizeof(pOlePresStreamHeaderEmpty
);
10173 /* Set width and height of the metafile */
10174 OlePres
.dwExtentX
= dwExtentX
;
10175 OlePres
.dwExtentY
= -dwExtentY
;
10177 /* Set Data and Length */
10178 if(dwDataLength
> sizeof(METAFILEPICT16
))
10180 OlePres
.dwSize
= dwDataLength
- sizeof(METAFILEPICT16
);
10181 OlePres
.pData
= &(pData
[8]);
10183 /* Save OlePres000 Data to Stream */
10184 hRes
= IStream_Write(pStream
, OlePres
.byUnknown1
, nHeaderSize
, NULL
);
10185 hRes
= IStream_Write(pStream
, &(OlePres
.dwExtentX
), sizeof(OlePres
.dwExtentX
), NULL
);
10186 hRes
= IStream_Write(pStream
, &(OlePres
.dwExtentY
), sizeof(OlePres
.dwExtentY
), NULL
);
10187 hRes
= IStream_Write(pStream
, &(OlePres
.dwSize
), sizeof(OlePres
.dwSize
), NULL
);
10188 if(OlePres
.dwSize
> 0)
10190 hRes
= IStream_Write(pStream
, OlePres
.pData
, OlePres
.dwSize
, NULL
);
10192 IStream_Release(pStream
);
10196 /*************************************************************************
10197 * OLECONVERT_CreateOle10NativeStream [Internal]
10199 * Creates the "\001Ole10Native" Stream (should contain a BMP)
10202 * pStorage [I] Dest storage to create the stream in
10203 * pData [I] Ole10 Native Data (ex. bmp)
10204 * dwDataLength [I] Size of the Ole10 Native Data
10210 * This function is used by OleConvertOLESTREAMToIStorage only.
10212 * Might need to verify the data and return appropriate error message
10215 static void OLECONVERT_CreateOle10NativeStream(LPSTORAGE pStorage
, const BYTE
*pData
, DWORD dwDataLength
)
10219 static const WCHAR wstrStreamName
[] = {1, 'O', 'l', 'e', '1', '0', 'N', 'a', 't', 'i', 'v', 'e', 0};
10221 /* Create the Ole10Native Stream */
10222 hRes
= IStorage_CreateStream(pStorage
, wstrStreamName
,
10223 STGM_CREATE
| STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &pStream
);
10227 /* Write info to stream */
10228 hRes
= IStream_Write(pStream
, &dwDataLength
, sizeof(dwDataLength
), NULL
);
10229 hRes
= IStream_Write(pStream
, pData
, dwDataLength
, NULL
);
10230 IStream_Release(pStream
);
10235 /*************************************************************************
10236 * OLECONVERT_GetOLE10ProgID [Internal]
10238 * Finds the ProgID (or OleTypeID) from the IStorage
10241 * pStorage [I] The Src IStorage to get the ProgID
10242 * strProgID [I] the ProgID string to get
10243 * dwSize [I] the size of the string
10247 * Failure: REGDB_E_CLASSNOTREG if cannot reconstruct the stream
10250 * This function is used by OleConvertIStorageToOLESTREAM only.
10254 static HRESULT
OLECONVERT_GetOLE10ProgID(LPSTORAGE pStorage
, char *strProgID
, DWORD
*dwSize
)
10258 LARGE_INTEGER iSeekPos
;
10259 OLECONVERT_ISTORAGE_COMPOBJ CompObj
;
10260 static const WCHAR wstrStreamName
[] = {1,'C', 'o', 'm', 'p', 'O', 'b', 'j', 0};
10262 /* Open the CompObj Stream */
10263 hRes
= IStorage_OpenStream(pStorage
, wstrStreamName
, NULL
,
10264 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &pStream
);
10268 /*Get the OleType from the CompObj Stream */
10269 iSeekPos
.u
.LowPart
= sizeof(CompObj
.byUnknown1
) + sizeof(CompObj
.clsid
);
10270 iSeekPos
.u
.HighPart
= 0;
10272 IStream_Seek(pStream
, iSeekPos
, STREAM_SEEK_SET
, NULL
);
10273 IStream_Read(pStream
, &CompObj
.dwCLSIDNameLength
, sizeof(CompObj
.dwCLSIDNameLength
), NULL
);
10274 iSeekPos
.u
.LowPart
= CompObj
.dwCLSIDNameLength
;
10275 IStream_Seek(pStream
, iSeekPos
, STREAM_SEEK_CUR
, NULL
);
10276 IStream_Read(pStream
, &CompObj
.dwOleTypeNameLength
, sizeof(CompObj
.dwOleTypeNameLength
), NULL
);
10277 iSeekPos
.u
.LowPart
= CompObj
.dwOleTypeNameLength
;
10278 IStream_Seek(pStream
, iSeekPos
, STREAM_SEEK_CUR
, NULL
);
10280 IStream_Read(pStream
, dwSize
, sizeof(*dwSize
), NULL
);
10283 IStream_Read(pStream
, strProgID
, *dwSize
, NULL
);
10285 IStream_Release(pStream
);
10290 LPOLESTR wstrProgID
;
10292 /* Get the OleType from the registry */
10293 REFCLSID clsid
= &(stat
.clsid
);
10294 IStorage_Stat(pStorage
, &stat
, STATFLAG_NONAME
);
10295 hRes
= ProgIDFromCLSID(clsid
, &wstrProgID
);
10298 *dwSize
= WideCharToMultiByte(CP_ACP
, 0, wstrProgID
, -1, strProgID
, *dwSize
, NULL
, FALSE
);
10299 CoTaskMemFree(wstrProgID
);
10306 /*************************************************************************
10307 * OLECONVERT_GetOle10PresData [Internal]
10309 * Converts IStorage "/001Ole10Native" stream to a OLE10 Stream
10312 * pStorage [I] Src IStroage
10313 * pOleStream [I] Dest OleStream Mem Struct
10319 * This function is used by OleConvertIStorageToOLESTREAM only.
10321 * Memory allocated for pData must be freed by the caller
10325 static void OLECONVERT_GetOle10PresData(LPSTORAGE pStorage
, OLECONVERT_OLESTREAM_DATA
*pOleStreamData
)
10330 static const WCHAR wstrStreamName
[] = {1, 'O', 'l', 'e', '1', '0', 'N', 'a', 't', 'i', 'v', 'e', 0};
10332 /* Initialize Default data for OLESTREAM */
10333 pOleStreamData
[0].dwOleID
= OLESTREAM_ID
;
10334 pOleStreamData
[0].dwTypeID
= 2;
10335 pOleStreamData
[1].dwOleID
= OLESTREAM_ID
;
10336 pOleStreamData
[1].dwTypeID
= 0;
10337 pOleStreamData
[0].dwMetaFileWidth
= 0;
10338 pOleStreamData
[0].dwMetaFileHeight
= 0;
10339 pOleStreamData
[0].pData
= NULL
;
10340 pOleStreamData
[1].pData
= NULL
;
10342 /* Open Ole10Native Stream */
10343 hRes
= IStorage_OpenStream(pStorage
, wstrStreamName
, NULL
,
10344 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &pStream
);
10348 /* Read Size and Data */
10349 IStream_Read(pStream
, &(pOleStreamData
->dwDataLength
), sizeof(pOleStreamData
->dwDataLength
), NULL
);
10350 if(pOleStreamData
->dwDataLength
> 0)
10352 pOleStreamData
->pData
= HeapAlloc(GetProcessHeap(),0,pOleStreamData
->dwDataLength
);
10353 IStream_Read(pStream
, pOleStreamData
->pData
, pOleStreamData
->dwDataLength
, NULL
);
10355 IStream_Release(pStream
);
10361 /*************************************************************************
10362 * OLECONVERT_GetOle20PresData[Internal]
10364 * Converts IStorage "/002OlePres000" stream to a OLE10 Stream
10367 * pStorage [I] Src IStroage
10368 * pOleStreamData [I] Dest OleStream Mem Struct
10374 * This function is used by OleConvertIStorageToOLESTREAM only.
10376 * Memory allocated for pData must be freed by the caller
10378 static void OLECONVERT_GetOle20PresData(LPSTORAGE pStorage
, OLECONVERT_OLESTREAM_DATA
*pOleStreamData
)
10382 OLECONVERT_ISTORAGE_OLEPRES olePress
;
10383 static const WCHAR wstrStreamName
[] = {2, 'O', 'l', 'e', 'P', 'r', 'e', 's', '0', '0', '0', 0};
10385 /* Initialize Default data for OLESTREAM */
10386 pOleStreamData
[0].dwOleID
= OLESTREAM_ID
;
10387 pOleStreamData
[0].dwTypeID
= 2;
10388 pOleStreamData
[0].dwMetaFileWidth
= 0;
10389 pOleStreamData
[0].dwMetaFileHeight
= 0;
10390 pOleStreamData
[0].dwDataLength
= OLECONVERT_WriteOLE20ToBuffer(pStorage
, &(pOleStreamData
[0].pData
));
10391 pOleStreamData
[1].dwOleID
= OLESTREAM_ID
;
10392 pOleStreamData
[1].dwTypeID
= 0;
10393 pOleStreamData
[1].dwOleTypeNameLength
= 0;
10394 pOleStreamData
[1].strOleTypeName
[0] = 0;
10395 pOleStreamData
[1].dwMetaFileWidth
= 0;
10396 pOleStreamData
[1].dwMetaFileHeight
= 0;
10397 pOleStreamData
[1].pData
= NULL
;
10398 pOleStreamData
[1].dwDataLength
= 0;
10401 /* Open OlePress000 stream */
10402 hRes
= IStorage_OpenStream(pStorage
, wstrStreamName
, NULL
,
10403 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &pStream
);
10406 LARGE_INTEGER iSeekPos
;
10407 METAFILEPICT16 MetaFilePict
;
10408 static const char strMetafilePictName
[] = "METAFILEPICT";
10410 /* Set the TypeID for a Metafile */
10411 pOleStreamData
[1].dwTypeID
= 5;
10413 /* Set the OleTypeName to Metafile */
10414 pOleStreamData
[1].dwOleTypeNameLength
= strlen(strMetafilePictName
) +1;
10415 strcpy(pOleStreamData
[1].strOleTypeName
, strMetafilePictName
);
10417 iSeekPos
.u
.HighPart
= 0;
10418 iSeekPos
.u
.LowPart
= sizeof(olePress
.byUnknown1
);
10420 /* Get Presentation Data */
10421 IStream_Seek(pStream
, iSeekPos
, STREAM_SEEK_SET
, NULL
);
10422 IStream_Read(pStream
, &(olePress
.dwExtentX
), sizeof(olePress
.dwExtentX
), NULL
);
10423 IStream_Read(pStream
, &(olePress
.dwExtentY
), sizeof(olePress
.dwExtentY
), NULL
);
10424 IStream_Read(pStream
, &(olePress
.dwSize
), sizeof(olePress
.dwSize
), NULL
);
10426 /*Set width and Height */
10427 pOleStreamData
[1].dwMetaFileWidth
= olePress
.dwExtentX
;
10428 pOleStreamData
[1].dwMetaFileHeight
= -olePress
.dwExtentY
;
10429 if(olePress
.dwSize
> 0)
10432 pOleStreamData
[1].dwDataLength
= olePress
.dwSize
+ sizeof(METAFILEPICT16
);
10434 /* Set MetaFilePict struct */
10435 MetaFilePict
.mm
= 8;
10436 MetaFilePict
.xExt
= olePress
.dwExtentX
;
10437 MetaFilePict
.yExt
= olePress
.dwExtentY
;
10438 MetaFilePict
.hMF
= 0;
10440 /* Get Metafile Data */
10441 pOleStreamData
[1].pData
= HeapAlloc(GetProcessHeap(),0,pOleStreamData
[1].dwDataLength
);
10442 memcpy(pOleStreamData
[1].pData
, &MetaFilePict
, sizeof(MetaFilePict
));
10443 IStream_Read(pStream
, &(pOleStreamData
[1].pData
[sizeof(MetaFilePict
)]), pOleStreamData
[1].dwDataLength
-sizeof(METAFILEPICT16
), NULL
);
10445 IStream_Release(pStream
);
10449 /*************************************************************************
10450 * OleConvertOLESTREAMToIStorage [OLE32.@]
10452 * Read info on MSDN
10455 * DVTARGETDEVICE parameter is not handled
10456 * Still unsure of some mem fields for OLE 10 Stream
10457 * Still some unknowns for the IStorage: "\002OlePres000", "\001CompObj",
10458 * and "\001OLE" streams
10461 HRESULT WINAPI
OleConvertOLESTREAMToIStorage (
10462 LPOLESTREAM pOleStream
,
10464 const DVTARGETDEVICE
* ptd
)
10468 OLECONVERT_OLESTREAM_DATA pOleStreamData
[2];
10470 TRACE("%p %p %p\n", pOleStream
, pstg
, ptd
);
10472 memset(pOleStreamData
, 0, sizeof(pOleStreamData
));
10476 FIXME("DVTARGETDEVICE is not NULL, unhandled parameter\n");
10479 if(pstg
== NULL
|| pOleStream
== NULL
)
10481 hRes
= E_INVALIDARG
;
10486 /* Load the OLESTREAM to Memory */
10487 hRes
= OLECONVERT_LoadOLE10(pOleStream
, &pOleStreamData
[0], TRUE
);
10492 /* Load the OLESTREAM to Memory (part 2)*/
10493 hRes
= OLECONVERT_LoadOLE10(pOleStream
, &pOleStreamData
[1], FALSE
);
10499 if(pOleStreamData
[0].dwDataLength
> sizeof(STORAGE_magic
))
10501 /* Do we have the IStorage Data in the OLESTREAM */
10502 if(memcmp(pOleStreamData
[0].pData
, STORAGE_magic
, sizeof(STORAGE_magic
)) ==0)
10504 OLECONVERT_GetOLE20FromOLE10(pstg
, pOleStreamData
[0].pData
, pOleStreamData
[0].dwDataLength
);
10505 OLECONVERT_CreateOlePresStream(pstg
, pOleStreamData
[1].dwMetaFileWidth
, pOleStreamData
[1].dwMetaFileHeight
, pOleStreamData
[1].pData
, pOleStreamData
[1].dwDataLength
);
10509 /* It must be an original OLE 1.0 source */
10510 OLECONVERT_CreateOle10NativeStream(pstg
, pOleStreamData
[0].pData
, pOleStreamData
[0].dwDataLength
);
10515 /* It must be an original OLE 1.0 source */
10516 OLECONVERT_CreateOle10NativeStream(pstg
, pOleStreamData
[0].pData
, pOleStreamData
[0].dwDataLength
);
10519 /* Create CompObj Stream if necessary */
10520 hRes
= OLECONVERT_CreateCompObjStream(pstg
, pOleStreamData
[0].strOleTypeName
);
10523 /*Create the Ole Stream if necessary */
10524 STORAGE_CreateOleStream(pstg
, 0);
10529 /* Free allocated memory */
10530 for(i
=0; i
< 2; i
++)
10532 HeapFree(GetProcessHeap(),0,pOleStreamData
[i
].pData
);
10533 HeapFree(GetProcessHeap(),0,pOleStreamData
[i
].pstrOleObjFileName
);
10534 pOleStreamData
[i
].pstrOleObjFileName
= NULL
;
10539 /*************************************************************************
10540 * OleConvertIStorageToOLESTREAM [OLE32.@]
10542 * Read info on MSDN
10544 * Read info on MSDN
10547 * Still unsure of some mem fields for OLE 10 Stream
10548 * Still some unknowns for the IStorage: "\002OlePres000", "\001CompObj",
10549 * and "\001OLE" streams.
10552 HRESULT WINAPI
OleConvertIStorageToOLESTREAM (
10554 LPOLESTREAM pOleStream
)
10557 HRESULT hRes
= S_OK
;
10559 OLECONVERT_OLESTREAM_DATA pOleStreamData
[2];
10560 static const WCHAR wstrStreamName
[] = {1, 'O', 'l', 'e', '1', '0', 'N', 'a', 't', 'i', 'v', 'e', 0};
10562 TRACE("%p %p\n", pstg
, pOleStream
);
10564 memset(pOleStreamData
, 0, sizeof(pOleStreamData
));
10566 if(pstg
== NULL
|| pOleStream
== NULL
)
10568 hRes
= E_INVALIDARG
;
10572 /* Get the ProgID */
10573 pOleStreamData
[0].dwOleTypeNameLength
= OLESTREAM_MAX_STR_LEN
;
10574 hRes
= OLECONVERT_GetOLE10ProgID(pstg
, pOleStreamData
[0].strOleTypeName
, &(pOleStreamData
[0].dwOleTypeNameLength
));
10578 /* Was it originally Ole10 */
10579 hRes
= IStorage_OpenStream(pstg
, wstrStreamName
, 0, STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &pStream
);
10582 IStream_Release(pStream
);
10583 /* Get Presentation Data for Ole10Native */
10584 OLECONVERT_GetOle10PresData(pstg
, pOleStreamData
);
10588 /* Get Presentation Data (OLE20) */
10589 OLECONVERT_GetOle20PresData(pstg
, pOleStreamData
);
10592 /* Save OLESTREAM */
10593 hRes
= OLECONVERT_SaveOLE10(&(pOleStreamData
[0]), pOleStream
);
10596 hRes
= OLECONVERT_SaveOLE10(&(pOleStreamData
[1]), pOleStream
);
10601 /* Free allocated memory */
10602 for(i
=0; i
< 2; i
++)
10604 HeapFree(GetProcessHeap(),0,pOleStreamData
[i
].pData
);
10610 enum stream_1ole_flags
{
10611 OleStream_LinkedObject
= 0x00000001,
10612 OleStream_Convert
= 0x00000004
10615 /***********************************************************************
10616 * GetConvertStg (OLE32.@)
10618 HRESULT WINAPI
GetConvertStg(IStorage
*stg
)
10620 static const WCHAR stream_1oleW
[] = {1,'O','l','e',0};
10621 static const DWORD version_magic
= 0x02000001;
10626 TRACE("%p\n", stg
);
10628 if (!stg
) return E_INVALIDARG
;
10630 hr
= IStorage_OpenStream(stg
, stream_1oleW
, NULL
, STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stream
);
10631 if (FAILED(hr
)) return hr
;
10633 hr
= IStream_Read(stream
, header
, sizeof(header
), NULL
);
10634 IStream_Release(stream
);
10635 if (FAILED(hr
)) return hr
;
10637 if (header
[0] != version_magic
)
10639 ERR("got wrong version magic for 1Ole stream, 0x%08x\n", header
[0]);
10643 return header
[1] & OleStream_Convert
? S_OK
: S_FALSE
;
10646 /***********************************************************************
10647 * SetConvertStg (OLE32.@)
10649 HRESULT WINAPI
SetConvertStg(IStorage
*storage
, BOOL convert
)
10651 static const WCHAR stream_1oleW
[] = {1,'O','l','e',0};
10652 DWORD flags
= convert
? OleStream_Convert
: 0;
10657 TRACE("(%p, %d)\n", storage
, convert
);
10659 hr
= IStorage_OpenStream(storage
, stream_1oleW
, NULL
, STGM_READWRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stream
);
10662 if (hr
!= STG_E_FILENOTFOUND
)
10665 return STORAGE_CreateOleStream(storage
, flags
);
10668 hr
= IStream_Read(stream
, header
, sizeof(header
), NULL
);
10671 IStream_Release(stream
);
10675 /* update flag if differs */
10676 if ((header
[1] ^ flags
) & OleStream_Convert
)
10678 LARGE_INTEGER pos
= {{0}};
10680 if (header
[1] & OleStream_Convert
)
10681 flags
= header
[1] & ~OleStream_Convert
;
10683 flags
= header
[1] | OleStream_Convert
;
10685 pos
.QuadPart
= sizeof(DWORD
);
10686 hr
= IStream_Seek(stream
, pos
, STREAM_SEEK_SET
, NULL
);
10689 IStream_Release(stream
);
10693 hr
= IStream_Write(stream
, &flags
, sizeof(flags
), NULL
);
10696 IStream_Release(stream
);