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
;
252 HRESULT res
= S_FALSE
;
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 SmallBlockChainStream_ReadAt(This
->smallBlockChain
,
277 This
->currentPosition
,
278 bytesToReadFromBuffer
,
283 else if (This
->bigBlockChain
!=0)
285 BlockChainStream_ReadAt(This
->bigBlockChain
,
286 This
->currentPosition
,
287 bytesToReadFromBuffer
,
294 * Small and big block chains are both NULL. This case will happen
295 * when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
304 * We should always be able to read the proper amount of data from the
307 assert(bytesToReadFromBuffer
== *pcbRead
);
310 * Advance the pointer for the number of positions read.
312 This
->currentPosition
.u
.LowPart
+= *pcbRead
;
316 WARN("read %ld instead of the required %ld bytes !\n", *pcbRead
, cb
);
318 * this used to return S_FALSE, however MSDN docu says that an app should
319 * be prepared to handle error in case of stream end reached, as *some*
320 * implementations *might* return an error (IOW: most do *not*).
321 * As some program fails on returning S_FALSE, I better use S_OK here.
329 TRACE("<-- %08lx\n", res
);
334 * This method is part of the ISequentialStream interface.
336 * It writes a block of information to the stream at the current
337 * position. It then moves the current position at the end of the
338 * written block. If the stream is too small to fit the block,
339 * the stream is grown to fit.
341 * See the documentation of ISequentialStream for more info.
343 static HRESULT WINAPI
StgStreamImpl_Write(
345 const void* pv
, /* [size_is][in] */
347 ULONG
* pcbWritten
) /* [out] */
349 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
351 ULARGE_INTEGER newSize
;
352 ULONG bytesWritten
= 0;
354 TRACE("(%p, %p, %ld, %p)\n",
355 iface
, pv
, cb
, pcbWritten
);
358 * Do we have permission to write to this stream?
360 switch(STGM_ACCESS_MODE(This
->grfMode
))
366 return STG_E_ACCESSDENIED
;
370 return STG_E_INVALIDPOINTER
;
373 * If the caller is not interested in the number of bytes written,
374 * we use another buffer to avoid "if" statements in the code.
377 pcbWritten
= &bytesWritten
;
380 * Initialize the out parameter
390 newSize
.u
.HighPart
= 0;
391 newSize
.u
.LowPart
= This
->currentPosition
.u
.LowPart
+ cb
;
395 * Verify if we need to grow the stream
397 if (newSize
.u
.LowPart
> This
->streamSize
.u
.LowPart
)
400 IStream_SetSize(iface
, newSize
);
404 * Depending on the type of chain that was opened when the stream was constructed,
405 * we delegate the work to the method that readwrites to the block chains.
407 if (This
->smallBlockChain
!=0)
409 SmallBlockChainStream_WriteAt(This
->smallBlockChain
,
410 This
->currentPosition
,
416 else if (This
->bigBlockChain
!=0)
418 BlockChainStream_WriteAt(This
->bigBlockChain
,
419 This
->currentPosition
,
428 * Advance the position pointer for the number of positions written.
430 This
->currentPosition
.u
.LowPart
+= *pcbWritten
;
436 * This method is part of the IStream interface.
438 * It will move the current stream pointer according to the parameters
441 * See the documentation of IStream for more info.
443 static HRESULT WINAPI
StgStreamImpl_Seek(
445 LARGE_INTEGER dlibMove
, /* [in] */
446 DWORD dwOrigin
, /* [in] */
447 ULARGE_INTEGER
* plibNewPosition
) /* [out] */
449 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
451 ULARGE_INTEGER newPosition
;
453 TRACE("(%p, %ld, %ld, %p)\n",
454 iface
, dlibMove
.u
.LowPart
, dwOrigin
, plibNewPosition
);
457 * The caller is allowed to pass in NULL as the new position return value.
458 * If it happens, we assign it to a dynamic variable to avoid special cases
461 if (plibNewPosition
== 0)
463 plibNewPosition
= &newPosition
;
467 * The file pointer is moved depending on the given "function"
472 case STREAM_SEEK_SET
:
473 plibNewPosition
->u
.HighPart
= 0;
474 plibNewPosition
->u
.LowPart
= 0;
476 case STREAM_SEEK_CUR
:
477 *plibNewPosition
= This
->currentPosition
;
479 case STREAM_SEEK_END
:
480 *plibNewPosition
= This
->streamSize
;
483 return STG_E_INVALIDFUNCTION
;
486 plibNewPosition
->QuadPart
= RtlLargeIntegerAdd( plibNewPosition
->QuadPart
, dlibMove
.QuadPart
);
489 * tell the caller what we calculated
491 This
->currentPosition
= *plibNewPosition
;
497 * This method is part of the IStream interface.
499 * It will change the size of a stream.
501 * TODO: Switch from small blocks to big blocks and vice versa.
503 * See the documentation of IStream for more info.
505 static HRESULT WINAPI
StgStreamImpl_SetSize(
507 ULARGE_INTEGER libNewSize
) /* [in] */
509 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
511 StgProperty curProperty
;
514 TRACE("(%p, %ld)\n", iface
, libNewSize
.u
.LowPart
);
519 if (libNewSize
.u
.HighPart
!= 0)
520 return STG_E_INVALIDFUNCTION
;
523 * Do we have permission?
525 if (!(This
->grfMode
& (STGM_WRITE
| STGM_READWRITE
)))
526 return STG_E_ACCESSDENIED
;
528 if (This
->streamSize
.u
.LowPart
== libNewSize
.u
.LowPart
)
532 * This will happen if we're creating a stream
534 if ((This
->smallBlockChain
== 0) && (This
->bigBlockChain
== 0))
536 if (libNewSize
.u
.LowPart
< LIMIT_TO_USE_SMALL_BLOCK
)
538 This
->smallBlockChain
= SmallBlockChainStream_Construct(
539 This
->parentStorage
->ancestorStorage
,
540 This
->ownerProperty
);
544 This
->bigBlockChain
= BlockChainStream_Construct(
545 This
->parentStorage
->ancestorStorage
,
547 This
->ownerProperty
);
552 * Read this stream's property to see if it's small blocks or big blocks
554 Success
= StorageImpl_ReadProperty(This
->parentStorage
->ancestorStorage
,
558 * Determine if we have to switch from small to big blocks or vice versa
560 if ( (This
->smallBlockChain
!=0) &&
561 (curProperty
.size
.u
.LowPart
< LIMIT_TO_USE_SMALL_BLOCK
) )
563 if (libNewSize
.u
.LowPart
>= LIMIT_TO_USE_SMALL_BLOCK
)
566 * Transform the small block chain into a big block chain
568 This
->bigBlockChain
= Storage32Impl_SmallBlocksToBigBlocks(
569 This
->parentStorage
->ancestorStorage
,
570 &This
->smallBlockChain
);
574 if (This
->smallBlockChain
!=0)
576 Success
= SmallBlockChainStream_SetSize(This
->smallBlockChain
, libNewSize
);
580 Success
= BlockChainStream_SetSize(This
->bigBlockChain
, libNewSize
);
584 * Write the new information about this stream to the property
586 Success
= StorageImpl_ReadProperty(This
->parentStorage
->ancestorStorage
,
590 curProperty
.size
.u
.HighPart
= libNewSize
.u
.HighPart
;
591 curProperty
.size
.u
.LowPart
= libNewSize
.u
.LowPart
;
595 StorageImpl_WriteProperty(This
->parentStorage
->ancestorStorage
,
600 This
->streamSize
= libNewSize
;
606 * This method is part of the IStream interface.
608 * It will copy the 'cb' Bytes to 'pstm' IStream.
610 * See the documentation of IStream for more info.
612 static HRESULT WINAPI
StgStreamImpl_CopyTo(
614 IStream
* pstm
, /* [unique][in] */
615 ULARGE_INTEGER cb
, /* [in] */
616 ULARGE_INTEGER
* pcbRead
, /* [out] */
617 ULARGE_INTEGER
* pcbWritten
) /* [out] */
621 ULONG bytesRead
, bytesWritten
, copySize
;
622 ULARGE_INTEGER totalBytesRead
;
623 ULARGE_INTEGER totalBytesWritten
;
625 TRACE("(%p, %p, %ld, %p, %p)\n",
626 iface
, pstm
, cb
.u
.LowPart
, pcbRead
, pcbWritten
);
632 return STG_E_INVALIDPOINTER
;
634 totalBytesRead
.u
.LowPart
= totalBytesRead
.u
.HighPart
= 0;
635 totalBytesWritten
.u
.LowPart
= totalBytesWritten
.u
.HighPart
= 0;
638 * use stack to store data temporarily
639 * there is surely a more performant way of doing it, for now this basic
640 * implementation will do the job
642 while ( cb
.u
.LowPart
> 0 )
644 if ( cb
.u
.LowPart
>= 128 )
647 copySize
= cb
.u
.LowPart
;
649 IStream_Read(iface
, tmpBuffer
, copySize
, &bytesRead
);
651 totalBytesRead
.u
.LowPart
+= bytesRead
;
653 IStream_Write(pstm
, tmpBuffer
, bytesRead
, &bytesWritten
);
655 totalBytesWritten
.u
.LowPart
+= bytesWritten
;
658 * Check that read & write operations were successful
660 if (bytesRead
!= bytesWritten
)
662 hr
= STG_E_MEDIUMFULL
;
666 if (bytesRead
!=copySize
)
669 cb
.u
.LowPart
-= bytesRead
;
673 * Update number of bytes read and written
677 pcbRead
->u
.LowPart
= totalBytesRead
.u
.LowPart
;
678 pcbRead
->u
.HighPart
= totalBytesRead
.u
.HighPart
;
683 pcbWritten
->u
.LowPart
= totalBytesWritten
.u
.LowPart
;
684 pcbWritten
->u
.HighPart
= totalBytesWritten
.u
.HighPart
;
690 * This method is part of the IStream interface.
692 * For streams contained in structured storages, this method
693 * does nothing. This is what the documentation tells us.
695 * See the documentation of IStream for more info.
697 static HRESULT WINAPI
StgStreamImpl_Commit(
699 DWORD grfCommitFlags
) /* [in] */
705 * This method is part of the IStream interface.
707 * For streams contained in structured storages, this method
708 * does nothing. This is what the documentation tells us.
710 * See the documentation of IStream for more info.
712 static HRESULT WINAPI
StgStreamImpl_Revert(
718 static HRESULT WINAPI
StgStreamImpl_LockRegion(
720 ULARGE_INTEGER libOffset
, /* [in] */
721 ULARGE_INTEGER cb
, /* [in] */
722 DWORD dwLockType
) /* [in] */
724 FIXME("not implemented!\n");
728 static HRESULT WINAPI
StgStreamImpl_UnlockRegion(
730 ULARGE_INTEGER libOffset
, /* [in] */
731 ULARGE_INTEGER cb
, /* [in] */
732 DWORD dwLockType
) /* [in] */
734 FIXME("not implemented!\n");
739 * This method is part of the IStream interface.
741 * This method returns information about the current
744 * See the documentation of IStream for more info.
746 static HRESULT WINAPI
StgStreamImpl_Stat(
748 STATSTG
* pstatstg
, /* [out] */
749 DWORD grfStatFlag
) /* [in] */
751 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
753 StgProperty curProperty
;
757 * Read the information from the property.
759 readSucessful
= StorageImpl_ReadProperty(This
->parentStorage
->ancestorStorage
,
765 StorageUtl_CopyPropertyToSTATSTG(pstatstg
,
769 pstatstg
->grfMode
= This
->grfMode
;
778 * This method is part of the IStream interface.
780 * This method returns a clone of the interface that allows for
781 * another seek pointer
783 * See the documentation of IStream for more info.
785 * I am not totally sure what I am doing here but I presume that this
786 * should be basically as simple as creating a new stream with the same
787 * parent etc and positioning its seek cursor.
789 static HRESULT WINAPI
StgStreamImpl_Clone(
791 IStream
** ppstm
) /* [out] */
793 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
795 StgStreamImpl
* new_stream
;
796 LARGE_INTEGER seek_pos
;
802 return STG_E_INVALIDPOINTER
;
804 new_stream
= StgStreamImpl_Construct (This
->parentStorage
, This
->grfMode
, This
->ownerProperty
);
807 return STG_E_INSUFFICIENTMEMORY
; /* Currently the only reason for new_stream=0 */
809 *ppstm
= (IStream
*) new_stream
;
810 seek_pos
.QuadPart
= This
->currentPosition
.QuadPart
;
812 hres
=StgStreamImpl_Seek (*ppstm
, seek_pos
, STREAM_SEEK_SET
, NULL
);
814 assert (SUCCEEDED(hres
));
820 * Virtual function table for the StgStreamImpl class.
822 static const IStreamVtbl StgStreamImpl_Vtbl
=
824 StgStreamImpl_QueryInterface
,
825 StgStreamImpl_AddRef
,
826 StgStreamImpl_Release
,
830 StgStreamImpl_SetSize
,
831 StgStreamImpl_CopyTo
,
832 StgStreamImpl_Commit
,
833 StgStreamImpl_Revert
,
834 StgStreamImpl_LockRegion
,
835 StgStreamImpl_UnlockRegion
,
840 /******************************************************************************
841 ** StgStreamImpl implementation
845 * This is the constructor for the StgStreamImpl class.
848 * parentStorage - Pointer to the storage that contains the stream to open
849 * ownerProperty - Index of the property that points to this stream.
851 StgStreamImpl
* StgStreamImpl_Construct(
852 StorageBaseImpl
* parentStorage
,
856 StgStreamImpl
* newStream
;
858 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl
));
863 * Set-up the virtual function table and reference count.
865 newStream
->lpVtbl
= &StgStreamImpl_Vtbl
;
869 * We want to nail-down the reference to the storage in case the
870 * stream out-lives the storage in the client application.
872 newStream
->parentStorage
= parentStorage
;
873 IStorage_AddRef((IStorage
*)newStream
->parentStorage
);
875 newStream
->grfMode
= grfMode
;
876 newStream
->ownerProperty
= ownerProperty
;
879 * Start the stream at the beginning.
881 newStream
->currentPosition
.u
.HighPart
= 0;
882 newStream
->currentPosition
.u
.LowPart
= 0;
885 * Initialize the rest of the data.
887 newStream
->streamSize
.u
.HighPart
= 0;
888 newStream
->streamSize
.u
.LowPart
= 0;
889 newStream
->bigBlockChain
= 0;
890 newStream
->smallBlockChain
= 0;
893 * Read the size from the property and determine if the blocks forming
894 * this stream are large or small.
896 StgStreamImpl_OpenBlockChain(newStream
);