Fixed header dependencies to be fully compatible with the Windows
[wine/multimedia.git] / dlls / ole32 / stg_stream.c
blob2a5c9d192e2382b4be66f0ffc71f057533744ea6
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 ICOM_VTABLE(IStream) StgStreamImpl_Vtbl =
51 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
52 StgStreamImpl_QueryInterface,
53 StgStreamImpl_AddRef,
54 StgStreamImpl_Release,
55 StgStreamImpl_Read,
56 StgStreamImpl_Write,
57 StgStreamImpl_Seek,
58 StgStreamImpl_SetSize,
59 StgStreamImpl_CopyTo,
60 StgStreamImpl_Commit,
61 StgStreamImpl_Revert,
62 StgStreamImpl_LockRegion,
63 StgStreamImpl_UnlockRegion,
64 StgStreamImpl_Stat,
65 StgStreamImpl_Clone
68 /******************************************************************************
69 ** StgStreamImpl implementation
72 /***
73 * This is the constructor for the StgStreamImpl class.
75 * Params:
76 * parentStorage - Pointer to the storage that contains the stream to open
77 * ownerProperty - Index of the property that points to this stream.
79 StgStreamImpl* StgStreamImpl_Construct(
80 StorageBaseImpl* parentStorage,
81 DWORD grfMode,
82 ULONG ownerProperty)
84 StgStreamImpl* newStream;
86 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
88 if (newStream!=0)
91 * Set-up the virtual function table and reference count.
93 newStream->lpVtbl = &StgStreamImpl_Vtbl;
94 newStream->ref = 0;
97 * We want to nail-down the reference to the storage in case the
98 * stream out-lives the storage in the client application.
100 newStream->parentStorage = parentStorage;
101 IStorage_AddRef((IStorage*)newStream->parentStorage);
103 newStream->grfMode = grfMode;
104 newStream->ownerProperty = ownerProperty;
107 * Start the stream at the beginning.
109 newStream->currentPosition.s.HighPart = 0;
110 newStream->currentPosition.s.LowPart = 0;
113 * Initialize the rest of the data.
115 newStream->streamSize.s.HighPart = 0;
116 newStream->streamSize.s.LowPart = 0;
117 newStream->bigBlockChain = 0;
118 newStream->smallBlockChain = 0;
121 * Read the size from the property and determine if the blocks forming
122 * this stream are large or small.
124 StgStreamImpl_OpenBlockChain(newStream);
127 return newStream;
130 /***
131 * This is the destructor of the StgStreamImpl class.
133 * This method will clean-up all the resources used-up by the given StgStreamImpl
134 * class. The pointer passed-in to this function will be freed and will not
135 * be valid anymore.
137 void StgStreamImpl_Destroy(StgStreamImpl* This)
139 TRACE("(%p)\n", This);
142 * Release the reference we are holding on the parent storage.
144 IStorage_Release((IStorage*)This->parentStorage);
145 This->parentStorage = 0;
148 * Make sure we clean-up the block chain stream objects that we were using.
150 if (This->bigBlockChain != 0)
152 BlockChainStream_Destroy(This->bigBlockChain);
153 This->bigBlockChain = 0;
156 if (This->smallBlockChain != 0)
158 SmallBlockChainStream_Destroy(This->smallBlockChain);
159 This->smallBlockChain = 0;
163 * Finally, free the memory used-up by the class.
165 HeapFree(GetProcessHeap(), 0, This);
168 /***
169 * This implements the IUnknown method QueryInterface for this
170 * class
172 HRESULT WINAPI StgStreamImpl_QueryInterface(
173 IStream* iface,
174 REFIID riid, /* [in] */
175 void** ppvObject) /* [iid_is][out] */
177 StgStreamImpl* const This=(StgStreamImpl*)iface;
180 * Perform a sanity check on the parameters.
182 if (ppvObject==0)
183 return E_INVALIDARG;
186 * Initialize the return parameter.
188 *ppvObject = 0;
191 * Compare the riid with the interface IDs implemented by this object.
193 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
195 *ppvObject = (IStream*)This;
197 else if (memcmp(&IID_IStream, riid, sizeof(IID_IStream)) == 0)
199 *ppvObject = (IStream*)This;
203 * Check that we obtained an interface.
205 if ((*ppvObject)==0)
206 return E_NOINTERFACE;
209 * Query Interface always increases the reference count by one when it is
210 * successful
212 StgStreamImpl_AddRef(iface);
214 return S_OK;
217 /***
218 * This implements the IUnknown method AddRef for this
219 * class
221 ULONG WINAPI StgStreamImpl_AddRef(
222 IStream* iface)
224 StgStreamImpl* const This=(StgStreamImpl*)iface;
226 This->ref++;
228 return This->ref;
231 /***
232 * This implements the IUnknown method Release for this
233 * class
235 ULONG WINAPI StgStreamImpl_Release(
236 IStream* iface)
238 StgStreamImpl* const This=(StgStreamImpl*)iface;
240 ULONG newRef;
242 This->ref--;
244 newRef = This->ref;
247 * If the reference count goes down to 0, perform suicide.
249 if (newRef==0)
251 StgStreamImpl_Destroy(This);
254 return newRef;
257 /***
258 * This method will open the block chain pointed by the property
259 * that describes the stream.
260 * If the stream's size is null, no chain is opened.
262 void StgStreamImpl_OpenBlockChain(
263 StgStreamImpl* This)
265 StgProperty curProperty;
266 BOOL readSucessful;
269 * Make sure no old object is left over.
271 if (This->smallBlockChain != 0)
273 SmallBlockChainStream_Destroy(This->smallBlockChain);
274 This->smallBlockChain = 0;
277 if (This->bigBlockChain != 0)
279 BlockChainStream_Destroy(This->bigBlockChain);
280 This->bigBlockChain = 0;
284 * Read the information from the property.
286 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
287 This->ownerProperty,
288 &curProperty);
290 if (readSucessful)
292 This->streamSize = curProperty.size;
295 * This code supports only streams that are <32 bits in size.
297 assert(This->streamSize.s.HighPart == 0);
299 if(curProperty.startingBlock == BLOCK_END_OF_CHAIN)
301 assert( (This->streamSize.s.HighPart == 0) && (This->streamSize.s.LowPart == 0) );
303 else
305 if ( (This->streamSize.s.HighPart == 0) &&
306 (This->streamSize.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
308 This->smallBlockChain = SmallBlockChainStream_Construct(
309 This->parentStorage->ancestorStorage,
310 This->ownerProperty);
312 else
314 This->bigBlockChain = BlockChainStream_Construct(
315 This->parentStorage->ancestorStorage,
316 NULL,
317 This->ownerProperty);
323 /***
324 * This method is part of the ISequentialStream interface.
326 * It reads a block of information from the stream at the current
327 * position. It then moves the current position at the end of the
328 * read block
330 * See the documentation of ISequentialStream for more info.
332 HRESULT WINAPI StgStreamImpl_Read(
333 IStream* iface,
334 void* pv, /* [length_is][size_is][out] */
335 ULONG cb, /* [in] */
336 ULONG* pcbRead) /* [out] */
338 StgStreamImpl* const This=(StgStreamImpl*)iface;
340 ULONG bytesReadBuffer;
341 ULONG bytesToReadFromBuffer;
342 HRESULT res = S_FALSE;
344 TRACE("(%p, %p, %ld, %p)\n",
345 iface, pv, cb, pcbRead);
348 * If the caller is not interested in the number of bytes read,
349 * we use another buffer to avoid "if" statements in the code.
351 if (pcbRead==0)
352 pcbRead = &bytesReadBuffer;
355 * Using the known size of the stream, calculate the number of bytes
356 * to read from the block chain
358 bytesToReadFromBuffer = min( This->streamSize.s.LowPart - This->currentPosition.s.LowPart, cb);
361 * Depending on the type of chain that was opened when the stream was constructed,
362 * we delegate the work to the method that reads the block chains.
364 if (This->smallBlockChain!=0)
366 SmallBlockChainStream_ReadAt(This->smallBlockChain,
367 This->currentPosition,
368 bytesToReadFromBuffer,
370 pcbRead);
373 else if (This->bigBlockChain!=0)
375 BlockChainStream_ReadAt(This->bigBlockChain,
376 This->currentPosition,
377 bytesToReadFromBuffer,
379 pcbRead);
381 else
384 * Small and big block chains are both NULL. This case will happen
385 * when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
388 *pcbRead = 0;
389 res = S_OK;
390 goto end;
394 * We should always be able to read the proper amount of data from the
395 * chain.
397 assert(bytesToReadFromBuffer == *pcbRead);
400 * Advance the pointer for the number of positions read.
402 This->currentPosition.s.LowPart += *pcbRead;
404 if(*pcbRead != cb)
406 WARN("read %ld instead of the required %ld bytes !\n", *pcbRead, cb);
408 * this used to return S_FALSE, however MSDN docu says that an app should
409 * be prepared to handle error in case of stream end reached, as *some*
410 * implementations *might* return an error (IOW: most do *not*).
411 * As some program fails on returning S_FALSE, I better use S_OK here.
413 res = S_OK;
415 else
416 res = S_OK;
418 end:
419 TRACE("<-- %08lx\n", res);
420 return res;
423 /***
424 * This method is part of the ISequentialStream interface.
426 * It writes a block of information to the stream at the current
427 * position. It then moves the current position at the end of the
428 * written block. If the stream is too small to fit the block,
429 * the stream is grown to fit.
431 * See the documentation of ISequentialStream for more info.
433 HRESULT WINAPI StgStreamImpl_Write(
434 IStream* iface,
435 const void* pv, /* [size_is][in] */
436 ULONG cb, /* [in] */
437 ULONG* pcbWritten) /* [out] */
439 StgStreamImpl* const This=(StgStreamImpl*)iface;
441 ULARGE_INTEGER newSize;
442 ULONG bytesWritten = 0;
444 TRACE("(%p, %p, %ld, %p)\n",
445 iface, pv, cb, pcbWritten);
448 * Do we have permission to write to this stream?
450 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE))) {
451 return STG_E_ACCESSDENIED;
455 * If the caller is not interested in the number of bytes written,
456 * we use another buffer to avoid "if" statements in the code.
458 if (pcbWritten == 0)
459 pcbWritten = &bytesWritten;
462 * Initialize the out parameter
464 *pcbWritten = 0;
466 if (cb == 0)
468 return S_OK;
470 else
472 newSize.s.HighPart = 0;
473 newSize.s.LowPart = This->currentPosition.s.LowPart + cb;
477 * Verify if we need to grow the stream
479 if (newSize.s.LowPart > This->streamSize.s.LowPart)
481 /* grow stream */
482 IStream_SetSize(iface, newSize);
486 * Depending on the type of chain that was opened when the stream was constructed,
487 * we delegate the work to the method that readwrites to the block chains.
489 if (This->smallBlockChain!=0)
491 SmallBlockChainStream_WriteAt(This->smallBlockChain,
492 This->currentPosition,
495 pcbWritten);
498 else if (This->bigBlockChain!=0)
500 BlockChainStream_WriteAt(This->bigBlockChain,
501 This->currentPosition,
504 pcbWritten);
506 else
507 assert(FALSE);
510 * Advance the position pointer for the number of positions written.
512 This->currentPosition.s.LowPart += *pcbWritten;
514 return S_OK;
517 /***
518 * This method is part of the IStream interface.
520 * It will move the current stream pointer according to the parameters
521 * given.
523 * See the documentation of IStream for more info.
525 HRESULT WINAPI StgStreamImpl_Seek(
526 IStream* iface,
527 LARGE_INTEGER dlibMove, /* [in] */
528 DWORD dwOrigin, /* [in] */
529 ULARGE_INTEGER* plibNewPosition) /* [out] */
531 StgStreamImpl* const This=(StgStreamImpl*)iface;
533 ULARGE_INTEGER newPosition;
535 TRACE("(%p, %ld, %ld, %p)\n",
536 iface, dlibMove.s.LowPart, dwOrigin, plibNewPosition);
539 * The caller is allowed to pass in NULL as the new position return value.
540 * If it happens, we assign it to a dynamic variable to avoid special cases
541 * in the code below.
543 if (plibNewPosition == 0)
545 plibNewPosition = &newPosition;
549 * The file pointer is moved depending on the given "function"
550 * parameter.
552 switch (dwOrigin)
554 case STREAM_SEEK_SET:
555 plibNewPosition->s.HighPart = 0;
556 plibNewPosition->s.LowPart = 0;
557 break;
558 case STREAM_SEEK_CUR:
559 *plibNewPosition = This->currentPosition;
560 break;
561 case STREAM_SEEK_END:
562 *plibNewPosition = This->streamSize;
563 break;
564 default:
565 return STG_E_INVALIDFUNCTION;
568 plibNewPosition->QuadPart = RtlLargeIntegerAdd( plibNewPosition->QuadPart, dlibMove.QuadPart );
571 * tell the caller what we calculated
573 This->currentPosition = *plibNewPosition;
575 return S_OK;
578 /***
579 * This method is part of the IStream interface.
581 * It will change the size of a stream.
583 * TODO: Switch from small blocks to big blocks and vice versa.
585 * See the documentation of IStream for more info.
587 HRESULT WINAPI StgStreamImpl_SetSize(
588 IStream* iface,
589 ULARGE_INTEGER libNewSize) /* [in] */
591 StgStreamImpl* const This=(StgStreamImpl*)iface;
593 StgProperty curProperty;
594 BOOL Success;
596 TRACE("(%p, %ld)\n", iface, libNewSize.s.LowPart);
599 * As documented.
601 if (libNewSize.s.HighPart != 0)
602 return STG_E_INVALIDFUNCTION;
605 * Do we have permission?
607 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
608 return STG_E_ACCESSDENIED;
610 if (This->streamSize.s.LowPart == libNewSize.s.LowPart)
611 return S_OK;
614 * This will happen if we're creating a stream
616 if ((This->smallBlockChain == 0) && (This->bigBlockChain == 0))
618 if (libNewSize.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
620 This->smallBlockChain = SmallBlockChainStream_Construct(
621 This->parentStorage->ancestorStorage,
622 This->ownerProperty);
624 else
626 This->bigBlockChain = BlockChainStream_Construct(
627 This->parentStorage->ancestorStorage,
628 NULL,
629 This->ownerProperty);
634 * Read this stream's property to see if it's small blocks or big blocks
636 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
637 This->ownerProperty,
638 &curProperty);
640 * Determine if we have to switch from small to big blocks or vice versa
642 if ( (This->smallBlockChain!=0) &&
643 (curProperty.size.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
645 if (libNewSize.s.LowPart >= LIMIT_TO_USE_SMALL_BLOCK)
648 * Transform the small block chain into a big block chain
650 This->bigBlockChain = Storage32Impl_SmallBlocksToBigBlocks(
651 This->parentStorage->ancestorStorage,
652 &This->smallBlockChain);
656 if (This->smallBlockChain!=0)
658 Success = SmallBlockChainStream_SetSize(This->smallBlockChain, libNewSize);
660 else
662 Success = BlockChainStream_SetSize(This->bigBlockChain, libNewSize);
666 * Write the new information about this stream to the property
668 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
669 This->ownerProperty,
670 &curProperty);
672 curProperty.size.s.HighPart = libNewSize.s.HighPart;
673 curProperty.size.s.LowPart = libNewSize.s.LowPart;
675 if (Success)
677 StorageImpl_WriteProperty(This->parentStorage->ancestorStorage,
678 This->ownerProperty,
679 &curProperty);
682 This->streamSize = libNewSize;
684 return S_OK;
687 /***
688 * This method is part of the IStream interface.
690 * It will copy the 'cb' Bytes to 'pstm' IStream.
692 * See the documentation of IStream for more info.
694 HRESULT WINAPI StgStreamImpl_CopyTo(
695 IStream* iface,
696 IStream* pstm, /* [unique][in] */
697 ULARGE_INTEGER cb, /* [in] */
698 ULARGE_INTEGER* pcbRead, /* [out] */
699 ULARGE_INTEGER* pcbWritten) /* [out] */
701 HRESULT hr = S_OK;
702 BYTE tmpBuffer[128];
703 ULONG bytesRead, bytesWritten, copySize;
704 ULARGE_INTEGER totalBytesRead;
705 ULARGE_INTEGER totalBytesWritten;
707 TRACE("(%p, %p, %ld, %p, %p)\n",
708 iface, pstm, cb.s.LowPart, pcbRead, pcbWritten);
711 * Sanity check
713 if ( pstm == 0 )
714 return STG_E_INVALIDPOINTER;
716 totalBytesRead.s.LowPart = totalBytesRead.s.HighPart = 0;
717 totalBytesWritten.s.LowPart = totalBytesWritten.s.HighPart = 0;
720 * use stack to store data temporarily
721 * there is surely a more performant way of doing it, for now this basic
722 * implementation will do the job
724 while ( cb.s.LowPart > 0 )
726 if ( cb.s.LowPart >= 128 )
727 copySize = 128;
728 else
729 copySize = cb.s.LowPart;
731 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
733 totalBytesRead.s.LowPart += bytesRead;
735 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
737 totalBytesWritten.s.LowPart += bytesWritten;
740 * Check that read & write operations were successful
742 if (bytesRead != bytesWritten)
744 hr = STG_E_MEDIUMFULL;
745 break;
748 if (bytesRead!=copySize)
749 cb.s.LowPart = 0;
750 else
751 cb.s.LowPart -= bytesRead;
755 * Update number of bytes read and written
757 if (pcbRead)
759 pcbRead->s.LowPart = totalBytesRead.s.LowPart;
760 pcbRead->s.HighPart = totalBytesRead.s.HighPart;
763 if (pcbWritten)
765 pcbWritten->s.LowPart = totalBytesWritten.s.LowPart;
766 pcbWritten->s.HighPart = totalBytesWritten.s.HighPart;
768 return hr;
771 /***
772 * This method is part of the IStream interface.
774 * For streams contained in structured storages, this method
775 * does nothing. This is what the documentation tells us.
777 * See the documentation of IStream for more info.
779 HRESULT WINAPI StgStreamImpl_Commit(
780 IStream* iface,
781 DWORD grfCommitFlags) /* [in] */
783 return S_OK;
786 /***
787 * This method is part of the IStream interface.
789 * For streams contained in structured storages, this method
790 * does nothing. This is what the documentation tells us.
792 * See the documentation of IStream for more info.
794 HRESULT WINAPI StgStreamImpl_Revert(
795 IStream* iface)
797 return S_OK;
800 HRESULT WINAPI StgStreamImpl_LockRegion(
801 IStream* iface,
802 ULARGE_INTEGER libOffset, /* [in] */
803 ULARGE_INTEGER cb, /* [in] */
804 DWORD dwLockType) /* [in] */
806 FIXME("not implemented!\n");
807 return E_NOTIMPL;
810 HRESULT WINAPI StgStreamImpl_UnlockRegion(
811 IStream* iface,
812 ULARGE_INTEGER libOffset, /* [in] */
813 ULARGE_INTEGER cb, /* [in] */
814 DWORD dwLockType) /* [in] */
816 FIXME("not implemented!\n");
817 return E_NOTIMPL;
820 /***
821 * This method is part of the IStream interface.
823 * This method returns information about the current
824 * stream.
826 * See the documentation of IStream for more info.
828 HRESULT WINAPI StgStreamImpl_Stat(
829 IStream* iface,
830 STATSTG* pstatstg, /* [out] */
831 DWORD grfStatFlag) /* [in] */
833 StgStreamImpl* const This=(StgStreamImpl*)iface;
835 StgProperty curProperty;
836 BOOL readSucessful;
839 * Read the information from the property.
841 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
842 This->ownerProperty,
843 &curProperty);
845 if (readSucessful)
847 StorageUtl_CopyPropertyToSTATSTG(pstatstg,
848 &curProperty,
849 grfStatFlag);
851 pstatstg->grfMode = This->grfMode;
853 return S_OK;
856 return E_FAIL;
859 /***
860 * This method is part of the IStream interface.
862 * This method returns a clone of the interface that allows for
863 * another seek pointer
865 * See the documentation of IStream for more info.
867 * I am not totally sure what I am doing here but I presume that this
868 * should be basically as simple as creating a new stream with the same
869 * parent etc and positioning its seek cursor.
871 HRESULT WINAPI StgStreamImpl_Clone(
872 IStream* iface,
873 IStream** ppstm) /* [out] */
875 StgStreamImpl* const This=(StgStreamImpl*)iface;
876 HRESULT hres;
877 StgStreamImpl* new_stream;
878 LARGE_INTEGER seek_pos;
881 * Sanity check
883 if ( ppstm == 0 )
884 return STG_E_INVALIDPOINTER;
886 new_stream = StgStreamImpl_Construct (This->parentStorage, This->grfMode, This->ownerProperty);
888 if (!new_stream)
889 return STG_E_INSUFFICIENTMEMORY; /* Currently the only reason for new_stream=0 */
891 *ppstm = (IStream*) new_stream;
892 seek_pos.QuadPart = This->currentPosition.QuadPart;
894 hres=StgStreamImpl_Seek (*ppstm, seek_pos, STREAM_SEEK_SET, NULL);
896 assert (SUCCEEDED(hres));
898 return S_OK;