Authors: Chris Morgan <cmorgan@wpi.edu>, James Abbatiello <abbeyj@wpi.edu>
[wine.git] / ole / stg_stream.c
blob2504d800a71fe905c1b241494dfa7535ede8e1e2
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 "debug.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 newStream->lpvtbl = &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.HighPart = 0;
88 newStream->currentPosition.LowPart = 0;
91 * Initialize the rest of the data.
93 newStream->streamSize.HighPart = 0;
94 newStream->streamSize.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(storage, "(%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.HighPart == 0);
277 if(curProperty.startingBlock == BLOCK_END_OF_CHAIN)
279 assert( (This->streamSize.HighPart == 0) && (This->streamSize.LowPart == 0) );
281 else
283 if ( (This->streamSize.HighPart == 0) &&
284 (This->streamSize.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(storage, "(%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.LowPart - This->currentPosition.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
359 assert(FALSE);
362 * We should always be able to read the proper amount of data from the
363 * chain.
365 assert(bytesToReadFromBuffer == *pcbRead);
368 * Advance the pointer for the number of positions read.
370 This->currentPosition.LowPart += *pcbRead;
373 * The function returns S_OK if the buffer was filled completely
374 * it returns S_FALSE if the end of the stream is reached before the
375 * buffer is filled
377 if(*pcbRead == cb)
378 return S_OK;
380 return S_FALSE;
383 /***
384 * This method is part of the ISequentialStream interface.
386 * It writes a block of information to the stream at the current
387 * position. It then moves the current position at the end of the
388 * written block. If the stream is too small to fit the block,
389 * the stream is grown to fit.
391 * See the documentation of ISequentialStream for more info.
393 HRESULT WINAPI StgStreamImpl_Write(
394 IStream* iface,
395 const void* pv, /* [size_is][in] */
396 ULONG cb, /* [in] */
397 ULONG* pcbWritten) /* [out] */
399 StgStreamImpl* const This=(StgStreamImpl*)iface;
401 ULARGE_INTEGER newSize;
402 ULONG bytesWritten = 0;
404 TRACE(storage, "(%p, %p, %ld, %p)\n",
405 iface, pv, cb, pcbWritten);
408 * If the caller is not interested in the number of bytes written,
409 * we use another buffer to avoid "if" statements in the code.
411 if (pcbWritten == 0)
412 pcbWritten = &bytesWritten;
415 * Initialize the out parameter
417 *pcbWritten = 0;
419 if (cb == 0)
421 return S_OK;
423 else
425 newSize.HighPart = 0;
426 newSize.LowPart = This->currentPosition.LowPart + cb;
430 * Verify if we need to grow the stream
432 if (newSize.LowPart > This->streamSize.LowPart)
434 /* grow stream */
435 IStream_SetSize(iface, newSize);
439 * Depending on the type of chain that was opened when the stream was constructed,
440 * we delegate the work to the method that readwrites to the block chains.
442 if (This->smallBlockChain!=0)
444 SmallBlockChainStream_WriteAt(This->smallBlockChain,
445 This->currentPosition,
448 pcbWritten);
451 else if (This->bigBlockChain!=0)
453 BlockChainStream_WriteAt(This->bigBlockChain,
454 This->currentPosition,
457 pcbWritten);
459 else
460 assert(FALSE);
463 * Advance the position pointer for the number of positions written.
465 This->currentPosition.LowPart += *pcbWritten;
467 return S_OK;
470 /***
471 * This method is part of the IStream interface.
473 * It will move the current stream pointer according to the parameters
474 * given.
476 * See the documentation of IStream for more info.
478 HRESULT WINAPI StgStreamImpl_Seek(
479 IStream* iface,
480 LARGE_INTEGER dlibMove, /* [in] */
481 DWORD dwOrigin, /* [in] */
482 ULARGE_INTEGER* plibNewPosition) /* [out] */
484 StgStreamImpl* const This=(StgStreamImpl*)iface;
486 ULARGE_INTEGER newPosition;
488 TRACE(storage, "(%p, %ld, %ld, %p)\n",
489 iface, dlibMove.LowPart, dwOrigin, plibNewPosition);
492 * The caller is allowed to pass in NULL as the new position return value.
493 * If it happens, we assign it to a dynamic variable to avoid special cases
494 * in the code below.
496 if (plibNewPosition == 0)
498 plibNewPosition = &newPosition;
502 * The file pointer is moved depending on the given "function"
503 * parameter.
505 switch (dwOrigin)
507 case STREAM_SEEK_SET:
508 plibNewPosition->HighPart = 0;
509 plibNewPosition->LowPart = 0;
510 break;
511 case STREAM_SEEK_CUR:
512 *plibNewPosition = This->currentPosition;
513 break;
514 case STREAM_SEEK_END:
515 *plibNewPosition = This->streamSize;
516 break;
517 default:
518 return STG_E_INVALIDFUNCTION;
522 * We don't support files with offsets of 64 bits.
524 assert(dlibMove.HighPart == 0);
527 * Check if we end-up before the beginning of the file. That should trigger an
528 * error.
530 if ( (dlibMove.LowPart<0) && (plibNewPosition->LowPart < (ULONG)(-dlibMove.LowPart)) )
533 * I don't know what error to send there.
535 return E_FAIL;
539 * Move the actual file pointer
540 * If the file pointer ends-up after the end of the stream, the next Write operation will
541 * make the file larger. This is how it is documented.
543 plibNewPosition->LowPart += dlibMove.LowPart;
544 This->currentPosition = *plibNewPosition;
546 return S_OK;
549 /***
550 * This method is part of the IStream interface.
552 * It will change the size of a stream.
554 * TODO: Switch from small blocks to big blocks and vice versa.
556 * See the documentation of IStream for more info.
558 HRESULT WINAPI StgStreamImpl_SetSize(
559 IStream* iface,
560 ULARGE_INTEGER libNewSize) /* [in] */
562 StgStreamImpl* const This=(StgStreamImpl*)iface;
564 StgProperty curProperty;
565 BOOL Success;
567 TRACE(storage, "(%p, %ld)\n", iface, libNewSize.LowPart);
570 * As documented.
572 if (libNewSize.HighPart != 0)
573 return STG_E_INVALIDFUNCTION;
575 if (This->streamSize.LowPart == libNewSize.LowPart)
576 return S_OK;
579 * This will happen if we're creating a stream
581 if ((This->smallBlockChain == 0) && (This->bigBlockChain == 0))
583 if (libNewSize.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
585 This->smallBlockChain = SmallBlockChainStream_Construct(
586 This->parentStorage->ancestorStorage,
587 This->ownerProperty);
589 else
591 This->bigBlockChain = BlockChainStream_Construct(
592 This->parentStorage->ancestorStorage,
593 NULL,
594 This->ownerProperty);
599 * Read this stream's property to see if it's small blocks or big blocks
601 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
602 This->ownerProperty,
603 &curProperty);
605 * Determine if we have to switch from small to big blocks or vice versa
607 if ( (This->smallBlockChain!=0) &&
608 (curProperty.size.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
610 if (libNewSize.LowPart >= LIMIT_TO_USE_SMALL_BLOCK)
613 * Transform the small block chain into a big block chain
615 This->bigBlockChain = Storage32Impl_SmallBlocksToBigBlocks(
616 This->parentStorage->ancestorStorage,
617 &This->smallBlockChain);
621 if (This->smallBlockChain!=0)
623 Success = SmallBlockChainStream_SetSize(This->smallBlockChain, libNewSize);
625 else
627 Success = BlockChainStream_SetSize(This->bigBlockChain, libNewSize);
631 * Write to the property the new information about this stream
633 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
634 This->ownerProperty,
635 &curProperty);
637 curProperty.size.HighPart = libNewSize.HighPart;
638 curProperty.size.LowPart = libNewSize.LowPart;
640 if (Success)
642 StorageImpl_WriteProperty(This->parentStorage->ancestorStorage,
643 This->ownerProperty,
644 &curProperty);
647 This->streamSize = libNewSize;
649 return S_OK;
652 /***
653 * This method is part of the IStream interface.
655 * It will copy the 'cb' Bytes to 'pstm' IStream.
657 * See the documentation of IStream for more info.
659 HRESULT WINAPI StgStreamImpl_CopyTo(
660 IStream* iface,
661 IStream* pstm, /* [unique][in] */
662 ULARGE_INTEGER cb, /* [in] */
663 ULARGE_INTEGER* pcbRead, /* [out] */
664 ULARGE_INTEGER* pcbWritten) /* [out] */
666 HRESULT hr = S_OK;
667 BYTE tmpBuffer[128];
668 ULONG bytesRead, bytesWritten, copySize;
669 ULARGE_INTEGER totalBytesRead;
670 ULARGE_INTEGER totalBytesWritten;
672 TRACE(storage, "(%p, %p, %ld, %p, %p)\n",
673 iface, pstm, cb.LowPart, pcbRead, pcbWritten);
676 * Sanity check
678 if ( pstm == 0 )
679 return STG_E_INVALIDPOINTER;
681 totalBytesRead.LowPart = totalBytesRead.HighPart = 0;
682 totalBytesWritten.LowPart = totalBytesWritten.HighPart = 0;
685 * use stack to store data temporarly
686 * there is surely more performant way of doing it, for now this basic
687 * implementation will do the job
689 while ( cb.LowPart > 0 )
691 if ( cb.LowPart >= 128 )
692 copySize = 128;
693 else
694 copySize = cb.LowPart;
696 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
698 totalBytesRead.LowPart += bytesRead;
700 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
702 totalBytesWritten.LowPart += bytesWritten;
705 * Check that read & write operations were succesfull
707 if (bytesRead != bytesWritten)
709 hr = STG_E_MEDIUMFULL;
710 break;
713 if (bytesRead!=copySize)
714 cb.LowPart = 0;
715 else
716 cb.LowPart -= bytesRead;
720 * Update number of bytes read and written
722 if (pcbRead)
724 pcbRead->LowPart = totalBytesRead.LowPart;
725 pcbRead->HighPart = totalBytesRead.HighPart;
728 if (pcbWritten)
730 pcbWritten->LowPart = totalBytesWritten.LowPart;
731 pcbWritten->HighPart = totalBytesWritten.HighPart;
733 return hr;
736 /***
737 * This method is part of the IStream interface.
739 * For streams contained in structured storages, this method
740 * does nothing. This is what the documentation tells us.
742 * See the documentation of IStream for more info.
744 HRESULT WINAPI StgStreamImpl_Commit(
745 IStream* iface,
746 DWORD grfCommitFlags) /* [in] */
748 return S_OK;
751 /***
752 * This method is part of the IStream interface.
754 * For streams contained in structured storages, this method
755 * does nothing. This is what the documentation tells us.
757 * See the documentation of IStream for more info.
759 HRESULT WINAPI StgStreamImpl_Revert(
760 IStream* iface)
762 return S_OK;
765 HRESULT WINAPI StgStreamImpl_LockRegion(
766 IStream* iface,
767 ULARGE_INTEGER libOffset, /* [in] */
768 ULARGE_INTEGER cb, /* [in] */
769 DWORD dwLockType) /* [in] */
771 FIXME(storage, "not implemented!\n");
772 return E_NOTIMPL;
775 HRESULT WINAPI StgStreamImpl_UnlockRegion(
776 IStream* iface,
777 ULARGE_INTEGER libOffset, /* [in] */
778 ULARGE_INTEGER cb, /* [in] */
779 DWORD dwLockType) /* [in] */
781 FIXME(storage, "not implemented!\n");
782 return E_NOTIMPL;
785 /***
786 * This method is part of the IStream interface.
788 * This method returns information about the current
789 * stream.
791 * See the documentation of IStream for more info.
793 HRESULT WINAPI StgStreamImpl_Stat(
794 IStream* iface,
795 STATSTG* pstatstg, /* [out] */
796 DWORD grfStatFlag) /* [in] */
798 StgStreamImpl* const This=(StgStreamImpl*)iface;
800 StgProperty curProperty;
801 BOOL readSucessful;
804 * Read the information from the property.
806 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
807 This->ownerProperty,
808 &curProperty);
810 if (readSucessful)
812 StorageUtl_CopyPropertyToSTATSTG(pstatstg,
813 &curProperty,
814 grfStatFlag);
816 return S_OK;
819 return E_FAIL;
822 HRESULT WINAPI StgStreamImpl_Clone(
823 IStream* iface,
824 IStream** ppstm) /* [out] */
826 FIXME(storage, "not implemented!\n");
827 return E_NOTIMPL;