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
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(storage);
46 /****************************************************************************
47 * HGLOBALStreamImpl definition.
49 * This class imlements the IStream inteface and represents a stream
50 * supported by an HGLOBAL pointer.
52 struct HGLOBALStreamImpl
54 ICOM_VFIELD(IStream); /* Needs to be the first item in the stuct
55 * since we want to cast this in a IStream pointer */
63 * Support for the stream
65 HGLOBAL supportHandle;
68 * This flag is TRUE if the HGLOBAL is destroyed when the stream
69 * is finally released.
74 * Helper variable that contains the size of the stream
76 ULARGE_INTEGER streamSize;
79 * This is the current position of the cursor in the stream
81 ULARGE_INTEGER currentPosition;
84 typedef struct HGLOBALStreamImpl HGLOBALStreamImpl;
87 * Method definition for the StgStreamImpl class.
89 HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
91 BOOL fDeleteOnRelease);
93 void HGLOBALStreamImpl_Destroy(
94 HGLOBALStreamImpl* This);
96 void HGLOBALStreamImpl_OpenBlockChain(
97 HGLOBALStreamImpl* This);
99 HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
101 REFIID riid, /* [in] */
102 void** ppvObject); /* [iid_is][out] */
104 ULONG WINAPI HGLOBALStreamImpl_AddRef(
107 ULONG WINAPI HGLOBALStreamImpl_Release(
110 HRESULT WINAPI HGLOBALStreamImpl_Read(
112 void* pv, /* [length_is][size_is][out] */
114 ULONG* pcbRead); /* [out] */
116 HRESULT WINAPI HGLOBALStreamImpl_Write(
118 const void* pv, /* [size_is][in] */
120 ULONG* pcbWritten); /* [out] */
122 HRESULT WINAPI HGLOBALStreamImpl_Seek(
124 LARGE_INTEGER dlibMove, /* [in] */
125 DWORD dwOrigin, /* [in] */
126 ULARGE_INTEGER* plibNewPosition); /* [out] */
128 HRESULT WINAPI HGLOBALStreamImpl_SetSize(
130 ULARGE_INTEGER libNewSize); /* [in] */
132 HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
134 IStream* pstm, /* [unique][in] */
135 ULARGE_INTEGER cb, /* [in] */
136 ULARGE_INTEGER* pcbRead, /* [out] */
137 ULARGE_INTEGER* pcbWritten); /* [out] */
139 HRESULT WINAPI HGLOBALStreamImpl_Commit(
141 DWORD grfCommitFlags); /* [in] */
143 HRESULT WINAPI HGLOBALStreamImpl_Revert(
146 HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
148 ULARGE_INTEGER libOffset, /* [in] */
149 ULARGE_INTEGER cb, /* [in] */
150 DWORD dwLockType); /* [in] */
152 HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
154 ULARGE_INTEGER libOffset, /* [in] */
155 ULARGE_INTEGER cb, /* [in] */
156 DWORD dwLockType); /* [in] */
158 HRESULT WINAPI HGLOBALStreamImpl_Stat(
160 STATSTG* pstatstg, /* [out] */
161 DWORD grfStatFlag); /* [in] */
163 HRESULT WINAPI HGLOBALStreamImpl_Clone(
165 IStream** ppstm); /* [out] */
169 * Virtual function table for the HGLOBALStreamImpl class.
171 static ICOM_VTABLE(IStream) HGLOBALStreamImpl_Vtbl =
173 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
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.61]
193 HRESULT WINAPI CreateStreamOnHGlobal(
195 BOOL fDeleteOnRelease,
198 HGLOBALStreamImpl* newStream;
200 newStream = HGLOBALStreamImpl_Construct(hGlobal,
205 return IUnknown_QueryInterface((IUnknown*)newStream,
210 return E_OUTOFMEMORY;
213 /***********************************************************************
214 * GetHGlobalFromStream [OLE32.71]
216 HRESULT WINAPI GetHGlobalFromStream(IStream* pstm, HGLOBAL* phglobal)
218 HGLOBALStreamImpl* pStream;
223 pStream = (HGLOBALStreamImpl*) pstm;
226 * Verify that the stream object was created with CreateStreamOnHGlobal.
228 if (pStream->lpVtbl == &HGLOBALStreamImpl_Vtbl)
229 *phglobal = pStream->supportHandle;
239 /******************************************************************************
240 ** HGLOBALStreamImpl implementation
244 * This is the constructor for the HGLOBALStreamImpl class.
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(
253 BOOL fDeleteOnRelease)
255 HGLOBALStreamImpl* newStream;
257 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl));
262 * Set-up the virtual function table and reference count.
264 newStream->lpVtbl = &HGLOBALStreamImpl_Vtbl;
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 |
283 * Start the stream at the beginning.
285 newStream->currentPosition.s.HighPart = 0;
286 newStream->currentPosition.s.LowPart = 0;
289 * Initialize the size of the stream to the size of the handle.
291 newStream->streamSize.s.HighPart = 0;
292 newStream->streamSize.s.LowPart = GlobalSize(newStream->supportHandle);
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
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);
325 * This implements the IUnknown method QueryInterface for this
328 HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
330 REFIID riid, /* [in] */
331 void** ppvObject) /* [iid_is][out] */
333 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
336 * Perform a sanity check on the parameters.
342 * Initialize the return parameter.
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.
362 return E_NOINTERFACE;
365 * Query Interface always increases the reference count by one when it is
368 HGLOBALStreamImpl_AddRef(iface);
374 * This implements the IUnknown method AddRef for this
377 ULONG WINAPI HGLOBALStreamImpl_AddRef(
380 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
388 * This implements the IUnknown method Release for this
391 ULONG WINAPI HGLOBALStreamImpl_Release(
394 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
403 * If the reference count goes down to 0, perform suicide.
407 HGLOBALStreamImpl_Destroy(This);
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
420 * See the documentation of ISequentialStream for more info.
422 HRESULT WINAPI HGLOBALStreamImpl_Read(
424 void* pv, /* [length_is][size_is][out] */
426 ULONG* pcbRead) /* [out] */
428 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
431 ULONG bytesReadBuffer;
432 ULONG bytesToReadFromBuffer;
434 TRACE("(%p, %p, %ld, %p)\n", iface,
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.
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.s.LowPart - This->currentPosition.s.LowPart, cb);
451 * Lock the buffer in position and copy the data.
453 supportBuffer = GlobalLock(This->supportHandle);
455 memcpy(pv, (char *) supportBuffer+This->currentPosition.s.LowPart, bytesToReadFromBuffer);
458 * Move the current position to the new position
460 This->currentPosition.s.LowPart+=bytesToReadFromBuffer;
463 * Return the number of bytes read.
465 *pcbRead = bytesToReadFromBuffer;
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
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(
495 const void* pv, /* [size_is][in] */
497 ULONG* pcbWritten) /* [out] */
499 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
502 ULARGE_INTEGER newSize;
503 ULONG bytesWritten = 0;
505 TRACE("(%p, %p, %ld, %p)\n", iface,
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.
513 pcbWritten = &bytesWritten;
521 newSize.s.HighPart = 0;
522 newSize.s.LowPart = This->currentPosition.s.LowPart + cb;
526 * Verify if we need to grow the stream
528 if (newSize.s.LowPart > This->streamSize.s.LowPart)
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.s.LowPart, pv, cb);
542 * Move the current position to the new position
544 This->currentPosition.s.LowPart+=cb;
547 * Return the number of bytes read.
554 GlobalUnlock(This->supportHandle);
560 * This method is part of the IStream interface.
562 * It will move the current stream pointer according to the parameters
565 * See the documentation of IStream for more info.
567 HRESULT WINAPI HGLOBALStreamImpl_Seek(
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, %ld, %ld, %p)\n", iface,
578 dlibMove.s.LowPart, dwOrigin, plibNewPosition);
581 * The file pointer is moved depending on the given "function"
586 case STREAM_SEEK_SET:
587 newPosition.s.HighPart = 0;
588 newPosition.s.LowPart = 0;
590 case STREAM_SEEK_CUR:
591 newPosition = This->currentPosition;
593 case STREAM_SEEK_END:
594 newPosition = This->streamSize;
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;
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(
625 ULARGE_INTEGER libNewSize) /* [in] */
627 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
629 TRACE("(%p, %ld)\n", iface, libNewSize.s.LowPart);
634 if (libNewSize.s.HighPart != 0)
635 return STG_E_INVALIDFUNCTION;
637 if (This->streamSize.s.LowPart == libNewSize.s.LowPart)
641 * Re allocate the HGlobal to fit the new size of the stream.
643 This->supportHandle = GlobalReAlloc(This->supportHandle,
644 libNewSize.s.LowPart,
647 This->streamSize.s.LowPart = libNewSize.s.LowPart;
653 * This method is part of the IStream interface.
655 * It will copy the 'cb' Bytes to 'pstm' IStream.
657 * See the documentation of IStream for more info.
659 HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
661 IStream* pstm, /* [unique][in] */
662 ULARGE_INTEGER cb, /* [in] */
663 ULARGE_INTEGER* pcbRead, /* [out] */
664 ULARGE_INTEGER* pcbWritten) /* [out] */
668 ULONG bytesRead, bytesWritten, copySize;
669 ULARGE_INTEGER totalBytesRead;
670 ULARGE_INTEGER totalBytesWritten;
672 TRACE("(%p, %p, %ld, %p, %p)\n", iface, pstm,
673 cb.s.LowPart, pcbRead, pcbWritten);
679 return STG_E_INVALIDPOINTER;
681 totalBytesRead.s.LowPart = totalBytesRead.s.HighPart = 0;
682 totalBytesWritten.s.LowPart = totalBytesWritten.s.HighPart = 0;
685 * use stack to store data temporarly
686 * there is surely more performant way of doing it, for now this basic
687 * implementation will do the job
689 while ( cb.s.LowPart > 0 )
691 if ( cb.s.LowPart >= 128 )
694 copySize = cb.s.LowPart;
696 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
698 totalBytesRead.s.LowPart += bytesRead;
700 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
702 totalBytesWritten.s.LowPart += bytesWritten;
705 * Check that read & write operations were succesfull
707 if (bytesRead != bytesWritten)
709 hr = STG_E_MEDIUMFULL;
713 if (bytesRead!=copySize)
716 cb.s.LowPart -= bytesRead;
720 * Update number of bytes read and written
724 pcbRead->s.LowPart = totalBytesRead.s.LowPart;
725 pcbRead->s.HighPart = totalBytesRead.s.HighPart;
730 pcbWritten->s.LowPart = totalBytesWritten.s.LowPart;
731 pcbWritten->s.HighPart = totalBytesWritten.s.HighPart;
737 * This method is part of the IStream interface.
739 * For streams supported by HGLOBALS, this function does nothing.
740 * This is what the documentation tells us.
742 * See the documentation of IStream for more info.
744 HRESULT WINAPI HGLOBALStreamImpl_Commit(
746 DWORD grfCommitFlags) /* [in] */
752 * This method is part of the IStream interface.
754 * For streams supported by HGLOBALS, this function does nothing.
755 * This is what the documentation tells us.
757 * See the documentation of IStream for more info.
759 HRESULT WINAPI HGLOBALStreamImpl_Revert(
766 * This method is part of the IStream interface.
768 * For streams supported by HGLOBALS, this function does nothing.
769 * This is what the documentation tells us.
771 * See the documentation of IStream for more info.
773 HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
775 ULARGE_INTEGER libOffset, /* [in] */
776 ULARGE_INTEGER cb, /* [in] */
777 DWORD dwLockType) /* [in] */
783 * This method is part of the IStream interface.
785 * For streams supported by HGLOBALS, this function does nothing.
786 * This is what the documentation tells us.
788 * See the documentation of IStream for more info.
790 HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
792 ULARGE_INTEGER libOffset, /* [in] */
793 ULARGE_INTEGER cb, /* [in] */
794 DWORD dwLockType) /* [in] */
800 * This method is part of the IStream interface.
802 * This method returns information about the current
805 * See the documentation of IStream for more info.
807 HRESULT WINAPI HGLOBALStreamImpl_Stat(
809 STATSTG* pstatstg, /* [out] */
810 DWORD grfStatFlag) /* [in] */
812 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
814 memset(pstatstg, 0, sizeof(STATSTG));
816 pstatstg->pwcsName = NULL;
817 pstatstg->type = STGTY_STREAM;
818 pstatstg->cbSize = This->streamSize;
823 HRESULT WINAPI HGLOBALStreamImpl_Clone(
825 IStream** ppstm) /* [out] */
827 ULARGE_INTEGER dummy;
828 LARGE_INTEGER offset;
830 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
831 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface,This->deleteOnRelease,(long)This->currentPosition.QuadPart);
832 hr=CreateStreamOnHGlobal(This->supportHandle, FALSE, ppstm);
835 offset.QuadPart=(LONGLONG)This->currentPosition.QuadPart;
836 HGLOBALStreamImpl_Seek(*ppstm,offset,STREAM_SEEK_SET,&dummy);