Implement OLE object notifications, making sure to cope with the case
[wine/gsoc_dplay.git] / dlls / ole32 / stg_stream.c
blob928028b2597aa59722431a0fa4c155ee871983f7
1 /*
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
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
32 #define COBJMACROS
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
36 #include "windef.h"
37 #include "winbase.h"
38 #include "winerror.h"
39 #include "winreg.h"
40 #include "winternl.h"
41 #include "wine/debug.h"
43 #include "storage32.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(storage);
48 /***
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
53 * be valid anymore.
55 static void StgStreamImpl_Destroy(StgStreamImpl* This)
57 TRACE("(%p)\n", This);
60 * Release the reference we are holding on the parent storage.
62 IStorage_Release((IStorage*)This->parentStorage);
63 This->parentStorage = 0;
66 * Make sure we clean-up the block chain stream objects that we were using.
68 if (This->bigBlockChain != 0)
70 BlockChainStream_Destroy(This->bigBlockChain);
71 This->bigBlockChain = 0;
74 if (This->smallBlockChain != 0)
76 SmallBlockChainStream_Destroy(This->smallBlockChain);
77 This->smallBlockChain = 0;
81 * Finally, free the memory used-up by the class.
83 HeapFree(GetProcessHeap(), 0, This);
86 /***
87 * This implements the IUnknown method QueryInterface for this
88 * class
90 static HRESULT WINAPI StgStreamImpl_QueryInterface(
91 IStream* iface,
92 REFIID riid, /* [in] */
93 void** ppvObject) /* [iid_is][out] */
95 StgStreamImpl* const This=(StgStreamImpl*)iface;
98 * Perform a sanity check on the parameters.
100 if (ppvObject==0)
101 return E_INVALIDARG;
104 * Initialize the return parameter.
106 *ppvObject = 0;
109 * Compare the riid with the interface IDs implemented by this object.
111 if (IsEqualGUID(&IID_IUnknown, riid)||
112 IsEqualGUID(&IID_IStream, riid))
114 *ppvObject = (IStream*)This;
118 * Check that we obtained an interface.
120 if ((*ppvObject)==0)
121 return E_NOINTERFACE;
124 * Query Interface always increases the reference count by one when it is
125 * successful
127 IStream_AddRef(iface);
129 return S_OK;
132 /***
133 * This implements the IUnknown method AddRef for this
134 * class
136 static ULONG WINAPI StgStreamImpl_AddRef(
137 IStream* iface)
139 StgStreamImpl* const This=(StgStreamImpl*)iface;
140 return InterlockedIncrement(&This->ref);
143 /***
144 * This implements the IUnknown method Release for this
145 * class
147 static ULONG WINAPI StgStreamImpl_Release(
148 IStream* iface)
150 StgStreamImpl* const This=(StgStreamImpl*)iface;
152 ULONG ref;
154 ref = InterlockedDecrement(&This->ref);
157 * If the reference count goes down to 0, perform suicide.
159 if (ref==0)
161 StgStreamImpl_Destroy(This);
164 return ref;
167 /***
168 * This method will open the block chain pointed by the property
169 * that describes the stream.
170 * If the stream's size is null, no chain is opened.
172 static void StgStreamImpl_OpenBlockChain(
173 StgStreamImpl* This)
175 StgProperty curProperty;
176 BOOL readSucessful;
179 * Make sure no old object is left over.
181 if (This->smallBlockChain != 0)
183 SmallBlockChainStream_Destroy(This->smallBlockChain);
184 This->smallBlockChain = 0;
187 if (This->bigBlockChain != 0)
189 BlockChainStream_Destroy(This->bigBlockChain);
190 This->bigBlockChain = 0;
194 * Read the information from the property.
196 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
197 This->ownerProperty,
198 &curProperty);
200 if (readSucessful)
202 This->streamSize = curProperty.size;
205 * This code supports only streams that are <32 bits in size.
207 assert(This->streamSize.u.HighPart == 0);
209 if(curProperty.startingBlock == BLOCK_END_OF_CHAIN)
211 assert( (This->streamSize.u.HighPart == 0) && (This->streamSize.u.LowPart == 0) );
213 else
215 if ( (This->streamSize.u.HighPart == 0) &&
216 (This->streamSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
218 This->smallBlockChain = SmallBlockChainStream_Construct(
219 This->parentStorage->ancestorStorage,
220 This->ownerProperty);
222 else
224 This->bigBlockChain = BlockChainStream_Construct(
225 This->parentStorage->ancestorStorage,
226 NULL,
227 This->ownerProperty);
233 /***
234 * This method is part of the ISequentialStream interface.
236 * It reads a block of information from the stream at the current
237 * position. It then moves the current position at the end of the
238 * read block
240 * See the documentation of ISequentialStream for more info.
242 static HRESULT WINAPI StgStreamImpl_Read(
243 IStream* iface,
244 void* pv, /* [length_is][size_is][out] */
245 ULONG cb, /* [in] */
246 ULONG* pcbRead) /* [out] */
248 StgStreamImpl* const This=(StgStreamImpl*)iface;
250 ULONG bytesReadBuffer;
251 ULONG bytesToReadFromBuffer;
252 HRESULT res = S_FALSE;
254 TRACE("(%p, %p, %ld, %p)\n",
255 iface, pv, cb, pcbRead);
258 * If the caller is not interested in the number of bytes read,
259 * we use another buffer to avoid "if" statements in the code.
261 if (pcbRead==0)
262 pcbRead = &bytesReadBuffer;
265 * Using the known size of the stream, calculate the number of bytes
266 * to read from the block chain
268 bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
271 * Depending on the type of chain that was opened when the stream was constructed,
272 * we delegate the work to the method that reads the block chains.
274 if (This->smallBlockChain!=0)
276 SmallBlockChainStream_ReadAt(This->smallBlockChain,
277 This->currentPosition,
278 bytesToReadFromBuffer,
280 pcbRead);
283 else if (This->bigBlockChain!=0)
285 BlockChainStream_ReadAt(This->bigBlockChain,
286 This->currentPosition,
287 bytesToReadFromBuffer,
289 pcbRead);
291 else
294 * Small and big block chains are both NULL. This case will happen
295 * when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
298 *pcbRead = 0;
299 res = S_OK;
300 goto end;
304 * We should always be able to read the proper amount of data from the
305 * chain.
307 assert(bytesToReadFromBuffer == *pcbRead);
310 * Advance the pointer for the number of positions read.
312 This->currentPosition.u.LowPart += *pcbRead;
314 if(*pcbRead != cb)
316 WARN("read %ld instead of the required %ld bytes !\n", *pcbRead, cb);
318 * this used to return S_FALSE, however MSDN docu says that an app should
319 * be prepared to handle error in case of stream end reached, as *some*
320 * implementations *might* return an error (IOW: most do *not*).
321 * As some program fails on returning S_FALSE, I better use S_OK here.
323 res = S_OK;
325 else
326 res = S_OK;
328 end:
329 TRACE("<-- %08lx\n", res);
330 return res;
333 /***
334 * This method is part of the ISequentialStream interface.
336 * It writes a block of information to the stream at the current
337 * position. It then moves the current position at the end of the
338 * written block. If the stream is too small to fit the block,
339 * the stream is grown to fit.
341 * See the documentation of ISequentialStream for more info.
343 static HRESULT WINAPI StgStreamImpl_Write(
344 IStream* iface,
345 const void* pv, /* [size_is][in] */
346 ULONG cb, /* [in] */
347 ULONG* pcbWritten) /* [out] */
349 StgStreamImpl* const This=(StgStreamImpl*)iface;
351 ULARGE_INTEGER newSize;
352 ULONG bytesWritten = 0;
354 TRACE("(%p, %p, %ld, %p)\n",
355 iface, pv, cb, pcbWritten);
358 * Do we have permission to write to this stream?
360 switch(STGM_ACCESS_MODE(This->grfMode))
362 case STGM_WRITE:
363 case STGM_READWRITE:
364 break;
365 default:
366 return STG_E_ACCESSDENIED;
369 if (!pv)
370 return STG_E_INVALIDPOINTER;
373 * If the caller is not interested in the number of bytes written,
374 * we use another buffer to avoid "if" statements in the code.
376 if (pcbWritten == 0)
377 pcbWritten = &bytesWritten;
380 * Initialize the out parameter
382 *pcbWritten = 0;
384 if (cb == 0)
386 return S_OK;
388 else
390 newSize.u.HighPart = 0;
391 newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
395 * Verify if we need to grow the stream
397 if (newSize.u.LowPart > This->streamSize.u.LowPart)
399 /* grow stream */
400 IStream_SetSize(iface, newSize);
404 * Depending on the type of chain that was opened when the stream was constructed,
405 * we delegate the work to the method that readwrites to the block chains.
407 if (This->smallBlockChain!=0)
409 SmallBlockChainStream_WriteAt(This->smallBlockChain,
410 This->currentPosition,
413 pcbWritten);
416 else if (This->bigBlockChain!=0)
418 BlockChainStream_WriteAt(This->bigBlockChain,
419 This->currentPosition,
422 pcbWritten);
424 else
425 assert(FALSE);
428 * Advance the position pointer for the number of positions written.
430 This->currentPosition.u.LowPart += *pcbWritten;
432 return S_OK;
435 /***
436 * This method is part of the IStream interface.
438 * It will move the current stream pointer according to the parameters
439 * given.
441 * See the documentation of IStream for more info.
443 static HRESULT WINAPI StgStreamImpl_Seek(
444 IStream* iface,
445 LARGE_INTEGER dlibMove, /* [in] */
446 DWORD dwOrigin, /* [in] */
447 ULARGE_INTEGER* plibNewPosition) /* [out] */
449 StgStreamImpl* const This=(StgStreamImpl*)iface;
451 ULARGE_INTEGER newPosition;
453 TRACE("(%p, %ld, %ld, %p)\n",
454 iface, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
457 * The caller is allowed to pass in NULL as the new position return value.
458 * If it happens, we assign it to a dynamic variable to avoid special cases
459 * in the code below.
461 if (plibNewPosition == 0)
463 plibNewPosition = &newPosition;
467 * The file pointer is moved depending on the given "function"
468 * parameter.
470 switch (dwOrigin)
472 case STREAM_SEEK_SET:
473 plibNewPosition->u.HighPart = 0;
474 plibNewPosition->u.LowPart = 0;
475 break;
476 case STREAM_SEEK_CUR:
477 *plibNewPosition = This->currentPosition;
478 break;
479 case STREAM_SEEK_END:
480 *plibNewPosition = This->streamSize;
481 break;
482 default:
483 return STG_E_INVALIDFUNCTION;
486 plibNewPosition->QuadPart = RtlLargeIntegerAdd( plibNewPosition->QuadPart, dlibMove.QuadPart );
489 * tell the caller what we calculated
491 This->currentPosition = *plibNewPosition;
493 return S_OK;
496 /***
497 * This method is part of the IStream interface.
499 * It will change the size of a stream.
501 * TODO: Switch from small blocks to big blocks and vice versa.
503 * See the documentation of IStream for more info.
505 static HRESULT WINAPI StgStreamImpl_SetSize(
506 IStream* iface,
507 ULARGE_INTEGER libNewSize) /* [in] */
509 StgStreamImpl* const This=(StgStreamImpl*)iface;
511 StgProperty curProperty;
512 BOOL Success;
514 TRACE("(%p, %ld)\n", iface, libNewSize.u.LowPart);
517 * As documented.
519 if (libNewSize.u.HighPart != 0)
520 return STG_E_INVALIDFUNCTION;
523 * Do we have permission?
525 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
526 return STG_E_ACCESSDENIED;
528 if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
529 return S_OK;
532 * This will happen if we're creating a stream
534 if ((This->smallBlockChain == 0) && (This->bigBlockChain == 0))
536 if (libNewSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
538 This->smallBlockChain = SmallBlockChainStream_Construct(
539 This->parentStorage->ancestorStorage,
540 This->ownerProperty);
542 else
544 This->bigBlockChain = BlockChainStream_Construct(
545 This->parentStorage->ancestorStorage,
546 NULL,
547 This->ownerProperty);
552 * Read this stream's property to see if it's small blocks or big blocks
554 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
555 This->ownerProperty,
556 &curProperty);
558 * Determine if we have to switch from small to big blocks or vice versa
560 if ( (This->smallBlockChain!=0) &&
561 (curProperty.size.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
563 if (libNewSize.u.LowPart >= LIMIT_TO_USE_SMALL_BLOCK)
566 * Transform the small block chain into a big block chain
568 This->bigBlockChain = Storage32Impl_SmallBlocksToBigBlocks(
569 This->parentStorage->ancestorStorage,
570 &This->smallBlockChain);
574 if (This->smallBlockChain!=0)
576 Success = SmallBlockChainStream_SetSize(This->smallBlockChain, libNewSize);
578 else
580 Success = BlockChainStream_SetSize(This->bigBlockChain, libNewSize);
584 * Write the new information about this stream to the property
586 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
587 This->ownerProperty,
588 &curProperty);
590 curProperty.size.u.HighPart = libNewSize.u.HighPart;
591 curProperty.size.u.LowPart = libNewSize.u.LowPart;
593 if (Success)
595 StorageImpl_WriteProperty(This->parentStorage->ancestorStorage,
596 This->ownerProperty,
597 &curProperty);
600 This->streamSize = libNewSize;
602 return S_OK;
605 /***
606 * This method is part of the IStream interface.
608 * It will copy the 'cb' Bytes to 'pstm' IStream.
610 * See the documentation of IStream for more info.
612 static HRESULT WINAPI StgStreamImpl_CopyTo(
613 IStream* iface,
614 IStream* pstm, /* [unique][in] */
615 ULARGE_INTEGER cb, /* [in] */
616 ULARGE_INTEGER* pcbRead, /* [out] */
617 ULARGE_INTEGER* pcbWritten) /* [out] */
619 HRESULT hr = S_OK;
620 BYTE tmpBuffer[128];
621 ULONG bytesRead, bytesWritten, copySize;
622 ULARGE_INTEGER totalBytesRead;
623 ULARGE_INTEGER totalBytesWritten;
625 TRACE("(%p, %p, %ld, %p, %p)\n",
626 iface, pstm, cb.u.LowPart, pcbRead, pcbWritten);
629 * Sanity check
631 if ( pstm == 0 )
632 return STG_E_INVALIDPOINTER;
634 totalBytesRead.u.LowPart = totalBytesRead.u.HighPart = 0;
635 totalBytesWritten.u.LowPart = totalBytesWritten.u.HighPart = 0;
638 * use stack to store data temporarily
639 * there is surely a more performant way of doing it, for now this basic
640 * implementation will do the job
642 while ( cb.u.LowPart > 0 )
644 if ( cb.u.LowPart >= 128 )
645 copySize = 128;
646 else
647 copySize = cb.u.LowPart;
649 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
651 totalBytesRead.u.LowPart += bytesRead;
653 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
655 totalBytesWritten.u.LowPart += bytesWritten;
658 * Check that read & write operations were successful
660 if (bytesRead != bytesWritten)
662 hr = STG_E_MEDIUMFULL;
663 break;
666 if (bytesRead!=copySize)
667 cb.u.LowPart = 0;
668 else
669 cb.u.LowPart -= bytesRead;
673 * Update number of bytes read and written
675 if (pcbRead)
677 pcbRead->u.LowPart = totalBytesRead.u.LowPart;
678 pcbRead->u.HighPart = totalBytesRead.u.HighPart;
681 if (pcbWritten)
683 pcbWritten->u.LowPart = totalBytesWritten.u.LowPart;
684 pcbWritten->u.HighPart = totalBytesWritten.u.HighPart;
686 return hr;
689 /***
690 * This method is part of the IStream interface.
692 * For streams contained in structured storages, this method
693 * does nothing. This is what the documentation tells us.
695 * See the documentation of IStream for more info.
697 static HRESULT WINAPI StgStreamImpl_Commit(
698 IStream* iface,
699 DWORD grfCommitFlags) /* [in] */
701 return S_OK;
704 /***
705 * This method is part of the IStream interface.
707 * For streams contained in structured storages, this method
708 * does nothing. This is what the documentation tells us.
710 * See the documentation of IStream for more info.
712 static HRESULT WINAPI StgStreamImpl_Revert(
713 IStream* iface)
715 return S_OK;
718 static HRESULT WINAPI StgStreamImpl_LockRegion(
719 IStream* iface,
720 ULARGE_INTEGER libOffset, /* [in] */
721 ULARGE_INTEGER cb, /* [in] */
722 DWORD dwLockType) /* [in] */
724 FIXME("not implemented!\n");
725 return E_NOTIMPL;
728 static HRESULT WINAPI StgStreamImpl_UnlockRegion(
729 IStream* iface,
730 ULARGE_INTEGER libOffset, /* [in] */
731 ULARGE_INTEGER cb, /* [in] */
732 DWORD dwLockType) /* [in] */
734 FIXME("not implemented!\n");
735 return E_NOTIMPL;
738 /***
739 * This method is part of the IStream interface.
741 * This method returns information about the current
742 * stream.
744 * See the documentation of IStream for more info.
746 static HRESULT WINAPI StgStreamImpl_Stat(
747 IStream* iface,
748 STATSTG* pstatstg, /* [out] */
749 DWORD grfStatFlag) /* [in] */
751 StgStreamImpl* const This=(StgStreamImpl*)iface;
753 StgProperty curProperty;
754 BOOL readSucessful;
757 * Read the information from the property.
759 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
760 This->ownerProperty,
761 &curProperty);
763 if (readSucessful)
765 StorageUtl_CopyPropertyToSTATSTG(pstatstg,
766 &curProperty,
767 grfStatFlag);
769 pstatstg->grfMode = This->grfMode;
771 return S_OK;
774 return E_FAIL;
777 /***
778 * This method is part of the IStream interface.
780 * This method returns a clone of the interface that allows for
781 * another seek pointer
783 * See the documentation of IStream for more info.
785 * I am not totally sure what I am doing here but I presume that this
786 * should be basically as simple as creating a new stream with the same
787 * parent etc and positioning its seek cursor.
789 static HRESULT WINAPI StgStreamImpl_Clone(
790 IStream* iface,
791 IStream** ppstm) /* [out] */
793 StgStreamImpl* const This=(StgStreamImpl*)iface;
794 HRESULT hres;
795 StgStreamImpl* new_stream;
796 LARGE_INTEGER seek_pos;
799 * Sanity check
801 if ( ppstm == 0 )
802 return STG_E_INVALIDPOINTER;
804 new_stream = StgStreamImpl_Construct (This->parentStorage, This->grfMode, This->ownerProperty);
806 if (!new_stream)
807 return STG_E_INSUFFICIENTMEMORY; /* Currently the only reason for new_stream=0 */
809 *ppstm = (IStream*) new_stream;
810 seek_pos.QuadPart = This->currentPosition.QuadPart;
812 hres=StgStreamImpl_Seek (*ppstm, seek_pos, STREAM_SEEK_SET, NULL);
814 assert (SUCCEEDED(hres));
816 return S_OK;
820 * Virtual function table for the StgStreamImpl class.
822 static const IStreamVtbl StgStreamImpl_Vtbl =
824 StgStreamImpl_QueryInterface,
825 StgStreamImpl_AddRef,
826 StgStreamImpl_Release,
827 StgStreamImpl_Read,
828 StgStreamImpl_Write,
829 StgStreamImpl_Seek,
830 StgStreamImpl_SetSize,
831 StgStreamImpl_CopyTo,
832 StgStreamImpl_Commit,
833 StgStreamImpl_Revert,
834 StgStreamImpl_LockRegion,
835 StgStreamImpl_UnlockRegion,
836 StgStreamImpl_Stat,
837 StgStreamImpl_Clone
840 /******************************************************************************
841 ** StgStreamImpl implementation
844 /***
845 * This is the constructor for the StgStreamImpl class.
847 * Params:
848 * parentStorage - Pointer to the storage that contains the stream to open
849 * ownerProperty - Index of the property that points to this stream.
851 StgStreamImpl* StgStreamImpl_Construct(
852 StorageBaseImpl* parentStorage,
853 DWORD grfMode,
854 ULONG ownerProperty)
856 StgStreamImpl* newStream;
858 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
860 if (newStream!=0)
863 * Set-up the virtual function table and reference count.
865 newStream->lpVtbl = &StgStreamImpl_Vtbl;
866 newStream->ref = 0;
869 * We want to nail-down the reference to the storage in case the
870 * stream out-lives the storage in the client application.
872 newStream->parentStorage = parentStorage;
873 IStorage_AddRef((IStorage*)newStream->parentStorage);
875 newStream->grfMode = grfMode;
876 newStream->ownerProperty = ownerProperty;
879 * Start the stream at the beginning.
881 newStream->currentPosition.u.HighPart = 0;
882 newStream->currentPosition.u.LowPart = 0;
885 * Initialize the rest of the data.
887 newStream->streamSize.u.HighPart = 0;
888 newStream->streamSize.u.LowPart = 0;
889 newStream->bigBlockChain = 0;
890 newStream->smallBlockChain = 0;
893 * Read the size from the property and determine if the blocks forming
894 * this stream are large or small.
896 StgStreamImpl_OpenBlockChain(newStream);
899 return newStream;