ole32: Remove redundant NULL check (Coverity).
[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 implements the IStream interface 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, %d, %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    * Always returns S_OK even if the end of the stream is reached before the
259    * buffer is filled
260    */
261
262   return S_OK;
263 }
264
265 /***
266  * This method is part of the ISequentialStream interface.
267  *
268  * It writes a block of information to the stream at the current
269  * position. It then moves the current position at the end of the
270  * written block. If the stream is too small to fit the block,
271  * the stream is grown to fit.
272  *
273  * See the documentation of ISequentialStream for more info.
274  */
275 static HRESULT WINAPI HGLOBALStreamImpl_Write(
276                   IStream*     iface,
277                   const void*    pv,          /* [size_is][in] */
278                   ULONG          cb,          /* [in] */
279                   ULONG*         pcbWritten)  /* [out] */
280 {
281   HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
282
283   void*          supportBuffer;
284   ULARGE_INTEGER newSize;
285   ULONG          bytesWritten = 0;
286
287   TRACE("(%p, %p, %d, %p)\n", iface, pv, cb, pcbWritten);
288
289   /*
290    * If the caller is not interested in the number of bytes written,
291    * we use another buffer to avoid "if" statements in the code.
292    */
293   if (pcbWritten == 0)
294     pcbWritten = &bytesWritten;
295
296   if (cb == 0)
297     goto out;
298
299   newSize.u.HighPart = 0;
300   newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
301
302   /*
303    * Verify if we need to grow the stream
304    */
305   if (newSize.u.LowPart > This->streamSize.u.LowPart)
306   {
307     /* grow stream */
308     HRESULT hr = IStream_SetSize(iface, newSize);
309     if (FAILED(hr))
310     {
311       ERR("IStream_SetSize failed with error 0x%08x\n", hr);
312       return hr;
313     }
314   }
315
316   /*
317    * Lock the buffer in position and copy the data.
318    */
319   supportBuffer = GlobalLock(This->supportHandle);
320
321   memcpy((char *) supportBuffer+This->currentPosition.u.LowPart, pv, cb);
322
323   /*
324    * Move the current position to the new position
325    */
326   This->currentPosition.u.LowPart+=cb;
327
328   /*
329    * Cleanup
330    */
331   GlobalUnlock(This->supportHandle);
332
333 out:
334   /*
335    * Return the number of bytes read.
336    */
337   *pcbWritten = cb;
338
339   return S_OK;
340 }
341
342 /***
343  * This method is part of the IStream interface.
344  *
345  * It will move the current stream pointer according to the parameters
346  * given.
347  *
348  * See the documentation of IStream for more info.
349  */
350 static HRESULT WINAPI HGLOBALStreamImpl_Seek(
351                   IStream*      iface,
352                   LARGE_INTEGER   dlibMove,         /* [in] */
353                   DWORD           dwOrigin,         /* [in] */
354                   ULARGE_INTEGER* plibNewPosition) /* [out] */
355 {
356   HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
357
358   ULARGE_INTEGER newPosition;
359
360   TRACE("(%p, %x%08x, %d, %p)\n", iface, dlibMove.u.HighPart,
361         dlibMove.u.LowPart, dwOrigin, plibNewPosition);
362
363   /*
364    * The file pointer is moved depending on the given "function"
365    * parameter.
366    */
367   switch (dwOrigin)
368   {
369     case STREAM_SEEK_SET:
370       newPosition.u.HighPart = 0;
371       newPosition.u.LowPart = 0;
372       break;
373     case STREAM_SEEK_CUR:
374       newPosition = This->currentPosition;
375       break;
376     case STREAM_SEEK_END:
377       newPosition = This->streamSize;
378       break;
379     default:
380       return STG_E_INVALIDFUNCTION;
381   }
382
383   /*
384    * Move the actual file pointer
385    * If the file pointer ends-up after the end of the stream, the next Write operation will
386    * make the file larger. This is how it is documented.
387    */
388   if (dlibMove.QuadPart < 0 && newPosition.QuadPart < -dlibMove.QuadPart) return STG_E_INVALIDFUNCTION;
389
390   newPosition.QuadPart = RtlLargeIntegerAdd(newPosition.QuadPart, dlibMove.QuadPart);
391
392   if (plibNewPosition) *plibNewPosition = newPosition;
393   This->currentPosition = newPosition;
394
395   return S_OK;
396 }
397
398 /***
399  * This method is part of the IStream interface.
400  *
401  * It will change the size of a stream.
402  *
403  * TODO: Switch from small blocks to big blocks and vice versa.
404  *
405  * See the documentation of IStream for more info.
406  */
407 static HRESULT WINAPI HGLOBALStreamImpl_SetSize(
408                                      IStream*      iface,
409                                      ULARGE_INTEGER  libNewSize)   /* [in] */
410 {
411   HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
412   HGLOBAL supportHandle;
413
414   TRACE("(%p, %d)\n", iface, libNewSize.u.LowPart);
415
416   /*
417    * HighPart is ignored as shown in tests
418    */
419
420   if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
421     return S_OK;
422
423   /*
424    * Re allocate the HGlobal to fit the new size of the stream.
425    */
426   supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
427
428   if (supportHandle == 0)
429     return E_OUTOFMEMORY;
430
431   This->supportHandle = supportHandle;
432   This->streamSize.u.LowPart = libNewSize.u.LowPart;
433
434   return S_OK;
435 }
436
437 /***
438  * This method is part of the IStream interface.
439  *
440  * It will copy the 'cb' Bytes to 'pstm' IStream.
441  *
442  * See the documentation of IStream for more info.
443  */
444 static HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
445                                     IStream*      iface,
446                                     IStream*      pstm,         /* [unique][in] */
447                                     ULARGE_INTEGER  cb,           /* [in] */
448                                     ULARGE_INTEGER* pcbRead,      /* [out] */
449                                     ULARGE_INTEGER* pcbWritten)   /* [out] */
450 {
451   HRESULT        hr = S_OK;
452   BYTE           tmpBuffer[128];
453   ULONG          bytesRead, bytesWritten, copySize;
454   ULARGE_INTEGER totalBytesRead;
455   ULARGE_INTEGER totalBytesWritten;
456
457   TRACE("(%p, %p, %d, %p, %p)\n", iface, pstm,
458         cb.u.LowPart, pcbRead, pcbWritten);
459
460   /*
461    * Sanity check
462    */
463   if ( pstm == 0 )
464     return STG_E_INVALIDPOINTER;
465
466   totalBytesRead.u.LowPart = totalBytesRead.u.HighPart = 0;
467   totalBytesWritten.u.LowPart = totalBytesWritten.u.HighPart = 0;
468
469   /*
470    * use stack to store data temporarly
471    * there is surely more performant way of doing it, for now this basic
472    * implementation will do the job
473    */
474   while ( cb.u.LowPart > 0 )
475   {
476     if ( cb.u.LowPart >= 128 )
477       copySize = 128;
478     else
479       copySize = cb.u.LowPart;
480
481     IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
482
483     totalBytesRead.u.LowPart += bytesRead;
484
485     IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
486
487     totalBytesWritten.u.LowPart += bytesWritten;
488
489     /*
490      * Check that read & write operations were successful
491      */
492     if (bytesRead != bytesWritten)
493     {
494       hr = STG_E_MEDIUMFULL;
495       break;
496     }
497
498     if (bytesRead!=copySize)
499       cb.u.LowPart = 0;
500     else
501       cb.u.LowPart -= bytesRead;
502   }
503
504   /*
505    * Update number of bytes read and written
506    */
507   if (pcbRead)
508   {
509     pcbRead->u.LowPart = totalBytesRead.u.LowPart;
510     pcbRead->u.HighPart = totalBytesRead.u.HighPart;
511   }
512
513   if (pcbWritten)
514   {
515     pcbWritten->u.LowPart = totalBytesWritten.u.LowPart;
516     pcbWritten->u.HighPart = totalBytesWritten.u.HighPart;
517   }
518   return hr;
519 }
520
521 /***
522  * This method is part of the IStream interface.
523  *
524  * For streams supported by HGLOBALS, this function does nothing.
525  * This is what the documentation tells us.
526  *
527  * See the documentation of IStream for more info.
528  */
529 static HRESULT WINAPI HGLOBALStreamImpl_Commit(
530                   IStream*      iface,
531                   DWORD         grfCommitFlags)  /* [in] */
532 {
533   return S_OK;
534 }
535
536 /***
537  * This method is part of the IStream interface.
538  *
539  * For streams supported by HGLOBALS, this function does nothing.
540  * This is what the documentation tells us.
541  *
542  * See the documentation of IStream for more info.
543  */
544 static HRESULT WINAPI HGLOBALStreamImpl_Revert(
545                   IStream* iface)
546 {
547   return S_OK;
548 }
549
550 /***
551  * This method is part of the IStream interface.
552  *
553  * For streams supported by HGLOBALS, this function does nothing.
554  * This is what the documentation tells us.
555  *
556  * See the documentation of IStream for more info.
557  */
558 static HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
559                   IStream*       iface,
560                   ULARGE_INTEGER libOffset,   /* [in] */
561                   ULARGE_INTEGER cb,          /* [in] */
562                   DWORD          dwLockType)  /* [in] */
563 {
564   return STG_E_INVALIDFUNCTION;
565 }
566
567 /*
568  * This method is part of the IStream interface.
569  *
570  * For streams supported by HGLOBALS, this function does nothing.
571  * This is what the documentation tells us.
572  *
573  * See the documentation of IStream for more info.
574  */
575 static HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
576                   IStream*       iface,
577                   ULARGE_INTEGER libOffset,   /* [in] */
578                   ULARGE_INTEGER cb,          /* [in] */
579                   DWORD          dwLockType)  /* [in] */
580 {
581   return S_OK;
582 }
583
584 /***
585  * This method is part of the IStream interface.
586  *
587  * This method returns information about the current
588  * stream.
589  *
590  * See the documentation of IStream for more info.
591  */
592 static HRESULT WINAPI HGLOBALStreamImpl_Stat(
593                   IStream*     iface,
594                   STATSTG*     pstatstg,     /* [out] */
595                   DWORD        grfStatFlag)  /* [in] */
596 {
597   HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
598
599   memset(pstatstg, 0, sizeof(STATSTG));
600
601   pstatstg->pwcsName = NULL;
602   pstatstg->type     = STGTY_STREAM;
603   pstatstg->cbSize   = This->streamSize;
604
605   return S_OK;
606 }
607
608 static HRESULT WINAPI HGLOBALStreamImpl_Clone(
609                   IStream*     iface,
610                   IStream**    ppstm) /* [out] */
611 {
612   ULARGE_INTEGER dummy;
613   LARGE_INTEGER offset;
614   HRESULT hr;
615   HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
616   TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface,This->deleteOnRelease,(long)This->currentPosition.QuadPart);
617   hr=CreateStreamOnHGlobal(This->supportHandle, FALSE, ppstm);
618   if(FAILED(hr))
619     return hr;
620   offset.QuadPart=(LONGLONG)This->currentPosition.QuadPart;
621   HGLOBALStreamImpl_Seek(*ppstm,offset,STREAM_SEEK_SET,&dummy);
622   return S_OK;
623 }
624
625 /*
626  * Virtual function table for the HGLOBALStreamImpl class.
627  */
628 static const IStreamVtbl HGLOBALStreamImpl_Vtbl =
629 {
630     HGLOBALStreamImpl_QueryInterface,
631     HGLOBALStreamImpl_AddRef,
632     HGLOBALStreamImpl_Release,
633     HGLOBALStreamImpl_Read,
634     HGLOBALStreamImpl_Write,
635     HGLOBALStreamImpl_Seek,
636     HGLOBALStreamImpl_SetSize,
637     HGLOBALStreamImpl_CopyTo,
638     HGLOBALStreamImpl_Commit,
639     HGLOBALStreamImpl_Revert,
640     HGLOBALStreamImpl_LockRegion,
641     HGLOBALStreamImpl_UnlockRegion,
642     HGLOBALStreamImpl_Stat,
643     HGLOBALStreamImpl_Clone
644 };
645
646 /******************************************************************************
647 ** HGLOBALStreamImpl implementation
648 */
649
650 /***
651  * This is the constructor for the HGLOBALStreamImpl class.
652  *
653  * Params:
654  *    hGlobal          - Handle that will support the stream. can be NULL.
655  *    fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
656  *                       when the IStream object is destroyed.
657  */
658 HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
659                 HGLOBAL  hGlobal,
660                 BOOL     fDeleteOnRelease)
661 {
662   HGLOBALStreamImpl* newStream;
663
664   newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl));
665
666   if (newStream!=0)
667   {
668     /*
669      * Set-up the virtual function table and reference count.
670      */
671     newStream->lpVtbl = &HGLOBALStreamImpl_Vtbl;
672     newStream->ref    = 0;
673
674     /*
675      * Initialize the support.
676      */
677     newStream->supportHandle = hGlobal;
678     newStream->deleteOnRelease = fDeleteOnRelease;
679
680     /*
681      * This method will allocate a handle if one is not supplied.
682      */
683     if (!newStream->supportHandle)
684     {
685       newStream->supportHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD |
686                                              GMEM_SHARE, 0);
687     }
688
689     /*
690      * Start the stream at the beginning.
691      */
692     newStream->currentPosition.u.HighPart = 0;
693     newStream->currentPosition.u.LowPart = 0;
694
695     /*
696      * Initialize the size of the stream to the size of the handle.
697      */
698     newStream->streamSize.u.HighPart = 0;
699     newStream->streamSize.u.LowPart  = GlobalSize(newStream->supportHandle);
700   }
701
702   return newStream;
703 }
704
705
706 /***********************************************************************
707  *           CreateStreamOnHGlobal     [OLE32.@]
708  */
709 HRESULT WINAPI CreateStreamOnHGlobal(
710                 HGLOBAL   hGlobal,
711                 BOOL      fDeleteOnRelease,
712                 LPSTREAM* ppstm)
713 {
714   HGLOBALStreamImpl* newStream;
715
716   newStream = HGLOBALStreamImpl_Construct(hGlobal,
717                                           fDeleteOnRelease);
718
719   if (newStream!=NULL)
720   {
721     return IUnknown_QueryInterface((IUnknown*)newStream,
722                                    &IID_IStream,
723                                    (void**)ppstm);
724   }
725
726   return E_OUTOFMEMORY;
727 }
728
729 /***********************************************************************
730  *           GetHGlobalFromStream     [OLE32.@]
731  */
732 HRESULT WINAPI GetHGlobalFromStream(IStream* pstm, HGLOBAL* phglobal)
733 {
734   HGLOBALStreamImpl* pStream;
735
736   if (pstm == NULL)
737     return E_INVALIDARG;
738
739   pStream = (HGLOBALStreamImpl*) pstm;
740
741   /*
742    * Verify that the stream object was created with CreateStreamOnHGlobal.
743    */
744   if (pStream->lpVtbl == &HGLOBALStreamImpl_Vtbl)
745     *phglobal = pStream->supportHandle;
746   else
747   {
748     *phglobal = 0;
749     return E_INVALIDARG;
750   }
751
752   return S_OK;
753 }