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