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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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.
61 * IStorage_Release((IStorage*)This->parentStorage);
63 * No, don't do this. Some apps call IStorage_Release without
64 * calling IStream_Release first. If we grab a reference the
65 * file is not closed, and the app fails when it tries to
66 * reopen the file (Easy-PC, for example). Just inform the
67 * storage that we have closed the stream
70 if(This->parentStorage) {
72 StorageBaseImpl_RemoveStream(This->parentStorage, This);
76 This->parentStorage = 0;
79 * Make sure we clean-up the block chain stream objects that we were using.
81 if (This->bigBlockChain != 0)
83 BlockChainStream_Destroy(This->bigBlockChain);
84 This->bigBlockChain = 0;
87 if (This->smallBlockChain != 0)
89 SmallBlockChainStream_Destroy(This->smallBlockChain);
90 This->smallBlockChain = 0;
94 * Finally, free the memory used-up by the class.
96 HeapFree(GetProcessHeap(), 0, This);
100 * This implements the IUnknown method QueryInterface for this
103 static HRESULT WINAPI StgStreamImpl_QueryInterface(
105 REFIID riid, /* [in] */
106 void** ppvObject) /* [iid_is][out] */
108 StgStreamImpl* const This=(StgStreamImpl*)iface;
111 * Perform a sanity check on the parameters.
117 * Initialize the return parameter.
122 * Compare the riid with the interface IDs implemented by this object.
124 if (IsEqualIID(&IID_IUnknown, riid) ||
125 IsEqualIID(&IID_IPersist, riid) ||
126 IsEqualIID(&IID_IPersistStream, riid) ||
127 IsEqualIID(&IID_ISequentialStream, riid) ||
128 IsEqualIID(&IID_IStream, riid))
130 *ppvObject = (IStream*)This;
134 * Check that we obtained an interface.
137 return E_NOINTERFACE;
140 * Query Interface always increases the reference count by one when it is
143 IStream_AddRef(iface);
149 * This implements the IUnknown method AddRef for this
152 static ULONG WINAPI StgStreamImpl_AddRef(
155 StgStreamImpl* const This=(StgStreamImpl*)iface;
156 return InterlockedIncrement(&This->ref);
160 * This implements the IUnknown method Release for this
163 static ULONG WINAPI StgStreamImpl_Release(
166 StgStreamImpl* const This=(StgStreamImpl*)iface;
170 ref = InterlockedDecrement(&This->ref);
173 * If the reference count goes down to 0, perform suicide.
177 StgStreamImpl_Destroy(This);
184 * This method will open the block chain pointed by the property
185 * that describes the stream.
186 * If the stream's size is null, no chain is opened.
188 static void StgStreamImpl_OpenBlockChain(
191 StgProperty curProperty;
195 * Make sure no old object is left over.
197 if (This->smallBlockChain != 0)
199 SmallBlockChainStream_Destroy(This->smallBlockChain);
200 This->smallBlockChain = 0;
203 if (This->bigBlockChain != 0)
205 BlockChainStream_Destroy(This->bigBlockChain);
206 This->bigBlockChain = 0;
210 * Read the information from the property.
212 readSuccessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
218 This->streamSize = curProperty.size;
221 * This code supports only streams that are <32 bits in size.
223 assert(This->streamSize.u.HighPart == 0);
225 if(curProperty.startingBlock == BLOCK_END_OF_CHAIN)
227 assert( (This->streamSize.u.HighPart == 0) && (This->streamSize.u.LowPart == 0) );
231 if ( (This->streamSize.u.HighPart == 0) &&
232 (This->streamSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
234 This->smallBlockChain = SmallBlockChainStream_Construct(
235 This->parentStorage->ancestorStorage,
236 This->ownerProperty);
240 This->bigBlockChain = BlockChainStream_Construct(
241 This->parentStorage->ancestorStorage,
243 This->ownerProperty);
250 * This method is part of the ISequentialStream interface.
252 * It reads a block of information from the stream at the current
253 * position. It then moves the current position at the end of the
256 * See the documentation of ISequentialStream for more info.
258 static HRESULT WINAPI StgStreamImpl_Read(
260 void* pv, /* [length_is][size_is][out] */
262 ULONG* pcbRead) /* [out] */
264 StgStreamImpl* const This=(StgStreamImpl*)iface;
266 ULONG bytesReadBuffer;
267 ULONG bytesToReadFromBuffer;
270 TRACE("(%p, %p, %d, %p)\n",
271 iface, pv, cb, pcbRead);
273 if (!This->parentStorage)
275 WARN("storage reverted\n");
276 return STG_E_REVERTED;
280 * If the caller is not interested in the number of bytes read,
281 * we use another buffer to avoid "if" statements in the code.
284 pcbRead = &bytesReadBuffer;
287 * Using the known size of the stream, calculate the number of bytes
288 * to read from the block chain
290 bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
293 * Depending on the type of chain that was opened when the stream was constructed,
294 * we delegate the work to the method that reads the block chains.
296 if (This->smallBlockChain!=0)
298 res = SmallBlockChainStream_ReadAt(This->smallBlockChain,
299 This->currentPosition,
300 bytesToReadFromBuffer,
305 else if (This->bigBlockChain!=0)
307 res = BlockChainStream_ReadAt(This->bigBlockChain,
308 This->currentPosition,
309 bytesToReadFromBuffer,
316 * Small and big block chains are both NULL. This case will happen
317 * when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
328 * We should always be able to read the proper amount of data from the
331 assert(bytesToReadFromBuffer == *pcbRead);
334 * Advance the pointer for the number of positions read.
336 This->currentPosition.u.LowPart += *pcbRead;
340 TRACE("<-- %08x\n", res);
345 * This method is part of the ISequentialStream interface.
347 * It writes a block of information to the stream at the current
348 * position. It then moves the current position at the end of the
349 * written block. If the stream is too small to fit the block,
350 * the stream is grown to fit.
352 * See the documentation of ISequentialStream for more info.
354 static HRESULT WINAPI StgStreamImpl_Write(
356 const void* pv, /* [size_is][in] */
358 ULONG* pcbWritten) /* [out] */
360 StgStreamImpl* const This=(StgStreamImpl*)iface;
362 ULARGE_INTEGER newSize;
363 ULONG bytesWritten = 0;
366 TRACE("(%p, %p, %d, %p)\n",
367 iface, pv, cb, pcbWritten);
370 * Do we have permission to write to this stream?
372 switch(STGM_ACCESS_MODE(This->grfMode))
378 WARN("access denied by flags: 0x%x\n", STGM_ACCESS_MODE(This->grfMode));
379 return STG_E_ACCESSDENIED;
383 return STG_E_INVALIDPOINTER;
385 if (!This->parentStorage)
387 WARN("storage reverted\n");
388 return STG_E_REVERTED;
392 * If the caller is not interested in the number of bytes written,
393 * we use another buffer to avoid "if" statements in the code.
396 pcbWritten = &bytesWritten;
399 * Initialize the out parameter
405 TRACE("<-- S_OK, written 0\n");
410 newSize.u.HighPart = 0;
411 newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
415 * Verify if we need to grow the stream
417 if (newSize.u.LowPart > This->streamSize.u.LowPart)
420 res = IStream_SetSize(iface, newSize);
426 * Depending on the type of chain that was opened when the stream was constructed,
427 * we delegate the work to the method that readwrites to the block chains.
429 if (This->smallBlockChain!=0)
431 res = SmallBlockChainStream_WriteAt(This->smallBlockChain,
432 This->currentPosition,
438 else if (This->bigBlockChain!=0)
440 res = BlockChainStream_WriteAt(This->bigBlockChain,
441 This->currentPosition,
448 /* this should never happen because the IStream_SetSize call above will
449 * make sure a big or small block chain is created */
455 * Advance the position pointer for the number of positions written.
457 This->currentPosition.u.LowPart += *pcbWritten;
459 TRACE("<-- S_OK, written %u\n", *pcbWritten);
464 * This method is part of the IStream interface.
466 * It will move the current stream pointer according to the parameters
469 * See the documentation of IStream for more info.
471 static HRESULT WINAPI StgStreamImpl_Seek(
473 LARGE_INTEGER dlibMove, /* [in] */
474 DWORD dwOrigin, /* [in] */
475 ULARGE_INTEGER* plibNewPosition) /* [out] */
477 StgStreamImpl* const This=(StgStreamImpl*)iface;
479 ULARGE_INTEGER newPosition;
481 TRACE("(%p, %d, %d, %p)\n",
482 iface, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
485 * fail if the stream has no parent (as does windows)
488 if (!This->parentStorage)
490 WARN("storage reverted\n");
491 return STG_E_REVERTED;
495 * The caller is allowed to pass in NULL as the new position return value.
496 * If it happens, we assign it to a dynamic variable to avoid special cases
499 if (plibNewPosition == 0)
501 plibNewPosition = &newPosition;
505 * The file pointer is moved depending on the given "function"
510 case STREAM_SEEK_SET:
511 plibNewPosition->u.HighPart = 0;
512 plibNewPosition->u.LowPart = 0;
514 case STREAM_SEEK_CUR:
515 *plibNewPosition = This->currentPosition;
517 case STREAM_SEEK_END:
518 *plibNewPosition = This->streamSize;
521 WARN("invalid dwOrigin %d\n", dwOrigin);
522 return STG_E_INVALIDFUNCTION;
525 plibNewPosition->QuadPart = RtlLargeIntegerAdd( plibNewPosition->QuadPart, dlibMove.QuadPart );
528 * tell the caller what we calculated
530 This->currentPosition = *plibNewPosition;
536 * This method is part of the IStream interface.
538 * It will change the size of a stream.
540 * TODO: Switch from small blocks to big blocks and vice versa.
542 * See the documentation of IStream for more info.
544 static HRESULT WINAPI StgStreamImpl_SetSize(
546 ULARGE_INTEGER libNewSize) /* [in] */
548 StgStreamImpl* const This=(StgStreamImpl*)iface;
550 StgProperty curProperty;
553 TRACE("(%p, %d)\n", iface, libNewSize.u.LowPart);
555 if(!This->parentStorage)
557 WARN("storage reverted\n");
558 return STG_E_REVERTED;
564 if (libNewSize.u.HighPart != 0)
566 WARN("invalid value for libNewSize.u.HighPart %d\n", libNewSize.u.HighPart);
567 return STG_E_INVALIDFUNCTION;
571 * Do we have permission?
573 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
575 WARN("access denied\n");
576 return STG_E_ACCESSDENIED;
579 if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
583 * This will happen if we're creating a stream
585 if ((This->smallBlockChain == 0) && (This->bigBlockChain == 0))
587 if (libNewSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
589 This->smallBlockChain = SmallBlockChainStream_Construct(
590 This->parentStorage->ancestorStorage,
591 This->ownerProperty);
595 This->bigBlockChain = BlockChainStream_Construct(
596 This->parentStorage->ancestorStorage,
598 This->ownerProperty);
603 * Read this stream's property to see if it's small blocks or big blocks
605 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
609 * Determine if we have to switch from small to big blocks or vice versa
611 if ( (This->smallBlockChain!=0) &&
612 (curProperty.size.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
614 if (libNewSize.u.LowPart >= LIMIT_TO_USE_SMALL_BLOCK)
617 * Transform the small block chain into a big block chain
619 This->bigBlockChain = Storage32Impl_SmallBlocksToBigBlocks(
620 This->parentStorage->ancestorStorage,
621 &This->smallBlockChain);
625 if (This->smallBlockChain!=0)
627 Success = SmallBlockChainStream_SetSize(This->smallBlockChain, libNewSize);
631 Success = BlockChainStream_SetSize(This->bigBlockChain, libNewSize);
635 * Write the new information about this stream to the property
637 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
641 curProperty.size.u.HighPart = libNewSize.u.HighPart;
642 curProperty.size.u.LowPart = libNewSize.u.LowPart;
646 StorageImpl_WriteProperty(This->parentStorage->ancestorStorage,
651 This->streamSize = libNewSize;
657 * This method is part of the IStream interface.
659 * It will copy the 'cb' Bytes to 'pstm' IStream.
661 * See the documentation of IStream for more info.
663 static HRESULT WINAPI StgStreamImpl_CopyTo(
665 IStream* pstm, /* [unique][in] */
666 ULARGE_INTEGER cb, /* [in] */
667 ULARGE_INTEGER* pcbRead, /* [out] */
668 ULARGE_INTEGER* pcbWritten) /* [out] */
670 StgStreamImpl* const This=(StgStreamImpl*)iface;
673 ULONG bytesRead, bytesWritten, copySize;
674 ULARGE_INTEGER totalBytesRead;
675 ULARGE_INTEGER totalBytesWritten;
677 TRACE("(%p, %p, %d, %p, %p)\n",
678 iface, pstm, cb.u.LowPart, pcbRead, pcbWritten);
684 if (!This->parentStorage)
686 WARN("storage reverted\n");
687 return STG_E_REVERTED;
691 return STG_E_INVALIDPOINTER;
693 totalBytesRead.u.LowPart = totalBytesRead.u.HighPart = 0;
694 totalBytesWritten.u.LowPart = totalBytesWritten.u.HighPart = 0;
697 * use stack to store data temporarily
698 * there is surely a more performant way of doing it, for now this basic
699 * implementation will do the job
701 while ( cb.u.LowPart > 0 )
703 if ( cb.u.LowPart >= 128 )
706 copySize = cb.u.LowPart;
708 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
710 totalBytesRead.u.LowPart += bytesRead;
712 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
714 totalBytesWritten.u.LowPart += bytesWritten;
717 * Check that read & write operations were successful
719 if (bytesRead != bytesWritten)
721 hr = STG_E_MEDIUMFULL;
722 WARN("medium full\n");
726 if (bytesRead!=copySize)
729 cb.u.LowPart -= bytesRead;
733 * Update number of bytes read and written
737 pcbRead->u.LowPart = totalBytesRead.u.LowPart;
738 pcbRead->u.HighPart = totalBytesRead.u.HighPart;
743 pcbWritten->u.LowPart = totalBytesWritten.u.LowPart;
744 pcbWritten->u.HighPart = totalBytesWritten.u.HighPart;
750 * This method is part of the IStream interface.
752 * For streams contained in structured storages, this method
753 * does nothing. This is what the documentation tells us.
755 * See the documentation of IStream for more info.
757 static HRESULT WINAPI StgStreamImpl_Commit(
759 DWORD grfCommitFlags) /* [in] */
761 StgStreamImpl* const This=(StgStreamImpl*)iface;
763 if (!This->parentStorage)
765 WARN("storage reverted\n");
766 return STG_E_REVERTED;
773 * This method is part of the IStream interface.
775 * For streams contained in structured storages, this method
776 * does nothing. This is what the documentation tells us.
778 * See the documentation of IStream for more info.
780 static HRESULT WINAPI StgStreamImpl_Revert(
786 static HRESULT WINAPI StgStreamImpl_LockRegion(
788 ULARGE_INTEGER libOffset, /* [in] */
789 ULARGE_INTEGER cb, /* [in] */
790 DWORD dwLockType) /* [in] */
792 StgStreamImpl* const This=(StgStreamImpl*)iface;
794 if (!This->parentStorage)
796 WARN("storage reverted\n");
797 return STG_E_REVERTED;
800 FIXME("not implemented!\n");
804 static HRESULT WINAPI StgStreamImpl_UnlockRegion(
806 ULARGE_INTEGER libOffset, /* [in] */
807 ULARGE_INTEGER cb, /* [in] */
808 DWORD dwLockType) /* [in] */
810 StgStreamImpl* const This=(StgStreamImpl*)iface;
812 if (!This->parentStorage)
814 WARN("storage reverted\n");
815 return STG_E_REVERTED;
818 FIXME("not implemented!\n");
823 * This method is part of the IStream interface.
825 * This method returns information about the current
828 * See the documentation of IStream for more info.
830 static HRESULT WINAPI StgStreamImpl_Stat(
832 STATSTG* pstatstg, /* [out] */
833 DWORD grfStatFlag) /* [in] */
835 StgStreamImpl* const This=(StgStreamImpl*)iface;
837 StgProperty curProperty;
840 TRACE("%p %p %d\n", This, pstatstg, grfStatFlag);
843 * if stream has no parent, return STG_E_REVERTED
846 if (!This->parentStorage)
848 WARN("storage reverted\n");
849 return STG_E_REVERTED;
853 * Read the information from the property.
855 readSuccessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
861 StorageUtl_CopyPropertyToSTATSTG(pstatstg,
865 pstatstg->grfMode = This->grfMode;
870 WARN("failed to read properties\n");
875 * This method is part of the IStream interface.
877 * This method returns a clone of the interface that allows for
878 * another seek pointer
880 * See the documentation of IStream for more info.
882 * I am not totally sure what I am doing here but I presume that this
883 * should be basically as simple as creating a new stream with the same
884 * parent etc and positioning its seek cursor.
886 static HRESULT WINAPI StgStreamImpl_Clone(
888 IStream** ppstm) /* [out] */
890 StgStreamImpl* const This=(StgStreamImpl*)iface;
892 StgStreamImpl* new_stream;
893 LARGE_INTEGER seek_pos;
895 TRACE("%p %p\n", This, ppstm);
901 if (!This->parentStorage)
902 return STG_E_REVERTED;
905 return STG_E_INVALIDPOINTER;
907 new_stream = StgStreamImpl_Construct (This->parentStorage, This->grfMode, This->ownerProperty);
910 return STG_E_INSUFFICIENTMEMORY; /* Currently the only reason for new_stream=0 */
912 *ppstm = (IStream*) new_stream;
913 IStream_AddRef(*ppstm);
915 seek_pos.QuadPart = This->currentPosition.QuadPart;
917 hres=StgStreamImpl_Seek (*ppstm, seek_pos, STREAM_SEEK_SET, NULL);
919 assert (SUCCEEDED(hres));
925 * Virtual function table for the StgStreamImpl class.
927 static const IStreamVtbl StgStreamImpl_Vtbl =
929 StgStreamImpl_QueryInterface,
930 StgStreamImpl_AddRef,
931 StgStreamImpl_Release,
935 StgStreamImpl_SetSize,
936 StgStreamImpl_CopyTo,
937 StgStreamImpl_Commit,
938 StgStreamImpl_Revert,
939 StgStreamImpl_LockRegion,
940 StgStreamImpl_UnlockRegion,
945 /******************************************************************************
946 ** StgStreamImpl implementation
950 * This is the constructor for the StgStreamImpl class.
953 * parentStorage - Pointer to the storage that contains the stream to open
954 * ownerProperty - Index of the property that points to this stream.
956 StgStreamImpl* StgStreamImpl_Construct(
957 StorageBaseImpl* parentStorage,
961 StgStreamImpl* newStream;
963 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
968 * Set-up the virtual function table and reference count.
970 newStream->lpVtbl = &StgStreamImpl_Vtbl;
973 newStream->parentStorage = parentStorage;
976 * We want to nail-down the reference to the storage in case the
977 * stream out-lives the storage in the client application.
979 * -- IStorage_AddRef((IStorage*)newStream->parentStorage);
981 * No, don't do this. Some apps call IStorage_Release without
982 * calling IStream_Release first. If we grab a reference the
983 * file is not closed, and the app fails when it tries to
984 * reopen the file (Easy-PC, for example)
987 newStream->grfMode = grfMode;
988 newStream->ownerProperty = ownerProperty;
991 * Start the stream at the beginning.
993 newStream->currentPosition.u.HighPart = 0;
994 newStream->currentPosition.u.LowPart = 0;
997 * Initialize the rest of the data.
999 newStream->streamSize.u.HighPart = 0;
1000 newStream->streamSize.u.LowPart = 0;
1001 newStream->bigBlockChain = 0;
1002 newStream->smallBlockChain = 0;
1005 * Read the size from the property and determine if the blocks forming
1006 * this stream are large or small.
1008 StgStreamImpl_OpenBlockChain(newStream);
1010 /* add us to the storage's list of active streams */
1011 StorageBaseImpl_AddStream(parentStorage, newStream);