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
18 #include "debugtools.h"
20 #include "storage32.h"
22 DEFAULT_DEBUG_CHANNEL(storage);
26 * Virtual function table for the StgStreamImpl class.
28 static ICOM_VTABLE(IStream) StgStreamImpl_Vtbl =
30 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
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,
63 StgStreamImpl* newStream;
65 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
70 * Set-up the virtual function table and reference count.
72 ICOM_VTBL(newStream) = &StgStreamImpl_Vtbl;
76 * We want to nail-down the reference to the storage in case the
77 * stream out-lives the storage in the client application.
79 newStream->parentStorage = parentStorage;
80 IStorage_AddRef((IStorage*)newStream->parentStorage);
82 newStream->grfMode = grfMode;
83 newStream->ownerProperty = ownerProperty;
86 * Start the stream at the begining.
88 newStream->currentPosition.s.HighPart = 0;
89 newStream->currentPosition.s.LowPart = 0;
92 * Initialize the rest of the data.
94 newStream->streamSize.s.HighPart = 0;
95 newStream->streamSize.s.LowPart = 0;
96 newStream->bigBlockChain = 0;
97 newStream->smallBlockChain = 0;
100 * Read the size from the property and determine if the blocks forming
101 * this stream are large or small.
103 StgStreamImpl_OpenBlockChain(newStream);
110 * This is the destructor of the StgStreamImpl class.
112 * This method will clean-up all the resources used-up by the given StgStreamImpl
113 * class. The pointer passed-in to this function will be freed and will not
116 void StgStreamImpl_Destroy(StgStreamImpl* This)
118 TRACE("(%p)\n", This);
121 * Release the reference we are holding on the parent storage.
123 IStorage_Release((IStorage*)This->parentStorage);
124 This->parentStorage = 0;
127 * Make sure we clean-up the block chain stream objects that we were using.
129 if (This->bigBlockChain != 0)
131 BlockChainStream_Destroy(This->bigBlockChain);
132 This->bigBlockChain = 0;
135 if (This->smallBlockChain != 0)
137 SmallBlockChainStream_Destroy(This->smallBlockChain);
138 This->smallBlockChain = 0;
142 * Finally, free the memory used-up by the class.
144 HeapFree(GetProcessHeap(), 0, This);
148 * This implements the IUnknown method QueryInterface for this
151 HRESULT WINAPI StgStreamImpl_QueryInterface(
153 REFIID riid, /* [in] */
154 void** ppvObject) /* [iid_is][out] */
156 StgStreamImpl* const This=(StgStreamImpl*)iface;
159 * Perform a sanity check on the parameters.
165 * Initialize the return parameter.
170 * Compare the riid with the interface IDs implemented by this object.
172 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
174 *ppvObject = (IStream*)This;
176 else if (memcmp(&IID_IStream, riid, sizeof(IID_IStream)) == 0)
178 *ppvObject = (IStream*)This;
182 * Check that we obtained an interface.
185 return E_NOINTERFACE;
188 * Query Interface always increases the reference count by one when it is
191 StgStreamImpl_AddRef(iface);
197 * This implements the IUnknown method AddRef for this
200 ULONG WINAPI StgStreamImpl_AddRef(
203 StgStreamImpl* const This=(StgStreamImpl*)iface;
211 * This implements the IUnknown method Release for this
214 ULONG WINAPI StgStreamImpl_Release(
217 StgStreamImpl* const This=(StgStreamImpl*)iface;
226 * If the reference count goes down to 0, perform suicide.
230 StgStreamImpl_Destroy(This);
237 * This method will open the block chain pointed by the property
238 * that describes the stream.
239 * If the stream's size is null, no chain is opened.
241 void StgStreamImpl_OpenBlockChain(
244 StgProperty curProperty;
248 * Make sure no old object is staying behind.
250 if (This->smallBlockChain != 0)
252 SmallBlockChainStream_Destroy(This->smallBlockChain);
253 This->smallBlockChain = 0;
256 if (This->bigBlockChain != 0)
258 BlockChainStream_Destroy(This->bigBlockChain);
259 This->bigBlockChain = 0;
263 * Read the information from the property.
265 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
271 This->streamSize = curProperty.size;
274 * This code supports only streams that are <32 bits in size.
276 assert(This->streamSize.s.HighPart == 0);
278 if(curProperty.startingBlock == BLOCK_END_OF_CHAIN)
280 assert( (This->streamSize.s.HighPart == 0) && (This->streamSize.s.LowPart == 0) );
284 if ( (This->streamSize.s.HighPart == 0) &&
285 (This->streamSize.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
287 This->smallBlockChain = SmallBlockChainStream_Construct(
288 This->parentStorage->ancestorStorage,
289 This->ownerProperty);
293 This->bigBlockChain = BlockChainStream_Construct(
294 This->parentStorage->ancestorStorage,
296 This->ownerProperty);
303 * This method is part of the ISequentialStream interface.
305 * If reads a block of information from the stream at the current
306 * position. It then moves the current position at the end of the
309 * See the documentation of ISequentialStream for more info.
311 HRESULT WINAPI StgStreamImpl_Read(
313 void* pv, /* [length_is][size_is][out] */
315 ULONG* pcbRead) /* [out] */
317 StgStreamImpl* const This=(StgStreamImpl*)iface;
319 ULONG bytesReadBuffer;
320 ULONG bytesToReadFromBuffer;
322 TRACE("(%p, %p, %ld, %p)\n",
323 iface, pv, cb, pcbRead);
326 * If the caller is not interested in the nubmer of bytes read,
327 * we use another buffer to avoid "if" statements in the code.
330 pcbRead = &bytesReadBuffer;
333 * Using the known size of the stream, calculate the number of bytes
334 * to read from the block chain
336 bytesToReadFromBuffer = min( This->streamSize.s.LowPart - This->currentPosition.s.LowPart, cb);
339 * Depending on the type of chain that was opened when the stream was constructed,
340 * we delegate the work to the method that read the block chains.
342 if (This->smallBlockChain!=0)
344 SmallBlockChainStream_ReadAt(This->smallBlockChain,
345 This->currentPosition,
346 bytesToReadFromBuffer,
351 else if (This->bigBlockChain!=0)
353 BlockChainStream_ReadAt(This->bigBlockChain,
354 This->currentPosition,
355 bytesToReadFromBuffer,
362 * Small and big block chains are both NULL. This case will happen
363 * when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
371 * We should always be able to read the proper amount of data from the
374 assert(bytesToReadFromBuffer == *pcbRead);
377 * Advance the pointer for the number of positions read.
379 This->currentPosition.s.LowPart += *pcbRead;
382 * The function returns S_OK if the buffer was filled completely
383 * it returns S_FALSE if the end of the stream is reached before the
393 * This method is part of the ISequentialStream interface.
395 * It writes a block of information to the stream at the current
396 * position. It then moves the current position at the end of the
397 * written block. If the stream is too small to fit the block,
398 * the stream is grown to fit.
400 * See the documentation of ISequentialStream for more info.
402 HRESULT WINAPI StgStreamImpl_Write(
404 const void* pv, /* [size_is][in] */
406 ULONG* pcbWritten) /* [out] */
408 StgStreamImpl* const This=(StgStreamImpl*)iface;
410 ULARGE_INTEGER newSize;
411 ULONG bytesWritten = 0;
413 TRACE("(%p, %p, %ld, %p)\n",
414 iface, pv, cb, pcbWritten);
417 * Do we have permission to write to this stream?
419 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE))) {
420 return STG_E_ACCESSDENIED;
424 * If the caller is not interested in the number of bytes written,
425 * we use another buffer to avoid "if" statements in the code.
428 pcbWritten = &bytesWritten;
431 * Initialize the out parameter
441 newSize.s.HighPart = 0;
442 newSize.s.LowPart = This->currentPosition.s.LowPart + cb;
446 * Verify if we need to grow the stream
448 if (newSize.s.LowPart > This->streamSize.s.LowPart)
451 IStream_SetSize(iface, newSize);
455 * Depending on the type of chain that was opened when the stream was constructed,
456 * we delegate the work to the method that readwrites to the block chains.
458 if (This->smallBlockChain!=0)
460 SmallBlockChainStream_WriteAt(This->smallBlockChain,
461 This->currentPosition,
467 else if (This->bigBlockChain!=0)
469 BlockChainStream_WriteAt(This->bigBlockChain,
470 This->currentPosition,
479 * Advance the position pointer for the number of positions written.
481 This->currentPosition.s.LowPart += *pcbWritten;
487 * This method is part of the IStream interface.
489 * It will move the current stream pointer according to the parameters
492 * See the documentation of IStream for more info.
494 HRESULT WINAPI StgStreamImpl_Seek(
496 LARGE_INTEGER dlibMove, /* [in] */
497 DWORD dwOrigin, /* [in] */
498 ULARGE_INTEGER* plibNewPosition) /* [out] */
500 StgStreamImpl* const This=(StgStreamImpl*)iface;
502 ULARGE_INTEGER newPosition;
504 TRACE("(%p, %ld, %ld, %p)\n",
505 iface, dlibMove.s.LowPart, dwOrigin, plibNewPosition);
508 * The caller is allowed to pass in NULL as the new position return value.
509 * If it happens, we assign it to a dynamic variable to avoid special cases
512 if (plibNewPosition == 0)
514 plibNewPosition = &newPosition;
518 * The file pointer is moved depending on the given "function"
523 case STREAM_SEEK_SET:
524 plibNewPosition->s.HighPart = 0;
525 plibNewPosition->s.LowPart = 0;
527 case STREAM_SEEK_CUR:
528 *plibNewPosition = This->currentPosition;
530 case STREAM_SEEK_END:
531 *plibNewPosition = This->streamSize;
534 return STG_E_INVALIDFUNCTION;
537 #if SIZEOF_LONG_LONG >= 8
538 plibNewPosition->QuadPart += dlibMove.QuadPart;
541 * do some multiword arithmetic:
542 * treat HighPart as a signed value
543 * treat LowPart as unsigned
544 * NOTE: this stuff is two's complement specific!
546 if (dlibMove.s.HighPart < 0) { /* dlibMove is < 0 */
547 /* calculate the absolute value of dlibMove ... */
548 dlibMove.s.HighPart = -dlibMove.s.HighPart;
549 dlibMove.s.LowPart ^= -1;
550 /* ... and subtract with carry */
551 if (dlibMove.s.LowPart > plibNewPosition->s.LowPart) {
552 /* carry needed, This accounts for any underflows at [1]*/
553 plibNewPosition->s.HighPart -= 1;
555 plibNewPosition->s.LowPart -= dlibMove.s.LowPart; /* [1] */
556 plibNewPosition->s.HighPart -= dlibMove.s.HighPart;
559 int initialLowPart = plibNewPosition->s.LowPart;
560 plibNewPosition->s.LowPart += dlibMove.s.LowPart;
561 if((plibNewPosition->s.LowPart < initialLowPart) ||
562 (plibNewPosition->s.LowPart < dlibMove.s.LowPart)) {
563 /* LowPart has rolled over => add the carry digit to HighPart */
564 plibNewPosition->s.HighPart++;
566 plibNewPosition->s.HighPart += dlibMove.s.HighPart;
569 * Check if we end-up before the beginning of the file. That should
572 if (plibNewPosition->s.HighPart < 0) {
573 return STG_E_INVALIDPOINTER;
577 * We currently don't support files with offsets of >32 bits.
578 * Note that we have checked for a negative offset already
580 assert(plibNewPosition->s.HighPart <= 0);
585 * tell the caller what we calculated
587 This->currentPosition = *plibNewPosition;
593 * This method is part of the IStream interface.
595 * It will change the size of a stream.
597 * TODO: Switch from small blocks to big blocks and vice versa.
599 * See the documentation of IStream for more info.
601 HRESULT WINAPI StgStreamImpl_SetSize(
603 ULARGE_INTEGER libNewSize) /* [in] */
605 StgStreamImpl* const This=(StgStreamImpl*)iface;
607 StgProperty curProperty;
610 TRACE("(%p, %ld)\n", iface, libNewSize.s.LowPart);
615 if (libNewSize.s.HighPart != 0)
616 return STG_E_INVALIDFUNCTION;
619 * Do we have permission?
621 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
622 return STG_E_ACCESSDENIED;
624 if (This->streamSize.s.LowPart == libNewSize.s.LowPart)
628 * This will happen if we're creating a stream
630 if ((This->smallBlockChain == 0) && (This->bigBlockChain == 0))
632 if (libNewSize.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
634 This->smallBlockChain = SmallBlockChainStream_Construct(
635 This->parentStorage->ancestorStorage,
636 This->ownerProperty);
640 This->bigBlockChain = BlockChainStream_Construct(
641 This->parentStorage->ancestorStorage,
643 This->ownerProperty);
648 * Read this stream's property to see if it's small blocks or big blocks
650 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
654 * Determine if we have to switch from small to big blocks or vice versa
656 if ( (This->smallBlockChain!=0) &&
657 (curProperty.size.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
659 if (libNewSize.s.LowPart >= LIMIT_TO_USE_SMALL_BLOCK)
662 * Transform the small block chain into a big block chain
664 This->bigBlockChain = Storage32Impl_SmallBlocksToBigBlocks(
665 This->parentStorage->ancestorStorage,
666 &This->smallBlockChain);
670 if (This->smallBlockChain!=0)
672 Success = SmallBlockChainStream_SetSize(This->smallBlockChain, libNewSize);
676 Success = BlockChainStream_SetSize(This->bigBlockChain, libNewSize);
680 * Write to the property the new information about this stream
682 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
686 curProperty.size.s.HighPart = libNewSize.s.HighPart;
687 curProperty.size.s.LowPart = libNewSize.s.LowPart;
691 StorageImpl_WriteProperty(This->parentStorage->ancestorStorage,
696 This->streamSize = libNewSize;
702 * This method is part of the IStream interface.
704 * It will copy the 'cb' Bytes to 'pstm' IStream.
706 * See the documentation of IStream for more info.
708 HRESULT WINAPI StgStreamImpl_CopyTo(
710 IStream* pstm, /* [unique][in] */
711 ULARGE_INTEGER cb, /* [in] */
712 ULARGE_INTEGER* pcbRead, /* [out] */
713 ULARGE_INTEGER* pcbWritten) /* [out] */
717 ULONG bytesRead, bytesWritten, copySize;
718 ULARGE_INTEGER totalBytesRead;
719 ULARGE_INTEGER totalBytesWritten;
721 TRACE("(%p, %p, %ld, %p, %p)\n",
722 iface, pstm, cb.s.LowPart, pcbRead, pcbWritten);
728 return STG_E_INVALIDPOINTER;
730 totalBytesRead.s.LowPart = totalBytesRead.s.HighPart = 0;
731 totalBytesWritten.s.LowPart = totalBytesWritten.s.HighPart = 0;
734 * use stack to store data temporarly
735 * there is surely more performant way of doing it, for now this basic
736 * implementation will do the job
738 while ( cb.s.LowPart > 0 )
740 if ( cb.s.LowPart >= 128 )
743 copySize = cb.s.LowPart;
745 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
747 totalBytesRead.s.LowPart += bytesRead;
749 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
751 totalBytesWritten.s.LowPart += bytesWritten;
754 * Check that read & write operations were succesfull
756 if (bytesRead != bytesWritten)
758 hr = STG_E_MEDIUMFULL;
762 if (bytesRead!=copySize)
765 cb.s.LowPart -= bytesRead;
769 * Update number of bytes read and written
773 pcbRead->s.LowPart = totalBytesRead.s.LowPart;
774 pcbRead->s.HighPart = totalBytesRead.s.HighPart;
779 pcbWritten->s.LowPart = totalBytesWritten.s.LowPart;
780 pcbWritten->s.HighPart = totalBytesWritten.s.HighPart;
786 * This method is part of the IStream interface.
788 * For streams contained in structured storages, this method
789 * does nothing. This is what the documentation tells us.
791 * See the documentation of IStream for more info.
793 HRESULT WINAPI StgStreamImpl_Commit(
795 DWORD grfCommitFlags) /* [in] */
801 * This method is part of the IStream interface.
803 * For streams contained in structured storages, this method
804 * does nothing. This is what the documentation tells us.
806 * See the documentation of IStream for more info.
808 HRESULT WINAPI StgStreamImpl_Revert(
814 HRESULT WINAPI StgStreamImpl_LockRegion(
816 ULARGE_INTEGER libOffset, /* [in] */
817 ULARGE_INTEGER cb, /* [in] */
818 DWORD dwLockType) /* [in] */
820 FIXME("not implemented!\n");
824 HRESULT WINAPI StgStreamImpl_UnlockRegion(
826 ULARGE_INTEGER libOffset, /* [in] */
827 ULARGE_INTEGER cb, /* [in] */
828 DWORD dwLockType) /* [in] */
830 FIXME("not implemented!\n");
835 * This method is part of the IStream interface.
837 * This method returns information about the current
840 * See the documentation of IStream for more info.
842 HRESULT WINAPI StgStreamImpl_Stat(
844 STATSTG* pstatstg, /* [out] */
845 DWORD grfStatFlag) /* [in] */
847 StgStreamImpl* const This=(StgStreamImpl*)iface;
849 StgProperty curProperty;
853 * Read the information from the property.
855 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
861 StorageUtl_CopyPropertyToSTATSTG(pstatstg,
865 pstatstg->grfMode = This->grfMode;
873 HRESULT WINAPI StgStreamImpl_Clone(
875 IStream** ppstm) /* [out] */
877 FIXME("not implemented!\n");