Add a handle for urlmon.dll, fix MSVC warning.
[wine/wine64.git] / dlls / ole32 / stg_stream.c
bloba82d4b98bd1a72c7481e06cc003ca118ab11cf3d
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 <stdio.h>
29 #include <string.h>
31 #define NONAMELESSUNION
32 #define NONAMELESSSTRUCT
33 #include "winbase.h"
34 #include "winerror.h"
35 #include "winternl.h"
36 #include "wine/debug.h"
38 #include "storage32.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(storage);
44 * Virtual function table for the StgStreamImpl class.
46 static ICOM_VTABLE(IStream) StgStreamImpl_Vtbl =
48 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
49 StgStreamImpl_QueryInterface,
50 StgStreamImpl_AddRef,
51 StgStreamImpl_Release,
52 StgStreamImpl_Read,
53 StgStreamImpl_Write,
54 StgStreamImpl_Seek,
55 StgStreamImpl_SetSize,
56 StgStreamImpl_CopyTo,
57 StgStreamImpl_Commit,
58 StgStreamImpl_Revert,
59 StgStreamImpl_LockRegion,
60 StgStreamImpl_UnlockRegion,
61 StgStreamImpl_Stat,
62 StgStreamImpl_Clone
65 /******************************************************************************
66 ** StgStreamImpl implementation
69 /***
70 * This is the constructor for the StgStreamImpl class.
72 * Params:
73 * parentStorage - Pointer to the storage that contains the stream to open
74 * ownerProperty - Index of the property that points to this stream.
76 StgStreamImpl* StgStreamImpl_Construct(
77 StorageBaseImpl* parentStorage,
78 DWORD grfMode,
79 ULONG ownerProperty)
81 StgStreamImpl* newStream;
83 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
85 if (newStream!=0)
88 * Set-up the virtual function table and reference count.
90 newStream->lpVtbl = &StgStreamImpl_Vtbl;
91 newStream->ref = 0;
94 * We want to nail-down the reference to the storage in case the
95 * stream out-lives the storage in the client application.
97 newStream->parentStorage = parentStorage;
98 IStorage_AddRef((IStorage*)newStream->parentStorage);
100 newStream->grfMode = grfMode;
101 newStream->ownerProperty = ownerProperty;
104 * Start the stream at the beginning.
106 newStream->currentPosition.s.HighPart = 0;
107 newStream->currentPosition.s.LowPart = 0;
110 * Initialize the rest of the data.
112 newStream->streamSize.s.HighPart = 0;
113 newStream->streamSize.s.LowPart = 0;
114 newStream->bigBlockChain = 0;
115 newStream->smallBlockChain = 0;
118 * Read the size from the property and determine if the blocks forming
119 * this stream are large or small.
121 StgStreamImpl_OpenBlockChain(newStream);
124 return newStream;
127 /***
128 * This is the destructor of the StgStreamImpl class.
130 * This method will clean-up all the resources used-up by the given StgStreamImpl
131 * class. The pointer passed-in to this function will be freed and will not
132 * be valid anymore.
134 void StgStreamImpl_Destroy(StgStreamImpl* This)
136 TRACE("(%p)\n", This);
139 * Release the reference we are holding on the parent storage.
141 IStorage_Release((IStorage*)This->parentStorage);
142 This->parentStorage = 0;
145 * Make sure we clean-up the block chain stream objects that we were using.
147 if (This->bigBlockChain != 0)
149 BlockChainStream_Destroy(This->bigBlockChain);
150 This->bigBlockChain = 0;
153 if (This->smallBlockChain != 0)
155 SmallBlockChainStream_Destroy(This->smallBlockChain);
156 This->smallBlockChain = 0;
160 * Finally, free the memory used-up by the class.
162 HeapFree(GetProcessHeap(), 0, This);
165 /***
166 * This implements the IUnknown method QueryInterface for this
167 * class
169 HRESULT WINAPI StgStreamImpl_QueryInterface(
170 IStream* iface,
171 REFIID riid, /* [in] */
172 void** ppvObject) /* [iid_is][out] */
174 StgStreamImpl* const This=(StgStreamImpl*)iface;
177 * Perform a sanity check on the parameters.
179 if (ppvObject==0)
180 return E_INVALIDARG;
183 * Initialize the return parameter.
185 *ppvObject = 0;
188 * Compare the riid with the interface IDs implemented by this object.
190 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
192 *ppvObject = (IStream*)This;
194 else if (memcmp(&IID_IStream, riid, sizeof(IID_IStream)) == 0)
196 *ppvObject = (IStream*)This;
200 * Check that we obtained an interface.
202 if ((*ppvObject)==0)
203 return E_NOINTERFACE;
206 * Query Interface always increases the reference count by one when it is
207 * successful
209 StgStreamImpl_AddRef(iface);
211 return S_OK;
214 /***
215 * This implements the IUnknown method AddRef for this
216 * class
218 ULONG WINAPI StgStreamImpl_AddRef(
219 IStream* iface)
221 StgStreamImpl* const This=(StgStreamImpl*)iface;
223 This->ref++;
225 return This->ref;
228 /***
229 * This implements the IUnknown method Release for this
230 * class
232 ULONG WINAPI StgStreamImpl_Release(
233 IStream* iface)
235 StgStreamImpl* const This=(StgStreamImpl*)iface;
237 ULONG newRef;
239 This->ref--;
241 newRef = This->ref;
244 * If the reference count goes down to 0, perform suicide.
246 if (newRef==0)
248 StgStreamImpl_Destroy(This);
251 return newRef;
254 /***
255 * This method will open the block chain pointed by the property
256 * that describes the stream.
257 * If the stream's size is null, no chain is opened.
259 void StgStreamImpl_OpenBlockChain(
260 StgStreamImpl* This)
262 StgProperty curProperty;
263 BOOL readSucessful;
266 * Make sure no old object is left over.
268 if (This->smallBlockChain != 0)
270 SmallBlockChainStream_Destroy(This->smallBlockChain);
271 This->smallBlockChain = 0;
274 if (This->bigBlockChain != 0)
276 BlockChainStream_Destroy(This->bigBlockChain);
277 This->bigBlockChain = 0;
281 * Read the information from the property.
283 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
284 This->ownerProperty,
285 &curProperty);
287 if (readSucessful)
289 This->streamSize = curProperty.size;
292 * This code supports only streams that are <32 bits in size.
294 assert(This->streamSize.s.HighPart == 0);
296 if(curProperty.startingBlock == BLOCK_END_OF_CHAIN)
298 assert( (This->streamSize.s.HighPart == 0) && (This->streamSize.s.LowPart == 0) );
300 else
302 if ( (This->streamSize.s.HighPart == 0) &&
303 (This->streamSize.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
305 This->smallBlockChain = SmallBlockChainStream_Construct(
306 This->parentStorage->ancestorStorage,
307 This->ownerProperty);
309 else
311 This->bigBlockChain = BlockChainStream_Construct(
312 This->parentStorage->ancestorStorage,
313 NULL,
314 This->ownerProperty);
320 /***
321 * This method is part of the ISequentialStream interface.
323 * It reads a block of information from the stream at the current
324 * position. It then moves the current position at the end of the
325 * read block
327 * See the documentation of ISequentialStream for more info.
329 HRESULT WINAPI StgStreamImpl_Read(
330 IStream* iface,
331 void* pv, /* [length_is][size_is][out] */
332 ULONG cb, /* [in] */
333 ULONG* pcbRead) /* [out] */
335 StgStreamImpl* const This=(StgStreamImpl*)iface;
337 ULONG bytesReadBuffer;
338 ULONG bytesToReadFromBuffer;
339 HRESULT res = S_FALSE;
341 TRACE("(%p, %p, %ld, %p)\n",
342 iface, pv, cb, pcbRead);
345 * If the caller is not interested in the number of bytes read,
346 * we use another buffer to avoid "if" statements in the code.
348 if (pcbRead==0)
349 pcbRead = &bytesReadBuffer;
352 * Using the known size of the stream, calculate the number of bytes
353 * to read from the block chain
355 bytesToReadFromBuffer = min( This->streamSize.s.LowPart - This->currentPosition.s.LowPart, cb);
358 * Depending on the type of chain that was opened when the stream was constructed,
359 * we delegate the work to the method that reads the block chains.
361 if (This->smallBlockChain!=0)
363 SmallBlockChainStream_ReadAt(This->smallBlockChain,
364 This->currentPosition,
365 bytesToReadFromBuffer,
367 pcbRead);
370 else if (This->bigBlockChain!=0)
372 BlockChainStream_ReadAt(This->bigBlockChain,
373 This->currentPosition,
374 bytesToReadFromBuffer,
376 pcbRead);
378 else
381 * Small and big block chains are both NULL. This case will happen
382 * when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
385 *pcbRead = 0;
386 res = S_OK;
387 goto end;
391 * We should always be able to read the proper amount of data from the
392 * chain.
394 assert(bytesToReadFromBuffer == *pcbRead);
397 * Advance the pointer for the number of positions read.
399 This->currentPosition.s.LowPart += *pcbRead;
401 if(*pcbRead != cb)
403 WARN("read %ld instead of the required %ld bytes !\n", *pcbRead, cb);
405 * this used to return S_FALSE, however MSDN docu says that an app should
406 * be prepared to handle error in case of stream end reached, as *some*
407 * implementations *might* return an error (IOW: most do *not*).
408 * As some program fails on returning S_FALSE, I better use S_OK here.
410 res = S_OK;
412 else
413 res = S_OK;
415 end:
416 TRACE("<-- %08lx\n", res);
417 return res;
420 /***
421 * This method is part of the ISequentialStream interface.
423 * It writes a block of information to the stream at the current
424 * position. It then moves the current position at the end of the
425 * written block. If the stream is too small to fit the block,
426 * the stream is grown to fit.
428 * See the documentation of ISequentialStream for more info.
430 HRESULT WINAPI StgStreamImpl_Write(
431 IStream* iface,
432 const void* pv, /* [size_is][in] */
433 ULONG cb, /* [in] */
434 ULONG* pcbWritten) /* [out] */
436 StgStreamImpl* const This=(StgStreamImpl*)iface;
438 ULARGE_INTEGER newSize;
439 ULONG bytesWritten = 0;
441 TRACE("(%p, %p, %ld, %p)\n",
442 iface, pv, cb, pcbWritten);
445 * Do we have permission to write to this stream?
447 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE))) {
448 return STG_E_ACCESSDENIED;
452 * If the caller is not interested in the number of bytes written,
453 * we use another buffer to avoid "if" statements in the code.
455 if (pcbWritten == 0)
456 pcbWritten = &bytesWritten;
459 * Initialize the out parameter
461 *pcbWritten = 0;
463 if (cb == 0)
465 return S_OK;
467 else
469 newSize.s.HighPart = 0;
470 newSize.s.LowPart = This->currentPosition.s.LowPart + cb;
474 * Verify if we need to grow the stream
476 if (newSize.s.LowPart > This->streamSize.s.LowPart)
478 /* grow stream */
479 IStream_SetSize(iface, newSize);
483 * Depending on the type of chain that was opened when the stream was constructed,
484 * we delegate the work to the method that readwrites to the block chains.
486 if (This->smallBlockChain!=0)
488 SmallBlockChainStream_WriteAt(This->smallBlockChain,
489 This->currentPosition,
492 pcbWritten);
495 else if (This->bigBlockChain!=0)
497 BlockChainStream_WriteAt(This->bigBlockChain,
498 This->currentPosition,
501 pcbWritten);
503 else
504 assert(FALSE);
507 * Advance the position pointer for the number of positions written.
509 This->currentPosition.s.LowPart += *pcbWritten;
511 return S_OK;
514 /***
515 * This method is part of the IStream interface.
517 * It will move the current stream pointer according to the parameters
518 * given.
520 * See the documentation of IStream for more info.
522 HRESULT WINAPI StgStreamImpl_Seek(
523 IStream* iface,
524 LARGE_INTEGER dlibMove, /* [in] */
525 DWORD dwOrigin, /* [in] */
526 ULARGE_INTEGER* plibNewPosition) /* [out] */
528 StgStreamImpl* const This=(StgStreamImpl*)iface;
530 ULARGE_INTEGER newPosition;
532 TRACE("(%p, %ld, %ld, %p)\n",
533 iface, dlibMove.s.LowPart, dwOrigin, plibNewPosition);
536 * The caller is allowed to pass in NULL as the new position return value.
537 * If it happens, we assign it to a dynamic variable to avoid special cases
538 * in the code below.
540 if (plibNewPosition == 0)
542 plibNewPosition = &newPosition;
546 * The file pointer is moved depending on the given "function"
547 * parameter.
549 switch (dwOrigin)
551 case STREAM_SEEK_SET:
552 plibNewPosition->s.HighPart = 0;
553 plibNewPosition->s.LowPart = 0;
554 break;
555 case STREAM_SEEK_CUR:
556 *plibNewPosition = This->currentPosition;
557 break;
558 case STREAM_SEEK_END:
559 *plibNewPosition = This->streamSize;
560 break;
561 default:
562 return STG_E_INVALIDFUNCTION;
565 plibNewPosition->QuadPart = RtlLargeIntegerAdd( plibNewPosition->QuadPart, dlibMove.QuadPart );
568 * tell the caller what we calculated
570 This->currentPosition = *plibNewPosition;
572 return S_OK;
575 /***
576 * This method is part of the IStream interface.
578 * It will change the size of a stream.
580 * TODO: Switch from small blocks to big blocks and vice versa.
582 * See the documentation of IStream for more info.
584 HRESULT WINAPI StgStreamImpl_SetSize(
585 IStream* iface,
586 ULARGE_INTEGER libNewSize) /* [in] */
588 StgStreamImpl* const This=(StgStreamImpl*)iface;
590 StgProperty curProperty;
591 BOOL Success;
593 TRACE("(%p, %ld)\n", iface, libNewSize.s.LowPart);
596 * As documented.
598 if (libNewSize.s.HighPart != 0)
599 return STG_E_INVALIDFUNCTION;
602 * Do we have permission?
604 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
605 return STG_E_ACCESSDENIED;
607 if (This->streamSize.s.LowPart == libNewSize.s.LowPart)
608 return S_OK;
611 * This will happen if we're creating a stream
613 if ((This->smallBlockChain == 0) && (This->bigBlockChain == 0))
615 if (libNewSize.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
617 This->smallBlockChain = SmallBlockChainStream_Construct(
618 This->parentStorage->ancestorStorage,
619 This->ownerProperty);
621 else
623 This->bigBlockChain = BlockChainStream_Construct(
624 This->parentStorage->ancestorStorage,
625 NULL,
626 This->ownerProperty);
631 * Read this stream's property to see if it's small blocks or big blocks
633 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
634 This->ownerProperty,
635 &curProperty);
637 * Determine if we have to switch from small to big blocks or vice versa
639 if ( (This->smallBlockChain!=0) &&
640 (curProperty.size.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
642 if (libNewSize.s.LowPart >= LIMIT_TO_USE_SMALL_BLOCK)
645 * Transform the small block chain into a big block chain
647 This->bigBlockChain = Storage32Impl_SmallBlocksToBigBlocks(
648 This->parentStorage->ancestorStorage,
649 &This->smallBlockChain);
653 if (This->smallBlockChain!=0)
655 Success = SmallBlockChainStream_SetSize(This->smallBlockChain, libNewSize);
657 else
659 Success = BlockChainStream_SetSize(This->bigBlockChain, libNewSize);
663 * Write the new information about this stream to the property
665 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
666 This->ownerProperty,
667 &curProperty);
669 curProperty.size.s.HighPart = libNewSize.s.HighPart;
670 curProperty.size.s.LowPart = libNewSize.s.LowPart;
672 if (Success)
674 StorageImpl_WriteProperty(This->parentStorage->ancestorStorage,
675 This->ownerProperty,
676 &curProperty);
679 This->streamSize = libNewSize;
681 return S_OK;
684 /***
685 * This method is part of the IStream interface.
687 * It will copy the 'cb' Bytes to 'pstm' IStream.
689 * See the documentation of IStream for more info.
691 HRESULT WINAPI StgStreamImpl_CopyTo(
692 IStream* iface,
693 IStream* pstm, /* [unique][in] */
694 ULARGE_INTEGER cb, /* [in] */
695 ULARGE_INTEGER* pcbRead, /* [out] */
696 ULARGE_INTEGER* pcbWritten) /* [out] */
698 HRESULT hr = S_OK;
699 BYTE tmpBuffer[128];
700 ULONG bytesRead, bytesWritten, copySize;
701 ULARGE_INTEGER totalBytesRead;
702 ULARGE_INTEGER totalBytesWritten;
704 TRACE("(%p, %p, %ld, %p, %p)\n",
705 iface, pstm, cb.s.LowPart, pcbRead, pcbWritten);
708 * Sanity check
710 if ( pstm == 0 )
711 return STG_E_INVALIDPOINTER;
713 totalBytesRead.s.LowPart = totalBytesRead.s.HighPart = 0;
714 totalBytesWritten.s.LowPart = totalBytesWritten.s.HighPart = 0;
717 * use stack to store data temporarily
718 * there is surely a more performant way of doing it, for now this basic
719 * implementation will do the job
721 while ( cb.s.LowPart > 0 )
723 if ( cb.s.LowPart >= 128 )
724 copySize = 128;
725 else
726 copySize = cb.s.LowPart;
728 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
730 totalBytesRead.s.LowPart += bytesRead;
732 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
734 totalBytesWritten.s.LowPart += bytesWritten;
737 * Check that read & write operations were successful
739 if (bytesRead != bytesWritten)
741 hr = STG_E_MEDIUMFULL;
742 break;
745 if (bytesRead!=copySize)
746 cb.s.LowPart = 0;
747 else
748 cb.s.LowPart -= bytesRead;
752 * Update number of bytes read and written
754 if (pcbRead)
756 pcbRead->s.LowPart = totalBytesRead.s.LowPart;
757 pcbRead->s.HighPart = totalBytesRead.s.HighPart;
760 if (pcbWritten)
762 pcbWritten->s.LowPart = totalBytesWritten.s.LowPart;
763 pcbWritten->s.HighPart = totalBytesWritten.s.HighPart;
765 return hr;
768 /***
769 * This method is part of the IStream interface.
771 * For streams contained in structured storages, this method
772 * does nothing. This is what the documentation tells us.
774 * See the documentation of IStream for more info.
776 HRESULT WINAPI StgStreamImpl_Commit(
777 IStream* iface,
778 DWORD grfCommitFlags) /* [in] */
780 return S_OK;
783 /***
784 * This method is part of the IStream interface.
786 * For streams contained in structured storages, this method
787 * does nothing. This is what the documentation tells us.
789 * See the documentation of IStream for more info.
791 HRESULT WINAPI StgStreamImpl_Revert(
792 IStream* iface)
794 return S_OK;
797 HRESULT WINAPI StgStreamImpl_LockRegion(
798 IStream* iface,
799 ULARGE_INTEGER libOffset, /* [in] */
800 ULARGE_INTEGER cb, /* [in] */
801 DWORD dwLockType) /* [in] */
803 FIXME("not implemented!\n");
804 return E_NOTIMPL;
807 HRESULT WINAPI StgStreamImpl_UnlockRegion(
808 IStream* iface,
809 ULARGE_INTEGER libOffset, /* [in] */
810 ULARGE_INTEGER cb, /* [in] */
811 DWORD dwLockType) /* [in] */
813 FIXME("not implemented!\n");
814 return E_NOTIMPL;
817 /***
818 * This method is part of the IStream interface.
820 * This method returns information about the current
821 * stream.
823 * See the documentation of IStream for more info.
825 HRESULT WINAPI StgStreamImpl_Stat(
826 IStream* iface,
827 STATSTG* pstatstg, /* [out] */
828 DWORD grfStatFlag) /* [in] */
830 StgStreamImpl* const This=(StgStreamImpl*)iface;
832 StgProperty curProperty;
833 BOOL readSucessful;
836 * Read the information from the property.
838 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
839 This->ownerProperty,
840 &curProperty);
842 if (readSucessful)
844 StorageUtl_CopyPropertyToSTATSTG(pstatstg,
845 &curProperty,
846 grfStatFlag);
848 pstatstg->grfMode = This->grfMode;
850 return S_OK;
853 return E_FAIL;
856 /***
857 * This method is part of the IStream interface.
859 * This method returns a clone of the interface that allows for
860 * another seek pointer
862 * See the documentation of IStream for more info.
864 * I am not totally sure what I am doing here but I presume that this
865 * should be basically as simple as creating a new stream with the same
866 * parent etc and positioning its seek cursor.
868 HRESULT WINAPI StgStreamImpl_Clone(
869 IStream* iface,
870 IStream** ppstm) /* [out] */
872 StgStreamImpl* const This=(StgStreamImpl*)iface;
873 HRESULT hres;
874 StgStreamImpl* new_stream;
875 LARGE_INTEGER seek_pos;
878 * Sanity check
880 if ( ppstm == 0 )
881 return STG_E_INVALIDPOINTER;
883 new_stream = StgStreamImpl_Construct (This->parentStorage, This->grfMode, This->ownerProperty);
885 if (!new_stream)
886 return STG_E_INSUFFICIENTMEMORY; /* Currently the only reason for new_stream=0 */
888 *ppstm = (IStream*) new_stream;
889 seek_pos.QuadPart = This->currentPosition.QuadPart;
891 hres=StgStreamImpl_Seek (*ppstm, seek_pos, STREAM_SEEK_SET, NULL);
893 assert (SUCCEEDED(hres));
895 return S_OK;