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 "debugtools.h"
21 #include "storage32.h"
23 DEFAULT_DEBUG_CHANNEL(storage);
27 * Virtual function table for the StgStreamImpl class.
29 static ICOM_VTABLE(IStream) StgStreamImpl_Vtbl =
31 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
32 StgStreamImpl_QueryInterface,
34 StgStreamImpl_Release,
38 StgStreamImpl_SetSize,
42 StgStreamImpl_LockRegion,
43 StgStreamImpl_UnlockRegion,
48 /******************************************************************************
49 ** StgStreamImpl implementation
53 * This is the constructor for the StgStreamImpl class.
56 * parentStorage - Pointer to the storage that contains the stream to open
57 * ownerProperty - Index of the property that points to this stream.
59 StgStreamImpl* StgStreamImpl_Construct(
60 StorageBaseImpl* parentStorage,
64 StgStreamImpl* newStream;
66 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
71 * Set-up the virtual function table and reference count.
73 ICOM_VTBL(newStream) = &StgStreamImpl_Vtbl;
77 * We want to nail-down the reference to the storage in case the
78 * stream out-lives the storage in the client application.
80 newStream->parentStorage = parentStorage;
81 IStorage_AddRef((IStorage*)newStream->parentStorage);
83 newStream->grfMode = grfMode;
84 newStream->ownerProperty = ownerProperty;
87 * Start the stream at the beginning.
89 newStream->currentPosition.s.HighPart = 0;
90 newStream->currentPosition.s.LowPart = 0;
93 * Initialize the rest of the data.
95 newStream->streamSize.s.HighPart = 0;
96 newStream->streamSize.s.LowPart = 0;
97 newStream->bigBlockChain = 0;
98 newStream->smallBlockChain = 0;
101 * Read the size from the property and determine if the blocks forming
102 * this stream are large or small.
104 StgStreamImpl_OpenBlockChain(newStream);
111 * This is the destructor of the StgStreamImpl class.
113 * This method will clean-up all the resources used-up by the given StgStreamImpl
114 * class. The pointer passed-in to this function will be freed and will not
117 void StgStreamImpl_Destroy(StgStreamImpl* This)
119 TRACE("(%p)\n", This);
122 * Release the reference we are holding on the parent storage.
124 IStorage_Release((IStorage*)This->parentStorage);
125 This->parentStorage = 0;
128 * Make sure we clean-up the block chain stream objects that we were using.
130 if (This->bigBlockChain != 0)
132 BlockChainStream_Destroy(This->bigBlockChain);
133 This->bigBlockChain = 0;
136 if (This->smallBlockChain != 0)
138 SmallBlockChainStream_Destroy(This->smallBlockChain);
139 This->smallBlockChain = 0;
143 * Finally, free the memory used-up by the class.
145 HeapFree(GetProcessHeap(), 0, This);
149 * This implements the IUnknown method QueryInterface for this
152 HRESULT WINAPI StgStreamImpl_QueryInterface(
154 REFIID riid, /* [in] */
155 void** ppvObject) /* [iid_is][out] */
157 StgStreamImpl* const This=(StgStreamImpl*)iface;
160 * Perform a sanity check on the parameters.
166 * Initialize the return parameter.
171 * Compare the riid with the interface IDs implemented by this object.
173 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
175 *ppvObject = (IStream*)This;
177 else if (memcmp(&IID_IStream, riid, sizeof(IID_IStream)) == 0)
179 *ppvObject = (IStream*)This;
183 * Check that we obtained an interface.
186 return E_NOINTERFACE;
189 * Query Interface always increases the reference count by one when it is
192 StgStreamImpl_AddRef(iface);
198 * This implements the IUnknown method AddRef for this
201 ULONG WINAPI StgStreamImpl_AddRef(
204 StgStreamImpl* const This=(StgStreamImpl*)iface;
212 * This implements the IUnknown method Release for this
215 ULONG WINAPI StgStreamImpl_Release(
218 StgStreamImpl* const This=(StgStreamImpl*)iface;
227 * If the reference count goes down to 0, perform suicide.
231 StgStreamImpl_Destroy(This);
238 * This method will open the block chain pointed by the property
239 * that describes the stream.
240 * If the stream's size is null, no chain is opened.
242 void StgStreamImpl_OpenBlockChain(
245 StgProperty curProperty;
249 * Make sure no old object is left over.
251 if (This->smallBlockChain != 0)
253 SmallBlockChainStream_Destroy(This->smallBlockChain);
254 This->smallBlockChain = 0;
257 if (This->bigBlockChain != 0)
259 BlockChainStream_Destroy(This->bigBlockChain);
260 This->bigBlockChain = 0;
264 * Read the information from the property.
266 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
272 This->streamSize = curProperty.size;
275 * This code supports only streams that are <32 bits in size.
277 assert(This->streamSize.s.HighPart == 0);
279 if(curProperty.startingBlock == BLOCK_END_OF_CHAIN)
281 assert( (This->streamSize.s.HighPart == 0) && (This->streamSize.s.LowPart == 0) );
285 if ( (This->streamSize.s.HighPart == 0) &&
286 (This->streamSize.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
288 This->smallBlockChain = SmallBlockChainStream_Construct(
289 This->parentStorage->ancestorStorage,
290 This->ownerProperty);
294 This->bigBlockChain = BlockChainStream_Construct(
295 This->parentStorage->ancestorStorage,
297 This->ownerProperty);
304 * This method is part of the ISequentialStream interface.
306 * It reads a block of information from the stream at the current
307 * position. It then moves the current position at the end of the
310 * See the documentation of ISequentialStream for more info.
312 HRESULT WINAPI StgStreamImpl_Read(
314 void* pv, /* [length_is][size_is][out] */
316 ULONG* pcbRead) /* [out] */
318 StgStreamImpl* const This=(StgStreamImpl*)iface;
320 ULONG bytesReadBuffer;
321 ULONG bytesToReadFromBuffer;
322 HRESULT res = S_FALSE;
324 TRACE("(%p, %p, %ld, %p)\n",
325 iface, pv, cb, pcbRead);
328 * If the caller is not interested in the number of bytes read,
329 * we use another buffer to avoid "if" statements in the code.
332 pcbRead = &bytesReadBuffer;
335 * Using the known size of the stream, calculate the number of bytes
336 * to read from the block chain
338 bytesToReadFromBuffer = min( This->streamSize.s.LowPart - This->currentPosition.s.LowPart, cb);
341 * Depending on the type of chain that was opened when the stream was constructed,
342 * we delegate the work to the method that reads the block chains.
344 if (This->smallBlockChain!=0)
346 SmallBlockChainStream_ReadAt(This->smallBlockChain,
347 This->currentPosition,
348 bytesToReadFromBuffer,
353 else if (This->bigBlockChain!=0)
355 BlockChainStream_ReadAt(This->bigBlockChain,
356 This->currentPosition,
357 bytesToReadFromBuffer,
364 * Small and big block chains are both NULL. This case will happen
365 * when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
374 * We should always be able to read the proper amount of data from the
377 assert(bytesToReadFromBuffer == *pcbRead);
380 * Advance the pointer for the number of positions read.
382 This->currentPosition.s.LowPart += *pcbRead;
386 WARN("read %ld instead of the required %ld bytes !\n", *pcbRead, cb);
388 * this used to return S_FALSE, however MSDN docu says that an app should
389 * be prepared to handle error in case of stream end reached, as *some*
390 * implementations *might* return an error (IOW: most do *not*).
391 * As some program fails on returning S_FALSE, I better use S_OK here.
399 TRACE("<-- %08lx\n", res);
404 * This method is part of the ISequentialStream interface.
406 * It writes a block of information to the stream at the current
407 * position. It then moves the current position at the end of the
408 * written block. If the stream is too small to fit the block,
409 * the stream is grown to fit.
411 * See the documentation of ISequentialStream for more info.
413 HRESULT WINAPI StgStreamImpl_Write(
415 const void* pv, /* [size_is][in] */
417 ULONG* pcbWritten) /* [out] */
419 StgStreamImpl* const This=(StgStreamImpl*)iface;
421 ULARGE_INTEGER newSize;
422 ULONG bytesWritten = 0;
424 TRACE("(%p, %p, %ld, %p)\n",
425 iface, pv, cb, pcbWritten);
428 * Do we have permission to write to this stream?
430 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE))) {
431 return STG_E_ACCESSDENIED;
435 * If the caller is not interested in the number of bytes written,
436 * we use another buffer to avoid "if" statements in the code.
439 pcbWritten = &bytesWritten;
442 * Initialize the out parameter
452 newSize.s.HighPart = 0;
453 newSize.s.LowPart = This->currentPosition.s.LowPart + cb;
457 * Verify if we need to grow the stream
459 if (newSize.s.LowPart > This->streamSize.s.LowPart)
462 IStream_SetSize(iface, newSize);
466 * Depending on the type of chain that was opened when the stream was constructed,
467 * we delegate the work to the method that readwrites to the block chains.
469 if (This->smallBlockChain!=0)
471 SmallBlockChainStream_WriteAt(This->smallBlockChain,
472 This->currentPosition,
478 else if (This->bigBlockChain!=0)
480 BlockChainStream_WriteAt(This->bigBlockChain,
481 This->currentPosition,
490 * Advance the position pointer for the number of positions written.
492 This->currentPosition.s.LowPart += *pcbWritten;
498 * This method is part of the IStream interface.
500 * It will move the current stream pointer according to the parameters
503 * See the documentation of IStream for more info.
505 HRESULT WINAPI StgStreamImpl_Seek(
507 LARGE_INTEGER dlibMove, /* [in] */
508 DWORD dwOrigin, /* [in] */
509 ULARGE_INTEGER* plibNewPosition) /* [out] */
511 StgStreamImpl* const This=(StgStreamImpl*)iface;
513 ULARGE_INTEGER newPosition;
515 TRACE("(%p, %ld, %ld, %p)\n",
516 iface, dlibMove.s.LowPart, dwOrigin, plibNewPosition);
519 * The caller is allowed to pass in NULL as the new position return value.
520 * If it happens, we assign it to a dynamic variable to avoid special cases
523 if (plibNewPosition == 0)
525 plibNewPosition = &newPosition;
529 * The file pointer is moved depending on the given "function"
534 case STREAM_SEEK_SET:
535 plibNewPosition->s.HighPart = 0;
536 plibNewPosition->s.LowPart = 0;
538 case STREAM_SEEK_CUR:
539 *plibNewPosition = This->currentPosition;
541 case STREAM_SEEK_END:
542 *plibNewPosition = This->streamSize;
545 return STG_E_INVALIDFUNCTION;
548 plibNewPosition->QuadPart = RtlLargeIntegerAdd( plibNewPosition->QuadPart, dlibMove.QuadPart );
551 * tell the caller what we calculated
553 This->currentPosition = *plibNewPosition;
559 * This method is part of the IStream interface.
561 * It will change the size of a stream.
563 * TODO: Switch from small blocks to big blocks and vice versa.
565 * See the documentation of IStream for more info.
567 HRESULT WINAPI StgStreamImpl_SetSize(
569 ULARGE_INTEGER libNewSize) /* [in] */
571 StgStreamImpl* const This=(StgStreamImpl*)iface;
573 StgProperty curProperty;
576 TRACE("(%p, %ld)\n", iface, libNewSize.s.LowPart);
581 if (libNewSize.s.HighPart != 0)
582 return STG_E_INVALIDFUNCTION;
585 * Do we have permission?
587 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
588 return STG_E_ACCESSDENIED;
590 if (This->streamSize.s.LowPart == libNewSize.s.LowPart)
594 * This will happen if we're creating a stream
596 if ((This->smallBlockChain == 0) && (This->bigBlockChain == 0))
598 if (libNewSize.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
600 This->smallBlockChain = SmallBlockChainStream_Construct(
601 This->parentStorage->ancestorStorage,
602 This->ownerProperty);
606 This->bigBlockChain = BlockChainStream_Construct(
607 This->parentStorage->ancestorStorage,
609 This->ownerProperty);
614 * Read this stream's property to see if it's small blocks or big blocks
616 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
620 * Determine if we have to switch from small to big blocks or vice versa
622 if ( (This->smallBlockChain!=0) &&
623 (curProperty.size.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
625 if (libNewSize.s.LowPart >= LIMIT_TO_USE_SMALL_BLOCK)
628 * Transform the small block chain into a big block chain
630 This->bigBlockChain = Storage32Impl_SmallBlocksToBigBlocks(
631 This->parentStorage->ancestorStorage,
632 &This->smallBlockChain);
636 if (This->smallBlockChain!=0)
638 Success = SmallBlockChainStream_SetSize(This->smallBlockChain, libNewSize);
642 Success = BlockChainStream_SetSize(This->bigBlockChain, libNewSize);
646 * Write the new information about this stream to the property
648 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
652 curProperty.size.s.HighPart = libNewSize.s.HighPart;
653 curProperty.size.s.LowPart = libNewSize.s.LowPart;
657 StorageImpl_WriteProperty(This->parentStorage->ancestorStorage,
662 This->streamSize = libNewSize;
668 * This method is part of the IStream interface.
670 * It will copy the 'cb' Bytes to 'pstm' IStream.
672 * See the documentation of IStream for more info.
674 HRESULT WINAPI StgStreamImpl_CopyTo(
676 IStream* pstm, /* [unique][in] */
677 ULARGE_INTEGER cb, /* [in] */
678 ULARGE_INTEGER* pcbRead, /* [out] */
679 ULARGE_INTEGER* pcbWritten) /* [out] */
683 ULONG bytesRead, bytesWritten, copySize;
684 ULARGE_INTEGER totalBytesRead;
685 ULARGE_INTEGER totalBytesWritten;
687 TRACE("(%p, %p, %ld, %p, %p)\n",
688 iface, pstm, cb.s.LowPart, pcbRead, pcbWritten);
694 return STG_E_INVALIDPOINTER;
696 totalBytesRead.s.LowPart = totalBytesRead.s.HighPart = 0;
697 totalBytesWritten.s.LowPart = totalBytesWritten.s.HighPart = 0;
700 * use stack to store data temporarily
701 * there is surely a more performant way of doing it, for now this basic
702 * implementation will do the job
704 while ( cb.s.LowPart > 0 )
706 if ( cb.s.LowPart >= 128 )
709 copySize = cb.s.LowPart;
711 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
713 totalBytesRead.s.LowPart += bytesRead;
715 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
717 totalBytesWritten.s.LowPart += bytesWritten;
720 * Check that read & write operations were successful
722 if (bytesRead != bytesWritten)
724 hr = STG_E_MEDIUMFULL;
728 if (bytesRead!=copySize)
731 cb.s.LowPart -= bytesRead;
735 * Update number of bytes read and written
739 pcbRead->s.LowPart = totalBytesRead.s.LowPart;
740 pcbRead->s.HighPart = totalBytesRead.s.HighPart;
745 pcbWritten->s.LowPart = totalBytesWritten.s.LowPart;
746 pcbWritten->s.HighPart = totalBytesWritten.s.HighPart;
752 * This method is part of the IStream interface.
754 * For streams contained in structured storages, this method
755 * does nothing. This is what the documentation tells us.
757 * See the documentation of IStream for more info.
759 HRESULT WINAPI StgStreamImpl_Commit(
761 DWORD grfCommitFlags) /* [in] */
767 * This method is part of the IStream interface.
769 * For streams contained in structured storages, this method
770 * does nothing. This is what the documentation tells us.
772 * See the documentation of IStream for more info.
774 HRESULT WINAPI StgStreamImpl_Revert(
780 HRESULT WINAPI StgStreamImpl_LockRegion(
782 ULARGE_INTEGER libOffset, /* [in] */
783 ULARGE_INTEGER cb, /* [in] */
784 DWORD dwLockType) /* [in] */
786 FIXME("not implemented!\n");
790 HRESULT WINAPI StgStreamImpl_UnlockRegion(
792 ULARGE_INTEGER libOffset, /* [in] */
793 ULARGE_INTEGER cb, /* [in] */
794 DWORD dwLockType) /* [in] */
796 FIXME("not implemented!\n");
801 * This method is part of the IStream interface.
803 * This method returns information about the current
806 * See the documentation of IStream for more info.
808 HRESULT WINAPI StgStreamImpl_Stat(
810 STATSTG* pstatstg, /* [out] */
811 DWORD grfStatFlag) /* [in] */
813 StgStreamImpl* const This=(StgStreamImpl*)iface;
815 StgProperty curProperty;
819 * Read the information from the property.
821 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
827 StorageUtl_CopyPropertyToSTATSTG(pstatstg,
831 pstatstg->grfMode = This->grfMode;
840 * This method is part of the IStream interface.
842 * This method returns a clone of the interface that allows for
843 * another seek pointer
845 * See the documentation of IStream for more info.
847 * I am not totally sure what I am doing here but I presume that this
848 * should be basically as simple as creating a new stream with the same
849 * parent etc and positioning its seek cursor.
851 HRESULT WINAPI StgStreamImpl_Clone(
853 IStream** ppstm) /* [out] */
855 StgStreamImpl* const This=(StgStreamImpl*)iface;
857 StgStreamImpl* new_stream;
858 LARGE_INTEGER seek_pos;
864 return STG_E_INVALIDPOINTER;
866 new_stream = StgStreamImpl_Construct (This->parentStorage, This->grfMode, This->ownerProperty);
869 return STG_E_INSUFFICIENTMEMORY; /* Currently the only reason for new_stream=0 */
871 *ppstm = (IStream*) new_stream;
872 seek_pos.QuadPart = This->currentPosition.QuadPart;
874 hres=StgStreamImpl_Seek (*ppstm, seek_pos, STREAM_SEEK_SET, NULL);
876 assert (SUCCEEDED(hres));