widl: Allow pointers, type-qualifiers and other more complicated constructions in...
[wine] / dlls / ole32 / stg_stream.c
1 /*
2  * Compound Storage (32 bit version)
3  * Stream implementation
4  *
5  * This file contains the implementation of the stream interface
6  * for streams contained in a compound storage.
7  *
8  * Copyright 1999 Francis Beaudet
9  * Copyright 1999 Thuy Nguyen
10  *
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.
15  *
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.
20  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #define COBJMACROS
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
35
36 #include "windef.h"
37 #include "winbase.h"
38 #include "winerror.h"
39 #include "winternl.h"
40 #include "wine/debug.h"
41
42 #include "storage32.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(storage);
45
46
47 /***
48  * This is the destructor of the StgStreamImpl class.
49  *
50  * This method will clean-up all the resources used-up by the given StgStreamImpl
51  * class. The pointer passed-in to this function will be freed and will not
52  * be valid anymore.
53  */
54 static void StgStreamImpl_Destroy(StgStreamImpl* This)
55 {
56   TRACE("(%p)\n", This);
57
58   /*
59    * Release the reference we are holding on the parent storage.
60    * IStorage_Release((IStorage*)This->parentStorage);
61    *
62    * No, don't do this. Some apps call IStorage_Release without
63    * calling IStream_Release first. If we grab a reference the
64    * file is not closed, and the app fails when it tries to
65    * reopen the file (Easy-PC, for example). Just inform the
66    * storage that we have closed the stream
67    */
68
69   if(This->parentStorage) {
70
71     StorageBaseImpl_RemoveStream(This->parentStorage, This);
72
73   }
74
75   This->parentStorage = 0;
76
77   /*
78    * Make sure we clean-up the block chain stream objects that we were using.
79    */
80   if (This->bigBlockChain != 0)
81   {
82     BlockChainStream_Destroy(This->bigBlockChain);
83     This->bigBlockChain = 0;
84   }
85
86   if (This->smallBlockChain != 0)
87   {
88     SmallBlockChainStream_Destroy(This->smallBlockChain);
89     This->smallBlockChain = 0;
90   }
91
92   /*
93    * Finally, free the memory used-up by the class.
94    */
95   HeapFree(GetProcessHeap(), 0, This);
96 }
97
98 /***
99  * This implements the IUnknown method QueryInterface for this
100  * class
101  */
102 static HRESULT WINAPI StgStreamImpl_QueryInterface(
103                   IStream*     iface,
104                   REFIID         riid,        /* [in] */
105                   void**         ppvObject)   /* [iid_is][out] */
106 {
107   StgStreamImpl* const This=(StgStreamImpl*)iface;
108
109   /*
110    * Perform a sanity check on the parameters.
111    */
112   if (ppvObject==0)
113     return E_INVALIDARG;
114
115   /*
116    * Initialize the return parameter.
117    */
118   *ppvObject = 0;
119
120   /*
121    * Compare the riid with the interface IDs implemented by this object.
122    */
123   if (IsEqualIID(&IID_IUnknown, riid) ||
124       IsEqualIID(&IID_IPersist, riid) ||
125       IsEqualIID(&IID_IPersistStream, riid) ||
126       IsEqualIID(&IID_ISequentialStream, riid) ||
127       IsEqualIID(&IID_IStream, riid))
128   {
129     *ppvObject = This;
130   }
131
132   /*
133    * Check that we obtained an interface.
134    */
135   if ((*ppvObject)==0)
136     return E_NOINTERFACE;
137
138   /*
139    * Query Interface always increases the reference count by one when it is
140    * successful
141    */
142   IStream_AddRef(iface);
143
144   return S_OK;
145 }
146
147 /***
148  * This implements the IUnknown method AddRef for this
149  * class
150  */
151 static ULONG WINAPI StgStreamImpl_AddRef(
152                 IStream* iface)
153 {
154   StgStreamImpl* const This=(StgStreamImpl*)iface;
155   return InterlockedIncrement(&This->ref);
156 }
157
158 /***
159  * This implements the IUnknown method Release for this
160  * class
161  */
162 static ULONG WINAPI StgStreamImpl_Release(
163                 IStream* iface)
164 {
165   StgStreamImpl* const This=(StgStreamImpl*)iface;
166
167   ULONG ref;
168
169   ref = InterlockedDecrement(&This->ref);
170
171   /*
172    * If the reference count goes down to 0, perform suicide.
173    */
174   if (ref==0)
175   {
176     StgStreamImpl_Destroy(This);
177   }
178
179   return ref;
180 }
181
182 /***
183  * This method will open the block chain pointed by the property
184  * that describes the stream.
185  * If the stream's size is null, no chain is opened.
186  */
187 static void StgStreamImpl_OpenBlockChain(
188         StgStreamImpl* This)
189 {
190   StgProperty    curProperty;
191   BOOL         readSuccessful;
192
193   /*
194    * Make sure no old object is left over.
195    */
196   if (This->smallBlockChain != 0)
197   {
198     SmallBlockChainStream_Destroy(This->smallBlockChain);
199     This->smallBlockChain = 0;
200   }
201
202   if (This->bigBlockChain != 0)
203   {
204     BlockChainStream_Destroy(This->bigBlockChain);
205     This->bigBlockChain = 0;
206   }
207
208   /*
209    * Read the information from the property.
210    */
211   readSuccessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
212                                              This->ownerProperty,
213                                              &curProperty);
214
215   if (readSuccessful)
216   {
217     This->streamSize = curProperty.size;
218
219     /*
220      * This code supports only streams that are <32 bits in size.
221      */
222     assert(This->streamSize.u.HighPart == 0);
223
224     if(curProperty.startingBlock == BLOCK_END_OF_CHAIN)
225     {
226       assert( (This->streamSize.u.HighPart == 0) && (This->streamSize.u.LowPart == 0) );
227     }
228     else
229     {
230       if ( (This->streamSize.u.HighPart == 0) &&
231            (This->streamSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
232       {
233         This->smallBlockChain = SmallBlockChainStream_Construct(
234                                                                 This->parentStorage->ancestorStorage,
235                                                                 NULL,
236                                                                 This->ownerProperty);
237       }
238       else
239       {
240         This->bigBlockChain = BlockChainStream_Construct(
241                                                          This->parentStorage->ancestorStorage,
242                                                          NULL,
243                                                          This->ownerProperty);
244       }
245     }
246   }
247 }
248
249 /***
250  * This method is part of the ISequentialStream interface.
251  *
252  * It reads a block of information from the stream at the current
253  * position. It then moves the current position at the end of the
254  * read block
255  *
256  * See the documentation of ISequentialStream for more info.
257  */
258 static HRESULT WINAPI StgStreamImpl_Read(
259                   IStream*     iface,
260                   void*          pv,        /* [length_is][size_is][out] */
261                   ULONG          cb,        /* [in] */
262                   ULONG*         pcbRead)   /* [out] */
263 {
264   StgStreamImpl* const This=(StgStreamImpl*)iface;
265
266   ULONG bytesReadBuffer;
267   ULONG bytesToReadFromBuffer;
268   HRESULT res;
269
270   TRACE("(%p, %p, %d, %p)\n",
271         iface, pv, cb, pcbRead);
272
273   if (!This->parentStorage)
274   {
275     WARN("storage reverted\n");
276     return STG_E_REVERTED;
277   }
278
279   /*
280    * If the caller is not interested in the number of bytes read,
281    * we use another buffer to avoid "if" statements in the code.
282    */
283   if (pcbRead==0)
284     pcbRead = &bytesReadBuffer;
285
286   /*
287    * Using the known size of the stream, calculate the number of bytes
288    * to read from the block chain
289    */
290   bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
291
292   /*
293    * Depending on the type of chain that was opened when the stream was constructed,
294    * we delegate the work to the method that reads the block chains.
295    */
296   if (This->smallBlockChain!=0)
297   {
298     res = SmallBlockChainStream_ReadAt(This->smallBlockChain,
299                                  This->currentPosition,
300                                  bytesToReadFromBuffer,
301                                  pv,
302                                  pcbRead);
303
304   }
305   else if (This->bigBlockChain!=0)
306   {
307     res = BlockChainStream_ReadAt(This->bigBlockChain,
308                  This->currentPosition,
309                  bytesToReadFromBuffer,
310                  pv,
311                  pcbRead);
312   }
313   else
314   {
315     /*
316      * Small and big block chains are both NULL. This case will happen
317      * when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
318      */
319
320     *pcbRead = 0;
321     res = S_OK;
322     goto end;
323   }
324
325   if (SUCCEEDED(res))
326   {
327       /*
328        * We should always be able to read the proper amount of data from the
329        * chain.
330        */
331       assert(bytesToReadFromBuffer == *pcbRead);
332
333       /*
334        * Advance the pointer for the number of positions read.
335        */
336       This->currentPosition.u.LowPart += *pcbRead;
337   }
338
339 end:
340   TRACE("<-- %08x\n", res);
341   return res;
342 }
343
344 /***
345  * This method is part of the ISequentialStream interface.
346  *
347  * It writes a block of information to the stream at the current
348  * position. It then moves the current position at the end of the
349  * written block. If the stream is too small to fit the block,
350  * the stream is grown to fit.
351  *
352  * See the documentation of ISequentialStream for more info.
353  */
354 static HRESULT WINAPI StgStreamImpl_Write(
355                   IStream*     iface,
356                   const void*    pv,          /* [size_is][in] */
357                   ULONG          cb,          /* [in] */
358                   ULONG*         pcbWritten)  /* [out] */
359 {
360   StgStreamImpl* const This=(StgStreamImpl*)iface;
361
362   ULARGE_INTEGER newSize;
363   ULONG bytesWritten = 0;
364   HRESULT res;
365
366   TRACE("(%p, %p, %d, %p)\n",
367         iface, pv, cb, pcbWritten);
368
369   /*
370    * Do we have permission to write to this stream?
371    */
372   switch(STGM_ACCESS_MODE(This->grfMode))
373   {
374   case STGM_WRITE:
375   case STGM_READWRITE:
376       break;
377   default:
378       WARN("access denied by flags: 0x%x\n", STGM_ACCESS_MODE(This->grfMode));
379       return STG_E_ACCESSDENIED;
380   }
381
382   if (!pv)
383     return STG_E_INVALIDPOINTER;
384
385   if (!This->parentStorage)
386   {
387     WARN("storage reverted\n");
388     return STG_E_REVERTED;
389   }
390  
391   /*
392    * If the caller is not interested in the number of bytes written,
393    * we use another buffer to avoid "if" statements in the code.
394    */
395   if (pcbWritten == 0)
396     pcbWritten = &bytesWritten;
397
398   /*
399    * Initialize the out parameter
400    */
401   *pcbWritten = 0;
402
403   if (cb == 0)
404   {
405     TRACE("<-- S_OK, written 0\n");
406     return S_OK;
407   }
408   else
409   {
410     newSize.u.HighPart = 0;
411     newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
412   }
413
414   /*
415    * Verify if we need to grow the stream
416    */
417   if (newSize.u.LowPart > This->streamSize.u.LowPart)
418   {
419     /* grow stream */
420     res = IStream_SetSize(iface, newSize);
421     if (FAILED(res))
422       return res;
423   }
424
425   /*
426    * Depending on the type of chain that was opened when the stream was constructed,
427    * we delegate the work to the method that readwrites to the block chains.
428    */
429   if (This->smallBlockChain!=0)
430   {
431     res = SmallBlockChainStream_WriteAt(This->smallBlockChain,
432                                   This->currentPosition,
433                                   cb,
434                                   pv,
435                                   pcbWritten);
436
437   }
438   else if (This->bigBlockChain!=0)
439   {
440     res = BlockChainStream_WriteAt(This->bigBlockChain,
441                              This->currentPosition,
442                              cb,
443                              pv,
444                              pcbWritten);
445   }
446   else
447   {
448     /* this should never happen because the IStream_SetSize call above will
449      * make sure a big or small block chain is created */
450     assert(FALSE);
451     res = 0;
452   }
453
454   /*
455    * Advance the position pointer for the number of positions written.
456    */
457   This->currentPosition.u.LowPart += *pcbWritten;
458
459   TRACE("<-- S_OK, written %u\n", *pcbWritten);
460   return res;
461 }
462
463 /***
464  * This method is part of the IStream interface.
465  *
466  * It will move the current stream pointer according to the parameters
467  * given.
468  *
469  * See the documentation of IStream for more info.
470  */
471 static HRESULT WINAPI StgStreamImpl_Seek(
472                   IStream*      iface,
473                   LARGE_INTEGER   dlibMove,         /* [in] */
474                   DWORD           dwOrigin,         /* [in] */
475                   ULARGE_INTEGER* plibNewPosition) /* [out] */
476 {
477   StgStreamImpl* const This=(StgStreamImpl*)iface;
478
479   ULARGE_INTEGER newPosition;
480
481   TRACE("(%p, %d, %d, %p)\n",
482         iface, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
483
484   /*
485    * fail if the stream has no parent (as does windows)
486    */
487
488   if (!This->parentStorage)
489   {
490     WARN("storage reverted\n");
491     return STG_E_REVERTED;
492   }
493
494   /*
495    * The caller is allowed to pass in NULL as the new position return value.
496    * If it happens, we assign it to a dynamic variable to avoid special cases
497    * in the code below.
498    */
499   if (plibNewPosition == 0)
500   {
501     plibNewPosition = &newPosition;
502   }
503
504   /*
505    * The file pointer is moved depending on the given "function"
506    * parameter.
507    */
508   switch (dwOrigin)
509   {
510     case STREAM_SEEK_SET:
511       plibNewPosition->u.HighPart = 0;
512       plibNewPosition->u.LowPart  = 0;
513       break;
514     case STREAM_SEEK_CUR:
515       *plibNewPosition = This->currentPosition;
516       break;
517     case STREAM_SEEK_END:
518       *plibNewPosition = This->streamSize;
519       break;
520     default:
521       WARN("invalid dwOrigin %d\n", dwOrigin);
522       return STG_E_INVALIDFUNCTION;
523   }
524
525   plibNewPosition->QuadPart += dlibMove.QuadPart;
526
527   /*
528    * tell the caller what we calculated
529    */
530   This->currentPosition = *plibNewPosition;
531
532   return S_OK;
533 }
534
535 /***
536  * This method is part of the IStream interface.
537  *
538  * It will change the size of a stream.
539  *
540  * See the documentation of IStream for more info.
541  */
542 static HRESULT WINAPI StgStreamImpl_SetSize(
543                                      IStream*      iface,
544                                      ULARGE_INTEGER  libNewSize)   /* [in] */
545 {
546   StgStreamImpl* const This=(StgStreamImpl*)iface;
547
548   StgProperty    curProperty;
549   BOOL         Success;
550
551   TRACE("(%p, %d)\n", iface, libNewSize.u.LowPart);
552
553   if(!This->parentStorage)
554   {
555     WARN("storage reverted\n");
556     return STG_E_REVERTED;
557   }
558
559   /*
560    * As documented.
561    */
562   if (libNewSize.u.HighPart != 0)
563   {
564     WARN("invalid value for libNewSize.u.HighPart %d\n", libNewSize.u.HighPart);
565     return STG_E_INVALIDFUNCTION;
566   }
567
568   /*
569    * Do we have permission?
570    */
571   if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
572   {
573     WARN("access denied\n");
574     return STG_E_ACCESSDENIED;
575   }
576
577   /* In simple mode keep the stream size above the small block limit */
578   if (This->parentStorage->ancestorStorage->base.openFlags & STGM_SIMPLE)
579     libNewSize.u.LowPart = max(libNewSize.u.LowPart, LIMIT_TO_USE_SMALL_BLOCK);
580
581   if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
582     return S_OK;
583
584   /*
585    * This will happen if we're creating a stream
586    */
587   if ((This->smallBlockChain == 0) && (This->bigBlockChain == 0))
588   {
589     if (libNewSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
590     {
591       This->smallBlockChain = SmallBlockChainStream_Construct(
592                                     This->parentStorage->ancestorStorage,
593                                     NULL,
594                                     This->ownerProperty);
595     }
596     else
597     {
598       This->bigBlockChain = BlockChainStream_Construct(
599                                 This->parentStorage->ancestorStorage,
600                                 NULL,
601                                 This->ownerProperty);
602     }
603   }
604
605   /*
606    * Read this stream's property to see if it's small blocks or big blocks
607    */
608   Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
609                                        This->ownerProperty,
610                                        &curProperty);
611   /*
612    * Determine if we have to switch from small to big blocks or vice versa
613    */
614   if ( (This->smallBlockChain!=0) &&
615        (curProperty.size.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
616   {
617     if (libNewSize.u.LowPart >= LIMIT_TO_USE_SMALL_BLOCK)
618     {
619       /*
620        * Transform the small block chain into a big block chain
621        */
622       This->bigBlockChain = Storage32Impl_SmallBlocksToBigBlocks(
623                                 This->parentStorage->ancestorStorage,
624                                 &This->smallBlockChain);
625     }
626   }
627   else if ( (This->bigBlockChain!=0) &&
628             (curProperty.size.u.LowPart >= LIMIT_TO_USE_SMALL_BLOCK) )
629   {
630     if (libNewSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
631     {
632       /*
633        * Transform the big block chain into a small block chain
634        */
635       This->smallBlockChain = Storage32Impl_BigBlocksToSmallBlocks(
636                                 This->parentStorage->ancestorStorage,
637                                 &This->bigBlockChain);
638     }
639   }
640
641   if (This->smallBlockChain!=0)
642   {
643     Success = SmallBlockChainStream_SetSize(This->smallBlockChain, libNewSize);
644   }
645   else
646   {
647     Success = BlockChainStream_SetSize(This->bigBlockChain, libNewSize);
648   }
649
650   /*
651    * Write the new information about this stream to the property
652    */
653   Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
654                                        This->ownerProperty,
655                                        &curProperty);
656
657   curProperty.size.u.HighPart = libNewSize.u.HighPart;
658   curProperty.size.u.LowPart = libNewSize.u.LowPart;
659
660   if (Success)
661   {
662     StorageImpl_WriteProperty(This->parentStorage->ancestorStorage,
663                                 This->ownerProperty,
664                                 &curProperty);
665   }
666
667   This->streamSize = libNewSize;
668
669   return S_OK;
670 }
671
672 /***
673  * This method is part of the IStream interface.
674  *
675  * It will copy the 'cb' Bytes to 'pstm' IStream.
676  *
677  * See the documentation of IStream for more info.
678  */
679 static HRESULT WINAPI StgStreamImpl_CopyTo(
680                                     IStream*      iface,
681                                     IStream*      pstm,         /* [unique][in] */
682                                     ULARGE_INTEGER  cb,           /* [in] */
683                                     ULARGE_INTEGER* pcbRead,      /* [out] */
684                                     ULARGE_INTEGER* pcbWritten)   /* [out] */
685 {
686   StgStreamImpl* const This=(StgStreamImpl*)iface;
687   HRESULT        hr = S_OK;
688   BYTE           tmpBuffer[128];
689   ULONG          bytesRead, bytesWritten, copySize;
690   ULARGE_INTEGER totalBytesRead;
691   ULARGE_INTEGER totalBytesWritten;
692
693   TRACE("(%p, %p, %d, %p, %p)\n",
694         iface, pstm, cb.u.LowPart, pcbRead, pcbWritten);
695
696   /*
697    * Sanity check
698    */
699
700   if (!This->parentStorage)
701   {
702     WARN("storage reverted\n");
703     return STG_E_REVERTED;
704   }
705
706   if ( pstm == 0 )
707     return STG_E_INVALIDPOINTER;
708
709   totalBytesRead.QuadPart = 0;
710   totalBytesWritten.QuadPart = 0;
711
712   while ( cb.QuadPart > 0 )
713   {
714     if ( cb.QuadPart >= sizeof(tmpBuffer) )
715       copySize = sizeof(tmpBuffer);
716     else
717       copySize = cb.u.LowPart;
718
719     IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
720
721     totalBytesRead.QuadPart += bytesRead;
722
723     IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
724
725     totalBytesWritten.QuadPart += bytesWritten;
726
727     /*
728      * Check that read & write operations were successful
729      */
730     if (bytesRead != bytesWritten)
731     {
732       hr = STG_E_MEDIUMFULL;
733       WARN("medium full\n");
734       break;
735     }
736
737     if (bytesRead!=copySize)
738       cb.QuadPart = 0;
739     else
740       cb.QuadPart -= bytesRead;
741   }
742
743   if (pcbRead) pcbRead->QuadPart = totalBytesRead.QuadPart;
744   if (pcbWritten) pcbWritten->QuadPart = totalBytesWritten.QuadPart;
745
746   return hr;
747 }
748
749 /***
750  * This method is part of the IStream interface.
751  *
752  * For streams contained in structured storages, this method
753  * does nothing. This is what the documentation tells us.
754  *
755  * See the documentation of IStream for more info.
756  */
757 static HRESULT WINAPI StgStreamImpl_Commit(
758                   IStream*      iface,
759                   DWORD           grfCommitFlags)  /* [in] */
760 {
761   StgStreamImpl* const This=(StgStreamImpl*)iface;
762
763   if (!This->parentStorage)
764   {
765     WARN("storage reverted\n");
766     return STG_E_REVERTED;
767   }
768
769   return S_OK;
770 }
771
772 /***
773  * This method is part of the IStream interface.
774  *
775  * For streams contained in structured storages, this method
776  * does nothing. This is what the documentation tells us.
777  *
778  * See the documentation of IStream for more info.
779  */
780 static HRESULT WINAPI StgStreamImpl_Revert(
781                   IStream* iface)
782 {
783   return S_OK;
784 }
785
786 static HRESULT WINAPI StgStreamImpl_LockRegion(
787                                         IStream*     iface,
788                                         ULARGE_INTEGER libOffset,   /* [in] */
789                                         ULARGE_INTEGER cb,          /* [in] */
790                                         DWORD          dwLockType)  /* [in] */
791 {
792   StgStreamImpl* const This=(StgStreamImpl*)iface;
793
794   if (!This->parentStorage)
795   {
796     WARN("storage reverted\n");
797     return STG_E_REVERTED;
798   }
799
800   FIXME("not implemented!\n");
801   return E_NOTIMPL;
802 }
803
804 static HRESULT WINAPI StgStreamImpl_UnlockRegion(
805                                           IStream*     iface,
806                                           ULARGE_INTEGER libOffset,   /* [in] */
807                                           ULARGE_INTEGER cb,          /* [in] */
808                                           DWORD          dwLockType)  /* [in] */
809 {
810   StgStreamImpl* const This=(StgStreamImpl*)iface;
811
812   if (!This->parentStorage)
813   {
814     WARN("storage reverted\n");
815     return STG_E_REVERTED;
816   }
817
818   FIXME("not implemented!\n");
819   return E_NOTIMPL;
820 }
821
822 /***
823  * This method is part of the IStream interface.
824  *
825  * This method returns information about the current
826  * stream.
827  *
828  * See the documentation of IStream for more info.
829  */
830 static HRESULT WINAPI StgStreamImpl_Stat(
831                   IStream*     iface,
832                   STATSTG*       pstatstg,     /* [out] */
833                   DWORD          grfStatFlag)  /* [in] */
834 {
835   StgStreamImpl* const This=(StgStreamImpl*)iface;
836
837   StgProperty    curProperty;
838   BOOL         readSuccessful;
839
840   TRACE("%p %p %d\n", This, pstatstg, grfStatFlag);
841
842   /*
843    * if stream has no parent, return STG_E_REVERTED
844    */
845
846   if (!This->parentStorage)
847   {
848     WARN("storage reverted\n");
849     return STG_E_REVERTED;
850   }
851
852   /*
853    * Read the information from the property.
854    */
855   readSuccessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
856                                              This->ownerProperty,
857                                              &curProperty);
858
859   if (readSuccessful)
860   {
861     StorageImpl *root = This->parentStorage->ancestorStorage;
862
863     StorageUtl_CopyPropertyToSTATSTG(pstatstg,
864                                      &curProperty,
865                                      grfStatFlag);
866
867     pstatstg->grfMode = This->grfMode;
868
869     /* In simple create mode cbSize is the current pos */
870     if((root->base.openFlags & STGM_SIMPLE) && root->create)
871       pstatstg->cbSize = This->currentPosition;
872
873     return S_OK;
874   }
875
876   WARN("failed to read properties\n");
877   return E_FAIL;
878 }
879
880 /***
881  * This method is part of the IStream interface.
882  *
883  * This method returns a clone of the interface that allows for
884  * another seek pointer
885  *
886  * See the documentation of IStream for more info.
887  *
888  * I am not totally sure what I am doing here but I presume that this
889  * should be basically as simple as creating a new stream with the same
890  * parent etc and positioning its seek cursor.
891  */
892 static HRESULT WINAPI StgStreamImpl_Clone(
893                                    IStream*     iface,
894                                    IStream**    ppstm) /* [out] */
895 {
896   StgStreamImpl* const This=(StgStreamImpl*)iface;
897   HRESULT hres;
898   StgStreamImpl* new_stream;
899   LARGE_INTEGER seek_pos;
900
901   TRACE("%p %p\n", This, ppstm);
902
903   /*
904    * Sanity check
905    */
906
907   if (!This->parentStorage)
908     return STG_E_REVERTED;
909
910   if ( ppstm == 0 )
911     return STG_E_INVALIDPOINTER;
912
913   new_stream = StgStreamImpl_Construct (This->parentStorage, This->grfMode, This->ownerProperty);
914
915   if (!new_stream)
916     return STG_E_INSUFFICIENTMEMORY; /* Currently the only reason for new_stream=0 */
917
918   *ppstm = (IStream*) new_stream;
919   IStream_AddRef(*ppstm);
920
921   seek_pos.QuadPart = This->currentPosition.QuadPart;
922
923   hres=StgStreamImpl_Seek (*ppstm, seek_pos, STREAM_SEEK_SET, NULL);
924
925   assert (SUCCEEDED(hres));
926
927   return S_OK;
928 }
929
930 /*
931  * Virtual function table for the StgStreamImpl class.
932  */
933 static const IStreamVtbl StgStreamImpl_Vtbl =
934 {
935     StgStreamImpl_QueryInterface,
936     StgStreamImpl_AddRef,
937     StgStreamImpl_Release,
938     StgStreamImpl_Read,
939     StgStreamImpl_Write,
940     StgStreamImpl_Seek,
941     StgStreamImpl_SetSize,
942     StgStreamImpl_CopyTo,
943     StgStreamImpl_Commit,
944     StgStreamImpl_Revert,
945     StgStreamImpl_LockRegion,
946     StgStreamImpl_UnlockRegion,
947     StgStreamImpl_Stat,
948     StgStreamImpl_Clone
949 };
950
951 /******************************************************************************
952 ** StgStreamImpl implementation
953 */
954
955 /***
956  * This is the constructor for the StgStreamImpl class.
957  *
958  * Params:
959  *    parentStorage - Pointer to the storage that contains the stream to open
960  *    ownerProperty - Index of the property that points to this stream.
961  */
962 StgStreamImpl* StgStreamImpl_Construct(
963                 StorageBaseImpl* parentStorage,
964     DWORD            grfMode,
965     ULONG            ownerProperty)
966 {
967   StgStreamImpl* newStream;
968
969   newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
970
971   if (newStream!=0)
972   {
973     /*
974      * Set-up the virtual function table and reference count.
975      */
976     newStream->lpVtbl    = &StgStreamImpl_Vtbl;
977     newStream->ref       = 0;
978
979     newStream->parentStorage = parentStorage;
980
981     /*
982      * We want to nail-down the reference to the storage in case the
983      * stream out-lives the storage in the client application.
984      *
985      * -- IStorage_AddRef((IStorage*)newStream->parentStorage);
986      *
987      * No, don't do this. Some apps call IStorage_Release without
988      * calling IStream_Release first. If we grab a reference the
989      * file is not closed, and the app fails when it tries to
990      * reopen the file (Easy-PC, for example)
991      */
992
993     newStream->grfMode = grfMode;
994     newStream->ownerProperty = ownerProperty;
995
996     /*
997      * Start the stream at the beginning.
998      */
999     newStream->currentPosition.u.HighPart = 0;
1000     newStream->currentPosition.u.LowPart = 0;
1001
1002     /*
1003      * Initialize the rest of the data.
1004      */
1005     newStream->streamSize.u.HighPart = 0;
1006     newStream->streamSize.u.LowPart  = 0;
1007     newStream->bigBlockChain       = 0;
1008     newStream->smallBlockChain     = 0;
1009
1010     /*
1011      * Read the size from the property and determine if the blocks forming
1012      * this stream are large or small.
1013      */
1014     StgStreamImpl_OpenBlockChain(newStream);
1015
1016     /* add us to the storage's list of active streams */
1017     StorageBaseImpl_AddStream(parentStorage, newStream);
1018   }
1019
1020   return newStream;
1021 }