Added stub for CoResumeClassObjects.
[wine.git] / dlls / ole32 / stg_stream.c
blobfe1dfa3eed1ad07eb1a2c842541d871f4df8e577
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 #include <assert.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
16 #include "winbase.h"
17 #include "winerror.h"
18 #include "debugtools.h"
19 #include "wine/obj_storage.h"
21 #include "storage32.h"
23 DEFAULT_DEBUG_CHANNEL(storage)
27 * Virtual function table for the StgStreamImpl class.
29 static ICOM_VTABLE(IStream) StgStreamImpl_Vtbl =
31 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
32 StgStreamImpl_QueryInterface,
33 StgStreamImpl_AddRef,
34 StgStreamImpl_Release,
35 StgStreamImpl_Read,
36 StgStreamImpl_Write,
37 StgStreamImpl_Seek,
38 StgStreamImpl_SetSize,
39 StgStreamImpl_CopyTo,
40 StgStreamImpl_Commit,
41 StgStreamImpl_Revert,
42 StgStreamImpl_LockRegion,
43 StgStreamImpl_UnlockRegion,
44 StgStreamImpl_Stat,
45 StgStreamImpl_Clone
48 /******************************************************************************
49 ** StgStreamImpl implementation
52 /***
53 * This is the constructor for the StgStreamImpl class.
55 * Params:
56 * parentStorage - Pointer to the storage that contains the stream to open
57 * ownerProperty - Index of the property that points to this stream.
59 StgStreamImpl* StgStreamImpl_Construct(
60 StorageBaseImpl* parentStorage,
61 ULONG ownerProperty)
63 StgStreamImpl* newStream;
65 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
67 if (newStream!=0)
70 * Set-up the virtual function table and reference count.
72 ICOM_VTBL(newStream) = &StgStreamImpl_Vtbl;
73 newStream->ref = 0;
76 * We want to nail-down the reference to the storage in case the
77 * stream out-lives the storage in the client application.
79 newStream->parentStorage = parentStorage;
80 IStorage_AddRef((IStorage*)newStream->parentStorage);
82 newStream->ownerProperty = ownerProperty;
85 * Start the stream at the begining.
87 newStream->currentPosition.s.HighPart = 0;
88 newStream->currentPosition.s.LowPart = 0;
91 * Initialize the rest of the data.
93 newStream->streamSize.s.HighPart = 0;
94 newStream->streamSize.s.LowPart = 0;
95 newStream->bigBlockChain = 0;
96 newStream->smallBlockChain = 0;
99 * Read the size from the property and determine if the blocks forming
100 * this stream are large or small.
102 StgStreamImpl_OpenBlockChain(newStream);
105 return newStream;
108 /***
109 * This is the destructor of the StgStreamImpl class.
111 * This method will clean-up all the resources used-up by the given StgStreamImpl
112 * class. The pointer passed-in to this function will be freed and will not
113 * be valid anymore.
115 void StgStreamImpl_Destroy(StgStreamImpl* This)
117 TRACE("(%p)\n", This);
120 * Release the reference we are holding on the parent storage.
122 IStorage_Release((IStorage*)This->parentStorage);
123 This->parentStorage = 0;
126 * Make sure we clean-up the block chain stream objects that we were using.
128 if (This->bigBlockChain != 0)
130 BlockChainStream_Destroy(This->bigBlockChain);
131 This->bigBlockChain = 0;
134 if (This->smallBlockChain != 0)
136 SmallBlockChainStream_Destroy(This->smallBlockChain);
137 This->smallBlockChain = 0;
141 * Finally, free the memory used-up by the class.
143 HeapFree(GetProcessHeap(), 0, This);
146 /***
147 * This implements the IUnknown method QueryInterface for this
148 * class
150 HRESULT WINAPI StgStreamImpl_QueryInterface(
151 IStream* iface,
152 REFIID riid, /* [in] */
153 void** ppvObject) /* [iid_is][out] */
155 StgStreamImpl* const This=(StgStreamImpl*)iface;
158 * Perform a sanity check on the parameters.
160 if (ppvObject==0)
161 return E_INVALIDARG;
164 * Initialize the return parameter.
166 *ppvObject = 0;
169 * Compare the riid with the interface IDs implemented by this object.
171 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
173 *ppvObject = (IStream*)This;
175 else if (memcmp(&IID_IStream, riid, sizeof(IID_IStream)) == 0)
177 *ppvObject = (IStream*)This;
181 * Check that we obtained an interface.
183 if ((*ppvObject)==0)
184 return E_NOINTERFACE;
187 * Query Interface always increases the reference count by one when it is
188 * successful
190 StgStreamImpl_AddRef(iface);
192 return S_OK;;
195 /***
196 * This implements the IUnknown method AddRef for this
197 * class
199 ULONG WINAPI StgStreamImpl_AddRef(
200 IStream* iface)
202 StgStreamImpl* const This=(StgStreamImpl*)iface;
204 This->ref++;
206 return This->ref;
209 /***
210 * This implements the IUnknown method Release for this
211 * class
213 ULONG WINAPI StgStreamImpl_Release(
214 IStream* iface)
216 StgStreamImpl* const This=(StgStreamImpl*)iface;
218 ULONG newRef;
220 This->ref--;
222 newRef = This->ref;
225 * If the reference count goes down to 0, perform suicide.
227 if (newRef==0)
229 StgStreamImpl_Destroy(This);
232 return newRef;
235 /***
236 * This method will open the block chain pointed by the property
237 * that describes the stream.
238 * If the stream's size is null, no chain is opened.
240 void StgStreamImpl_OpenBlockChain(
241 StgStreamImpl* This)
243 StgProperty curProperty;
244 BOOL readSucessful;
247 * Make sure no old object is staying behind.
249 if (This->smallBlockChain != 0)
251 SmallBlockChainStream_Destroy(This->smallBlockChain);
252 This->smallBlockChain = 0;
255 if (This->bigBlockChain != 0)
257 BlockChainStream_Destroy(This->bigBlockChain);
258 This->bigBlockChain = 0;
262 * Read the information from the property.
264 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
265 This->ownerProperty,
266 &curProperty);
268 if (readSucessful)
270 This->streamSize = curProperty.size;
273 * This code supports only streams that are <32 bits in size.
275 assert(This->streamSize.s.HighPart == 0);
277 if(curProperty.startingBlock == BLOCK_END_OF_CHAIN)
279 assert( (This->streamSize.s.HighPart == 0) && (This->streamSize.s.LowPart == 0) );
281 else
283 if ( (This->streamSize.s.HighPart == 0) &&
284 (This->streamSize.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
286 This->smallBlockChain = SmallBlockChainStream_Construct(
287 This->parentStorage->ancestorStorage,
288 This->ownerProperty);
290 else
292 This->bigBlockChain = BlockChainStream_Construct(
293 This->parentStorage->ancestorStorage,
294 NULL,
295 This->ownerProperty);
301 /***
302 * This method is part of the ISequentialStream interface.
304 * If reads a block of information from the stream at the current
305 * position. It then moves the current position at the end of the
306 * read block
308 * See the documentation of ISequentialStream for more info.
310 HRESULT WINAPI StgStreamImpl_Read(
311 IStream* iface,
312 void* pv, /* [length_is][size_is][out] */
313 ULONG cb, /* [in] */
314 ULONG* pcbRead) /* [out] */
316 StgStreamImpl* const This=(StgStreamImpl*)iface;
318 ULONG bytesReadBuffer;
319 ULONG bytesToReadFromBuffer;
321 TRACE("(%p, %p, %ld, %p)\n",
322 iface, pv, cb, pcbRead);
325 * If the caller is not interested in the nubmer of bytes read,
326 * we use another buffer to avoid "if" statements in the code.
328 if (pcbRead==0)
329 pcbRead = &bytesReadBuffer;
332 * Using the known size of the stream, calculate the number of bytes
333 * to read from the block chain
335 bytesToReadFromBuffer = MIN( This->streamSize.s.LowPart - This->currentPosition.s.LowPart, cb);
338 * Depending on the type of chain that was opened when the stream was constructed,
339 * we delegate the work to the method that read the block chains.
341 if (This->smallBlockChain!=0)
343 SmallBlockChainStream_ReadAt(This->smallBlockChain,
344 This->currentPosition,
345 bytesToReadFromBuffer,
347 pcbRead);
350 else if (This->bigBlockChain!=0)
352 BlockChainStream_ReadAt(This->bigBlockChain,
353 This->currentPosition,
354 bytesToReadFromBuffer,
356 pcbRead);
358 else
361 * Small and big block chains are both NULL. This case will happen
362 * when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
365 *pcbRead = 0;
366 return S_OK;
370 * We should always be able to read the proper amount of data from the
371 * chain.
373 assert(bytesToReadFromBuffer == *pcbRead);
376 * Advance the pointer for the number of positions read.
378 This->currentPosition.s.LowPart += *pcbRead;
381 * The function returns S_OK if the buffer was filled completely
382 * it returns S_FALSE if the end of the stream is reached before the
383 * buffer is filled
385 if(*pcbRead == cb)
386 return S_OK;
388 return S_FALSE;
391 /***
392 * This method is part of the ISequentialStream interface.
394 * It writes a block of information to the stream at the current
395 * position. It then moves the current position at the end of the
396 * written block. If the stream is too small to fit the block,
397 * the stream is grown to fit.
399 * See the documentation of ISequentialStream for more info.
401 HRESULT WINAPI StgStreamImpl_Write(
402 IStream* iface,
403 const void* pv, /* [size_is][in] */
404 ULONG cb, /* [in] */
405 ULONG* pcbWritten) /* [out] */
407 StgStreamImpl* const This=(StgStreamImpl*)iface;
409 ULARGE_INTEGER newSize;
410 ULONG bytesWritten = 0;
412 TRACE("(%p, %p, %ld, %p)\n",
413 iface, pv, cb, pcbWritten);
415 if (!(This->grfMode & STGM_WRITE))
416 return STG_E_ACCESSDENIED;
419 * If the caller is not interested in the number of bytes written,
420 * we use another buffer to avoid "if" statements in the code.
422 if (pcbWritten == 0)
423 pcbWritten = &bytesWritten;
426 * Initialize the out parameter
428 *pcbWritten = 0;
430 if (cb == 0)
432 return S_OK;
434 else
436 newSize.s.HighPart = 0;
437 newSize.s.LowPart = This->currentPosition.s.LowPart + cb;
441 * Verify if we need to grow the stream
443 if (newSize.s.LowPart > This->streamSize.s.LowPart)
445 /* grow stream */
446 IStream_SetSize(iface, newSize);
450 * Depending on the type of chain that was opened when the stream was constructed,
451 * we delegate the work to the method that readwrites to the block chains.
453 if (This->smallBlockChain!=0)
455 SmallBlockChainStream_WriteAt(This->smallBlockChain,
456 This->currentPosition,
459 pcbWritten);
462 else if (This->bigBlockChain!=0)
464 BlockChainStream_WriteAt(This->bigBlockChain,
465 This->currentPosition,
468 pcbWritten);
470 else
471 assert(FALSE);
474 * Advance the position pointer for the number of positions written.
476 This->currentPosition.s.LowPart += *pcbWritten;
478 return S_OK;
481 /***
482 * This method is part of the IStream interface.
484 * It will move the current stream pointer according to the parameters
485 * given.
487 * See the documentation of IStream for more info.
489 HRESULT WINAPI StgStreamImpl_Seek(
490 IStream* iface,
491 LARGE_INTEGER dlibMove, /* [in] */
492 DWORD dwOrigin, /* [in] */
493 ULARGE_INTEGER* plibNewPosition) /* [out] */
495 StgStreamImpl* const This=(StgStreamImpl*)iface;
497 ULARGE_INTEGER newPosition;
499 TRACE("(%p, %ld, %ld, %p)\n",
500 iface, dlibMove.s.LowPart, dwOrigin, plibNewPosition);
503 * The caller is allowed to pass in NULL as the new position return value.
504 * If it happens, we assign it to a dynamic variable to avoid special cases
505 * in the code below.
507 if (plibNewPosition == 0)
509 plibNewPosition = &newPosition;
513 * The file pointer is moved depending on the given "function"
514 * parameter.
516 switch (dwOrigin)
518 case STREAM_SEEK_SET:
519 plibNewPosition->s.HighPart = 0;
520 plibNewPosition->s.LowPart = 0;
521 break;
522 case STREAM_SEEK_CUR:
523 *plibNewPosition = This->currentPosition;
524 break;
525 case STREAM_SEEK_END:
526 *plibNewPosition = This->streamSize;
527 break;
528 default:
529 return STG_E_INVALIDFUNCTION;
533 * We don't support files with offsets of 64 bits.
535 assert(dlibMove.s.HighPart == 0);
538 * Check if we end-up before the beginning of the file. That should trigger an
539 * error.
541 if ( (dlibMove.s.LowPart<0) && (plibNewPosition->s.LowPart < (ULONG)(-dlibMove.s.LowPart)) )
544 * I don't know what error to send there.
546 return E_FAIL;
550 * Move the actual file pointer
551 * If the file pointer ends-up after the end of the stream, the next Write operation will
552 * make the file larger. This is how it is documented.
554 plibNewPosition->s.LowPart += dlibMove.s.LowPart;
555 This->currentPosition = *plibNewPosition;
557 return S_OK;
560 /***
561 * This method is part of the IStream interface.
563 * It will change the size of a stream.
565 * TODO: Switch from small blocks to big blocks and vice versa.
567 * See the documentation of IStream for more info.
569 HRESULT WINAPI StgStreamImpl_SetSize(
570 IStream* iface,
571 ULARGE_INTEGER libNewSize) /* [in] */
573 StgStreamImpl* const This=(StgStreamImpl*)iface;
575 StgProperty curProperty;
576 BOOL Success;
578 TRACE("(%p, %ld)\n", iface, libNewSize.s.LowPart);
581 * As documented.
583 if (libNewSize.s.HighPart != 0)
584 return STG_E_INVALIDFUNCTION;
586 if (This->streamSize.s.LowPart == libNewSize.s.LowPart)
587 return S_OK;
590 * This will happen if we're creating a stream
592 if ((This->smallBlockChain == 0) && (This->bigBlockChain == 0))
594 if (libNewSize.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
596 This->smallBlockChain = SmallBlockChainStream_Construct(
597 This->parentStorage->ancestorStorage,
598 This->ownerProperty);
600 else
602 This->bigBlockChain = BlockChainStream_Construct(
603 This->parentStorage->ancestorStorage,
604 NULL,
605 This->ownerProperty);
610 * Read this stream's property to see if it's small blocks or big blocks
612 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
613 This->ownerProperty,
614 &curProperty);
616 * Determine if we have to switch from small to big blocks or vice versa
618 if ( (This->smallBlockChain!=0) &&
619 (curProperty.size.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
621 if (libNewSize.s.LowPart >= LIMIT_TO_USE_SMALL_BLOCK)
624 * Transform the small block chain into a big block chain
626 This->bigBlockChain = Storage32Impl_SmallBlocksToBigBlocks(
627 This->parentStorage->ancestorStorage,
628 &This->smallBlockChain);
632 if (This->smallBlockChain!=0)
634 Success = SmallBlockChainStream_SetSize(This->smallBlockChain, libNewSize);
636 else
638 Success = BlockChainStream_SetSize(This->bigBlockChain, libNewSize);
642 * Write to the property the new information about this stream
644 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
645 This->ownerProperty,
646 &curProperty);
648 curProperty.size.s.HighPart = libNewSize.s.HighPart;
649 curProperty.size.s.LowPart = libNewSize.s.LowPart;
651 if (Success)
653 StorageImpl_WriteProperty(This->parentStorage->ancestorStorage,
654 This->ownerProperty,
655 &curProperty);
658 This->streamSize = libNewSize;
660 return S_OK;
663 /***
664 * This method is part of the IStream interface.
666 * It will copy the 'cb' Bytes to 'pstm' IStream.
668 * See the documentation of IStream for more info.
670 HRESULT WINAPI StgStreamImpl_CopyTo(
671 IStream* iface,
672 IStream* pstm, /* [unique][in] */
673 ULARGE_INTEGER cb, /* [in] */
674 ULARGE_INTEGER* pcbRead, /* [out] */
675 ULARGE_INTEGER* pcbWritten) /* [out] */
677 HRESULT hr = S_OK;
678 BYTE tmpBuffer[128];
679 ULONG bytesRead, bytesWritten, copySize;
680 ULARGE_INTEGER totalBytesRead;
681 ULARGE_INTEGER totalBytesWritten;
683 TRACE("(%p, %p, %ld, %p, %p)\n",
684 iface, pstm, cb.s.LowPart, pcbRead, pcbWritten);
687 * Sanity check
689 if ( pstm == 0 )
690 return STG_E_INVALIDPOINTER;
692 totalBytesRead.s.LowPart = totalBytesRead.s.HighPart = 0;
693 totalBytesWritten.s.LowPart = totalBytesWritten.s.HighPart = 0;
696 * use stack to store data temporarly
697 * there is surely more performant way of doing it, for now this basic
698 * implementation will do the job
700 while ( cb.s.LowPart > 0 )
702 if ( cb.s.LowPart >= 128 )
703 copySize = 128;
704 else
705 copySize = cb.s.LowPart;
707 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
709 totalBytesRead.s.LowPart += bytesRead;
711 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
713 totalBytesWritten.s.LowPart += bytesWritten;
716 * Check that read & write operations were succesfull
718 if (bytesRead != bytesWritten)
720 hr = STG_E_MEDIUMFULL;
721 break;
724 if (bytesRead!=copySize)
725 cb.s.LowPart = 0;
726 else
727 cb.s.LowPart -= bytesRead;
731 * Update number of bytes read and written
733 if (pcbRead)
735 pcbRead->s.LowPart = totalBytesRead.s.LowPart;
736 pcbRead->s.HighPart = totalBytesRead.s.HighPart;
739 if (pcbWritten)
741 pcbWritten->s.LowPart = totalBytesWritten.s.LowPart;
742 pcbWritten->s.HighPart = totalBytesWritten.s.HighPart;
744 return hr;
747 /***
748 * This method is part of the IStream interface.
750 * For streams contained in structured storages, this method
751 * does nothing. This is what the documentation tells us.
753 * See the documentation of IStream for more info.
755 HRESULT WINAPI StgStreamImpl_Commit(
756 IStream* iface,
757 DWORD grfCommitFlags) /* [in] */
759 return S_OK;
762 /***
763 * This method is part of the IStream interface.
765 * For streams contained in structured storages, this method
766 * does nothing. This is what the documentation tells us.
768 * See the documentation of IStream for more info.
770 HRESULT WINAPI StgStreamImpl_Revert(
771 IStream* iface)
773 return S_OK;
776 HRESULT WINAPI StgStreamImpl_LockRegion(
777 IStream* iface,
778 ULARGE_INTEGER libOffset, /* [in] */
779 ULARGE_INTEGER cb, /* [in] */
780 DWORD dwLockType) /* [in] */
782 FIXME("not implemented!\n");
783 return E_NOTIMPL;
786 HRESULT WINAPI StgStreamImpl_UnlockRegion(
787 IStream* iface,
788 ULARGE_INTEGER libOffset, /* [in] */
789 ULARGE_INTEGER cb, /* [in] */
790 DWORD dwLockType) /* [in] */
792 FIXME("not implemented!\n");
793 return E_NOTIMPL;
796 /***
797 * This method is part of the IStream interface.
799 * This method returns information about the current
800 * stream.
802 * See the documentation of IStream for more info.
804 HRESULT WINAPI StgStreamImpl_Stat(
805 IStream* iface,
806 STATSTG* pstatstg, /* [out] */
807 DWORD grfStatFlag) /* [in] */
809 StgStreamImpl* const This=(StgStreamImpl*)iface;
811 StgProperty curProperty;
812 BOOL readSucessful;
815 * Read the information from the property.
817 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
818 This->ownerProperty,
819 &curProperty);
821 if (readSucessful)
823 StorageUtl_CopyPropertyToSTATSTG(pstatstg,
824 &curProperty,
825 grfStatFlag);
827 return S_OK;
830 return E_FAIL;
833 HRESULT WINAPI StgStreamImpl_Clone(
834 IStream* iface,
835 IStream** ppstm) /* [out] */
837 FIXME("not implemented!\n");
838 return E_NOTIMPL;