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
16 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(storage
);
22 /****************************************************************************
23 * HGLOBALStreamImpl definition.
25 * This class imlements the IStream inteface and represents a stream
26 * supported by an HGLOBAL pointer.
28 struct HGLOBALStreamImpl
30 ICOM_VFIELD(IStream
); /* Needs to be the first item in the stuct
31 * since we want to cast this in a IStream pointer */
39 * Support for the stream
41 HGLOBAL supportHandle
;
44 * This flag is TRUE if the HGLOBAL is destroyed when the stream
45 * is finally released.
50 * Helper variable that contains the size of the stream
52 ULARGE_INTEGER streamSize
;
55 * This is the current position of the cursor in the stream
57 ULARGE_INTEGER currentPosition
;
60 typedef struct HGLOBALStreamImpl HGLOBALStreamImpl
;
63 * Method definition for the StgStreamImpl class.
65 HGLOBALStreamImpl
* HGLOBALStreamImpl_Construct(
67 BOOL fDeleteOnRelease
);
69 void HGLOBALStreamImpl_Destroy(
70 HGLOBALStreamImpl
* This
);
72 void HGLOBALStreamImpl_OpenBlockChain(
73 HGLOBALStreamImpl
* This
);
75 HRESULT WINAPI
HGLOBALStreamImpl_QueryInterface(
77 REFIID riid
, /* [in] */
78 void** ppvObject
); /* [iid_is][out] */
80 ULONG WINAPI
HGLOBALStreamImpl_AddRef(
83 ULONG WINAPI
HGLOBALStreamImpl_Release(
86 HRESULT WINAPI
HGLOBALStreamImpl_Read(
88 void* pv
, /* [length_is][size_is][out] */
90 ULONG
* pcbRead
); /* [out] */
92 HRESULT WINAPI
HGLOBALStreamImpl_Write(
94 const void* pv
, /* [size_is][in] */
96 ULONG
* pcbWritten
); /* [out] */
98 HRESULT WINAPI
HGLOBALStreamImpl_Seek(
100 LARGE_INTEGER dlibMove
, /* [in] */
101 DWORD dwOrigin
, /* [in] */
102 ULARGE_INTEGER
* plibNewPosition
); /* [out] */
104 HRESULT WINAPI
HGLOBALStreamImpl_SetSize(
106 ULARGE_INTEGER libNewSize
); /* [in] */
108 HRESULT WINAPI
HGLOBALStreamImpl_CopyTo(
110 IStream
* pstm
, /* [unique][in] */
111 ULARGE_INTEGER cb
, /* [in] */
112 ULARGE_INTEGER
* pcbRead
, /* [out] */
113 ULARGE_INTEGER
* pcbWritten
); /* [out] */
115 HRESULT WINAPI
HGLOBALStreamImpl_Commit(
117 DWORD grfCommitFlags
); /* [in] */
119 HRESULT WINAPI
HGLOBALStreamImpl_Revert(
122 HRESULT WINAPI
HGLOBALStreamImpl_LockRegion(
124 ULARGE_INTEGER libOffset
, /* [in] */
125 ULARGE_INTEGER cb
, /* [in] */
126 DWORD dwLockType
); /* [in] */
128 HRESULT WINAPI
HGLOBALStreamImpl_UnlockRegion(
130 ULARGE_INTEGER libOffset
, /* [in] */
131 ULARGE_INTEGER cb
, /* [in] */
132 DWORD dwLockType
); /* [in] */
134 HRESULT WINAPI
HGLOBALStreamImpl_Stat(
136 STATSTG
* pstatstg
, /* [out] */
137 DWORD grfStatFlag
); /* [in] */
139 HRESULT WINAPI
HGLOBALStreamImpl_Clone(
141 IStream
** ppstm
); /* [out] */
145 * Virtual function table for the HGLOBALStreamImpl class.
147 static ICOM_VTABLE(IStream
) HGLOBALStreamImpl_Vtbl
=
149 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
150 HGLOBALStreamImpl_QueryInterface
,
151 HGLOBALStreamImpl_AddRef
,
152 HGLOBALStreamImpl_Release
,
153 HGLOBALStreamImpl_Read
,
154 HGLOBALStreamImpl_Write
,
155 HGLOBALStreamImpl_Seek
,
156 HGLOBALStreamImpl_SetSize
,
157 HGLOBALStreamImpl_CopyTo
,
158 HGLOBALStreamImpl_Commit
,
159 HGLOBALStreamImpl_Revert
,
160 HGLOBALStreamImpl_LockRegion
,
161 HGLOBALStreamImpl_UnlockRegion
,
162 HGLOBALStreamImpl_Stat
,
163 HGLOBALStreamImpl_Clone
166 /***********************************************************************
167 * CreateStreamOnHGlobal [OLE32.61]
169 HRESULT WINAPI
CreateStreamOnHGlobal(
171 BOOL fDeleteOnRelease
,
174 HGLOBALStreamImpl
* newStream
;
176 newStream
= HGLOBALStreamImpl_Construct(hGlobal
,
181 return IUnknown_QueryInterface((IUnknown
*)newStream
,
186 return E_OUTOFMEMORY
;
189 /***********************************************************************
190 * GetHGlobalFromStream [OLE32.71]
192 HRESULT WINAPI
GetHGlobalFromStream(IStream
* pstm
, HGLOBAL
* phglobal
)
194 HGLOBALStreamImpl
* pStream
;
199 pStream
= (HGLOBALStreamImpl
*) pstm
;
202 * Verify that the stream object was created with CreateStreamOnHGlobal.
204 if (ICOM_VTBL(pStream
) == &HGLOBALStreamImpl_Vtbl
)
205 *phglobal
= pStream
->supportHandle
;
215 /******************************************************************************
216 ** HGLOBALStreamImpl implementation
220 * This is the constructor for the HGLOBALStreamImpl class.
223 * hGlobal - Handle that will support the stream. can be NULL.
224 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
225 * when the IStream object is destroyed.
227 HGLOBALStreamImpl
* HGLOBALStreamImpl_Construct(
229 BOOL fDeleteOnRelease
)
231 HGLOBALStreamImpl
* newStream
;
233 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl
));
238 * Set-up the virtual function table and reference count.
240 ICOM_VTBL(newStream
) = &HGLOBALStreamImpl_Vtbl
;
244 * Initialize the support.
246 newStream
->supportHandle
= hGlobal
;
247 newStream
->deleteOnRelease
= fDeleteOnRelease
;
250 * This method will allocate a handle if one is not supplied.
252 if (!newStream
->supportHandle
)
254 newStream
->supportHandle
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_NODISCARD
|
259 * Start the stream at the begining.
261 newStream
->currentPosition
.s
.HighPart
= 0;
262 newStream
->currentPosition
.s
.LowPart
= 0;
265 * Initialize the size of the stream to the size of the handle.
267 newStream
->streamSize
.s
.HighPart
= 0;
268 newStream
->streamSize
.s
.LowPart
= GlobalSize(newStream
->supportHandle
);
275 * This is the destructor of the HGLOBALStreamImpl class.
277 * This method will clean-up all the resources used-up by the given HGLOBALStreamImpl
278 * class. The pointer passed-in to this function will be freed and will not
281 void HGLOBALStreamImpl_Destroy(HGLOBALStreamImpl
* This
)
283 TRACE("(%p)\n", This
);
286 * Release the HGlobal if the constructor asked for that.
288 if (This
->deleteOnRelease
)
290 GlobalFree(This
->supportHandle
);
291 This
->supportHandle
=0;
295 * Finally, free the memory used-up by the class.
297 HeapFree(GetProcessHeap(), 0, This
);
301 * This implements the IUnknown method QueryInterface for this
304 HRESULT WINAPI
HGLOBALStreamImpl_QueryInterface(
306 REFIID riid
, /* [in] */
307 void** ppvObject
) /* [iid_is][out] */
309 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
312 * Perform a sanity check on the parameters.
318 * Initialize the return parameter.
323 * Compare the riid with the interface IDs implemented by this object.
325 if (memcmp(&IID_IUnknown
, riid
, sizeof(IID_IUnknown
)) == 0)
327 *ppvObject
= (IStream
*)This
;
329 else if (memcmp(&IID_IStream
, riid
, sizeof(IID_IStream
)) == 0)
331 *ppvObject
= (IStream
*)This
;
335 * Check that we obtained an interface.
338 return E_NOINTERFACE
;
341 * Query Interface always increases the reference count by one when it is
344 HGLOBALStreamImpl_AddRef(iface
);
350 * This implements the IUnknown method AddRef for this
353 ULONG WINAPI
HGLOBALStreamImpl_AddRef(
356 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
364 * This implements the IUnknown method Release for this
367 ULONG WINAPI
HGLOBALStreamImpl_Release(
370 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
379 * If the reference count goes down to 0, perform suicide.
383 HGLOBALStreamImpl_Destroy(This
);
390 * This method is part of the ISequentialStream interface.
392 * If reads a block of information from the stream at the current
393 * position. It then moves the current position at the end of the
396 * See the documentation of ISequentialStream for more info.
398 HRESULT WINAPI
HGLOBALStreamImpl_Read(
400 void* pv
, /* [length_is][size_is][out] */
402 ULONG
* pcbRead
) /* [out] */
404 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
407 ULONG bytesReadBuffer
;
408 ULONG bytesToReadFromBuffer
;
410 TRACE("(%p, %p, %ld, %p)\n", iface
,
414 * If the caller is not interested in the nubmer of bytes read,
415 * we use another buffer to avoid "if" statements in the code.
418 pcbRead
= &bytesReadBuffer
;
421 * Using the known size of the stream, calculate the number of bytes
422 * to read from the block chain
424 bytesToReadFromBuffer
= min( This
->streamSize
.s
.LowPart
- This
->currentPosition
.s
.LowPart
, cb
);
427 * Lock the buffer in position and copy the data.
429 supportBuffer
= GlobalLock(This
->supportHandle
);
431 memcpy(pv
, (char *) supportBuffer
+This
->currentPosition
.s
.LowPart
, bytesToReadFromBuffer
);
434 * Move the current position to the new position
436 This
->currentPosition
.s
.LowPart
+=bytesToReadFromBuffer
;
439 * Return the number of bytes read.
441 *pcbRead
= bytesToReadFromBuffer
;
446 GlobalUnlock(This
->supportHandle
);
449 * The function returns S_OK if the buffer was filled completely
450 * it returns S_FALSE if the end of the stream is reached before the
460 * This method is part of the ISequentialStream interface.
462 * It writes a block of information to the stream at the current
463 * position. It then moves the current position at the end of the
464 * written block. If the stream is too small to fit the block,
465 * the stream is grown to fit.
467 * See the documentation of ISequentialStream for more info.
469 HRESULT WINAPI
HGLOBALStreamImpl_Write(
471 const void* pv
, /* [size_is][in] */
473 ULONG
* pcbWritten
) /* [out] */
475 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
478 ULARGE_INTEGER newSize
;
479 ULONG bytesWritten
= 0;
481 TRACE("(%p, %p, %ld, %p)\n", iface
,
485 * If the caller is not interested in the number of bytes written,
486 * we use another buffer to avoid "if" statements in the code.
489 pcbWritten
= &bytesWritten
;
497 newSize
.s
.HighPart
= 0;
498 newSize
.s
.LowPart
= This
->currentPosition
.s
.LowPart
+ cb
;
502 * Verify if we need to grow the stream
504 if (newSize
.s
.LowPart
> This
->streamSize
.s
.LowPart
)
507 IStream_SetSize(iface
, newSize
);
511 * Lock the buffer in position and copy the data.
513 supportBuffer
= GlobalLock(This
->supportHandle
);
515 memcpy((char *) supportBuffer
+This
->currentPosition
.s
.LowPart
, pv
, cb
);
518 * Move the current position to the new position
520 This
->currentPosition
.s
.LowPart
+=cb
;
523 * Return the number of bytes read.
530 GlobalUnlock(This
->supportHandle
);
536 * This method is part of the IStream interface.
538 * It will move the current stream pointer according to the parameters
541 * See the documentation of IStream for more info.
543 HRESULT WINAPI
HGLOBALStreamImpl_Seek(
545 LARGE_INTEGER dlibMove
, /* [in] */
546 DWORD dwOrigin
, /* [in] */
547 ULARGE_INTEGER
* plibNewPosition
) /* [out] */
549 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
551 ULARGE_INTEGER newPosition
;
553 TRACE("(%p, %ld, %ld, %p)\n", iface
,
554 dlibMove
.s
.LowPart
, dwOrigin
, plibNewPosition
);
557 * The caller is allowed to pass in NULL as the new position return value.
558 * If it happens, we assign it to a dynamic variable to avoid special cases
561 if (plibNewPosition
== 0)
563 plibNewPosition
= &newPosition
;
567 * The file pointer is moved depending on the given "function"
572 case STREAM_SEEK_SET
:
573 plibNewPosition
->s
.HighPart
= 0;
574 plibNewPosition
->s
.LowPart
= 0;
576 case STREAM_SEEK_CUR
:
577 *plibNewPosition
= This
->currentPosition
;
579 case STREAM_SEEK_END
:
580 *plibNewPosition
= This
->streamSize
;
583 return STG_E_INVALIDFUNCTION
;
587 * We don't support files with offsets of 64 bits.
589 assert(dlibMove
.s
.HighPart
== 0);
592 * Check if we end-up before the beginning of the file. That should trigger an
595 if ( (dlibMove
.s
.LowPart
<0) && (plibNewPosition
->s
.LowPart
< (ULONG
)(-dlibMove
.s
.LowPart
)) )
598 * I don't know what error to send there.
604 * Move the actual file pointer
605 * If the file pointer ends-up after the end of the stream, the next Write operation will
606 * make the file larger. This is how it is documented.
608 plibNewPosition
->s
.LowPart
+= dlibMove
.s
.LowPart
;
609 This
->currentPosition
= *plibNewPosition
;
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 FIXME("not implemented!\n");