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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include "wine/debug.h"
35 #include "storage32.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(storage
);
41 * Virtual function table for the StgStreamImpl class.
43 static ICOM_VTABLE(IStream
) StgStreamImpl_Vtbl
=
45 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
46 StgStreamImpl_QueryInterface
,
48 StgStreamImpl_Release
,
52 StgStreamImpl_SetSize
,
56 StgStreamImpl_LockRegion
,
57 StgStreamImpl_UnlockRegion
,
62 /******************************************************************************
63 ** StgStreamImpl implementation
67 * This is the constructor for the StgStreamImpl class.
70 * parentStorage - Pointer to the storage that contains the stream to open
71 * ownerProperty - Index of the property that points to this stream.
73 StgStreamImpl
* StgStreamImpl_Construct(
74 StorageBaseImpl
* parentStorage
,
78 StgStreamImpl
* newStream
;
80 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl
));
85 * Set-up the virtual function table and reference count.
87 ICOM_VTBL(newStream
) = &StgStreamImpl_Vtbl
;
91 * We want to nail-down the reference to the storage in case the
92 * stream out-lives the storage in the client application.
94 newStream
->parentStorage
= parentStorage
;
95 IStorage_AddRef((IStorage
*)newStream
->parentStorage
);
97 newStream
->grfMode
= grfMode
;
98 newStream
->ownerProperty
= ownerProperty
;
101 * Start the stream at the beginning.
103 newStream
->currentPosition
.s
.HighPart
= 0;
104 newStream
->currentPosition
.s
.LowPart
= 0;
107 * Initialize the rest of the data.
109 newStream
->streamSize
.s
.HighPart
= 0;
110 newStream
->streamSize
.s
.LowPart
= 0;
111 newStream
->bigBlockChain
= 0;
112 newStream
->smallBlockChain
= 0;
115 * Read the size from the property and determine if the blocks forming
116 * this stream are large or small.
118 StgStreamImpl_OpenBlockChain(newStream
);
125 * This is the destructor of the StgStreamImpl class.
127 * This method will clean-up all the resources used-up by the given StgStreamImpl
128 * class. The pointer passed-in to this function will be freed and will not
131 void StgStreamImpl_Destroy(StgStreamImpl
* This
)
133 TRACE("(%p)\n", This
);
136 * Release the reference we are holding on the parent storage.
138 IStorage_Release((IStorage
*)This
->parentStorage
);
139 This
->parentStorage
= 0;
142 * Make sure we clean-up the block chain stream objects that we were using.
144 if (This
->bigBlockChain
!= 0)
146 BlockChainStream_Destroy(This
->bigBlockChain
);
147 This
->bigBlockChain
= 0;
150 if (This
->smallBlockChain
!= 0)
152 SmallBlockChainStream_Destroy(This
->smallBlockChain
);
153 This
->smallBlockChain
= 0;
157 * Finally, free the memory used-up by the class.
159 HeapFree(GetProcessHeap(), 0, This
);
163 * This implements the IUnknown method QueryInterface for this
166 HRESULT WINAPI
StgStreamImpl_QueryInterface(
168 REFIID riid
, /* [in] */
169 void** ppvObject
) /* [iid_is][out] */
171 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
174 * Perform a sanity check on the parameters.
180 * Initialize the return parameter.
185 * Compare the riid with the interface IDs implemented by this object.
187 if (memcmp(&IID_IUnknown
, riid
, sizeof(IID_IUnknown
)) == 0)
189 *ppvObject
= (IStream
*)This
;
191 else if (memcmp(&IID_IStream
, riid
, sizeof(IID_IStream
)) == 0)
193 *ppvObject
= (IStream
*)This
;
197 * Check that we obtained an interface.
200 return E_NOINTERFACE
;
203 * Query Interface always increases the reference count by one when it is
206 StgStreamImpl_AddRef(iface
);
212 * This implements the IUnknown method AddRef for this
215 ULONG WINAPI
StgStreamImpl_AddRef(
218 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
226 * This implements the IUnknown method Release for this
229 ULONG WINAPI
StgStreamImpl_Release(
232 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
241 * If the reference count goes down to 0, perform suicide.
245 StgStreamImpl_Destroy(This
);
252 * This method will open the block chain pointed by the property
253 * that describes the stream.
254 * If the stream's size is null, no chain is opened.
256 void StgStreamImpl_OpenBlockChain(
259 StgProperty curProperty
;
263 * Make sure no old object is left over.
265 if (This
->smallBlockChain
!= 0)
267 SmallBlockChainStream_Destroy(This
->smallBlockChain
);
268 This
->smallBlockChain
= 0;
271 if (This
->bigBlockChain
!= 0)
273 BlockChainStream_Destroy(This
->bigBlockChain
);
274 This
->bigBlockChain
= 0;
278 * Read the information from the property.
280 readSucessful
= StorageImpl_ReadProperty(This
->parentStorage
->ancestorStorage
,
286 This
->streamSize
= curProperty
.size
;
289 * This code supports only streams that are <32 bits in size.
291 assert(This
->streamSize
.s
.HighPart
== 0);
293 if(curProperty
.startingBlock
== BLOCK_END_OF_CHAIN
)
295 assert( (This
->streamSize
.s
.HighPart
== 0) && (This
->streamSize
.s
.LowPart
== 0) );
299 if ( (This
->streamSize
.s
.HighPart
== 0) &&
300 (This
->streamSize
.s
.LowPart
< LIMIT_TO_USE_SMALL_BLOCK
) )
302 This
->smallBlockChain
= SmallBlockChainStream_Construct(
303 This
->parentStorage
->ancestorStorage
,
304 This
->ownerProperty
);
308 This
->bigBlockChain
= BlockChainStream_Construct(
309 This
->parentStorage
->ancestorStorage
,
311 This
->ownerProperty
);
318 * This method is part of the ISequentialStream interface.
320 * It reads a block of information from the stream at the current
321 * position. It then moves the current position at the end of the
324 * See the documentation of ISequentialStream for more info.
326 HRESULT WINAPI
StgStreamImpl_Read(
328 void* pv
, /* [length_is][size_is][out] */
330 ULONG
* pcbRead
) /* [out] */
332 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
334 ULONG bytesReadBuffer
;
335 ULONG bytesToReadFromBuffer
;
336 HRESULT res
= S_FALSE
;
338 TRACE("(%p, %p, %ld, %p)\n",
339 iface
, pv
, cb
, pcbRead
);
342 * If the caller is not interested in the number of bytes read,
343 * we use another buffer to avoid "if" statements in the code.
346 pcbRead
= &bytesReadBuffer
;
349 * Using the known size of the stream, calculate the number of bytes
350 * to read from the block chain
352 bytesToReadFromBuffer
= min( This
->streamSize
.s
.LowPart
- This
->currentPosition
.s
.LowPart
, cb
);
355 * Depending on the type of chain that was opened when the stream was constructed,
356 * we delegate the work to the method that reads the block chains.
358 if (This
->smallBlockChain
!=0)
360 SmallBlockChainStream_ReadAt(This
->smallBlockChain
,
361 This
->currentPosition
,
362 bytesToReadFromBuffer
,
367 else if (This
->bigBlockChain
!=0)
369 BlockChainStream_ReadAt(This
->bigBlockChain
,
370 This
->currentPosition
,
371 bytesToReadFromBuffer
,
378 * Small and big block chains are both NULL. This case will happen
379 * when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
388 * We should always be able to read the proper amount of data from the
391 assert(bytesToReadFromBuffer
== *pcbRead
);
394 * Advance the pointer for the number of positions read.
396 This
->currentPosition
.s
.LowPart
+= *pcbRead
;
400 WARN("read %ld instead of the required %ld bytes !\n", *pcbRead
, cb
);
402 * this used to return S_FALSE, however MSDN docu says that an app should
403 * be prepared to handle error in case of stream end reached, as *some*
404 * implementations *might* return an error (IOW: most do *not*).
405 * As some program fails on returning S_FALSE, I better use S_OK here.
413 TRACE("<-- %08lx\n", res
);
418 * This method is part of the ISequentialStream interface.
420 * It writes a block of information to the stream at the current
421 * position. It then moves the current position at the end of the
422 * written block. If the stream is too small to fit the block,
423 * the stream is grown to fit.
425 * See the documentation of ISequentialStream for more info.
427 HRESULT WINAPI
StgStreamImpl_Write(
429 const void* pv
, /* [size_is][in] */
431 ULONG
* pcbWritten
) /* [out] */
433 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
435 ULARGE_INTEGER newSize
;
436 ULONG bytesWritten
= 0;
438 TRACE("(%p, %p, %ld, %p)\n",
439 iface
, pv
, cb
, pcbWritten
);
442 * Do we have permission to write to this stream?
444 if (!(This
->grfMode
& (STGM_WRITE
| STGM_READWRITE
))) {
445 return STG_E_ACCESSDENIED
;
449 * If the caller is not interested in the number of bytes written,
450 * we use another buffer to avoid "if" statements in the code.
453 pcbWritten
= &bytesWritten
;
456 * Initialize the out parameter
466 newSize
.s
.HighPart
= 0;
467 newSize
.s
.LowPart
= This
->currentPosition
.s
.LowPart
+ cb
;
471 * Verify if we need to grow the stream
473 if (newSize
.s
.LowPart
> This
->streamSize
.s
.LowPart
)
476 IStream_SetSize(iface
, newSize
);
480 * Depending on the type of chain that was opened when the stream was constructed,
481 * we delegate the work to the method that readwrites to the block chains.
483 if (This
->smallBlockChain
!=0)
485 SmallBlockChainStream_WriteAt(This
->smallBlockChain
,
486 This
->currentPosition
,
492 else if (This
->bigBlockChain
!=0)
494 BlockChainStream_WriteAt(This
->bigBlockChain
,
495 This
->currentPosition
,
504 * Advance the position pointer for the number of positions written.
506 This
->currentPosition
.s
.LowPart
+= *pcbWritten
;
512 * This method is part of the IStream interface.
514 * It will move the current stream pointer according to the parameters
517 * See the documentation of IStream for more info.
519 HRESULT WINAPI
StgStreamImpl_Seek(
521 LARGE_INTEGER dlibMove
, /* [in] */
522 DWORD dwOrigin
, /* [in] */
523 ULARGE_INTEGER
* plibNewPosition
) /* [out] */
525 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
527 ULARGE_INTEGER newPosition
;
529 TRACE("(%p, %ld, %ld, %p)\n",
530 iface
, dlibMove
.s
.LowPart
, dwOrigin
, plibNewPosition
);
533 * The caller is allowed to pass in NULL as the new position return value.
534 * If it happens, we assign it to a dynamic variable to avoid special cases
537 if (plibNewPosition
== 0)
539 plibNewPosition
= &newPosition
;
543 * The file pointer is moved depending on the given "function"
548 case STREAM_SEEK_SET
:
549 plibNewPosition
->s
.HighPart
= 0;
550 plibNewPosition
->s
.LowPart
= 0;
552 case STREAM_SEEK_CUR
:
553 *plibNewPosition
= This
->currentPosition
;
555 case STREAM_SEEK_END
:
556 *plibNewPosition
= This
->streamSize
;
559 return STG_E_INVALIDFUNCTION
;
562 plibNewPosition
->QuadPart
= RtlLargeIntegerAdd( plibNewPosition
->QuadPart
, dlibMove
.QuadPart
);
565 * tell the caller what we calculated
567 This
->currentPosition
= *plibNewPosition
;
573 * This method is part of the IStream interface.
575 * It will change the size of a stream.
577 * TODO: Switch from small blocks to big blocks and vice versa.
579 * See the documentation of IStream for more info.
581 HRESULT WINAPI
StgStreamImpl_SetSize(
583 ULARGE_INTEGER libNewSize
) /* [in] */
585 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
587 StgProperty curProperty
;
590 TRACE("(%p, %ld)\n", iface
, libNewSize
.s
.LowPart
);
595 if (libNewSize
.s
.HighPart
!= 0)
596 return STG_E_INVALIDFUNCTION
;
599 * Do we have permission?
601 if (!(This
->grfMode
& (STGM_WRITE
| STGM_READWRITE
)))
602 return STG_E_ACCESSDENIED
;
604 if (This
->streamSize
.s
.LowPart
== libNewSize
.s
.LowPart
)
608 * This will happen if we're creating a stream
610 if ((This
->smallBlockChain
== 0) && (This
->bigBlockChain
== 0))
612 if (libNewSize
.s
.LowPart
< LIMIT_TO_USE_SMALL_BLOCK
)
614 This
->smallBlockChain
= SmallBlockChainStream_Construct(
615 This
->parentStorage
->ancestorStorage
,
616 This
->ownerProperty
);
620 This
->bigBlockChain
= BlockChainStream_Construct(
621 This
->parentStorage
->ancestorStorage
,
623 This
->ownerProperty
);
628 * Read this stream's property to see if it's small blocks or big blocks
630 Success
= StorageImpl_ReadProperty(This
->parentStorage
->ancestorStorage
,
634 * Determine if we have to switch from small to big blocks or vice versa
636 if ( (This
->smallBlockChain
!=0) &&
637 (curProperty
.size
.s
.LowPart
< LIMIT_TO_USE_SMALL_BLOCK
) )
639 if (libNewSize
.s
.LowPart
>= LIMIT_TO_USE_SMALL_BLOCK
)
642 * Transform the small block chain into a big block chain
644 This
->bigBlockChain
= Storage32Impl_SmallBlocksToBigBlocks(
645 This
->parentStorage
->ancestorStorage
,
646 &This
->smallBlockChain
);
650 if (This
->smallBlockChain
!=0)
652 Success
= SmallBlockChainStream_SetSize(This
->smallBlockChain
, libNewSize
);
656 Success
= BlockChainStream_SetSize(This
->bigBlockChain
, libNewSize
);
660 * Write the new information about this stream to the property
662 Success
= StorageImpl_ReadProperty(This
->parentStorage
->ancestorStorage
,
666 curProperty
.size
.s
.HighPart
= libNewSize
.s
.HighPart
;
667 curProperty
.size
.s
.LowPart
= libNewSize
.s
.LowPart
;
671 StorageImpl_WriteProperty(This
->parentStorage
->ancestorStorage
,
676 This
->streamSize
= libNewSize
;
682 * This method is part of the IStream interface.
684 * It will copy the 'cb' Bytes to 'pstm' IStream.
686 * See the documentation of IStream for more info.
688 HRESULT WINAPI
StgStreamImpl_CopyTo(
690 IStream
* pstm
, /* [unique][in] */
691 ULARGE_INTEGER cb
, /* [in] */
692 ULARGE_INTEGER
* pcbRead
, /* [out] */
693 ULARGE_INTEGER
* pcbWritten
) /* [out] */
697 ULONG bytesRead
, bytesWritten
, copySize
;
698 ULARGE_INTEGER totalBytesRead
;
699 ULARGE_INTEGER totalBytesWritten
;
701 TRACE("(%p, %p, %ld, %p, %p)\n",
702 iface
, pstm
, cb
.s
.LowPart
, pcbRead
, pcbWritten
);
708 return STG_E_INVALIDPOINTER
;
710 totalBytesRead
.s
.LowPart
= totalBytesRead
.s
.HighPart
= 0;
711 totalBytesWritten
.s
.LowPart
= totalBytesWritten
.s
.HighPart
= 0;
714 * use stack to store data temporarily
715 * there is surely a more performant way of doing it, for now this basic
716 * implementation will do the job
718 while ( cb
.s
.LowPart
> 0 )
720 if ( cb
.s
.LowPart
>= 128 )
723 copySize
= cb
.s
.LowPart
;
725 IStream_Read(iface
, tmpBuffer
, copySize
, &bytesRead
);
727 totalBytesRead
.s
.LowPart
+= bytesRead
;
729 IStream_Write(pstm
, tmpBuffer
, bytesRead
, &bytesWritten
);
731 totalBytesWritten
.s
.LowPart
+= bytesWritten
;
734 * Check that read & write operations were successful
736 if (bytesRead
!= bytesWritten
)
738 hr
= STG_E_MEDIUMFULL
;
742 if (bytesRead
!=copySize
)
745 cb
.s
.LowPart
-= bytesRead
;
749 * Update number of bytes read and written
753 pcbRead
->s
.LowPart
= totalBytesRead
.s
.LowPart
;
754 pcbRead
->s
.HighPart
= totalBytesRead
.s
.HighPart
;
759 pcbWritten
->s
.LowPart
= totalBytesWritten
.s
.LowPart
;
760 pcbWritten
->s
.HighPart
= totalBytesWritten
.s
.HighPart
;
766 * This method is part of the IStream interface.
768 * For streams contained in structured storages, this method
769 * does nothing. This is what the documentation tells us.
771 * See the documentation of IStream for more info.
773 HRESULT WINAPI
StgStreamImpl_Commit(
775 DWORD grfCommitFlags
) /* [in] */
781 * This method is part of the IStream interface.
783 * For streams contained in structured storages, this method
784 * does nothing. This is what the documentation tells us.
786 * See the documentation of IStream for more info.
788 HRESULT WINAPI
StgStreamImpl_Revert(
794 HRESULT WINAPI
StgStreamImpl_LockRegion(
796 ULARGE_INTEGER libOffset
, /* [in] */
797 ULARGE_INTEGER cb
, /* [in] */
798 DWORD dwLockType
) /* [in] */
800 FIXME("not implemented!\n");
804 HRESULT WINAPI
StgStreamImpl_UnlockRegion(
806 ULARGE_INTEGER libOffset
, /* [in] */
807 ULARGE_INTEGER cb
, /* [in] */
808 DWORD dwLockType
) /* [in] */
810 FIXME("not implemented!\n");
815 * This method is part of the IStream interface.
817 * This method returns information about the current
820 * See the documentation of IStream for more info.
822 HRESULT WINAPI
StgStreamImpl_Stat(
824 STATSTG
* pstatstg
, /* [out] */
825 DWORD grfStatFlag
) /* [in] */
827 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
829 StgProperty curProperty
;
833 * Read the information from the property.
835 readSucessful
= StorageImpl_ReadProperty(This
->parentStorage
->ancestorStorage
,
841 StorageUtl_CopyPropertyToSTATSTG(pstatstg
,
845 pstatstg
->grfMode
= This
->grfMode
;
854 * This method is part of the IStream interface.
856 * This method returns a clone of the interface that allows for
857 * another seek pointer
859 * See the documentation of IStream for more info.
861 * I am not totally sure what I am doing here but I presume that this
862 * should be basically as simple as creating a new stream with the same
863 * parent etc and positioning its seek cursor.
865 HRESULT WINAPI
StgStreamImpl_Clone(
867 IStream
** ppstm
) /* [out] */
869 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
871 StgStreamImpl
* new_stream
;
872 LARGE_INTEGER seek_pos
;
878 return STG_E_INVALIDPOINTER
;
880 new_stream
= StgStreamImpl_Construct (This
->parentStorage
, This
->grfMode
, This
->ownerProperty
);
883 return STG_E_INSUFFICIENTMEMORY
; /* Currently the only reason for new_stream=0 */
885 *ppstm
= (IStream
*) new_stream
;
886 seek_pos
.QuadPart
= This
->currentPosition
.QuadPart
;
888 hres
=StgStreamImpl_Seek (*ppstm
, seek_pos
, STREAM_SEEK_SET
, NULL
);
890 assert (SUCCEEDED(hres
));