Get rid of the no longer used ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
[wine/multimedia.git] / dlls / ole32 / hglobalstream.c
blob3c8b41a102f0275c97f37a6dc73792b7848d858a
1 /*
2 * HGLOBAL Stream implementation
4 * This file contains the implementation of the stream interface
5 * for streams contained supported by an HGLOBAL pointer.
7 * Copyright 1999 Francis Beaudet
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "config.h"
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winuser.h"
37 #include "objbase.h"
38 #include "ole2.h"
39 #include "winerror.h"
40 #include "winreg.h"
41 #include "winternl.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(storage);
47 /****************************************************************************
48 * HGLOBALStreamImpl definition.
50 * This class imlements the IStream inteface and represents a stream
51 * supported by an HGLOBAL pointer.
53 struct HGLOBALStreamImpl
55 IStreamVtbl *lpVtbl; /* Needs to be the first item in the stuct
56 * since we want to cast this in a IStream pointer */
59 * Reference count
61 ULONG ref;
64 * Support for the stream
66 HGLOBAL supportHandle;
69 * This flag is TRUE if the HGLOBAL is destroyed when the stream
70 * is finally released.
72 BOOL deleteOnRelease;
75 * Helper variable that contains the size of the stream
77 ULARGE_INTEGER streamSize;
80 * This is the current position of the cursor in the stream
82 ULARGE_INTEGER currentPosition;
85 typedef struct HGLOBALStreamImpl HGLOBALStreamImpl;
88 * Method definition for the StgStreamImpl class.
90 HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
91 HGLOBAL hGlobal,
92 BOOL fDeleteOnRelease);
94 void HGLOBALStreamImpl_Destroy(
95 HGLOBALStreamImpl* This);
97 void HGLOBALStreamImpl_OpenBlockChain(
98 HGLOBALStreamImpl* This);
100 HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
101 IStream* iface,
102 REFIID riid, /* [in] */
103 void** ppvObject); /* [iid_is][out] */
105 ULONG WINAPI HGLOBALStreamImpl_AddRef(
106 IStream* iface);
108 ULONG WINAPI HGLOBALStreamImpl_Release(
109 IStream* iface);
111 HRESULT WINAPI HGLOBALStreamImpl_Read(
112 IStream* iface,
113 void* pv, /* [length_is][size_is][out] */
114 ULONG cb, /* [in] */
115 ULONG* pcbRead); /* [out] */
117 HRESULT WINAPI HGLOBALStreamImpl_Write(
118 IStream* iface,
119 const void* pv, /* [size_is][in] */
120 ULONG cb, /* [in] */
121 ULONG* pcbWritten); /* [out] */
123 HRESULT WINAPI HGLOBALStreamImpl_Seek(
124 IStream* iface,
125 LARGE_INTEGER dlibMove, /* [in] */
126 DWORD dwOrigin, /* [in] */
127 ULARGE_INTEGER* plibNewPosition); /* [out] */
129 HRESULT WINAPI HGLOBALStreamImpl_SetSize(
130 IStream* iface,
131 ULARGE_INTEGER libNewSize); /* [in] */
133 HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
134 IStream* iface,
135 IStream* pstm, /* [unique][in] */
136 ULARGE_INTEGER cb, /* [in] */
137 ULARGE_INTEGER* pcbRead, /* [out] */
138 ULARGE_INTEGER* pcbWritten); /* [out] */
140 HRESULT WINAPI HGLOBALStreamImpl_Commit(
141 IStream* iface,
142 DWORD grfCommitFlags); /* [in] */
144 HRESULT WINAPI HGLOBALStreamImpl_Revert(
145 IStream* iface);
147 HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
148 IStream* iface,
149 ULARGE_INTEGER libOffset, /* [in] */
150 ULARGE_INTEGER cb, /* [in] */
151 DWORD dwLockType); /* [in] */
153 HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
154 IStream* iface,
155 ULARGE_INTEGER libOffset, /* [in] */
156 ULARGE_INTEGER cb, /* [in] */
157 DWORD dwLockType); /* [in] */
159 HRESULT WINAPI HGLOBALStreamImpl_Stat(
160 IStream* iface,
161 STATSTG* pstatstg, /* [out] */
162 DWORD grfStatFlag); /* [in] */
164 HRESULT WINAPI HGLOBALStreamImpl_Clone(
165 IStream* iface,
166 IStream** ppstm); /* [out] */
170 * Virtual function table for the HGLOBALStreamImpl class.
172 static IStreamVtbl HGLOBALStreamImpl_Vtbl =
174 HGLOBALStreamImpl_QueryInterface,
175 HGLOBALStreamImpl_AddRef,
176 HGLOBALStreamImpl_Release,
177 HGLOBALStreamImpl_Read,
178 HGLOBALStreamImpl_Write,
179 HGLOBALStreamImpl_Seek,
180 HGLOBALStreamImpl_SetSize,
181 HGLOBALStreamImpl_CopyTo,
182 HGLOBALStreamImpl_Commit,
183 HGLOBALStreamImpl_Revert,
184 HGLOBALStreamImpl_LockRegion,
185 HGLOBALStreamImpl_UnlockRegion,
186 HGLOBALStreamImpl_Stat,
187 HGLOBALStreamImpl_Clone
190 /***********************************************************************
191 * CreateStreamOnHGlobal [OLE32.@]
193 HRESULT WINAPI CreateStreamOnHGlobal(
194 HGLOBAL hGlobal,
195 BOOL fDeleteOnRelease,
196 LPSTREAM* ppstm)
198 HGLOBALStreamImpl* newStream;
200 newStream = HGLOBALStreamImpl_Construct(hGlobal,
201 fDeleteOnRelease);
203 if (newStream!=NULL)
205 return IUnknown_QueryInterface((IUnknown*)newStream,
206 &IID_IStream,
207 (void**)ppstm);
210 return E_OUTOFMEMORY;
213 /***********************************************************************
214 * GetHGlobalFromStream [OLE32.@]
216 HRESULT WINAPI GetHGlobalFromStream(IStream* pstm, HGLOBAL* phglobal)
218 HGLOBALStreamImpl* pStream;
220 if (pstm == NULL)
221 return E_INVALIDARG;
223 pStream = (HGLOBALStreamImpl*) pstm;
226 * Verify that the stream object was created with CreateStreamOnHGlobal.
228 if (pStream->lpVtbl == &HGLOBALStreamImpl_Vtbl)
229 *phglobal = pStream->supportHandle;
230 else
232 *phglobal = 0;
233 return E_INVALIDARG;
236 return S_OK;
239 /******************************************************************************
240 ** HGLOBALStreamImpl implementation
243 /***
244 * This is the constructor for the HGLOBALStreamImpl class.
246 * Params:
247 * hGlobal - Handle that will support the stream. can be NULL.
248 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
249 * when the IStream object is destroyed.
251 HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
252 HGLOBAL hGlobal,
253 BOOL fDeleteOnRelease)
255 HGLOBALStreamImpl* newStream;
257 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl));
259 if (newStream!=0)
262 * Set-up the virtual function table and reference count.
264 newStream->lpVtbl = &HGLOBALStreamImpl_Vtbl;
265 newStream->ref = 0;
268 * Initialize the support.
270 newStream->supportHandle = hGlobal;
271 newStream->deleteOnRelease = fDeleteOnRelease;
274 * This method will allocate a handle if one is not supplied.
276 if (!newStream->supportHandle)
278 newStream->supportHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD |
279 GMEM_SHARE, 0);
283 * Start the stream at the beginning.
285 newStream->currentPosition.u.HighPart = 0;
286 newStream->currentPosition.u.LowPart = 0;
289 * Initialize the size of the stream to the size of the handle.
291 newStream->streamSize.u.HighPart = 0;
292 newStream->streamSize.u.LowPart = GlobalSize(newStream->supportHandle);
295 return newStream;
298 /***
299 * This is the destructor of the HGLOBALStreamImpl class.
301 * This method will clean-up all the resources used-up by the given HGLOBALStreamImpl
302 * class. The pointer passed-in to this function will be freed and will not
303 * be valid anymore.
305 void HGLOBALStreamImpl_Destroy(HGLOBALStreamImpl* This)
307 TRACE("(%p)\n", This);
310 * Release the HGlobal if the constructor asked for that.
312 if (This->deleteOnRelease)
314 GlobalFree(This->supportHandle);
315 This->supportHandle=0;
319 * Finally, free the memory used-up by the class.
321 HeapFree(GetProcessHeap(), 0, This);
324 /***
325 * This implements the IUnknown method QueryInterface for this
326 * class
328 HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
329 IStream* iface,
330 REFIID riid, /* [in] */
331 void** ppvObject) /* [iid_is][out] */
333 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
336 * Perform a sanity check on the parameters.
338 if (ppvObject==0)
339 return E_INVALIDARG;
342 * Initialize the return parameter.
344 *ppvObject = 0;
347 * Compare the riid with the interface IDs implemented by this object.
349 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
351 *ppvObject = (IStream*)This;
353 else if (memcmp(&IID_IStream, riid, sizeof(IID_IStream)) == 0)
355 *ppvObject = (IStream*)This;
359 * Check that we obtained an interface.
361 if ((*ppvObject)==0)
362 return E_NOINTERFACE;
365 * Query Interface always increases the reference count by one when it is
366 * successful
368 HGLOBALStreamImpl_AddRef(iface);
370 return S_OK;
373 /***
374 * This implements the IUnknown method AddRef for this
375 * class
377 ULONG WINAPI HGLOBALStreamImpl_AddRef(
378 IStream* iface)
380 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
382 This->ref++;
384 return This->ref;
387 /***
388 * This implements the IUnknown method Release for this
389 * class
391 ULONG WINAPI HGLOBALStreamImpl_Release(
392 IStream* iface)
394 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
396 ULONG newRef;
398 This->ref--;
400 newRef = This->ref;
403 * If the reference count goes down to 0, perform suicide.
405 if (newRef==0)
407 HGLOBALStreamImpl_Destroy(This);
410 return newRef;
413 /***
414 * This method is part of the ISequentialStream interface.
416 * If reads a block of information from the stream at the current
417 * position. It then moves the current position at the end of the
418 * read block
420 * See the documentation of ISequentialStream for more info.
422 HRESULT WINAPI HGLOBALStreamImpl_Read(
423 IStream* iface,
424 void* pv, /* [length_is][size_is][out] */
425 ULONG cb, /* [in] */
426 ULONG* pcbRead) /* [out] */
428 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
430 void* supportBuffer;
431 ULONG bytesReadBuffer;
432 ULONG bytesToReadFromBuffer;
434 TRACE("(%p, %p, %ld, %p)\n", iface,
435 pv, cb, pcbRead);
438 * If the caller is not interested in the nubmer of bytes read,
439 * we use another buffer to avoid "if" statements in the code.
441 if (pcbRead==0)
442 pcbRead = &bytesReadBuffer;
445 * Using the known size of the stream, calculate the number of bytes
446 * to read from the block chain
448 bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
451 * Lock the buffer in position and copy the data.
453 supportBuffer = GlobalLock(This->supportHandle);
455 memcpy(pv, (char *) supportBuffer+This->currentPosition.u.LowPart, bytesToReadFromBuffer);
458 * Move the current position to the new position
460 This->currentPosition.u.LowPart+=bytesToReadFromBuffer;
463 * Return the number of bytes read.
465 *pcbRead = bytesToReadFromBuffer;
468 * Cleanup
470 GlobalUnlock(This->supportHandle);
473 * The function returns S_OK if the buffer was filled completely
474 * it returns S_FALSE if the end of the stream is reached before the
475 * buffer is filled
477 if(*pcbRead == cb)
478 return S_OK;
480 return S_FALSE;
483 /***
484 * This method is part of the ISequentialStream interface.
486 * It writes a block of information to the stream at the current
487 * position. It then moves the current position at the end of the
488 * written block. If the stream is too small to fit the block,
489 * the stream is grown to fit.
491 * See the documentation of ISequentialStream for more info.
493 HRESULT WINAPI HGLOBALStreamImpl_Write(
494 IStream* iface,
495 const void* pv, /* [size_is][in] */
496 ULONG cb, /* [in] */
497 ULONG* pcbWritten) /* [out] */
499 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
501 void* supportBuffer;
502 ULARGE_INTEGER newSize;
503 ULONG bytesWritten = 0;
505 TRACE("(%p, %p, %ld, %p)\n", iface,
506 pv, cb, pcbWritten);
509 * If the caller is not interested in the number of bytes written,
510 * we use another buffer to avoid "if" statements in the code.
512 if (pcbWritten == 0)
513 pcbWritten = &bytesWritten;
515 if (cb == 0)
517 return S_OK;
519 else
521 newSize.u.HighPart = 0;
522 newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
526 * Verify if we need to grow the stream
528 if (newSize.u.LowPart > This->streamSize.u.LowPart)
530 /* grow stream */
531 IStream_SetSize(iface, newSize);
535 * Lock the buffer in position and copy the data.
537 supportBuffer = GlobalLock(This->supportHandle);
539 memcpy((char *) supportBuffer+This->currentPosition.u.LowPart, pv, cb);
542 * Move the current position to the new position
544 This->currentPosition.u.LowPart+=cb;
547 * Return the number of bytes read.
549 *pcbWritten = cb;
552 * Cleanup
554 GlobalUnlock(This->supportHandle);
556 return S_OK;
559 /***
560 * This method is part of the IStream interface.
562 * It will move the current stream pointer according to the parameters
563 * given.
565 * See the documentation of IStream for more info.
567 HRESULT WINAPI HGLOBALStreamImpl_Seek(
568 IStream* iface,
569 LARGE_INTEGER dlibMove, /* [in] */
570 DWORD dwOrigin, /* [in] */
571 ULARGE_INTEGER* plibNewPosition) /* [out] */
573 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
575 ULARGE_INTEGER newPosition;
577 TRACE("(%p, %lx%08lx, %ld, %p)\n", iface, dlibMove.u.HighPart,
578 dlibMove.u.LowPart, dwOrigin, plibNewPosition);
581 * The file pointer is moved depending on the given "function"
582 * parameter.
584 switch (dwOrigin)
586 case STREAM_SEEK_SET:
587 newPosition.u.HighPart = 0;
588 newPosition.u.LowPart = 0;
589 break;
590 case STREAM_SEEK_CUR:
591 newPosition = This->currentPosition;
592 break;
593 case STREAM_SEEK_END:
594 newPosition = This->streamSize;
595 break;
596 default:
597 return STG_E_INVALIDFUNCTION;
601 * Move the actual file pointer
602 * If the file pointer ends-up after the end of the stream, the next Write operation will
603 * make the file larger. This is how it is documented.
605 newPosition.QuadPart = RtlLargeIntegerAdd(newPosition.QuadPart, dlibMove.QuadPart);
606 if (newPosition.QuadPart < 0) return STG_E_INVALIDFUNCTION;
608 if (plibNewPosition) *plibNewPosition = newPosition;
609 This->currentPosition = newPosition;
611 return S_OK;
614 /***
615 * This method is part of the IStream interface.
617 * It will change the size of a stream.
619 * TODO: Switch from small blocks to big blocks and vice versa.
621 * See the documentation of IStream for more info.
623 HRESULT WINAPI HGLOBALStreamImpl_SetSize(
624 IStream* iface,
625 ULARGE_INTEGER libNewSize) /* [in] */
627 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
628 HGLOBAL supportHandle;
630 TRACE("(%p, %ld)\n", iface, libNewSize.u.LowPart);
633 * As documented.
635 if (libNewSize.u.HighPart != 0)
636 return STG_E_INVALIDFUNCTION;
638 if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
639 return S_OK;
642 * Re allocate the HGlobal to fit the new size of the stream.
644 supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
646 if (supportHandle == 0)
647 return STG_E_MEDIUMFULL;
649 This->supportHandle = supportHandle;
650 This->streamSize.u.LowPart = libNewSize.u.LowPart;
652 return S_OK;
655 /***
656 * This method is part of the IStream interface.
658 * It will copy the 'cb' Bytes to 'pstm' IStream.
660 * See the documentation of IStream for more info.
662 HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
663 IStream* iface,
664 IStream* pstm, /* [unique][in] */
665 ULARGE_INTEGER cb, /* [in] */
666 ULARGE_INTEGER* pcbRead, /* [out] */
667 ULARGE_INTEGER* pcbWritten) /* [out] */
669 HRESULT hr = S_OK;
670 BYTE tmpBuffer[128];
671 ULONG bytesRead, bytesWritten, copySize;
672 ULARGE_INTEGER totalBytesRead;
673 ULARGE_INTEGER totalBytesWritten;
675 TRACE("(%p, %p, %ld, %p, %p)\n", iface, pstm,
676 cb.u.LowPart, pcbRead, pcbWritten);
679 * Sanity check
681 if ( pstm == 0 )
682 return STG_E_INVALIDPOINTER;
684 totalBytesRead.u.LowPart = totalBytesRead.u.HighPart = 0;
685 totalBytesWritten.u.LowPart = totalBytesWritten.u.HighPart = 0;
688 * use stack to store data temporarly
689 * there is surely more performant way of doing it, for now this basic
690 * implementation will do the job
692 while ( cb.u.LowPart > 0 )
694 if ( cb.u.LowPart >= 128 )
695 copySize = 128;
696 else
697 copySize = cb.u.LowPart;
699 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
701 totalBytesRead.u.LowPart += bytesRead;
703 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
705 totalBytesWritten.u.LowPart += bytesWritten;
708 * Check that read & write operations were succesfull
710 if (bytesRead != bytesWritten)
712 hr = STG_E_MEDIUMFULL;
713 break;
716 if (bytesRead!=copySize)
717 cb.u.LowPart = 0;
718 else
719 cb.u.LowPart -= bytesRead;
723 * Update number of bytes read and written
725 if (pcbRead)
727 pcbRead->u.LowPart = totalBytesRead.u.LowPart;
728 pcbRead->u.HighPart = totalBytesRead.u.HighPart;
731 if (pcbWritten)
733 pcbWritten->u.LowPart = totalBytesWritten.u.LowPart;
734 pcbWritten->u.HighPart = totalBytesWritten.u.HighPart;
736 return hr;
739 /***
740 * This method is part of the IStream interface.
742 * For streams supported by HGLOBALS, this function does nothing.
743 * This is what the documentation tells us.
745 * See the documentation of IStream for more info.
747 HRESULT WINAPI HGLOBALStreamImpl_Commit(
748 IStream* iface,
749 DWORD grfCommitFlags) /* [in] */
751 return S_OK;
754 /***
755 * This method is part of the IStream interface.
757 * For streams supported by HGLOBALS, this function does nothing.
758 * This is what the documentation tells us.
760 * See the documentation of IStream for more info.
762 HRESULT WINAPI HGLOBALStreamImpl_Revert(
763 IStream* iface)
765 return S_OK;
768 /***
769 * This method is part of the IStream interface.
771 * For streams supported by HGLOBALS, this function does nothing.
772 * This is what the documentation tells us.
774 * See the documentation of IStream for more info.
776 HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
777 IStream* iface,
778 ULARGE_INTEGER libOffset, /* [in] */
779 ULARGE_INTEGER cb, /* [in] */
780 DWORD dwLockType) /* [in] */
782 return S_OK;
786 * This method is part of the IStream interface.
788 * For streams supported by HGLOBALS, this function does nothing.
789 * This is what the documentation tells us.
791 * See the documentation of IStream for more info.
793 HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
794 IStream* iface,
795 ULARGE_INTEGER libOffset, /* [in] */
796 ULARGE_INTEGER cb, /* [in] */
797 DWORD dwLockType) /* [in] */
799 return S_OK;
802 /***
803 * This method is part of the IStream interface.
805 * This method returns information about the current
806 * stream.
808 * See the documentation of IStream for more info.
810 HRESULT WINAPI HGLOBALStreamImpl_Stat(
811 IStream* iface,
812 STATSTG* pstatstg, /* [out] */
813 DWORD grfStatFlag) /* [in] */
815 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
817 memset(pstatstg, 0, sizeof(STATSTG));
819 pstatstg->pwcsName = NULL;
820 pstatstg->type = STGTY_STREAM;
821 pstatstg->cbSize = This->streamSize;
823 return S_OK;
826 HRESULT WINAPI HGLOBALStreamImpl_Clone(
827 IStream* iface,
828 IStream** ppstm) /* [out] */
830 ULARGE_INTEGER dummy;
831 LARGE_INTEGER offset;
832 HRESULT hr;
833 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
834 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface,This->deleteOnRelease,(long)This->currentPosition.QuadPart);
835 hr=CreateStreamOnHGlobal(This->supportHandle, FALSE, ppstm);
836 if(FAILED(hr))
837 return hr;
838 offset.QuadPart=(LONGLONG)This->currentPosition.QuadPart;
839 HGLOBALStreamImpl_Seek(*ppstm,offset,STREAM_SEEK_SET,&dummy);
840 return S_OK;