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
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 */
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.
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(
92 BOOL fDeleteOnRelease);
94 void HGLOBALStreamImpl_Destroy(
95 HGLOBALStreamImpl* This);
97 void HGLOBALStreamImpl_OpenBlockChain(
98 HGLOBALStreamImpl* This);
100 HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
102 REFIID riid, /* [in] */
103 void** ppvObject); /* [iid_is][out] */
105 ULONG WINAPI HGLOBALStreamImpl_AddRef(
108 ULONG WINAPI HGLOBALStreamImpl_Release(
111 HRESULT WINAPI HGLOBALStreamImpl_Read(
113 void* pv, /* [length_is][size_is][out] */
115 ULONG* pcbRead); /* [out] */
117 HRESULT WINAPI HGLOBALStreamImpl_Write(
119 const void* pv, /* [size_is][in] */
121 ULONG* pcbWritten); /* [out] */
123 HRESULT WINAPI HGLOBALStreamImpl_Seek(
125 LARGE_INTEGER dlibMove, /* [in] */
126 DWORD dwOrigin, /* [in] */
127 ULARGE_INTEGER* plibNewPosition); /* [out] */
129 HRESULT WINAPI HGLOBALStreamImpl_SetSize(
131 ULARGE_INTEGER libNewSize); /* [in] */
133 HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
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(
142 DWORD grfCommitFlags); /* [in] */
144 HRESULT WINAPI HGLOBALStreamImpl_Revert(
147 HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
149 ULARGE_INTEGER libOffset, /* [in] */
150 ULARGE_INTEGER cb, /* [in] */
151 DWORD dwLockType); /* [in] */
153 HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
155 ULARGE_INTEGER libOffset, /* [in] */
156 ULARGE_INTEGER cb, /* [in] */
157 DWORD dwLockType); /* [in] */
159 HRESULT WINAPI HGLOBALStreamImpl_Stat(
161 STATSTG* pstatstg, /* [out] */
162 DWORD grfStatFlag); /* [in] */
164 HRESULT WINAPI HGLOBALStreamImpl_Clone(
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(
196 BOOL fDeleteOnRelease,
199 HGLOBALStreamImpl* newStream;
201 newStream = HGLOBALStreamImpl_Construct(hGlobal,
206 return IUnknown_QueryInterface((IUnknown*)newStream,
211 return E_OUTOFMEMORY;
214 /***********************************************************************
215 * GetHGlobalFromStream [OLE32.@]
217 HRESULT WINAPI GetHGlobalFromStream(IStream* pstm, HGLOBAL* phglobal)
219 HGLOBALStreamImpl* pStream;
224 pStream = (HGLOBALStreamImpl*) pstm;
227 * Verify that the stream object was created with CreateStreamOnHGlobal.
229 if (pStream->lpVtbl == &HGLOBALStreamImpl_Vtbl)
230 *phglobal = pStream->supportHandle;
240 /******************************************************************************
241 ** HGLOBALStreamImpl implementation
245 * This is the constructor for the HGLOBALStreamImpl class.
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(
254 BOOL fDeleteOnRelease)
256 HGLOBALStreamImpl* newStream;
258 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl));
263 * Set-up the virtual function table and reference count.
265 newStream->lpVtbl = &HGLOBALStreamImpl_Vtbl;
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 |
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);
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
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);
326 * This implements the IUnknown method QueryInterface for this
329 HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
331 REFIID riid, /* [in] */
332 void** ppvObject) /* [iid_is][out] */
334 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
337 * Perform a sanity check on the parameters.
343 * Initialize the return parameter.
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.
363 return E_NOINTERFACE;
366 * Query Interface always increases the reference count by one when it is
369 HGLOBALStreamImpl_AddRef(iface);
375 * This implements the IUnknown method AddRef for this
378 ULONG WINAPI HGLOBALStreamImpl_AddRef(
381 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
389 * This implements the IUnknown method Release for this
392 ULONG WINAPI HGLOBALStreamImpl_Release(
395 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
404 * If the reference count goes down to 0, perform suicide.
408 HGLOBALStreamImpl_Destroy(This);
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
421 * See the documentation of ISequentialStream for more info.
423 HRESULT WINAPI HGLOBALStreamImpl_Read(
425 void* pv, /* [length_is][size_is][out] */
427 ULONG* pcbRead) /* [out] */
429 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
432 ULONG bytesReadBuffer;
433 ULONG bytesToReadFromBuffer;
435 TRACE("(%p, %p, %ld, %p)\n", iface,
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.
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;
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
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(
496 const void* pv, /* [size_is][in] */
498 ULONG* pcbWritten) /* [out] */
500 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
503 ULARGE_INTEGER newSize;
504 ULONG bytesWritten = 0;
506 TRACE("(%p, %p, %ld, %p)\n", iface,
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.
514 pcbWritten = &bytesWritten;
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)
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.
555 GlobalUnlock(This->supportHandle);
561 * This method is part of the IStream interface.
563 * It will move the current stream pointer according to the parameters
566 * See the documentation of IStream for more info.
568 HRESULT WINAPI HGLOBALStreamImpl_Seek(
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, %ld, %ld, %p)\n", iface,
579 dlibMove.s.LowPart, dwOrigin, plibNewPosition);
582 * The file pointer is moved depending on the given "function"
587 case STREAM_SEEK_SET:
588 newPosition.s.HighPart = 0;
589 newPosition.s.LowPart = 0;
591 case STREAM_SEEK_CUR:
592 newPosition = This->currentPosition;
594 case STREAM_SEEK_END:
595 newPosition = This->streamSize;
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;
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(
626 ULARGE_INTEGER libNewSize) /* [in] */
628 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
630 TRACE("(%p, %ld)\n", iface, libNewSize.s.LowPart);
635 if (libNewSize.s.HighPart != 0)
636 return STG_E_INVALIDFUNCTION;
638 if (This->streamSize.s.LowPart == libNewSize.s.LowPart)
642 * Re allocate the HGlobal to fit the new size of the stream.
644 This->supportHandle = GlobalReAlloc(This->supportHandle,
645 libNewSize.s.LowPart,
648 This->streamSize.s.LowPart = libNewSize.s.LowPart;
654 * This method is part of the IStream interface.
656 * It will copy the 'cb' Bytes to 'pstm' IStream.
658 * See the documentation of IStream for more info.
660 HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
662 IStream* pstm, /* [unique][in] */
663 ULARGE_INTEGER cb, /* [in] */
664 ULARGE_INTEGER* pcbRead, /* [out] */
665 ULARGE_INTEGER* pcbWritten) /* [out] */
669 ULONG bytesRead, bytesWritten, copySize;
670 ULARGE_INTEGER totalBytesRead;
671 ULARGE_INTEGER totalBytesWritten;
673 TRACE("(%p, %p, %ld, %p, %p)\n", iface, pstm,
674 cb.s.LowPart, pcbRead, pcbWritten);
680 return STG_E_INVALIDPOINTER;
682 totalBytesRead.s.LowPart = totalBytesRead.s.HighPart = 0;
683 totalBytesWritten.s.LowPart = totalBytesWritten.s.HighPart = 0;
686 * use stack to store data temporarly
687 * there is surely more performant way of doing it, for now this basic
688 * implementation will do the job
690 while ( cb.s.LowPart > 0 )
692 if ( cb.s.LowPart >= 128 )
695 copySize = cb.s.LowPart;
697 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
699 totalBytesRead.s.LowPart += bytesRead;
701 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
703 totalBytesWritten.s.LowPart += bytesWritten;
706 * Check that read & write operations were succesfull
708 if (bytesRead != bytesWritten)
710 hr = STG_E_MEDIUMFULL;
714 if (bytesRead!=copySize)
717 cb.s.LowPart -= bytesRead;
721 * Update number of bytes read and written
725 pcbRead->s.LowPart = totalBytesRead.s.LowPart;
726 pcbRead->s.HighPart = totalBytesRead.s.HighPart;
731 pcbWritten->s.LowPart = totalBytesWritten.s.LowPart;
732 pcbWritten->s.HighPart = totalBytesWritten.s.HighPart;
738 * This method is part of the IStream interface.
740 * For streams supported by HGLOBALS, this function does nothing.
741 * This is what the documentation tells us.
743 * See the documentation of IStream for more info.
745 HRESULT WINAPI HGLOBALStreamImpl_Commit(
747 DWORD grfCommitFlags) /* [in] */
753 * This method is part of the IStream interface.
755 * For streams supported by HGLOBALS, this function does nothing.
756 * This is what the documentation tells us.
758 * See the documentation of IStream for more info.
760 HRESULT WINAPI HGLOBALStreamImpl_Revert(
767 * This method is part of the IStream interface.
769 * For streams supported by HGLOBALS, this function does nothing.
770 * This is what the documentation tells us.
772 * See the documentation of IStream for more info.
774 HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
776 ULARGE_INTEGER libOffset, /* [in] */
777 ULARGE_INTEGER cb, /* [in] */
778 DWORD dwLockType) /* [in] */
784 * This method is part of the IStream interface.
786 * For streams supported by HGLOBALS, this function does nothing.
787 * This is what the documentation tells us.
789 * See the documentation of IStream for more info.
791 HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
793 ULARGE_INTEGER libOffset, /* [in] */
794 ULARGE_INTEGER cb, /* [in] */
795 DWORD dwLockType) /* [in] */
801 * This method is part of the IStream interface.
803 * This method returns information about the current
806 * See the documentation of IStream for more info.
808 HRESULT WINAPI HGLOBALStreamImpl_Stat(
810 STATSTG* pstatstg, /* [out] */
811 DWORD grfStatFlag) /* [in] */
813 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
815 memset(pstatstg, 0, sizeof(STATSTG));
817 pstatstg->pwcsName = NULL;
818 pstatstg->type = STGTY_STREAM;
819 pstatstg->cbSize = This->streamSize;
824 HRESULT WINAPI HGLOBALStreamImpl_Clone(
826 IStream** ppstm) /* [out] */
828 ULARGE_INTEGER dummy;
829 LARGE_INTEGER offset;
831 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
832 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface,This->deleteOnRelease,(long)This->currentPosition.QuadPart);
833 hr=CreateStreamOnHGlobal(This->supportHandle, FALSE, ppstm);
836 offset.QuadPart=(LONGLONG)This->currentPosition.QuadPart;
837 HGLOBALStreamImpl_Seek(*ppstm,offset,STREAM_SEEK_SET,&dummy);