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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(storage);
49 /****************************************************************************
50 * HGLOBALStreamImpl definition.
52 * This class implements the IStream interface and represents a stream
53 * supported by an HGLOBAL pointer.
55 struct HGLOBALStreamImpl
57 const IStreamVtbl *lpVtbl; /* Needs to be the first item in the struct
58 * since we want to cast this in an IStream pointer */
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.
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 * This is the destructor of the HGLOBALStreamImpl class.
92 * This method will clean-up all the resources used-up by the given HGLOBALStreamImpl
93 * class. The pointer passed-in to this function will be freed and will not
96 static void HGLOBALStreamImpl_Destroy(HGLOBALStreamImpl* This)
98 TRACE("(%p)\n", This);
101 * Release the HGlobal if the constructor asked for that.
103 if (This->deleteOnRelease)
105 GlobalFree(This->supportHandle);
106 This->supportHandle=0;
110 * Finally, free the memory used-up by the class.
112 HeapFree(GetProcessHeap(), 0, This);
116 * This implements the IUnknown method AddRef for this
119 static ULONG WINAPI HGLOBALStreamImpl_AddRef(
122 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
123 return InterlockedIncrement(&This->ref);
127 * This implements the IUnknown method QueryInterface for this
130 static HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
132 REFIID riid, /* [in] */
133 void** ppvObject) /* [iid_is][out] */
135 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
138 * Perform a sanity check on the parameters.
144 * Initialize the return parameter.
149 * Compare the riid with the interface IDs implemented by this object.
151 if (IsEqualIID(&IID_IUnknown, riid) ||
152 IsEqualIID(&IID_ISequentialStream, riid) ||
153 IsEqualIID(&IID_IStream, riid))
155 *ppvObject = (IStream*)This;
159 * Check that we obtained an interface.
162 return E_NOINTERFACE;
165 * Query Interface always increases the reference count by one when it is
168 HGLOBALStreamImpl_AddRef(iface);
174 * This implements the IUnknown method Release for this
177 static ULONG WINAPI HGLOBALStreamImpl_Release(
180 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
183 newRef = InterlockedDecrement(&This->ref);
186 * If the reference count goes down to 0, perform suicide.
190 HGLOBALStreamImpl_Destroy(This);
197 * This method is part of the ISequentialStream interface.
199 * If reads a block of information from the stream at the current
200 * position. It then moves the current position at the end of the
203 * See the documentation of ISequentialStream for more info.
205 static HRESULT WINAPI HGLOBALStreamImpl_Read(
207 void* pv, /* [length_is][size_is][out] */
209 ULONG* pcbRead) /* [out] */
211 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
214 ULONG bytesReadBuffer;
215 ULONG bytesToReadFromBuffer;
217 TRACE("(%p, %p, %d, %p)\n", iface,
221 * If the caller is not interested in the nubmer of bytes read,
222 * we use another buffer to avoid "if" statements in the code.
225 pcbRead = &bytesReadBuffer;
228 * Using the known size of the stream, calculate the number of bytes
229 * to read from the block chain
231 bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
234 * Lock the buffer in position and copy the data.
236 supportBuffer = GlobalLock(This->supportHandle);
238 memcpy(pv, (char *) supportBuffer+This->currentPosition.u.LowPart, bytesToReadFromBuffer);
241 * Move the current position to the new position
243 This->currentPosition.u.LowPart+=bytesToReadFromBuffer;
246 * Return the number of bytes read.
248 *pcbRead = bytesToReadFromBuffer;
253 GlobalUnlock(This->supportHandle);
256 * Always returns S_OK even if the end of the stream is reached before the
264 * This method is part of the ISequentialStream interface.
266 * It writes a block of information to the stream at the current
267 * position. It then moves the current position at the end of the
268 * written block. If the stream is too small to fit the block,
269 * the stream is grown to fit.
271 * See the documentation of ISequentialStream for more info.
273 static HRESULT WINAPI HGLOBALStreamImpl_Write(
275 const void* pv, /* [size_is][in] */
277 ULONG* pcbWritten) /* [out] */
279 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
282 ULARGE_INTEGER newSize;
283 ULONG bytesWritten = 0;
285 TRACE("(%p, %p, %d, %p)\n", iface, pv, cb, pcbWritten);
288 * If the caller is not interested in the number of bytes written,
289 * we use another buffer to avoid "if" statements in the code.
292 pcbWritten = &bytesWritten;
297 newSize.u.HighPart = 0;
298 newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
301 * Verify if we need to grow the stream
303 if (newSize.u.LowPart > This->streamSize.u.LowPart)
306 HRESULT hr = IStream_SetSize(iface, newSize);
309 ERR("IStream_SetSize failed with error 0x%08x\n", hr);
315 * Lock the buffer in position and copy the data.
317 supportBuffer = GlobalLock(This->supportHandle);
319 memcpy((char *) supportBuffer+This->currentPosition.u.LowPart, pv, cb);
322 * Move the current position to the new position
324 This->currentPosition.u.LowPart+=cb;
329 GlobalUnlock(This->supportHandle);
333 * Return the number of bytes read.
341 * This method is part of the IStream interface.
343 * It will move the current stream pointer according to the parameters
346 * See the documentation of IStream for more info.
348 static HRESULT WINAPI HGLOBALStreamImpl_Seek(
350 LARGE_INTEGER dlibMove, /* [in] */
351 DWORD dwOrigin, /* [in] */
352 ULARGE_INTEGER* plibNewPosition) /* [out] */
354 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
356 ULARGE_INTEGER newPosition;
358 TRACE("(%p, %x%08x, %d, %p)\n", iface, dlibMove.u.HighPart,
359 dlibMove.u.LowPart, dwOrigin, plibNewPosition);
362 * The file pointer is moved depending on the given "function"
367 case STREAM_SEEK_SET:
368 newPosition.u.HighPart = 0;
369 newPosition.u.LowPart = 0;
371 case STREAM_SEEK_CUR:
372 newPosition = This->currentPosition;
374 case STREAM_SEEK_END:
375 newPosition = This->streamSize;
378 return STG_E_INVALIDFUNCTION;
382 * Move the actual file pointer
383 * If the file pointer ends-up after the end of the stream, the next Write operation will
384 * make the file larger. This is how it is documented.
386 if (dlibMove.QuadPart < 0 && newPosition.QuadPart < -dlibMove.QuadPart) return STG_E_INVALIDFUNCTION;
388 newPosition.QuadPart = RtlLargeIntegerAdd(newPosition.QuadPart, dlibMove.QuadPart);
390 if (plibNewPosition) *plibNewPosition = newPosition;
391 This->currentPosition = newPosition;
397 * This method is part of the IStream interface.
399 * It will change the size of a stream.
401 * TODO: Switch from small blocks to big blocks and vice versa.
403 * See the documentation of IStream for more info.
405 static HRESULT WINAPI HGLOBALStreamImpl_SetSize(
407 ULARGE_INTEGER libNewSize) /* [in] */
409 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
410 HGLOBAL supportHandle;
412 TRACE("(%p, %d)\n", iface, libNewSize.u.LowPart);
415 * HighPart is ignored as shown in tests
418 if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
422 * Re allocate the HGlobal to fit the new size of the stream.
424 supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
426 if (supportHandle == 0)
427 return E_OUTOFMEMORY;
429 This->supportHandle = supportHandle;
430 This->streamSize.u.LowPart = libNewSize.u.LowPart;
436 * This method is part of the IStream interface.
438 * It will copy the 'cb' Bytes to 'pstm' IStream.
440 * See the documentation of IStream for more info.
442 static HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
444 IStream* pstm, /* [unique][in] */
445 ULARGE_INTEGER cb, /* [in] */
446 ULARGE_INTEGER* pcbRead, /* [out] */
447 ULARGE_INTEGER* pcbWritten) /* [out] */
451 ULONG bytesRead, bytesWritten, copySize;
452 ULARGE_INTEGER totalBytesRead;
453 ULARGE_INTEGER totalBytesWritten;
455 TRACE("(%p, %p, %d, %p, %p)\n", iface, pstm,
456 cb.u.LowPart, pcbRead, pcbWritten);
462 return STG_E_INVALIDPOINTER;
464 totalBytesRead.u.LowPart = totalBytesRead.u.HighPart = 0;
465 totalBytesWritten.u.LowPart = totalBytesWritten.u.HighPart = 0;
468 * use stack to store data temporarly
469 * there is surely more performant way of doing it, for now this basic
470 * implementation will do the job
472 while ( cb.u.LowPart > 0 )
474 if ( cb.u.LowPart >= 128 )
477 copySize = cb.u.LowPart;
479 hr = IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
483 totalBytesRead.u.LowPart += bytesRead;
487 hr = IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
491 totalBytesWritten.u.LowPart += bytesWritten;
494 if (bytesRead!=copySize)
497 cb.u.LowPart -= bytesRead;
501 * Update number of bytes read and written
505 pcbRead->u.LowPart = totalBytesRead.u.LowPart;
506 pcbRead->u.HighPart = totalBytesRead.u.HighPart;
511 pcbWritten->u.LowPart = totalBytesWritten.u.LowPart;
512 pcbWritten->u.HighPart = totalBytesWritten.u.HighPart;
518 * This method is part of the IStream interface.
520 * For streams supported by HGLOBALS, this function does nothing.
521 * This is what the documentation tells us.
523 * See the documentation of IStream for more info.
525 static HRESULT WINAPI HGLOBALStreamImpl_Commit(
527 DWORD grfCommitFlags) /* [in] */
533 * This method is part of the IStream interface.
535 * For streams supported by HGLOBALS, this function does nothing.
536 * This is what the documentation tells us.
538 * See the documentation of IStream for more info.
540 static HRESULT WINAPI HGLOBALStreamImpl_Revert(
547 * This method is part of the IStream interface.
549 * For streams supported by HGLOBALS, this function does nothing.
550 * This is what the documentation tells us.
552 * See the documentation of IStream for more info.
554 static HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
556 ULARGE_INTEGER libOffset, /* [in] */
557 ULARGE_INTEGER cb, /* [in] */
558 DWORD dwLockType) /* [in] */
560 return STG_E_INVALIDFUNCTION;
564 * This method is part of the IStream interface.
566 * For streams supported by HGLOBALS, this function does nothing.
567 * This is what the documentation tells us.
569 * See the documentation of IStream for more info.
571 static HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
573 ULARGE_INTEGER libOffset, /* [in] */
574 ULARGE_INTEGER cb, /* [in] */
575 DWORD dwLockType) /* [in] */
581 * This method is part of the IStream interface.
583 * This method returns information about the current
586 * See the documentation of IStream for more info.
588 static HRESULT WINAPI HGLOBALStreamImpl_Stat(
590 STATSTG* pstatstg, /* [out] */
591 DWORD grfStatFlag) /* [in] */
593 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
595 memset(pstatstg, 0, sizeof(STATSTG));
597 pstatstg->pwcsName = NULL;
598 pstatstg->type = STGTY_STREAM;
599 pstatstg->cbSize = This->streamSize;
604 static HRESULT WINAPI HGLOBALStreamImpl_Clone(
606 IStream** ppstm) /* [out] */
608 ULARGE_INTEGER dummy;
609 LARGE_INTEGER offset;
611 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
612 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface,This->deleteOnRelease,(long)This->currentPosition.QuadPart);
613 hr=CreateStreamOnHGlobal(This->supportHandle, FALSE, ppstm);
616 offset.QuadPart=(LONGLONG)This->currentPosition.QuadPart;
617 HGLOBALStreamImpl_Seek(*ppstm,offset,STREAM_SEEK_SET,&dummy);
622 * Virtual function table for the HGLOBALStreamImpl class.
624 static const IStreamVtbl HGLOBALStreamImpl_Vtbl =
626 HGLOBALStreamImpl_QueryInterface,
627 HGLOBALStreamImpl_AddRef,
628 HGLOBALStreamImpl_Release,
629 HGLOBALStreamImpl_Read,
630 HGLOBALStreamImpl_Write,
631 HGLOBALStreamImpl_Seek,
632 HGLOBALStreamImpl_SetSize,
633 HGLOBALStreamImpl_CopyTo,
634 HGLOBALStreamImpl_Commit,
635 HGLOBALStreamImpl_Revert,
636 HGLOBALStreamImpl_LockRegion,
637 HGLOBALStreamImpl_UnlockRegion,
638 HGLOBALStreamImpl_Stat,
639 HGLOBALStreamImpl_Clone
642 /******************************************************************************
643 ** HGLOBALStreamImpl implementation
647 * This is the constructor for the HGLOBALStreamImpl class.
650 * hGlobal - Handle that will support the stream. can be NULL.
651 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
652 * when the IStream object is destroyed.
654 static HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
656 BOOL fDeleteOnRelease)
658 HGLOBALStreamImpl* newStream;
660 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl));
665 * Set-up the virtual function table and reference count.
667 newStream->lpVtbl = &HGLOBALStreamImpl_Vtbl;
671 * Initialize the support.
673 newStream->supportHandle = hGlobal;
674 newStream->deleteOnRelease = fDeleteOnRelease;
677 * This method will allocate a handle if one is not supplied.
679 if (!newStream->supportHandle)
681 newStream->supportHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD |
686 * Start the stream at the beginning.
688 newStream->currentPosition.u.HighPart = 0;
689 newStream->currentPosition.u.LowPart = 0;
692 * Initialize the size of the stream to the size of the handle.
694 newStream->streamSize.u.HighPart = 0;
695 newStream->streamSize.u.LowPart = GlobalSize(newStream->supportHandle);
702 /***********************************************************************
703 * CreateStreamOnHGlobal [OLE32.@]
705 HRESULT WINAPI CreateStreamOnHGlobal(
707 BOOL fDeleteOnRelease,
710 HGLOBALStreamImpl* newStream;
712 newStream = HGLOBALStreamImpl_Construct(hGlobal,
717 return IUnknown_QueryInterface((IUnknown*)newStream,
722 return E_OUTOFMEMORY;
725 /***********************************************************************
726 * GetHGlobalFromStream [OLE32.@]
728 HRESULT WINAPI GetHGlobalFromStream(IStream* pstm, HGLOBAL* phglobal)
730 HGLOBALStreamImpl* pStream;
735 pStream = (HGLOBALStreamImpl*) pstm;
738 * Verify that the stream object was created with CreateStreamOnHGlobal.
740 if (pStream->lpVtbl == &HGLOBALStreamImpl_Vtbl)
741 *phglobal = pStream->supportHandle;