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
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
39 #include "wine/debug.h"
41 #include "storage32.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(storage);
47 * Virtual function table for the StgStreamImpl class.
49 static ICOM_VTABLE(IStream) StgStreamImpl_Vtbl =
51 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
52 StgStreamImpl_QueryInterface,
54 StgStreamImpl_Release,
58 StgStreamImpl_SetSize,
62 StgStreamImpl_LockRegion,
63 StgStreamImpl_UnlockRegion,
68 /******************************************************************************
69 ** StgStreamImpl implementation
73 * This is the constructor for the StgStreamImpl class.
76 * parentStorage - Pointer to the storage that contains the stream to open
77 * ownerProperty - Index of the property that points to this stream.
79 StgStreamImpl* StgStreamImpl_Construct(
80 StorageBaseImpl* parentStorage,
84 StgStreamImpl* newStream;
86 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
91 * Set-up the virtual function table and reference count.
93 newStream->lpVtbl = &StgStreamImpl_Vtbl;
97 * We want to nail-down the reference to the storage in case the
98 * stream out-lives the storage in the client application.
100 newStream->parentStorage = parentStorage;
101 IStorage_AddRef((IStorage*)newStream->parentStorage);
103 newStream->grfMode = grfMode;
104 newStream->ownerProperty = ownerProperty;
107 * Start the stream at the beginning.
109 newStream->currentPosition.u.HighPart = 0;
110 newStream->currentPosition.u.LowPart = 0;
113 * Initialize the rest of the data.
115 newStream->streamSize.u.HighPart = 0;
116 newStream->streamSize.u.LowPart = 0;
117 newStream->bigBlockChain = 0;
118 newStream->smallBlockChain = 0;
121 * Read the size from the property and determine if the blocks forming
122 * this stream are large or small.
124 StgStreamImpl_OpenBlockChain(newStream);
131 * This is the destructor of the StgStreamImpl class.
133 * This method will clean-up all the resources used-up by the given StgStreamImpl
134 * class. The pointer passed-in to this function will be freed and will not
137 void StgStreamImpl_Destroy(StgStreamImpl* This)
139 TRACE("(%p)\n", This);
142 * Release the reference we are holding on the parent storage.
144 IStorage_Release((IStorage*)This->parentStorage);
145 This->parentStorage = 0;
148 * Make sure we clean-up the block chain stream objects that we were using.
150 if (This->bigBlockChain != 0)
152 BlockChainStream_Destroy(This->bigBlockChain);
153 This->bigBlockChain = 0;
156 if (This->smallBlockChain != 0)
158 SmallBlockChainStream_Destroy(This->smallBlockChain);
159 This->smallBlockChain = 0;
163 * Finally, free the memory used-up by the class.
165 HeapFree(GetProcessHeap(), 0, This);
169 * This implements the IUnknown method QueryInterface for this
172 HRESULT WINAPI StgStreamImpl_QueryInterface(
174 REFIID riid, /* [in] */
175 void** ppvObject) /* [iid_is][out] */
177 StgStreamImpl* const This=(StgStreamImpl*)iface;
180 * Perform a sanity check on the parameters.
186 * Initialize the return parameter.
191 * Compare the riid with the interface IDs implemented by this object.
193 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
195 *ppvObject = (IStream*)This;
197 else if (memcmp(&IID_IStream, riid, sizeof(IID_IStream)) == 0)
199 *ppvObject = (IStream*)This;
203 * Check that we obtained an interface.
206 return E_NOINTERFACE;
209 * Query Interface always increases the reference count by one when it is
212 StgStreamImpl_AddRef(iface);
218 * This implements the IUnknown method AddRef for this
221 ULONG WINAPI StgStreamImpl_AddRef(
224 StgStreamImpl* const This=(StgStreamImpl*)iface;
232 * This implements the IUnknown method Release for this
235 ULONG WINAPI StgStreamImpl_Release(
238 StgStreamImpl* const This=(StgStreamImpl*)iface;
247 * If the reference count goes down to 0, perform suicide.
251 StgStreamImpl_Destroy(This);
258 * This method will open the block chain pointed by the property
259 * that describes the stream.
260 * If the stream's size is null, no chain is opened.
262 void StgStreamImpl_OpenBlockChain(
265 StgProperty curProperty;
269 * Make sure no old object is left over.
271 if (This->smallBlockChain != 0)
273 SmallBlockChainStream_Destroy(This->smallBlockChain);
274 This->smallBlockChain = 0;
277 if (This->bigBlockChain != 0)
279 BlockChainStream_Destroy(This->bigBlockChain);
280 This->bigBlockChain = 0;
284 * Read the information from the property.
286 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
292 This->streamSize = curProperty.size;
295 * This code supports only streams that are <32 bits in size.
297 assert(This->streamSize.u.HighPart == 0);
299 if(curProperty.startingBlock == BLOCK_END_OF_CHAIN)
301 assert( (This->streamSize.u.HighPart == 0) && (This->streamSize.u.LowPart == 0) );
305 if ( (This->streamSize.u.HighPart == 0) &&
306 (This->streamSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
308 This->smallBlockChain = SmallBlockChainStream_Construct(
309 This->parentStorage->ancestorStorage,
310 This->ownerProperty);
314 This->bigBlockChain = BlockChainStream_Construct(
315 This->parentStorage->ancestorStorage,
317 This->ownerProperty);
324 * This method is part of the ISequentialStream interface.
326 * It reads a block of information from the stream at the current
327 * position. It then moves the current position at the end of the
330 * See the documentation of ISequentialStream for more info.
332 HRESULT WINAPI StgStreamImpl_Read(
334 void* pv, /* [length_is][size_is][out] */
336 ULONG* pcbRead) /* [out] */
338 StgStreamImpl* const This=(StgStreamImpl*)iface;
340 ULONG bytesReadBuffer;
341 ULONG bytesToReadFromBuffer;
342 HRESULT res = S_FALSE;
344 TRACE("(%p, %p, %ld, %p)\n",
345 iface, pv, cb, pcbRead);
348 * If the caller is not interested in the number of bytes read,
349 * we use another buffer to avoid "if" statements in the code.
352 pcbRead = &bytesReadBuffer;
355 * Using the known size of the stream, calculate the number of bytes
356 * to read from the block chain
358 bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
361 * Depending on the type of chain that was opened when the stream was constructed,
362 * we delegate the work to the method that reads the block chains.
364 if (This->smallBlockChain!=0)
366 SmallBlockChainStream_ReadAt(This->smallBlockChain,
367 This->currentPosition,
368 bytesToReadFromBuffer,
373 else if (This->bigBlockChain!=0)
375 BlockChainStream_ReadAt(This->bigBlockChain,
376 This->currentPosition,
377 bytesToReadFromBuffer,
384 * Small and big block chains are both NULL. This case will happen
385 * when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
394 * We should always be able to read the proper amount of data from the
397 assert(bytesToReadFromBuffer == *pcbRead);
400 * Advance the pointer for the number of positions read.
402 This->currentPosition.u.LowPart += *pcbRead;
406 WARN("read %ld instead of the required %ld bytes !\n", *pcbRead, cb);
408 * this used to return S_FALSE, however MSDN docu says that an app should
409 * be prepared to handle error in case of stream end reached, as *some*
410 * implementations *might* return an error (IOW: most do *not*).
411 * As some program fails on returning S_FALSE, I better use S_OK here.
419 TRACE("<-- %08lx\n", res);
424 * This method is part of the ISequentialStream interface.
426 * It writes a block of information to the stream at the current
427 * position. It then moves the current position at the end of the
428 * written block. If the stream is too small to fit the block,
429 * the stream is grown to fit.
431 * See the documentation of ISequentialStream for more info.
433 HRESULT WINAPI StgStreamImpl_Write(
435 const void* pv, /* [size_is][in] */
437 ULONG* pcbWritten) /* [out] */
439 StgStreamImpl* const This=(StgStreamImpl*)iface;
441 ULARGE_INTEGER newSize;
442 ULONG bytesWritten = 0;
444 TRACE("(%p, %p, %ld, %p)\n",
445 iface, pv, cb, pcbWritten);
448 * Do we have permission to write to this stream?
450 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE))) {
451 return STG_E_ACCESSDENIED;
455 * If the caller is not interested in the number of bytes written,
456 * we use another buffer to avoid "if" statements in the code.
459 pcbWritten = &bytesWritten;
462 * Initialize the out parameter
472 newSize.u.HighPart = 0;
473 newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
477 * Verify if we need to grow the stream
479 if (newSize.u.LowPart > This->streamSize.u.LowPart)
482 IStream_SetSize(iface, newSize);
486 * Depending on the type of chain that was opened when the stream was constructed,
487 * we delegate the work to the method that readwrites to the block chains.
489 if (This->smallBlockChain!=0)
491 SmallBlockChainStream_WriteAt(This->smallBlockChain,
492 This->currentPosition,
498 else if (This->bigBlockChain!=0)
500 BlockChainStream_WriteAt(This->bigBlockChain,
501 This->currentPosition,
510 * Advance the position pointer for the number of positions written.
512 This->currentPosition.u.LowPart += *pcbWritten;
518 * This method is part of the IStream interface.
520 * It will move the current stream pointer according to the parameters
523 * See the documentation of IStream for more info.
525 HRESULT WINAPI StgStreamImpl_Seek(
527 LARGE_INTEGER dlibMove, /* [in] */
528 DWORD dwOrigin, /* [in] */
529 ULARGE_INTEGER* plibNewPosition) /* [out] */
531 StgStreamImpl* const This=(StgStreamImpl*)iface;
533 ULARGE_INTEGER newPosition;
535 TRACE("(%p, %ld, %ld, %p)\n",
536 iface, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
539 * The caller is allowed to pass in NULL as the new position return value.
540 * If it happens, we assign it to a dynamic variable to avoid special cases
543 if (plibNewPosition == 0)
545 plibNewPosition = &newPosition;
549 * The file pointer is moved depending on the given "function"
554 case STREAM_SEEK_SET:
555 plibNewPosition->u.HighPart = 0;
556 plibNewPosition->u.LowPart = 0;
558 case STREAM_SEEK_CUR:
559 *plibNewPosition = This->currentPosition;
561 case STREAM_SEEK_END:
562 *plibNewPosition = This->streamSize;
565 return STG_E_INVALIDFUNCTION;
568 plibNewPosition->QuadPart = RtlLargeIntegerAdd( plibNewPosition->QuadPart, dlibMove.QuadPart );
571 * tell the caller what we calculated
573 This->currentPosition = *plibNewPosition;
579 * This method is part of the IStream interface.
581 * It will change the size of a stream.
583 * TODO: Switch from small blocks to big blocks and vice versa.
585 * See the documentation of IStream for more info.
587 HRESULT WINAPI StgStreamImpl_SetSize(
589 ULARGE_INTEGER libNewSize) /* [in] */
591 StgStreamImpl* const This=(StgStreamImpl*)iface;
593 StgProperty curProperty;
596 TRACE("(%p, %ld)\n", iface, libNewSize.u.LowPart);
601 if (libNewSize.u.HighPart != 0)
602 return STG_E_INVALIDFUNCTION;
605 * Do we have permission?
607 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
608 return STG_E_ACCESSDENIED;
610 if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
614 * This will happen if we're creating a stream
616 if ((This->smallBlockChain == 0) && (This->bigBlockChain == 0))
618 if (libNewSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
620 This->smallBlockChain = SmallBlockChainStream_Construct(
621 This->parentStorage->ancestorStorage,
622 This->ownerProperty);
626 This->bigBlockChain = BlockChainStream_Construct(
627 This->parentStorage->ancestorStorage,
629 This->ownerProperty);
634 * Read this stream's property to see if it's small blocks or big blocks
636 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
640 * Determine if we have to switch from small to big blocks or vice versa
642 if ( (This->smallBlockChain!=0) &&
643 (curProperty.size.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
645 if (libNewSize.u.LowPart >= LIMIT_TO_USE_SMALL_BLOCK)
648 * Transform the small block chain into a big block chain
650 This->bigBlockChain = Storage32Impl_SmallBlocksToBigBlocks(
651 This->parentStorage->ancestorStorage,
652 &This->smallBlockChain);
656 if (This->smallBlockChain!=0)
658 Success = SmallBlockChainStream_SetSize(This->smallBlockChain, libNewSize);
662 Success = BlockChainStream_SetSize(This->bigBlockChain, libNewSize);
666 * Write the new information about this stream to the property
668 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
672 curProperty.size.u.HighPart = libNewSize.u.HighPart;
673 curProperty.size.u.LowPart = libNewSize.u.LowPart;
677 StorageImpl_WriteProperty(This->parentStorage->ancestorStorage,
682 This->streamSize = libNewSize;
688 * This method is part of the IStream interface.
690 * It will copy the 'cb' Bytes to 'pstm' IStream.
692 * See the documentation of IStream for more info.
694 HRESULT WINAPI StgStreamImpl_CopyTo(
696 IStream* pstm, /* [unique][in] */
697 ULARGE_INTEGER cb, /* [in] */
698 ULARGE_INTEGER* pcbRead, /* [out] */
699 ULARGE_INTEGER* pcbWritten) /* [out] */
703 ULONG bytesRead, bytesWritten, copySize;
704 ULARGE_INTEGER totalBytesRead;
705 ULARGE_INTEGER totalBytesWritten;
707 TRACE("(%p, %p, %ld, %p, %p)\n",
708 iface, pstm, cb.u.LowPart, pcbRead, pcbWritten);
714 return STG_E_INVALIDPOINTER;
716 totalBytesRead.u.LowPart = totalBytesRead.u.HighPart = 0;
717 totalBytesWritten.u.LowPart = totalBytesWritten.u.HighPart = 0;
720 * use stack to store data temporarily
721 * there is surely a more performant way of doing it, for now this basic
722 * implementation will do the job
724 while ( cb.u.LowPart > 0 )
726 if ( cb.u.LowPart >= 128 )
729 copySize = cb.u.LowPart;
731 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
733 totalBytesRead.u.LowPart += bytesRead;
735 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
737 totalBytesWritten.u.LowPart += bytesWritten;
740 * Check that read & write operations were successful
742 if (bytesRead != bytesWritten)
744 hr = STG_E_MEDIUMFULL;
748 if (bytesRead!=copySize)
751 cb.u.LowPart -= bytesRead;
755 * Update number of bytes read and written
759 pcbRead->u.LowPart = totalBytesRead.u.LowPart;
760 pcbRead->u.HighPart = totalBytesRead.u.HighPart;
765 pcbWritten->u.LowPart = totalBytesWritten.u.LowPart;
766 pcbWritten->u.HighPart = totalBytesWritten.u.HighPart;
772 * This method is part of the IStream interface.
774 * For streams contained in structured storages, this method
775 * does nothing. This is what the documentation tells us.
777 * See the documentation of IStream for more info.
779 HRESULT WINAPI StgStreamImpl_Commit(
781 DWORD grfCommitFlags) /* [in] */
787 * This method is part of the IStream interface.
789 * For streams contained in structured storages, this method
790 * does nothing. This is what the documentation tells us.
792 * See the documentation of IStream for more info.
794 HRESULT WINAPI StgStreamImpl_Revert(
800 HRESULT WINAPI StgStreamImpl_LockRegion(
802 ULARGE_INTEGER libOffset, /* [in] */
803 ULARGE_INTEGER cb, /* [in] */
804 DWORD dwLockType) /* [in] */
806 FIXME("not implemented!\n");
810 HRESULT WINAPI StgStreamImpl_UnlockRegion(
812 ULARGE_INTEGER libOffset, /* [in] */
813 ULARGE_INTEGER cb, /* [in] */
814 DWORD dwLockType) /* [in] */
816 FIXME("not implemented!\n");
821 * This method is part of the IStream interface.
823 * This method returns information about the current
826 * See the documentation of IStream for more info.
828 HRESULT WINAPI StgStreamImpl_Stat(
830 STATSTG* pstatstg, /* [out] */
831 DWORD grfStatFlag) /* [in] */
833 StgStreamImpl* const This=(StgStreamImpl*)iface;
835 StgProperty curProperty;
839 * Read the information from the property.
841 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
847 StorageUtl_CopyPropertyToSTATSTG(pstatstg,
851 pstatstg->grfMode = This->grfMode;
860 * This method is part of the IStream interface.
862 * This method returns a clone of the interface that allows for
863 * another seek pointer
865 * See the documentation of IStream for more info.
867 * I am not totally sure what I am doing here but I presume that this
868 * should be basically as simple as creating a new stream with the same
869 * parent etc and positioning its seek cursor.
871 HRESULT WINAPI StgStreamImpl_Clone(
873 IStream** ppstm) /* [out] */
875 StgStreamImpl* const This=(StgStreamImpl*)iface;
877 StgStreamImpl* new_stream;
878 LARGE_INTEGER seek_pos;
884 return STG_E_INVALIDPOINTER;
886 new_stream = StgStreamImpl_Construct (This->parentStorage, This->grfMode, This->ownerProperty);
889 return STG_E_INSUFFICIENTMEMORY; /* Currently the only reason for new_stream=0 */
891 *ppstm = (IStream*) new_stream;
892 seek_pos.QuadPart = This->currentPosition.QuadPart;
894 hres=StgStreamImpl_Seek (*ppstm, seek_pos, STREAM_SEEK_SET, NULL);
896 assert (SUCCEEDED(hres));