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
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(storage
);
47 /****************************************************************************
48 * HGLOBALStreamImpl definition.
50 * This class imlements the IStream inteface and represents a stream
51 * supported by an HGLOBAL pointer.
53 struct HGLOBALStreamImpl
55 ICOM_VFIELD(IStream
); /* Needs to be the first item in the stuct
56 * since we want to cast this in a IStream pointer */
64 * Support for the stream
66 HGLOBAL supportHandle
;
69 * This flag is TRUE if the HGLOBAL is destroyed when the stream
70 * is finally released.
75 * Helper variable that contains the size of the stream
77 ULARGE_INTEGER streamSize
;
80 * This is the current position of the cursor in the stream
82 ULARGE_INTEGER currentPosition
;
85 typedef struct HGLOBALStreamImpl HGLOBALStreamImpl
;
88 * Method definition for the StgStreamImpl class.
90 HGLOBALStreamImpl
* HGLOBALStreamImpl_Construct(
92 BOOL fDeleteOnRelease
);
94 void HGLOBALStreamImpl_Destroy(
95 HGLOBALStreamImpl
* This
);
97 void HGLOBALStreamImpl_OpenBlockChain(
98 HGLOBALStreamImpl
* This
);
100 HRESULT WINAPI
HGLOBALStreamImpl_QueryInterface(
102 REFIID riid
, /* [in] */
103 void** ppvObject
); /* [iid_is][out] */
105 ULONG WINAPI
HGLOBALStreamImpl_AddRef(
108 ULONG WINAPI
HGLOBALStreamImpl_Release(
111 HRESULT WINAPI
HGLOBALStreamImpl_Read(
113 void* pv
, /* [length_is][size_is][out] */
115 ULONG
* pcbRead
); /* [out] */
117 HRESULT WINAPI
HGLOBALStreamImpl_Write(
119 const void* pv
, /* [size_is][in] */
121 ULONG
* pcbWritten
); /* [out] */
123 HRESULT WINAPI
HGLOBALStreamImpl_Seek(
125 LARGE_INTEGER dlibMove
, /* [in] */
126 DWORD dwOrigin
, /* [in] */
127 ULARGE_INTEGER
* plibNewPosition
); /* [out] */
129 HRESULT WINAPI
HGLOBALStreamImpl_SetSize(
131 ULARGE_INTEGER libNewSize
); /* [in] */
133 HRESULT WINAPI
HGLOBALStreamImpl_CopyTo(
135 IStream
* pstm
, /* [unique][in] */
136 ULARGE_INTEGER cb
, /* [in] */
137 ULARGE_INTEGER
* pcbRead
, /* [out] */
138 ULARGE_INTEGER
* pcbWritten
); /* [out] */
140 HRESULT WINAPI
HGLOBALStreamImpl_Commit(
142 DWORD grfCommitFlags
); /* [in] */
144 HRESULT WINAPI
HGLOBALStreamImpl_Revert(
147 HRESULT WINAPI
HGLOBALStreamImpl_LockRegion(
149 ULARGE_INTEGER libOffset
, /* [in] */
150 ULARGE_INTEGER cb
, /* [in] */
151 DWORD dwLockType
); /* [in] */
153 HRESULT WINAPI
HGLOBALStreamImpl_UnlockRegion(
155 ULARGE_INTEGER libOffset
, /* [in] */
156 ULARGE_INTEGER cb
, /* [in] */
157 DWORD dwLockType
); /* [in] */
159 HRESULT WINAPI
HGLOBALStreamImpl_Stat(
161 STATSTG
* pstatstg
, /* [out] */
162 DWORD grfStatFlag
); /* [in] */
164 HRESULT WINAPI
HGLOBALStreamImpl_Clone(
166 IStream
** ppstm
); /* [out] */
170 * Virtual function table for the HGLOBALStreamImpl class.
172 static ICOM_VTABLE(IStream
) HGLOBALStreamImpl_Vtbl
=
174 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
175 HGLOBALStreamImpl_QueryInterface
,
176 HGLOBALStreamImpl_AddRef
,
177 HGLOBALStreamImpl_Release
,
178 HGLOBALStreamImpl_Read
,
179 HGLOBALStreamImpl_Write
,
180 HGLOBALStreamImpl_Seek
,
181 HGLOBALStreamImpl_SetSize
,
182 HGLOBALStreamImpl_CopyTo
,
183 HGLOBALStreamImpl_Commit
,
184 HGLOBALStreamImpl_Revert
,
185 HGLOBALStreamImpl_LockRegion
,
186 HGLOBALStreamImpl_UnlockRegion
,
187 HGLOBALStreamImpl_Stat
,
188 HGLOBALStreamImpl_Clone
191 /***********************************************************************
192 * CreateStreamOnHGlobal [OLE32.@]
194 HRESULT WINAPI
CreateStreamOnHGlobal(
196 BOOL fDeleteOnRelease
,
199 HGLOBALStreamImpl
* newStream
;
201 newStream
= HGLOBALStreamImpl_Construct(hGlobal
,
206 return IUnknown_QueryInterface((IUnknown
*)newStream
,
211 return E_OUTOFMEMORY
;
214 /***********************************************************************
215 * GetHGlobalFromStream [OLE32.@]
217 HRESULT WINAPI
GetHGlobalFromStream(IStream
* pstm
, HGLOBAL
* phglobal
)
219 HGLOBALStreamImpl
* pStream
;
224 pStream
= (HGLOBALStreamImpl
*) pstm
;
227 * Verify that the stream object was created with CreateStreamOnHGlobal.
229 if (pStream
->lpVtbl
== &HGLOBALStreamImpl_Vtbl
)
230 *phglobal
= pStream
->supportHandle
;
240 /******************************************************************************
241 ** HGLOBALStreamImpl implementation
245 * This is the constructor for the HGLOBALStreamImpl class.
248 * hGlobal - Handle that will support the stream. can be NULL.
249 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
250 * when the IStream object is destroyed.
252 HGLOBALStreamImpl
* HGLOBALStreamImpl_Construct(
254 BOOL fDeleteOnRelease
)
256 HGLOBALStreamImpl
* newStream
;
258 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl
));
263 * Set-up the virtual function table and reference count.
265 newStream
->lpVtbl
= &HGLOBALStreamImpl_Vtbl
;
269 * Initialize the support.
271 newStream
->supportHandle
= hGlobal
;
272 newStream
->deleteOnRelease
= fDeleteOnRelease
;
275 * This method will allocate a handle if one is not supplied.
277 if (!newStream
->supportHandle
)
279 newStream
->supportHandle
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_NODISCARD
|
284 * Start the stream at the beginning.
286 newStream
->currentPosition
.s
.HighPart
= 0;
287 newStream
->currentPosition
.s
.LowPart
= 0;
290 * Initialize the size of the stream to the size of the handle.
292 newStream
->streamSize
.s
.HighPart
= 0;
293 newStream
->streamSize
.s
.LowPart
= GlobalSize(newStream
->supportHandle
);
300 * This is the destructor of the HGLOBALStreamImpl class.
302 * This method will clean-up all the resources used-up by the given HGLOBALStreamImpl
303 * class. The pointer passed-in to this function will be freed and will not
306 void HGLOBALStreamImpl_Destroy(HGLOBALStreamImpl
* This
)
308 TRACE("(%p)\n", This
);
311 * Release the HGlobal if the constructor asked for that.
313 if (This
->deleteOnRelease
)
315 GlobalFree(This
->supportHandle
);
316 This
->supportHandle
=0;
320 * Finally, free the memory used-up by the class.
322 HeapFree(GetProcessHeap(), 0, This
);
326 * This implements the IUnknown method QueryInterface for this
329 HRESULT WINAPI
HGLOBALStreamImpl_QueryInterface(
331 REFIID riid
, /* [in] */
332 void** ppvObject
) /* [iid_is][out] */
334 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
337 * Perform a sanity check on the parameters.
343 * Initialize the return parameter.
348 * Compare the riid with the interface IDs implemented by this object.
350 if (memcmp(&IID_IUnknown
, riid
, sizeof(IID_IUnknown
)) == 0)
352 *ppvObject
= (IStream
*)This
;
354 else if (memcmp(&IID_IStream
, riid
, sizeof(IID_IStream
)) == 0)
356 *ppvObject
= (IStream
*)This
;
360 * Check that we obtained an interface.
363 return E_NOINTERFACE
;
366 * Query Interface always increases the reference count by one when it is
369 HGLOBALStreamImpl_AddRef(iface
);
375 * This implements the IUnknown method AddRef for this
378 ULONG WINAPI
HGLOBALStreamImpl_AddRef(
381 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
389 * This implements the IUnknown method Release for this
392 ULONG WINAPI
HGLOBALStreamImpl_Release(
395 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
404 * If the reference count goes down to 0, perform suicide.
408 HGLOBALStreamImpl_Destroy(This
);
415 * This method is part of the ISequentialStream interface.
417 * If reads a block of information from the stream at the current
418 * position. It then moves the current position at the end of the
421 * See the documentation of ISequentialStream for more info.
423 HRESULT WINAPI
HGLOBALStreamImpl_Read(
425 void* pv
, /* [length_is][size_is][out] */
427 ULONG
* pcbRead
) /* [out] */
429 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
432 ULONG bytesReadBuffer
;
433 ULONG bytesToReadFromBuffer
;
435 TRACE("(%p, %p, %ld, %p)\n", iface
,
439 * If the caller is not interested in the nubmer of bytes read,
440 * we use another buffer to avoid "if" statements in the code.
443 pcbRead
= &bytesReadBuffer
;
446 * Using the known size of the stream, calculate the number of bytes
447 * to read from the block chain
449 bytesToReadFromBuffer
= min( This
->streamSize
.s
.LowPart
- This
->currentPosition
.s
.LowPart
, cb
);
452 * Lock the buffer in position and copy the data.
454 supportBuffer
= GlobalLock(This
->supportHandle
);
456 memcpy(pv
, (char *) supportBuffer
+This
->currentPosition
.s
.LowPart
, bytesToReadFromBuffer
);
459 * Move the current position to the new position
461 This
->currentPosition
.s
.LowPart
+=bytesToReadFromBuffer
;
464 * Return the number of bytes read.
466 *pcbRead
= bytesToReadFromBuffer
;
471 GlobalUnlock(This
->supportHandle
);
474 * The function returns S_OK if the buffer was filled completely
475 * it returns S_FALSE if the end of the stream is reached before the
485 * This method is part of the ISequentialStream interface.
487 * It writes a block of information to the stream at the current
488 * position. It then moves the current position at the end of the
489 * written block. If the stream is too small to fit the block,
490 * the stream is grown to fit.
492 * See the documentation of ISequentialStream for more info.
494 HRESULT WINAPI
HGLOBALStreamImpl_Write(
496 const void* pv
, /* [size_is][in] */
498 ULONG
* pcbWritten
) /* [out] */
500 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
503 ULARGE_INTEGER newSize
;
504 ULONG bytesWritten
= 0;
506 TRACE("(%p, %p, %ld, %p)\n", iface
,
510 * If the caller is not interested in the number of bytes written,
511 * we use another buffer to avoid "if" statements in the code.
514 pcbWritten
= &bytesWritten
;
522 newSize
.s
.HighPart
= 0;
523 newSize
.s
.LowPart
= This
->currentPosition
.s
.LowPart
+ cb
;
527 * Verify if we need to grow the stream
529 if (newSize
.s
.LowPart
> This
->streamSize
.s
.LowPart
)
532 IStream_SetSize(iface
, newSize
);
536 * Lock the buffer in position and copy the data.
538 supportBuffer
= GlobalLock(This
->supportHandle
);
540 memcpy((char *) supportBuffer
+This
->currentPosition
.s
.LowPart
, pv
, cb
);
543 * Move the current position to the new position
545 This
->currentPosition
.s
.LowPart
+=cb
;
548 * Return the number of bytes read.
555 GlobalUnlock(This
->supportHandle
);
561 * This method is part of the IStream interface.
563 * It will move the current stream pointer according to the parameters
566 * See the documentation of IStream for more info.
568 HRESULT WINAPI
HGLOBALStreamImpl_Seek(
570 LARGE_INTEGER dlibMove
, /* [in] */
571 DWORD dwOrigin
, /* [in] */
572 ULARGE_INTEGER
* plibNewPosition
) /* [out] */
574 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
576 ULARGE_INTEGER newPosition
;
578 TRACE("(%p, %lx%08lx, %ld, %p)\n", iface
, dlibMove
.s
.HighPart
,
579 dlibMove
.s
.LowPart
, dwOrigin
, plibNewPosition
);
582 * The file pointer is moved depending on the given "function"
587 case STREAM_SEEK_SET
:
588 newPosition
.s
.HighPart
= 0;
589 newPosition
.s
.LowPart
= 0;
591 case STREAM_SEEK_CUR
:
592 newPosition
= This
->currentPosition
;
594 case STREAM_SEEK_END
:
595 newPosition
= This
->streamSize
;
598 return STG_E_INVALIDFUNCTION
;
602 * Move the actual file pointer
603 * If the file pointer ends-up after the end of the stream, the next Write operation will
604 * make the file larger. This is how it is documented.
606 newPosition
.QuadPart
= RtlLargeIntegerAdd(newPosition
.QuadPart
, dlibMove
.QuadPart
);
607 if (newPosition
.QuadPart
< 0) return STG_E_INVALIDFUNCTION
;
609 if (plibNewPosition
) *plibNewPosition
= newPosition
;
610 This
->currentPosition
= newPosition
;
616 * This method is part of the IStream interface.
618 * It will change the size of a stream.
620 * TODO: Switch from small blocks to big blocks and vice versa.
622 * See the documentation of IStream for more info.
624 HRESULT WINAPI
HGLOBALStreamImpl_SetSize(
626 ULARGE_INTEGER libNewSize
) /* [in] */
628 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
629 HGLOBAL supportHandle
;
631 TRACE("(%p, %ld)\n", iface
, libNewSize
.s
.LowPart
);
636 if (libNewSize
.s
.HighPart
!= 0)
637 return STG_E_INVALIDFUNCTION
;
639 if (This
->streamSize
.s
.LowPart
== libNewSize
.s
.LowPart
)
643 * Re allocate the HGlobal to fit the new size of the stream.
645 supportHandle
= GlobalReAlloc(This
->supportHandle
, libNewSize
.s
.LowPart
, 0);
647 if (supportHandle
== 0)
648 return STG_E_MEDIUMFULL
;
650 This
->supportHandle
= supportHandle
;
651 This
->streamSize
.s
.LowPart
= libNewSize
.s
.LowPart
;
657 * This method is part of the IStream interface.
659 * It will copy the 'cb' Bytes to 'pstm' IStream.
661 * See the documentation of IStream for more info.
663 HRESULT WINAPI
HGLOBALStreamImpl_CopyTo(
665 IStream
* pstm
, /* [unique][in] */
666 ULARGE_INTEGER cb
, /* [in] */
667 ULARGE_INTEGER
* pcbRead
, /* [out] */
668 ULARGE_INTEGER
* pcbWritten
) /* [out] */
672 ULONG bytesRead
, bytesWritten
, copySize
;
673 ULARGE_INTEGER totalBytesRead
;
674 ULARGE_INTEGER totalBytesWritten
;
676 TRACE("(%p, %p, %ld, %p, %p)\n", iface
, pstm
,
677 cb
.s
.LowPart
, pcbRead
, pcbWritten
);
683 return STG_E_INVALIDPOINTER
;
685 totalBytesRead
.s
.LowPart
= totalBytesRead
.s
.HighPart
= 0;
686 totalBytesWritten
.s
.LowPart
= totalBytesWritten
.s
.HighPart
= 0;
689 * use stack to store data temporarly
690 * there is surely more performant way of doing it, for now this basic
691 * implementation will do the job
693 while ( cb
.s
.LowPart
> 0 )
695 if ( cb
.s
.LowPart
>= 128 )
698 copySize
= cb
.s
.LowPart
;
700 IStream_Read(iface
, tmpBuffer
, copySize
, &bytesRead
);
702 totalBytesRead
.s
.LowPart
+= bytesRead
;
704 IStream_Write(pstm
, tmpBuffer
, bytesRead
, &bytesWritten
);
706 totalBytesWritten
.s
.LowPart
+= bytesWritten
;
709 * Check that read & write operations were succesfull
711 if (bytesRead
!= bytesWritten
)
713 hr
= STG_E_MEDIUMFULL
;
717 if (bytesRead
!=copySize
)
720 cb
.s
.LowPart
-= bytesRead
;
724 * Update number of bytes read and written
728 pcbRead
->s
.LowPart
= totalBytesRead
.s
.LowPart
;
729 pcbRead
->s
.HighPart
= totalBytesRead
.s
.HighPart
;
734 pcbWritten
->s
.LowPart
= totalBytesWritten
.s
.LowPart
;
735 pcbWritten
->s
.HighPart
= totalBytesWritten
.s
.HighPart
;
741 * This method is part of the IStream interface.
743 * For streams supported by HGLOBALS, this function does nothing.
744 * This is what the documentation tells us.
746 * See the documentation of IStream for more info.
748 HRESULT WINAPI
HGLOBALStreamImpl_Commit(
750 DWORD grfCommitFlags
) /* [in] */
756 * This method is part of the IStream interface.
758 * For streams supported by HGLOBALS, this function does nothing.
759 * This is what the documentation tells us.
761 * See the documentation of IStream for more info.
763 HRESULT WINAPI
HGLOBALStreamImpl_Revert(
770 * This method is part of the IStream interface.
772 * For streams supported by HGLOBALS, this function does nothing.
773 * This is what the documentation tells us.
775 * See the documentation of IStream for more info.
777 HRESULT WINAPI
HGLOBALStreamImpl_LockRegion(
779 ULARGE_INTEGER libOffset
, /* [in] */
780 ULARGE_INTEGER cb
, /* [in] */
781 DWORD dwLockType
) /* [in] */
787 * This method is part of the IStream interface.
789 * For streams supported by HGLOBALS, this function does nothing.
790 * This is what the documentation tells us.
792 * See the documentation of IStream for more info.
794 HRESULT WINAPI
HGLOBALStreamImpl_UnlockRegion(
796 ULARGE_INTEGER libOffset
, /* [in] */
797 ULARGE_INTEGER cb
, /* [in] */
798 DWORD dwLockType
) /* [in] */
804 * This method is part of the IStream interface.
806 * This method returns information about the current
809 * See the documentation of IStream for more info.
811 HRESULT WINAPI
HGLOBALStreamImpl_Stat(
813 STATSTG
* pstatstg
, /* [out] */
814 DWORD grfStatFlag
) /* [in] */
816 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
818 memset(pstatstg
, 0, sizeof(STATSTG
));
820 pstatstg
->pwcsName
= NULL
;
821 pstatstg
->type
= STGTY_STREAM
;
822 pstatstg
->cbSize
= This
->streamSize
;
827 HRESULT WINAPI
HGLOBALStreamImpl_Clone(
829 IStream
** ppstm
) /* [out] */
831 ULARGE_INTEGER dummy
;
832 LARGE_INTEGER offset
;
834 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
835 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface
,This
->deleteOnRelease
,(long)This
->currentPosition
.QuadPart
);
836 hr
=CreateStreamOnHGlobal(This
->supportHandle
, FALSE
, ppstm
);
839 offset
.QuadPart
=(LONGLONG
)This
->currentPosition
.QuadPart
;
840 HGLOBALStreamImpl_Seek(*ppstm
,offset
,STREAM_SEEK_SET
,&dummy
);