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
19 #include "wine/obj_storage.h"
21 #include "storage32.h"
23 DEFAULT_DEBUG_CHANNEL(ole
)
27 * Virtual function table for the StgStreamImpl class.
29 static ICOM_VTABLE(IStream
) StgStreamImpl_Vtbl
=
31 StgStreamImpl_QueryInterface
,
33 StgStreamImpl_Release
,
37 StgStreamImpl_SetSize
,
41 StgStreamImpl_LockRegion
,
42 StgStreamImpl_UnlockRegion
,
47 /******************************************************************************
48 ** StgStreamImpl implementation
52 * This is the constructor for the StgStreamImpl class.
55 * parentStorage - Pointer to the storage that contains the stream to open
56 * ownerProperty - Index of the property that points to this stream.
58 StgStreamImpl
* StgStreamImpl_Construct(
59 StorageBaseImpl
* parentStorage
,
62 StgStreamImpl
* newStream
;
64 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl
));
69 * Set-up the virtual function table and reference count.
71 newStream
->lpvtbl
= &StgStreamImpl_Vtbl
;
75 * We want to nail-down the reference to the storage in case the
76 * stream out-lives the storage in the client application.
78 newStream
->parentStorage
= parentStorage
;
79 IStorage_AddRef((IStorage
*)newStream
->parentStorage
);
81 newStream
->ownerProperty
= ownerProperty
;
84 * Start the stream at the begining.
86 newStream
->currentPosition
.HighPart
= 0;
87 newStream
->currentPosition
.LowPart
= 0;
90 * Initialize the rest of the data.
92 newStream
->streamSize
.HighPart
= 0;
93 newStream
->streamSize
.LowPart
= 0;
94 newStream
->bigBlockChain
= 0;
95 newStream
->smallBlockChain
= 0;
98 * Read the size from the property and determine if the blocks forming
99 * this stream are large or small.
101 StgStreamImpl_OpenBlockChain(newStream
);
108 * This is the destructor of the StgStreamImpl class.
110 * This method will clean-up all the resources used-up by the given StgStreamImpl
111 * class. The pointer passed-in to this function will be freed and will not
114 void StgStreamImpl_Destroy(StgStreamImpl
* This
)
117 * Release the reference we are holding on the parent storage.
119 IStorage_Release((IStorage
*)This
->parentStorage
);
120 This
->parentStorage
= 0;
123 * Make sure we clean-up the block chain stream objects that we were using.
125 if (This
->bigBlockChain
!= 0)
127 BlockChainStream_Destroy(This
->bigBlockChain
);
128 This
->bigBlockChain
= 0;
131 if (This
->smallBlockChain
!= 0)
133 SmallBlockChainStream_Destroy(This
->smallBlockChain
);
134 This
->smallBlockChain
= 0;
138 * Finally, free the memory used-up by the class.
140 HeapFree(GetProcessHeap(), 0, This
);
144 * This implements the IUnknown method QueryInterface for this
147 HRESULT WINAPI
StgStreamImpl_QueryInterface(
149 REFIID riid
, /* [in] */
150 void** ppvObject
) /* [iid_is][out] */
152 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
155 * Perform a sanity check on the parameters.
161 * Initialize the return parameter.
166 * Compare the riid with the interface IDs implemented by this object.
168 if (memcmp(&IID_IUnknown
, riid
, sizeof(IID_IUnknown
)) == 0)
170 *ppvObject
= (IStream
*)This
;
172 else if (memcmp(&IID_IStorage
, riid
, sizeof(IID_IStream
)) == 0)
174 *ppvObject
= (IStream
*)This
;
178 * Check that we obtained an interface.
181 return E_NOINTERFACE
;
184 * Query Interface always increases the reference count by one when it is
187 StgStreamImpl_AddRef(iface
);
193 * This implements the IUnknown method AddRef for this
196 ULONG WINAPI
StgStreamImpl_AddRef(
199 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
207 * This implements the IUnknown method Release for this
210 ULONG WINAPI
StgStreamImpl_Release(
213 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
222 * If the reference count goes down to 0, perform suicide.
226 StgStreamImpl_Destroy(This
);
233 * This method will open the block chain pointed by the property
234 * that describes the stream.
235 * If the stream's size is null, no chain is opened.
237 void StgStreamImpl_OpenBlockChain(
240 StgProperty curProperty
;
244 * Make sure no old object is staying behind.
246 if (This
->smallBlockChain
!= 0)
248 SmallBlockChainStream_Destroy(This
->smallBlockChain
);
249 This
->smallBlockChain
= 0;
252 if (This
->bigBlockChain
!= 0)
254 BlockChainStream_Destroy(This
->bigBlockChain
);
255 This
->bigBlockChain
= 0;
259 * Read the information from the property.
261 readSucessful
= StorageImpl_ReadProperty(This
->parentStorage
->ancestorStorage
,
267 This
->streamSize
= curProperty
.size
;
270 * This code supports only streams that are <32 bits in size.
272 assert(This
->streamSize
.HighPart
== 0);
274 if(curProperty
.startingBlock
== BLOCK_END_OF_CHAIN
)
276 assert( (This
->streamSize
.HighPart
== 0) && (This
->streamSize
.LowPart
== 0) );
280 if ( (This
->streamSize
.HighPart
== 0) &&
281 (This
->streamSize
.LowPart
< LIMIT_TO_USE_SMALL_BLOCK
) )
283 This
->smallBlockChain
= SmallBlockChainStream_Construct(
284 This
->parentStorage
->ancestorStorage
,
285 This
->ownerProperty
);
289 This
->bigBlockChain
= BlockChainStream_Construct(
290 This
->parentStorage
->ancestorStorage
,
292 This
->ownerProperty
);
299 * This method is part of the ISequentialStream interface.
301 * If reads a block of information from the stream at the current
302 * position. It then moves the current position at the end of the
305 * See the documentation of ISequentialStream for more info.
307 HRESULT WINAPI
StgStreamImpl_Read(
309 void* pv
, /* [length_is][size_is][out] */
311 ULONG
* pcbRead
) /* [out] */
313 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
315 ULONG bytesReadBuffer
;
316 ULONG bytesToReadFromBuffer
;
319 * If the caller is not interested in the nubmer of bytes read,
320 * we use another buffer to avoid "if" statements in the code.
323 pcbRead
= &bytesReadBuffer
;
326 * Using the known size of the stream, calculate the number of bytes
327 * to read from the block chain
329 bytesToReadFromBuffer
= MIN( This
->streamSize
.LowPart
- This
->currentPosition
.LowPart
, cb
);
332 * Depending on the type of chain that was opened when the stream was constructed,
333 * we delegate the work to the method that read the block chains.
335 if (This
->smallBlockChain
!=0)
337 SmallBlockChainStream_ReadAt(This
->smallBlockChain
,
338 This
->currentPosition
,
339 bytesToReadFromBuffer
,
344 else if (This
->bigBlockChain
!=0)
346 BlockChainStream_ReadAt(This
->bigBlockChain
,
347 This
->currentPosition
,
348 bytesToReadFromBuffer
,
356 * We should always be able to read the proper amount of data from the
359 assert(bytesToReadFromBuffer
== *pcbRead
);
362 * Advance the pointer for the number of positions read.
364 This
->currentPosition
.LowPart
+= *pcbRead
;
367 * The function returns S_OK if the buffer was filled completely
368 * it returns S_FALSE if the end of the stream is reached before the
378 * This method is part of the ISequentialStream interface.
380 * It writes a block of information to the stream at the current
381 * position. It then moves the current position at the end of the
382 * written block. If the stream is too small to fit the block,
383 * the stream is grown to fit.
385 * See the documentation of ISequentialStream for more info.
387 HRESULT WINAPI
StgStreamImpl_Write(
389 const void* pv
, /* [size_is][in] */
391 ULONG
* pcbWritten
) /* [out] */
393 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
395 ULARGE_INTEGER newSize
;
396 ULONG bytesWritten
= 0;
399 * If the caller is not interested in the number of bytes written,
400 * we use another buffer to avoid "if" statements in the code.
403 pcbWritten
= &bytesWritten
;
411 newSize
.HighPart
= 0;
412 newSize
.LowPart
= This
->currentPosition
.LowPart
+ cb
;
416 * Verify if we need to grow the stream
418 if (newSize
.LowPart
> This
->streamSize
.LowPart
)
421 StgStreamImpl_SetSize(iface
, newSize
);
425 * Depending on the type of chain that was opened when the stream was constructed,
426 * we delegate the work to the method that readwrites to the block chains.
428 if (This
->smallBlockChain
!=0)
430 SmallBlockChainStream_WriteAt(This
->smallBlockChain
,
431 This
->currentPosition
,
437 else if (This
->bigBlockChain
!=0)
439 BlockChainStream_WriteAt(This
->bigBlockChain
,
440 This
->currentPosition
,
449 * Advance the position pointer for the number of positions written.
451 This
->currentPosition
.LowPart
+= *pcbWritten
;
457 * This method is part of the IStream interface.
459 * It will move the current stream pointer according to the parameters
462 * See the documentation of IStream for more info.
464 HRESULT WINAPI
StgStreamImpl_Seek(
466 LARGE_INTEGER dlibMove
, /* [in] */
467 DWORD dwOrigin
, /* [in] */
468 ULARGE_INTEGER
* plibNewPosition
) /* [out] */
470 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
472 ULARGE_INTEGER newPosition
;
475 * The caller is allowed to pass in NULL as the new position return value.
476 * If it happens, we assign it to a dynamic variable to avoid special cases
479 if (plibNewPosition
== 0)
481 plibNewPosition
= &newPosition
;
485 * The file pointer is moved depending on the given "function"
490 case STREAM_SEEK_SET
:
491 plibNewPosition
->HighPart
= 0;
492 plibNewPosition
->LowPart
= 0;
494 case STREAM_SEEK_CUR
:
495 *plibNewPosition
= This
->currentPosition
;
497 case STREAM_SEEK_END
:
498 *plibNewPosition
= This
->streamSize
;
501 return STG_E_INVALIDFUNCTION
;
505 * We don't support files with offsets of 64 bits.
507 assert(dlibMove
.HighPart
== 0);
510 * Check if we end-up before the beginning of the file. That should trigger an
513 if ( (dlibMove
.LowPart
<0) && (plibNewPosition
->LowPart
< (ULONG
)(-dlibMove
.LowPart
)) )
516 * I don't know what error to send there.
522 * Move the actual file pointer
523 * If the file pointer ends-up after the end of the stream, the next Write operation will
524 * make the file larger. This is how it is documented.
526 plibNewPosition
->LowPart
+= dlibMove
.LowPart
;
527 This
->currentPosition
= *plibNewPosition
;
533 * This method is part of the IStream interface.
535 * It will change the size of a stream.
537 * TODO: Switch from small blocks to big blocks and vice versa.
539 * See the documentation of IStream for more info.
541 HRESULT WINAPI
StgStreamImpl_SetSize(
543 ULARGE_INTEGER libNewSize
) /* [in] */
545 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
547 StgProperty curProperty
;
553 if (libNewSize
.HighPart
!= 0)
554 return STG_E_INVALIDFUNCTION
;
556 if (This
->streamSize
.LowPart
== libNewSize
.LowPart
)
560 * This will happen if we're creating a stream
562 if ((This
->smallBlockChain
== 0) && (This
->bigBlockChain
== 0))
564 if (libNewSize
.LowPart
< LIMIT_TO_USE_SMALL_BLOCK
)
566 This
->smallBlockChain
= SmallBlockChainStream_Construct(
567 This
->parentStorage
->ancestorStorage
,
568 This
->ownerProperty
);
572 This
->bigBlockChain
= BlockChainStream_Construct(
573 This
->parentStorage
->ancestorStorage
,
575 This
->ownerProperty
);
580 * Read this stream's property to see if it's small blocks or big blocks
582 Success
= StorageImpl_ReadProperty(This
->parentStorage
->ancestorStorage
,
586 * Determine if we have to switch from small to big blocks or vice versa
589 if (curProperty
.size
.LowPart
< LIMIT_TO_USE_SMALL_BLOCK
)
591 if (libNewSize
.LowPart
>= LIMIT_TO_USE_SMALL_BLOCK
)
594 * Transform the small block chain into a big block chain
596 This
->bigBlockChain
= Storage32Impl_SmallBlocksToBigBlocks(
597 This
->parentStorage
->ancestorStorage
,
598 &This
->smallBlockChain
);
602 if (This
->smallBlockChain
!=0)
604 Success
= SmallBlockChainStream_SetSize(This
->smallBlockChain
, libNewSize
);
608 Success
= BlockChainStream_SetSize(This
->bigBlockChain
, libNewSize
);
612 * Write to the property the new information about this stream
614 Success
= StorageImpl_ReadProperty(This
->parentStorage
->ancestorStorage
,
618 curProperty
.size
.HighPart
= libNewSize
.HighPart
;
619 curProperty
.size
.LowPart
= libNewSize
.LowPart
;
623 StorageImpl_WriteProperty(This
->parentStorage
->ancestorStorage
,
628 This
->streamSize
= libNewSize
;
634 * This method is part of the IStream interface.
636 * It will copy the 'cb' Bytes to 'pstm' IStream.
638 * See the documentation of IStream for more info.
640 HRESULT WINAPI
StgStreamImpl_CopyTo(
642 IStream
* pstm
, /* [unique][in] */
643 ULARGE_INTEGER cb
, /* [in] */
644 ULARGE_INTEGER
* pcbRead
, /* [out] */
645 ULARGE_INTEGER
* pcbWritten
) /* [out] */
647 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
650 ULONG bytesRead
, bytesWritten
, copySize
;
651 ULARGE_INTEGER totalBytesRead
;
652 ULARGE_INTEGER totalBytesWritten
;
658 return STG_E_INVALIDPOINTER
;
660 totalBytesRead
.LowPart
= totalBytesRead
.HighPart
= 0;
661 totalBytesWritten
.LowPart
= totalBytesWritten
.HighPart
= 0;
664 * use stack to store data temporarly
665 * there is surely more performant way of doing it, for now this basic
666 * implementation will do the job
668 while ( cb
.LowPart
> 0 )
670 if ( cb
.LowPart
>= 128 )
673 copySize
= cb
.LowPart
;
675 StgStreamImpl_Read(iface
, tmpBuffer
, 128, &bytesRead
);
677 totalBytesRead
.LowPart
+= bytesRead
;
679 StgStreamImpl_Write(pstm
, tmpBuffer
, bytesRead
, &bytesWritten
);
681 totalBytesWritten
.LowPart
+= bytesWritten
;
684 * Check that read & write operations were succesfull
686 if ( (bytesRead
!= copySize
) && (bytesWritten
!= copySize
) )
688 hr
= STG_E_MEDIUMFULL
;
692 cb
.LowPart
= cb
.LowPart
- copySize
;
696 * Update number of bytes read and written
700 pcbRead
->LowPart
= totalBytesRead
.LowPart
;
701 pcbRead
->HighPart
= totalBytesRead
.HighPart
;
706 pcbWritten
->LowPart
= totalBytesWritten
.LowPart
;
707 pcbWritten
->HighPart
= totalBytesWritten
.HighPart
;
713 * This method is part of the IStream interface.
715 * For streams contained in structured storages, this method
716 * does nothing. This is what the documentation tells us.
718 * See the documentation of IStream for more info.
720 HRESULT WINAPI
StgStreamImpl_Commit(
722 DWORD grfCommitFlags
) /* [in] */
728 * This method is part of the IStream interface.
730 * For streams contained in structured storages, this method
731 * does nothing. This is what the documentation tells us.
733 * See the documentation of IStream for more info.
735 HRESULT WINAPI
StgStreamImpl_Revert(
741 HRESULT WINAPI
StgStreamImpl_LockRegion(
743 ULARGE_INTEGER libOffset
, /* [in] */
744 ULARGE_INTEGER cb
, /* [in] */
745 DWORD dwLockType
) /* [in] */
747 FIXME(ole
, "not implemented!\n");
751 HRESULT WINAPI
StgStreamImpl_UnlockRegion(
753 ULARGE_INTEGER libOffset
, /* [in] */
754 ULARGE_INTEGER cb
, /* [in] */
755 DWORD dwLockType
) /* [in] */
757 FIXME(ole
, "not implemented!\n");
762 * This method is part of the IStream interface.
764 * This method returns information about the current
767 * See the documentation of IStream for more info.
769 HRESULT WINAPI
StgStreamImpl_Stat(
771 STATSTG
* pstatstg
, /* [out] */
772 DWORD grfStatFlag
) /* [in] */
774 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
776 StgProperty curProperty
;
780 * Read the information from the property.
782 readSucessful
= StorageImpl_ReadProperty(This
->parentStorage
->ancestorStorage
,
788 StorageUtl_CopyPropertyToSTATSTG(pstatstg
,
798 HRESULT WINAPI
StgStreamImpl_Clone(
800 IStream
** ppstm
) /* [out] */
802 FIXME(ole
, "not implemented!\n");