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 #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.
62 IStorage_Release((IStorage
*)This
->parentStorage
);
63 This
->parentStorage
= 0;
66 * Make sure we clean-up the block chain stream objects that we were using.
68 if (This
->bigBlockChain
!= 0)
70 BlockChainStream_Destroy(This
->bigBlockChain
);
71 This
->bigBlockChain
= 0;
74 if (This
->smallBlockChain
!= 0)
76 SmallBlockChainStream_Destroy(This
->smallBlockChain
);
77 This
->smallBlockChain
= 0;
81 * Finally, free the memory used-up by the class.
83 HeapFree(GetProcessHeap(), 0, This
);
87 * This implements the IUnknown method QueryInterface for this
90 static HRESULT WINAPI
StgStreamImpl_QueryInterface(
92 REFIID riid
, /* [in] */
93 void** ppvObject
) /* [iid_is][out] */
95 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
98 * Perform a sanity check on the parameters.
104 * Initialize the return parameter.
109 * Compare the riid with the interface IDs implemented by this object.
111 if (IsEqualGUID(&IID_IUnknown
, riid
)||
112 IsEqualGUID(&IID_IStream
, riid
))
114 *ppvObject
= (IStream
*)This
;
118 * Check that we obtained an interface.
121 return E_NOINTERFACE
;
124 * Query Interface always increases the reference count by one when it is
127 IStream_AddRef(iface
);
133 * This implements the IUnknown method AddRef for this
136 static ULONG WINAPI
StgStreamImpl_AddRef(
139 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
140 return InterlockedIncrement(&This
->ref
);
144 * This implements the IUnknown method Release for this
147 static ULONG WINAPI
StgStreamImpl_Release(
150 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
154 ref
= InterlockedDecrement(&This
->ref
);
157 * If the reference count goes down to 0, perform suicide.
161 StgStreamImpl_Destroy(This
);
168 * This method will open the block chain pointed by the property
169 * that describes the stream.
170 * If the stream's size is null, no chain is opened.
172 static void StgStreamImpl_OpenBlockChain(
175 StgProperty curProperty
;
179 * Make sure no old object is left over.
181 if (This
->smallBlockChain
!= 0)
183 SmallBlockChainStream_Destroy(This
->smallBlockChain
);
184 This
->smallBlockChain
= 0;
187 if (This
->bigBlockChain
!= 0)
189 BlockChainStream_Destroy(This
->bigBlockChain
);
190 This
->bigBlockChain
= 0;
194 * Read the information from the property.
196 readSucessful
= StorageImpl_ReadProperty(This
->parentStorage
->ancestorStorage
,
202 This
->streamSize
= curProperty
.size
;
205 * This code supports only streams that are <32 bits in size.
207 assert(This
->streamSize
.u
.HighPart
== 0);
209 if(curProperty
.startingBlock
== BLOCK_END_OF_CHAIN
)
211 assert( (This
->streamSize
.u
.HighPart
== 0) && (This
->streamSize
.u
.LowPart
== 0) );
215 if ( (This
->streamSize
.u
.HighPart
== 0) &&
216 (This
->streamSize
.u
.LowPart
< LIMIT_TO_USE_SMALL_BLOCK
) )
218 This
->smallBlockChain
= SmallBlockChainStream_Construct(
219 This
->parentStorage
->ancestorStorage
,
220 This
->ownerProperty
);
224 This
->bigBlockChain
= BlockChainStream_Construct(
225 This
->parentStorage
->ancestorStorage
,
227 This
->ownerProperty
);
234 * This method is part of the ISequentialStream interface.
236 * It reads a block of information from the stream at the current
237 * position. It then moves the current position at the end of the
240 * See the documentation of ISequentialStream for more info.
242 static HRESULT WINAPI
StgStreamImpl_Read(
244 void* pv
, /* [length_is][size_is][out] */
246 ULONG
* pcbRead
) /* [out] */
248 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
250 ULONG bytesReadBuffer
;
251 ULONG bytesToReadFromBuffer
;
254 TRACE("(%p, %p, %ld, %p)\n",
255 iface
, pv
, cb
, pcbRead
);
258 * If the caller is not interested in the number of bytes read,
259 * we use another buffer to avoid "if" statements in the code.
262 pcbRead
= &bytesReadBuffer
;
265 * Using the known size of the stream, calculate the number of bytes
266 * to read from the block chain
268 bytesToReadFromBuffer
= min( This
->streamSize
.u
.LowPart
- This
->currentPosition
.u
.LowPart
, cb
);
271 * Depending on the type of chain that was opened when the stream was constructed,
272 * we delegate the work to the method that reads the block chains.
274 if (This
->smallBlockChain
!=0)
276 res
= SmallBlockChainStream_ReadAt(This
->smallBlockChain
,
277 This
->currentPosition
,
278 bytesToReadFromBuffer
,
283 else if (This
->bigBlockChain
!=0)
285 BOOL success
= BlockChainStream_ReadAt(This
->bigBlockChain
,
286 This
->currentPosition
,
287 bytesToReadFromBuffer
,
293 res
= STG_E_READFAULT
;
298 * Small and big block chains are both NULL. This case will happen
299 * when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
310 * We should always be able to read the proper amount of data from the
313 assert(bytesToReadFromBuffer
== *pcbRead
);
316 * Advance the pointer for the number of positions read.
318 This
->currentPosition
.u
.LowPart
+= *pcbRead
;
322 TRACE("<-- %08lx\n", res
);
327 * This method is part of the ISequentialStream interface.
329 * It writes a block of information to the stream at the current
330 * position. It then moves the current position at the end of the
331 * written block. If the stream is too small to fit the block,
332 * the stream is grown to fit.
334 * See the documentation of ISequentialStream for more info.
336 static HRESULT WINAPI
StgStreamImpl_Write(
338 const void* pv
, /* [size_is][in] */
340 ULONG
* pcbWritten
) /* [out] */
342 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
344 ULARGE_INTEGER newSize
;
345 ULONG bytesWritten
= 0;
347 TRACE("(%p, %p, %ld, %p)\n",
348 iface
, pv
, cb
, pcbWritten
);
351 * Do we have permission to write to this stream?
353 switch(STGM_ACCESS_MODE(This
->grfMode
))
359 return STG_E_ACCESSDENIED
;
363 return STG_E_INVALIDPOINTER
;
366 * If the caller is not interested in the number of bytes written,
367 * we use another buffer to avoid "if" statements in the code.
370 pcbWritten
= &bytesWritten
;
373 * Initialize the out parameter
383 newSize
.u
.HighPart
= 0;
384 newSize
.u
.LowPart
= This
->currentPosition
.u
.LowPart
+ cb
;
388 * Verify if we need to grow the stream
390 if (newSize
.u
.LowPart
> This
->streamSize
.u
.LowPart
)
393 IStream_SetSize(iface
, newSize
);
397 * Depending on the type of chain that was opened when the stream was constructed,
398 * we delegate the work to the method that readwrites to the block chains.
400 if (This
->smallBlockChain
!=0)
402 SmallBlockChainStream_WriteAt(This
->smallBlockChain
,
403 This
->currentPosition
,
409 else if (This
->bigBlockChain
!=0)
411 BlockChainStream_WriteAt(This
->bigBlockChain
,
412 This
->currentPosition
,
421 * Advance the position pointer for the number of positions written.
423 This
->currentPosition
.u
.LowPart
+= *pcbWritten
;
429 * This method is part of the IStream interface.
431 * It will move the current stream pointer according to the parameters
434 * See the documentation of IStream for more info.
436 static HRESULT WINAPI
StgStreamImpl_Seek(
438 LARGE_INTEGER dlibMove
, /* [in] */
439 DWORD dwOrigin
, /* [in] */
440 ULARGE_INTEGER
* plibNewPosition
) /* [out] */
442 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
444 ULARGE_INTEGER newPosition
;
446 TRACE("(%p, %ld, %ld, %p)\n",
447 iface
, dlibMove
.u
.LowPart
, dwOrigin
, plibNewPosition
);
450 * The caller is allowed to pass in NULL as the new position return value.
451 * If it happens, we assign it to a dynamic variable to avoid special cases
454 if (plibNewPosition
== 0)
456 plibNewPosition
= &newPosition
;
460 * The file pointer is moved depending on the given "function"
465 case STREAM_SEEK_SET
:
466 plibNewPosition
->u
.HighPart
= 0;
467 plibNewPosition
->u
.LowPart
= 0;
469 case STREAM_SEEK_CUR
:
470 *plibNewPosition
= This
->currentPosition
;
472 case STREAM_SEEK_END
:
473 *plibNewPosition
= This
->streamSize
;
476 return STG_E_INVALIDFUNCTION
;
479 plibNewPosition
->QuadPart
= RtlLargeIntegerAdd( plibNewPosition
->QuadPart
, dlibMove
.QuadPart
);
482 * tell the caller what we calculated
484 This
->currentPosition
= *plibNewPosition
;
490 * This method is part of the IStream interface.
492 * It will change the size of a stream.
494 * TODO: Switch from small blocks to big blocks and vice versa.
496 * See the documentation of IStream for more info.
498 static HRESULT WINAPI
StgStreamImpl_SetSize(
500 ULARGE_INTEGER libNewSize
) /* [in] */
502 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
504 StgProperty curProperty
;
507 TRACE("(%p, %ld)\n", iface
, libNewSize
.u
.LowPart
);
512 if (libNewSize
.u
.HighPart
!= 0)
513 return STG_E_INVALIDFUNCTION
;
516 * Do we have permission?
518 if (!(This
->grfMode
& (STGM_WRITE
| STGM_READWRITE
)))
519 return STG_E_ACCESSDENIED
;
521 if (This
->streamSize
.u
.LowPart
== libNewSize
.u
.LowPart
)
525 * This will happen if we're creating a stream
527 if ((This
->smallBlockChain
== 0) && (This
->bigBlockChain
== 0))
529 if (libNewSize
.u
.LowPart
< LIMIT_TO_USE_SMALL_BLOCK
)
531 This
->smallBlockChain
= SmallBlockChainStream_Construct(
532 This
->parentStorage
->ancestorStorage
,
533 This
->ownerProperty
);
537 This
->bigBlockChain
= BlockChainStream_Construct(
538 This
->parentStorage
->ancestorStorage
,
540 This
->ownerProperty
);
545 * Read this stream's property to see if it's small blocks or big blocks
547 Success
= StorageImpl_ReadProperty(This
->parentStorage
->ancestorStorage
,
551 * Determine if we have to switch from small to big blocks or vice versa
553 if ( (This
->smallBlockChain
!=0) &&
554 (curProperty
.size
.u
.LowPart
< LIMIT_TO_USE_SMALL_BLOCK
) )
556 if (libNewSize
.u
.LowPart
>= LIMIT_TO_USE_SMALL_BLOCK
)
559 * Transform the small block chain into a big block chain
561 This
->bigBlockChain
= Storage32Impl_SmallBlocksToBigBlocks(
562 This
->parentStorage
->ancestorStorage
,
563 &This
->smallBlockChain
);
567 if (This
->smallBlockChain
!=0)
569 Success
= SmallBlockChainStream_SetSize(This
->smallBlockChain
, libNewSize
);
573 Success
= BlockChainStream_SetSize(This
->bigBlockChain
, libNewSize
);
577 * Write the new information about this stream to the property
579 Success
= StorageImpl_ReadProperty(This
->parentStorage
->ancestorStorage
,
583 curProperty
.size
.u
.HighPart
= libNewSize
.u
.HighPart
;
584 curProperty
.size
.u
.LowPart
= libNewSize
.u
.LowPart
;
588 StorageImpl_WriteProperty(This
->parentStorage
->ancestorStorage
,
593 This
->streamSize
= libNewSize
;
599 * This method is part of the IStream interface.
601 * It will copy the 'cb' Bytes to 'pstm' IStream.
603 * See the documentation of IStream for more info.
605 static HRESULT WINAPI
StgStreamImpl_CopyTo(
607 IStream
* pstm
, /* [unique][in] */
608 ULARGE_INTEGER cb
, /* [in] */
609 ULARGE_INTEGER
* pcbRead
, /* [out] */
610 ULARGE_INTEGER
* pcbWritten
) /* [out] */
614 ULONG bytesRead
, bytesWritten
, copySize
;
615 ULARGE_INTEGER totalBytesRead
;
616 ULARGE_INTEGER totalBytesWritten
;
618 TRACE("(%p, %p, %ld, %p, %p)\n",
619 iface
, pstm
, cb
.u
.LowPart
, pcbRead
, pcbWritten
);
625 return STG_E_INVALIDPOINTER
;
627 totalBytesRead
.u
.LowPart
= totalBytesRead
.u
.HighPart
= 0;
628 totalBytesWritten
.u
.LowPart
= totalBytesWritten
.u
.HighPart
= 0;
631 * use stack to store data temporarily
632 * there is surely a more performant way of doing it, for now this basic
633 * implementation will do the job
635 while ( cb
.u
.LowPart
> 0 )
637 if ( cb
.u
.LowPart
>= 128 )
640 copySize
= cb
.u
.LowPart
;
642 IStream_Read(iface
, tmpBuffer
, copySize
, &bytesRead
);
644 totalBytesRead
.u
.LowPart
+= bytesRead
;
646 IStream_Write(pstm
, tmpBuffer
, bytesRead
, &bytesWritten
);
648 totalBytesWritten
.u
.LowPart
+= bytesWritten
;
651 * Check that read & write operations were successful
653 if (bytesRead
!= bytesWritten
)
655 hr
= STG_E_MEDIUMFULL
;
659 if (bytesRead
!=copySize
)
662 cb
.u
.LowPart
-= bytesRead
;
666 * Update number of bytes read and written
670 pcbRead
->u
.LowPart
= totalBytesRead
.u
.LowPart
;
671 pcbRead
->u
.HighPart
= totalBytesRead
.u
.HighPart
;
676 pcbWritten
->u
.LowPart
= totalBytesWritten
.u
.LowPart
;
677 pcbWritten
->u
.HighPart
= totalBytesWritten
.u
.HighPart
;
683 * This method is part of the IStream interface.
685 * For streams contained in structured storages, this method
686 * does nothing. This is what the documentation tells us.
688 * See the documentation of IStream for more info.
690 static HRESULT WINAPI
StgStreamImpl_Commit(
692 DWORD grfCommitFlags
) /* [in] */
698 * This method is part of the IStream interface.
700 * For streams contained in structured storages, this method
701 * does nothing. This is what the documentation tells us.
703 * See the documentation of IStream for more info.
705 static HRESULT WINAPI
StgStreamImpl_Revert(
711 static HRESULT WINAPI
StgStreamImpl_LockRegion(
713 ULARGE_INTEGER libOffset
, /* [in] */
714 ULARGE_INTEGER cb
, /* [in] */
715 DWORD dwLockType
) /* [in] */
717 FIXME("not implemented!\n");
721 static HRESULT WINAPI
StgStreamImpl_UnlockRegion(
723 ULARGE_INTEGER libOffset
, /* [in] */
724 ULARGE_INTEGER cb
, /* [in] */
725 DWORD dwLockType
) /* [in] */
727 FIXME("not implemented!\n");
732 * This method is part of the IStream interface.
734 * This method returns information about the current
737 * See the documentation of IStream for more info.
739 static HRESULT WINAPI
StgStreamImpl_Stat(
741 STATSTG
* pstatstg
, /* [out] */
742 DWORD grfStatFlag
) /* [in] */
744 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
746 StgProperty curProperty
;
750 * Read the information from the property.
752 readSucessful
= StorageImpl_ReadProperty(This
->parentStorage
->ancestorStorage
,
758 StorageUtl_CopyPropertyToSTATSTG(pstatstg
,
762 pstatstg
->grfMode
= This
->grfMode
;
771 * This method is part of the IStream interface.
773 * This method returns a clone of the interface that allows for
774 * another seek pointer
776 * See the documentation of IStream for more info.
778 * I am not totally sure what I am doing here but I presume that this
779 * should be basically as simple as creating a new stream with the same
780 * parent etc and positioning its seek cursor.
782 static HRESULT WINAPI
StgStreamImpl_Clone(
784 IStream
** ppstm
) /* [out] */
786 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
788 StgStreamImpl
* new_stream
;
789 LARGE_INTEGER seek_pos
;
795 return STG_E_INVALIDPOINTER
;
797 new_stream
= StgStreamImpl_Construct (This
->parentStorage
, This
->grfMode
, This
->ownerProperty
);
800 return STG_E_INSUFFICIENTMEMORY
; /* Currently the only reason for new_stream=0 */
802 *ppstm
= (IStream
*) new_stream
;
803 seek_pos
.QuadPart
= This
->currentPosition
.QuadPart
;
805 hres
=StgStreamImpl_Seek (*ppstm
, seek_pos
, STREAM_SEEK_SET
, NULL
);
807 assert (SUCCEEDED(hres
));
813 * Virtual function table for the StgStreamImpl class.
815 static const IStreamVtbl StgStreamImpl_Vtbl
=
817 StgStreamImpl_QueryInterface
,
818 StgStreamImpl_AddRef
,
819 StgStreamImpl_Release
,
823 StgStreamImpl_SetSize
,
824 StgStreamImpl_CopyTo
,
825 StgStreamImpl_Commit
,
826 StgStreamImpl_Revert
,
827 StgStreamImpl_LockRegion
,
828 StgStreamImpl_UnlockRegion
,
833 /******************************************************************************
834 ** StgStreamImpl implementation
838 * This is the constructor for the StgStreamImpl class.
841 * parentStorage - Pointer to the storage that contains the stream to open
842 * ownerProperty - Index of the property that points to this stream.
844 StgStreamImpl
* StgStreamImpl_Construct(
845 StorageBaseImpl
* parentStorage
,
849 StgStreamImpl
* newStream
;
851 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl
));
856 * Set-up the virtual function table and reference count.
858 newStream
->lpVtbl
= &StgStreamImpl_Vtbl
;
862 * We want to nail-down the reference to the storage in case the
863 * stream out-lives the storage in the client application.
865 newStream
->parentStorage
= parentStorage
;
866 IStorage_AddRef((IStorage
*)newStream
->parentStorage
);
868 newStream
->grfMode
= grfMode
;
869 newStream
->ownerProperty
= ownerProperty
;
872 * Start the stream at the beginning.
874 newStream
->currentPosition
.u
.HighPart
= 0;
875 newStream
->currentPosition
.u
.LowPart
= 0;
878 * Initialize the rest of the data.
880 newStream
->streamSize
.u
.HighPart
= 0;
881 newStream
->streamSize
.u
.LowPart
= 0;
882 newStream
->bigBlockChain
= 0;
883 newStream
->smallBlockChain
= 0;
886 * Read the size from the property and determine if the blocks forming
887 * this stream are large or small.
889 StgStreamImpl_OpenBlockChain(newStream
);