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