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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(storage
);
48 /****************************************************************************
49 * HGLOBALStreamImpl definition.
51 * This class implements the IStream interface and represents a stream
52 * supported by an HGLOBAL pointer.
54 struct HGLOBALStreamImpl
56 const IStreamVtbl
*lpVtbl
; /* Needs to be the first item in the struct
57 * since we want to cast this in an IStream pointer */
65 * Support for the stream
67 HGLOBAL supportHandle
;
70 * This flag is TRUE if the HGLOBAL is destroyed when the stream
71 * is finally released.
76 * Helper variable that contains the size of the stream
78 ULARGE_INTEGER streamSize
;
81 * This is the current position of the cursor in the stream
83 ULARGE_INTEGER currentPosition
;
86 typedef struct HGLOBALStreamImpl HGLOBALStreamImpl
;
89 * This is the destructor of the HGLOBALStreamImpl class.
91 * This method will clean-up all the resources used-up by the given HGLOBALStreamImpl
92 * class. The pointer passed-in to this function will be freed and will not
95 static void HGLOBALStreamImpl_Destroy(HGLOBALStreamImpl
* This
)
97 TRACE("(%p)\n", This
);
100 * Release the HGlobal if the constructor asked for that.
102 if (This
->deleteOnRelease
)
104 GlobalFree(This
->supportHandle
);
105 This
->supportHandle
=0;
109 * Finally, free the memory used-up by the class.
111 HeapFree(GetProcessHeap(), 0, This
);
115 * This implements the IUnknown method AddRef for this
118 static ULONG WINAPI
HGLOBALStreamImpl_AddRef(
121 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
122 return InterlockedIncrement(&This
->ref
);
126 * This implements the IUnknown method QueryInterface for this
129 static HRESULT WINAPI
HGLOBALStreamImpl_QueryInterface(
131 REFIID riid
, /* [in] */
132 void** ppvObject
) /* [iid_is][out] */
134 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
137 * Perform a sanity check on the parameters.
143 * Initialize the return parameter.
148 * Compare the riid with the interface IDs implemented by this object.
150 if (IsEqualIID(&IID_IUnknown
, riid
) ||
151 IsEqualIID(&IID_ISequentialStream
, riid
) ||
152 IsEqualIID(&IID_IStream
, riid
))
158 * Check that we obtained an interface.
161 return E_NOINTERFACE
;
164 * Query Interface always increases the reference count by one when it is
167 HGLOBALStreamImpl_AddRef(iface
);
173 * This implements the IUnknown method Release for this
176 static ULONG WINAPI
HGLOBALStreamImpl_Release(
179 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
182 newRef
= InterlockedDecrement(&This
->ref
);
185 * If the reference count goes down to 0, perform suicide.
189 HGLOBALStreamImpl_Destroy(This
);
196 * This method is part of the ISequentialStream interface.
198 * If reads a block of information from the stream at the current
199 * position. It then moves the current position at the end of the
202 * See the documentation of ISequentialStream for more info.
204 static HRESULT WINAPI
HGLOBALStreamImpl_Read(
206 void* pv
, /* [length_is][size_is][out] */
208 ULONG
* pcbRead
) /* [out] */
210 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
213 ULONG bytesReadBuffer
;
214 ULONG bytesToReadFromBuffer
;
216 TRACE("(%p, %p, %d, %p)\n", iface
,
220 * If the caller is not interested in the nubmer of bytes read,
221 * we use another buffer to avoid "if" statements in the code.
224 pcbRead
= &bytesReadBuffer
;
227 * Using the known size of the stream, calculate the number of bytes
228 * to read from the block chain
230 bytesToReadFromBuffer
= min( This
->streamSize
.u
.LowPart
- This
->currentPosition
.u
.LowPart
, cb
);
233 * Lock the buffer in position and copy the data.
235 supportBuffer
= GlobalLock(This
->supportHandle
);
238 WARN("read from invalid hglobal %p\n", This
->supportHandle
);
243 memcpy(pv
, (char *) supportBuffer
+This
->currentPosition
.u
.LowPart
, bytesToReadFromBuffer
);
246 * Move the current position to the new position
248 This
->currentPosition
.u
.LowPart
+=bytesToReadFromBuffer
;
251 * Return the number of bytes read.
253 *pcbRead
= bytesToReadFromBuffer
;
258 GlobalUnlock(This
->supportHandle
);
261 * Always returns S_OK even if the end of the stream is reached before the
269 * This method is part of the ISequentialStream interface.
271 * It writes a block of information to the stream at the current
272 * position. It then moves the current position at the end of the
273 * written block. If the stream is too small to fit the block,
274 * the stream is grown to fit.
276 * See the documentation of ISequentialStream for more info.
278 static HRESULT WINAPI
HGLOBALStreamImpl_Write(
280 const void* pv
, /* [size_is][in] */
282 ULONG
* pcbWritten
) /* [out] */
284 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
287 ULARGE_INTEGER newSize
;
288 ULONG bytesWritten
= 0;
290 TRACE("(%p, %p, %d, %p)\n", iface
, pv
, cb
, pcbWritten
);
293 * If the caller is not interested in the number of bytes written,
294 * we use another buffer to avoid "if" statements in the code.
297 pcbWritten
= &bytesWritten
;
304 newSize
.u
.HighPart
= 0;
305 newSize
.u
.LowPart
= This
->currentPosition
.u
.LowPart
+ cb
;
308 * Verify if we need to grow the stream
310 if (newSize
.u
.LowPart
> This
->streamSize
.u
.LowPart
)
313 HRESULT hr
= IStream_SetSize(iface
, newSize
);
316 ERR("IStream_SetSize failed with error 0x%08x\n", hr
);
322 * Lock the buffer in position and copy the data.
324 supportBuffer
= GlobalLock(This
->supportHandle
);
327 WARN("write to invalid hglobal %p\n", This
->supportHandle
);
331 memcpy((char *) supportBuffer
+This
->currentPosition
.u
.LowPart
, pv
, cb
);
334 * Move the current position to the new position
336 This
->currentPosition
.u
.LowPart
+=cb
;
341 GlobalUnlock(This
->supportHandle
);
345 * Return the number of bytes read.
353 * This method is part of the IStream interface.
355 * It will move the current stream pointer according to the parameters
358 * See the documentation of IStream for more info.
360 static HRESULT WINAPI
HGLOBALStreamImpl_Seek(
362 LARGE_INTEGER dlibMove
, /* [in] */
363 DWORD dwOrigin
, /* [in] */
364 ULARGE_INTEGER
* plibNewPosition
) /* [out] */
366 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
368 ULARGE_INTEGER newPosition
;
370 TRACE("(%p, %x%08x, %d, %p)\n", iface
, dlibMove
.u
.HighPart
,
371 dlibMove
.u
.LowPart
, dwOrigin
, plibNewPosition
);
374 * The file pointer is moved depending on the given "function"
379 case STREAM_SEEK_SET
:
380 newPosition
.u
.HighPart
= 0;
381 newPosition
.u
.LowPart
= 0;
383 case STREAM_SEEK_CUR
:
384 newPosition
= This
->currentPosition
;
386 case STREAM_SEEK_END
:
387 newPosition
= This
->streamSize
;
390 return STG_E_INVALIDFUNCTION
;
394 * Move the actual file pointer
395 * If the file pointer ends-up after the end of the stream, the next Write operation will
396 * make the file larger. This is how it is documented.
398 if (dlibMove
.QuadPart
< 0 && newPosition
.QuadPart
< -dlibMove
.QuadPart
) return STG_E_INVALIDFUNCTION
;
400 newPosition
.QuadPart
= RtlLargeIntegerAdd(newPosition
.QuadPart
, dlibMove
.QuadPart
);
402 if (plibNewPosition
) *plibNewPosition
= newPosition
;
403 This
->currentPosition
= newPosition
;
409 * This method is part of the IStream interface.
411 * It will change the size of a stream.
413 * TODO: Switch from small blocks to big blocks and vice versa.
415 * See the documentation of IStream for more info.
417 static HRESULT WINAPI
HGLOBALStreamImpl_SetSize(
419 ULARGE_INTEGER libNewSize
) /* [in] */
421 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
422 HGLOBAL supportHandle
;
424 TRACE("(%p, %d)\n", iface
, libNewSize
.u
.LowPart
);
427 * HighPart is ignored as shown in tests
430 if (This
->streamSize
.u
.LowPart
== libNewSize
.u
.LowPart
)
434 * Re allocate the HGlobal to fit the new size of the stream.
436 supportHandle
= GlobalReAlloc(This
->supportHandle
, libNewSize
.u
.LowPart
, 0);
438 if (supportHandle
== 0)
439 return E_OUTOFMEMORY
;
441 This
->supportHandle
= supportHandle
;
442 This
->streamSize
.u
.LowPart
= libNewSize
.u
.LowPart
;
448 * This method is part of the IStream interface.
450 * It will copy the 'cb' Bytes to 'pstm' IStream.
452 * See the documentation of IStream for more info.
454 static HRESULT WINAPI
HGLOBALStreamImpl_CopyTo(
456 IStream
* pstm
, /* [unique][in] */
457 ULARGE_INTEGER cb
, /* [in] */
458 ULARGE_INTEGER
* pcbRead
, /* [out] */
459 ULARGE_INTEGER
* pcbWritten
) /* [out] */
463 ULONG bytesRead
, bytesWritten
, copySize
;
464 ULARGE_INTEGER totalBytesRead
;
465 ULARGE_INTEGER totalBytesWritten
;
467 TRACE("(%p, %p, %d, %p, %p)\n", iface
, pstm
,
468 cb
.u
.LowPart
, pcbRead
, pcbWritten
);
471 return STG_E_INVALIDPOINTER
;
473 totalBytesRead
.QuadPart
= 0;
474 totalBytesWritten
.QuadPart
= 0;
476 while ( cb
.QuadPart
> 0 )
478 if ( cb
.QuadPart
>= sizeof(tmpBuffer
) )
479 copySize
= sizeof(tmpBuffer
);
481 copySize
= cb
.u
.LowPart
;
483 hr
= IStream_Read(iface
, tmpBuffer
, copySize
, &bytesRead
);
487 totalBytesRead
.QuadPart
+= bytesRead
;
491 hr
= IStream_Write(pstm
, tmpBuffer
, bytesRead
, &bytesWritten
);
495 totalBytesWritten
.QuadPart
+= bytesWritten
;
498 if (bytesRead
!=copySize
)
501 cb
.QuadPart
-= bytesRead
;
504 if (pcbRead
) pcbRead
->QuadPart
= totalBytesRead
.QuadPart
;
505 if (pcbWritten
) pcbWritten
->QuadPart
= totalBytesWritten
.QuadPart
;
511 * This method is part of the IStream interface.
513 * For streams supported by HGLOBALS, this function does nothing.
514 * This is what the documentation tells us.
516 * See the documentation of IStream for more info.
518 static HRESULT WINAPI
HGLOBALStreamImpl_Commit(
520 DWORD grfCommitFlags
) /* [in] */
526 * This method is part of the IStream interface.
528 * For streams supported by HGLOBALS, this function does nothing.
529 * This is what the documentation tells us.
531 * See the documentation of IStream for more info.
533 static HRESULT WINAPI
HGLOBALStreamImpl_Revert(
540 * This method is part of the IStream interface.
542 * For streams supported by HGLOBALS, this function does nothing.
543 * This is what the documentation tells us.
545 * See the documentation of IStream for more info.
547 static HRESULT WINAPI
HGLOBALStreamImpl_LockRegion(
549 ULARGE_INTEGER libOffset
, /* [in] */
550 ULARGE_INTEGER cb
, /* [in] */
551 DWORD dwLockType
) /* [in] */
553 return STG_E_INVALIDFUNCTION
;
557 * This method is part of the IStream interface.
559 * For streams supported by HGLOBALS, this function does nothing.
560 * This is what the documentation tells us.
562 * See the documentation of IStream for more info.
564 static HRESULT WINAPI
HGLOBALStreamImpl_UnlockRegion(
566 ULARGE_INTEGER libOffset
, /* [in] */
567 ULARGE_INTEGER cb
, /* [in] */
568 DWORD dwLockType
) /* [in] */
574 * This method is part of the IStream interface.
576 * This method returns information about the current
579 * See the documentation of IStream for more info.
581 static HRESULT WINAPI
HGLOBALStreamImpl_Stat(
583 STATSTG
* pstatstg
, /* [out] */
584 DWORD grfStatFlag
) /* [in] */
586 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
588 memset(pstatstg
, 0, sizeof(STATSTG
));
590 pstatstg
->pwcsName
= NULL
;
591 pstatstg
->type
= STGTY_STREAM
;
592 pstatstg
->cbSize
= This
->streamSize
;
597 static HRESULT WINAPI
HGLOBALStreamImpl_Clone(
599 IStream
** ppstm
) /* [out] */
601 ULARGE_INTEGER dummy
;
602 LARGE_INTEGER offset
;
604 HGLOBALStreamImpl
* const This
=(HGLOBALStreamImpl
*)iface
;
605 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface
,This
->deleteOnRelease
,(long)This
->currentPosition
.QuadPart
);
606 hr
=CreateStreamOnHGlobal(This
->supportHandle
, FALSE
, ppstm
);
609 offset
.QuadPart
=(LONGLONG
)This
->currentPosition
.QuadPart
;
610 HGLOBALStreamImpl_Seek(*ppstm
,offset
,STREAM_SEEK_SET
,&dummy
);
615 * Virtual function table for the HGLOBALStreamImpl class.
617 static const IStreamVtbl HGLOBALStreamImpl_Vtbl
=
619 HGLOBALStreamImpl_QueryInterface
,
620 HGLOBALStreamImpl_AddRef
,
621 HGLOBALStreamImpl_Release
,
622 HGLOBALStreamImpl_Read
,
623 HGLOBALStreamImpl_Write
,
624 HGLOBALStreamImpl_Seek
,
625 HGLOBALStreamImpl_SetSize
,
626 HGLOBALStreamImpl_CopyTo
,
627 HGLOBALStreamImpl_Commit
,
628 HGLOBALStreamImpl_Revert
,
629 HGLOBALStreamImpl_LockRegion
,
630 HGLOBALStreamImpl_UnlockRegion
,
631 HGLOBALStreamImpl_Stat
,
632 HGLOBALStreamImpl_Clone
635 /******************************************************************************
636 ** HGLOBALStreamImpl implementation
640 * This is the constructor for the HGLOBALStreamImpl class.
643 * hGlobal - Handle that will support the stream. can be NULL.
644 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
645 * when the IStream object is destroyed.
647 static HGLOBALStreamImpl
* HGLOBALStreamImpl_Construct(
649 BOOL fDeleteOnRelease
)
651 HGLOBALStreamImpl
* newStream
;
653 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl
));
658 * Set-up the virtual function table and reference count.
660 newStream
->lpVtbl
= &HGLOBALStreamImpl_Vtbl
;
664 * Initialize the support.
666 newStream
->supportHandle
= hGlobal
;
667 newStream
->deleteOnRelease
= fDeleteOnRelease
;
670 * This method will allocate a handle if one is not supplied.
672 if (!newStream
->supportHandle
)
674 newStream
->supportHandle
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_NODISCARD
|
679 * Start the stream at the beginning.
681 newStream
->currentPosition
.u
.HighPart
= 0;
682 newStream
->currentPosition
.u
.LowPart
= 0;
685 * Initialize the size of the stream to the size of the handle.
687 newStream
->streamSize
.u
.HighPart
= 0;
688 newStream
->streamSize
.u
.LowPart
= GlobalSize(newStream
->supportHandle
);
695 /***********************************************************************
696 * CreateStreamOnHGlobal [OLE32.@]
698 HRESULT WINAPI
CreateStreamOnHGlobal(
700 BOOL fDeleteOnRelease
,
703 HGLOBALStreamImpl
* newStream
;
708 newStream
= HGLOBALStreamImpl_Construct(hGlobal
,
713 return IUnknown_QueryInterface((IUnknown
*)newStream
,
718 return E_OUTOFMEMORY
;
721 /***********************************************************************
722 * GetHGlobalFromStream [OLE32.@]
724 HRESULT WINAPI
GetHGlobalFromStream(IStream
* pstm
, HGLOBAL
* phglobal
)
726 HGLOBALStreamImpl
* pStream
;
731 pStream
= (HGLOBALStreamImpl
*) pstm
;
734 * Verify that the stream object was created with CreateStreamOnHGlobal.
736 if (pStream
->lpVtbl
== &HGLOBALStreamImpl_Vtbl
)
737 *phglobal
= pStream
->supportHandle
;