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
31 #define NONAMELESSUNION
32 #define NONAMELESSSTRUCT
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(storage);
44 /****************************************************************************
45 * HGLOBALStreamImpl definition.
47 * This class imlements the IStream inteface and represents a stream
48 * supported by an HGLOBAL pointer.
50 struct HGLOBALStreamImpl
52 ICOM_VFIELD(IStream); /* Needs to be the first item in the stuct
53 * since we want to cast this in a IStream pointer */
61 * Support for the stream
63 HGLOBAL supportHandle;
66 * This flag is TRUE if the HGLOBAL is destroyed when the stream
67 * is finally released.
72 * Helper variable that contains the size of the stream
74 ULARGE_INTEGER streamSize;
77 * This is the current position of the cursor in the stream
79 ULARGE_INTEGER currentPosition;
82 typedef struct HGLOBALStreamImpl HGLOBALStreamImpl;
85 * Method definition for the StgStreamImpl class.
87 HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
89 BOOL fDeleteOnRelease);
91 void HGLOBALStreamImpl_Destroy(
92 HGLOBALStreamImpl* This);
94 void HGLOBALStreamImpl_OpenBlockChain(
95 HGLOBALStreamImpl* This);
97 HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
99 REFIID riid, /* [in] */
100 void** ppvObject); /* [iid_is][out] */
102 ULONG WINAPI HGLOBALStreamImpl_AddRef(
105 ULONG WINAPI HGLOBALStreamImpl_Release(
108 HRESULT WINAPI HGLOBALStreamImpl_Read(
110 void* pv, /* [length_is][size_is][out] */
112 ULONG* pcbRead); /* [out] */
114 HRESULT WINAPI HGLOBALStreamImpl_Write(
116 const void* pv, /* [size_is][in] */
118 ULONG* pcbWritten); /* [out] */
120 HRESULT WINAPI HGLOBALStreamImpl_Seek(
122 LARGE_INTEGER dlibMove, /* [in] */
123 DWORD dwOrigin, /* [in] */
124 ULARGE_INTEGER* plibNewPosition); /* [out] */
126 HRESULT WINAPI HGLOBALStreamImpl_SetSize(
128 ULARGE_INTEGER libNewSize); /* [in] */
130 HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
132 IStream* pstm, /* [unique][in] */
133 ULARGE_INTEGER cb, /* [in] */
134 ULARGE_INTEGER* pcbRead, /* [out] */
135 ULARGE_INTEGER* pcbWritten); /* [out] */
137 HRESULT WINAPI HGLOBALStreamImpl_Commit(
139 DWORD grfCommitFlags); /* [in] */
141 HRESULT WINAPI HGLOBALStreamImpl_Revert(
144 HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
146 ULARGE_INTEGER libOffset, /* [in] */
147 ULARGE_INTEGER cb, /* [in] */
148 DWORD dwLockType); /* [in] */
150 HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
152 ULARGE_INTEGER libOffset, /* [in] */
153 ULARGE_INTEGER cb, /* [in] */
154 DWORD dwLockType); /* [in] */
156 HRESULT WINAPI HGLOBALStreamImpl_Stat(
158 STATSTG* pstatstg, /* [out] */
159 DWORD grfStatFlag); /* [in] */
161 HRESULT WINAPI HGLOBALStreamImpl_Clone(
163 IStream** ppstm); /* [out] */
167 * Virtual function table for the HGLOBALStreamImpl class.
169 static ICOM_VTABLE(IStream) HGLOBALStreamImpl_Vtbl =
171 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
172 HGLOBALStreamImpl_QueryInterface,
173 HGLOBALStreamImpl_AddRef,
174 HGLOBALStreamImpl_Release,
175 HGLOBALStreamImpl_Read,
176 HGLOBALStreamImpl_Write,
177 HGLOBALStreamImpl_Seek,
178 HGLOBALStreamImpl_SetSize,
179 HGLOBALStreamImpl_CopyTo,
180 HGLOBALStreamImpl_Commit,
181 HGLOBALStreamImpl_Revert,
182 HGLOBALStreamImpl_LockRegion,
183 HGLOBALStreamImpl_UnlockRegion,
184 HGLOBALStreamImpl_Stat,
185 HGLOBALStreamImpl_Clone
188 /***********************************************************************
189 * CreateStreamOnHGlobal [OLE32.61]
191 HRESULT WINAPI CreateStreamOnHGlobal(
193 BOOL fDeleteOnRelease,
196 HGLOBALStreamImpl* newStream;
198 newStream = HGLOBALStreamImpl_Construct(hGlobal,
203 return IUnknown_QueryInterface((IUnknown*)newStream,
208 return E_OUTOFMEMORY;
211 /***********************************************************************
212 * GetHGlobalFromStream [OLE32.71]
214 HRESULT WINAPI GetHGlobalFromStream(IStream* pstm, HGLOBAL* phglobal)
216 HGLOBALStreamImpl* pStream;
221 pStream = (HGLOBALStreamImpl*) pstm;
224 * Verify that the stream object was created with CreateStreamOnHGlobal.
226 if (ICOM_VTBL(pStream) == &HGLOBALStreamImpl_Vtbl)
227 *phglobal = pStream->supportHandle;
237 /******************************************************************************
238 ** HGLOBALStreamImpl implementation
242 * This is the constructor for the HGLOBALStreamImpl class.
245 * hGlobal - Handle that will support the stream. can be NULL.
246 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
247 * when the IStream object is destroyed.
249 HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
251 BOOL fDeleteOnRelease)
253 HGLOBALStreamImpl* newStream;
255 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl));
260 * Set-up the virtual function table and reference count.
262 ICOM_VTBL(newStream) = &HGLOBALStreamImpl_Vtbl;
266 * Initialize the support.
268 newStream->supportHandle = hGlobal;
269 newStream->deleteOnRelease = fDeleteOnRelease;
272 * This method will allocate a handle if one is not supplied.
274 if (!newStream->supportHandle)
276 newStream->supportHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD |
281 * Start the stream at the beginning.
283 newStream->currentPosition.s.HighPart = 0;
284 newStream->currentPosition.s.LowPart = 0;
287 * Initialize the size of the stream to the size of the handle.
289 newStream->streamSize.s.HighPart = 0;
290 newStream->streamSize.s.LowPart = GlobalSize(newStream->supportHandle);
297 * This is the destructor of the HGLOBALStreamImpl class.
299 * This method will clean-up all the resources used-up by the given HGLOBALStreamImpl
300 * class. The pointer passed-in to this function will be freed and will not
303 void HGLOBALStreamImpl_Destroy(HGLOBALStreamImpl* This)
305 TRACE("(%p)\n", This);
308 * Release the HGlobal if the constructor asked for that.
310 if (This->deleteOnRelease)
312 GlobalFree(This->supportHandle);
313 This->supportHandle=0;
317 * Finally, free the memory used-up by the class.
319 HeapFree(GetProcessHeap(), 0, This);
323 * This implements the IUnknown method QueryInterface for this
326 HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
328 REFIID riid, /* [in] */
329 void** ppvObject) /* [iid_is][out] */
331 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
334 * Perform a sanity check on the parameters.
340 * Initialize the return parameter.
345 * Compare the riid with the interface IDs implemented by this object.
347 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
349 *ppvObject = (IStream*)This;
351 else if (memcmp(&IID_IStream, riid, sizeof(IID_IStream)) == 0)
353 *ppvObject = (IStream*)This;
357 * Check that we obtained an interface.
360 return E_NOINTERFACE;
363 * Query Interface always increases the reference count by one when it is
366 HGLOBALStreamImpl_AddRef(iface);
372 * This implements the IUnknown method AddRef for this
375 ULONG WINAPI HGLOBALStreamImpl_AddRef(
378 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
386 * This implements the IUnknown method Release for this
389 ULONG WINAPI HGLOBALStreamImpl_Release(
392 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
401 * If the reference count goes down to 0, perform suicide.
405 HGLOBALStreamImpl_Destroy(This);
412 * This method is part of the ISequentialStream interface.
414 * If reads a block of information from the stream at the current
415 * position. It then moves the current position at the end of the
418 * See the documentation of ISequentialStream for more info.
420 HRESULT WINAPI HGLOBALStreamImpl_Read(
422 void* pv, /* [length_is][size_is][out] */
424 ULONG* pcbRead) /* [out] */
426 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
429 ULONG bytesReadBuffer;
430 ULONG bytesToReadFromBuffer;
432 TRACE("(%p, %p, %ld, %p)\n", iface,
436 * If the caller is not interested in the nubmer of bytes read,
437 * we use another buffer to avoid "if" statements in the code.
440 pcbRead = &bytesReadBuffer;
443 * Using the known size of the stream, calculate the number of bytes
444 * to read from the block chain
446 bytesToReadFromBuffer = min( This->streamSize.s.LowPart - This->currentPosition.s.LowPart, cb);
449 * Lock the buffer in position and copy the data.
451 supportBuffer = GlobalLock(This->supportHandle);
453 memcpy(pv, (char *) supportBuffer+This->currentPosition.s.LowPart, bytesToReadFromBuffer);
456 * Move the current position to the new position
458 This->currentPosition.s.LowPart+=bytesToReadFromBuffer;
461 * Return the number of bytes read.
463 *pcbRead = bytesToReadFromBuffer;
468 GlobalUnlock(This->supportHandle);
471 * The function returns S_OK if the buffer was filled completely
472 * it returns S_FALSE if the end of the stream is reached before the
482 * This method is part of the ISequentialStream interface.
484 * It writes a block of information to the stream at the current
485 * position. It then moves the current position at the end of the
486 * written block. If the stream is too small to fit the block,
487 * the stream is grown to fit.
489 * See the documentation of ISequentialStream for more info.
491 HRESULT WINAPI HGLOBALStreamImpl_Write(
493 const void* pv, /* [size_is][in] */
495 ULONG* pcbWritten) /* [out] */
497 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
500 ULARGE_INTEGER newSize;
501 ULONG bytesWritten = 0;
503 TRACE("(%p, %p, %ld, %p)\n", iface,
507 * If the caller is not interested in the number of bytes written,
508 * we use another buffer to avoid "if" statements in the code.
511 pcbWritten = &bytesWritten;
519 newSize.s.HighPart = 0;
520 newSize.s.LowPart = This->currentPosition.s.LowPart + cb;
524 * Verify if we need to grow the stream
526 if (newSize.s.LowPart > This->streamSize.s.LowPart)
529 IStream_SetSize(iface, newSize);
533 * Lock the buffer in position and copy the data.
535 supportBuffer = GlobalLock(This->supportHandle);
537 memcpy((char *) supportBuffer+This->currentPosition.s.LowPart, pv, cb);
540 * Move the current position to the new position
542 This->currentPosition.s.LowPart+=cb;
545 * Return the number of bytes read.
552 GlobalUnlock(This->supportHandle);
558 * This method is part of the IStream interface.
560 * It will move the current stream pointer according to the parameters
563 * See the documentation of IStream for more info.
565 HRESULT WINAPI HGLOBALStreamImpl_Seek(
567 LARGE_INTEGER dlibMove, /* [in] */
568 DWORD dwOrigin, /* [in] */
569 ULARGE_INTEGER* plibNewPosition) /* [out] */
571 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
573 ULARGE_INTEGER newPosition;
575 TRACE("(%p, %ld, %ld, %p)\n", iface,
576 dlibMove.s.LowPart, dwOrigin, plibNewPosition);
579 * The file pointer is moved depending on the given "function"
584 case STREAM_SEEK_SET:
585 newPosition.s.HighPart = 0;
586 newPosition.s.LowPart = 0;
588 case STREAM_SEEK_CUR:
589 newPosition = This->currentPosition;
591 case STREAM_SEEK_END:
592 newPosition = This->streamSize;
595 return STG_E_INVALIDFUNCTION;
599 * Move the actual file pointer
600 * If the file pointer ends-up after the end of the stream, the next Write operation will
601 * make the file larger. This is how it is documented.
603 newPosition.QuadPart = RtlLargeIntegerAdd(newPosition.QuadPart, dlibMove.QuadPart);
604 if (newPosition.QuadPart < 0) return STG_E_INVALIDFUNCTION;
606 if (plibNewPosition) *plibNewPosition = newPosition;
607 This->currentPosition = newPosition;
613 * This method is part of the IStream interface.
615 * It will change the size of a stream.
617 * TODO: Switch from small blocks to big blocks and vice versa.
619 * See the documentation of IStream for more info.
621 HRESULT WINAPI HGLOBALStreamImpl_SetSize(
623 ULARGE_INTEGER libNewSize) /* [in] */
625 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
627 TRACE("(%p, %ld)\n", iface, libNewSize.s.LowPart);
632 if (libNewSize.s.HighPart != 0)
633 return STG_E_INVALIDFUNCTION;
635 if (This->streamSize.s.LowPart == libNewSize.s.LowPart)
639 * Re allocate the HGlobal to fit the new size of the stream.
641 This->supportHandle = GlobalReAlloc(This->supportHandle,
642 libNewSize.s.LowPart,
645 This->streamSize.s.LowPart = libNewSize.s.LowPart;
651 * This method is part of the IStream interface.
653 * It will copy the 'cb' Bytes to 'pstm' IStream.
655 * See the documentation of IStream for more info.
657 HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
659 IStream* pstm, /* [unique][in] */
660 ULARGE_INTEGER cb, /* [in] */
661 ULARGE_INTEGER* pcbRead, /* [out] */
662 ULARGE_INTEGER* pcbWritten) /* [out] */
666 ULONG bytesRead, bytesWritten, copySize;
667 ULARGE_INTEGER totalBytesRead;
668 ULARGE_INTEGER totalBytesWritten;
670 TRACE("(%p, %p, %ld, %p, %p)\n", iface, pstm,
671 cb.s.LowPart, pcbRead, pcbWritten);
677 return STG_E_INVALIDPOINTER;
679 totalBytesRead.s.LowPart = totalBytesRead.s.HighPart = 0;
680 totalBytesWritten.s.LowPart = totalBytesWritten.s.HighPart = 0;
683 * use stack to store data temporarly
684 * there is surely more performant way of doing it, for now this basic
685 * implementation will do the job
687 while ( cb.s.LowPart > 0 )
689 if ( cb.s.LowPart >= 128 )
692 copySize = cb.s.LowPart;
694 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
696 totalBytesRead.s.LowPart += bytesRead;
698 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
700 totalBytesWritten.s.LowPart += bytesWritten;
703 * Check that read & write operations were succesfull
705 if (bytesRead != bytesWritten)
707 hr = STG_E_MEDIUMFULL;
711 if (bytesRead!=copySize)
714 cb.s.LowPart -= bytesRead;
718 * Update number of bytes read and written
722 pcbRead->s.LowPart = totalBytesRead.s.LowPart;
723 pcbRead->s.HighPart = totalBytesRead.s.HighPart;
728 pcbWritten->s.LowPart = totalBytesWritten.s.LowPart;
729 pcbWritten->s.HighPart = totalBytesWritten.s.HighPart;
735 * This method is part of the IStream interface.
737 * For streams supported by HGLOBALS, this function does nothing.
738 * This is what the documentation tells us.
740 * See the documentation of IStream for more info.
742 HRESULT WINAPI HGLOBALStreamImpl_Commit(
744 DWORD grfCommitFlags) /* [in] */
750 * This method is part of the IStream interface.
752 * For streams supported by HGLOBALS, this function does nothing.
753 * This is what the documentation tells us.
755 * See the documentation of IStream for more info.
757 HRESULT WINAPI HGLOBALStreamImpl_Revert(
764 * This method is part of the IStream interface.
766 * For streams supported by HGLOBALS, this function does nothing.
767 * This is what the documentation tells us.
769 * See the documentation of IStream for more info.
771 HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
773 ULARGE_INTEGER libOffset, /* [in] */
774 ULARGE_INTEGER cb, /* [in] */
775 DWORD dwLockType) /* [in] */
781 * This method is part of the IStream interface.
783 * For streams supported by HGLOBALS, this function does nothing.
784 * This is what the documentation tells us.
786 * See the documentation of IStream for more info.
788 HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
790 ULARGE_INTEGER libOffset, /* [in] */
791 ULARGE_INTEGER cb, /* [in] */
792 DWORD dwLockType) /* [in] */
798 * This method is part of the IStream interface.
800 * This method returns information about the current
803 * See the documentation of IStream for more info.
805 HRESULT WINAPI HGLOBALStreamImpl_Stat(
807 STATSTG* pstatstg, /* [out] */
808 DWORD grfStatFlag) /* [in] */
810 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
812 memset(pstatstg, 0, sizeof(STATSTG));
814 pstatstg->pwcsName = NULL;
815 pstatstg->type = STGTY_STREAM;
816 pstatstg->cbSize = This->streamSize;
821 HRESULT WINAPI HGLOBALStreamImpl_Clone(
823 IStream** ppstm) /* [out] */
825 ULARGE_INTEGER dummy;
826 LARGE_INTEGER offset;
828 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
829 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface,This->deleteOnRelease,(long)This->currentPosition.QuadPart);
830 hr=CreateStreamOnHGlobal(This->supportHandle, FALSE, ppstm);
833 offset.QuadPart=(LONGLONG)This->currentPosition.QuadPart;
834 HGLOBALStreamImpl_Seek(*ppstm,offset,STREAM_SEEK_SET,&dummy);