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
== NULL
)
254 newStream
->supportHandle
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_NODISCARD
, 0);
258 * Start the stream at the begining.
260 newStream
->currentPosition
.s
.HighPart
= 0;
261 newStream
->currentPosition
.s
.LowPart
= 0;
264 * Initialize the size of the stream to the size of the handle.
266 newStream
->streamSize
.s
.HighPart
= 0;
267 newStream
->streamSize
.s
.LowPart
= GlobalSize(newStream
->supportHandle
);
274 * This is the destructor of the HGLOBALStreamImpl class.
276 * This method will clean-up all the resources used-up by the given HGLOBALStreamImpl
277 * class. The pointer passed-in to this function will be freed and will not
280 void HGLOBALStreamImpl_Destroy(HGLOBALStreamImpl
* This
)
282 TRACE("(%p)\n", This
);
285 * Release the HGlobal if the constructor asked for that.
287 if (This
->deleteOnRelease
)
289 GlobalFree(This
->supportHandle
);
290 This
->supportHandle
=0;
294 * Finally, free the memory used-up by the class.
296 HeapFree(GetProcessHeap(), 0, This
);
300 * This implements the IUnknown method QueryInterface for this
303 HRESULT WINAPI
HGLOBALStreamImpl_QueryInterface(
305 REFIID riid
, /* [in] */
306 void** ppvObject
) /* [iid_is][out] */
308 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
311 * Perform a sanity check on the parameters.
317 * Initialize the return parameter.
322 * Compare the riid with the interface IDs implemented by this object.
324 if (memcmp(&IID_IUnknown
, riid
, sizeof(IID_IUnknown
)) == 0)
326 *ppvObject
= (IStream
*)This
;
328 else if (memcmp(&IID_IStream
, riid
, sizeof(IID_IStream
)) == 0)
330 *ppvObject
= (IStream
*)This
;
334 * Check that we obtained an interface.
337 return E_NOINTERFACE
;
340 * Query Interface always increases the reference count by one when it is
343 HGLOBALStreamImpl_AddRef(iface
);
349 * This implements the IUnknown method AddRef for this
352 ULONG WINAPI
HGLOBALStreamImpl_AddRef(
355 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
363 * This implements the IUnknown method Release for this
366 ULONG WINAPI
HGLOBALStreamImpl_Release(
369 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
378 * If the reference count goes down to 0, perform suicide.
382 HGLOBALStreamImpl_Destroy(This
);
389 * This method is part of the ISequentialStream interface.
391 * If reads a block of information from the stream at the current
392 * position. It then moves the current position at the end of the
395 * See the documentation of ISequentialStream for more info.
397 HRESULT WINAPI
HGLOBALStreamImpl_Read(
399 void* pv
, /* [length_is][size_is][out] */
401 ULONG
* pcbRead
) /* [out] */
403 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
406 ULONG bytesReadBuffer
;
407 ULONG bytesToReadFromBuffer
;
409 TRACE("(%p, %p, %ld, %p)\n", iface
,
413 * If the caller is not interested in the nubmer of bytes read,
414 * we use another buffer to avoid "if" statements in the code.
417 pcbRead
= &bytesReadBuffer
;
420 * Using the known size of the stream, calculate the number of bytes
421 * to read from the block chain
423 bytesToReadFromBuffer
= min( This
->streamSize
.s
.LowPart
- This
->currentPosition
.s
.LowPart
, cb
);
426 * Lock the buffer in position and copy the data.
428 supportBuffer
= GlobalLock(This
->supportHandle
);
430 memcpy(pv
, (char *) supportBuffer
+This
->currentPosition
.s
.LowPart
, bytesToReadFromBuffer
);
433 * Move the current position to the new position
435 This
->currentPosition
.s
.LowPart
+=bytesToReadFromBuffer
;
438 * Return the number of bytes read.
440 *pcbRead
= bytesToReadFromBuffer
;
445 GlobalUnlock(This
->supportHandle
);
448 * The function returns S_OK if the buffer was filled completely
449 * it returns S_FALSE if the end of the stream is reached before the
459 * This method is part of the ISequentialStream interface.
461 * It writes a block of information to the stream at the current
462 * position. It then moves the current position at the end of the
463 * written block. If the stream is too small to fit the block,
464 * the stream is grown to fit.
466 * See the documentation of ISequentialStream for more info.
468 HRESULT WINAPI
HGLOBALStreamImpl_Write(
470 const void* pv
, /* [size_is][in] */
472 ULONG
* pcbWritten
) /* [out] */
474 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
477 ULARGE_INTEGER newSize
;
478 ULONG bytesWritten
= 0;
480 TRACE("(%p, %p, %ld, %p)\n", iface
,
484 * If the caller is not interested in the number of bytes written,
485 * we use another buffer to avoid "if" statements in the code.
488 pcbWritten
= &bytesWritten
;
496 newSize
.s
.HighPart
= 0;
497 newSize
.s
.LowPart
= This
->currentPosition
.s
.LowPart
+ cb
;
501 * Verify if we need to grow the stream
503 if (newSize
.s
.LowPart
> This
->streamSize
.s
.LowPart
)
506 IStream_SetSize(iface
, newSize
);
510 * Lock the buffer in position and copy the data.
512 supportBuffer
= GlobalLock(This
->supportHandle
);
514 memcpy((char *) supportBuffer
+This
->currentPosition
.s
.LowPart
, pv
, cb
);
517 * Move the current position to the new position
519 This
->currentPosition
.s
.LowPart
+=cb
;
522 * Return the number of bytes read.
529 GlobalUnlock(This
->supportHandle
);
535 * This method is part of the IStream interface.
537 * It will move the current stream pointer according to the parameters
540 * See the documentation of IStream for more info.
542 HRESULT WINAPI
HGLOBALStreamImpl_Seek(
544 LARGE_INTEGER dlibMove
, /* [in] */
545 DWORD dwOrigin
, /* [in] */
546 ULARGE_INTEGER
* plibNewPosition
) /* [out] */
548 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
550 ULARGE_INTEGER newPosition
;
552 TRACE("(%p, %ld, %ld, %p)\n", iface
,
553 dlibMove
.s
.LowPart
, dwOrigin
, plibNewPosition
);
556 * The caller is allowed to pass in NULL as the new position return value.
557 * If it happens, we assign it to a dynamic variable to avoid special cases
560 if (plibNewPosition
== 0)
562 plibNewPosition
= &newPosition
;
566 * The file pointer is moved depending on the given "function"
571 case STREAM_SEEK_SET
:
572 plibNewPosition
->s
.HighPart
= 0;
573 plibNewPosition
->s
.LowPart
= 0;
575 case STREAM_SEEK_CUR
:
576 *plibNewPosition
= This
->currentPosition
;
578 case STREAM_SEEK_END
:
579 *plibNewPosition
= This
->streamSize
;
582 return STG_E_INVALIDFUNCTION
;
586 * We don't support files with offsets of 64 bits.
588 assert(dlibMove
.s
.HighPart
== 0);
591 * Check if we end-up before the beginning of the file. That should trigger an
594 if ( (dlibMove
.s
.LowPart
<0) && (plibNewPosition
->s
.LowPart
< (ULONG
)(-dlibMove
.s
.LowPart
)) )
597 * I don't know what error to send there.
603 * Move the actual file pointer
604 * If the file pointer ends-up after the end of the stream, the next Write operation will
605 * make the file larger. This is how it is documented.
607 plibNewPosition
->s
.LowPart
+= dlibMove
.s
.LowPart
;
608 This
->currentPosition
= *plibNewPosition
;
614 * This method is part of the IStream interface.
616 * It will change the size of a stream.
618 * TODO: Switch from small blocks to big blocks and vice versa.
620 * See the documentation of IStream for more info.
622 HRESULT WINAPI
HGLOBALStreamImpl_SetSize(
624 ULARGE_INTEGER libNewSize
) /* [in] */
626 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
628 TRACE("(%p, %ld)\n", iface
, libNewSize
.s
.LowPart
);
633 if (libNewSize
.s
.HighPart
!= 0)
634 return STG_E_INVALIDFUNCTION
;
636 if (This
->streamSize
.s
.LowPart
== libNewSize
.s
.LowPart
)
640 * Re allocate the HGlobal to fit the new size of the stream.
642 This
->supportHandle
= GlobalReAlloc(This
->supportHandle
,
643 libNewSize
.s
.LowPart
,
646 This
->streamSize
.s
.LowPart
= libNewSize
.s
.LowPart
;
652 * This method is part of the IStream interface.
654 * It will copy the 'cb' Bytes to 'pstm' IStream.
656 * See the documentation of IStream for more info.
658 HRESULT WINAPI
HGLOBALStreamImpl_CopyTo(
660 IStream
* pstm
, /* [unique][in] */
661 ULARGE_INTEGER cb
, /* [in] */
662 ULARGE_INTEGER
* pcbRead
, /* [out] */
663 ULARGE_INTEGER
* pcbWritten
) /* [out] */
667 ULONG bytesRead
, bytesWritten
, copySize
;
668 ULARGE_INTEGER totalBytesRead
;
669 ULARGE_INTEGER totalBytesWritten
;
671 TRACE("(%p, %p, %ld, %p, %p)\n", iface
, pstm
,
672 cb
.s
.LowPart
, pcbRead
, pcbWritten
);
678 return STG_E_INVALIDPOINTER
;
680 totalBytesRead
.s
.LowPart
= totalBytesRead
.s
.HighPart
= 0;
681 totalBytesWritten
.s
.LowPart
= totalBytesWritten
.s
.HighPart
= 0;
684 * use stack to store data temporarly
685 * there is surely more performant way of doing it, for now this basic
686 * implementation will do the job
688 while ( cb
.s
.LowPart
> 0 )
690 if ( cb
.s
.LowPart
>= 128 )
693 copySize
= cb
.s
.LowPart
;
695 IStream_Read(iface
, tmpBuffer
, copySize
, &bytesRead
);
697 totalBytesRead
.s
.LowPart
+= bytesRead
;
699 IStream_Write(pstm
, tmpBuffer
, bytesRead
, &bytesWritten
);
701 totalBytesWritten
.s
.LowPart
+= bytesWritten
;
704 * Check that read & write operations were succesfull
706 if (bytesRead
!= bytesWritten
)
708 hr
= STG_E_MEDIUMFULL
;
712 if (bytesRead
!=copySize
)
715 cb
.s
.LowPart
-= bytesRead
;
719 * Update number of bytes read and written
723 pcbRead
->s
.LowPart
= totalBytesRead
.s
.LowPart
;
724 pcbRead
->s
.HighPart
= totalBytesRead
.s
.HighPart
;
729 pcbWritten
->s
.LowPart
= totalBytesWritten
.s
.LowPart
;
730 pcbWritten
->s
.HighPart
= totalBytesWritten
.s
.HighPart
;
736 * This method is part of the IStream interface.
738 * For streams supported by HGLOBALS, this function does nothing.
739 * This is what the documentation tells us.
741 * See the documentation of IStream for more info.
743 HRESULT WINAPI
HGLOBALStreamImpl_Commit(
745 DWORD grfCommitFlags
) /* [in] */
751 * This method is part of the IStream interface.
753 * For streams supported by HGLOBALS, this function does nothing.
754 * This is what the documentation tells us.
756 * See the documentation of IStream for more info.
758 HRESULT WINAPI
HGLOBALStreamImpl_Revert(
765 * This method is part of the IStream interface.
767 * For streams supported by HGLOBALS, this function does nothing.
768 * This is what the documentation tells us.
770 * See the documentation of IStream for more info.
772 HRESULT WINAPI
HGLOBALStreamImpl_LockRegion(
774 ULARGE_INTEGER libOffset
, /* [in] */
775 ULARGE_INTEGER cb
, /* [in] */
776 DWORD dwLockType
) /* [in] */
782 * This method is part of the IStream interface.
784 * For streams supported by HGLOBALS, this function does nothing.
785 * This is what the documentation tells us.
787 * See the documentation of IStream for more info.
789 HRESULT WINAPI
HGLOBALStreamImpl_UnlockRegion(
791 ULARGE_INTEGER libOffset
, /* [in] */
792 ULARGE_INTEGER cb
, /* [in] */
793 DWORD dwLockType
) /* [in] */
799 * This method is part of the IStream interface.
801 * This method returns information about the current
804 * See the documentation of IStream for more info.
806 HRESULT WINAPI
HGLOBALStreamImpl_Stat(
808 STATSTG
* pstatstg
, /* [out] */
809 DWORD grfStatFlag
) /* [in] */
811 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
813 memset(pstatstg
, 0, sizeof(STATSTG
));
815 pstatstg
->pwcsName
= NULL
;
816 pstatstg
->type
= STGTY_STREAM
;
817 pstatstg
->cbSize
= This
->streamSize
;
822 HRESULT WINAPI
HGLOBALStreamImpl_Clone(
824 IStream
** ppstm
) /* [out] */
826 FIXME("not implemented!\n");