d3dx9: Implement volume texture filtering in D3DXFilterTexture.
[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(&This->parentStorage->IStorage_iface);
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   HeapFree(GetProcessHeap(), 0, This);
78 }
79
80 /***
81  * This implements the IUnknown method QueryInterface for this
82  * class
83  */
84 static HRESULT WINAPI StgStreamImpl_QueryInterface(
85                   IStream*     iface,
86                   REFIID         riid,        /* [in] */
87                   void**         ppvObject)   /* [iid_is][out] */
88 {
89   StgStreamImpl* This = impl_from_IStream(iface);
90
91   if (ppvObject==0)
92     return E_INVALIDARG;
93
94   *ppvObject = 0;
95
96   if (IsEqualIID(&IID_IUnknown, riid) ||
97       IsEqualIID(&IID_IPersist, riid) ||
98       IsEqualIID(&IID_IPersistStream, riid) ||
99       IsEqualIID(&IID_ISequentialStream, riid) ||
100       IsEqualIID(&IID_IStream, riid))
101   {
102     *ppvObject = &This->IStream_iface;
103   }
104   else
105     return E_NOINTERFACE;
106
107   IStream_AddRef(iface);
108
109   return S_OK;
110 }
111
112 /***
113  * This implements the IUnknown method AddRef for this
114  * class
115  */
116 static ULONG WINAPI StgStreamImpl_AddRef(
117                 IStream* iface)
118 {
119   StgStreamImpl* This = impl_from_IStream(iface);
120   return InterlockedIncrement(&This->ref);
121 }
122
123 /***
124  * This implements the IUnknown method Release for this
125  * class
126  */
127 static ULONG WINAPI StgStreamImpl_Release(
128                 IStream* iface)
129 {
130   StgStreamImpl* This = impl_from_IStream(iface);
131
132   ULONG ref;
133
134   ref = InterlockedDecrement(&This->ref);
135
136   /*
137    * If the reference count goes down to 0, perform suicide.
138    */
139   if (ref==0)
140   {
141     StgStreamImpl_Destroy(This);
142   }
143
144   return ref;
145 }
146
147 /***
148  * This method is part of the ISequentialStream interface.
149  *
150  * It reads a block of information from the stream at the current
151  * position. It then moves the current position at the end of the
152  * read block
153  *
154  * See the documentation of ISequentialStream for more info.
155  */
156 static HRESULT WINAPI StgStreamImpl_Read(
157                   IStream*     iface,
158                   void*          pv,        /* [length_is][size_is][out] */
159                   ULONG          cb,        /* [in] */
160                   ULONG*         pcbRead)   /* [out] */
161 {
162   StgStreamImpl* This = impl_from_IStream(iface);
163
164   ULONG bytesReadBuffer;
165   HRESULT res;
166
167   TRACE("(%p, %p, %d, %p)\n",
168         iface, pv, cb, pcbRead);
169
170   if (!This->parentStorage)
171   {
172     WARN("storage reverted\n");
173     return STG_E_REVERTED;
174   }
175
176   /*
177    * If the caller is not interested in the number of bytes read,
178    * we use another buffer to avoid "if" statements in the code.
179    */
180   if (pcbRead==0)
181     pcbRead = &bytesReadBuffer;
182
183   res = StorageBaseImpl_StreamReadAt(This->parentStorage,
184                                      This->dirEntry,
185                                      This->currentPosition,
186                                      cb,
187                                      pv,
188                                      pcbRead);
189
190   if (SUCCEEDED(res))
191   {
192     /*
193      * Advance the pointer for the number of positions read.
194      */
195     This->currentPosition.u.LowPart += *pcbRead;
196   }
197
198   TRACE("<-- %08x\n", res);
199   return res;
200 }
201
202 /***
203  * This method is part of the ISequentialStream interface.
204  *
205  * It writes a block of information to the stream at the current
206  * position. It then moves the current position at the end of the
207  * written block. If the stream is too small to fit the block,
208  * the stream is grown to fit.
209  *
210  * See the documentation of ISequentialStream for more info.
211  */
212 static HRESULT WINAPI StgStreamImpl_Write(
213                   IStream*     iface,
214                   const void*    pv,          /* [size_is][in] */
215                   ULONG          cb,          /* [in] */
216                   ULONG*         pcbWritten)  /* [out] */
217 {
218   StgStreamImpl* This = impl_from_IStream(iface);
219
220   ULONG bytesWritten = 0;
221   HRESULT res;
222
223   TRACE("(%p, %p, %d, %p)\n",
224         iface, pv, cb, pcbWritten);
225
226   /*
227    * Do we have permission to write to this stream?
228    */
229   switch(STGM_ACCESS_MODE(This->grfMode))
230   {
231   case STGM_WRITE:
232   case STGM_READWRITE:
233       break;
234   default:
235       WARN("access denied by flags: 0x%x\n", STGM_ACCESS_MODE(This->grfMode));
236       return STG_E_ACCESSDENIED;
237   }
238
239   if (!pv)
240     return STG_E_INVALIDPOINTER;
241
242   if (!This->parentStorage)
243   {
244     WARN("storage reverted\n");
245     return STG_E_REVERTED;
246   }
247  
248   /*
249    * If the caller is not interested in the number of bytes written,
250    * we use another buffer to avoid "if" statements in the code.
251    */
252   if (pcbWritten == 0)
253     pcbWritten = &bytesWritten;
254
255   /*
256    * Initialize the out parameter
257    */
258   *pcbWritten = 0;
259
260   if (cb == 0)
261   {
262     TRACE("<-- S_OK, written 0\n");
263     return S_OK;
264   }
265
266   res = StorageBaseImpl_StreamWriteAt(This->parentStorage,
267                                       This->dirEntry,
268                                       This->currentPosition,
269                                       cb,
270                                       pv,
271                                       pcbWritten);
272
273   /*
274    * Advance the position pointer for the number of positions written.
275    */
276   This->currentPosition.u.LowPart += *pcbWritten;
277
278   if (SUCCEEDED(res))
279     res = StorageBaseImpl_Flush(This->parentStorage);
280
281   TRACE("<-- S_OK, written %u\n", *pcbWritten);
282   return res;
283 }
284
285 /***
286  * This method is part of the IStream interface.
287  *
288  * It will move the current stream pointer according to the parameters
289  * given.
290  *
291  * See the documentation of IStream for more info.
292  */
293 static HRESULT WINAPI StgStreamImpl_Seek(
294                   IStream*      iface,
295                   LARGE_INTEGER   dlibMove,         /* [in] */
296                   DWORD           dwOrigin,         /* [in] */
297                   ULARGE_INTEGER* plibNewPosition) /* [out] */
298 {
299   StgStreamImpl* This = impl_from_IStream(iface);
300
301   ULARGE_INTEGER newPosition;
302   DirEntry currentEntry;
303   HRESULT hr;
304
305   TRACE("(%p, %d, %d, %p)\n",
306         iface, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
307
308   /*
309    * fail if the stream has no parent (as does windows)
310    */
311
312   if (!This->parentStorage)
313   {
314     WARN("storage reverted\n");
315     return STG_E_REVERTED;
316   }
317
318   /*
319    * The caller is allowed to pass in NULL as the new position return value.
320    * If it happens, we assign it to a dynamic variable to avoid special cases
321    * in the code below.
322    */
323   if (plibNewPosition == 0)
324   {
325     plibNewPosition = &newPosition;
326   }
327
328   /*
329    * The file pointer is moved depending on the given "function"
330    * parameter.
331    */
332   switch (dwOrigin)
333   {
334     case STREAM_SEEK_SET:
335       plibNewPosition->u.HighPart = 0;
336       plibNewPosition->u.LowPart  = 0;
337       break;
338     case STREAM_SEEK_CUR:
339       *plibNewPosition = This->currentPosition;
340       break;
341     case STREAM_SEEK_END:
342       hr = StorageBaseImpl_ReadDirEntry(This->parentStorage, This->dirEntry, &currentEntry);
343       if (FAILED(hr)) return hr;
344       *plibNewPosition = currentEntry.size;
345       break;
346     default:
347       WARN("invalid dwOrigin %d\n", dwOrigin);
348       return STG_E_INVALIDFUNCTION;
349   }
350
351   plibNewPosition->QuadPart += dlibMove.QuadPart;
352
353   /*
354    * tell the caller what we calculated
355    */
356   This->currentPosition = *plibNewPosition;
357
358   return S_OK;
359 }
360
361 /***
362  * This method is part of the IStream interface.
363  *
364  * It will change the size of a stream.
365  *
366  * See the documentation of IStream for more info.
367  */
368 static HRESULT WINAPI StgStreamImpl_SetSize(
369                                      IStream*      iface,
370                                      ULARGE_INTEGER  libNewSize)   /* [in] */
371 {
372   StgStreamImpl* This = impl_from_IStream(iface);
373
374   HRESULT      hr;
375
376   TRACE("(%p, %d)\n", iface, libNewSize.u.LowPart);
377
378   if(!This->parentStorage)
379   {
380     WARN("storage reverted\n");
381     return STG_E_REVERTED;
382   }
383
384   /*
385    * As documented.
386    */
387   if (libNewSize.u.HighPart != 0)
388   {
389     WARN("invalid value for libNewSize.u.HighPart %d\n", libNewSize.u.HighPart);
390     return STG_E_INVALIDFUNCTION;
391   }
392
393   /*
394    * Do we have permission?
395    */
396   if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
397   {
398     WARN("access denied\n");
399     return STG_E_ACCESSDENIED;
400   }
401
402   hr = StorageBaseImpl_StreamSetSize(This->parentStorage, This->dirEntry, libNewSize);
403
404   if (SUCCEEDED(hr))
405     hr = StorageBaseImpl_Flush(This->parentStorage);
406
407   return hr;
408 }
409
410 /***
411  * This method is part of the IStream interface.
412  *
413  * It will copy the 'cb' Bytes to 'pstm' IStream.
414  *
415  * See the documentation of IStream for more info.
416  */
417 static HRESULT WINAPI StgStreamImpl_CopyTo(
418                                     IStream*      iface,
419                                     IStream*      pstm,         /* [unique][in] */
420                                     ULARGE_INTEGER  cb,           /* [in] */
421                                     ULARGE_INTEGER* pcbRead,      /* [out] */
422                                     ULARGE_INTEGER* pcbWritten)   /* [out] */
423 {
424   StgStreamImpl* This = impl_from_IStream(iface);
425   HRESULT        hr = S_OK;
426   BYTE           tmpBuffer[128];
427   ULONG          bytesRead, bytesWritten, copySize;
428   ULARGE_INTEGER totalBytesRead;
429   ULARGE_INTEGER totalBytesWritten;
430
431   TRACE("(%p, %p, %d, %p, %p)\n",
432         iface, pstm, cb.u.LowPart, pcbRead, pcbWritten);
433
434   /*
435    * Sanity check
436    */
437
438   if (!This->parentStorage)
439   {
440     WARN("storage reverted\n");
441     return STG_E_REVERTED;
442   }
443
444   if ( pstm == 0 )
445     return STG_E_INVALIDPOINTER;
446
447   totalBytesRead.QuadPart = 0;
448   totalBytesWritten.QuadPart = 0;
449
450   while ( cb.QuadPart > 0 )
451   {
452     if ( cb.QuadPart >= sizeof(tmpBuffer) )
453       copySize = sizeof(tmpBuffer);
454     else
455       copySize = cb.u.LowPart;
456
457     IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
458
459     totalBytesRead.QuadPart += bytesRead;
460
461     IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
462
463     totalBytesWritten.QuadPart += bytesWritten;
464
465     /*
466      * Check that read & write operations were successful
467      */
468     if (bytesRead != bytesWritten)
469     {
470       hr = STG_E_MEDIUMFULL;
471       WARN("medium full\n");
472       break;
473     }
474
475     if (bytesRead!=copySize)
476       cb.QuadPart = 0;
477     else
478       cb.QuadPart -= bytesRead;
479   }
480
481   if (pcbRead) pcbRead->QuadPart = totalBytesRead.QuadPart;
482   if (pcbWritten) pcbWritten->QuadPart = totalBytesWritten.QuadPart;
483
484   return hr;
485 }
486
487 /***
488  * This method is part of the IStream interface.
489  *
490  * For streams contained in structured storages, this method
491  * does nothing. This is what the documentation tells us.
492  *
493  * See the documentation of IStream for more info.
494  */
495 static HRESULT WINAPI StgStreamImpl_Commit(
496                   IStream*      iface,
497                   DWORD           grfCommitFlags)  /* [in] */
498 {
499   StgStreamImpl* This = impl_from_IStream(iface);
500
501   if (!This->parentStorage)
502   {
503     WARN("storage reverted\n");
504     return STG_E_REVERTED;
505   }
506
507   return StorageBaseImpl_Flush(This->parentStorage);
508 }
509
510 /***
511  * This method is part of the IStream interface.
512  *
513  * For streams contained in structured storages, this method
514  * does nothing. This is what the documentation tells us.
515  *
516  * See the documentation of IStream for more info.
517  */
518 static HRESULT WINAPI StgStreamImpl_Revert(
519                   IStream* iface)
520 {
521   return S_OK;
522 }
523
524 static HRESULT WINAPI StgStreamImpl_LockRegion(
525                                         IStream*     iface,
526                                         ULARGE_INTEGER libOffset,   /* [in] */
527                                         ULARGE_INTEGER cb,          /* [in] */
528                                         DWORD          dwLockType)  /* [in] */
529 {
530   StgStreamImpl* This = impl_from_IStream(iface);
531
532   if (!This->parentStorage)
533   {
534     WARN("storage reverted\n");
535     return STG_E_REVERTED;
536   }
537
538   FIXME("not implemented!\n");
539   return E_NOTIMPL;
540 }
541
542 static HRESULT WINAPI StgStreamImpl_UnlockRegion(
543                                           IStream*     iface,
544                                           ULARGE_INTEGER libOffset,   /* [in] */
545                                           ULARGE_INTEGER cb,          /* [in] */
546                                           DWORD          dwLockType)  /* [in] */
547 {
548   StgStreamImpl* This = impl_from_IStream(iface);
549
550   if (!This->parentStorage)
551   {
552     WARN("storage reverted\n");
553     return STG_E_REVERTED;
554   }
555
556   FIXME("not implemented!\n");
557   return E_NOTIMPL;
558 }
559
560 /***
561  * This method is part of the IStream interface.
562  *
563  * This method returns information about the current
564  * stream.
565  *
566  * See the documentation of IStream for more info.
567  */
568 static HRESULT WINAPI StgStreamImpl_Stat(
569                   IStream*     iface,
570                   STATSTG*       pstatstg,     /* [out] */
571                   DWORD          grfStatFlag)  /* [in] */
572 {
573   StgStreamImpl* This = impl_from_IStream(iface);
574
575   DirEntry     currentEntry;
576   HRESULT      hr;
577
578   TRACE("%p %p %d\n", This, pstatstg, grfStatFlag);
579
580   /*
581    * if stream has no parent, return STG_E_REVERTED
582    */
583
584   if (!This->parentStorage)
585   {
586     WARN("storage reverted\n");
587     return STG_E_REVERTED;
588   }
589
590   /*
591    * Read the information from the directory entry.
592    */
593   hr = StorageBaseImpl_ReadDirEntry(This->parentStorage,
594                                              This->dirEntry,
595                                              &currentEntry);
596
597   if (SUCCEEDED(hr))
598   {
599     StorageUtl_CopyDirEntryToSTATSTG(This->parentStorage,
600                      pstatstg,
601                                      &currentEntry,
602                                      grfStatFlag);
603
604     pstatstg->grfMode = This->grfMode;
605
606     /* In simple create mode cbSize is the current pos */
607     if((This->parentStorage->openFlags & STGM_SIMPLE) && This->parentStorage->create)
608       pstatstg->cbSize = This->currentPosition;
609
610     return S_OK;
611   }
612
613   WARN("failed to read entry\n");
614   return hr;
615 }
616
617 /***
618  * This method is part of the IStream interface.
619  *
620  * This method returns a clone of the interface that allows for
621  * another seek pointer
622  *
623  * See the documentation of IStream for more info.
624  *
625  * I am not totally sure what I am doing here but I presume that this
626  * should be basically as simple as creating a new stream with the same
627  * parent etc and positioning its seek cursor.
628  */
629 static HRESULT WINAPI StgStreamImpl_Clone(
630                                    IStream*     iface,
631                                    IStream**    ppstm) /* [out] */
632 {
633   StgStreamImpl* This = impl_from_IStream(iface);
634   HRESULT hres;
635   StgStreamImpl* new_stream;
636   LARGE_INTEGER seek_pos;
637
638   TRACE("%p %p\n", This, ppstm);
639
640   /*
641    * Sanity check
642    */
643
644   if (!This->parentStorage)
645     return STG_E_REVERTED;
646
647   if ( ppstm == 0 )
648     return STG_E_INVALIDPOINTER;
649
650   new_stream = StgStreamImpl_Construct (This->parentStorage, This->grfMode, This->dirEntry);
651
652   if (!new_stream)
653     return STG_E_INSUFFICIENTMEMORY; /* Currently the only reason for new_stream=0 */
654
655   *ppstm = &new_stream->IStream_iface;
656   IStream_AddRef(*ppstm);
657
658   seek_pos.QuadPart = This->currentPosition.QuadPart;
659
660   hres=StgStreamImpl_Seek (*ppstm, seek_pos, STREAM_SEEK_SET, NULL);
661
662   assert (SUCCEEDED(hres));
663
664   return S_OK;
665 }
666
667 /*
668  * Virtual function table for the StgStreamImpl class.
669  */
670 static const IStreamVtbl StgStreamVtbl =
671 {
672     StgStreamImpl_QueryInterface,
673     StgStreamImpl_AddRef,
674     StgStreamImpl_Release,
675     StgStreamImpl_Read,
676     StgStreamImpl_Write,
677     StgStreamImpl_Seek,
678     StgStreamImpl_SetSize,
679     StgStreamImpl_CopyTo,
680     StgStreamImpl_Commit,
681     StgStreamImpl_Revert,
682     StgStreamImpl_LockRegion,
683     StgStreamImpl_UnlockRegion,
684     StgStreamImpl_Stat,
685     StgStreamImpl_Clone
686 };
687
688 /******************************************************************************
689 ** StgStreamImpl implementation
690 */
691
692 /***
693  * This is the constructor for the StgStreamImpl class.
694  *
695  * Params:
696  *    parentStorage - Pointer to the storage that contains the stream to open
697  *    dirEntry      - Index of the directory entry that points to this stream.
698  */
699 StgStreamImpl* StgStreamImpl_Construct(
700                 StorageBaseImpl* parentStorage,
701     DWORD            grfMode,
702     DirRef           dirEntry)
703 {
704   StgStreamImpl* newStream;
705
706   newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
707
708   if (newStream)
709   {
710     /*
711      * Set-up the virtual function table and reference count.
712      */
713     newStream->IStream_iface.lpVtbl = &StgStreamVtbl;
714     newStream->ref       = 0;
715
716     newStream->parentStorage = parentStorage;
717
718     /*
719      * We want to nail-down the reference to the storage in case the
720      * stream out-lives the storage in the client application.
721      *
722      * -- IStorage_AddRef(&newStream->parentStorage->IStorage_iface);
723      *
724      * No, don't do this. Some apps call IStorage_Release without
725      * calling IStream_Release first. If we grab a reference the
726      * file is not closed, and the app fails when it tries to
727      * reopen the file (Easy-PC, for example)
728      */
729
730     newStream->grfMode = grfMode;
731     newStream->dirEntry = dirEntry;
732
733     /*
734      * Start the stream at the beginning.
735      */
736     newStream->currentPosition.u.HighPart = 0;
737     newStream->currentPosition.u.LowPart = 0;
738
739     /* add us to the storage's list of active streams */
740     StorageBaseImpl_AddStream(parentStorage, newStream);
741   }
742
743   return newStream;
744 }