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 stuct
58 * since we want to cast this in a 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 * Method definition for the StgStreamImpl class.
92 HGLOBALStreamImpl
* HGLOBALStreamImpl_Construct(
94 BOOL fDeleteOnRelease
);
96 void HGLOBALStreamImpl_Destroy(
97 HGLOBALStreamImpl
* This
);
99 void HGLOBALStreamImpl_OpenBlockChain(
100 HGLOBALStreamImpl
* This
);
102 HRESULT WINAPI
HGLOBALStreamImpl_QueryInterface(
104 REFIID riid
, /* [in] */
105 void** ppvObject
); /* [iid_is][out] */
107 ULONG WINAPI
HGLOBALStreamImpl_AddRef(
110 ULONG WINAPI
HGLOBALStreamImpl_Release(
113 HRESULT WINAPI
HGLOBALStreamImpl_Read(
115 void* pv
, /* [length_is][size_is][out] */
117 ULONG
* pcbRead
); /* [out] */
119 HRESULT WINAPI
HGLOBALStreamImpl_Write(
121 const void* pv
, /* [size_is][in] */
123 ULONG
* pcbWritten
); /* [out] */
125 HRESULT WINAPI
HGLOBALStreamImpl_Seek(
127 LARGE_INTEGER dlibMove
, /* [in] */
128 DWORD dwOrigin
, /* [in] */
129 ULARGE_INTEGER
* plibNewPosition
); /* [out] */
131 HRESULT WINAPI
HGLOBALStreamImpl_SetSize(
133 ULARGE_INTEGER libNewSize
); /* [in] */
135 HRESULT WINAPI
HGLOBALStreamImpl_CopyTo(
137 IStream
* pstm
, /* [unique][in] */
138 ULARGE_INTEGER cb
, /* [in] */
139 ULARGE_INTEGER
* pcbRead
, /* [out] */
140 ULARGE_INTEGER
* pcbWritten
); /* [out] */
142 HRESULT WINAPI
HGLOBALStreamImpl_Commit(
144 DWORD grfCommitFlags
); /* [in] */
146 HRESULT WINAPI
HGLOBALStreamImpl_Revert(
149 HRESULT WINAPI
HGLOBALStreamImpl_LockRegion(
151 ULARGE_INTEGER libOffset
, /* [in] */
152 ULARGE_INTEGER cb
, /* [in] */
153 DWORD dwLockType
); /* [in] */
155 HRESULT WINAPI
HGLOBALStreamImpl_UnlockRegion(
157 ULARGE_INTEGER libOffset
, /* [in] */
158 ULARGE_INTEGER cb
, /* [in] */
159 DWORD dwLockType
); /* [in] */
161 HRESULT WINAPI
HGLOBALStreamImpl_Stat(
163 STATSTG
* pstatstg
, /* [out] */
164 DWORD grfStatFlag
); /* [in] */
166 HRESULT WINAPI
HGLOBALStreamImpl_Clone(
168 IStream
** ppstm
); /* [out] */
172 * Virtual function table for the HGLOBALStreamImpl class.
174 static IStreamVtbl HGLOBALStreamImpl_Vtbl
=
176 HGLOBALStreamImpl_QueryInterface
,
177 HGLOBALStreamImpl_AddRef
,
178 HGLOBALStreamImpl_Release
,
179 HGLOBALStreamImpl_Read
,
180 HGLOBALStreamImpl_Write
,
181 HGLOBALStreamImpl_Seek
,
182 HGLOBALStreamImpl_SetSize
,
183 HGLOBALStreamImpl_CopyTo
,
184 HGLOBALStreamImpl_Commit
,
185 HGLOBALStreamImpl_Revert
,
186 HGLOBALStreamImpl_LockRegion
,
187 HGLOBALStreamImpl_UnlockRegion
,
188 HGLOBALStreamImpl_Stat
,
189 HGLOBALStreamImpl_Clone
192 /***********************************************************************
193 * CreateStreamOnHGlobal [OLE32.@]
195 HRESULT WINAPI
CreateStreamOnHGlobal(
197 BOOL fDeleteOnRelease
,
200 HGLOBALStreamImpl
* newStream
;
202 newStream
= HGLOBALStreamImpl_Construct(hGlobal
,
207 return IUnknown_QueryInterface((IUnknown
*)newStream
,
212 return E_OUTOFMEMORY
;
215 /***********************************************************************
216 * GetHGlobalFromStream [OLE32.@]
218 HRESULT WINAPI
GetHGlobalFromStream(IStream
* pstm
, HGLOBAL
* phglobal
)
220 HGLOBALStreamImpl
* pStream
;
225 pStream
= (HGLOBALStreamImpl
*) pstm
;
228 * Verify that the stream object was created with CreateStreamOnHGlobal.
230 if (pStream
->lpVtbl
== &HGLOBALStreamImpl_Vtbl
)
231 *phglobal
= pStream
->supportHandle
;
241 /******************************************************************************
242 ** HGLOBALStreamImpl implementation
246 * This is the constructor for the HGLOBALStreamImpl class.
249 * hGlobal - Handle that will support the stream. can be NULL.
250 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
251 * when the IStream object is destroyed.
253 HGLOBALStreamImpl
* HGLOBALStreamImpl_Construct(
255 BOOL fDeleteOnRelease
)
257 HGLOBALStreamImpl
* newStream
;
259 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl
));
264 * Set-up the virtual function table and reference count.
266 newStream
->lpVtbl
= &HGLOBALStreamImpl_Vtbl
;
270 * Initialize the support.
272 newStream
->supportHandle
= hGlobal
;
273 newStream
->deleteOnRelease
= fDeleteOnRelease
;
276 * This method will allocate a handle if one is not supplied.
278 if (!newStream
->supportHandle
)
280 newStream
->supportHandle
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_NODISCARD
|
285 * Start the stream at the beginning.
287 newStream
->currentPosition
.u
.HighPart
= 0;
288 newStream
->currentPosition
.u
.LowPart
= 0;
291 * Initialize the size of the stream to the size of the handle.
293 newStream
->streamSize
.u
.HighPart
= 0;
294 newStream
->streamSize
.u
.LowPart
= GlobalSize(newStream
->supportHandle
);
301 * This is the destructor of the HGLOBALStreamImpl class.
303 * This method will clean-up all the resources used-up by the given HGLOBALStreamImpl
304 * class. The pointer passed-in to this function will be freed and will not
307 void HGLOBALStreamImpl_Destroy(HGLOBALStreamImpl
* This
)
309 TRACE("(%p)\n", This
);
312 * Release the HGlobal if the constructor asked for that.
314 if (This
->deleteOnRelease
)
316 GlobalFree(This
->supportHandle
);
317 This
->supportHandle
=0;
321 * Finally, free the memory used-up by the class.
323 HeapFree(GetProcessHeap(), 0, This
);
327 * This implements the IUnknown method QueryInterface for this
330 HRESULT WINAPI
HGLOBALStreamImpl_QueryInterface(
332 REFIID riid
, /* [in] */
333 void** ppvObject
) /* [iid_is][out] */
335 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
338 * Perform a sanity check on the parameters.
344 * Initialize the return parameter.
349 * Compare the riid with the interface IDs implemented by this object.
351 if (memcmp(&IID_IUnknown
, riid
, sizeof(IID_IUnknown
)) == 0)
353 *ppvObject
= (IStream
*)This
;
355 else if (memcmp(&IID_IStream
, riid
, sizeof(IID_IStream
)) == 0)
357 *ppvObject
= (IStream
*)This
;
361 * Check that we obtained an interface.
364 return E_NOINTERFACE
;
367 * Query Interface always increases the reference count by one when it is
370 HGLOBALStreamImpl_AddRef(iface
);
376 * This implements the IUnknown method AddRef for this
379 ULONG WINAPI
HGLOBALStreamImpl_AddRef(
382 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
383 return InterlockedIncrement(&This
->ref
);
387 * This implements the IUnknown method Release for this
390 ULONG WINAPI
HGLOBALStreamImpl_Release(
393 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
396 newRef
= InterlockedDecrement(&This
->ref
);
399 * If the reference count goes down to 0, perform suicide.
403 HGLOBALStreamImpl_Destroy(This
);
410 * This method is part of the ISequentialStream interface.
412 * If reads a block of information from the stream at the current
413 * position. It then moves the current position at the end of the
416 * See the documentation of ISequentialStream for more info.
418 HRESULT WINAPI
HGLOBALStreamImpl_Read(
420 void* pv
, /* [length_is][size_is][out] */
422 ULONG
* pcbRead
) /* [out] */
424 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
427 ULONG bytesReadBuffer
;
428 ULONG bytesToReadFromBuffer
;
430 TRACE("(%p, %p, %ld, %p)\n", iface
,
434 * If the caller is not interested in the nubmer of bytes read,
435 * we use another buffer to avoid "if" statements in the code.
438 pcbRead
= &bytesReadBuffer
;
441 * Using the known size of the stream, calculate the number of bytes
442 * to read from the block chain
444 bytesToReadFromBuffer
= min( This
->streamSize
.u
.LowPart
- This
->currentPosition
.u
.LowPart
, cb
);
447 * Lock the buffer in position and copy the data.
449 supportBuffer
= GlobalLock(This
->supportHandle
);
451 memcpy(pv
, (char *) supportBuffer
+This
->currentPosition
.u
.LowPart
, bytesToReadFromBuffer
);
454 * Move the current position to the new position
456 This
->currentPosition
.u
.LowPart
+=bytesToReadFromBuffer
;
459 * Return the number of bytes read.
461 *pcbRead
= bytesToReadFromBuffer
;
466 GlobalUnlock(This
->supportHandle
);
469 * The function returns S_OK if the buffer was filled completely
470 * it returns S_FALSE if the end of the stream is reached before the
480 * This method is part of the ISequentialStream interface.
482 * It writes a block of information to the stream at the current
483 * position. It then moves the current position at the end of the
484 * written block. If the stream is too small to fit the block,
485 * the stream is grown to fit.
487 * See the documentation of ISequentialStream for more info.
489 HRESULT WINAPI
HGLOBALStreamImpl_Write(
491 const void* pv
, /* [size_is][in] */
493 ULONG
* pcbWritten
) /* [out] */
495 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
498 ULARGE_INTEGER newSize
;
499 ULONG bytesWritten
= 0;
501 TRACE("(%p, %p, %ld, %p)\n", iface
,
505 * If the caller is not interested in the number of bytes written,
506 * we use another buffer to avoid "if" statements in the code.
509 pcbWritten
= &bytesWritten
;
517 newSize
.u
.HighPart
= 0;
518 newSize
.u
.LowPart
= This
->currentPosition
.u
.LowPart
+ cb
;
522 * Verify if we need to grow the stream
524 if (newSize
.u
.LowPart
> This
->streamSize
.u
.LowPart
)
527 IStream_SetSize(iface
, newSize
);
531 * Lock the buffer in position and copy the data.
533 supportBuffer
= GlobalLock(This
->supportHandle
);
535 memcpy((char *) supportBuffer
+This
->currentPosition
.u
.LowPart
, pv
, cb
);
538 * Move the current position to the new position
540 This
->currentPosition
.u
.LowPart
+=cb
;
543 * Return the number of bytes read.
550 GlobalUnlock(This
->supportHandle
);
556 * This method is part of the IStream interface.
558 * It will move the current stream pointer according to the parameters
561 * See the documentation of IStream for more info.
563 HRESULT WINAPI
HGLOBALStreamImpl_Seek(
565 LARGE_INTEGER dlibMove
, /* [in] */
566 DWORD dwOrigin
, /* [in] */
567 ULARGE_INTEGER
* plibNewPosition
) /* [out] */
569 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
571 ULARGE_INTEGER newPosition
;
573 TRACE("(%p, %lx%08lx, %ld, %p)\n", iface
, dlibMove
.u
.HighPart
,
574 dlibMove
.u
.LowPart
, dwOrigin
, plibNewPosition
);
577 * The file pointer is moved depending on the given "function"
582 case STREAM_SEEK_SET
:
583 newPosition
.u
.HighPart
= 0;
584 newPosition
.u
.LowPart
= 0;
586 case STREAM_SEEK_CUR
:
587 newPosition
= This
->currentPosition
;
589 case STREAM_SEEK_END
:
590 newPosition
= This
->streamSize
;
593 return STG_E_INVALIDFUNCTION
;
597 * Move the actual file pointer
598 * If the file pointer ends-up after the end of the stream, the next Write operation will
599 * make the file larger. This is how it is documented.
601 newPosition
.QuadPart
= RtlLargeIntegerAdd(newPosition
.QuadPart
, dlibMove
.QuadPart
);
602 if (newPosition
.QuadPart
< 0) return STG_E_INVALIDFUNCTION
;
604 if (plibNewPosition
) *plibNewPosition
= newPosition
;
605 This
->currentPosition
= newPosition
;
611 * This method is part of the IStream interface.
613 * It will change the size of a stream.
615 * TODO: Switch from small blocks to big blocks and vice versa.
617 * See the documentation of IStream for more info.
619 HRESULT WINAPI
HGLOBALStreamImpl_SetSize(
621 ULARGE_INTEGER libNewSize
) /* [in] */
623 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
624 HGLOBAL supportHandle
;
626 TRACE("(%p, %ld)\n", iface
, libNewSize
.u
.LowPart
);
631 if (libNewSize
.u
.HighPart
!= 0)
632 return STG_E_INVALIDFUNCTION
;
634 if (This
->streamSize
.u
.LowPart
== libNewSize
.u
.LowPart
)
638 * Re allocate the HGlobal to fit the new size of the stream.
640 supportHandle
= GlobalReAlloc(This
->supportHandle
, libNewSize
.u
.LowPart
, 0);
642 if (supportHandle
== 0)
643 return STG_E_MEDIUMFULL
;
645 This
->supportHandle
= supportHandle
;
646 This
->streamSize
.u
.LowPart
= libNewSize
.u
.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
.u
.LowPart
, pcbRead
, pcbWritten
);
678 return STG_E_INVALIDPOINTER
;
680 totalBytesRead
.u
.LowPart
= totalBytesRead
.u
.HighPart
= 0;
681 totalBytesWritten
.u
.LowPart
= totalBytesWritten
.u
.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
.u
.LowPart
> 0 )
690 if ( cb
.u
.LowPart
>= 128 )
693 copySize
= cb
.u
.LowPart
;
695 IStream_Read(iface
, tmpBuffer
, copySize
, &bytesRead
);
697 totalBytesRead
.u
.LowPart
+= bytesRead
;
699 IStream_Write(pstm
, tmpBuffer
, bytesRead
, &bytesWritten
);
701 totalBytesWritten
.u
.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
.u
.LowPart
-= bytesRead
;
719 * Update number of bytes read and written
723 pcbRead
->u
.LowPart
= totalBytesRead
.u
.LowPart
;
724 pcbRead
->u
.HighPart
= totalBytesRead
.u
.HighPart
;
729 pcbWritten
->u
.LowPart
= totalBytesWritten
.u
.LowPart
;
730 pcbWritten
->u
.HighPart
= totalBytesWritten
.u
.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 ULARGE_INTEGER dummy
;
827 LARGE_INTEGER offset
;
829 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
830 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface
,This
->deleteOnRelease
,(long)This
->currentPosition
.QuadPart
);
831 hr
=CreateStreamOnHGlobal(This
->supportHandle
, FALSE
, ppstm
);
834 offset
.QuadPart
=(LONGLONG
)This
->currentPosition
.QuadPart
;
835 HGLOBALStreamImpl_Seek(*ppstm
,offset
,STREAM_SEEK_SET
,&dummy
);