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
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(storage);
49 /****************************************************************************
50 * HGLOBALStreamImpl definition.
52 * This class imlements the IStream inteface and represents a stream
53 * supported by an HGLOBAL pointer.
55 struct HGLOBALStreamImpl
57 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 (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
153 *ppvObject = (IStream*)This;
155 else if (memcmp(&IID_IStream, riid, sizeof(IID_IStream)) == 0)
157 *ppvObject = (IStream*)This;
161 * Check that we obtained an interface.
164 return E_NOINTERFACE;
167 * Query Interface always increases the reference count by one when it is
170 HGLOBALStreamImpl_AddRef(iface);
176 * This implements the IUnknown method Release for this
179 static ULONG WINAPI HGLOBALStreamImpl_Release(
182 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
185 newRef = InterlockedDecrement(&This->ref);
188 * If the reference count goes down to 0, perform suicide.
192 HGLOBALStreamImpl_Destroy(This);
199 * This method is part of the ISequentialStream interface.
201 * If reads a block of information from the stream at the current
202 * position. It then moves the current position at the end of the
205 * See the documentation of ISequentialStream for more info.
207 static HRESULT WINAPI HGLOBALStreamImpl_Read(
209 void* pv, /* [length_is][size_is][out] */
211 ULONG* pcbRead) /* [out] */
213 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
216 ULONG bytesReadBuffer;
217 ULONG bytesToReadFromBuffer;
219 TRACE("(%p, %p, %ld, %p)\n", iface,
223 * If the caller is not interested in the nubmer of bytes read,
224 * we use another buffer to avoid "if" statements in the code.
227 pcbRead = &bytesReadBuffer;
230 * Using the known size of the stream, calculate the number of bytes
231 * to read from the block chain
233 bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
236 * Lock the buffer in position and copy the data.
238 supportBuffer = GlobalLock(This->supportHandle);
240 memcpy(pv, (char *) supportBuffer+This->currentPosition.u.LowPart, bytesToReadFromBuffer);
243 * Move the current position to the new position
245 This->currentPosition.u.LowPart+=bytesToReadFromBuffer;
248 * Return the number of bytes read.
250 *pcbRead = bytesToReadFromBuffer;
255 GlobalUnlock(This->supportHandle);
258 * The function returns S_OK if the buffer was filled completely
259 * it returns S_FALSE if the end of the stream is reached before the
269 * This method is part of the ISequentialStream interface.
271 * It writes a block of information to the stream at the current
272 * position. It then moves the current position at the end of the
273 * written block. If the stream is too small to fit the block,
274 * the stream is grown to fit.
276 * See the documentation of ISequentialStream for more info.
278 static HRESULT WINAPI HGLOBALStreamImpl_Write(
280 const void* pv, /* [size_is][in] */
282 ULONG* pcbWritten) /* [out] */
284 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
287 ULARGE_INTEGER newSize;
288 ULONG bytesWritten = 0;
290 TRACE("(%p, %p, %ld, %p)\n", iface,
294 * If the caller is not interested in the number of bytes written,
295 * we use another buffer to avoid "if" statements in the code.
298 pcbWritten = &bytesWritten;
306 newSize.u.HighPart = 0;
307 newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
311 * Verify if we need to grow the stream
313 if (newSize.u.LowPart > This->streamSize.u.LowPart)
316 IStream_SetSize(iface, newSize);
320 * Lock the buffer in position and copy the data.
322 supportBuffer = GlobalLock(This->supportHandle);
324 memcpy((char *) supportBuffer+This->currentPosition.u.LowPart, pv, cb);
327 * Move the current position to the new position
329 This->currentPosition.u.LowPart+=cb;
332 * Return the number of bytes read.
339 GlobalUnlock(This->supportHandle);
345 * This method is part of the IStream interface.
347 * It will move the current stream pointer according to the parameters
350 * See the documentation of IStream for more info.
352 static HRESULT WINAPI HGLOBALStreamImpl_Seek(
354 LARGE_INTEGER dlibMove, /* [in] */
355 DWORD dwOrigin, /* [in] */
356 ULARGE_INTEGER* plibNewPosition) /* [out] */
358 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
360 ULARGE_INTEGER newPosition;
362 TRACE("(%p, %lx%08lx, %ld, %p)\n", iface, dlibMove.u.HighPart,
363 dlibMove.u.LowPart, dwOrigin, plibNewPosition);
366 * The file pointer is moved depending on the given "function"
371 case STREAM_SEEK_SET:
372 newPosition.u.HighPart = 0;
373 newPosition.u.LowPart = 0;
375 case STREAM_SEEK_CUR:
376 newPosition = This->currentPosition;
378 case STREAM_SEEK_END:
379 newPosition = This->streamSize;
382 return STG_E_INVALIDFUNCTION;
386 * Move the actual file pointer
387 * If the file pointer ends-up after the end of the stream, the next Write operation will
388 * make the file larger. This is how it is documented.
390 if (dlibMove.QuadPart < 0 && newPosition.QuadPart < -dlibMove.QuadPart) return STG_E_INVALIDFUNCTION;
392 newPosition.QuadPart = RtlLargeIntegerAdd(newPosition.QuadPart, dlibMove.QuadPart);
394 if (plibNewPosition) *plibNewPosition = newPosition;
395 This->currentPosition = newPosition;
401 * This method is part of the IStream interface.
403 * It will change the size of a stream.
405 * TODO: Switch from small blocks to big blocks and vice versa.
407 * See the documentation of IStream for more info.
409 static HRESULT WINAPI HGLOBALStreamImpl_SetSize(
411 ULARGE_INTEGER libNewSize) /* [in] */
413 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
414 HGLOBAL supportHandle;
416 TRACE("(%p, %ld)\n", iface, libNewSize.u.LowPart);
421 if (libNewSize.u.HighPart != 0)
422 return STG_E_INVALIDFUNCTION;
424 if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
428 * Re allocate the HGlobal to fit the new size of the stream.
430 supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
432 if (supportHandle == 0)
433 return STG_E_MEDIUMFULL;
435 This->supportHandle = supportHandle;
436 This->streamSize.u.LowPart = libNewSize.u.LowPart;
442 * This method is part of the IStream interface.
444 * It will copy the 'cb' Bytes to 'pstm' IStream.
446 * See the documentation of IStream for more info.
448 static HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
450 IStream* pstm, /* [unique][in] */
451 ULARGE_INTEGER cb, /* [in] */
452 ULARGE_INTEGER* pcbRead, /* [out] */
453 ULARGE_INTEGER* pcbWritten) /* [out] */
457 ULONG bytesRead, bytesWritten, copySize;
458 ULARGE_INTEGER totalBytesRead;
459 ULARGE_INTEGER totalBytesWritten;
461 TRACE("(%p, %p, %ld, %p, %p)\n", iface, pstm,
462 cb.u.LowPart, pcbRead, pcbWritten);
468 return STG_E_INVALIDPOINTER;
470 totalBytesRead.u.LowPart = totalBytesRead.u.HighPart = 0;
471 totalBytesWritten.u.LowPart = totalBytesWritten.u.HighPart = 0;
474 * use stack to store data temporarly
475 * there is surely more performant way of doing it, for now this basic
476 * implementation will do the job
478 while ( cb.u.LowPart > 0 )
480 if ( cb.u.LowPart >= 128 )
483 copySize = cb.u.LowPart;
485 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
487 totalBytesRead.u.LowPart += bytesRead;
489 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
491 totalBytesWritten.u.LowPart += bytesWritten;
494 * Check that read & write operations were succesfull
496 if (bytesRead != bytesWritten)
498 hr = STG_E_MEDIUMFULL;
502 if (bytesRead!=copySize)
505 cb.u.LowPart -= bytesRead;
509 * Update number of bytes read and written
513 pcbRead->u.LowPart = totalBytesRead.u.LowPart;
514 pcbRead->u.HighPart = totalBytesRead.u.HighPart;
519 pcbWritten->u.LowPart = totalBytesWritten.u.LowPart;
520 pcbWritten->u.HighPart = totalBytesWritten.u.HighPart;
526 * This method is part of the IStream interface.
528 * For streams supported by HGLOBALS, this function does nothing.
529 * This is what the documentation tells us.
531 * See the documentation of IStream for more info.
533 static HRESULT WINAPI HGLOBALStreamImpl_Commit(
535 DWORD grfCommitFlags) /* [in] */
541 * This method is part of the IStream interface.
543 * For streams supported by HGLOBALS, this function does nothing.
544 * This is what the documentation tells us.
546 * See the documentation of IStream for more info.
548 static HRESULT WINAPI HGLOBALStreamImpl_Revert(
555 * This method is part of the IStream interface.
557 * For streams supported by HGLOBALS, this function does nothing.
558 * This is what the documentation tells us.
560 * See the documentation of IStream for more info.
562 static HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
564 ULARGE_INTEGER libOffset, /* [in] */
565 ULARGE_INTEGER cb, /* [in] */
566 DWORD dwLockType) /* [in] */
572 * This method is part of the IStream interface.
574 * For streams supported by HGLOBALS, this function does nothing.
575 * This is what the documentation tells us.
577 * See the documentation of IStream for more info.
579 static HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
581 ULARGE_INTEGER libOffset, /* [in] */
582 ULARGE_INTEGER cb, /* [in] */
583 DWORD dwLockType) /* [in] */
589 * This method is part of the IStream interface.
591 * This method returns information about the current
594 * See the documentation of IStream for more info.
596 static HRESULT WINAPI HGLOBALStreamImpl_Stat(
598 STATSTG* pstatstg, /* [out] */
599 DWORD grfStatFlag) /* [in] */
601 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
603 memset(pstatstg, 0, sizeof(STATSTG));
605 pstatstg->pwcsName = NULL;
606 pstatstg->type = STGTY_STREAM;
607 pstatstg->cbSize = This->streamSize;
612 static HRESULT WINAPI HGLOBALStreamImpl_Clone(
614 IStream** ppstm) /* [out] */
616 ULARGE_INTEGER dummy;
617 LARGE_INTEGER offset;
619 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
620 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface,This->deleteOnRelease,(long)This->currentPosition.QuadPart);
621 hr=CreateStreamOnHGlobal(This->supportHandle, FALSE, ppstm);
624 offset.QuadPart=(LONGLONG)This->currentPosition.QuadPart;
625 HGLOBALStreamImpl_Seek(*ppstm,offset,STREAM_SEEK_SET,&dummy);
630 * Virtual function table for the HGLOBALStreamImpl class.
632 static IStreamVtbl HGLOBALStreamImpl_Vtbl =
634 HGLOBALStreamImpl_QueryInterface,
635 HGLOBALStreamImpl_AddRef,
636 HGLOBALStreamImpl_Release,
637 HGLOBALStreamImpl_Read,
638 HGLOBALStreamImpl_Write,
639 HGLOBALStreamImpl_Seek,
640 HGLOBALStreamImpl_SetSize,
641 HGLOBALStreamImpl_CopyTo,
642 HGLOBALStreamImpl_Commit,
643 HGLOBALStreamImpl_Revert,
644 HGLOBALStreamImpl_LockRegion,
645 HGLOBALStreamImpl_UnlockRegion,
646 HGLOBALStreamImpl_Stat,
647 HGLOBALStreamImpl_Clone
650 /******************************************************************************
651 ** HGLOBALStreamImpl implementation
655 * This is the constructor for the HGLOBALStreamImpl class.
658 * hGlobal - Handle that will support the stream. can be NULL.
659 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
660 * when the IStream object is destroyed.
662 HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
664 BOOL fDeleteOnRelease)
666 HGLOBALStreamImpl* newStream;
668 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl));
673 * Set-up the virtual function table and reference count.
675 newStream->lpVtbl = &HGLOBALStreamImpl_Vtbl;
679 * Initialize the support.
681 newStream->supportHandle = hGlobal;
682 newStream->deleteOnRelease = fDeleteOnRelease;
685 * This method will allocate a handle if one is not supplied.
687 if (!newStream->supportHandle)
689 newStream->supportHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD |
694 * Start the stream at the beginning.
696 newStream->currentPosition.u.HighPart = 0;
697 newStream->currentPosition.u.LowPart = 0;
700 * Initialize the size of the stream to the size of the handle.
702 newStream->streamSize.u.HighPart = 0;
703 newStream->streamSize.u.LowPart = GlobalSize(newStream->supportHandle);
710 /***********************************************************************
711 * CreateStreamOnHGlobal [OLE32.@]
713 HRESULT WINAPI CreateStreamOnHGlobal(
715 BOOL fDeleteOnRelease,
718 HGLOBALStreamImpl* newStream;
720 newStream = HGLOBALStreamImpl_Construct(hGlobal,
725 return IUnknown_QueryInterface((IUnknown*)newStream,
730 return E_OUTOFMEMORY;
733 /***********************************************************************
734 * GetHGlobalFromStream [OLE32.@]
736 HRESULT WINAPI GetHGlobalFromStream(IStream* pstm, HGLOBAL* phglobal)
738 HGLOBALStreamImpl* pStream;
743 pStream = (HGLOBALStreamImpl*) pstm;
746 * Verify that the stream object was created with CreateStreamOnHGlobal.
748 if (pStream->lpVtbl == &HGLOBALStreamImpl_Vtbl)
749 *phglobal = pStream->supportHandle;