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
41 #include "wine/debug.h"
43 #include "storage32.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(storage
);
49 * This is the destructor of the StgStreamImpl class.
51 * This method will clean-up all the resources used-up by the given StgStreamImpl
52 * class. The pointer passed-in to this function will be freed and will not
55 static void StgStreamImpl_Destroy(StgStreamImpl
* This
)
57 TRACE("(%p)\n", This
);
60 * Release the reference we are holding on the parent storage.
61 * IStorage_Release((IStorage*)This->parentStorage);
63 * No, don't do this. Some apps call IStorage_Release without
64 * calling IStream_Release first. If we grab a reference the
65 * file is not closed, and the app fails when it tries to
66 * reopen the file (Easy-PC, for example). Just inform the
67 * storage that we have closed the stream
70 if(This
->parentStorage
) {
72 StorageBaseImpl_RemoveStream(This
->parentStorage
, This
);
76 This
->parentStorage
= 0;
79 * Make sure we clean-up the block chain stream objects that we were using.
81 if (This
->bigBlockChain
!= 0)
83 BlockChainStream_Destroy(This
->bigBlockChain
);
84 This
->bigBlockChain
= 0;
87 if (This
->smallBlockChain
!= 0)
89 SmallBlockChainStream_Destroy(This
->smallBlockChain
);
90 This
->smallBlockChain
= 0;
94 * Finally, free the memory used-up by the class.
96 HeapFree(GetProcessHeap(), 0, This
);
100 * This implements the IUnknown method QueryInterface for this
103 static HRESULT WINAPI
StgStreamImpl_QueryInterface(
105 REFIID riid
, /* [in] */
106 void** ppvObject
) /* [iid_is][out] */
108 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
111 * Perform a sanity check on the parameters.
117 * Initialize the return parameter.
122 * Compare the riid with the interface IDs implemented by this object.
124 if (IsEqualIID(&IID_IUnknown
, riid
) ||
125 IsEqualIID(&IID_IPersist
, riid
) ||
126 IsEqualIID(&IID_IPersistStream
, riid
) ||
127 IsEqualIID(&IID_ISequentialStream
, riid
) ||
128 IsEqualIID(&IID_IStream
, riid
))
130 *ppvObject
= (IStream
*)This
;
134 * Check that we obtained an interface.
137 return E_NOINTERFACE
;
140 * Query Interface always increases the reference count by one when it is
143 IStream_AddRef(iface
);
149 * This implements the IUnknown method AddRef for this
152 static ULONG WINAPI
StgStreamImpl_AddRef(
155 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
156 return InterlockedIncrement(&This
->ref
);
160 * This implements the IUnknown method Release for this
163 static ULONG WINAPI
StgStreamImpl_Release(
166 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
170 ref
= InterlockedDecrement(&This
->ref
);
173 * If the reference count goes down to 0, perform suicide.
177 StgStreamImpl_Destroy(This
);
184 * This method will open the block chain pointed by the property
185 * that describes the stream.
186 * If the stream's size is null, no chain is opened.
188 static void StgStreamImpl_OpenBlockChain(
191 StgProperty curProperty
;
195 * Make sure no old object is left over.
197 if (This
->smallBlockChain
!= 0)
199 SmallBlockChainStream_Destroy(This
->smallBlockChain
);
200 This
->smallBlockChain
= 0;
203 if (This
->bigBlockChain
!= 0)
205 BlockChainStream_Destroy(This
->bigBlockChain
);
206 This
->bigBlockChain
= 0;
210 * Read the information from the property.
212 readSuccessful
= StorageImpl_ReadProperty(This
->parentStorage
->ancestorStorage
,
218 This
->streamSize
= curProperty
.size
;
221 * This code supports only streams that are <32 bits in size.
223 assert(This
->streamSize
.u
.HighPart
== 0);
225 if(curProperty
.startingBlock
== BLOCK_END_OF_CHAIN
)
227 assert( (This
->streamSize
.u
.HighPart
== 0) && (This
->streamSize
.u
.LowPart
== 0) );
231 if ( (This
->streamSize
.u
.HighPart
== 0) &&
232 (This
->streamSize
.u
.LowPart
< LIMIT_TO_USE_SMALL_BLOCK
) )
234 This
->smallBlockChain
= SmallBlockChainStream_Construct(
235 This
->parentStorage
->ancestorStorage
,
236 This
->ownerProperty
);
240 This
->bigBlockChain
= BlockChainStream_Construct(
241 This
->parentStorage
->ancestorStorage
,
243 This
->ownerProperty
);
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
)
489 return STG_E_REVERTED
;
492 * The caller is allowed to pass in NULL as the new position return value.
493 * If it happens, we assign it to a dynamic variable to avoid special cases
496 if (plibNewPosition
== 0)
498 plibNewPosition
= &newPosition
;
502 * The file pointer is moved depending on the given "function"
507 case STREAM_SEEK_SET
:
508 plibNewPosition
->u
.HighPart
= 0;
509 plibNewPosition
->u
.LowPart
= 0;
511 case STREAM_SEEK_CUR
:
512 *plibNewPosition
= This
->currentPosition
;
514 case STREAM_SEEK_END
:
515 *plibNewPosition
= This
->streamSize
;
518 return STG_E_INVALIDFUNCTION
;
521 plibNewPosition
->QuadPart
= RtlLargeIntegerAdd( plibNewPosition
->QuadPart
, dlibMove
.QuadPart
);
524 * tell the caller what we calculated
526 This
->currentPosition
= *plibNewPosition
;
532 * This method is part of the IStream interface.
534 * It will change the size of a stream.
536 * TODO: Switch from small blocks to big blocks and vice versa.
538 * See the documentation of IStream for more info.
540 static HRESULT WINAPI
StgStreamImpl_SetSize(
542 ULARGE_INTEGER libNewSize
) /* [in] */
544 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
546 StgProperty curProperty
;
549 TRACE("(%p, %d)\n", iface
, libNewSize
.u
.LowPart
);
551 if(!This
->parentStorage
)
552 return STG_E_REVERTED
;
557 if (libNewSize
.u
.HighPart
!= 0)
558 return STG_E_INVALIDFUNCTION
;
561 * Do we have permission?
563 if (!(This
->grfMode
& (STGM_WRITE
| STGM_READWRITE
)))
564 return STG_E_ACCESSDENIED
;
566 if (This
->streamSize
.u
.LowPart
== libNewSize
.u
.LowPart
)
570 * This will happen if we're creating a stream
572 if ((This
->smallBlockChain
== 0) && (This
->bigBlockChain
== 0))
574 if (libNewSize
.u
.LowPart
< LIMIT_TO_USE_SMALL_BLOCK
)
576 This
->smallBlockChain
= SmallBlockChainStream_Construct(
577 This
->parentStorage
->ancestorStorage
,
578 This
->ownerProperty
);
582 This
->bigBlockChain
= BlockChainStream_Construct(
583 This
->parentStorage
->ancestorStorage
,
585 This
->ownerProperty
);
590 * Read this stream's property to see if it's small blocks or big blocks
592 Success
= StorageImpl_ReadProperty(This
->parentStorage
->ancestorStorage
,
596 * Determine if we have to switch from small to big blocks or vice versa
598 if ( (This
->smallBlockChain
!=0) &&
599 (curProperty
.size
.u
.LowPart
< LIMIT_TO_USE_SMALL_BLOCK
) )
601 if (libNewSize
.u
.LowPart
>= LIMIT_TO_USE_SMALL_BLOCK
)
604 * Transform the small block chain into a big block chain
606 This
->bigBlockChain
= Storage32Impl_SmallBlocksToBigBlocks(
607 This
->parentStorage
->ancestorStorage
,
608 &This
->smallBlockChain
);
612 if (This
->smallBlockChain
!=0)
614 Success
= SmallBlockChainStream_SetSize(This
->smallBlockChain
, libNewSize
);
618 Success
= BlockChainStream_SetSize(This
->bigBlockChain
, libNewSize
);
622 * Write the new information about this stream to the property
624 Success
= StorageImpl_ReadProperty(This
->parentStorage
->ancestorStorage
,
628 curProperty
.size
.u
.HighPart
= libNewSize
.u
.HighPart
;
629 curProperty
.size
.u
.LowPart
= libNewSize
.u
.LowPart
;
633 StorageImpl_WriteProperty(This
->parentStorage
->ancestorStorage
,
638 This
->streamSize
= libNewSize
;
644 * This method is part of the IStream interface.
646 * It will copy the 'cb' Bytes to 'pstm' IStream.
648 * See the documentation of IStream for more info.
650 static HRESULT WINAPI
StgStreamImpl_CopyTo(
652 IStream
* pstm
, /* [unique][in] */
653 ULARGE_INTEGER cb
, /* [in] */
654 ULARGE_INTEGER
* pcbRead
, /* [out] */
655 ULARGE_INTEGER
* pcbWritten
) /* [out] */
657 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
660 ULONG bytesRead
, bytesWritten
, copySize
;
661 ULARGE_INTEGER totalBytesRead
;
662 ULARGE_INTEGER totalBytesWritten
;
664 TRACE("(%p, %p, %d, %p, %p)\n",
665 iface
, pstm
, cb
.u
.LowPart
, pcbRead
, pcbWritten
);
671 if (!This
->parentStorage
)
672 return STG_E_REVERTED
;
675 return STG_E_INVALIDPOINTER
;
677 totalBytesRead
.u
.LowPart
= totalBytesRead
.u
.HighPart
= 0;
678 totalBytesWritten
.u
.LowPart
= totalBytesWritten
.u
.HighPart
= 0;
681 * use stack to store data temporarily
682 * there is surely a more performant way of doing it, for now this basic
683 * implementation will do the job
685 while ( cb
.u
.LowPart
> 0 )
687 if ( cb
.u
.LowPart
>= 128 )
690 copySize
= cb
.u
.LowPart
;
692 IStream_Read(iface
, tmpBuffer
, copySize
, &bytesRead
);
694 totalBytesRead
.u
.LowPart
+= bytesRead
;
696 IStream_Write(pstm
, tmpBuffer
, bytesRead
, &bytesWritten
);
698 totalBytesWritten
.u
.LowPart
+= bytesWritten
;
701 * Check that read & write operations were successful
703 if (bytesRead
!= bytesWritten
)
705 hr
= STG_E_MEDIUMFULL
;
709 if (bytesRead
!=copySize
)
712 cb
.u
.LowPart
-= bytesRead
;
716 * Update number of bytes read and written
720 pcbRead
->u
.LowPart
= totalBytesRead
.u
.LowPart
;
721 pcbRead
->u
.HighPart
= totalBytesRead
.u
.HighPart
;
726 pcbWritten
->u
.LowPart
= totalBytesWritten
.u
.LowPart
;
727 pcbWritten
->u
.HighPart
= totalBytesWritten
.u
.HighPart
;
733 * This method is part of the IStream interface.
735 * For streams contained in structured storages, this method
736 * does nothing. This is what the documentation tells us.
738 * See the documentation of IStream for more info.
740 static HRESULT WINAPI
StgStreamImpl_Commit(
742 DWORD grfCommitFlags
) /* [in] */
744 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
746 if (!This
->parentStorage
)
747 return STG_E_REVERTED
;
753 * This method is part of the IStream interface.
755 * For streams contained in structured storages, this method
756 * does nothing. This is what the documentation tells us.
758 * See the documentation of IStream for more info.
760 static HRESULT WINAPI
StgStreamImpl_Revert(
766 static HRESULT WINAPI
StgStreamImpl_LockRegion(
768 ULARGE_INTEGER libOffset
, /* [in] */
769 ULARGE_INTEGER cb
, /* [in] */
770 DWORD dwLockType
) /* [in] */
772 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
774 if (!This
->parentStorage
)
775 return STG_E_REVERTED
;
777 FIXME("not implemented!\n");
781 static HRESULT WINAPI
StgStreamImpl_UnlockRegion(
783 ULARGE_INTEGER libOffset
, /* [in] */
784 ULARGE_INTEGER cb
, /* [in] */
785 DWORD dwLockType
) /* [in] */
787 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
789 if (!This
->parentStorage
)
790 return STG_E_REVERTED
;
792 FIXME("not implemented!\n");
797 * This method is part of the IStream interface.
799 * This method returns information about the current
802 * See the documentation of IStream for more info.
804 static HRESULT WINAPI
StgStreamImpl_Stat(
806 STATSTG
* pstatstg
, /* [out] */
807 DWORD grfStatFlag
) /* [in] */
809 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
811 StgProperty curProperty
;
814 TRACE("%p %p %d\n", This
, pstatstg
, grfStatFlag
);
817 * if stream has no parent, return STG_E_REVERTED
820 if (!This
->parentStorage
)
821 return STG_E_REVERTED
;
824 * Read the information from the property.
826 readSuccessful
= StorageImpl_ReadProperty(This
->parentStorage
->ancestorStorage
,
832 StorageUtl_CopyPropertyToSTATSTG(pstatstg
,
836 pstatstg
->grfMode
= This
->grfMode
;
845 * This method is part of the IStream interface.
847 * This method returns a clone of the interface that allows for
848 * another seek pointer
850 * See the documentation of IStream for more info.
852 * I am not totally sure what I am doing here but I presume that this
853 * should be basically as simple as creating a new stream with the same
854 * parent etc and positioning its seek cursor.
856 static HRESULT WINAPI
StgStreamImpl_Clone(
858 IStream
** ppstm
) /* [out] */
860 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
862 StgStreamImpl
* new_stream
;
863 LARGE_INTEGER seek_pos
;
865 TRACE("%p %p\n", This
, ppstm
);
871 if (!This
->parentStorage
)
872 return STG_E_REVERTED
;
875 return STG_E_INVALIDPOINTER
;
877 new_stream
= StgStreamImpl_Construct (This
->parentStorage
, This
->grfMode
, This
->ownerProperty
);
880 return STG_E_INSUFFICIENTMEMORY
; /* Currently the only reason for new_stream=0 */
882 *ppstm
= (IStream
*) new_stream
;
883 IStream_AddRef(*ppstm
);
885 seek_pos
.QuadPart
= This
->currentPosition
.QuadPart
;
887 hres
=StgStreamImpl_Seek (*ppstm
, seek_pos
, STREAM_SEEK_SET
, NULL
);
889 assert (SUCCEEDED(hres
));
895 * Virtual function table for the StgStreamImpl class.
897 static const IStreamVtbl StgStreamImpl_Vtbl
=
899 StgStreamImpl_QueryInterface
,
900 StgStreamImpl_AddRef
,
901 StgStreamImpl_Release
,
905 StgStreamImpl_SetSize
,
906 StgStreamImpl_CopyTo
,
907 StgStreamImpl_Commit
,
908 StgStreamImpl_Revert
,
909 StgStreamImpl_LockRegion
,
910 StgStreamImpl_UnlockRegion
,
915 /******************************************************************************
916 ** StgStreamImpl implementation
920 * This is the constructor for the StgStreamImpl class.
923 * parentStorage - Pointer to the storage that contains the stream to open
924 * ownerProperty - Index of the property that points to this stream.
926 StgStreamImpl
* StgStreamImpl_Construct(
927 StorageBaseImpl
* parentStorage
,
931 StgStreamImpl
* newStream
;
933 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl
));
938 * Set-up the virtual function table and reference count.
940 newStream
->lpVtbl
= &StgStreamImpl_Vtbl
;
943 newStream
->parentStorage
= parentStorage
;
946 * We want to nail-down the reference to the storage in case the
947 * stream out-lives the storage in the client application.
949 * -- IStorage_AddRef((IStorage*)newStream->parentStorage);
951 * No, don't do this. Some apps call IStorage_Release without
952 * calling IStream_Release first. If we grab a reference the
953 * file is not closed, and the app fails when it tries to
954 * reopen the file (Easy-PC, for example)
957 newStream
->grfMode
= grfMode
;
958 newStream
->ownerProperty
= ownerProperty
;
961 * Start the stream at the beginning.
963 newStream
->currentPosition
.u
.HighPart
= 0;
964 newStream
->currentPosition
.u
.LowPart
= 0;
967 * Initialize the rest of the data.
969 newStream
->streamSize
.u
.HighPart
= 0;
970 newStream
->streamSize
.u
.LowPart
= 0;
971 newStream
->bigBlockChain
= 0;
972 newStream
->smallBlockChain
= 0;
975 * Read the size from the property and determine if the blocks forming
976 * this stream are large or small.
978 StgStreamImpl_OpenBlockChain(newStream
);
980 /* add us to the storage's list of active streams */
981 StorageBaseImpl_AddStream(parentStorage
, newStream
);