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 imlements the IStream inteface 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 (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 * Always returns S_OK even if the end of the stream is reached before the
266 * This method is part of the ISequentialStream interface.
268 * It writes a block of information to the stream at the current
269 * position. It then moves the current position at the end of the
270 * written block. If the stream is too small to fit the block,
271 * the stream is grown to fit.
273 * See the documentation of ISequentialStream for more info.
275 static HRESULT WINAPI HGLOBALStreamImpl_Write(
277 const void* pv, /* [size_is][in] */
279 ULONG* pcbWritten) /* [out] */
281 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
284 ULARGE_INTEGER newSize;
285 ULONG bytesWritten = 0;
287 TRACE("(%p, %p, %ld, %p)\n", iface, pv, cb, pcbWritten);
290 * If the caller is not interested in the number of bytes written,
291 * we use another buffer to avoid "if" statements in the code.
294 pcbWritten = &bytesWritten;
299 newSize.u.HighPart = 0;
300 newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
303 * Verify if we need to grow the stream
305 if (newSize.u.LowPart > This->streamSize.u.LowPart)
308 HRESULT hr = IStream_SetSize(iface, newSize);
311 ERR("IStream_SetSize failed with error 0x%08lx\n", hr);
317 * Lock the buffer in position and copy the data.
319 supportBuffer = GlobalLock(This->supportHandle);
321 memcpy((char *) supportBuffer+This->currentPosition.u.LowPart, pv, cb);
324 * Move the current position to the new position
326 This->currentPosition.u.LowPart+=cb;
331 GlobalUnlock(This->supportHandle);
335 * Return the number of bytes read.
343 * This method is part of the IStream interface.
345 * It will move the current stream pointer according to the parameters
348 * See the documentation of IStream for more info.
350 static HRESULT WINAPI HGLOBALStreamImpl_Seek(
352 LARGE_INTEGER dlibMove, /* [in] */
353 DWORD dwOrigin, /* [in] */
354 ULARGE_INTEGER* plibNewPosition) /* [out] */
356 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
358 ULARGE_INTEGER newPosition;
360 TRACE("(%p, %lx%08lx, %ld, %p)\n", iface, dlibMove.u.HighPart,
361 dlibMove.u.LowPart, dwOrigin, plibNewPosition);
364 * The file pointer is moved depending on the given "function"
369 case STREAM_SEEK_SET:
370 newPosition.u.HighPart = 0;
371 newPosition.u.LowPart = 0;
373 case STREAM_SEEK_CUR:
374 newPosition = This->currentPosition;
376 case STREAM_SEEK_END:
377 newPosition = This->streamSize;
380 return STG_E_INVALIDFUNCTION;
384 * Move the actual file pointer
385 * If the file pointer ends-up after the end of the stream, the next Write operation will
386 * make the file larger. This is how it is documented.
388 if (dlibMove.QuadPart < 0 && newPosition.QuadPart < -dlibMove.QuadPart) return STG_E_INVALIDFUNCTION;
390 newPosition.QuadPart = RtlLargeIntegerAdd(newPosition.QuadPart, dlibMove.QuadPart);
392 if (plibNewPosition) *plibNewPosition = newPosition;
393 This->currentPosition = newPosition;
399 * This method is part of the IStream interface.
401 * It will change the size of a stream.
403 * TODO: Switch from small blocks to big blocks and vice versa.
405 * See the documentation of IStream for more info.
407 static HRESULT WINAPI HGLOBALStreamImpl_SetSize(
409 ULARGE_INTEGER libNewSize) /* [in] */
411 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
412 HGLOBAL supportHandle;
414 TRACE("(%p, %ld)\n", iface, libNewSize.u.LowPart);
419 if (libNewSize.u.HighPart != 0)
420 return STG_E_INVALIDFUNCTION;
422 if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
426 * Re allocate the HGlobal to fit the new size of the stream.
428 supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
430 if (supportHandle == 0)
431 return STG_E_MEDIUMFULL;
433 This->supportHandle = supportHandle;
434 This->streamSize.u.LowPart = libNewSize.u.LowPart;
440 * This method is part of the IStream interface.
442 * It will copy the 'cb' Bytes to 'pstm' IStream.
444 * See the documentation of IStream for more info.
446 static HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
448 IStream* pstm, /* [unique][in] */
449 ULARGE_INTEGER cb, /* [in] */
450 ULARGE_INTEGER* pcbRead, /* [out] */
451 ULARGE_INTEGER* pcbWritten) /* [out] */
455 ULONG bytesRead, bytesWritten, copySize;
456 ULARGE_INTEGER totalBytesRead;
457 ULARGE_INTEGER totalBytesWritten;
459 TRACE("(%p, %p, %ld, %p, %p)\n", iface, pstm,
460 cb.u.LowPart, pcbRead, pcbWritten);
466 return STG_E_INVALIDPOINTER;
468 totalBytesRead.u.LowPart = totalBytesRead.u.HighPart = 0;
469 totalBytesWritten.u.LowPart = totalBytesWritten.u.HighPart = 0;
472 * use stack to store data temporarly
473 * there is surely more performant way of doing it, for now this basic
474 * implementation will do the job
476 while ( cb.u.LowPart > 0 )
478 if ( cb.u.LowPart >= 128 )
481 copySize = cb.u.LowPart;
483 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
485 totalBytesRead.u.LowPart += bytesRead;
487 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
489 totalBytesWritten.u.LowPart += bytesWritten;
492 * Check that read & write operations were succesfull
494 if (bytesRead != bytesWritten)
496 hr = STG_E_MEDIUMFULL;
500 if (bytesRead!=copySize)
503 cb.u.LowPart -= bytesRead;
507 * Update number of bytes read and written
511 pcbRead->u.LowPart = totalBytesRead.u.LowPart;
512 pcbRead->u.HighPart = totalBytesRead.u.HighPart;
517 pcbWritten->u.LowPart = totalBytesWritten.u.LowPart;
518 pcbWritten->u.HighPart = totalBytesWritten.u.HighPart;
524 * This method is part of the IStream interface.
526 * For streams supported by HGLOBALS, this function does nothing.
527 * This is what the documentation tells us.
529 * See the documentation of IStream for more info.
531 static HRESULT WINAPI HGLOBALStreamImpl_Commit(
533 DWORD grfCommitFlags) /* [in] */
539 * This method is part of the IStream interface.
541 * For streams supported by HGLOBALS, this function does nothing.
542 * This is what the documentation tells us.
544 * See the documentation of IStream for more info.
546 static HRESULT WINAPI HGLOBALStreamImpl_Revert(
553 * This method is part of the IStream interface.
555 * For streams supported by HGLOBALS, this function does nothing.
556 * This is what the documentation tells us.
558 * See the documentation of IStream for more info.
560 static HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
562 ULARGE_INTEGER libOffset, /* [in] */
563 ULARGE_INTEGER cb, /* [in] */
564 DWORD dwLockType) /* [in] */
566 return STG_E_INVALIDFUNCTION;
570 * This method is part of the IStream interface.
572 * For streams supported by HGLOBALS, this function does nothing.
573 * This is what the documentation tells us.
575 * See the documentation of IStream for more info.
577 static HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
579 ULARGE_INTEGER libOffset, /* [in] */
580 ULARGE_INTEGER cb, /* [in] */
581 DWORD dwLockType) /* [in] */
587 * This method is part of the IStream interface.
589 * This method returns information about the current
592 * See the documentation of IStream for more info.
594 static HRESULT WINAPI HGLOBALStreamImpl_Stat(
596 STATSTG* pstatstg, /* [out] */
597 DWORD grfStatFlag) /* [in] */
599 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
601 memset(pstatstg, 0, sizeof(STATSTG));
603 pstatstg->pwcsName = NULL;
604 pstatstg->type = STGTY_STREAM;
605 pstatstg->cbSize = This->streamSize;
610 static HRESULT WINAPI HGLOBALStreamImpl_Clone(
612 IStream** ppstm) /* [out] */
614 ULARGE_INTEGER dummy;
615 LARGE_INTEGER offset;
617 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
618 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface,This->deleteOnRelease,(long)This->currentPosition.QuadPart);
619 hr=CreateStreamOnHGlobal(This->supportHandle, FALSE, ppstm);
622 offset.QuadPart=(LONGLONG)This->currentPosition.QuadPart;
623 HGLOBALStreamImpl_Seek(*ppstm,offset,STREAM_SEEK_SET,&dummy);
628 * Virtual function table for the HGLOBALStreamImpl class.
630 static const IStreamVtbl HGLOBALStreamImpl_Vtbl =
632 HGLOBALStreamImpl_QueryInterface,
633 HGLOBALStreamImpl_AddRef,
634 HGLOBALStreamImpl_Release,
635 HGLOBALStreamImpl_Read,
636 HGLOBALStreamImpl_Write,
637 HGLOBALStreamImpl_Seek,
638 HGLOBALStreamImpl_SetSize,
639 HGLOBALStreamImpl_CopyTo,
640 HGLOBALStreamImpl_Commit,
641 HGLOBALStreamImpl_Revert,
642 HGLOBALStreamImpl_LockRegion,
643 HGLOBALStreamImpl_UnlockRegion,
644 HGLOBALStreamImpl_Stat,
645 HGLOBALStreamImpl_Clone
648 /******************************************************************************
649 ** HGLOBALStreamImpl implementation
653 * This is the constructor for the HGLOBALStreamImpl class.
656 * hGlobal - Handle that will support the stream. can be NULL.
657 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
658 * when the IStream object is destroyed.
660 HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
662 BOOL fDeleteOnRelease)
664 HGLOBALStreamImpl* newStream;
666 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl));
671 * Set-up the virtual function table and reference count.
673 newStream->lpVtbl = &HGLOBALStreamImpl_Vtbl;
677 * Initialize the support.
679 newStream->supportHandle = hGlobal;
680 newStream->deleteOnRelease = fDeleteOnRelease;
683 * This method will allocate a handle if one is not supplied.
685 if (!newStream->supportHandle)
687 newStream->supportHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD |
692 * Start the stream at the beginning.
694 newStream->currentPosition.u.HighPart = 0;
695 newStream->currentPosition.u.LowPart = 0;
698 * Initialize the size of the stream to the size of the handle.
700 newStream->streamSize.u.HighPart = 0;
701 newStream->streamSize.u.LowPart = GlobalSize(newStream->supportHandle);
708 /***********************************************************************
709 * CreateStreamOnHGlobal [OLE32.@]
711 HRESULT WINAPI CreateStreamOnHGlobal(
713 BOOL fDeleteOnRelease,
716 HGLOBALStreamImpl* newStream;
718 newStream = HGLOBALStreamImpl_Construct(hGlobal,
723 return IUnknown_QueryInterface((IUnknown*)newStream,
728 return E_OUTOFMEMORY;
731 /***********************************************************************
732 * GetHGlobalFromStream [OLE32.@]
734 HRESULT WINAPI GetHGlobalFromStream(IStream* pstm, HGLOBAL* phglobal)
736 HGLOBALStreamImpl* pStream;
741 pStream = (HGLOBALStreamImpl*) pstm;
744 * Verify that the stream object was created with CreateStreamOnHGlobal.
746 if (pStream->lpVtbl == &HGLOBALStreamImpl_Vtbl)
747 *phglobal = pStream->supportHandle;