New debug scheme with explicit debug channels declaration.
[wine/hacks.git] / ole / stg_stream.c
blob906c8bbdd76864957681a63423c82db9d5071cf2
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(ole)
27 * Virtual function table for the StgStreamImpl class.
29 static ICOM_VTABLE(IStream) StgStreamImpl_Vtbl =
31 StgStreamImpl_QueryInterface,
32 StgStreamImpl_AddRef,
33 StgStreamImpl_Release,
34 StgStreamImpl_Read,
35 StgStreamImpl_Write,
36 StgStreamImpl_Seek,
37 StgStreamImpl_SetSize,
38 StgStreamImpl_CopyTo,
39 StgStreamImpl_Commit,
40 StgStreamImpl_Revert,
41 StgStreamImpl_LockRegion,
42 StgStreamImpl_UnlockRegion,
43 StgStreamImpl_Stat,
44 StgStreamImpl_Clone
47 /******************************************************************************
48 ** StgStreamImpl implementation
51 /***
52 * This is the constructor for the StgStreamImpl class.
54 * Params:
55 * parentStorage - Pointer to the storage that contains the stream to open
56 * ownerProperty - Index of the property that points to this stream.
58 StgStreamImpl* StgStreamImpl_Construct(
59 StorageBaseImpl* parentStorage,
60 ULONG ownerProperty)
62 StgStreamImpl* newStream;
64 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
66 if (newStream!=0)
69 * Set-up the virtual function table and reference count.
71 newStream->lpvtbl = &StgStreamImpl_Vtbl;
72 newStream->ref = 0;
75 * We want to nail-down the reference to the storage in case the
76 * stream out-lives the storage in the client application.
78 newStream->parentStorage = parentStorage;
79 IStorage_AddRef((IStorage*)newStream->parentStorage);
81 newStream->ownerProperty = ownerProperty;
84 * Start the stream at the begining.
86 newStream->currentPosition.HighPart = 0;
87 newStream->currentPosition.LowPart = 0;
90 * Initialize the rest of the data.
92 newStream->streamSize.HighPart = 0;
93 newStream->streamSize.LowPart = 0;
94 newStream->bigBlockChain = 0;
95 newStream->smallBlockChain = 0;
98 * Read the size from the property and determine if the blocks forming
99 * this stream are large or small.
101 StgStreamImpl_OpenBlockChain(newStream);
104 return newStream;
107 /***
108 * This is the destructor of the StgStreamImpl class.
110 * This method will clean-up all the resources used-up by the given StgStreamImpl
111 * class. The pointer passed-in to this function will be freed and will not
112 * be valid anymore.
114 void StgStreamImpl_Destroy(StgStreamImpl* This)
117 * Release the reference we are holding on the parent storage.
119 IStorage_Release((IStorage*)This->parentStorage);
120 This->parentStorage = 0;
123 * Make sure we clean-up the block chain stream objects that we were using.
125 if (This->bigBlockChain != 0)
127 BlockChainStream_Destroy(This->bigBlockChain);
128 This->bigBlockChain = 0;
131 if (This->smallBlockChain != 0)
133 SmallBlockChainStream_Destroy(This->smallBlockChain);
134 This->smallBlockChain = 0;
138 * Finally, free the memory used-up by the class.
140 HeapFree(GetProcessHeap(), 0, This);
143 /***
144 * This implements the IUnknown method QueryInterface for this
145 * class
147 HRESULT WINAPI StgStreamImpl_QueryInterface(
148 IStream* iface,
149 REFIID riid, /* [in] */
150 void** ppvObject) /* [iid_is][out] */
152 StgStreamImpl* const This=(StgStreamImpl*)iface;
155 * Perform a sanity check on the parameters.
157 if (ppvObject==0)
158 return E_INVALIDARG;
161 * Initialize the return parameter.
163 *ppvObject = 0;
166 * Compare the riid with the interface IDs implemented by this object.
168 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
170 *ppvObject = (IStream*)This;
172 else if (memcmp(&IID_IStorage, riid, sizeof(IID_IStream)) == 0)
174 *ppvObject = (IStream*)This;
178 * Check that we obtained an interface.
180 if ((*ppvObject)==0)
181 return E_NOINTERFACE;
184 * Query Interface always increases the reference count by one when it is
185 * successful
187 StgStreamImpl_AddRef(iface);
189 return S_OK;;
192 /***
193 * This implements the IUnknown method AddRef for this
194 * class
196 ULONG WINAPI StgStreamImpl_AddRef(
197 IStream* iface)
199 StgStreamImpl* const This=(StgStreamImpl*)iface;
201 This->ref++;
203 return This->ref;
206 /***
207 * This implements the IUnknown method Release for this
208 * class
210 ULONG WINAPI StgStreamImpl_Release(
211 IStream* iface)
213 StgStreamImpl* const This=(StgStreamImpl*)iface;
215 ULONG newRef;
217 This->ref--;
219 newRef = This->ref;
222 * If the reference count goes down to 0, perform suicide.
224 if (newRef==0)
226 StgStreamImpl_Destroy(This);
229 return newRef;
232 /***
233 * This method will open the block chain pointed by the property
234 * that describes the stream.
235 * If the stream's size is null, no chain is opened.
237 void StgStreamImpl_OpenBlockChain(
238 StgStreamImpl* This)
240 StgProperty curProperty;
241 BOOL readSucessful;
244 * Make sure no old object is staying behind.
246 if (This->smallBlockChain != 0)
248 SmallBlockChainStream_Destroy(This->smallBlockChain);
249 This->smallBlockChain = 0;
252 if (This->bigBlockChain != 0)
254 BlockChainStream_Destroy(This->bigBlockChain);
255 This->bigBlockChain = 0;
259 * Read the information from the property.
261 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
262 This->ownerProperty,
263 &curProperty);
265 if (readSucessful)
267 This->streamSize = curProperty.size;
270 * This code supports only streams that are <32 bits in size.
272 assert(This->streamSize.HighPart == 0);
274 if(curProperty.startingBlock == BLOCK_END_OF_CHAIN)
276 assert( (This->streamSize.HighPart == 0) && (This->streamSize.LowPart == 0) );
278 else
280 if ( (This->streamSize.HighPart == 0) &&
281 (This->streamSize.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
283 This->smallBlockChain = SmallBlockChainStream_Construct(
284 This->parentStorage->ancestorStorage,
285 This->ownerProperty);
287 else
289 This->bigBlockChain = BlockChainStream_Construct(
290 This->parentStorage->ancestorStorage,
291 NULL,
292 This->ownerProperty);
298 /***
299 * This method is part of the ISequentialStream interface.
301 * If reads a block of information from the stream at the current
302 * position. It then moves the current position at the end of the
303 * read block
305 * See the documentation of ISequentialStream for more info.
307 HRESULT WINAPI StgStreamImpl_Read(
308 IStream* iface,
309 void* pv, /* [length_is][size_is][out] */
310 ULONG cb, /* [in] */
311 ULONG* pcbRead) /* [out] */
313 StgStreamImpl* const This=(StgStreamImpl*)iface;
315 ULONG bytesReadBuffer;
316 ULONG bytesToReadFromBuffer;
319 * If the caller is not interested in the nubmer of bytes read,
320 * we use another buffer to avoid "if" statements in the code.
322 if (pcbRead==0)
323 pcbRead = &bytesReadBuffer;
326 * Using the known size of the stream, calculate the number of bytes
327 * to read from the block chain
329 bytesToReadFromBuffer = MIN( This->streamSize.LowPart - This->currentPosition.LowPart, cb);
332 * Depending on the type of chain that was opened when the stream was constructed,
333 * we delegate the work to the method that read the block chains.
335 if (This->smallBlockChain!=0)
337 SmallBlockChainStream_ReadAt(This->smallBlockChain,
338 This->currentPosition,
339 bytesToReadFromBuffer,
341 pcbRead);
344 else if (This->bigBlockChain!=0)
346 BlockChainStream_ReadAt(This->bigBlockChain,
347 This->currentPosition,
348 bytesToReadFromBuffer,
350 pcbRead);
352 else
353 assert(FALSE);
356 * We should always be able to read the proper amount of data from the
357 * chain.
359 assert(bytesToReadFromBuffer == *pcbRead);
362 * Advance the pointer for the number of positions read.
364 This->currentPosition.LowPart += *pcbRead;
367 * The function returns S_OK if the buffer was filled completely
368 * it returns S_FALSE if the end of the stream is reached before the
369 * buffer is filled
371 if(*pcbRead == cb)
372 return S_OK;
374 return S_FALSE;
377 /***
378 * This method is part of the ISequentialStream interface.
380 * It writes a block of information to the stream at the current
381 * position. It then moves the current position at the end of the
382 * written block. If the stream is too small to fit the block,
383 * the stream is grown to fit.
385 * See the documentation of ISequentialStream for more info.
387 HRESULT WINAPI StgStreamImpl_Write(
388 IStream* iface,
389 const void* pv, /* [size_is][in] */
390 ULONG cb, /* [in] */
391 ULONG* pcbWritten) /* [out] */
393 StgStreamImpl* const This=(StgStreamImpl*)iface;
395 ULARGE_INTEGER newSize;
396 ULONG bytesWritten = 0;
399 * If the caller is not interested in the number of bytes written,
400 * we use another buffer to avoid "if" statements in the code.
402 if (pcbWritten == 0)
403 pcbWritten = &bytesWritten;
405 if (cb == 0)
407 return S_OK;
409 else
411 newSize.HighPart = 0;
412 newSize.LowPart = This->currentPosition.LowPart + cb;
416 * Verify if we need to grow the stream
418 if (newSize.LowPart > This->streamSize.LowPart)
420 /* grow stream */
421 StgStreamImpl_SetSize(iface, newSize);
425 * Depending on the type of chain that was opened when the stream was constructed,
426 * we delegate the work to the method that readwrites to the block chains.
428 if (This->smallBlockChain!=0)
430 SmallBlockChainStream_WriteAt(This->smallBlockChain,
431 This->currentPosition,
434 pcbWritten);
437 else if (This->bigBlockChain!=0)
439 BlockChainStream_WriteAt(This->bigBlockChain,
440 This->currentPosition,
443 pcbWritten);
445 else
446 assert(FALSE);
449 * Advance the position pointer for the number of positions written.
451 This->currentPosition.LowPart += *pcbWritten;
453 return S_OK;
456 /***
457 * This method is part of the IStream interface.
459 * It will move the current stream pointer according to the parameters
460 * given.
462 * See the documentation of IStream for more info.
464 HRESULT WINAPI StgStreamImpl_Seek(
465 IStream* iface,
466 LARGE_INTEGER dlibMove, /* [in] */
467 DWORD dwOrigin, /* [in] */
468 ULARGE_INTEGER* plibNewPosition) /* [out] */
470 StgStreamImpl* const This=(StgStreamImpl*)iface;
472 ULARGE_INTEGER newPosition;
475 * The caller is allowed to pass in NULL as the new position return value.
476 * If it happens, we assign it to a dynamic variable to avoid special cases
477 * in the code below.
479 if (plibNewPosition == 0)
481 plibNewPosition = &newPosition;
485 * The file pointer is moved depending on the given "function"
486 * parameter.
488 switch (dwOrigin)
490 case STREAM_SEEK_SET:
491 plibNewPosition->HighPart = 0;
492 plibNewPosition->LowPart = 0;
493 break;
494 case STREAM_SEEK_CUR:
495 *plibNewPosition = This->currentPosition;
496 break;
497 case STREAM_SEEK_END:
498 *plibNewPosition = This->streamSize;
499 break;
500 default:
501 return STG_E_INVALIDFUNCTION;
505 * We don't support files with offsets of 64 bits.
507 assert(dlibMove.HighPart == 0);
510 * Check if we end-up before the beginning of the file. That should trigger an
511 * error.
513 if ( (dlibMove.LowPart<0) && (plibNewPosition->LowPart < (ULONG)(-dlibMove.LowPart)) )
516 * I don't know what error to send there.
518 return E_FAIL;
522 * Move the actual file pointer
523 * If the file pointer ends-up after the end of the stream, the next Write operation will
524 * make the file larger. This is how it is documented.
526 plibNewPosition->LowPart += dlibMove.LowPart;
527 This->currentPosition = *plibNewPosition;
529 return S_OK;
532 /***
533 * This method is part of the IStream interface.
535 * It will change the size of a stream.
537 * TODO: Switch from small blocks to big blocks and vice versa.
539 * See the documentation of IStream for more info.
541 HRESULT WINAPI StgStreamImpl_SetSize(
542 IStream* iface,
543 ULARGE_INTEGER libNewSize) /* [in] */
545 StgStreamImpl* const This=(StgStreamImpl*)iface;
547 StgProperty curProperty;
548 BOOL Success;
551 * As documented.
553 if (libNewSize.HighPart != 0)
554 return STG_E_INVALIDFUNCTION;
556 if (This->streamSize.LowPart == libNewSize.LowPart)
557 return S_OK;
560 * This will happen if we're creating a stream
562 if ((This->smallBlockChain == 0) && (This->bigBlockChain == 0))
564 if (libNewSize.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
566 This->smallBlockChain = SmallBlockChainStream_Construct(
567 This->parentStorage->ancestorStorage,
568 This->ownerProperty);
570 else
572 This->bigBlockChain = BlockChainStream_Construct(
573 This->parentStorage->ancestorStorage,
574 NULL,
575 This->ownerProperty);
580 * Read this stream's property to see if it's small blocks or big blocks
582 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
583 This->ownerProperty,
584 &curProperty);
586 * Determine if we have to switch from small to big blocks or vice versa
589 if (curProperty.size.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
591 if (libNewSize.LowPart >= LIMIT_TO_USE_SMALL_BLOCK)
594 * Transform the small block chain into a big block chain
596 This->bigBlockChain = Storage32Impl_SmallBlocksToBigBlocks(
597 This->parentStorage->ancestorStorage,
598 &This->smallBlockChain);
602 if (This->smallBlockChain!=0)
604 Success = SmallBlockChainStream_SetSize(This->smallBlockChain, libNewSize);
606 else
608 Success = BlockChainStream_SetSize(This->bigBlockChain, libNewSize);
612 * Write to the property the new information about this stream
614 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
615 This->ownerProperty,
616 &curProperty);
618 curProperty.size.HighPart = libNewSize.HighPart;
619 curProperty.size.LowPart = libNewSize.LowPart;
621 if (Success)
623 StorageImpl_WriteProperty(This->parentStorage->ancestorStorage,
624 This->ownerProperty,
625 &curProperty);
628 This->streamSize = libNewSize;
630 return S_OK;
633 /***
634 * This method is part of the IStream interface.
636 * It will copy the 'cb' Bytes to 'pstm' IStream.
638 * See the documentation of IStream for more info.
640 HRESULT WINAPI StgStreamImpl_CopyTo(
641 IStream* iface,
642 IStream* pstm, /* [unique][in] */
643 ULARGE_INTEGER cb, /* [in] */
644 ULARGE_INTEGER* pcbRead, /* [out] */
645 ULARGE_INTEGER* pcbWritten) /* [out] */
647 StgStreamImpl* const This=(StgStreamImpl*)iface;
648 HRESULT hr = S_OK;
649 BYTE tmpBuffer[128];
650 ULONG bytesRead, bytesWritten, copySize;
651 ULARGE_INTEGER totalBytesRead;
652 ULARGE_INTEGER totalBytesWritten;
655 * Sanity check
657 if ( pstm == 0 )
658 return STG_E_INVALIDPOINTER;
660 totalBytesRead.LowPart = totalBytesRead.HighPart = 0;
661 totalBytesWritten.LowPart = totalBytesWritten.HighPart = 0;
664 * use stack to store data temporarly
665 * there is surely more performant way of doing it, for now this basic
666 * implementation will do the job
668 while ( cb.LowPart > 0 )
670 if ( cb.LowPart >= 128 )
671 copySize = 128;
672 else
673 copySize = cb.LowPart;
675 StgStreamImpl_Read(iface, tmpBuffer, 128, &bytesRead);
677 totalBytesRead.LowPart += bytesRead;
679 StgStreamImpl_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
681 totalBytesWritten.LowPart += bytesWritten;
684 * Check that read & write operations were succesfull
686 if ( (bytesRead != copySize) && (bytesWritten != copySize) )
688 hr = STG_E_MEDIUMFULL;
689 break;
692 cb.LowPart = cb.LowPart - copySize;
696 * Update number of bytes read and written
698 if (pcbRead)
700 pcbRead->LowPart = totalBytesRead.LowPart;
701 pcbRead->HighPart = totalBytesRead.HighPart;
704 if (pcbWritten)
706 pcbWritten->LowPart = totalBytesWritten.LowPart;
707 pcbWritten->HighPart = totalBytesWritten.HighPart;
709 return hr;
712 /***
713 * This method is part of the IStream interface.
715 * For streams contained in structured storages, this method
716 * does nothing. This is what the documentation tells us.
718 * See the documentation of IStream for more info.
720 HRESULT WINAPI StgStreamImpl_Commit(
721 IStream* iface,
722 DWORD grfCommitFlags) /* [in] */
724 return S_OK;
727 /***
728 * This method is part of the IStream interface.
730 * For streams contained in structured storages, this method
731 * does nothing. This is what the documentation tells us.
733 * See the documentation of IStream for more info.
735 HRESULT WINAPI StgStreamImpl_Revert(
736 IStream* iface)
738 return S_OK;
741 HRESULT WINAPI StgStreamImpl_LockRegion(
742 IStream* iface,
743 ULARGE_INTEGER libOffset, /* [in] */
744 ULARGE_INTEGER cb, /* [in] */
745 DWORD dwLockType) /* [in] */
747 FIXME(ole, "not implemented!\n");
748 return E_NOTIMPL;
751 HRESULT WINAPI StgStreamImpl_UnlockRegion(
752 IStream* iface,
753 ULARGE_INTEGER libOffset, /* [in] */
754 ULARGE_INTEGER cb, /* [in] */
755 DWORD dwLockType) /* [in] */
757 FIXME(ole, "not implemented!\n");
758 return E_NOTIMPL;
761 /***
762 * This method is part of the IStream interface.
764 * This method returns information about the current
765 * stream.
767 * See the documentation of IStream for more info.
769 HRESULT WINAPI StgStreamImpl_Stat(
770 IStream* iface,
771 STATSTG* pstatstg, /* [out] */
772 DWORD grfStatFlag) /* [in] */
774 StgStreamImpl* const This=(StgStreamImpl*)iface;
776 StgProperty curProperty;
777 BOOL readSucessful;
780 * Read the information from the property.
782 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
783 This->ownerProperty,
784 &curProperty);
786 if (readSucessful)
788 StorageUtl_CopyPropertyToSTATSTG(pstatstg,
789 &curProperty,
790 grfStatFlag);
792 return S_OK;
795 return E_FAIL;
798 HRESULT WINAPI StgStreamImpl_Clone(
799 IStream* iface,
800 IStream** ppstm) /* [out] */
802 FIXME(ole, "not implemented!\n");
803 return E_NOTIMPL;