Fixed some debug message crashes.
[wine/hacks.git] / dlls / ole32 / stg_stream.c
blob90e445dde864731f110aac6f5ccfd1aa119f3fed
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 DWORD grfMode,
62 ULONG ownerProperty)
64 StgStreamImpl* newStream;
66 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
68 if (newStream!=0)
71 * Set-up the virtual function table and reference count.
73 ICOM_VTBL(newStream) = &StgStreamImpl_Vtbl;
74 newStream->ref = 0;
77 * We want to nail-down the reference to the storage in case the
78 * stream out-lives the storage in the client application.
80 newStream->parentStorage = parentStorage;
81 IStorage_AddRef((IStorage*)newStream->parentStorage);
83 newStream->grfMode = grfMode;
84 newStream->ownerProperty = ownerProperty;
87 * Start the stream at the begining.
89 newStream->currentPosition.s.HighPart = 0;
90 newStream->currentPosition.s.LowPart = 0;
93 * Initialize the rest of the data.
95 newStream->streamSize.s.HighPart = 0;
96 newStream->streamSize.s.LowPart = 0;
97 newStream->bigBlockChain = 0;
98 newStream->smallBlockChain = 0;
101 * Read the size from the property and determine if the blocks forming
102 * this stream are large or small.
104 StgStreamImpl_OpenBlockChain(newStream);
107 return newStream;
110 /***
111 * This is the destructor of the StgStreamImpl class.
113 * This method will clean-up all the resources used-up by the given StgStreamImpl
114 * class. The pointer passed-in to this function will be freed and will not
115 * be valid anymore.
117 void StgStreamImpl_Destroy(StgStreamImpl* This)
119 TRACE("(%p)\n", This);
122 * Release the reference we are holding on the parent storage.
124 IStorage_Release((IStorage*)This->parentStorage);
125 This->parentStorage = 0;
128 * Make sure we clean-up the block chain stream objects that we were using.
130 if (This->bigBlockChain != 0)
132 BlockChainStream_Destroy(This->bigBlockChain);
133 This->bigBlockChain = 0;
136 if (This->smallBlockChain != 0)
138 SmallBlockChainStream_Destroy(This->smallBlockChain);
139 This->smallBlockChain = 0;
143 * Finally, free the memory used-up by the class.
145 HeapFree(GetProcessHeap(), 0, This);
148 /***
149 * This implements the IUnknown method QueryInterface for this
150 * class
152 HRESULT WINAPI StgStreamImpl_QueryInterface(
153 IStream* iface,
154 REFIID riid, /* [in] */
155 void** ppvObject) /* [iid_is][out] */
157 StgStreamImpl* const This=(StgStreamImpl*)iface;
160 * Perform a sanity check on the parameters.
162 if (ppvObject==0)
163 return E_INVALIDARG;
166 * Initialize the return parameter.
168 *ppvObject = 0;
171 * Compare the riid with the interface IDs implemented by this object.
173 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
175 *ppvObject = (IStream*)This;
177 else if (memcmp(&IID_IStream, riid, sizeof(IID_IStream)) == 0)
179 *ppvObject = (IStream*)This;
183 * Check that we obtained an interface.
185 if ((*ppvObject)==0)
186 return E_NOINTERFACE;
189 * Query Interface always increases the reference count by one when it is
190 * successful
192 StgStreamImpl_AddRef(iface);
194 return S_OK;;
197 /***
198 * This implements the IUnknown method AddRef for this
199 * class
201 ULONG WINAPI StgStreamImpl_AddRef(
202 IStream* iface)
204 StgStreamImpl* const This=(StgStreamImpl*)iface;
206 This->ref++;
208 return This->ref;
211 /***
212 * This implements the IUnknown method Release for this
213 * class
215 ULONG WINAPI StgStreamImpl_Release(
216 IStream* iface)
218 StgStreamImpl* const This=(StgStreamImpl*)iface;
220 ULONG newRef;
222 This->ref--;
224 newRef = This->ref;
227 * If the reference count goes down to 0, perform suicide.
229 if (newRef==0)
231 StgStreamImpl_Destroy(This);
234 return newRef;
237 /***
238 * This method will open the block chain pointed by the property
239 * that describes the stream.
240 * If the stream's size is null, no chain is opened.
242 void StgStreamImpl_OpenBlockChain(
243 StgStreamImpl* This)
245 StgProperty curProperty;
246 BOOL readSucessful;
249 * Make sure no old object is staying behind.
251 if (This->smallBlockChain != 0)
253 SmallBlockChainStream_Destroy(This->smallBlockChain);
254 This->smallBlockChain = 0;
257 if (This->bigBlockChain != 0)
259 BlockChainStream_Destroy(This->bigBlockChain);
260 This->bigBlockChain = 0;
264 * Read the information from the property.
266 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
267 This->ownerProperty,
268 &curProperty);
270 if (readSucessful)
272 This->streamSize = curProperty.size;
275 * This code supports only streams that are <32 bits in size.
277 assert(This->streamSize.s.HighPart == 0);
279 if(curProperty.startingBlock == BLOCK_END_OF_CHAIN)
281 assert( (This->streamSize.s.HighPart == 0) && (This->streamSize.s.LowPart == 0) );
283 else
285 if ( (This->streamSize.s.HighPart == 0) &&
286 (This->streamSize.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
288 This->smallBlockChain = SmallBlockChainStream_Construct(
289 This->parentStorage->ancestorStorage,
290 This->ownerProperty);
292 else
294 This->bigBlockChain = BlockChainStream_Construct(
295 This->parentStorage->ancestorStorage,
296 NULL,
297 This->ownerProperty);
303 /***
304 * This method is part of the ISequentialStream interface.
306 * If reads a block of information from the stream at the current
307 * position. It then moves the current position at the end of the
308 * read block
310 * See the documentation of ISequentialStream for more info.
312 HRESULT WINAPI StgStreamImpl_Read(
313 IStream* iface,
314 void* pv, /* [length_is][size_is][out] */
315 ULONG cb, /* [in] */
316 ULONG* pcbRead) /* [out] */
318 StgStreamImpl* const This=(StgStreamImpl*)iface;
320 ULONG bytesReadBuffer;
321 ULONG bytesToReadFromBuffer;
323 TRACE("(%p, %p, %ld, %p)\n",
324 iface, pv, cb, pcbRead);
327 * If the caller is not interested in the nubmer of bytes read,
328 * we use another buffer to avoid "if" statements in the code.
330 if (pcbRead==0)
331 pcbRead = &bytesReadBuffer;
334 * Using the known size of the stream, calculate the number of bytes
335 * to read from the block chain
337 bytesToReadFromBuffer = MIN( This->streamSize.s.LowPart - This->currentPosition.s.LowPart, cb);
340 * Depending on the type of chain that was opened when the stream was constructed,
341 * we delegate the work to the method that read the block chains.
343 if (This->smallBlockChain!=0)
345 SmallBlockChainStream_ReadAt(This->smallBlockChain,
346 This->currentPosition,
347 bytesToReadFromBuffer,
349 pcbRead);
352 else if (This->bigBlockChain!=0)
354 BlockChainStream_ReadAt(This->bigBlockChain,
355 This->currentPosition,
356 bytesToReadFromBuffer,
358 pcbRead);
360 else
363 * Small and big block chains are both NULL. This case will happen
364 * when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
367 *pcbRead = 0;
368 return S_OK;
372 * We should always be able to read the proper amount of data from the
373 * chain.
375 assert(bytesToReadFromBuffer == *pcbRead);
378 * Advance the pointer for the number of positions read.
380 This->currentPosition.s.LowPart += *pcbRead;
383 * The function returns S_OK if the buffer was filled completely
384 * it returns S_FALSE if the end of the stream is reached before the
385 * buffer is filled
387 if(*pcbRead == cb)
388 return S_OK;
390 return S_FALSE;
393 /***
394 * This method is part of the ISequentialStream interface.
396 * It writes a block of information to the stream at the current
397 * position. It then moves the current position at the end of the
398 * written block. If the stream is too small to fit the block,
399 * the stream is grown to fit.
401 * See the documentation of ISequentialStream for more info.
403 HRESULT WINAPI StgStreamImpl_Write(
404 IStream* iface,
405 const void* pv, /* [size_is][in] */
406 ULONG cb, /* [in] */
407 ULONG* pcbWritten) /* [out] */
409 StgStreamImpl* const This=(StgStreamImpl*)iface;
411 ULARGE_INTEGER newSize;
412 ULONG bytesWritten = 0;
414 TRACE("(%p, %p, %ld, %p)\n",
415 iface, pv, cb, pcbWritten);
417 if (!(This->grfMode & STGM_WRITE))
418 return STG_E_ACCESSDENIED;
421 * If the caller is not interested in the number of bytes written,
422 * we use another buffer to avoid "if" statements in the code.
424 if (pcbWritten == 0)
425 pcbWritten = &bytesWritten;
428 * Initialize the out parameter
430 *pcbWritten = 0;
433 * Do we have permission to write to this stream?
435 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
436 return STG_E_ACCESSDENIED;
438 if (cb == 0)
440 return S_OK;
442 else
444 newSize.s.HighPart = 0;
445 newSize.s.LowPart = This->currentPosition.s.LowPart + cb;
449 * Verify if we need to grow the stream
451 if (newSize.s.LowPart > This->streamSize.s.LowPart)
453 /* grow stream */
454 IStream_SetSize(iface, newSize);
458 * Depending on the type of chain that was opened when the stream was constructed,
459 * we delegate the work to the method that readwrites to the block chains.
461 if (This->smallBlockChain!=0)
463 SmallBlockChainStream_WriteAt(This->smallBlockChain,
464 This->currentPosition,
467 pcbWritten);
470 else if (This->bigBlockChain!=0)
472 BlockChainStream_WriteAt(This->bigBlockChain,
473 This->currentPosition,
476 pcbWritten);
478 else
479 assert(FALSE);
482 * Advance the position pointer for the number of positions written.
484 This->currentPosition.s.LowPart += *pcbWritten;
486 return S_OK;
489 /***
490 * This method is part of the IStream interface.
492 * It will move the current stream pointer according to the parameters
493 * given.
495 * See the documentation of IStream for more info.
497 HRESULT WINAPI StgStreamImpl_Seek(
498 IStream* iface,
499 LARGE_INTEGER dlibMove, /* [in] */
500 DWORD dwOrigin, /* [in] */
501 ULARGE_INTEGER* plibNewPosition) /* [out] */
503 StgStreamImpl* const This=(StgStreamImpl*)iface;
505 ULARGE_INTEGER newPosition;
507 TRACE("(%p, %ld, %ld, %p)\n",
508 iface, dlibMove.s.LowPart, dwOrigin, plibNewPosition);
511 * The caller is allowed to pass in NULL as the new position return value.
512 * If it happens, we assign it to a dynamic variable to avoid special cases
513 * in the code below.
515 if (plibNewPosition == 0)
517 plibNewPosition = &newPosition;
521 * The file pointer is moved depending on the given "function"
522 * parameter.
524 switch (dwOrigin)
526 case STREAM_SEEK_SET:
527 plibNewPosition->s.HighPart = 0;
528 plibNewPosition->s.LowPart = 0;
529 break;
530 case STREAM_SEEK_CUR:
531 *plibNewPosition = This->currentPosition;
532 break;
533 case STREAM_SEEK_END:
534 *plibNewPosition = This->streamSize;
535 break;
536 default:
537 return STG_E_INVALIDFUNCTION;
540 #if SIZEOF_LONG_LONG >= 8
541 plibNewPosition->QuadPart += dlibMove.QuadPart;
542 #else
544 * do some multiword arithmetic:
545 * treat HighPart as a signed value
546 * treat LowPart as unsigned
547 * NOTE: this stuff is two's complement specific!
549 if (dlibMove.s.HighPart < 0) { /* dlibMove is < 0 */
550 /* calculate the absolute value of dlibMove ... */
551 dlibMove.s.HighPart = -dlibMove.s.HighPart;
552 dlibMove.s.LowPart ^= -1;
553 /* ... and subtract with carry */
554 if (dlibMove.s.LowPart > plibNewPosition->s.LowPart) {
555 /* carry needed, This accounts for any underflows at [1]*/
556 plibNewPosition->s.HighPart -= 1;
558 plibNewPosition->s.LowPart -= dlibMove.s.LowPart; /* [1] */
559 plibNewPosition->s.HighPart -= dlibMove.s.HighPart;
560 } else {
561 /* add directly */
562 int initialLowPart = plibNewPosition->s.LowPart;
563 plibNewPosition->s.LowPart += dlibMove.s.LowPart;
564 if((plibNewPosition->s.LowPart < initialLowPart) ||
565 (plibNewPosition->s.LowPart < dlibMove.s.LowPart)) {
566 /* LowPart has rolled over => add the carry digit to HighPart */
567 plibNewPosition->s.HighPart++;
569 plibNewPosition->s.HighPart += dlibMove.s.HighPart;
572 * Check if we end-up before the beginning of the file. That should
573 * trigger an error.
575 if (plibNewPosition->s.HighPart < 0) {
576 return STG_E_INVALIDPOINTER;
580 * We currently don't support files with offsets of >32 bits.
581 * Note that we have checked for a negative offset already
583 assert(plibNewPosition->s.HighPart <= 0);
585 #endif
588 * tell the caller what we calculated
590 This->currentPosition = *plibNewPosition;
592 return S_OK;
595 /***
596 * This method is part of the IStream interface.
598 * It will change the size of a stream.
600 * TODO: Switch from small blocks to big blocks and vice versa.
602 * See the documentation of IStream for more info.
604 HRESULT WINAPI StgStreamImpl_SetSize(
605 IStream* iface,
606 ULARGE_INTEGER libNewSize) /* [in] */
608 StgStreamImpl* const This=(StgStreamImpl*)iface;
610 StgProperty curProperty;
611 BOOL Success;
613 TRACE("(%p, %ld)\n", iface, libNewSize.s.LowPart);
616 * As documented.
618 if (libNewSize.s.HighPart != 0)
619 return STG_E_INVALIDFUNCTION;
622 * Do we have permission?
624 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
625 return STG_E_ACCESSDENIED;
627 if (This->streamSize.s.LowPart == libNewSize.s.LowPart)
628 return S_OK;
631 * This will happen if we're creating a stream
633 if ((This->smallBlockChain == 0) && (This->bigBlockChain == 0))
635 if (libNewSize.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
637 This->smallBlockChain = SmallBlockChainStream_Construct(
638 This->parentStorage->ancestorStorage,
639 This->ownerProperty);
641 else
643 This->bigBlockChain = BlockChainStream_Construct(
644 This->parentStorage->ancestorStorage,
645 NULL,
646 This->ownerProperty);
651 * Read this stream's property to see if it's small blocks or big blocks
653 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
654 This->ownerProperty,
655 &curProperty);
657 * Determine if we have to switch from small to big blocks or vice versa
659 if ( (This->smallBlockChain!=0) &&
660 (curProperty.size.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
662 if (libNewSize.s.LowPart >= LIMIT_TO_USE_SMALL_BLOCK)
665 * Transform the small block chain into a big block chain
667 This->bigBlockChain = Storage32Impl_SmallBlocksToBigBlocks(
668 This->parentStorage->ancestorStorage,
669 &This->smallBlockChain);
673 if (This->smallBlockChain!=0)
675 Success = SmallBlockChainStream_SetSize(This->smallBlockChain, libNewSize);
677 else
679 Success = BlockChainStream_SetSize(This->bigBlockChain, libNewSize);
683 * Write to the property the new information about this stream
685 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
686 This->ownerProperty,
687 &curProperty);
689 curProperty.size.s.HighPart = libNewSize.s.HighPart;
690 curProperty.size.s.LowPart = libNewSize.s.LowPart;
692 if (Success)
694 StorageImpl_WriteProperty(This->parentStorage->ancestorStorage,
695 This->ownerProperty,
696 &curProperty);
699 This->streamSize = libNewSize;
701 return S_OK;
704 /***
705 * This method is part of the IStream interface.
707 * It will copy the 'cb' Bytes to 'pstm' IStream.
709 * See the documentation of IStream for more info.
711 HRESULT WINAPI StgStreamImpl_CopyTo(
712 IStream* iface,
713 IStream* pstm, /* [unique][in] */
714 ULARGE_INTEGER cb, /* [in] */
715 ULARGE_INTEGER* pcbRead, /* [out] */
716 ULARGE_INTEGER* pcbWritten) /* [out] */
718 HRESULT hr = S_OK;
719 BYTE tmpBuffer[128];
720 ULONG bytesRead, bytesWritten, copySize;
721 ULARGE_INTEGER totalBytesRead;
722 ULARGE_INTEGER totalBytesWritten;
724 TRACE("(%p, %p, %ld, %p, %p)\n",
725 iface, pstm, cb.s.LowPart, pcbRead, pcbWritten);
728 * Sanity check
730 if ( pstm == 0 )
731 return STG_E_INVALIDPOINTER;
733 totalBytesRead.s.LowPart = totalBytesRead.s.HighPart = 0;
734 totalBytesWritten.s.LowPart = totalBytesWritten.s.HighPart = 0;
737 * use stack to store data temporarly
738 * there is surely more performant way of doing it, for now this basic
739 * implementation will do the job
741 while ( cb.s.LowPart > 0 )
743 if ( cb.s.LowPart >= 128 )
744 copySize = 128;
745 else
746 copySize = cb.s.LowPart;
748 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
750 totalBytesRead.s.LowPart += bytesRead;
752 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
754 totalBytesWritten.s.LowPart += bytesWritten;
757 * Check that read & write operations were succesfull
759 if (bytesRead != bytesWritten)
761 hr = STG_E_MEDIUMFULL;
762 break;
765 if (bytesRead!=copySize)
766 cb.s.LowPart = 0;
767 else
768 cb.s.LowPart -= bytesRead;
772 * Update number of bytes read and written
774 if (pcbRead)
776 pcbRead->s.LowPart = totalBytesRead.s.LowPart;
777 pcbRead->s.HighPart = totalBytesRead.s.HighPart;
780 if (pcbWritten)
782 pcbWritten->s.LowPart = totalBytesWritten.s.LowPart;
783 pcbWritten->s.HighPart = totalBytesWritten.s.HighPart;
785 return hr;
788 /***
789 * This method is part of the IStream interface.
791 * For streams contained in structured storages, this method
792 * does nothing. This is what the documentation tells us.
794 * See the documentation of IStream for more info.
796 HRESULT WINAPI StgStreamImpl_Commit(
797 IStream* iface,
798 DWORD grfCommitFlags) /* [in] */
800 return S_OK;
803 /***
804 * This method is part of the IStream interface.
806 * For streams contained in structured storages, this method
807 * does nothing. This is what the documentation tells us.
809 * See the documentation of IStream for more info.
811 HRESULT WINAPI StgStreamImpl_Revert(
812 IStream* iface)
814 return S_OK;
817 HRESULT WINAPI StgStreamImpl_LockRegion(
818 IStream* iface,
819 ULARGE_INTEGER libOffset, /* [in] */
820 ULARGE_INTEGER cb, /* [in] */
821 DWORD dwLockType) /* [in] */
823 FIXME("not implemented!\n");
824 return E_NOTIMPL;
827 HRESULT WINAPI StgStreamImpl_UnlockRegion(
828 IStream* iface,
829 ULARGE_INTEGER libOffset, /* [in] */
830 ULARGE_INTEGER cb, /* [in] */
831 DWORD dwLockType) /* [in] */
833 FIXME("not implemented!\n");
834 return E_NOTIMPL;
837 /***
838 * This method is part of the IStream interface.
840 * This method returns information about the current
841 * stream.
843 * See the documentation of IStream for more info.
845 HRESULT WINAPI StgStreamImpl_Stat(
846 IStream* iface,
847 STATSTG* pstatstg, /* [out] */
848 DWORD grfStatFlag) /* [in] */
850 StgStreamImpl* const This=(StgStreamImpl*)iface;
852 StgProperty curProperty;
853 BOOL readSucessful;
856 * Read the information from the property.
858 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
859 This->ownerProperty,
860 &curProperty);
862 if (readSucessful)
864 StorageUtl_CopyPropertyToSTATSTG(pstatstg,
865 &curProperty,
866 grfStatFlag);
868 pstatstg->grfMode = This->grfMode;
870 return S_OK;
873 return E_FAIL;
876 HRESULT WINAPI StgStreamImpl_Clone(
877 IStream* iface,
878 IStream** ppstm) /* [out] */
880 FIXME("not implemented!\n");
881 return E_NOTIMPL;