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
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(storage
);
42 /****************************************************************************
43 * HGLOBALStreamImpl definition.
45 * This class imlements the IStream inteface and represents a stream
46 * supported by an HGLOBAL pointer.
48 struct HGLOBALStreamImpl
50 ICOM_VFIELD(IStream
); /* Needs to be the first item in the stuct
51 * since we want to cast this in a IStream pointer */
59 * Support for the stream
61 HGLOBAL supportHandle
;
64 * This flag is TRUE if the HGLOBAL is destroyed when the stream
65 * is finally released.
70 * Helper variable that contains the size of the stream
72 ULARGE_INTEGER streamSize
;
75 * This is the current position of the cursor in the stream
77 ULARGE_INTEGER currentPosition
;
80 typedef struct HGLOBALStreamImpl HGLOBALStreamImpl
;
83 * Method definition for the StgStreamImpl class.
85 HGLOBALStreamImpl
* HGLOBALStreamImpl_Construct(
87 BOOL fDeleteOnRelease
);
89 void HGLOBALStreamImpl_Destroy(
90 HGLOBALStreamImpl
* This
);
92 void HGLOBALStreamImpl_OpenBlockChain(
93 HGLOBALStreamImpl
* This
);
95 HRESULT WINAPI
HGLOBALStreamImpl_QueryInterface(
97 REFIID riid
, /* [in] */
98 void** ppvObject
); /* [iid_is][out] */
100 ULONG WINAPI
HGLOBALStreamImpl_AddRef(
103 ULONG WINAPI
HGLOBALStreamImpl_Release(
106 HRESULT WINAPI
HGLOBALStreamImpl_Read(
108 void* pv
, /* [length_is][size_is][out] */
110 ULONG
* pcbRead
); /* [out] */
112 HRESULT WINAPI
HGLOBALStreamImpl_Write(
114 const void* pv
, /* [size_is][in] */
116 ULONG
* pcbWritten
); /* [out] */
118 HRESULT WINAPI
HGLOBALStreamImpl_Seek(
120 LARGE_INTEGER dlibMove
, /* [in] */
121 DWORD dwOrigin
, /* [in] */
122 ULARGE_INTEGER
* plibNewPosition
); /* [out] */
124 HRESULT WINAPI
HGLOBALStreamImpl_SetSize(
126 ULARGE_INTEGER libNewSize
); /* [in] */
128 HRESULT WINAPI
HGLOBALStreamImpl_CopyTo(
130 IStream
* pstm
, /* [unique][in] */
131 ULARGE_INTEGER cb
, /* [in] */
132 ULARGE_INTEGER
* pcbRead
, /* [out] */
133 ULARGE_INTEGER
* pcbWritten
); /* [out] */
135 HRESULT WINAPI
HGLOBALStreamImpl_Commit(
137 DWORD grfCommitFlags
); /* [in] */
139 HRESULT WINAPI
HGLOBALStreamImpl_Revert(
142 HRESULT WINAPI
HGLOBALStreamImpl_LockRegion(
144 ULARGE_INTEGER libOffset
, /* [in] */
145 ULARGE_INTEGER cb
, /* [in] */
146 DWORD dwLockType
); /* [in] */
148 HRESULT WINAPI
HGLOBALStreamImpl_UnlockRegion(
150 ULARGE_INTEGER libOffset
, /* [in] */
151 ULARGE_INTEGER cb
, /* [in] */
152 DWORD dwLockType
); /* [in] */
154 HRESULT WINAPI
HGLOBALStreamImpl_Stat(
156 STATSTG
* pstatstg
, /* [out] */
157 DWORD grfStatFlag
); /* [in] */
159 HRESULT WINAPI
HGLOBALStreamImpl_Clone(
161 IStream
** ppstm
); /* [out] */
165 * Virtual function table for the HGLOBALStreamImpl class.
167 static ICOM_VTABLE(IStream
) HGLOBALStreamImpl_Vtbl
=
169 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
170 HGLOBALStreamImpl_QueryInterface
,
171 HGLOBALStreamImpl_AddRef
,
172 HGLOBALStreamImpl_Release
,
173 HGLOBALStreamImpl_Read
,
174 HGLOBALStreamImpl_Write
,
175 HGLOBALStreamImpl_Seek
,
176 HGLOBALStreamImpl_SetSize
,
177 HGLOBALStreamImpl_CopyTo
,
178 HGLOBALStreamImpl_Commit
,
179 HGLOBALStreamImpl_Revert
,
180 HGLOBALStreamImpl_LockRegion
,
181 HGLOBALStreamImpl_UnlockRegion
,
182 HGLOBALStreamImpl_Stat
,
183 HGLOBALStreamImpl_Clone
186 /***********************************************************************
187 * CreateStreamOnHGlobal [OLE32.61]
189 HRESULT WINAPI
CreateStreamOnHGlobal(
191 BOOL fDeleteOnRelease
,
194 HGLOBALStreamImpl
* newStream
;
196 newStream
= HGLOBALStreamImpl_Construct(hGlobal
,
201 return IUnknown_QueryInterface((IUnknown
*)newStream
,
206 return E_OUTOFMEMORY
;
209 /***********************************************************************
210 * GetHGlobalFromStream [OLE32.71]
212 HRESULT WINAPI
GetHGlobalFromStream(IStream
* pstm
, HGLOBAL
* phglobal
)
214 HGLOBALStreamImpl
* pStream
;
219 pStream
= (HGLOBALStreamImpl
*) pstm
;
222 * Verify that the stream object was created with CreateStreamOnHGlobal.
224 if (ICOM_VTBL(pStream
) == &HGLOBALStreamImpl_Vtbl
)
225 *phglobal
= pStream
->supportHandle
;
235 /******************************************************************************
236 ** HGLOBALStreamImpl implementation
240 * This is the constructor for the HGLOBALStreamImpl class.
243 * hGlobal - Handle that will support the stream. can be NULL.
244 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
245 * when the IStream object is destroyed.
247 HGLOBALStreamImpl
* HGLOBALStreamImpl_Construct(
249 BOOL fDeleteOnRelease
)
251 HGLOBALStreamImpl
* newStream
;
253 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl
));
258 * Set-up the virtual function table and reference count.
260 ICOM_VTBL(newStream
) = &HGLOBALStreamImpl_Vtbl
;
264 * Initialize the support.
266 newStream
->supportHandle
= hGlobal
;
267 newStream
->deleteOnRelease
= fDeleteOnRelease
;
270 * This method will allocate a handle if one is not supplied.
272 if (!newStream
->supportHandle
)
274 newStream
->supportHandle
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_NODISCARD
|
279 * Start the stream at the beginning.
281 newStream
->currentPosition
.s
.HighPart
= 0;
282 newStream
->currentPosition
.s
.LowPart
= 0;
285 * Initialize the size of the stream to the size of the handle.
287 newStream
->streamSize
.s
.HighPart
= 0;
288 newStream
->streamSize
.s
.LowPart
= GlobalSize(newStream
->supportHandle
);
295 * This is the destructor of the HGLOBALStreamImpl class.
297 * This method will clean-up all the resources used-up by the given HGLOBALStreamImpl
298 * class. The pointer passed-in to this function will be freed and will not
301 void HGLOBALStreamImpl_Destroy(HGLOBALStreamImpl
* This
)
303 TRACE("(%p)\n", This
);
306 * Release the HGlobal if the constructor asked for that.
308 if (This
->deleteOnRelease
)
310 GlobalFree(This
->supportHandle
);
311 This
->supportHandle
=0;
315 * Finally, free the memory used-up by the class.
317 HeapFree(GetProcessHeap(), 0, This
);
321 * This implements the IUnknown method QueryInterface for this
324 HRESULT WINAPI
HGLOBALStreamImpl_QueryInterface(
326 REFIID riid
, /* [in] */
327 void** ppvObject
) /* [iid_is][out] */
329 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
332 * Perform a sanity check on the parameters.
338 * Initialize the return parameter.
343 * Compare the riid with the interface IDs implemented by this object.
345 if (memcmp(&IID_IUnknown
, riid
, sizeof(IID_IUnknown
)) == 0)
347 *ppvObject
= (IStream
*)This
;
349 else if (memcmp(&IID_IStream
, riid
, sizeof(IID_IStream
)) == 0)
351 *ppvObject
= (IStream
*)This
;
355 * Check that we obtained an interface.
358 return E_NOINTERFACE
;
361 * Query Interface always increases the reference count by one when it is
364 HGLOBALStreamImpl_AddRef(iface
);
370 * This implements the IUnknown method AddRef for this
373 ULONG WINAPI
HGLOBALStreamImpl_AddRef(
376 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
384 * This implements the IUnknown method Release for this
387 ULONG WINAPI
HGLOBALStreamImpl_Release(
390 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
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
.s
.LowPart
- This
->currentPosition
.s
.LowPart
, cb
);
447 * Lock the buffer in position and copy the data.
449 supportBuffer
= GlobalLock(This
->supportHandle
);
451 memcpy(pv
, (char *) supportBuffer
+This
->currentPosition
.s
.LowPart
, bytesToReadFromBuffer
);
454 * Move the current position to the new position
456 This
->currentPosition
.s
.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
.s
.HighPart
= 0;
518 newSize
.s
.LowPart
= This
->currentPosition
.s
.LowPart
+ cb
;
522 * Verify if we need to grow the stream
524 if (newSize
.s
.LowPart
> This
->streamSize
.s
.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
.s
.LowPart
, pv
, cb
);
538 * Move the current position to the new position
540 This
->currentPosition
.s
.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, %ld, %ld, %p)\n", iface
,
574 dlibMove
.s
.LowPart
, dwOrigin
, plibNewPosition
);
577 * The file pointer is moved depending on the given "function"
582 case STREAM_SEEK_SET
:
583 newPosition
.s
.HighPart
= 0;
584 newPosition
.s
.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
;
625 TRACE("(%p, %ld)\n", iface
, libNewSize
.s
.LowPart
);
630 if (libNewSize
.s
.HighPart
!= 0)
631 return STG_E_INVALIDFUNCTION
;
633 if (This
->streamSize
.s
.LowPart
== libNewSize
.s
.LowPart
)
637 * Re allocate the HGlobal to fit the new size of the stream.
639 This
->supportHandle
= GlobalReAlloc(This
->supportHandle
,
640 libNewSize
.s
.LowPart
,
643 This
->streamSize
.s
.LowPart
= libNewSize
.s
.LowPart
;
649 * This method is part of the IStream interface.
651 * It will copy the 'cb' Bytes to 'pstm' IStream.
653 * See the documentation of IStream for more info.
655 HRESULT WINAPI
HGLOBALStreamImpl_CopyTo(
657 IStream
* pstm
, /* [unique][in] */
658 ULARGE_INTEGER cb
, /* [in] */
659 ULARGE_INTEGER
* pcbRead
, /* [out] */
660 ULARGE_INTEGER
* pcbWritten
) /* [out] */
664 ULONG bytesRead
, bytesWritten
, copySize
;
665 ULARGE_INTEGER totalBytesRead
;
666 ULARGE_INTEGER totalBytesWritten
;
668 TRACE("(%p, %p, %ld, %p, %p)\n", iface
, pstm
,
669 cb
.s
.LowPart
, pcbRead
, pcbWritten
);
675 return STG_E_INVALIDPOINTER
;
677 totalBytesRead
.s
.LowPart
= totalBytesRead
.s
.HighPart
= 0;
678 totalBytesWritten
.s
.LowPart
= totalBytesWritten
.s
.HighPart
= 0;
681 * use stack to store data temporarly
682 * there is surely more performant way of doing it, for now this basic
683 * implementation will do the job
685 while ( cb
.s
.LowPart
> 0 )
687 if ( cb
.s
.LowPart
>= 128 )
690 copySize
= cb
.s
.LowPart
;
692 IStream_Read(iface
, tmpBuffer
, copySize
, &bytesRead
);
694 totalBytesRead
.s
.LowPart
+= bytesRead
;
696 IStream_Write(pstm
, tmpBuffer
, bytesRead
, &bytesWritten
);
698 totalBytesWritten
.s
.LowPart
+= bytesWritten
;
701 * Check that read & write operations were succesfull
703 if (bytesRead
!= bytesWritten
)
705 hr
= STG_E_MEDIUMFULL
;
709 if (bytesRead
!=copySize
)
712 cb
.s
.LowPart
-= bytesRead
;
716 * Update number of bytes read and written
720 pcbRead
->s
.LowPart
= totalBytesRead
.s
.LowPart
;
721 pcbRead
->s
.HighPart
= totalBytesRead
.s
.HighPart
;
726 pcbWritten
->s
.LowPart
= totalBytesWritten
.s
.LowPart
;
727 pcbWritten
->s
.HighPart
= totalBytesWritten
.s
.HighPart
;
733 * This method is part of the IStream interface.
735 * For streams supported by HGLOBALS, this function does nothing.
736 * This is what the documentation tells us.
738 * See the documentation of IStream for more info.
740 HRESULT WINAPI
HGLOBALStreamImpl_Commit(
742 DWORD grfCommitFlags
) /* [in] */
748 * This method is part of the IStream interface.
750 * For streams supported by HGLOBALS, this function does nothing.
751 * This is what the documentation tells us.
753 * See the documentation of IStream for more info.
755 HRESULT WINAPI
HGLOBALStreamImpl_Revert(
762 * This method is part of the IStream interface.
764 * For streams supported by HGLOBALS, this function does nothing.
765 * This is what the documentation tells us.
767 * See the documentation of IStream for more info.
769 HRESULT WINAPI
HGLOBALStreamImpl_LockRegion(
771 ULARGE_INTEGER libOffset
, /* [in] */
772 ULARGE_INTEGER cb
, /* [in] */
773 DWORD dwLockType
) /* [in] */
779 * This method is part of the IStream interface.
781 * For streams supported by HGLOBALS, this function does nothing.
782 * This is what the documentation tells us.
784 * See the documentation of IStream for more info.
786 HRESULT WINAPI
HGLOBALStreamImpl_UnlockRegion(
788 ULARGE_INTEGER libOffset
, /* [in] */
789 ULARGE_INTEGER cb
, /* [in] */
790 DWORD dwLockType
) /* [in] */
796 * This method is part of the IStream interface.
798 * This method returns information about the current
801 * See the documentation of IStream for more info.
803 HRESULT WINAPI
HGLOBALStreamImpl_Stat(
805 STATSTG
* pstatstg
, /* [out] */
806 DWORD grfStatFlag
) /* [in] */
808 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
810 memset(pstatstg
, 0, sizeof(STATSTG
));
812 pstatstg
->pwcsName
= NULL
;
813 pstatstg
->type
= STGTY_STREAM
;
814 pstatstg
->cbSize
= This
->streamSize
;
819 HRESULT WINAPI
HGLOBALStreamImpl_Clone(
821 IStream
** ppstm
) /* [out] */
823 FIXME("not implemented!\n");