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 IStreamVtbl
*lpVtbl
; /* 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 IStreamVtbl HGLOBALStreamImpl_Vtbl
=
174 HGLOBALStreamImpl_QueryInterface
,
175 HGLOBALStreamImpl_AddRef
,
176 HGLOBALStreamImpl_Release
,
177 HGLOBALStreamImpl_Read
,
178 HGLOBALStreamImpl_Write
,
179 HGLOBALStreamImpl_Seek
,
180 HGLOBALStreamImpl_SetSize
,
181 HGLOBALStreamImpl_CopyTo
,
182 HGLOBALStreamImpl_Commit
,
183 HGLOBALStreamImpl_Revert
,
184 HGLOBALStreamImpl_LockRegion
,
185 HGLOBALStreamImpl_UnlockRegion
,
186 HGLOBALStreamImpl_Stat
,
187 HGLOBALStreamImpl_Clone
190 /***********************************************************************
191 * CreateStreamOnHGlobal [OLE32.@]
193 HRESULT WINAPI
CreateStreamOnHGlobal(
195 BOOL fDeleteOnRelease
,
198 HGLOBALStreamImpl
* newStream
;
200 newStream
= HGLOBALStreamImpl_Construct(hGlobal
,
205 return IUnknown_QueryInterface((IUnknown
*)newStream
,
210 return E_OUTOFMEMORY
;
213 /***********************************************************************
214 * GetHGlobalFromStream [OLE32.@]
216 HRESULT WINAPI
GetHGlobalFromStream(IStream
* pstm
, HGLOBAL
* phglobal
)
218 HGLOBALStreamImpl
* pStream
;
223 pStream
= (HGLOBALStreamImpl
*) pstm
;
226 * Verify that the stream object was created with CreateStreamOnHGlobal.
228 if (pStream
->lpVtbl
== &HGLOBALStreamImpl_Vtbl
)
229 *phglobal
= pStream
->supportHandle
;
239 /******************************************************************************
240 ** HGLOBALStreamImpl implementation
244 * This is the constructor for the HGLOBALStreamImpl class.
247 * hGlobal - Handle that will support the stream. can be NULL.
248 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
249 * when the IStream object is destroyed.
251 HGLOBALStreamImpl
* HGLOBALStreamImpl_Construct(
253 BOOL fDeleteOnRelease
)
255 HGLOBALStreamImpl
* newStream
;
257 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl
));
262 * Set-up the virtual function table and reference count.
264 newStream
->lpVtbl
= &HGLOBALStreamImpl_Vtbl
;
268 * Initialize the support.
270 newStream
->supportHandle
= hGlobal
;
271 newStream
->deleteOnRelease
= fDeleteOnRelease
;
274 * This method will allocate a handle if one is not supplied.
276 if (!newStream
->supportHandle
)
278 newStream
->supportHandle
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_NODISCARD
|
283 * Start the stream at the beginning.
285 newStream
->currentPosition
.u
.HighPart
= 0;
286 newStream
->currentPosition
.u
.LowPart
= 0;
289 * Initialize the size of the stream to the size of the handle.
291 newStream
->streamSize
.u
.HighPart
= 0;
292 newStream
->streamSize
.u
.LowPart
= GlobalSize(newStream
->supportHandle
);
299 * This is the destructor of the HGLOBALStreamImpl class.
301 * This method will clean-up all the resources used-up by the given HGLOBALStreamImpl
302 * class. The pointer passed-in to this function will be freed and will not
305 void HGLOBALStreamImpl_Destroy(HGLOBALStreamImpl
* This
)
307 TRACE("(%p)\n", This
);
310 * Release the HGlobal if the constructor asked for that.
312 if (This
->deleteOnRelease
)
314 GlobalFree(This
->supportHandle
);
315 This
->supportHandle
=0;
319 * Finally, free the memory used-up by the class.
321 HeapFree(GetProcessHeap(), 0, This
);
325 * This implements the IUnknown method QueryInterface for this
328 HRESULT WINAPI
HGLOBALStreamImpl_QueryInterface(
330 REFIID riid
, /* [in] */
331 void** ppvObject
) /* [iid_is][out] */
333 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
336 * Perform a sanity check on the parameters.
342 * Initialize the return parameter.
347 * Compare the riid with the interface IDs implemented by this object.
349 if (memcmp(&IID_IUnknown
, riid
, sizeof(IID_IUnknown
)) == 0)
351 *ppvObject
= (IStream
*)This
;
353 else if (memcmp(&IID_IStream
, riid
, sizeof(IID_IStream
)) == 0)
355 *ppvObject
= (IStream
*)This
;
359 * Check that we obtained an interface.
362 return E_NOINTERFACE
;
365 * Query Interface always increases the reference count by one when it is
368 HGLOBALStreamImpl_AddRef(iface
);
374 * This implements the IUnknown method AddRef for this
377 ULONG WINAPI
HGLOBALStreamImpl_AddRef(
380 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
388 * This implements the IUnknown method Release for this
391 ULONG WINAPI
HGLOBALStreamImpl_Release(
394 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
403 * If the reference count goes down to 0, perform suicide.
407 HGLOBALStreamImpl_Destroy(This
);
414 * This method is part of the ISequentialStream interface.
416 * If reads a block of information from the stream at the current
417 * position. It then moves the current position at the end of the
420 * See the documentation of ISequentialStream for more info.
422 HRESULT WINAPI
HGLOBALStreamImpl_Read(
424 void* pv
, /* [length_is][size_is][out] */
426 ULONG
* pcbRead
) /* [out] */
428 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
431 ULONG bytesReadBuffer
;
432 ULONG bytesToReadFromBuffer
;
434 TRACE("(%p, %p, %ld, %p)\n", iface
,
438 * If the caller is not interested in the nubmer of bytes read,
439 * we use another buffer to avoid "if" statements in the code.
442 pcbRead
= &bytesReadBuffer
;
445 * Using the known size of the stream, calculate the number of bytes
446 * to read from the block chain
448 bytesToReadFromBuffer
= min( This
->streamSize
.u
.LowPart
- This
->currentPosition
.u
.LowPart
, cb
);
451 * Lock the buffer in position and copy the data.
453 supportBuffer
= GlobalLock(This
->supportHandle
);
455 memcpy(pv
, (char *) supportBuffer
+This
->currentPosition
.u
.LowPart
, bytesToReadFromBuffer
);
458 * Move the current position to the new position
460 This
->currentPosition
.u
.LowPart
+=bytesToReadFromBuffer
;
463 * Return the number of bytes read.
465 *pcbRead
= bytesToReadFromBuffer
;
470 GlobalUnlock(This
->supportHandle
);
473 * The function returns S_OK if the buffer was filled completely
474 * it returns S_FALSE if the end of the stream is reached before the
484 * This method is part of the ISequentialStream interface.
486 * It writes a block of information to the stream at the current
487 * position. It then moves the current position at the end of the
488 * written block. If the stream is too small to fit the block,
489 * the stream is grown to fit.
491 * See the documentation of ISequentialStream for more info.
493 HRESULT WINAPI
HGLOBALStreamImpl_Write(
495 const void* pv
, /* [size_is][in] */
497 ULONG
* pcbWritten
) /* [out] */
499 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
502 ULARGE_INTEGER newSize
;
503 ULONG bytesWritten
= 0;
505 TRACE("(%p, %p, %ld, %p)\n", iface
,
509 * If the caller is not interested in the number of bytes written,
510 * we use another buffer to avoid "if" statements in the code.
513 pcbWritten
= &bytesWritten
;
521 newSize
.u
.HighPart
= 0;
522 newSize
.u
.LowPart
= This
->currentPosition
.u
.LowPart
+ cb
;
526 * Verify if we need to grow the stream
528 if (newSize
.u
.LowPart
> This
->streamSize
.u
.LowPart
)
531 IStream_SetSize(iface
, newSize
);
535 * Lock the buffer in position and copy the data.
537 supportBuffer
= GlobalLock(This
->supportHandle
);
539 memcpy((char *) supportBuffer
+This
->currentPosition
.u
.LowPart
, pv
, cb
);
542 * Move the current position to the new position
544 This
->currentPosition
.u
.LowPart
+=cb
;
547 * Return the number of bytes read.
554 GlobalUnlock(This
->supportHandle
);
560 * This method is part of the IStream interface.
562 * It will move the current stream pointer according to the parameters
565 * See the documentation of IStream for more info.
567 HRESULT WINAPI
HGLOBALStreamImpl_Seek(
569 LARGE_INTEGER dlibMove
, /* [in] */
570 DWORD dwOrigin
, /* [in] */
571 ULARGE_INTEGER
* plibNewPosition
) /* [out] */
573 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
575 ULARGE_INTEGER newPosition
;
577 TRACE("(%p, %lx%08lx, %ld, %p)\n", iface
, dlibMove
.u
.HighPart
,
578 dlibMove
.u
.LowPart
, dwOrigin
, plibNewPosition
);
581 * The file pointer is moved depending on the given "function"
586 case STREAM_SEEK_SET
:
587 newPosition
.u
.HighPart
= 0;
588 newPosition
.u
.LowPart
= 0;
590 case STREAM_SEEK_CUR
:
591 newPosition
= This
->currentPosition
;
593 case STREAM_SEEK_END
:
594 newPosition
= This
->streamSize
;
597 return STG_E_INVALIDFUNCTION
;
601 * Move the actual file pointer
602 * If the file pointer ends-up after the end of the stream, the next Write operation will
603 * make the file larger. This is how it is documented.
605 newPosition
.QuadPart
= RtlLargeIntegerAdd(newPosition
.QuadPart
, dlibMove
.QuadPart
);
606 if (newPosition
.QuadPart
< 0) return STG_E_INVALIDFUNCTION
;
608 if (plibNewPosition
) *plibNewPosition
= newPosition
;
609 This
->currentPosition
= newPosition
;
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
;
628 HGLOBAL supportHandle
;
630 TRACE("(%p, %ld)\n", iface
, libNewSize
.u
.LowPart
);
635 if (libNewSize
.u
.HighPart
!= 0)
636 return STG_E_INVALIDFUNCTION
;
638 if (This
->streamSize
.u
.LowPart
== libNewSize
.u
.LowPart
)
642 * Re allocate the HGlobal to fit the new size of the stream.
644 supportHandle
= GlobalReAlloc(This
->supportHandle
, libNewSize
.u
.LowPart
, 0);
646 if (supportHandle
== 0)
647 return STG_E_MEDIUMFULL
;
649 This
->supportHandle
= supportHandle
;
650 This
->streamSize
.u
.LowPart
= libNewSize
.u
.LowPart
;
656 * This method is part of the IStream interface.
658 * It will copy the 'cb' Bytes to 'pstm' IStream.
660 * See the documentation of IStream for more info.
662 HRESULT WINAPI
HGLOBALStreamImpl_CopyTo(
664 IStream
* pstm
, /* [unique][in] */
665 ULARGE_INTEGER cb
, /* [in] */
666 ULARGE_INTEGER
* pcbRead
, /* [out] */
667 ULARGE_INTEGER
* pcbWritten
) /* [out] */
671 ULONG bytesRead
, bytesWritten
, copySize
;
672 ULARGE_INTEGER totalBytesRead
;
673 ULARGE_INTEGER totalBytesWritten
;
675 TRACE("(%p, %p, %ld, %p, %p)\n", iface
, pstm
,
676 cb
.u
.LowPart
, pcbRead
, pcbWritten
);
682 return STG_E_INVALIDPOINTER
;
684 totalBytesRead
.u
.LowPart
= totalBytesRead
.u
.HighPart
= 0;
685 totalBytesWritten
.u
.LowPart
= totalBytesWritten
.u
.HighPart
= 0;
688 * use stack to store data temporarly
689 * there is surely more performant way of doing it, for now this basic
690 * implementation will do the job
692 while ( cb
.u
.LowPart
> 0 )
694 if ( cb
.u
.LowPart
>= 128 )
697 copySize
= cb
.u
.LowPart
;
699 IStream_Read(iface
, tmpBuffer
, copySize
, &bytesRead
);
701 totalBytesRead
.u
.LowPart
+= bytesRead
;
703 IStream_Write(pstm
, tmpBuffer
, bytesRead
, &bytesWritten
);
705 totalBytesWritten
.u
.LowPart
+= bytesWritten
;
708 * Check that read & write operations were succesfull
710 if (bytesRead
!= bytesWritten
)
712 hr
= STG_E_MEDIUMFULL
;
716 if (bytesRead
!=copySize
)
719 cb
.u
.LowPart
-= bytesRead
;
723 * Update number of bytes read and written
727 pcbRead
->u
.LowPart
= totalBytesRead
.u
.LowPart
;
728 pcbRead
->u
.HighPart
= totalBytesRead
.u
.HighPart
;
733 pcbWritten
->u
.LowPart
= totalBytesWritten
.u
.LowPart
;
734 pcbWritten
->u
.HighPart
= totalBytesWritten
.u
.HighPart
;
740 * This method is part of the IStream interface.
742 * For streams supported by HGLOBALS, this function does nothing.
743 * This is what the documentation tells us.
745 * See the documentation of IStream for more info.
747 HRESULT WINAPI
HGLOBALStreamImpl_Commit(
749 DWORD grfCommitFlags
) /* [in] */
755 * This method is part of the IStream interface.
757 * For streams supported by HGLOBALS, this function does nothing.
758 * This is what the documentation tells us.
760 * See the documentation of IStream for more info.
762 HRESULT WINAPI
HGLOBALStreamImpl_Revert(
769 * This method is part of the IStream interface.
771 * For streams supported by HGLOBALS, this function does nothing.
772 * This is what the documentation tells us.
774 * See the documentation of IStream for more info.
776 HRESULT WINAPI
HGLOBALStreamImpl_LockRegion(
778 ULARGE_INTEGER libOffset
, /* [in] */
779 ULARGE_INTEGER cb
, /* [in] */
780 DWORD dwLockType
) /* [in] */
786 * This method is part of the IStream interface.
788 * For streams supported by HGLOBALS, this function does nothing.
789 * This is what the documentation tells us.
791 * See the documentation of IStream for more info.
793 HRESULT WINAPI
HGLOBALStreamImpl_UnlockRegion(
795 ULARGE_INTEGER libOffset
, /* [in] */
796 ULARGE_INTEGER cb
, /* [in] */
797 DWORD dwLockType
) /* [in] */
803 * This method is part of the IStream interface.
805 * This method returns information about the current
808 * See the documentation of IStream for more info.
810 HRESULT WINAPI
HGLOBALStreamImpl_Stat(
812 STATSTG
* pstatstg
, /* [out] */
813 DWORD grfStatFlag
) /* [in] */
815 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
817 memset(pstatstg
, 0, sizeof(STATSTG
));
819 pstatstg
->pwcsName
= NULL
;
820 pstatstg
->type
= STGTY_STREAM
;
821 pstatstg
->cbSize
= This
->streamSize
;
826 HRESULT WINAPI
HGLOBALStreamImpl_Clone(
828 IStream
** ppstm
) /* [out] */
830 ULARGE_INTEGER dummy
;
831 LARGE_INTEGER offset
;
833 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
834 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface
,This
->deleteOnRelease
,(long)This
->currentPosition
.QuadPart
);
835 hr
=CreateStreamOnHGlobal(This
->supportHandle
, FALSE
, ppstm
);
838 offset
.QuadPart
=(LONGLONG
)This
->currentPosition
.QuadPart
;
839 HGLOBALStreamImpl_Seek(*ppstm
,offset
,STREAM_SEEK_SET
,&dummy
);