msvcrt: Reworked _fullpath.
[wine/wine-kai.git] / dlls / ole32 / stg_stream.c
blob19b8bda5a7d252b66be8ca1b5d97cd32c05c1524
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);
48 /***
49 * This is the destructor of the StgStreamImpl class.
51 * This method will clean-up all the resources used-up by the given StgStreamImpl
52 * class. The pointer passed-in to this function will be freed and will not
53 * be valid anymore.
55 static void StgStreamImpl_Destroy(StgStreamImpl* This)
57 TRACE("(%p)\n", This);
60 * Release the reference we are holding on the parent storage.
62 IStorage_Release((IStorage*)This->parentStorage);
63 This->parentStorage = 0;
66 * Make sure we clean-up the block chain stream objects that we were using.
68 if (This->bigBlockChain != 0)
70 BlockChainStream_Destroy(This->bigBlockChain);
71 This->bigBlockChain = 0;
74 if (This->smallBlockChain != 0)
76 SmallBlockChainStream_Destroy(This->smallBlockChain);
77 This->smallBlockChain = 0;
81 * Finally, free the memory used-up by the class.
83 HeapFree(GetProcessHeap(), 0, This);
86 /***
87 * This implements the IUnknown method QueryInterface for this
88 * class
90 static HRESULT WINAPI StgStreamImpl_QueryInterface(
91 IStream* iface,
92 REFIID riid, /* [in] */
93 void** ppvObject) /* [iid_is][out] */
95 StgStreamImpl* const This=(StgStreamImpl*)iface;
98 * Perform a sanity check on the parameters.
100 if (ppvObject==0)
101 return E_INVALIDARG;
104 * Initialize the return parameter.
106 *ppvObject = 0;
109 * Compare the riid with the interface IDs implemented by this object.
111 if (IsEqualGUID(&IID_IUnknown, riid)||
112 IsEqualGUID(&IID_IStream, riid))
114 *ppvObject = (IStream*)This;
118 * Check that we obtained an interface.
120 if ((*ppvObject)==0)
121 return E_NOINTERFACE;
124 * Query Interface always increases the reference count by one when it is
125 * successful
127 IStream_AddRef(iface);
129 return S_OK;
132 /***
133 * This implements the IUnknown method AddRef for this
134 * class
136 static ULONG WINAPI StgStreamImpl_AddRef(
137 IStream* iface)
139 StgStreamImpl* const This=(StgStreamImpl*)iface;
140 return InterlockedIncrement(&This->ref);
143 /***
144 * This implements the IUnknown method Release for this
145 * class
147 static ULONG WINAPI StgStreamImpl_Release(
148 IStream* iface)
150 StgStreamImpl* const This=(StgStreamImpl*)iface;
152 ULONG ref;
154 ref = InterlockedDecrement(&This->ref);
157 * If the reference count goes down to 0, perform suicide.
159 if (ref==0)
161 StgStreamImpl_Destroy(This);
164 return ref;
167 /***
168 * This method will open the block chain pointed by the property
169 * that describes the stream.
170 * If the stream's size is null, no chain is opened.
172 static void StgStreamImpl_OpenBlockChain(
173 StgStreamImpl* This)
175 StgProperty curProperty;
176 BOOL readSucessful;
179 * Make sure no old object is left over.
181 if (This->smallBlockChain != 0)
183 SmallBlockChainStream_Destroy(This->smallBlockChain);
184 This->smallBlockChain = 0;
187 if (This->bigBlockChain != 0)
189 BlockChainStream_Destroy(This->bigBlockChain);
190 This->bigBlockChain = 0;
194 * Read the information from the property.
196 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
197 This->ownerProperty,
198 &curProperty);
200 if (readSucessful)
202 This->streamSize = curProperty.size;
205 * This code supports only streams that are <32 bits in size.
207 assert(This->streamSize.u.HighPart == 0);
209 if(curProperty.startingBlock == BLOCK_END_OF_CHAIN)
211 assert( (This->streamSize.u.HighPart == 0) && (This->streamSize.u.LowPart == 0) );
213 else
215 if ( (This->streamSize.u.HighPart == 0) &&
216 (This->streamSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
218 This->smallBlockChain = SmallBlockChainStream_Construct(
219 This->parentStorage->ancestorStorage,
220 This->ownerProperty);
222 else
224 This->bigBlockChain = BlockChainStream_Construct(
225 This->parentStorage->ancestorStorage,
226 NULL,
227 This->ownerProperty);
233 /***
234 * This method is part of the ISequentialStream interface.
236 * It reads a block of information from the stream at the current
237 * position. It then moves the current position at the end of the
238 * read block
240 * See the documentation of ISequentialStream for more info.
242 static HRESULT WINAPI StgStreamImpl_Read(
243 IStream* iface,
244 void* pv, /* [length_is][size_is][out] */
245 ULONG cb, /* [in] */
246 ULONG* pcbRead) /* [out] */
248 StgStreamImpl* const This=(StgStreamImpl*)iface;
250 ULONG bytesReadBuffer;
251 ULONG bytesToReadFromBuffer;
252 HRESULT res;
254 TRACE("(%p, %p, %ld, %p)\n",
255 iface, pv, cb, pcbRead);
258 * If the caller is not interested in the number of bytes read,
259 * we use another buffer to avoid "if" statements in the code.
261 if (pcbRead==0)
262 pcbRead = &bytesReadBuffer;
265 * Using the known size of the stream, calculate the number of bytes
266 * to read from the block chain
268 bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
271 * Depending on the type of chain that was opened when the stream was constructed,
272 * we delegate the work to the method that reads the block chains.
274 if (This->smallBlockChain!=0)
276 res = SmallBlockChainStream_ReadAt(This->smallBlockChain,
277 This->currentPosition,
278 bytesToReadFromBuffer,
280 pcbRead);
283 else if (This->bigBlockChain!=0)
285 BOOL success = BlockChainStream_ReadAt(This->bigBlockChain,
286 This->currentPosition,
287 bytesToReadFromBuffer,
289 pcbRead);
290 if (success)
291 res = S_OK;
292 else
293 res = STG_E_READFAULT;
295 else
298 * Small and big block chains are both NULL. This case will happen
299 * when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
302 *pcbRead = 0;
303 res = S_OK;
304 goto end;
307 if (SUCCEEDED(res))
310 * We should always be able to read the proper amount of data from the
311 * chain.
313 assert(bytesToReadFromBuffer == *pcbRead);
316 * Advance the pointer for the number of positions read.
318 This->currentPosition.u.LowPart += *pcbRead;
321 end:
322 TRACE("<-- %08lx\n", res);
323 return res;
326 /***
327 * This method is part of the ISequentialStream interface.
329 * It writes a block of information to the stream at the current
330 * position. It then moves the current position at the end of the
331 * written block. If the stream is too small to fit the block,
332 * the stream is grown to fit.
334 * See the documentation of ISequentialStream for more info.
336 static HRESULT WINAPI StgStreamImpl_Write(
337 IStream* iface,
338 const void* pv, /* [size_is][in] */
339 ULONG cb, /* [in] */
340 ULONG* pcbWritten) /* [out] */
342 StgStreamImpl* const This=(StgStreamImpl*)iface;
344 ULARGE_INTEGER newSize;
345 ULONG bytesWritten = 0;
347 TRACE("(%p, %p, %ld, %p)\n",
348 iface, pv, cb, pcbWritten);
351 * Do we have permission to write to this stream?
353 switch(STGM_ACCESS_MODE(This->grfMode))
355 case STGM_WRITE:
356 case STGM_READWRITE:
357 break;
358 default:
359 return STG_E_ACCESSDENIED;
362 if (!pv)
363 return STG_E_INVALIDPOINTER;
366 * If the caller is not interested in the number of bytes written,
367 * we use another buffer to avoid "if" statements in the code.
369 if (pcbWritten == 0)
370 pcbWritten = &bytesWritten;
373 * Initialize the out parameter
375 *pcbWritten = 0;
377 if (cb == 0)
379 return S_OK;
381 else
383 newSize.u.HighPart = 0;
384 newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
388 * Verify if we need to grow the stream
390 if (newSize.u.LowPart > This->streamSize.u.LowPart)
392 /* grow stream */
393 IStream_SetSize(iface, newSize);
397 * Depending on the type of chain that was opened when the stream was constructed,
398 * we delegate the work to the method that readwrites to the block chains.
400 if (This->smallBlockChain!=0)
402 SmallBlockChainStream_WriteAt(This->smallBlockChain,
403 This->currentPosition,
406 pcbWritten);
409 else if (This->bigBlockChain!=0)
411 BlockChainStream_WriteAt(This->bigBlockChain,
412 This->currentPosition,
415 pcbWritten);
417 else
418 assert(FALSE);
421 * Advance the position pointer for the number of positions written.
423 This->currentPosition.u.LowPart += *pcbWritten;
425 return S_OK;
428 /***
429 * This method is part of the IStream interface.
431 * It will move the current stream pointer according to the parameters
432 * given.
434 * See the documentation of IStream for more info.
436 static HRESULT WINAPI StgStreamImpl_Seek(
437 IStream* iface,
438 LARGE_INTEGER dlibMove, /* [in] */
439 DWORD dwOrigin, /* [in] */
440 ULARGE_INTEGER* plibNewPosition) /* [out] */
442 StgStreamImpl* const This=(StgStreamImpl*)iface;
444 ULARGE_INTEGER newPosition;
446 TRACE("(%p, %ld, %ld, %p)\n",
447 iface, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
450 * The caller is allowed to pass in NULL as the new position return value.
451 * If it happens, we assign it to a dynamic variable to avoid special cases
452 * in the code below.
454 if (plibNewPosition == 0)
456 plibNewPosition = &newPosition;
460 * The file pointer is moved depending on the given "function"
461 * parameter.
463 switch (dwOrigin)
465 case STREAM_SEEK_SET:
466 plibNewPosition->u.HighPart = 0;
467 plibNewPosition->u.LowPart = 0;
468 break;
469 case STREAM_SEEK_CUR:
470 *plibNewPosition = This->currentPosition;
471 break;
472 case STREAM_SEEK_END:
473 *plibNewPosition = This->streamSize;
474 break;
475 default:
476 return STG_E_INVALIDFUNCTION;
479 plibNewPosition->QuadPart = RtlLargeIntegerAdd( plibNewPosition->QuadPart, dlibMove.QuadPart );
482 * tell the caller what we calculated
484 This->currentPosition = *plibNewPosition;
486 return S_OK;
489 /***
490 * This method is part of the IStream interface.
492 * It will change the size of a stream.
494 * TODO: Switch from small blocks to big blocks and vice versa.
496 * See the documentation of IStream for more info.
498 static HRESULT WINAPI StgStreamImpl_SetSize(
499 IStream* iface,
500 ULARGE_INTEGER libNewSize) /* [in] */
502 StgStreamImpl* const This=(StgStreamImpl*)iface;
504 StgProperty curProperty;
505 BOOL Success;
507 TRACE("(%p, %ld)\n", iface, libNewSize.u.LowPart);
510 * As documented.
512 if (libNewSize.u.HighPart != 0)
513 return STG_E_INVALIDFUNCTION;
516 * Do we have permission?
518 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
519 return STG_E_ACCESSDENIED;
521 if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
522 return S_OK;
525 * This will happen if we're creating a stream
527 if ((This->smallBlockChain == 0) && (This->bigBlockChain == 0))
529 if (libNewSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
531 This->smallBlockChain = SmallBlockChainStream_Construct(
532 This->parentStorage->ancestorStorage,
533 This->ownerProperty);
535 else
537 This->bigBlockChain = BlockChainStream_Construct(
538 This->parentStorage->ancestorStorage,
539 NULL,
540 This->ownerProperty);
545 * Read this stream's property to see if it's small blocks or big blocks
547 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
548 This->ownerProperty,
549 &curProperty);
551 * Determine if we have to switch from small to big blocks or vice versa
553 if ( (This->smallBlockChain!=0) &&
554 (curProperty.size.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
556 if (libNewSize.u.LowPart >= LIMIT_TO_USE_SMALL_BLOCK)
559 * Transform the small block chain into a big block chain
561 This->bigBlockChain = Storage32Impl_SmallBlocksToBigBlocks(
562 This->parentStorage->ancestorStorage,
563 &This->smallBlockChain);
567 if (This->smallBlockChain!=0)
569 Success = SmallBlockChainStream_SetSize(This->smallBlockChain, libNewSize);
571 else
573 Success = BlockChainStream_SetSize(This->bigBlockChain, libNewSize);
577 * Write the new information about this stream to the property
579 Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
580 This->ownerProperty,
581 &curProperty);
583 curProperty.size.u.HighPart = libNewSize.u.HighPart;
584 curProperty.size.u.LowPart = libNewSize.u.LowPart;
586 if (Success)
588 StorageImpl_WriteProperty(This->parentStorage->ancestorStorage,
589 This->ownerProperty,
590 &curProperty);
593 This->streamSize = libNewSize;
595 return S_OK;
598 /***
599 * This method is part of the IStream interface.
601 * It will copy the 'cb' Bytes to 'pstm' IStream.
603 * See the documentation of IStream for more info.
605 static HRESULT WINAPI StgStreamImpl_CopyTo(
606 IStream* iface,
607 IStream* pstm, /* [unique][in] */
608 ULARGE_INTEGER cb, /* [in] */
609 ULARGE_INTEGER* pcbRead, /* [out] */
610 ULARGE_INTEGER* pcbWritten) /* [out] */
612 HRESULT hr = S_OK;
613 BYTE tmpBuffer[128];
614 ULONG bytesRead, bytesWritten, copySize;
615 ULARGE_INTEGER totalBytesRead;
616 ULARGE_INTEGER totalBytesWritten;
618 TRACE("(%p, %p, %ld, %p, %p)\n",
619 iface, pstm, cb.u.LowPart, pcbRead, pcbWritten);
622 * Sanity check
624 if ( pstm == 0 )
625 return STG_E_INVALIDPOINTER;
627 totalBytesRead.u.LowPart = totalBytesRead.u.HighPart = 0;
628 totalBytesWritten.u.LowPart = totalBytesWritten.u.HighPart = 0;
631 * use stack to store data temporarily
632 * there is surely a more performant way of doing it, for now this basic
633 * implementation will do the job
635 while ( cb.u.LowPart > 0 )
637 if ( cb.u.LowPart >= 128 )
638 copySize = 128;
639 else
640 copySize = cb.u.LowPart;
642 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
644 totalBytesRead.u.LowPart += bytesRead;
646 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
648 totalBytesWritten.u.LowPart += bytesWritten;
651 * Check that read & write operations were successful
653 if (bytesRead != bytesWritten)
655 hr = STG_E_MEDIUMFULL;
656 break;
659 if (bytesRead!=copySize)
660 cb.u.LowPart = 0;
661 else
662 cb.u.LowPart -= bytesRead;
666 * Update number of bytes read and written
668 if (pcbRead)
670 pcbRead->u.LowPart = totalBytesRead.u.LowPart;
671 pcbRead->u.HighPart = totalBytesRead.u.HighPart;
674 if (pcbWritten)
676 pcbWritten->u.LowPart = totalBytesWritten.u.LowPart;
677 pcbWritten->u.HighPart = totalBytesWritten.u.HighPart;
679 return hr;
682 /***
683 * This method is part of the IStream interface.
685 * For streams contained in structured storages, this method
686 * does nothing. This is what the documentation tells us.
688 * See the documentation of IStream for more info.
690 static HRESULT WINAPI StgStreamImpl_Commit(
691 IStream* iface,
692 DWORD grfCommitFlags) /* [in] */
694 return S_OK;
697 /***
698 * This method is part of the IStream interface.
700 * For streams contained in structured storages, this method
701 * does nothing. This is what the documentation tells us.
703 * See the documentation of IStream for more info.
705 static HRESULT WINAPI StgStreamImpl_Revert(
706 IStream* iface)
708 return S_OK;
711 static HRESULT WINAPI StgStreamImpl_LockRegion(
712 IStream* iface,
713 ULARGE_INTEGER libOffset, /* [in] */
714 ULARGE_INTEGER cb, /* [in] */
715 DWORD dwLockType) /* [in] */
717 FIXME("not implemented!\n");
718 return E_NOTIMPL;
721 static HRESULT WINAPI StgStreamImpl_UnlockRegion(
722 IStream* iface,
723 ULARGE_INTEGER libOffset, /* [in] */
724 ULARGE_INTEGER cb, /* [in] */
725 DWORD dwLockType) /* [in] */
727 FIXME("not implemented!\n");
728 return E_NOTIMPL;
731 /***
732 * This method is part of the IStream interface.
734 * This method returns information about the current
735 * stream.
737 * See the documentation of IStream for more info.
739 static HRESULT WINAPI StgStreamImpl_Stat(
740 IStream* iface,
741 STATSTG* pstatstg, /* [out] */
742 DWORD grfStatFlag) /* [in] */
744 StgStreamImpl* const This=(StgStreamImpl*)iface;
746 StgProperty curProperty;
747 BOOL readSucessful;
750 * Read the information from the property.
752 readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
753 This->ownerProperty,
754 &curProperty);
756 if (readSucessful)
758 StorageUtl_CopyPropertyToSTATSTG(pstatstg,
759 &curProperty,
760 grfStatFlag);
762 pstatstg->grfMode = This->grfMode;
764 return S_OK;
767 return E_FAIL;
770 /***
771 * This method is part of the IStream interface.
773 * This method returns a clone of the interface that allows for
774 * another seek pointer
776 * See the documentation of IStream for more info.
778 * I am not totally sure what I am doing here but I presume that this
779 * should be basically as simple as creating a new stream with the same
780 * parent etc and positioning its seek cursor.
782 static HRESULT WINAPI StgStreamImpl_Clone(
783 IStream* iface,
784 IStream** ppstm) /* [out] */
786 StgStreamImpl* const This=(StgStreamImpl*)iface;
787 HRESULT hres;
788 StgStreamImpl* new_stream;
789 LARGE_INTEGER seek_pos;
792 * Sanity check
794 if ( ppstm == 0 )
795 return STG_E_INVALIDPOINTER;
797 new_stream = StgStreamImpl_Construct (This->parentStorage, This->grfMode, This->ownerProperty);
799 if (!new_stream)
800 return STG_E_INSUFFICIENTMEMORY; /* Currently the only reason for new_stream=0 */
802 *ppstm = (IStream*) new_stream;
803 seek_pos.QuadPart = This->currentPosition.QuadPart;
805 hres=StgStreamImpl_Seek (*ppstm, seek_pos, STREAM_SEEK_SET, NULL);
807 assert (SUCCEEDED(hres));
809 return S_OK;
813 * Virtual function table for the StgStreamImpl class.
815 static const IStreamVtbl StgStreamImpl_Vtbl =
817 StgStreamImpl_QueryInterface,
818 StgStreamImpl_AddRef,
819 StgStreamImpl_Release,
820 StgStreamImpl_Read,
821 StgStreamImpl_Write,
822 StgStreamImpl_Seek,
823 StgStreamImpl_SetSize,
824 StgStreamImpl_CopyTo,
825 StgStreamImpl_Commit,
826 StgStreamImpl_Revert,
827 StgStreamImpl_LockRegion,
828 StgStreamImpl_UnlockRegion,
829 StgStreamImpl_Stat,
830 StgStreamImpl_Clone
833 /******************************************************************************
834 ** StgStreamImpl implementation
837 /***
838 * This is the constructor for the StgStreamImpl class.
840 * Params:
841 * parentStorage - Pointer to the storage that contains the stream to open
842 * ownerProperty - Index of the property that points to this stream.
844 StgStreamImpl* StgStreamImpl_Construct(
845 StorageBaseImpl* parentStorage,
846 DWORD grfMode,
847 ULONG ownerProperty)
849 StgStreamImpl* newStream;
851 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
853 if (newStream!=0)
856 * Set-up the virtual function table and reference count.
858 newStream->lpVtbl = &StgStreamImpl_Vtbl;
859 newStream->ref = 0;
862 * We want to nail-down the reference to the storage in case the
863 * stream out-lives the storage in the client application.
865 newStream->parentStorage = parentStorage;
866 IStorage_AddRef((IStorage*)newStream->parentStorage);
868 newStream->grfMode = grfMode;
869 newStream->ownerProperty = ownerProperty;
872 * Start the stream at the beginning.
874 newStream->currentPosition.u.HighPart = 0;
875 newStream->currentPosition.u.LowPart = 0;
878 * Initialize the rest of the data.
880 newStream->streamSize.u.HighPart = 0;
881 newStream->streamSize.u.LowPart = 0;
882 newStream->bigBlockChain = 0;
883 newStream->smallBlockChain = 0;
886 * Read the size from the property and determine if the blocks forming
887 * this stream are large or small.
889 StgStreamImpl_OpenBlockChain(newStream);
892 return newStream;