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
16 #include <ddk/ntddk.h>
17 #include <ole32/ole32.h>
18 #include "storage32.h"
24 * Virtual function table for the StgStreamImpl class.
26 static ICOM_VTABLE(IStream) StgStreamImpl_Vtbl =
28 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
29 StgStreamImpl_QueryInterface,
31 StgStreamImpl_Release,
35 StgStreamImpl_SetSize,
39 StgStreamImpl_LockRegion,
40 StgStreamImpl_UnlockRegion,
45 /******************************************************************************
46 ** StgStreamImpl implementation
50 * This is the constructor for the StgStreamImpl class.
53 * parentStorage - Pointer to the storage that contains the stream to open
54 * ownerProperty - Index of the property that points to this stream.
56 StgStreamImpl* StgStreamImpl_Construct(
57 StorageBaseImpl* parentStorage,
61 StgStreamImpl* newStream;
63 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
68 * Set-up the virtual function table and reference count.
70 ICOM_VTBL(newStream) = &StgStreamImpl_Vtbl;
74 * We want to nail-down the reference to the storage in case the
75 * stream out-lives the storage in the client application.
77 newStream->parentStorage = parentStorage;
78 IStorage_AddRef((IStorage*)newStream->parentStorage);
80 newStream->grfMode = grfMode;
81 newStream->ownerProperty = ownerProperty;
84 * Start the stream at the begining.
86 newStream->currentPosition.u.HighPart = 0;
87 newStream->currentPosition.u.LowPart = 0;
90 * Initialize the rest of the data.
92 newStream->streamSize.u.HighPart = 0;
93 newStream->streamSize.u.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)
116 Print(MAX_TRACE, ("(%p)\n", This));
119 * Release the reference we are holding on the parent storage.
121 IStorage_Release((IStorage*)This->parentStorage);
122 This->parentStorage = 0;
125 * Make sure we clean-up the block chain stream objects that we were using.
127 if (This->bigBlockChain != 0)
129 BlockChainStream_Destroy(This->bigBlockChain);
130 This->bigBlockChain = 0;
133 if (This->smallBlockChain != 0)
135 SmallBlockChainStream_Destroy(This->smallBlockChain);
136 This->smallBlockChain = 0;
140 * Finally, free the memory used-up by the class.
142 HeapFree(GetProcessHeap(), 0, This);
146 * This implements the IUnknown method QueryInterface for this
149 HRESULT WINAPI StgStreamImpl_QueryInterface(
151 REFIID riid, /* [in] */
152 void** ppvObject) /* [iid_is][out] */
154 StgStreamImpl* const This=(StgStreamImpl*)iface;
157 * Perform a sanity check on the parameters.
163 * Initialize the return parameter.
168 * Compare the riid with the interface IDs implemented by this object.
170 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
172 *ppvObject = (IStream*)This;
174 else if (memcmp(&IID_IStream, riid, sizeof(IID_IStream)) == 0)
176 *ppvObject = (IStream*)This;
180 * Check that we obtained an interface.
183 return E_NOINTERFACE;
186 * Query Interface always increases the reference count by one when it is
189 StgStreamImpl_AddRef(iface);
195 * This implements the IUnknown method AddRef for this
198 ULONG WINAPI StgStreamImpl_AddRef(
201 StgStreamImpl* const This=(StgStreamImpl*)iface;
209 * This implements the IUnknown method Release for this
212 ULONG WINAPI StgStreamImpl_Release(
215 StgStreamImpl* const This=(StgStreamImpl*)iface;
224 * If the reference count goes down to 0, perform suicide.
228 StgStreamImpl_Destroy(This);
235 * This method will open the block chain pointed by the property
236 * that describes the stream.
237 * If the stream's size is null, no chain is opened.
239 void StgStreamImpl_OpenBlockChain(
242 StgProperty curProperty;
246 * Make sure no old object is staying behind.
248 if (This->smallBlockChain != 0)
250 SmallBlockChainStream_Destroy(This->smallBlockChain);
251 This->smallBlockChain = 0;
254 if (This->bigBlockChain != 0)
256 BlockChainStream_Destroy(This->bigBlockChain);
257 This->bigBlockChain = 0;
261 * Read the information from the property.
263 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
269 This->streamSize = curProperty.size;
272 * This code supports only streams that are <32 bits in size.
274 assert(This->streamSize.u.HighPart == 0);
276 if(curProperty.startingBlock == BLOCK_END_OF_CHAIN)
278 assert( (This->streamSize.u.HighPart == 0) && (This->streamSize.u.LowPart == 0) );
282 if ( (This->streamSize.u.HighPart == 0) &&
283 (This->streamSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
285 This->smallBlockChain = SmallBlockChainStream_Construct(
286 This->parentStorage->ancestorStorage,
287 This->ownerProperty);
291 This->bigBlockChain = BlockChainStream_Construct(
292 This->parentStorage->ancestorStorage,
294 This->ownerProperty);
301 * This method is part of the ISequentialStream interface.
303 * If reads a block of information from the stream at the current
304 * position. It then moves the current position at the end of the
307 * See the documentation of ISequentialStream for more info.
309 HRESULT WINAPI StgStreamImpl_Read(
311 void* pv, /* [length_is][size_is][out] */
313 ULONG* pcbRead) /* [out] */
315 StgStreamImpl* const This=(StgStreamImpl*)iface;
317 ULONG bytesReadBuffer;
318 ULONG bytesToReadFromBuffer;
320 Print(MAX_TRACE, ("(%p, %p, %ld, %p)\n",
321 iface, pv, cb, pcbRead));
324 * If the caller is not interested in the nubmer of bytes read,
325 * we use another buffer to avoid "if" statements in the code.
328 pcbRead = &bytesReadBuffer;
331 * Using the known size of the stream, calculate the number of bytes
332 * to read from the block chain
334 bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
337 * Depending on the type of chain that was opened when the stream was constructed,
338 * we delegate the work to the method that read the block chains.
340 if (This->smallBlockChain!=0)
342 SmallBlockChainStream_ReadAt(This->smallBlockChain,
343 This->currentPosition,
344 bytesToReadFromBuffer,
349 else if (This->bigBlockChain!=0)
351 BlockChainStream_ReadAt(This->bigBlockChain,
352 This->currentPosition,
353 bytesToReadFromBuffer,
360 * Small and big block chains are both NULL. This case will happen
361 * when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
369 * We should always be able to read the proper amount of data from the
372 assert(bytesToReadFromBuffer == *pcbRead);
375 * Advance the pointer for the number of positions read.
377 This->currentPosition.u.LowPart += *pcbRead;
380 * The function returns S_OK if the buffer was filled completely
381 * it returns S_FALSE if the end of the stream is reached before the
391 * This method is part of the ISequentialStream interface.
393 * It writes a block of information to the stream at the current
394 * position. It then moves the current position at the end of the
395 * written block. If the stream is too small to fit the block,
396 * the stream is grown to fit.
398 * See the documentation of ISequentialStream for more info.
400 HRESULT WINAPI StgStreamImpl_Write(
402 const void* pv, /* [size_is][in] */
404 ULONG* pcbWritten) /* [out] */
406 StgStreamImpl* const This=(StgStreamImpl*)iface;
408 ULARGE_INTEGER newSize;
409 ULONG bytesWritten = 0;
411 Print(MAX_TRACE, ("(%p, %p, %ld, %p)\n",
412 iface, pv, cb, pcbWritten));
415 * Do we have permission to write to this stream?
417 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE))) {
418 return STG_E_ACCESSDENIED;
422 * If the caller is not interested in the number of bytes written,
423 * we use another buffer to avoid "if" statements in the code.
426 pcbWritten = &bytesWritten;
429 * Initialize the out parameter
439 newSize.u.HighPart = 0;
440 newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
444 * Verify if we need to grow the stream
446 if (newSize.u.LowPart > This->streamSize.u.LowPart)
449 IStream_SetSize(iface, newSize);
453 * Depending on the type of chain that was opened when the stream was constructed,
454 * we delegate the work to the method that readwrites to the block chains.
456 if (This->smallBlockChain!=0)
458 SmallBlockChainStream_WriteAt(This->smallBlockChain,
459 This->currentPosition,
465 else if (This->bigBlockChain!=0)
467 BlockChainStream_WriteAt(This->bigBlockChain,
468 This->currentPosition,
477 * Advance the position pointer for the number of positions written.
479 This->currentPosition.u.LowPart += *pcbWritten;
485 * This method is part of the IStream interface.
487 * It will move the current stream pointer according to the parameters
490 * See the documentation of IStream for more info.
492 HRESULT WINAPI StgStreamImpl_Seek(
494 LARGE_INTEGER dlibMove, /* [in] */
495 DWORD dwOrigin, /* [in] */
496 ULARGE_INTEGER* plibNewPosition) /* [out] */
498 StgStreamImpl* const This=(StgStreamImpl*)iface;
500 ULARGE_INTEGER newPosition;
501 LARGE_INTEGER Position;
503 Print(MAX_TRACE, ("(%p, %ld, %ld, %p)\n",
504 iface, dlibMove.u.LowPart, dwOrigin, plibNewPosition));
507 * The caller is allowed to pass in NULL as the new position return value.
508 * If it happens, we assign it to a dynamic variable to avoid special cases
511 if (plibNewPosition == 0)
513 plibNewPosition = &newPosition;
517 * The file pointer is moved depending on the given "function"
522 case STREAM_SEEK_SET:
523 plibNewPosition->u.HighPart = 0;
524 plibNewPosition->u.LowPart = 0;
526 case STREAM_SEEK_CUR:
527 *plibNewPosition = This->currentPosition;
529 case STREAM_SEEK_END:
530 *plibNewPosition = This->streamSize;
533 return STG_E_INVALIDFUNCTION;
536 Position = RtlLargeIntegerAdd( *((PLARGE_INTEGER)plibNewPosition), dlibMove );
538 plibNewPosition->QuadPart = Position.QuadPart;
541 * tell the caller what we calculated
543 This->currentPosition = *plibNewPosition;
549 * This method is part of the IStream interface.
551 * It will change the size of a stream.
553 * TODO: Switch from small blocks to big blocks and vice versa.
555 * See the documentation of IStream for more info.
557 HRESULT WINAPI StgStreamImpl_SetSize(
559 ULARGE_INTEGER libNewSize) /* [in] */
561 StgStreamImpl* const This=(StgStreamImpl*)iface;
563 StgProperty curProperty;
566 Print(MAX_TRACE, ("(%p, %ld)\n", iface, libNewSize.u.LowPart));
571 if (libNewSize.u.HighPart != 0)
572 return STG_E_INVALIDFUNCTION;
575 * Do we have permission?
577 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
578 return STG_E_ACCESSDENIED;
580 if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
584 * This will happen if we're creating a stream
586 if ((This->smallBlockChain == 0) && (This->bigBlockChain == 0))
588 if (libNewSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
590 This->smallBlockChain = SmallBlockChainStream_Construct(
591 This->parentStorage->ancestorStorage,
592 This->ownerProperty);
596 This->bigBlockChain = BlockChainStream_Construct(
597 This->parentStorage->ancestorStorage,
599 This->ownerProperty);
604 * Read this stream's property to see if it's small blocks or big blocks
606 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
610 * Determine if we have to switch from small to big blocks or vice versa
612 if ( (This->smallBlockChain!=0) &&
613 (curProperty.size.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
615 if (libNewSize.u.LowPart >= LIMIT_TO_USE_SMALL_BLOCK)
618 * Transform the small block chain into a big block chain
620 This->bigBlockChain = Storage32Impl_SmallBlocksToBigBlocks(
621 This->parentStorage->ancestorStorage,
622 &This->smallBlockChain);
626 if (This->smallBlockChain!=0)
628 Success = SmallBlockChainStream_SetSize(This->smallBlockChain, libNewSize);
632 Success = BlockChainStream_SetSize(This->bigBlockChain, libNewSize);
636 * Write to the property the new information about this stream
638 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
642 curProperty.size.u.HighPart = libNewSize.u.HighPart;
643 curProperty.size.u.LowPart = libNewSize.u.LowPart;
647 StorageImpl_WriteProperty(This->parentStorage->ancestorStorage,
652 This->streamSize = libNewSize;
658 * This method is part of the IStream interface.
660 * It will copy the 'cb' Bytes to 'pstm' IStream.
662 * See the documentation of IStream for more info.
664 HRESULT WINAPI StgStreamImpl_CopyTo(
666 IStream* pstm, /* [unique][in] */
667 ULARGE_INTEGER cb, /* [in] */
668 ULARGE_INTEGER* pcbRead, /* [out] */
669 ULARGE_INTEGER* pcbWritten) /* [out] */
673 ULONG bytesRead, bytesWritten, copySize;
674 ULARGE_INTEGER totalBytesRead;
675 ULARGE_INTEGER totalBytesWritten;
677 Print(MAX_TRACE, ("(%p, %p, %ld, %p, %p)\n",
678 iface, pstm, cb.u.LowPart, pcbRead, pcbWritten));
684 return STG_E_INVALIDPOINTER;
686 totalBytesRead.u.LowPart = totalBytesRead.u.HighPart = 0;
687 totalBytesWritten.u.LowPart = totalBytesWritten.u.HighPart = 0;
690 * use stack to store data temporarly
691 * there is surely more performant way of doing it, for now this basic
692 * implementation will do the job
694 while ( cb.u.LowPart > 0 )
696 if ( cb.u.LowPart >= 128 )
699 copySize = cb.u.LowPart;
701 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
703 totalBytesRead.u.LowPart += bytesRead;
705 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
707 totalBytesWritten.u.LowPart += bytesWritten;
710 * Check that read & write operations were succesfull
712 if (bytesRead != bytesWritten)
714 hr = STG_E_MEDIUMFULL;
718 if (bytesRead!=copySize)
721 cb.u.LowPart -= bytesRead;
725 * Update number of bytes read and written
729 pcbRead->u.LowPart = totalBytesRead.u.LowPart;
730 pcbRead->u.HighPart = totalBytesRead.u.HighPart;
735 pcbWritten->u.LowPart = totalBytesWritten.u.LowPart;
736 pcbWritten->u.HighPart = totalBytesWritten.u.HighPart;
742 * This method is part of the IStream interface.
744 * For streams contained in structured storages, this method
745 * does nothing. This is what the documentation tells us.
747 * See the documentation of IStream for more info.
749 HRESULT WINAPI StgStreamImpl_Commit(
751 DWORD grfCommitFlags) /* [in] */
757 * This method is part of the IStream interface.
759 * For streams contained in structured storages, this method
760 * does nothing. This is what the documentation tells us.
762 * See the documentation of IStream for more info.
764 HRESULT WINAPI StgStreamImpl_Revert(
770 HRESULT WINAPI StgStreamImpl_LockRegion(
772 ULARGE_INTEGER libOffset, /* [in] */
773 ULARGE_INTEGER cb, /* [in] */
774 DWORD dwLockType) /* [in] */
780 HRESULT WINAPI StgStreamImpl_UnlockRegion(
782 ULARGE_INTEGER libOffset, /* [in] */
783 ULARGE_INTEGER cb, /* [in] */
784 DWORD dwLockType) /* [in] */
791 * This method is part of the IStream interface.
793 * This method returns information about the current
796 * See the documentation of IStream for more info.
798 HRESULT WINAPI StgStreamImpl_Stat(
800 STATSTG* pstatstg, /* [out] */
801 DWORD grfStatFlag) /* [in] */
803 StgStreamImpl* const This=(StgStreamImpl*)iface;
805 StgProperty curProperty;
809 * Read the information from the property.
811 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
817 StorageUtl_CopyPropertyToSTATSTG(pstatstg,
821 pstatstg->grfMode = This->grfMode;
829 HRESULT WINAPI StgStreamImpl_Clone(
831 IStream** ppstm) /* [out] */