Ref count increment/decrement cleanup.
[wine/testsucceed.git] / dlls / ole32 / stg_stream.c
blob6accff0c3d0c42c2603361f7165e197dd3f72165
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 NONAMELESSUNION
33 #define NONAMELESSSTRUCT
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "winreg.h"
38 #include "winternl.h"
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 IStreamVtbl StgStreamImpl_Vtbl =
51 StgStreamImpl_QueryInterface,
52 StgStreamImpl_AddRef,
53 StgStreamImpl_Release,
54 StgStreamImpl_Read,
55 StgStreamImpl_Write,
56 StgStreamImpl_Seek,
57 StgStreamImpl_SetSize,
58 StgStreamImpl_CopyTo,
59 StgStreamImpl_Commit,
60 StgStreamImpl_Revert,
61 StgStreamImpl_LockRegion,
62 StgStreamImpl_UnlockRegion,
63 StgStreamImpl_Stat,
64 StgStreamImpl_Clone
67 /******************************************************************************
68 ** StgStreamImpl implementation
71 /***
72 * This is the constructor for the StgStreamImpl class.
74 * Params:
75 * parentStorage - Pointer to the storage that contains the stream to open
76 * ownerProperty - Index of the property that points to this stream.
78 StgStreamImpl* StgStreamImpl_Construct(
79 StorageBaseImpl* parentStorage,
80 DWORD grfMode,
81 ULONG ownerProperty)
83 StgStreamImpl* newStream;
85 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
87 if (newStream!=0)
90 * Set-up the virtual function table and reference count.
92 newStream->lpVtbl = &StgStreamImpl_Vtbl;
93 newStream->ref = 0;
96 * We want to nail-down the reference to the storage in case the
97 * stream out-lives the storage in the client application.
99 newStream->parentStorage = parentStorage;
100 IStorage_AddRef((IStorage*)newStream->parentStorage);
102 newStream->grfMode = grfMode;
103 newStream->ownerProperty = ownerProperty;
106 * Start the stream at the beginning.
108 newStream->currentPosition.u.HighPart = 0;
109 newStream->currentPosition.u.LowPart = 0;
112 * Initialize the rest of the data.
114 newStream->streamSize.u.HighPart = 0;
115 newStream->streamSize.u.LowPart = 0;
116 newStream->bigBlockChain = 0;
117 newStream->smallBlockChain = 0;
120 * Read the size from the property and determine if the blocks forming
121 * this stream are large or small.
123 StgStreamImpl_OpenBlockChain(newStream);
126 return newStream;
129 /***
130 * This is the destructor of the StgStreamImpl class.
132 * This method will clean-up all the resources used-up by the given StgStreamImpl
133 * class. The pointer passed-in to this function will be freed and will not
134 * be valid anymore.
136 void StgStreamImpl_Destroy(StgStreamImpl* This)
138 TRACE("(%p)\n", This);
141 * Release the reference we are holding on the parent storage.
143 IStorage_Release((IStorage*)This->parentStorage);
144 This->parentStorage = 0;
147 * Make sure we clean-up the block chain stream objects that we were using.
149 if (This->bigBlockChain != 0)
151 BlockChainStream_Destroy(This->bigBlockChain);
152 This->bigBlockChain = 0;
155 if (This->smallBlockChain != 0)
157 SmallBlockChainStream_Destroy(This->smallBlockChain);
158 This->smallBlockChain = 0;
162 * Finally, free the memory used-up by the class.
164 HeapFree(GetProcessHeap(), 0, This);
167 /***
168 * This implements the IUnknown method QueryInterface for this
169 * class
171 HRESULT WINAPI StgStreamImpl_QueryInterface(
172 IStream* iface,
173 REFIID riid, /* [in] */
174 void** ppvObject) /* [iid_is][out] */
176 StgStreamImpl* const This=(StgStreamImpl*)iface;
179 * Perform a sanity check on the parameters.
181 if (ppvObject==0)
182 return E_INVALIDARG;
185 * Initialize the return parameter.
187 *ppvObject = 0;
190 * Compare the riid with the interface IDs implemented by this object.
192 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
194 *ppvObject = (IStream*)This;
196 else if (memcmp(&IID_IStream, riid, sizeof(IID_IStream)) == 0)
198 *ppvObject = (IStream*)This;
202 * Check that we obtained an interface.
204 if ((*ppvObject)==0)
205 return E_NOINTERFACE;
208 * Query Interface always increases the reference count by one when it is
209 * successful
211 StgStreamImpl_AddRef(iface);
213 return S_OK;
216 /***
217 * This implements the IUnknown method AddRef for this
218 * class
220 ULONG WINAPI StgStreamImpl_AddRef(
221 IStream* iface)
223 StgStreamImpl* const This=(StgStreamImpl*)iface;
224 return InterlockedIncrement(&This->ref);
227 /***
228 * This implements the IUnknown method Release for this
229 * class
231 ULONG WINAPI StgStreamImpl_Release(
232 IStream* iface)
234 StgStreamImpl* const This=(StgStreamImpl*)iface;
236 ULONG ref;
238 ref = InterlockedDecrement(&This->ref);
241 * If the reference count goes down to 0, perform suicide.
243 if (ref==0)
245 StgStreamImpl_Destroy(This);
248 return ref;
251 /***
252 * This method will open the block chain pointed by the property
253 * that describes the stream.
254 * If the stream's size is null, no chain is opened.
256 void StgStreamImpl_OpenBlockChain(
257 StgStreamImpl* This)
259 StgProperty curProperty;
260 BOOL readSucessful;
263 * Make sure no old object is left over.
265 if (This->smallBlockChain != 0)
267 SmallBlockChainStream_Destroy(This->smallBlockChain);
268 This->smallBlockChain = 0;
271 if (This->bigBlockChain != 0)
273 BlockChainStream_Destroy(This->bigBlockChain);
274 This->bigBlockChain = 0;
278 * Read the information from the property.
280 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
281 This->ownerProperty,
282 &curProperty);
284 if (readSucessful)
286 This->streamSize = curProperty.size;
289 * This code supports only streams that are <32 bits in size.
291 assert(This->streamSize.u.HighPart == 0);
293 if(curProperty.startingBlock == BLOCK_END_OF_CHAIN)
295 assert( (This->streamSize.u.HighPart == 0) && (This->streamSize.u.LowPart == 0) );
297 else
299 if ( (This->streamSize.u.HighPart == 0) &&
300 (This->streamSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
302 This->smallBlockChain = SmallBlockChainStream_Construct(
303 This->parentStorage->ancestorStorage,
304 This->ownerProperty);
306 else
308 This->bigBlockChain = BlockChainStream_Construct(
309 This->parentStorage->ancestorStorage,
310 NULL,
311 This->ownerProperty);
317 /***
318 * This method is part of the ISequentialStream interface.
320 * It reads a block of information from the stream at the current
321 * position. It then moves the current position at the end of the
322 * read block
324 * See the documentation of ISequentialStream for more info.
326 HRESULT WINAPI StgStreamImpl_Read(
327 IStream* iface,
328 void* pv, /* [length_is][size_is][out] */
329 ULONG cb, /* [in] */
330 ULONG* pcbRead) /* [out] */
332 StgStreamImpl* const This=(StgStreamImpl*)iface;
334 ULONG bytesReadBuffer;
335 ULONG bytesToReadFromBuffer;
336 HRESULT res = S_FALSE;
338 TRACE("(%p, %p, %ld, %p)\n",
339 iface, pv, cb, pcbRead);
342 * If the caller is not interested in the number of bytes read,
343 * we use another buffer to avoid "if" statements in the code.
345 if (pcbRead==0)
346 pcbRead = &bytesReadBuffer;
349 * Using the known size of the stream, calculate the number of bytes
350 * to read from the block chain
352 bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
355 * Depending on the type of chain that was opened when the stream was constructed,
356 * we delegate the work to the method that reads the block chains.
358 if (This->smallBlockChain!=0)
360 SmallBlockChainStream_ReadAt(This->smallBlockChain,
361 This->currentPosition,
362 bytesToReadFromBuffer,
364 pcbRead);
367 else if (This->bigBlockChain!=0)
369 BlockChainStream_ReadAt(This->bigBlockChain,
370 This->currentPosition,
371 bytesToReadFromBuffer,
373 pcbRead);
375 else
378 * Small and big block chains are both NULL. This case will happen
379 * when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
382 *pcbRead = 0;
383 res = S_OK;
384 goto end;
388 * We should always be able to read the proper amount of data from the
389 * chain.
391 assert(bytesToReadFromBuffer == *pcbRead);
394 * Advance the pointer for the number of positions read.
396 This->currentPosition.u.LowPart += *pcbRead;
398 if(*pcbRead != cb)
400 WARN("read %ld instead of the required %ld bytes !\n", *pcbRead, cb);
402 * this used to return S_FALSE, however MSDN docu says that an app should
403 * be prepared to handle error in case of stream end reached, as *some*
404 * implementations *might* return an error (IOW: most do *not*).
405 * As some program fails on returning S_FALSE, I better use S_OK here.
407 res = S_OK;
409 else
410 res = S_OK;
412 end:
413 TRACE("<-- %08lx\n", res);
414 return res;
417 /***
418 * This method is part of the ISequentialStream interface.
420 * It writes a block of information to the stream at the current
421 * position. It then moves the current position at the end of the
422 * written block. If the stream is too small to fit the block,
423 * the stream is grown to fit.
425 * See the documentation of ISequentialStream for more info.
427 HRESULT WINAPI StgStreamImpl_Write(
428 IStream* iface,
429 const void* pv, /* [size_is][in] */
430 ULONG cb, /* [in] */
431 ULONG* pcbWritten) /* [out] */
433 StgStreamImpl* const This=(StgStreamImpl*)iface;
435 ULARGE_INTEGER newSize;
436 ULONG bytesWritten = 0;
438 TRACE("(%p, %p, %ld, %p)\n",
439 iface, pv, cb, pcbWritten);
442 * Do we have permission to write to this stream?
444 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE))) {
445 return STG_E_ACCESSDENIED;
449 * If the caller is not interested in the number of bytes written,
450 * we use another buffer to avoid "if" statements in the code.
452 if (pcbWritten == 0)
453 pcbWritten = &bytesWritten;
456 * Initialize the out parameter
458 *pcbWritten = 0;
460 if (cb == 0)
462 return S_OK;
464 else
466 newSize.u.HighPart = 0;
467 newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
471 * Verify if we need to grow the stream
473 if (newSize.u.LowPart > This->streamSize.u.LowPart)
475 /* grow stream */
476 IStream_SetSize(iface, newSize);
480 * Depending on the type of chain that was opened when the stream was constructed,
481 * we delegate the work to the method that readwrites to the block chains.
483 if (This->smallBlockChain!=0)
485 SmallBlockChainStream_WriteAt(This->smallBlockChain,
486 This->currentPosition,
489 pcbWritten);
492 else if (This->bigBlockChain!=0)
494 BlockChainStream_WriteAt(This->bigBlockChain,
495 This->currentPosition,
498 pcbWritten);
500 else
501 assert(FALSE);
504 * Advance the position pointer for the number of positions written.
506 This->currentPosition.u.LowPart += *pcbWritten;
508 return S_OK;
511 /***
512 * This method is part of the IStream interface.
514 * It will move the current stream pointer according to the parameters
515 * given.
517 * See the documentation of IStream for more info.
519 HRESULT WINAPI StgStreamImpl_Seek(
520 IStream* iface,
521 LARGE_INTEGER dlibMove, /* [in] */
522 DWORD dwOrigin, /* [in] */
523 ULARGE_INTEGER* plibNewPosition) /* [out] */
525 StgStreamImpl* const This=(StgStreamImpl*)iface;
527 ULARGE_INTEGER newPosition;
529 TRACE("(%p, %ld, %ld, %p)\n",
530 iface, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
533 * The caller is allowed to pass in NULL as the new position return value.
534 * If it happens, we assign it to a dynamic variable to avoid special cases
535 * in the code below.
537 if (plibNewPosition == 0)
539 plibNewPosition = &newPosition;
543 * The file pointer is moved depending on the given "function"
544 * parameter.
546 switch (dwOrigin)
548 case STREAM_SEEK_SET:
549 plibNewPosition->u.HighPart = 0;
550 plibNewPosition->u.LowPart = 0;
551 break;
552 case STREAM_SEEK_CUR:
553 *plibNewPosition = This->currentPosition;
554 break;
555 case STREAM_SEEK_END:
556 *plibNewPosition = This->streamSize;
557 break;
558 default:
559 return STG_E_INVALIDFUNCTION;
562 plibNewPosition->QuadPart = RtlLargeIntegerAdd( plibNewPosition->QuadPart, dlibMove.QuadPart );
565 * tell the caller what we calculated
567 This->currentPosition = *plibNewPosition;
569 return S_OK;
572 /***
573 * This method is part of the IStream interface.
575 * It will change the size of a stream.
577 * TODO: Switch from small blocks to big blocks and vice versa.
579 * See the documentation of IStream for more info.
581 HRESULT WINAPI StgStreamImpl_SetSize(
582 IStream* iface,
583 ULARGE_INTEGER libNewSize) /* [in] */
585 StgStreamImpl* const This=(StgStreamImpl*)iface;
587 StgProperty curProperty;
588 BOOL Success;
590 TRACE("(%p, %ld)\n", iface, libNewSize.u.LowPart);
593 * As documented.
595 if (libNewSize.u.HighPart != 0)
596 return STG_E_INVALIDFUNCTION;
599 * Do we have permission?
601 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
602 return STG_E_ACCESSDENIED;
604 if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
605 return S_OK;
608 * This will happen if we're creating a stream
610 if ((This->smallBlockChain == 0) && (This->bigBlockChain == 0))
612 if (libNewSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
614 This->smallBlockChain = SmallBlockChainStream_Construct(
615 This->parentStorage->ancestorStorage,
616 This->ownerProperty);
618 else
620 This->bigBlockChain = BlockChainStream_Construct(
621 This->parentStorage->ancestorStorage,
622 NULL,
623 This->ownerProperty);
628 * Read this stream's property to see if it's small blocks or big blocks
630 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
631 This->ownerProperty,
632 &curProperty);
634 * Determine if we have to switch from small to big blocks or vice versa
636 if ( (This->smallBlockChain!=0) &&
637 (curProperty.size.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
639 if (libNewSize.u.LowPart >= LIMIT_TO_USE_SMALL_BLOCK)
642 * Transform the small block chain into a big block chain
644 This->bigBlockChain = Storage32Impl_SmallBlocksToBigBlocks(
645 This->parentStorage->ancestorStorage,
646 &This->smallBlockChain);
650 if (This->smallBlockChain!=0)
652 Success = SmallBlockChainStream_SetSize(This->smallBlockChain, libNewSize);
654 else
656 Success = BlockChainStream_SetSize(This->bigBlockChain, libNewSize);
660 * Write the new information about this stream to the property
662 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
663 This->ownerProperty,
664 &curProperty);
666 curProperty.size.u.HighPart = libNewSize.u.HighPart;
667 curProperty.size.u.LowPart = libNewSize.u.LowPart;
669 if (Success)
671 StorageImpl_WriteProperty(This->parentStorage->ancestorStorage,
672 This->ownerProperty,
673 &curProperty);
676 This->streamSize = libNewSize;
678 return S_OK;
681 /***
682 * This method is part of the IStream interface.
684 * It will copy the 'cb' Bytes to 'pstm' IStream.
686 * See the documentation of IStream for more info.
688 HRESULT WINAPI StgStreamImpl_CopyTo(
689 IStream* iface,
690 IStream* pstm, /* [unique][in] */
691 ULARGE_INTEGER cb, /* [in] */
692 ULARGE_INTEGER* pcbRead, /* [out] */
693 ULARGE_INTEGER* pcbWritten) /* [out] */
695 HRESULT hr = S_OK;
696 BYTE tmpBuffer[128];
697 ULONG bytesRead, bytesWritten, copySize;
698 ULARGE_INTEGER totalBytesRead;
699 ULARGE_INTEGER totalBytesWritten;
701 TRACE("(%p, %p, %ld, %p, %p)\n",
702 iface, pstm, cb.u.LowPart, pcbRead, pcbWritten);
705 * Sanity check
707 if ( pstm == 0 )
708 return STG_E_INVALIDPOINTER;
710 totalBytesRead.u.LowPart = totalBytesRead.u.HighPart = 0;
711 totalBytesWritten.u.LowPart = totalBytesWritten.u.HighPart = 0;
714 * use stack to store data temporarily
715 * there is surely a more performant way of doing it, for now this basic
716 * implementation will do the job
718 while ( cb.u.LowPart > 0 )
720 if ( cb.u.LowPart >= 128 )
721 copySize = 128;
722 else
723 copySize = cb.u.LowPart;
725 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
727 totalBytesRead.u.LowPart += bytesRead;
729 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
731 totalBytesWritten.u.LowPart += bytesWritten;
734 * Check that read & write operations were successful
736 if (bytesRead != bytesWritten)
738 hr = STG_E_MEDIUMFULL;
739 break;
742 if (bytesRead!=copySize)
743 cb.u.LowPart = 0;
744 else
745 cb.u.LowPart -= bytesRead;
749 * Update number of bytes read and written
751 if (pcbRead)
753 pcbRead->u.LowPart = totalBytesRead.u.LowPart;
754 pcbRead->u.HighPart = totalBytesRead.u.HighPart;
757 if (pcbWritten)
759 pcbWritten->u.LowPart = totalBytesWritten.u.LowPart;
760 pcbWritten->u.HighPart = totalBytesWritten.u.HighPart;
762 return hr;
765 /***
766 * This method is part of the IStream interface.
768 * For streams contained in structured storages, this method
769 * does nothing. This is what the documentation tells us.
771 * See the documentation of IStream for more info.
773 HRESULT WINAPI StgStreamImpl_Commit(
774 IStream* iface,
775 DWORD grfCommitFlags) /* [in] */
777 return S_OK;
780 /***
781 * This method is part of the IStream interface.
783 * For streams contained in structured storages, this method
784 * does nothing. This is what the documentation tells us.
786 * See the documentation of IStream for more info.
788 HRESULT WINAPI StgStreamImpl_Revert(
789 IStream* iface)
791 return S_OK;
794 HRESULT WINAPI StgStreamImpl_LockRegion(
795 IStream* iface,
796 ULARGE_INTEGER libOffset, /* [in] */
797 ULARGE_INTEGER cb, /* [in] */
798 DWORD dwLockType) /* [in] */
800 FIXME("not implemented!\n");
801 return E_NOTIMPL;
804 HRESULT WINAPI StgStreamImpl_UnlockRegion(
805 IStream* iface,
806 ULARGE_INTEGER libOffset, /* [in] */
807 ULARGE_INTEGER cb, /* [in] */
808 DWORD dwLockType) /* [in] */
810 FIXME("not implemented!\n");
811 return E_NOTIMPL;
814 /***
815 * This method is part of the IStream interface.
817 * This method returns information about the current
818 * stream.
820 * See the documentation of IStream for more info.
822 HRESULT WINAPI StgStreamImpl_Stat(
823 IStream* iface,
824 STATSTG* pstatstg, /* [out] */
825 DWORD grfStatFlag) /* [in] */
827 StgStreamImpl* const This=(StgStreamImpl*)iface;
829 StgProperty curProperty;
830 BOOL readSucessful;
833 * Read the information from the property.
835 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
836 This->ownerProperty,
837 &curProperty);
839 if (readSucessful)
841 StorageUtl_CopyPropertyToSTATSTG(pstatstg,
842 &curProperty,
843 grfStatFlag);
845 pstatstg->grfMode = This->grfMode;
847 return S_OK;
850 return E_FAIL;
853 /***
854 * This method is part of the IStream interface.
856 * This method returns a clone of the interface that allows for
857 * another seek pointer
859 * See the documentation of IStream for more info.
861 * I am not totally sure what I am doing here but I presume that this
862 * should be basically as simple as creating a new stream with the same
863 * parent etc and positioning its seek cursor.
865 HRESULT WINAPI StgStreamImpl_Clone(
866 IStream* iface,
867 IStream** ppstm) /* [out] */
869 StgStreamImpl* const This=(StgStreamImpl*)iface;
870 HRESULT hres;
871 StgStreamImpl* new_stream;
872 LARGE_INTEGER seek_pos;
875 * Sanity check
877 if ( ppstm == 0 )
878 return STG_E_INVALIDPOINTER;
880 new_stream = StgStreamImpl_Construct (This->parentStorage, This->grfMode, This->ownerProperty);
882 if (!new_stream)
883 return STG_E_INSUFFICIENTMEMORY; /* Currently the only reason for new_stream=0 */
885 *ppstm = (IStream*) new_stream;
886 seek_pos.QuadPart = This->currentPosition.QuadPart;
888 hres=StgStreamImpl_Seek (*ppstm, seek_pos, STREAM_SEEK_SET, NULL);
890 assert (SUCCEEDED(hres));
892 return S_OK;