ole32: Don't send data changes in DataAdviseHolder_Advise for
[wine] / dlls / ole32 / hglobalstream.c
1 /*
2  * HGLOBAL Stream implementation
3  *
4  * This file contains the implementation of the stream interface
5  * for streams contained supported by an HGLOBAL pointer.
6  *
7  * Copyright 1999 Francis Beaudet
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include "config.h"
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 "winuser.h"
39 #include "objbase.h"
40 #include "ole2.h"
41 #include "winerror.h"
42 #include "winreg.h"
43 #include "winternl.h"
44
45 #include "wine/debug.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(storage);
48
49 /****************************************************************************
50  * HGLOBALStreamImpl definition.
51  *
52  * This class imlements the IStream inteface and represents a stream
53  * supported by an HGLOBAL pointer.
54  */
55 struct HGLOBALStreamImpl
56 {
57   const IStreamVtbl *lpVtbl;   /* Needs to be the first item in the struct
58                           * since we want to cast this in an IStream pointer */
59
60   /*
61    * Reference count
62    */
63   LONG               ref;
64
65   /*
66    * Support for the stream
67    */
68   HGLOBAL supportHandle;
69
70   /*
71    * This flag is TRUE if the HGLOBAL is destroyed when the stream
72    * is finally released.
73    */
74   BOOL    deleteOnRelease;
75
76   /*
77    * Helper variable that contains the size of the stream
78    */
79   ULARGE_INTEGER     streamSize;
80
81   /*
82    * This is the current position of the cursor in the stream
83    */
84   ULARGE_INTEGER     currentPosition;
85 };
86
87 typedef struct HGLOBALStreamImpl HGLOBALStreamImpl;
88
89 /***
90  * This is the destructor of the HGLOBALStreamImpl class.
91  *
92  * This method will clean-up all the resources used-up by the given HGLOBALStreamImpl
93  * class. The pointer passed-in to this function will be freed and will not
94  * be valid anymore.
95  */
96 static void HGLOBALStreamImpl_Destroy(HGLOBALStreamImpl* This)
97 {
98   TRACE("(%p)\n", This);
99
100   /*
101    * Release the HGlobal if the constructor asked for that.
102    */
103   if (This->deleteOnRelease)
104   {
105     GlobalFree(This->supportHandle);
106     This->supportHandle=0;
107   }
108
109   /*
110    * Finally, free the memory used-up by the class.
111    */
112   HeapFree(GetProcessHeap(), 0, This);
113 }
114
115 /***
116  * This implements the IUnknown method AddRef for this
117  * class
118  */
119 static ULONG WINAPI HGLOBALStreamImpl_AddRef(
120                 IStream* iface)
121 {
122   HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
123   return InterlockedIncrement(&This->ref);
124 }
125
126 /***
127  * This implements the IUnknown method QueryInterface for this
128  * class
129  */
130 static HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
131                   IStream*     iface,
132                   REFIID         riid,        /* [in] */
133                   void**         ppvObject)   /* [iid_is][out] */
134 {
135   HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
136
137   /*
138    * Perform a sanity check on the parameters.
139    */
140   if (ppvObject==0)
141     return E_INVALIDARG;
142
143   /*
144    * Initialize the return parameter.
145    */
146   *ppvObject = 0;
147
148   /*
149    * Compare the riid with the interface IDs implemented by this object.
150    */
151   if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
152   {
153     *ppvObject = (IStream*)This;
154   }
155   else if (memcmp(&IID_IStream, riid, sizeof(IID_IStream)) == 0)
156   {
157     *ppvObject = (IStream*)This;
158   }
159
160   /*
161    * Check that we obtained an interface.
162    */
163   if ((*ppvObject)==0)
164     return E_NOINTERFACE;
165
166   /*
167    * Query Interface always increases the reference count by one when it is
168    * successful
169    */
170   HGLOBALStreamImpl_AddRef(iface);
171
172   return S_OK;
173 }
174
175 /***
176  * This implements the IUnknown method Release for this
177  * class
178  */
179 static ULONG WINAPI HGLOBALStreamImpl_Release(
180                 IStream* iface)
181 {
182   HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
183   ULONG newRef;
184
185   newRef = InterlockedDecrement(&This->ref);
186
187   /*
188    * If the reference count goes down to 0, perform suicide.
189    */
190   if (newRef==0)
191   {
192     HGLOBALStreamImpl_Destroy(This);
193   }
194
195   return newRef;
196 }
197
198 /***
199  * This method is part of the ISequentialStream interface.
200  *
201  * If reads a block of information from the stream at the current
202  * position. It then moves the current position at the end of the
203  * read block
204  *
205  * See the documentation of ISequentialStream for more info.
206  */
207 static HRESULT WINAPI HGLOBALStreamImpl_Read(
208                   IStream*     iface,
209                   void*          pv,        /* [length_is][size_is][out] */
210                   ULONG          cb,        /* [in] */
211                   ULONG*         pcbRead)   /* [out] */
212 {
213   HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
214
215   void* supportBuffer;
216   ULONG bytesReadBuffer;
217   ULONG bytesToReadFromBuffer;
218
219   TRACE("(%p, %p, %ld, %p)\n", iface,
220         pv, cb, pcbRead);
221
222   /*
223    * If the caller is not interested in the nubmer of bytes read,
224    * we use another buffer to avoid "if" statements in the code.
225    */
226   if (pcbRead==0)
227     pcbRead = &bytesReadBuffer;
228
229   /*
230    * Using the known size of the stream, calculate the number of bytes
231    * to read from the block chain
232    */
233   bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
234
235   /*
236    * Lock the buffer in position and copy the data.
237    */
238   supportBuffer = GlobalLock(This->supportHandle);
239
240   memcpy(pv, (char *) supportBuffer+This->currentPosition.u.LowPart, bytesToReadFromBuffer);
241
242   /*
243    * Move the current position to the new position
244    */
245   This->currentPosition.u.LowPart+=bytesToReadFromBuffer;
246
247   /*
248    * Return the number of bytes read.
249    */
250   *pcbRead = bytesToReadFromBuffer;
251
252   /*
253    * Cleanup
254    */
255   GlobalUnlock(This->supportHandle);
256
257   /*
258    * The function returns S_OK if the buffer was filled completely
259    * it returns S_FALSE if the end of the stream is reached before the
260    * buffer is filled
261    */
262   if(*pcbRead == cb)
263     return S_OK;
264
265   return S_FALSE;
266 }
267
268 /***
269  * This method is part of the ISequentialStream interface.
270  *
271  * It writes a block of information to the stream at the current
272  * position. It then moves the current position at the end of the
273  * written block. If the stream is too small to fit the block,
274  * the stream is grown to fit.
275  *
276  * See the documentation of ISequentialStream for more info.
277  */
278 static HRESULT WINAPI HGLOBALStreamImpl_Write(
279                   IStream*     iface,
280                   const void*    pv,          /* [size_is][in] */
281                   ULONG          cb,          /* [in] */
282                   ULONG*         pcbWritten)  /* [out] */
283 {
284   HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
285
286   void*          supportBuffer;
287   ULARGE_INTEGER newSize;
288   ULONG          bytesWritten = 0;
289
290   TRACE("(%p, %p, %ld, %p)\n", iface, pv, cb, pcbWritten);
291
292   /*
293    * If the caller is not interested in the number of bytes written,
294    * we use another buffer to avoid "if" statements in the code.
295    */
296   if (pcbWritten == 0)
297     pcbWritten = &bytesWritten;
298
299   if (cb == 0)
300     goto out;
301
302   newSize.u.HighPart = 0;
303   newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
304
305   /*
306    * Verify if we need to grow the stream
307    */
308   if (newSize.u.LowPart > This->streamSize.u.LowPart)
309   {
310     /* grow stream */
311     HRESULT hr = IStream_SetSize(iface, newSize);
312     if (FAILED(hr))
313     {
314       ERR("IStream_SetSize failed with error 0x%08lx\n", hr);
315       return hr;
316     }
317   }
318
319   /*
320    * Lock the buffer in position and copy the data.
321    */
322   supportBuffer = GlobalLock(This->supportHandle);
323
324   memcpy((char *) supportBuffer+This->currentPosition.u.LowPart, pv, cb);
325
326   /*
327    * Move the current position to the new position
328    */
329   This->currentPosition.u.LowPart+=cb;
330
331   /*
332    * Cleanup
333    */
334   GlobalUnlock(This->supportHandle);
335
336 out:
337   /*
338    * Return the number of bytes read.
339    */
340   *pcbWritten = cb;
341
342   return S_OK;
343 }
344
345 /***
346  * This method is part of the IStream interface.
347  *
348  * It will move the current stream pointer according to the parameters
349  * given.
350  *
351  * See the documentation of IStream for more info.
352  */
353 static HRESULT WINAPI HGLOBALStreamImpl_Seek(
354                   IStream*      iface,
355                   LARGE_INTEGER   dlibMove,         /* [in] */
356                   DWORD           dwOrigin,         /* [in] */
357                   ULARGE_INTEGER* plibNewPosition) /* [out] */
358 {
359   HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
360
361   ULARGE_INTEGER newPosition;
362
363   TRACE("(%p, %lx%08lx, %ld, %p)\n", iface, dlibMove.u.HighPart,
364         dlibMove.u.LowPart, dwOrigin, plibNewPosition);
365
366   /*
367    * The file pointer is moved depending on the given "function"
368    * parameter.
369    */
370   switch (dwOrigin)
371   {
372     case STREAM_SEEK_SET:
373       newPosition.u.HighPart = 0;
374       newPosition.u.LowPart = 0;
375       break;
376     case STREAM_SEEK_CUR:
377       newPosition = This->currentPosition;
378       break;
379     case STREAM_SEEK_END:
380       newPosition = This->streamSize;
381       break;
382     default:
383       return STG_E_INVALIDFUNCTION;
384   }
385
386   /*
387    * Move the actual file pointer
388    * If the file pointer ends-up after the end of the stream, the next Write operation will
389    * make the file larger. This is how it is documented.
390    */
391   if (dlibMove.QuadPart < 0 && newPosition.QuadPart < -dlibMove.QuadPart) return STG_E_INVALIDFUNCTION;
392
393   newPosition.QuadPart = RtlLargeIntegerAdd(newPosition.QuadPart, dlibMove.QuadPart);
394
395   if (plibNewPosition) *plibNewPosition = newPosition;
396   This->currentPosition = newPosition;
397
398   return S_OK;
399 }
400
401 /***
402  * This method is part of the IStream interface.
403  *
404  * It will change the size of a stream.
405  *
406  * TODO: Switch from small blocks to big blocks and vice versa.
407  *
408  * See the documentation of IStream for more info.
409  */
410 static HRESULT WINAPI HGLOBALStreamImpl_SetSize(
411                                      IStream*      iface,
412                                      ULARGE_INTEGER  libNewSize)   /* [in] */
413 {
414   HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
415   HGLOBAL supportHandle;
416
417   TRACE("(%p, %ld)\n", iface, libNewSize.u.LowPart);
418
419   /*
420    * As documented.
421    */
422   if (libNewSize.u.HighPart != 0)
423     return STG_E_INVALIDFUNCTION;
424
425   if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
426     return S_OK;
427
428   /*
429    * Re allocate the HGlobal to fit the new size of the stream.
430    */
431   supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
432
433   if (supportHandle == 0)
434     return STG_E_MEDIUMFULL;
435
436   This->supportHandle = supportHandle;
437   This->streamSize.u.LowPart = libNewSize.u.LowPart;
438
439   return S_OK;
440 }
441
442 /***
443  * This method is part of the IStream interface.
444  *
445  * It will copy the 'cb' Bytes to 'pstm' IStream.
446  *
447  * See the documentation of IStream for more info.
448  */
449 static HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
450                                     IStream*      iface,
451                                     IStream*      pstm,         /* [unique][in] */
452                                     ULARGE_INTEGER  cb,           /* [in] */
453                                     ULARGE_INTEGER* pcbRead,      /* [out] */
454                                     ULARGE_INTEGER* pcbWritten)   /* [out] */
455 {
456   HRESULT        hr = S_OK;
457   BYTE           tmpBuffer[128];
458   ULONG          bytesRead, bytesWritten, copySize;
459   ULARGE_INTEGER totalBytesRead;
460   ULARGE_INTEGER totalBytesWritten;
461
462   TRACE("(%p, %p, %ld, %p, %p)\n", iface, pstm,
463         cb.u.LowPart, pcbRead, pcbWritten);
464
465   /*
466    * Sanity check
467    */
468   if ( pstm == 0 )
469     return STG_E_INVALIDPOINTER;
470
471   totalBytesRead.u.LowPart = totalBytesRead.u.HighPart = 0;
472   totalBytesWritten.u.LowPart = totalBytesWritten.u.HighPart = 0;
473
474   /*
475    * use stack to store data temporarly
476    * there is surely more performant way of doing it, for now this basic
477    * implementation will do the job
478    */
479   while ( cb.u.LowPart > 0 )
480   {
481     if ( cb.u.LowPart >= 128 )
482       copySize = 128;
483     else
484       copySize = cb.u.LowPart;
485
486     IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
487
488     totalBytesRead.u.LowPart += bytesRead;
489
490     IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
491
492     totalBytesWritten.u.LowPart += bytesWritten;
493
494     /*
495      * Check that read & write operations were succesfull
496      */
497     if (bytesRead != bytesWritten)
498     {
499       hr = STG_E_MEDIUMFULL;
500       break;
501     }
502
503     if (bytesRead!=copySize)
504       cb.u.LowPart = 0;
505     else
506       cb.u.LowPart -= bytesRead;
507   }
508
509   /*
510    * Update number of bytes read and written
511    */
512   if (pcbRead)
513   {
514     pcbRead->u.LowPart = totalBytesRead.u.LowPart;
515     pcbRead->u.HighPart = totalBytesRead.u.HighPart;
516   }
517
518   if (pcbWritten)
519   {
520     pcbWritten->u.LowPart = totalBytesWritten.u.LowPart;
521     pcbWritten->u.HighPart = totalBytesWritten.u.HighPart;
522   }
523   return hr;
524 }
525
526 /***
527  * This method is part of the IStream interface.
528  *
529  * For streams supported by HGLOBALS, this function does nothing.
530  * This is what the documentation tells us.
531  *
532  * See the documentation of IStream for more info.
533  */
534 static HRESULT WINAPI HGLOBALStreamImpl_Commit(
535                   IStream*      iface,
536                   DWORD         grfCommitFlags)  /* [in] */
537 {
538   return S_OK;
539 }
540
541 /***
542  * This method is part of the IStream interface.
543  *
544  * For streams supported by HGLOBALS, this function does nothing.
545  * This is what the documentation tells us.
546  *
547  * See the documentation of IStream for more info.
548  */
549 static HRESULT WINAPI HGLOBALStreamImpl_Revert(
550                   IStream* iface)
551 {
552   return S_OK;
553 }
554
555 /***
556  * This method is part of the IStream interface.
557  *
558  * For streams supported by HGLOBALS, this function does nothing.
559  * This is what the documentation tells us.
560  *
561  * See the documentation of IStream for more info.
562  */
563 static HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
564                   IStream*       iface,
565                   ULARGE_INTEGER libOffset,   /* [in] */
566                   ULARGE_INTEGER cb,          /* [in] */
567                   DWORD          dwLockType)  /* [in] */
568 {
569   return S_OK;
570 }
571
572 /*
573  * This method is part of the IStream interface.
574  *
575  * For streams supported by HGLOBALS, this function does nothing.
576  * This is what the documentation tells us.
577  *
578  * See the documentation of IStream for more info.
579  */
580 static HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
581                   IStream*       iface,
582                   ULARGE_INTEGER libOffset,   /* [in] */
583                   ULARGE_INTEGER cb,          /* [in] */
584                   DWORD          dwLockType)  /* [in] */
585 {
586   return S_OK;
587 }
588
589 /***
590  * This method is part of the IStream interface.
591  *
592  * This method returns information about the current
593  * stream.
594  *
595  * See the documentation of IStream for more info.
596  */
597 static HRESULT WINAPI HGLOBALStreamImpl_Stat(
598                   IStream*     iface,
599                   STATSTG*     pstatstg,     /* [out] */
600                   DWORD        grfStatFlag)  /* [in] */
601 {
602   HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
603
604   memset(pstatstg, 0, sizeof(STATSTG));
605
606   pstatstg->pwcsName = NULL;
607   pstatstg->type     = STGTY_STREAM;
608   pstatstg->cbSize   = This->streamSize;
609
610   return S_OK;
611 }
612
613 static HRESULT WINAPI HGLOBALStreamImpl_Clone(
614                   IStream*     iface,
615                   IStream**    ppstm) /* [out] */
616 {
617   ULARGE_INTEGER dummy;
618   LARGE_INTEGER offset;
619   HRESULT hr;
620   HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
621   TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface,This->deleteOnRelease,(long)This->currentPosition.QuadPart);
622   hr=CreateStreamOnHGlobal(This->supportHandle, FALSE, ppstm);
623   if(FAILED(hr))
624     return hr;
625   offset.QuadPart=(LONGLONG)This->currentPosition.QuadPart;
626   HGLOBALStreamImpl_Seek(*ppstm,offset,STREAM_SEEK_SET,&dummy);
627   return S_OK;
628 }
629
630 /*
631  * Virtual function table for the HGLOBALStreamImpl class.
632  */
633 static const IStreamVtbl HGLOBALStreamImpl_Vtbl =
634 {
635     HGLOBALStreamImpl_QueryInterface,
636     HGLOBALStreamImpl_AddRef,
637     HGLOBALStreamImpl_Release,
638     HGLOBALStreamImpl_Read,
639     HGLOBALStreamImpl_Write,
640     HGLOBALStreamImpl_Seek,
641     HGLOBALStreamImpl_SetSize,
642     HGLOBALStreamImpl_CopyTo,
643     HGLOBALStreamImpl_Commit,
644     HGLOBALStreamImpl_Revert,
645     HGLOBALStreamImpl_LockRegion,
646     HGLOBALStreamImpl_UnlockRegion,
647     HGLOBALStreamImpl_Stat,
648     HGLOBALStreamImpl_Clone
649 };
650
651 /******************************************************************************
652 ** HGLOBALStreamImpl implementation
653 */
654
655 /***
656  * This is the constructor for the HGLOBALStreamImpl class.
657  *
658  * Params:
659  *    hGlobal          - Handle that will support the stream. can be NULL.
660  *    fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
661  *                       when the IStream object is destroyed.
662  */
663 HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
664                 HGLOBAL  hGlobal,
665                 BOOL     fDeleteOnRelease)
666 {
667   HGLOBALStreamImpl* newStream;
668
669   newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl));
670
671   if (newStream!=0)
672   {
673     /*
674      * Set-up the virtual function table and reference count.
675      */
676     newStream->lpVtbl = &HGLOBALStreamImpl_Vtbl;
677     newStream->ref    = 0;
678
679     /*
680      * Initialize the support.
681      */
682     newStream->supportHandle = hGlobal;
683     newStream->deleteOnRelease = fDeleteOnRelease;
684
685     /*
686      * This method will allocate a handle if one is not supplied.
687      */
688     if (!newStream->supportHandle)
689     {
690       newStream->supportHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD |
691                                              GMEM_SHARE, 0);
692     }
693
694     /*
695      * Start the stream at the beginning.
696      */
697     newStream->currentPosition.u.HighPart = 0;
698     newStream->currentPosition.u.LowPart = 0;
699
700     /*
701      * Initialize the size of the stream to the size of the handle.
702      */
703     newStream->streamSize.u.HighPart = 0;
704     newStream->streamSize.u.LowPart  = GlobalSize(newStream->supportHandle);
705   }
706
707   return newStream;
708 }
709
710
711 /***********************************************************************
712  *           CreateStreamOnHGlobal     [OLE32.@]
713  */
714 HRESULT WINAPI CreateStreamOnHGlobal(
715                 HGLOBAL   hGlobal,
716                 BOOL      fDeleteOnRelease,
717                 LPSTREAM* ppstm)
718 {
719   HGLOBALStreamImpl* newStream;
720
721   newStream = HGLOBALStreamImpl_Construct(hGlobal,
722                                           fDeleteOnRelease);
723
724   if (newStream!=NULL)
725   {
726     return IUnknown_QueryInterface((IUnknown*)newStream,
727                                    &IID_IStream,
728                                    (void**)ppstm);
729   }
730
731   return E_OUTOFMEMORY;
732 }
733
734 /***********************************************************************
735  *           GetHGlobalFromStream     [OLE32.@]
736  */
737 HRESULT WINAPI GetHGlobalFromStream(IStream* pstm, HGLOBAL* phglobal)
738 {
739   HGLOBALStreamImpl* pStream;
740
741   if (pstm == NULL)
742     return E_INVALIDARG;
743
744   pStream = (HGLOBALStreamImpl*) pstm;
745
746   /*
747    * Verify that the stream object was created with CreateStreamOnHGlobal.
748    */
749   if (pStream->lpVtbl == &HGLOBALStreamImpl_Vtbl)
750     *phglobal = pStream->supportHandle;
751   else
752   {
753     *phglobal = 0;
754     return E_INVALIDARG;
755   }
756
757   return S_OK;
758 }