2 * Compound Storage (32 bit version)
3 * Stream implementation
5 * This file contains the implementation of the stream interface
6 * for streams contained in a compound storage.
8 * Copyright 1999 Francis Beaudet
9 * Copyright 1999 Thuy Nguyen
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
40 #include "wine/debug.h"
42 #include "storage32.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(storage
);
48 * This is the destructor of the StgStreamImpl class.
50 * This method will clean-up all the resources used-up by the given StgStreamImpl
51 * class. The pointer passed-in to this function will be freed and will not
54 static void StgStreamImpl_Destroy(StgStreamImpl
* This
)
56 TRACE("(%p)\n", This
);
59 * Release the reference we are holding on the parent storage.
60 * IStorage_Release((IStorage*)This->parentStorage);
62 * No, don't do this. Some apps call IStorage_Release without
63 * calling IStream_Release first. If we grab a reference the
64 * file is not closed, and the app fails when it tries to
65 * reopen the file (Easy-PC, for example). Just inform the
66 * storage that we have closed the stream
69 if(This
->parentStorage
) {
71 StorageBaseImpl_RemoveStream(This
->parentStorage
, This
);
75 This
->parentStorage
= 0;
78 * Make sure we clean-up the block chain stream objects that we were using.
80 if (This
->bigBlockChain
!= 0)
82 BlockChainStream_Destroy(This
->bigBlockChain
);
83 This
->bigBlockChain
= 0;
86 if (This
->smallBlockChain
!= 0)
88 SmallBlockChainStream_Destroy(This
->smallBlockChain
);
89 This
->smallBlockChain
= 0;
93 * Finally, free the memory used-up by the class.
95 HeapFree(GetProcessHeap(), 0, This
);
99 * This implements the IUnknown method QueryInterface for this
102 static HRESULT WINAPI
StgStreamImpl_QueryInterface(
104 REFIID riid
, /* [in] */
105 void** ppvObject
) /* [iid_is][out] */
107 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
110 * Perform a sanity check on the parameters.
116 * Initialize the return parameter.
121 * Compare the riid with the interface IDs implemented by this object.
123 if (IsEqualIID(&IID_IUnknown
, riid
) ||
124 IsEqualIID(&IID_IPersist
, riid
) ||
125 IsEqualIID(&IID_IPersistStream
, riid
) ||
126 IsEqualIID(&IID_ISequentialStream
, riid
) ||
127 IsEqualIID(&IID_IStream
, riid
))
133 * Check that we obtained an interface.
136 return E_NOINTERFACE
;
139 * Query Interface always increases the reference count by one when it is
142 IStream_AddRef(iface
);
148 * This implements the IUnknown method AddRef for this
151 static ULONG WINAPI
StgStreamImpl_AddRef(
154 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
155 return InterlockedIncrement(&This
->ref
);
159 * This implements the IUnknown method Release for this
162 static ULONG WINAPI
StgStreamImpl_Release(
165 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
169 ref
= InterlockedDecrement(&This
->ref
);
172 * If the reference count goes down to 0, perform suicide.
176 StgStreamImpl_Destroy(This
);
183 * This method will open the block chain pointed by the directory entry
184 * that describes the stream.
185 * If the stream's size is null, no chain is opened.
187 static void StgStreamImpl_OpenBlockChain(
190 DirEntry currentEntry
;
194 * Make sure no old object is left over.
196 if (This
->smallBlockChain
!= 0)
198 SmallBlockChainStream_Destroy(This
->smallBlockChain
);
199 This
->smallBlockChain
= 0;
202 if (This
->bigBlockChain
!= 0)
204 BlockChainStream_Destroy(This
->bigBlockChain
);
205 This
->bigBlockChain
= 0;
209 * Read the information from the directory entry.
211 readSuccessful
= StorageImpl_ReadDirEntry(This
->parentStorage
->ancestorStorage
,
217 This
->streamSize
= currentEntry
.size
;
220 * This code supports only streams that are <32 bits in size.
222 assert(This
->streamSize
.u
.HighPart
== 0);
224 if(currentEntry
.startingBlock
== BLOCK_END_OF_CHAIN
)
226 assert( (This
->streamSize
.u
.HighPart
== 0) && (This
->streamSize
.u
.LowPart
== 0) );
230 if ( (This
->streamSize
.u
.HighPart
== 0) &&
231 (This
->streamSize
.u
.LowPart
< LIMIT_TO_USE_SMALL_BLOCK
) )
233 This
->smallBlockChain
= SmallBlockChainStream_Construct(
234 This
->parentStorage
->ancestorStorage
,
240 This
->bigBlockChain
= BlockChainStream_Construct(
241 This
->parentStorage
->ancestorStorage
,
250 * This method is part of the ISequentialStream interface.
252 * It reads a block of information from the stream at the current
253 * position. It then moves the current position at the end of the
256 * See the documentation of ISequentialStream for more info.
258 static HRESULT WINAPI
StgStreamImpl_Read(
260 void* pv
, /* [length_is][size_is][out] */
262 ULONG
* pcbRead
) /* [out] */
264 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
266 ULONG bytesReadBuffer
;
267 ULONG bytesToReadFromBuffer
;
270 TRACE("(%p, %p, %d, %p)\n",
271 iface
, pv
, cb
, pcbRead
);
273 if (!This
->parentStorage
)
275 WARN("storage reverted\n");
276 return STG_E_REVERTED
;
280 * If the caller is not interested in the number of bytes read,
281 * we use another buffer to avoid "if" statements in the code.
284 pcbRead
= &bytesReadBuffer
;
287 * Using the known size of the stream, calculate the number of bytes
288 * to read from the block chain
290 bytesToReadFromBuffer
= min( This
->streamSize
.u
.LowPart
- This
->currentPosition
.u
.LowPart
, cb
);
293 * Depending on the type of chain that was opened when the stream was constructed,
294 * we delegate the work to the method that reads the block chains.
296 if (This
->smallBlockChain
!=0)
298 res
= SmallBlockChainStream_ReadAt(This
->smallBlockChain
,
299 This
->currentPosition
,
300 bytesToReadFromBuffer
,
305 else if (This
->bigBlockChain
!=0)
307 res
= BlockChainStream_ReadAt(This
->bigBlockChain
,
308 This
->currentPosition
,
309 bytesToReadFromBuffer
,
316 * Small and big block chains are both NULL. This case will happen
317 * when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
328 * We should always be able to read the proper amount of data from the
331 assert(bytesToReadFromBuffer
== *pcbRead
);
334 * Advance the pointer for the number of positions read.
336 This
->currentPosition
.u
.LowPart
+= *pcbRead
;
340 TRACE("<-- %08x\n", res
);
345 * This method is part of the ISequentialStream interface.
347 * It writes a block of information to the stream at the current
348 * position. It then moves the current position at the end of the
349 * written block. If the stream is too small to fit the block,
350 * the stream is grown to fit.
352 * See the documentation of ISequentialStream for more info.
354 static HRESULT WINAPI
StgStreamImpl_Write(
356 const void* pv
, /* [size_is][in] */
358 ULONG
* pcbWritten
) /* [out] */
360 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
362 ULARGE_INTEGER newSize
;
363 ULONG bytesWritten
= 0;
366 TRACE("(%p, %p, %d, %p)\n",
367 iface
, pv
, cb
, pcbWritten
);
370 * Do we have permission to write to this stream?
372 switch(STGM_ACCESS_MODE(This
->grfMode
))
378 WARN("access denied by flags: 0x%x\n", STGM_ACCESS_MODE(This
->grfMode
));
379 return STG_E_ACCESSDENIED
;
383 return STG_E_INVALIDPOINTER
;
385 if (!This
->parentStorage
)
387 WARN("storage reverted\n");
388 return STG_E_REVERTED
;
392 * If the caller is not interested in the number of bytes written,
393 * we use another buffer to avoid "if" statements in the code.
396 pcbWritten
= &bytesWritten
;
399 * Initialize the out parameter
405 TRACE("<-- S_OK, written 0\n");
410 newSize
.u
.HighPart
= 0;
411 newSize
.u
.LowPart
= This
->currentPosition
.u
.LowPart
+ cb
;
415 * Verify if we need to grow the stream
417 if (newSize
.u
.LowPart
> This
->streamSize
.u
.LowPart
)
420 res
= IStream_SetSize(iface
, newSize
);
426 * Depending on the type of chain that was opened when the stream was constructed,
427 * we delegate the work to the method that readwrites to the block chains.
429 if (This
->smallBlockChain
!=0)
431 res
= SmallBlockChainStream_WriteAt(This
->smallBlockChain
,
432 This
->currentPosition
,
438 else if (This
->bigBlockChain
!=0)
440 res
= BlockChainStream_WriteAt(This
->bigBlockChain
,
441 This
->currentPosition
,
448 /* this should never happen because the IStream_SetSize call above will
449 * make sure a big or small block chain is created */
455 * Advance the position pointer for the number of positions written.
457 This
->currentPosition
.u
.LowPart
+= *pcbWritten
;
459 TRACE("<-- S_OK, written %u\n", *pcbWritten
);
464 * This method is part of the IStream interface.
466 * It will move the current stream pointer according to the parameters
469 * See the documentation of IStream for more info.
471 static HRESULT WINAPI
StgStreamImpl_Seek(
473 LARGE_INTEGER dlibMove
, /* [in] */
474 DWORD dwOrigin
, /* [in] */
475 ULARGE_INTEGER
* plibNewPosition
) /* [out] */
477 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
479 ULARGE_INTEGER newPosition
;
481 TRACE("(%p, %d, %d, %p)\n",
482 iface
, dlibMove
.u
.LowPart
, dwOrigin
, plibNewPosition
);
485 * fail if the stream has no parent (as does windows)
488 if (!This
->parentStorage
)
490 WARN("storage reverted\n");
491 return STG_E_REVERTED
;
495 * The caller is allowed to pass in NULL as the new position return value.
496 * If it happens, we assign it to a dynamic variable to avoid special cases
499 if (plibNewPosition
== 0)
501 plibNewPosition
= &newPosition
;
505 * The file pointer is moved depending on the given "function"
510 case STREAM_SEEK_SET
:
511 plibNewPosition
->u
.HighPart
= 0;
512 plibNewPosition
->u
.LowPart
= 0;
514 case STREAM_SEEK_CUR
:
515 *plibNewPosition
= This
->currentPosition
;
517 case STREAM_SEEK_END
:
518 *plibNewPosition
= This
->streamSize
;
521 WARN("invalid dwOrigin %d\n", dwOrigin
);
522 return STG_E_INVALIDFUNCTION
;
525 plibNewPosition
->QuadPart
+= dlibMove
.QuadPart
;
528 * tell the caller what we calculated
530 This
->currentPosition
= *plibNewPosition
;
536 * This method is part of the IStream interface.
538 * It will change the size of a stream.
540 * See the documentation of IStream for more info.
542 static HRESULT WINAPI
StgStreamImpl_SetSize(
544 ULARGE_INTEGER libNewSize
) /* [in] */
546 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
548 DirEntry currentEntry
;
551 TRACE("(%p, %d)\n", iface
, libNewSize
.u
.LowPart
);
553 if(!This
->parentStorage
)
555 WARN("storage reverted\n");
556 return STG_E_REVERTED
;
562 if (libNewSize
.u
.HighPart
!= 0)
564 WARN("invalid value for libNewSize.u.HighPart %d\n", libNewSize
.u
.HighPart
);
565 return STG_E_INVALIDFUNCTION
;
569 * Do we have permission?
571 if (!(This
->grfMode
& (STGM_WRITE
| STGM_READWRITE
)))
573 WARN("access denied\n");
574 return STG_E_ACCESSDENIED
;
577 /* In simple mode keep the stream size above the small block limit */
578 if (This
->parentStorage
->openFlags
& STGM_SIMPLE
)
579 libNewSize
.u
.LowPart
= max(libNewSize
.u
.LowPart
, LIMIT_TO_USE_SMALL_BLOCK
);
581 if (This
->streamSize
.u
.LowPart
== libNewSize
.u
.LowPart
)
585 * This will happen if we're creating a stream
587 if ((This
->smallBlockChain
== 0) && (This
->bigBlockChain
== 0))
589 if (libNewSize
.u
.LowPart
< LIMIT_TO_USE_SMALL_BLOCK
)
591 This
->smallBlockChain
= SmallBlockChainStream_Construct(
592 This
->parentStorage
->ancestorStorage
,
598 This
->bigBlockChain
= BlockChainStream_Construct(
599 This
->parentStorage
->ancestorStorage
,
606 * Read this stream's size to see if it's small blocks or big blocks
608 Success
= StorageImpl_ReadDirEntry(This
->parentStorage
->ancestorStorage
,
612 * Determine if we have to switch from small to big blocks or vice versa
614 if ( (This
->smallBlockChain
!=0) &&
615 (currentEntry
.size
.u
.LowPart
< LIMIT_TO_USE_SMALL_BLOCK
) )
617 if (libNewSize
.u
.LowPart
>= LIMIT_TO_USE_SMALL_BLOCK
)
620 * Transform the small block chain into a big block chain
622 This
->bigBlockChain
= Storage32Impl_SmallBlocksToBigBlocks(
623 This
->parentStorage
->ancestorStorage
,
624 &This
->smallBlockChain
);
627 else if ( (This
->bigBlockChain
!=0) &&
628 (currentEntry
.size
.u
.LowPart
>= LIMIT_TO_USE_SMALL_BLOCK
) )
630 if (libNewSize
.u
.LowPart
< LIMIT_TO_USE_SMALL_BLOCK
)
633 * Transform the big block chain into a small block chain
635 This
->smallBlockChain
= Storage32Impl_BigBlocksToSmallBlocks(
636 This
->parentStorage
->ancestorStorage
,
637 &This
->bigBlockChain
);
641 if (This
->smallBlockChain
!=0)
643 Success
= SmallBlockChainStream_SetSize(This
->smallBlockChain
, libNewSize
);
647 Success
= BlockChainStream_SetSize(This
->bigBlockChain
, libNewSize
);
651 * Write the new information about this stream to the directory entry
653 Success
= StorageImpl_ReadDirEntry(This
->parentStorage
->ancestorStorage
,
657 currentEntry
.size
.u
.HighPart
= libNewSize
.u
.HighPart
;
658 currentEntry
.size
.u
.LowPart
= libNewSize
.u
.LowPart
;
662 StorageImpl_WriteDirEntry(This
->parentStorage
->ancestorStorage
,
667 This
->streamSize
= libNewSize
;
673 * This method is part of the IStream interface.
675 * It will copy the 'cb' Bytes to 'pstm' IStream.
677 * See the documentation of IStream for more info.
679 static HRESULT WINAPI
StgStreamImpl_CopyTo(
681 IStream
* pstm
, /* [unique][in] */
682 ULARGE_INTEGER cb
, /* [in] */
683 ULARGE_INTEGER
* pcbRead
, /* [out] */
684 ULARGE_INTEGER
* pcbWritten
) /* [out] */
686 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
689 ULONG bytesRead
, bytesWritten
, copySize
;
690 ULARGE_INTEGER totalBytesRead
;
691 ULARGE_INTEGER totalBytesWritten
;
693 TRACE("(%p, %p, %d, %p, %p)\n",
694 iface
, pstm
, cb
.u
.LowPart
, pcbRead
, pcbWritten
);
700 if (!This
->parentStorage
)
702 WARN("storage reverted\n");
703 return STG_E_REVERTED
;
707 return STG_E_INVALIDPOINTER
;
709 totalBytesRead
.QuadPart
= 0;
710 totalBytesWritten
.QuadPart
= 0;
712 while ( cb
.QuadPart
> 0 )
714 if ( cb
.QuadPart
>= sizeof(tmpBuffer
) )
715 copySize
= sizeof(tmpBuffer
);
717 copySize
= cb
.u
.LowPart
;
719 IStream_Read(iface
, tmpBuffer
, copySize
, &bytesRead
);
721 totalBytesRead
.QuadPart
+= bytesRead
;
723 IStream_Write(pstm
, tmpBuffer
, bytesRead
, &bytesWritten
);
725 totalBytesWritten
.QuadPart
+= bytesWritten
;
728 * Check that read & write operations were successful
730 if (bytesRead
!= bytesWritten
)
732 hr
= STG_E_MEDIUMFULL
;
733 WARN("medium full\n");
737 if (bytesRead
!=copySize
)
740 cb
.QuadPart
-= bytesRead
;
743 if (pcbRead
) pcbRead
->QuadPart
= totalBytesRead
.QuadPart
;
744 if (pcbWritten
) pcbWritten
->QuadPart
= totalBytesWritten
.QuadPart
;
750 * This method is part of the IStream interface.
752 * For streams contained in structured storages, this method
753 * does nothing. This is what the documentation tells us.
755 * See the documentation of IStream for more info.
757 static HRESULT WINAPI
StgStreamImpl_Commit(
759 DWORD grfCommitFlags
) /* [in] */
761 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
763 if (!This
->parentStorage
)
765 WARN("storage reverted\n");
766 return STG_E_REVERTED
;
773 * This method is part of the IStream interface.
775 * For streams contained in structured storages, this method
776 * does nothing. This is what the documentation tells us.
778 * See the documentation of IStream for more info.
780 static HRESULT WINAPI
StgStreamImpl_Revert(
786 static HRESULT WINAPI
StgStreamImpl_LockRegion(
788 ULARGE_INTEGER libOffset
, /* [in] */
789 ULARGE_INTEGER cb
, /* [in] */
790 DWORD dwLockType
) /* [in] */
792 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
794 if (!This
->parentStorage
)
796 WARN("storage reverted\n");
797 return STG_E_REVERTED
;
800 FIXME("not implemented!\n");
804 static HRESULT WINAPI
StgStreamImpl_UnlockRegion(
806 ULARGE_INTEGER libOffset
, /* [in] */
807 ULARGE_INTEGER cb
, /* [in] */
808 DWORD dwLockType
) /* [in] */
810 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
812 if (!This
->parentStorage
)
814 WARN("storage reverted\n");
815 return STG_E_REVERTED
;
818 FIXME("not implemented!\n");
823 * This method is part of the IStream interface.
825 * This method returns information about the current
828 * See the documentation of IStream for more info.
830 static HRESULT WINAPI
StgStreamImpl_Stat(
832 STATSTG
* pstatstg
, /* [out] */
833 DWORD grfStatFlag
) /* [in] */
835 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
837 DirEntry currentEntry
;
840 TRACE("%p %p %d\n", This
, pstatstg
, grfStatFlag
);
843 * if stream has no parent, return STG_E_REVERTED
846 if (!This
->parentStorage
)
848 WARN("storage reverted\n");
849 return STG_E_REVERTED
;
853 * Read the information from the directory entry.
855 readSuccessful
= StorageImpl_ReadDirEntry(This
->parentStorage
->ancestorStorage
,
861 StorageUtl_CopyDirEntryToSTATSTG(This
->parentStorage
,
866 pstatstg
->grfMode
= This
->grfMode
;
868 /* In simple create mode cbSize is the current pos */
869 if((This
->parentStorage
->openFlags
& STGM_SIMPLE
) && This
->parentStorage
->create
)
870 pstatstg
->cbSize
= This
->currentPosition
;
875 WARN("failed to read entry\n");
880 * This method is part of the IStream interface.
882 * This method returns a clone of the interface that allows for
883 * another seek pointer
885 * See the documentation of IStream for more info.
887 * I am not totally sure what I am doing here but I presume that this
888 * should be basically as simple as creating a new stream with the same
889 * parent etc and positioning its seek cursor.
891 static HRESULT WINAPI
StgStreamImpl_Clone(
893 IStream
** ppstm
) /* [out] */
895 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
897 StgStreamImpl
* new_stream
;
898 LARGE_INTEGER seek_pos
;
900 TRACE("%p %p\n", This
, ppstm
);
906 if (!This
->parentStorage
)
907 return STG_E_REVERTED
;
910 return STG_E_INVALIDPOINTER
;
912 new_stream
= StgStreamImpl_Construct (This
->parentStorage
, This
->grfMode
, This
->dirEntry
);
915 return STG_E_INSUFFICIENTMEMORY
; /* Currently the only reason for new_stream=0 */
917 *ppstm
= (IStream
*) new_stream
;
918 IStream_AddRef(*ppstm
);
920 seek_pos
.QuadPart
= This
->currentPosition
.QuadPart
;
922 hres
=StgStreamImpl_Seek (*ppstm
, seek_pos
, STREAM_SEEK_SET
, NULL
);
924 assert (SUCCEEDED(hres
));
930 * Virtual function table for the StgStreamImpl class.
932 static const IStreamVtbl StgStreamImpl_Vtbl
=
934 StgStreamImpl_QueryInterface
,
935 StgStreamImpl_AddRef
,
936 StgStreamImpl_Release
,
940 StgStreamImpl_SetSize
,
941 StgStreamImpl_CopyTo
,
942 StgStreamImpl_Commit
,
943 StgStreamImpl_Revert
,
944 StgStreamImpl_LockRegion
,
945 StgStreamImpl_UnlockRegion
,
950 /******************************************************************************
951 ** StgStreamImpl implementation
955 * This is the constructor for the StgStreamImpl class.
958 * parentStorage - Pointer to the storage that contains the stream to open
959 * dirEntry - Index of the directory entry that points to this stream.
961 StgStreamImpl
* StgStreamImpl_Construct(
962 StorageBaseImpl
* parentStorage
,
966 StgStreamImpl
* newStream
;
968 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl
));
973 * Set-up the virtual function table and reference count.
975 newStream
->lpVtbl
= &StgStreamImpl_Vtbl
;
978 newStream
->parentStorage
= parentStorage
;
981 * We want to nail-down the reference to the storage in case the
982 * stream out-lives the storage in the client application.
984 * -- IStorage_AddRef((IStorage*)newStream->parentStorage);
986 * No, don't do this. Some apps call IStorage_Release without
987 * calling IStream_Release first. If we grab a reference the
988 * file is not closed, and the app fails when it tries to
989 * reopen the file (Easy-PC, for example)
992 newStream
->grfMode
= grfMode
;
993 newStream
->dirEntry
= dirEntry
;
996 * Start the stream at the beginning.
998 newStream
->currentPosition
.u
.HighPart
= 0;
999 newStream
->currentPosition
.u
.LowPart
= 0;
1002 * Initialize the rest of the data.
1004 newStream
->streamSize
.u
.HighPart
= 0;
1005 newStream
->streamSize
.u
.LowPart
= 0;
1006 newStream
->bigBlockChain
= 0;
1007 newStream
->smallBlockChain
= 0;
1010 * Read the size from the directory entry and determine if the blocks forming
1011 * this stream are large or small.
1013 StgStreamImpl_OpenBlockChain(newStream
);
1015 /* add us to the storage's list of active streams */
1016 StorageBaseImpl_AddStream(parentStorage
, newStream
);