Trace the high part of the Seek offset.
[wine/multimedia.git] / dlls / ole32 / hglobalstream.c
blobf450a9da34ddb882d2942c1729956e1f78119e61
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 ICOM_VFIELD(IStream); /* 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 ICOM_VTABLE(IStream) HGLOBALStreamImpl_Vtbl =
174 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
175 HGLOBALStreamImpl_QueryInterface,
176 HGLOBALStreamImpl_AddRef,
177 HGLOBALStreamImpl_Release,
178 HGLOBALStreamImpl_Read,
179 HGLOBALStreamImpl_Write,
180 HGLOBALStreamImpl_Seek,
181 HGLOBALStreamImpl_SetSize,
182 HGLOBALStreamImpl_CopyTo,
183 HGLOBALStreamImpl_Commit,
184 HGLOBALStreamImpl_Revert,
185 HGLOBALStreamImpl_LockRegion,
186 HGLOBALStreamImpl_UnlockRegion,
187 HGLOBALStreamImpl_Stat,
188 HGLOBALStreamImpl_Clone
191 /***********************************************************************
192 * CreateStreamOnHGlobal [OLE32.@]
194 HRESULT WINAPI CreateStreamOnHGlobal(
195 HGLOBAL hGlobal,
196 BOOL fDeleteOnRelease,
197 LPSTREAM* ppstm)
199 HGLOBALStreamImpl* newStream;
201 newStream = HGLOBALStreamImpl_Construct(hGlobal,
202 fDeleteOnRelease);
204 if (newStream!=NULL)
206 return IUnknown_QueryInterface((IUnknown*)newStream,
207 &IID_IStream,
208 (void**)ppstm);
211 return E_OUTOFMEMORY;
214 /***********************************************************************
215 * GetHGlobalFromStream [OLE32.@]
217 HRESULT WINAPI GetHGlobalFromStream(IStream* pstm, HGLOBAL* phglobal)
219 HGLOBALStreamImpl* pStream;
221 if (pstm == NULL)
222 return E_INVALIDARG;
224 pStream = (HGLOBALStreamImpl*) pstm;
227 * Verify that the stream object was created with CreateStreamOnHGlobal.
229 if (pStream->lpVtbl == &HGLOBALStreamImpl_Vtbl)
230 *phglobal = pStream->supportHandle;
231 else
233 *phglobal = 0;
234 return E_INVALIDARG;
237 return S_OK;
240 /******************************************************************************
241 ** HGLOBALStreamImpl implementation
244 /***
245 * This is the constructor for the HGLOBALStreamImpl class.
247 * Params:
248 * hGlobal - Handle that will support the stream. can be NULL.
249 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
250 * when the IStream object is destroyed.
252 HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
253 HGLOBAL hGlobal,
254 BOOL fDeleteOnRelease)
256 HGLOBALStreamImpl* newStream;
258 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl));
260 if (newStream!=0)
263 * Set-up the virtual function table and reference count.
265 newStream->lpVtbl = &HGLOBALStreamImpl_Vtbl;
266 newStream->ref = 0;
269 * Initialize the support.
271 newStream->supportHandle = hGlobal;
272 newStream->deleteOnRelease = fDeleteOnRelease;
275 * This method will allocate a handle if one is not supplied.
277 if (!newStream->supportHandle)
279 newStream->supportHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD |
280 GMEM_SHARE, 0);
284 * Start the stream at the beginning.
286 newStream->currentPosition.s.HighPart = 0;
287 newStream->currentPosition.s.LowPart = 0;
290 * Initialize the size of the stream to the size of the handle.
292 newStream->streamSize.s.HighPart = 0;
293 newStream->streamSize.s.LowPart = GlobalSize(newStream->supportHandle);
296 return newStream;
299 /***
300 * This is the destructor of the HGLOBALStreamImpl class.
302 * This method will clean-up all the resources used-up by the given HGLOBALStreamImpl
303 * class. The pointer passed-in to this function will be freed and will not
304 * be valid anymore.
306 void HGLOBALStreamImpl_Destroy(HGLOBALStreamImpl* This)
308 TRACE("(%p)\n", This);
311 * Release the HGlobal if the constructor asked for that.
313 if (This->deleteOnRelease)
315 GlobalFree(This->supportHandle);
316 This->supportHandle=0;
320 * Finally, free the memory used-up by the class.
322 HeapFree(GetProcessHeap(), 0, This);
325 /***
326 * This implements the IUnknown method QueryInterface for this
327 * class
329 HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
330 IStream* iface,
331 REFIID riid, /* [in] */
332 void** ppvObject) /* [iid_is][out] */
334 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
337 * Perform a sanity check on the parameters.
339 if (ppvObject==0)
340 return E_INVALIDARG;
343 * Initialize the return parameter.
345 *ppvObject = 0;
348 * Compare the riid with the interface IDs implemented by this object.
350 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
352 *ppvObject = (IStream*)This;
354 else if (memcmp(&IID_IStream, riid, sizeof(IID_IStream)) == 0)
356 *ppvObject = (IStream*)This;
360 * Check that we obtained an interface.
362 if ((*ppvObject)==0)
363 return E_NOINTERFACE;
366 * Query Interface always increases the reference count by one when it is
367 * successful
369 HGLOBALStreamImpl_AddRef(iface);
371 return S_OK;
374 /***
375 * This implements the IUnknown method AddRef for this
376 * class
378 ULONG WINAPI HGLOBALStreamImpl_AddRef(
379 IStream* iface)
381 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
383 This->ref++;
385 return This->ref;
388 /***
389 * This implements the IUnknown method Release for this
390 * class
392 ULONG WINAPI HGLOBALStreamImpl_Release(
393 IStream* iface)
395 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
397 ULONG newRef;
399 This->ref--;
401 newRef = This->ref;
404 * If the reference count goes down to 0, perform suicide.
406 if (newRef==0)
408 HGLOBALStreamImpl_Destroy(This);
411 return newRef;
414 /***
415 * This method is part of the ISequentialStream interface.
417 * If reads a block of information from the stream at the current
418 * position. It then moves the current position at the end of the
419 * read block
421 * See the documentation of ISequentialStream for more info.
423 HRESULT WINAPI HGLOBALStreamImpl_Read(
424 IStream* iface,
425 void* pv, /* [length_is][size_is][out] */
426 ULONG cb, /* [in] */
427 ULONG* pcbRead) /* [out] */
429 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
431 void* supportBuffer;
432 ULONG bytesReadBuffer;
433 ULONG bytesToReadFromBuffer;
435 TRACE("(%p, %p, %ld, %p)\n", iface,
436 pv, cb, pcbRead);
439 * If the caller is not interested in the nubmer of bytes read,
440 * we use another buffer to avoid "if" statements in the code.
442 if (pcbRead==0)
443 pcbRead = &bytesReadBuffer;
446 * Using the known size of the stream, calculate the number of bytes
447 * to read from the block chain
449 bytesToReadFromBuffer = min( This->streamSize.s.LowPart - This->currentPosition.s.LowPart, cb);
452 * Lock the buffer in position and copy the data.
454 supportBuffer = GlobalLock(This->supportHandle);
456 memcpy(pv, (char *) supportBuffer+This->currentPosition.s.LowPart, bytesToReadFromBuffer);
459 * Move the current position to the new position
461 This->currentPosition.s.LowPart+=bytesToReadFromBuffer;
464 * Return the number of bytes read.
466 *pcbRead = bytesToReadFromBuffer;
469 * Cleanup
471 GlobalUnlock(This->supportHandle);
474 * The function returns S_OK if the buffer was filled completely
475 * it returns S_FALSE if the end of the stream is reached before the
476 * buffer is filled
478 if(*pcbRead == cb)
479 return S_OK;
481 return S_FALSE;
484 /***
485 * This method is part of the ISequentialStream interface.
487 * It writes a block of information to the stream at the current
488 * position. It then moves the current position at the end of the
489 * written block. If the stream is too small to fit the block,
490 * the stream is grown to fit.
492 * See the documentation of ISequentialStream for more info.
494 HRESULT WINAPI HGLOBALStreamImpl_Write(
495 IStream* iface,
496 const void* pv, /* [size_is][in] */
497 ULONG cb, /* [in] */
498 ULONG* pcbWritten) /* [out] */
500 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
502 void* supportBuffer;
503 ULARGE_INTEGER newSize;
504 ULONG bytesWritten = 0;
506 TRACE("(%p, %p, %ld, %p)\n", iface,
507 pv, cb, pcbWritten);
510 * If the caller is not interested in the number of bytes written,
511 * we use another buffer to avoid "if" statements in the code.
513 if (pcbWritten == 0)
514 pcbWritten = &bytesWritten;
516 if (cb == 0)
518 return S_OK;
520 else
522 newSize.s.HighPart = 0;
523 newSize.s.LowPart = This->currentPosition.s.LowPart + cb;
527 * Verify if we need to grow the stream
529 if (newSize.s.LowPart > This->streamSize.s.LowPart)
531 /* grow stream */
532 IStream_SetSize(iface, newSize);
536 * Lock the buffer in position and copy the data.
538 supportBuffer = GlobalLock(This->supportHandle);
540 memcpy((char *) supportBuffer+This->currentPosition.s.LowPart, pv, cb);
543 * Move the current position to the new position
545 This->currentPosition.s.LowPart+=cb;
548 * Return the number of bytes read.
550 *pcbWritten = cb;
553 * Cleanup
555 GlobalUnlock(This->supportHandle);
557 return S_OK;
560 /***
561 * This method is part of the IStream interface.
563 * It will move the current stream pointer according to the parameters
564 * given.
566 * See the documentation of IStream for more info.
568 HRESULT WINAPI HGLOBALStreamImpl_Seek(
569 IStream* iface,
570 LARGE_INTEGER dlibMove, /* [in] */
571 DWORD dwOrigin, /* [in] */
572 ULARGE_INTEGER* plibNewPosition) /* [out] */
574 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
576 ULARGE_INTEGER newPosition;
578 TRACE("(%p, %lx%08lx, %ld, %p)\n", iface, dlibMove.s.HighPart,
579 dlibMove.s.LowPart, dwOrigin, plibNewPosition);
582 * The file pointer is moved depending on the given "function"
583 * parameter.
585 switch (dwOrigin)
587 case STREAM_SEEK_SET:
588 newPosition.s.HighPart = 0;
589 newPosition.s.LowPart = 0;
590 break;
591 case STREAM_SEEK_CUR:
592 newPosition = This->currentPosition;
593 break;
594 case STREAM_SEEK_END:
595 newPosition = This->streamSize;
596 break;
597 default:
598 return STG_E_INVALIDFUNCTION;
602 * Move the actual file pointer
603 * If the file pointer ends-up after the end of the stream, the next Write operation will
604 * make the file larger. This is how it is documented.
606 newPosition.QuadPart = RtlLargeIntegerAdd(newPosition.QuadPart, dlibMove.QuadPart);
607 if (newPosition.QuadPart < 0) return STG_E_INVALIDFUNCTION;
609 if (plibNewPosition) *plibNewPosition = newPosition;
610 This->currentPosition = newPosition;
612 return S_OK;
615 /***
616 * This method is part of the IStream interface.
618 * It will change the size of a stream.
620 * TODO: Switch from small blocks to big blocks and vice versa.
622 * See the documentation of IStream for more info.
624 HRESULT WINAPI HGLOBALStreamImpl_SetSize(
625 IStream* iface,
626 ULARGE_INTEGER libNewSize) /* [in] */
628 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
629 HGLOBAL supportHandle;
631 TRACE("(%p, %ld)\n", iface, libNewSize.s.LowPart);
634 * As documented.
636 if (libNewSize.s.HighPart != 0)
637 return STG_E_INVALIDFUNCTION;
639 if (This->streamSize.s.LowPart == libNewSize.s.LowPart)
640 return S_OK;
643 * Re allocate the HGlobal to fit the new size of the stream.
645 supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.s.LowPart, 0);
647 if (supportHandle == 0)
648 return STG_E_MEDIUMFULL;
650 This->supportHandle = supportHandle;
651 This->streamSize.s.LowPart = libNewSize.s.LowPart;
653 return S_OK;
656 /***
657 * This method is part of the IStream interface.
659 * It will copy the 'cb' Bytes to 'pstm' IStream.
661 * See the documentation of IStream for more info.
663 HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
664 IStream* iface,
665 IStream* pstm, /* [unique][in] */
666 ULARGE_INTEGER cb, /* [in] */
667 ULARGE_INTEGER* pcbRead, /* [out] */
668 ULARGE_INTEGER* pcbWritten) /* [out] */
670 HRESULT hr = S_OK;
671 BYTE tmpBuffer[128];
672 ULONG bytesRead, bytesWritten, copySize;
673 ULARGE_INTEGER totalBytesRead;
674 ULARGE_INTEGER totalBytesWritten;
676 TRACE("(%p, %p, %ld, %p, %p)\n", iface, pstm,
677 cb.s.LowPart, pcbRead, pcbWritten);
680 * Sanity check
682 if ( pstm == 0 )
683 return STG_E_INVALIDPOINTER;
685 totalBytesRead.s.LowPart = totalBytesRead.s.HighPart = 0;
686 totalBytesWritten.s.LowPart = totalBytesWritten.s.HighPart = 0;
689 * use stack to store data temporarly
690 * there is surely more performant way of doing it, for now this basic
691 * implementation will do the job
693 while ( cb.s.LowPart > 0 )
695 if ( cb.s.LowPart >= 128 )
696 copySize = 128;
697 else
698 copySize = cb.s.LowPart;
700 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
702 totalBytesRead.s.LowPart += bytesRead;
704 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
706 totalBytesWritten.s.LowPart += bytesWritten;
709 * Check that read & write operations were succesfull
711 if (bytesRead != bytesWritten)
713 hr = STG_E_MEDIUMFULL;
714 break;
717 if (bytesRead!=copySize)
718 cb.s.LowPart = 0;
719 else
720 cb.s.LowPart -= bytesRead;
724 * Update number of bytes read and written
726 if (pcbRead)
728 pcbRead->s.LowPart = totalBytesRead.s.LowPart;
729 pcbRead->s.HighPart = totalBytesRead.s.HighPart;
732 if (pcbWritten)
734 pcbWritten->s.LowPart = totalBytesWritten.s.LowPart;
735 pcbWritten->s.HighPart = totalBytesWritten.s.HighPart;
737 return hr;
740 /***
741 * This method is part of the IStream interface.
743 * For streams supported by HGLOBALS, this function does nothing.
744 * This is what the documentation tells us.
746 * See the documentation of IStream for more info.
748 HRESULT WINAPI HGLOBALStreamImpl_Commit(
749 IStream* iface,
750 DWORD grfCommitFlags) /* [in] */
752 return S_OK;
755 /***
756 * This method is part of the IStream interface.
758 * For streams supported by HGLOBALS, this function does nothing.
759 * This is what the documentation tells us.
761 * See the documentation of IStream for more info.
763 HRESULT WINAPI HGLOBALStreamImpl_Revert(
764 IStream* iface)
766 return S_OK;
769 /***
770 * This method is part of the IStream interface.
772 * For streams supported by HGLOBALS, this function does nothing.
773 * This is what the documentation tells us.
775 * See the documentation of IStream for more info.
777 HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
778 IStream* iface,
779 ULARGE_INTEGER libOffset, /* [in] */
780 ULARGE_INTEGER cb, /* [in] */
781 DWORD dwLockType) /* [in] */
783 return S_OK;
787 * This method is part of the IStream interface.
789 * For streams supported by HGLOBALS, this function does nothing.
790 * This is what the documentation tells us.
792 * See the documentation of IStream for more info.
794 HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
795 IStream* iface,
796 ULARGE_INTEGER libOffset, /* [in] */
797 ULARGE_INTEGER cb, /* [in] */
798 DWORD dwLockType) /* [in] */
800 return S_OK;
803 /***
804 * This method is part of the IStream interface.
806 * This method returns information about the current
807 * stream.
809 * See the documentation of IStream for more info.
811 HRESULT WINAPI HGLOBALStreamImpl_Stat(
812 IStream* iface,
813 STATSTG* pstatstg, /* [out] */
814 DWORD grfStatFlag) /* [in] */
816 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
818 memset(pstatstg, 0, sizeof(STATSTG));
820 pstatstg->pwcsName = NULL;
821 pstatstg->type = STGTY_STREAM;
822 pstatstg->cbSize = This->streamSize;
824 return S_OK;
827 HRESULT WINAPI HGLOBALStreamImpl_Clone(
828 IStream* iface,
829 IStream** ppstm) /* [out] */
831 ULARGE_INTEGER dummy;
832 LARGE_INTEGER offset;
833 HRESULT hr;
834 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
835 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface,This->deleteOnRelease,(long)This->currentPosition.QuadPart);
836 hr=CreateStreamOnHGlobal(This->supportHandle, FALSE, ppstm);
837 if(FAILED(hr))
838 return hr;
839 offset.QuadPart=(LONGLONG)This->currentPosition.QuadPart;
840 HGLOBALStreamImpl_Seek(*ppstm,offset,STREAM_SEEK_SET,&dummy);
841 return S_OK;