Don't define COBJMACROS in objbase.h.
[wine/multimedia.git] / dlls / ole32 / hglobalstream.c
blob1966a0abe02bbe5b5b1894cb27afbe0ccfcc2a60
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 COBJMACROS
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
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"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(storage);
49 /****************************************************************************
50 * HGLOBALStreamImpl definition.
52 * This class imlements the IStream inteface and represents a stream
53 * supported by an HGLOBAL pointer.
55 struct HGLOBALStreamImpl
57 IStreamVtbl *lpVtbl; /* Needs to be the first item in the stuct
58 * since we want to cast this in a IStream pointer */
61 * Reference count
63 ULONG ref;
66 * Support for the stream
68 HGLOBAL supportHandle;
71 * This flag is TRUE if the HGLOBAL is destroyed when the stream
72 * is finally released.
74 BOOL deleteOnRelease;
77 * Helper variable that contains the size of the stream
79 ULARGE_INTEGER streamSize;
82 * This is the current position of the cursor in the stream
84 ULARGE_INTEGER currentPosition;
87 typedef struct HGLOBALStreamImpl HGLOBALStreamImpl;
90 * Method definition for the StgStreamImpl class.
92 HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
93 HGLOBAL hGlobal,
94 BOOL fDeleteOnRelease);
96 void HGLOBALStreamImpl_Destroy(
97 HGLOBALStreamImpl* This);
99 void HGLOBALStreamImpl_OpenBlockChain(
100 HGLOBALStreamImpl* This);
102 HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
103 IStream* iface,
104 REFIID riid, /* [in] */
105 void** ppvObject); /* [iid_is][out] */
107 ULONG WINAPI HGLOBALStreamImpl_AddRef(
108 IStream* iface);
110 ULONG WINAPI HGLOBALStreamImpl_Release(
111 IStream* iface);
113 HRESULT WINAPI HGLOBALStreamImpl_Read(
114 IStream* iface,
115 void* pv, /* [length_is][size_is][out] */
116 ULONG cb, /* [in] */
117 ULONG* pcbRead); /* [out] */
119 HRESULT WINAPI HGLOBALStreamImpl_Write(
120 IStream* iface,
121 const void* pv, /* [size_is][in] */
122 ULONG cb, /* [in] */
123 ULONG* pcbWritten); /* [out] */
125 HRESULT WINAPI HGLOBALStreamImpl_Seek(
126 IStream* iface,
127 LARGE_INTEGER dlibMove, /* [in] */
128 DWORD dwOrigin, /* [in] */
129 ULARGE_INTEGER* plibNewPosition); /* [out] */
131 HRESULT WINAPI HGLOBALStreamImpl_SetSize(
132 IStream* iface,
133 ULARGE_INTEGER libNewSize); /* [in] */
135 HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
136 IStream* iface,
137 IStream* pstm, /* [unique][in] */
138 ULARGE_INTEGER cb, /* [in] */
139 ULARGE_INTEGER* pcbRead, /* [out] */
140 ULARGE_INTEGER* pcbWritten); /* [out] */
142 HRESULT WINAPI HGLOBALStreamImpl_Commit(
143 IStream* iface,
144 DWORD grfCommitFlags); /* [in] */
146 HRESULT WINAPI HGLOBALStreamImpl_Revert(
147 IStream* iface);
149 HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
150 IStream* iface,
151 ULARGE_INTEGER libOffset, /* [in] */
152 ULARGE_INTEGER cb, /* [in] */
153 DWORD dwLockType); /* [in] */
155 HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
156 IStream* iface,
157 ULARGE_INTEGER libOffset, /* [in] */
158 ULARGE_INTEGER cb, /* [in] */
159 DWORD dwLockType); /* [in] */
161 HRESULT WINAPI HGLOBALStreamImpl_Stat(
162 IStream* iface,
163 STATSTG* pstatstg, /* [out] */
164 DWORD grfStatFlag); /* [in] */
166 HRESULT WINAPI HGLOBALStreamImpl_Clone(
167 IStream* iface,
168 IStream** ppstm); /* [out] */
172 * Virtual function table for the HGLOBALStreamImpl class.
174 static IStreamVtbl HGLOBALStreamImpl_Vtbl =
176 HGLOBALStreamImpl_QueryInterface,
177 HGLOBALStreamImpl_AddRef,
178 HGLOBALStreamImpl_Release,
179 HGLOBALStreamImpl_Read,
180 HGLOBALStreamImpl_Write,
181 HGLOBALStreamImpl_Seek,
182 HGLOBALStreamImpl_SetSize,
183 HGLOBALStreamImpl_CopyTo,
184 HGLOBALStreamImpl_Commit,
185 HGLOBALStreamImpl_Revert,
186 HGLOBALStreamImpl_LockRegion,
187 HGLOBALStreamImpl_UnlockRegion,
188 HGLOBALStreamImpl_Stat,
189 HGLOBALStreamImpl_Clone
192 /***********************************************************************
193 * CreateStreamOnHGlobal [OLE32.@]
195 HRESULT WINAPI CreateStreamOnHGlobal(
196 HGLOBAL hGlobal,
197 BOOL fDeleteOnRelease,
198 LPSTREAM* ppstm)
200 HGLOBALStreamImpl* newStream;
202 newStream = HGLOBALStreamImpl_Construct(hGlobal,
203 fDeleteOnRelease);
205 if (newStream!=NULL)
207 return IUnknown_QueryInterface((IUnknown*)newStream,
208 &IID_IStream,
209 (void**)ppstm);
212 return E_OUTOFMEMORY;
215 /***********************************************************************
216 * GetHGlobalFromStream [OLE32.@]
218 HRESULT WINAPI GetHGlobalFromStream(IStream* pstm, HGLOBAL* phglobal)
220 HGLOBALStreamImpl* pStream;
222 if (pstm == NULL)
223 return E_INVALIDARG;
225 pStream = (HGLOBALStreamImpl*) pstm;
228 * Verify that the stream object was created with CreateStreamOnHGlobal.
230 if (pStream->lpVtbl == &HGLOBALStreamImpl_Vtbl)
231 *phglobal = pStream->supportHandle;
232 else
234 *phglobal = 0;
235 return E_INVALIDARG;
238 return S_OK;
241 /******************************************************************************
242 ** HGLOBALStreamImpl implementation
245 /***
246 * This is the constructor for the HGLOBALStreamImpl class.
248 * Params:
249 * hGlobal - Handle that will support the stream. can be NULL.
250 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
251 * when the IStream object is destroyed.
253 HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
254 HGLOBAL hGlobal,
255 BOOL fDeleteOnRelease)
257 HGLOBALStreamImpl* newStream;
259 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl));
261 if (newStream!=0)
264 * Set-up the virtual function table and reference count.
266 newStream->lpVtbl = &HGLOBALStreamImpl_Vtbl;
267 newStream->ref = 0;
270 * Initialize the support.
272 newStream->supportHandle = hGlobal;
273 newStream->deleteOnRelease = fDeleteOnRelease;
276 * This method will allocate a handle if one is not supplied.
278 if (!newStream->supportHandle)
280 newStream->supportHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD |
281 GMEM_SHARE, 0);
285 * Start the stream at the beginning.
287 newStream->currentPosition.u.HighPart = 0;
288 newStream->currentPosition.u.LowPart = 0;
291 * Initialize the size of the stream to the size of the handle.
293 newStream->streamSize.u.HighPart = 0;
294 newStream->streamSize.u.LowPart = GlobalSize(newStream->supportHandle);
297 return newStream;
300 /***
301 * This is the destructor of the HGLOBALStreamImpl class.
303 * This method will clean-up all the resources used-up by the given HGLOBALStreamImpl
304 * class. The pointer passed-in to this function will be freed and will not
305 * be valid anymore.
307 void HGLOBALStreamImpl_Destroy(HGLOBALStreamImpl* This)
309 TRACE("(%p)\n", This);
312 * Release the HGlobal if the constructor asked for that.
314 if (This->deleteOnRelease)
316 GlobalFree(This->supportHandle);
317 This->supportHandle=0;
321 * Finally, free the memory used-up by the class.
323 HeapFree(GetProcessHeap(), 0, This);
326 /***
327 * This implements the IUnknown method QueryInterface for this
328 * class
330 HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
331 IStream* iface,
332 REFIID riid, /* [in] */
333 void** ppvObject) /* [iid_is][out] */
335 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
338 * Perform a sanity check on the parameters.
340 if (ppvObject==0)
341 return E_INVALIDARG;
344 * Initialize the return parameter.
346 *ppvObject = 0;
349 * Compare the riid with the interface IDs implemented by this object.
351 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
353 *ppvObject = (IStream*)This;
355 else if (memcmp(&IID_IStream, riid, sizeof(IID_IStream)) == 0)
357 *ppvObject = (IStream*)This;
361 * Check that we obtained an interface.
363 if ((*ppvObject)==0)
364 return E_NOINTERFACE;
367 * Query Interface always increases the reference count by one when it is
368 * successful
370 HGLOBALStreamImpl_AddRef(iface);
372 return S_OK;
375 /***
376 * This implements the IUnknown method AddRef for this
377 * class
379 ULONG WINAPI HGLOBALStreamImpl_AddRef(
380 IStream* iface)
382 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
383 return InterlockedIncrement(&This->ref);
386 /***
387 * This implements the IUnknown method Release for this
388 * class
390 ULONG WINAPI HGLOBALStreamImpl_Release(
391 IStream* iface)
393 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
394 ULONG newRef;
396 newRef = InterlockedDecrement(&This->ref);
399 * If the reference count goes down to 0, perform suicide.
401 if (newRef==0)
403 HGLOBALStreamImpl_Destroy(This);
406 return newRef;
409 /***
410 * This method is part of the ISequentialStream interface.
412 * If reads a block of information from the stream at the current
413 * position. It then moves the current position at the end of the
414 * read block
416 * See the documentation of ISequentialStream for more info.
418 HRESULT WINAPI HGLOBALStreamImpl_Read(
419 IStream* iface,
420 void* pv, /* [length_is][size_is][out] */
421 ULONG cb, /* [in] */
422 ULONG* pcbRead) /* [out] */
424 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
426 void* supportBuffer;
427 ULONG bytesReadBuffer;
428 ULONG bytesToReadFromBuffer;
430 TRACE("(%p, %p, %ld, %p)\n", iface,
431 pv, cb, pcbRead);
434 * If the caller is not interested in the nubmer of bytes read,
435 * we use another buffer to avoid "if" statements in the code.
437 if (pcbRead==0)
438 pcbRead = &bytesReadBuffer;
441 * Using the known size of the stream, calculate the number of bytes
442 * to read from the block chain
444 bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
447 * Lock the buffer in position and copy the data.
449 supportBuffer = GlobalLock(This->supportHandle);
451 memcpy(pv, (char *) supportBuffer+This->currentPosition.u.LowPart, bytesToReadFromBuffer);
454 * Move the current position to the new position
456 This->currentPosition.u.LowPart+=bytesToReadFromBuffer;
459 * Return the number of bytes read.
461 *pcbRead = bytesToReadFromBuffer;
464 * Cleanup
466 GlobalUnlock(This->supportHandle);
469 * The function returns S_OK if the buffer was filled completely
470 * it returns S_FALSE if the end of the stream is reached before the
471 * buffer is filled
473 if(*pcbRead == cb)
474 return S_OK;
476 return S_FALSE;
479 /***
480 * This method is part of the ISequentialStream interface.
482 * It writes a block of information to the stream at the current
483 * position. It then moves the current position at the end of the
484 * written block. If the stream is too small to fit the block,
485 * the stream is grown to fit.
487 * See the documentation of ISequentialStream for more info.
489 HRESULT WINAPI HGLOBALStreamImpl_Write(
490 IStream* iface,
491 const void* pv, /* [size_is][in] */
492 ULONG cb, /* [in] */
493 ULONG* pcbWritten) /* [out] */
495 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
497 void* supportBuffer;
498 ULARGE_INTEGER newSize;
499 ULONG bytesWritten = 0;
501 TRACE("(%p, %p, %ld, %p)\n", iface,
502 pv, cb, pcbWritten);
505 * If the caller is not interested in the number of bytes written,
506 * we use another buffer to avoid "if" statements in the code.
508 if (pcbWritten == 0)
509 pcbWritten = &bytesWritten;
511 if (cb == 0)
513 return S_OK;
515 else
517 newSize.u.HighPart = 0;
518 newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
522 * Verify if we need to grow the stream
524 if (newSize.u.LowPart > This->streamSize.u.LowPart)
526 /* grow stream */
527 IStream_SetSize(iface, newSize);
531 * Lock the buffer in position and copy the data.
533 supportBuffer = GlobalLock(This->supportHandle);
535 memcpy((char *) supportBuffer+This->currentPosition.u.LowPart, pv, cb);
538 * Move the current position to the new position
540 This->currentPosition.u.LowPart+=cb;
543 * Return the number of bytes read.
545 *pcbWritten = cb;
548 * Cleanup
550 GlobalUnlock(This->supportHandle);
552 return S_OK;
555 /***
556 * This method is part of the IStream interface.
558 * It will move the current stream pointer according to the parameters
559 * given.
561 * See the documentation of IStream for more info.
563 HRESULT WINAPI HGLOBALStreamImpl_Seek(
564 IStream* iface,
565 LARGE_INTEGER dlibMove, /* [in] */
566 DWORD dwOrigin, /* [in] */
567 ULARGE_INTEGER* plibNewPosition) /* [out] */
569 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
571 ULARGE_INTEGER newPosition;
573 TRACE("(%p, %lx%08lx, %ld, %p)\n", iface, dlibMove.u.HighPart,
574 dlibMove.u.LowPart, dwOrigin, plibNewPosition);
577 * The file pointer is moved depending on the given "function"
578 * parameter.
580 switch (dwOrigin)
582 case STREAM_SEEK_SET:
583 newPosition.u.HighPart = 0;
584 newPosition.u.LowPart = 0;
585 break;
586 case STREAM_SEEK_CUR:
587 newPosition = This->currentPosition;
588 break;
589 case STREAM_SEEK_END:
590 newPosition = This->streamSize;
591 break;
592 default:
593 return STG_E_INVALIDFUNCTION;
597 * Move the actual file pointer
598 * If the file pointer ends-up after the end of the stream, the next Write operation will
599 * make the file larger. This is how it is documented.
601 newPosition.QuadPart = RtlLargeIntegerAdd(newPosition.QuadPart, dlibMove.QuadPart);
602 if (newPosition.QuadPart < 0) return STG_E_INVALIDFUNCTION;
604 if (plibNewPosition) *plibNewPosition = newPosition;
605 This->currentPosition = newPosition;
607 return S_OK;
610 /***
611 * This method is part of the IStream interface.
613 * It will change the size of a stream.
615 * TODO: Switch from small blocks to big blocks and vice versa.
617 * See the documentation of IStream for more info.
619 HRESULT WINAPI HGLOBALStreamImpl_SetSize(
620 IStream* iface,
621 ULARGE_INTEGER libNewSize) /* [in] */
623 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
624 HGLOBAL supportHandle;
626 TRACE("(%p, %ld)\n", iface, libNewSize.u.LowPart);
629 * As documented.
631 if (libNewSize.u.HighPart != 0)
632 return STG_E_INVALIDFUNCTION;
634 if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
635 return S_OK;
638 * Re allocate the HGlobal to fit the new size of the stream.
640 supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
642 if (supportHandle == 0)
643 return STG_E_MEDIUMFULL;
645 This->supportHandle = supportHandle;
646 This->streamSize.u.LowPart = libNewSize.u.LowPart;
648 return S_OK;
651 /***
652 * This method is part of the IStream interface.
654 * It will copy the 'cb' Bytes to 'pstm' IStream.
656 * See the documentation of IStream for more info.
658 HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
659 IStream* iface,
660 IStream* pstm, /* [unique][in] */
661 ULARGE_INTEGER cb, /* [in] */
662 ULARGE_INTEGER* pcbRead, /* [out] */
663 ULARGE_INTEGER* pcbWritten) /* [out] */
665 HRESULT hr = S_OK;
666 BYTE tmpBuffer[128];
667 ULONG bytesRead, bytesWritten, copySize;
668 ULARGE_INTEGER totalBytesRead;
669 ULARGE_INTEGER totalBytesWritten;
671 TRACE("(%p, %p, %ld, %p, %p)\n", iface, pstm,
672 cb.u.LowPart, pcbRead, pcbWritten);
675 * Sanity check
677 if ( pstm == 0 )
678 return STG_E_INVALIDPOINTER;
680 totalBytesRead.u.LowPart = totalBytesRead.u.HighPart = 0;
681 totalBytesWritten.u.LowPart = totalBytesWritten.u.HighPart = 0;
684 * use stack to store data temporarly
685 * there is surely more performant way of doing it, for now this basic
686 * implementation will do the job
688 while ( cb.u.LowPart > 0 )
690 if ( cb.u.LowPart >= 128 )
691 copySize = 128;
692 else
693 copySize = cb.u.LowPart;
695 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
697 totalBytesRead.u.LowPart += bytesRead;
699 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
701 totalBytesWritten.u.LowPart += bytesWritten;
704 * Check that read & write operations were succesfull
706 if (bytesRead != bytesWritten)
708 hr = STG_E_MEDIUMFULL;
709 break;
712 if (bytesRead!=copySize)
713 cb.u.LowPart = 0;
714 else
715 cb.u.LowPart -= bytesRead;
719 * Update number of bytes read and written
721 if (pcbRead)
723 pcbRead->u.LowPart = totalBytesRead.u.LowPart;
724 pcbRead->u.HighPart = totalBytesRead.u.HighPart;
727 if (pcbWritten)
729 pcbWritten->u.LowPart = totalBytesWritten.u.LowPart;
730 pcbWritten->u.HighPart = totalBytesWritten.u.HighPart;
732 return hr;
735 /***
736 * This method is part of the IStream interface.
738 * For streams supported by HGLOBALS, this function does nothing.
739 * This is what the documentation tells us.
741 * See the documentation of IStream for more info.
743 HRESULT WINAPI HGLOBALStreamImpl_Commit(
744 IStream* iface,
745 DWORD grfCommitFlags) /* [in] */
747 return S_OK;
750 /***
751 * This method is part of the IStream interface.
753 * For streams supported by HGLOBALS, this function does nothing.
754 * This is what the documentation tells us.
756 * See the documentation of IStream for more info.
758 HRESULT WINAPI HGLOBALStreamImpl_Revert(
759 IStream* iface)
761 return S_OK;
764 /***
765 * This method is part of the IStream interface.
767 * For streams supported by HGLOBALS, this function does nothing.
768 * This is what the documentation tells us.
770 * See the documentation of IStream for more info.
772 HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
773 IStream* iface,
774 ULARGE_INTEGER libOffset, /* [in] */
775 ULARGE_INTEGER cb, /* [in] */
776 DWORD dwLockType) /* [in] */
778 return S_OK;
782 * This method is part of the IStream interface.
784 * For streams supported by HGLOBALS, this function does nothing.
785 * This is what the documentation tells us.
787 * See the documentation of IStream for more info.
789 HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
790 IStream* iface,
791 ULARGE_INTEGER libOffset, /* [in] */
792 ULARGE_INTEGER cb, /* [in] */
793 DWORD dwLockType) /* [in] */
795 return S_OK;
798 /***
799 * This method is part of the IStream interface.
801 * This method returns information about the current
802 * stream.
804 * See the documentation of IStream for more info.
806 HRESULT WINAPI HGLOBALStreamImpl_Stat(
807 IStream* iface,
808 STATSTG* pstatstg, /* [out] */
809 DWORD grfStatFlag) /* [in] */
811 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
813 memset(pstatstg, 0, sizeof(STATSTG));
815 pstatstg->pwcsName = NULL;
816 pstatstg->type = STGTY_STREAM;
817 pstatstg->cbSize = This->streamSize;
819 return S_OK;
822 HRESULT WINAPI HGLOBALStreamImpl_Clone(
823 IStream* iface,
824 IStream** ppstm) /* [out] */
826 ULARGE_INTEGER dummy;
827 LARGE_INTEGER offset;
828 HRESULT hr;
829 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
830 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface,This->deleteOnRelease,(long)This->currentPosition.QuadPart);
831 hr=CreateStreamOnHGlobal(This->supportHandle, FALSE, ppstm);
832 if(FAILED(hr))
833 return hr;
834 offset.QuadPart=(LONGLONG)This->currentPosition.QuadPart;
835 HGLOBALStreamImpl_Seek(*ppstm,offset,STREAM_SEEK_SET,&dummy);
836 return S_OK;