Added a missing prototype.
[wine/wine-kai.git] / dlls / ole32 / stg_stream.c
blobde966885189ead41ed895584560789bfd81c8d8c
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);
49 * Virtual function table for the StgStreamImpl class.
51 static IStreamVtbl StgStreamImpl_Vtbl =
53 StgStreamImpl_QueryInterface,
54 StgStreamImpl_AddRef,
55 StgStreamImpl_Release,
56 StgStreamImpl_Read,
57 StgStreamImpl_Write,
58 StgStreamImpl_Seek,
59 StgStreamImpl_SetSize,
60 StgStreamImpl_CopyTo,
61 StgStreamImpl_Commit,
62 StgStreamImpl_Revert,
63 StgStreamImpl_LockRegion,
64 StgStreamImpl_UnlockRegion,
65 StgStreamImpl_Stat,
66 StgStreamImpl_Clone
69 /******************************************************************************
70 ** StgStreamImpl implementation
73 /***
74 * This is the constructor for the StgStreamImpl class.
76 * Params:
77 * parentStorage - Pointer to the storage that contains the stream to open
78 * ownerProperty - Index of the property that points to this stream.
80 StgStreamImpl* StgStreamImpl_Construct(
81 StorageBaseImpl* parentStorage,
82 DWORD grfMode,
83 ULONG ownerProperty)
85 StgStreamImpl* newStream;
87 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
89 if (newStream!=0)
92 * Set-up the virtual function table and reference count.
94 newStream->lpVtbl = &StgStreamImpl_Vtbl;
95 newStream->ref = 0;
98 * We want to nail-down the reference to the storage in case the
99 * stream out-lives the storage in the client application.
101 newStream->parentStorage = parentStorage;
102 IStorage_AddRef((IStorage*)newStream->parentStorage);
104 newStream->grfMode = grfMode;
105 newStream->ownerProperty = ownerProperty;
108 * Start the stream at the beginning.
110 newStream->currentPosition.u.HighPart = 0;
111 newStream->currentPosition.u.LowPart = 0;
114 * Initialize the rest of the data.
116 newStream->streamSize.u.HighPart = 0;
117 newStream->streamSize.u.LowPart = 0;
118 newStream->bigBlockChain = 0;
119 newStream->smallBlockChain = 0;
122 * Read the size from the property and determine if the blocks forming
123 * this stream are large or small.
125 StgStreamImpl_OpenBlockChain(newStream);
128 return newStream;
131 /***
132 * This is the destructor of the StgStreamImpl class.
134 * This method will clean-up all the resources used-up by the given StgStreamImpl
135 * class. The pointer passed-in to this function will be freed and will not
136 * be valid anymore.
138 void StgStreamImpl_Destroy(StgStreamImpl* This)
140 TRACE("(%p)\n", This);
143 * Release the reference we are holding on the parent storage.
145 IStorage_Release((IStorage*)This->parentStorage);
146 This->parentStorage = 0;
149 * Make sure we clean-up the block chain stream objects that we were using.
151 if (This->bigBlockChain != 0)
153 BlockChainStream_Destroy(This->bigBlockChain);
154 This->bigBlockChain = 0;
157 if (This->smallBlockChain != 0)
159 SmallBlockChainStream_Destroy(This->smallBlockChain);
160 This->smallBlockChain = 0;
164 * Finally, free the memory used-up by the class.
166 HeapFree(GetProcessHeap(), 0, This);
169 /***
170 * This implements the IUnknown method QueryInterface for this
171 * class
173 HRESULT WINAPI StgStreamImpl_QueryInterface(
174 IStream* iface,
175 REFIID riid, /* [in] */
176 void** ppvObject) /* [iid_is][out] */
178 StgStreamImpl* const This=(StgStreamImpl*)iface;
181 * Perform a sanity check on the parameters.
183 if (ppvObject==0)
184 return E_INVALIDARG;
187 * Initialize the return parameter.
189 *ppvObject = 0;
192 * Compare the riid with the interface IDs implemented by this object.
194 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
196 *ppvObject = (IStream*)This;
198 else if (memcmp(&IID_IStream, riid, sizeof(IID_IStream)) == 0)
200 *ppvObject = (IStream*)This;
204 * Check that we obtained an interface.
206 if ((*ppvObject)==0)
207 return E_NOINTERFACE;
210 * Query Interface always increases the reference count by one when it is
211 * successful
213 StgStreamImpl_AddRef(iface);
215 return S_OK;
218 /***
219 * This implements the IUnknown method AddRef for this
220 * class
222 ULONG WINAPI StgStreamImpl_AddRef(
223 IStream* iface)
225 StgStreamImpl* const This=(StgStreamImpl*)iface;
226 return InterlockedIncrement(&This->ref);
229 /***
230 * This implements the IUnknown method Release for this
231 * class
233 ULONG WINAPI StgStreamImpl_Release(
234 IStream* iface)
236 StgStreamImpl* const This=(StgStreamImpl*)iface;
238 ULONG ref;
240 ref = InterlockedDecrement(&This->ref);
243 * If the reference count goes down to 0, perform suicide.
245 if (ref==0)
247 StgStreamImpl_Destroy(This);
250 return ref;
253 /***
254 * This method will open the block chain pointed by the property
255 * that describes the stream.
256 * If the stream's size is null, no chain is opened.
258 void StgStreamImpl_OpenBlockChain(
259 StgStreamImpl* This)
261 StgProperty curProperty;
262 BOOL readSucessful;
265 * Make sure no old object is left over.
267 if (This->smallBlockChain != 0)
269 SmallBlockChainStream_Destroy(This->smallBlockChain);
270 This->smallBlockChain = 0;
273 if (This->bigBlockChain != 0)
275 BlockChainStream_Destroy(This->bigBlockChain);
276 This->bigBlockChain = 0;
280 * Read the information from the property.
282 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
283 This->ownerProperty,
284 &curProperty);
286 if (readSucessful)
288 This->streamSize = curProperty.size;
291 * This code supports only streams that are <32 bits in size.
293 assert(This->streamSize.u.HighPart == 0);
295 if(curProperty.startingBlock == BLOCK_END_OF_CHAIN)
297 assert( (This->streamSize.u.HighPart == 0) && (This->streamSize.u.LowPart == 0) );
299 else
301 if ( (This->streamSize.u.HighPart == 0) &&
302 (This->streamSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
304 This->smallBlockChain = SmallBlockChainStream_Construct(
305 This->parentStorage->ancestorStorage,
306 This->ownerProperty);
308 else
310 This->bigBlockChain = BlockChainStream_Construct(
311 This->parentStorage->ancestorStorage,
312 NULL,
313 This->ownerProperty);
319 /***
320 * This method is part of the ISequentialStream interface.
322 * It reads a block of information from the stream at the current
323 * position. It then moves the current position at the end of the
324 * read block
326 * See the documentation of ISequentialStream for more info.
328 HRESULT WINAPI StgStreamImpl_Read(
329 IStream* iface,
330 void* pv, /* [length_is][size_is][out] */
331 ULONG cb, /* [in] */
332 ULONG* pcbRead) /* [out] */
334 StgStreamImpl* const This=(StgStreamImpl*)iface;
336 ULONG bytesReadBuffer;
337 ULONG bytesToReadFromBuffer;
338 HRESULT res = S_FALSE;
340 TRACE("(%p, %p, %ld, %p)\n",
341 iface, pv, cb, pcbRead);
344 * If the caller is not interested in the number of bytes read,
345 * we use another buffer to avoid "if" statements in the code.
347 if (pcbRead==0)
348 pcbRead = &bytesReadBuffer;
351 * Using the known size of the stream, calculate the number of bytes
352 * to read from the block chain
354 bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
357 * Depending on the type of chain that was opened when the stream was constructed,
358 * we delegate the work to the method that reads the block chains.
360 if (This->smallBlockChain!=0)
362 SmallBlockChainStream_ReadAt(This->smallBlockChain,
363 This->currentPosition,
364 bytesToReadFromBuffer,
366 pcbRead);
369 else if (This->bigBlockChain!=0)
371 BlockChainStream_ReadAt(This->bigBlockChain,
372 This->currentPosition,
373 bytesToReadFromBuffer,
375 pcbRead);
377 else
380 * Small and big block chains are both NULL. This case will happen
381 * when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
384 *pcbRead = 0;
385 res = S_OK;
386 goto end;
390 * We should always be able to read the proper amount of data from the
391 * chain.
393 assert(bytesToReadFromBuffer == *pcbRead);
396 * Advance the pointer for the number of positions read.
398 This->currentPosition.u.LowPart += *pcbRead;
400 if(*pcbRead != cb)
402 WARN("read %ld instead of the required %ld bytes !\n", *pcbRead, cb);
404 * this used to return S_FALSE, however MSDN docu says that an app should
405 * be prepared to handle error in case of stream end reached, as *some*
406 * implementations *might* return an error (IOW: most do *not*).
407 * As some program fails on returning S_FALSE, I better use S_OK here.
409 res = S_OK;
411 else
412 res = S_OK;
414 end:
415 TRACE("<-- %08lx\n", res);
416 return res;
419 /***
420 * This method is part of the ISequentialStream interface.
422 * It writes a block of information to the stream at the current
423 * position. It then moves the current position at the end of the
424 * written block. If the stream is too small to fit the block,
425 * the stream is grown to fit.
427 * See the documentation of ISequentialStream for more info.
429 HRESULT WINAPI StgStreamImpl_Write(
430 IStream* iface,
431 const void* pv, /* [size_is][in] */
432 ULONG cb, /* [in] */
433 ULONG* pcbWritten) /* [out] */
435 StgStreamImpl* const This=(StgStreamImpl*)iface;
437 ULARGE_INTEGER newSize;
438 ULONG bytesWritten = 0;
440 TRACE("(%p, %p, %ld, %p)\n",
441 iface, pv, cb, pcbWritten);
444 * Do we have permission to write to this stream?
446 switch(STGM_ACCESS_MODE(This->grfMode))
448 case STGM_WRITE:
449 case STGM_READWRITE:
450 break;
451 default:
452 return STG_E_ACCESSDENIED;
455 if (!pv)
456 return STG_E_INVALIDPOINTER;
459 * If the caller is not interested in the number of bytes written,
460 * we use another buffer to avoid "if" statements in the code.
462 if (pcbWritten == 0)
463 pcbWritten = &bytesWritten;
466 * Initialize the out parameter
468 *pcbWritten = 0;
470 if (cb == 0)
472 return S_OK;
474 else
476 newSize.u.HighPart = 0;
477 newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
481 * Verify if we need to grow the stream
483 if (newSize.u.LowPart > This->streamSize.u.LowPart)
485 /* grow stream */
486 IStream_SetSize(iface, newSize);
490 * Depending on the type of chain that was opened when the stream was constructed,
491 * we delegate the work to the method that readwrites to the block chains.
493 if (This->smallBlockChain!=0)
495 SmallBlockChainStream_WriteAt(This->smallBlockChain,
496 This->currentPosition,
499 pcbWritten);
502 else if (This->bigBlockChain!=0)
504 BlockChainStream_WriteAt(This->bigBlockChain,
505 This->currentPosition,
508 pcbWritten);
510 else
511 assert(FALSE);
514 * Advance the position pointer for the number of positions written.
516 This->currentPosition.u.LowPart += *pcbWritten;
518 return S_OK;
521 /***
522 * This method is part of the IStream interface.
524 * It will move the current stream pointer according to the parameters
525 * given.
527 * See the documentation of IStream for more info.
529 HRESULT WINAPI StgStreamImpl_Seek(
530 IStream* iface,
531 LARGE_INTEGER dlibMove, /* [in] */
532 DWORD dwOrigin, /* [in] */
533 ULARGE_INTEGER* plibNewPosition) /* [out] */
535 StgStreamImpl* const This=(StgStreamImpl*)iface;
537 ULARGE_INTEGER newPosition;
539 TRACE("(%p, %ld, %ld, %p)\n",
540 iface, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
543 * The caller is allowed to pass in NULL as the new position return value.
544 * If it happens, we assign it to a dynamic variable to avoid special cases
545 * in the code below.
547 if (plibNewPosition == 0)
549 plibNewPosition = &newPosition;
553 * The file pointer is moved depending on the given "function"
554 * parameter.
556 switch (dwOrigin)
558 case STREAM_SEEK_SET:
559 plibNewPosition->u.HighPart = 0;
560 plibNewPosition->u.LowPart = 0;
561 break;
562 case STREAM_SEEK_CUR:
563 *plibNewPosition = This->currentPosition;
564 break;
565 case STREAM_SEEK_END:
566 *plibNewPosition = This->streamSize;
567 break;
568 default:
569 return STG_E_INVALIDFUNCTION;
572 plibNewPosition->QuadPart = RtlLargeIntegerAdd( plibNewPosition->QuadPart, dlibMove.QuadPart );
575 * tell the caller what we calculated
577 This->currentPosition = *plibNewPosition;
579 return S_OK;
582 /***
583 * This method is part of the IStream interface.
585 * It will change the size of a stream.
587 * TODO: Switch from small blocks to big blocks and vice versa.
589 * See the documentation of IStream for more info.
591 HRESULT WINAPI StgStreamImpl_SetSize(
592 IStream* iface,
593 ULARGE_INTEGER libNewSize) /* [in] */
595 StgStreamImpl* const This=(StgStreamImpl*)iface;
597 StgProperty curProperty;
598 BOOL Success;
600 TRACE("(%p, %ld)\n", iface, libNewSize.u.LowPart);
603 * As documented.
605 if (libNewSize.u.HighPart != 0)
606 return STG_E_INVALIDFUNCTION;
609 * Do we have permission?
611 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
612 return STG_E_ACCESSDENIED;
614 if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
615 return S_OK;
618 * This will happen if we're creating a stream
620 if ((This->smallBlockChain == 0) && (This->bigBlockChain == 0))
622 if (libNewSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
624 This->smallBlockChain = SmallBlockChainStream_Construct(
625 This->parentStorage->ancestorStorage,
626 This->ownerProperty);
628 else
630 This->bigBlockChain = BlockChainStream_Construct(
631 This->parentStorage->ancestorStorage,
632 NULL,
633 This->ownerProperty);
638 * Read this stream's property to see if it's small blocks or big blocks
640 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
641 This->ownerProperty,
642 &curProperty);
644 * Determine if we have to switch from small to big blocks or vice versa
646 if ( (This->smallBlockChain!=0) &&
647 (curProperty.size.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
649 if (libNewSize.u.LowPart >= LIMIT_TO_USE_SMALL_BLOCK)
652 * Transform the small block chain into a big block chain
654 This->bigBlockChain = Storage32Impl_SmallBlocksToBigBlocks(
655 This->parentStorage->ancestorStorage,
656 &This->smallBlockChain);
660 if (This->smallBlockChain!=0)
662 Success = SmallBlockChainStream_SetSize(This->smallBlockChain, libNewSize);
664 else
666 Success = BlockChainStream_SetSize(This->bigBlockChain, libNewSize);
670 * Write the new information about this stream to the property
672 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
673 This->ownerProperty,
674 &curProperty);
676 curProperty.size.u.HighPart = libNewSize.u.HighPart;
677 curProperty.size.u.LowPart = libNewSize.u.LowPart;
679 if (Success)
681 StorageImpl_WriteProperty(This->parentStorage->ancestorStorage,
682 This->ownerProperty,
683 &curProperty);
686 This->streamSize = libNewSize;
688 return S_OK;
691 /***
692 * This method is part of the IStream interface.
694 * It will copy the 'cb' Bytes to 'pstm' IStream.
696 * See the documentation of IStream for more info.
698 HRESULT WINAPI StgStreamImpl_CopyTo(
699 IStream* iface,
700 IStream* pstm, /* [unique][in] */
701 ULARGE_INTEGER cb, /* [in] */
702 ULARGE_INTEGER* pcbRead, /* [out] */
703 ULARGE_INTEGER* pcbWritten) /* [out] */
705 HRESULT hr = S_OK;
706 BYTE tmpBuffer[128];
707 ULONG bytesRead, bytesWritten, copySize;
708 ULARGE_INTEGER totalBytesRead;
709 ULARGE_INTEGER totalBytesWritten;
711 TRACE("(%p, %p, %ld, %p, %p)\n",
712 iface, pstm, cb.u.LowPart, pcbRead, pcbWritten);
715 * Sanity check
717 if ( pstm == 0 )
718 return STG_E_INVALIDPOINTER;
720 totalBytesRead.u.LowPart = totalBytesRead.u.HighPart = 0;
721 totalBytesWritten.u.LowPart = totalBytesWritten.u.HighPart = 0;
724 * use stack to store data temporarily
725 * there is surely a more performant way of doing it, for now this basic
726 * implementation will do the job
728 while ( cb.u.LowPart > 0 )
730 if ( cb.u.LowPart >= 128 )
731 copySize = 128;
732 else
733 copySize = cb.u.LowPart;
735 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
737 totalBytesRead.u.LowPart += bytesRead;
739 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
741 totalBytesWritten.u.LowPart += bytesWritten;
744 * Check that read & write operations were successful
746 if (bytesRead != bytesWritten)
748 hr = STG_E_MEDIUMFULL;
749 break;
752 if (bytesRead!=copySize)
753 cb.u.LowPart = 0;
754 else
755 cb.u.LowPart -= bytesRead;
759 * Update number of bytes read and written
761 if (pcbRead)
763 pcbRead->u.LowPart = totalBytesRead.u.LowPart;
764 pcbRead->u.HighPart = totalBytesRead.u.HighPart;
767 if (pcbWritten)
769 pcbWritten->u.LowPart = totalBytesWritten.u.LowPart;
770 pcbWritten->u.HighPart = totalBytesWritten.u.HighPart;
772 return hr;
775 /***
776 * This method is part of the IStream interface.
778 * For streams contained in structured storages, this method
779 * does nothing. This is what the documentation tells us.
781 * See the documentation of IStream for more info.
783 HRESULT WINAPI StgStreamImpl_Commit(
784 IStream* iface,
785 DWORD grfCommitFlags) /* [in] */
787 return S_OK;
790 /***
791 * This method is part of the IStream interface.
793 * For streams contained in structured storages, this method
794 * does nothing. This is what the documentation tells us.
796 * See the documentation of IStream for more info.
798 HRESULT WINAPI StgStreamImpl_Revert(
799 IStream* iface)
801 return S_OK;
804 HRESULT WINAPI StgStreamImpl_LockRegion(
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 HRESULT WINAPI StgStreamImpl_UnlockRegion(
815 IStream* iface,
816 ULARGE_INTEGER libOffset, /* [in] */
817 ULARGE_INTEGER cb, /* [in] */
818 DWORD dwLockType) /* [in] */
820 FIXME("not implemented!\n");
821 return E_NOTIMPL;
824 /***
825 * This method is part of the IStream interface.
827 * This method returns information about the current
828 * stream.
830 * See the documentation of IStream for more info.
832 HRESULT WINAPI StgStreamImpl_Stat(
833 IStream* iface,
834 STATSTG* pstatstg, /* [out] */
835 DWORD grfStatFlag) /* [in] */
837 StgStreamImpl* const This=(StgStreamImpl*)iface;
839 StgProperty curProperty;
840 BOOL readSucessful;
843 * Read the information from the property.
845 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
846 This->ownerProperty,
847 &curProperty);
849 if (readSucessful)
851 StorageUtl_CopyPropertyToSTATSTG(pstatstg,
852 &curProperty,
853 grfStatFlag);
855 pstatstg->grfMode = This->grfMode;
857 return S_OK;
860 return E_FAIL;
863 /***
864 * This method is part of the IStream interface.
866 * This method returns a clone of the interface that allows for
867 * another seek pointer
869 * See the documentation of IStream for more info.
871 * I am not totally sure what I am doing here but I presume that this
872 * should be basically as simple as creating a new stream with the same
873 * parent etc and positioning its seek cursor.
875 HRESULT WINAPI StgStreamImpl_Clone(
876 IStream* iface,
877 IStream** ppstm) /* [out] */
879 StgStreamImpl* const This=(StgStreamImpl*)iface;
880 HRESULT hres;
881 StgStreamImpl* new_stream;
882 LARGE_INTEGER seek_pos;
885 * Sanity check
887 if ( ppstm == 0 )
888 return STG_E_INVALIDPOINTER;
890 new_stream = StgStreamImpl_Construct (This->parentStorage, This->grfMode, This->ownerProperty);
892 if (!new_stream)
893 return STG_E_INSUFFICIENTMEMORY; /* Currently the only reason for new_stream=0 */
895 *ppstm = (IStream*) new_stream;
896 seek_pos.QuadPart = This->currentPosition.QuadPart;
898 hres=StgStreamImpl_Seek (*ppstm, seek_pos, STREAM_SEEK_SET, NULL);
900 assert (SUCCEEDED(hres));
902 return S_OK;