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
8 * Copyright 2016 Dmitry Timoshkov
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
34 #define NONAMELESSUNION
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(hglobalstream
);
53 BOOL delete_on_release
;
54 CRITICAL_SECTION lock
;
57 static void handle_addref(struct handle_wrapper
*handle
)
59 InterlockedIncrement(&handle
->ref
);
62 static void handle_release(struct handle_wrapper
*handle
)
64 ULONG ref
= InterlockedDecrement(&handle
->ref
);
68 if (handle
->delete_on_release
)
70 GlobalFree(handle
->hglobal
);
71 handle
->hglobal
= NULL
;
74 handle
->lock
.DebugInfo
->Spare
[0] = 0;
75 DeleteCriticalSection(&handle
->lock
);
76 HeapFree(GetProcessHeap(), 0, handle
);
80 static ULONG
handle_read(struct handle_wrapper
*handle
, ULONG
*pos
, void *dest
, ULONG len
)
84 EnterCriticalSection(&handle
->lock
);
86 if (*pos
< handle
->size
)
87 len
= min(handle
->size
- *pos
, len
);
91 source
= GlobalLock(handle
->hglobal
);
94 memcpy(dest
, (char *)source
+ *pos
, len
);
96 GlobalUnlock(handle
->hglobal
);
100 WARN("read from invalid hglobal %p\n", handle
->hglobal
);
104 LeaveCriticalSection(&handle
->lock
);
108 static ULONG
handle_write(struct handle_wrapper
*handle
, ULONG
*pos
, const void *source
, ULONG len
)
115 EnterCriticalSection(&handle
->lock
);
117 if (*pos
+ len
> handle
->size
)
119 HGLOBAL hglobal
= GlobalReAlloc(handle
->hglobal
, *pos
+ len
, GMEM_MOVEABLE
);
122 handle
->hglobal
= hglobal
;
123 handle
->size
= *pos
+ len
;
132 dest
= GlobalLock(handle
->hglobal
);
135 memcpy((char *)dest
+ *pos
, source
, len
);
137 GlobalUnlock(handle
->hglobal
);
141 WARN("write to invalid hglobal %p\n", handle
->hglobal
);
146 LeaveCriticalSection(&handle
->lock
);
150 static HGLOBAL
handle_gethglobal(struct handle_wrapper
*handle
)
152 return handle
->hglobal
;
155 static HRESULT
handle_setsize(struct handle_wrapper
*handle
, ULONG size
)
159 EnterCriticalSection(&handle
->lock
);
161 if (handle
->size
!= size
)
163 HGLOBAL hglobal
= GlobalReAlloc(handle
->hglobal
, size
, GMEM_MOVEABLE
);
166 handle
->hglobal
= hglobal
;
173 LeaveCriticalSection(&handle
->lock
);
177 static ULONG
handle_getsize(struct handle_wrapper
*handle
)
182 static struct handle_wrapper
*handle_create(HGLOBAL hglobal
, BOOL delete_on_release
)
184 struct handle_wrapper
*handle
;
186 handle
= HeapAlloc(GetProcessHeap(), 0, sizeof(*handle
));
190 handle
->hglobal
= hglobal
;
191 handle
->size
= GlobalSize(hglobal
);
192 handle
->delete_on_release
= delete_on_release
;
193 InitializeCriticalSection(&handle
->lock
);
194 handle
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": handle_wrapper.lock");
199 /****************************************************************************
200 * HGLOBALStreamImpl definition.
202 * This class implements the IStream interface and represents a stream
203 * supported by an HGLOBAL pointer.
207 IStream IStream_iface
;
210 struct handle_wrapper
*handle
;
212 /* current position of the cursor */
213 ULARGE_INTEGER currentPosition
;
216 static inline HGLOBALStreamImpl
*impl_from_IStream(IStream
*iface
)
218 return CONTAINING_RECORD(iface
, HGLOBALStreamImpl
, IStream_iface
);
221 static HRESULT WINAPI
HGLOBALStreamImpl_QueryInterface(
223 REFIID riid
, /* [in] */
224 void** ppvObject
) /* [iid_is][out] */
226 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
233 if (IsEqualIID(&IID_IUnknown
, riid
) ||
234 IsEqualIID(&IID_ISequentialStream
, riid
) ||
235 IsEqualIID(&IID_IStream
, riid
))
237 *ppvObject
= &This
->IStream_iface
;
241 return E_NOINTERFACE
;
243 IStream_AddRef(iface
);
248 static ULONG WINAPI
HGLOBALStreamImpl_AddRef(IStream
* iface
)
250 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
251 return InterlockedIncrement(&This
->ref
);
254 static ULONG WINAPI
HGLOBALStreamImpl_Release(
257 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
258 ULONG ref
= InterlockedDecrement(&This
->ref
);
262 handle_release(This
->handle
);
263 HeapFree(GetProcessHeap(), 0, This
);
270 * This method is part of the ISequentialStream interface.
272 * If reads a block of information from the stream at the current
273 * position. It then moves the current position at the end of the
276 * See the documentation of ISequentialStream for more info.
278 static HRESULT WINAPI
HGLOBALStreamImpl_Read(
280 void* pv
, /* [length_is][size_is][out] */
282 ULONG
* pcbRead
) /* [out] */
284 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
287 TRACE("(%p, %p, %d, %p)\n", iface
, pv
, cb
, pcbRead
);
289 num_bytes
= handle_read(This
->handle
, &This
->currentPosition
.u
.LowPart
, pv
, cb
);
290 if (pcbRead
) *pcbRead
= num_bytes
;
296 * This method is part of the ISequentialStream interface.
298 * It writes a block of information to the stream at the current
299 * position. It then moves the current position at the end of the
300 * written block. If the stream is too small to fit the block,
301 * the stream is grown to fit.
303 * See the documentation of ISequentialStream for more info.
305 static HRESULT WINAPI
HGLOBALStreamImpl_Write(
307 const void* pv
, /* [size_is][in] */
309 ULONG
* pcbWritten
) /* [out] */
311 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
314 TRACE("(%p, %p, %d, %p)\n", iface
, pv
, cb
, pcbWritten
);
316 num_bytes
= handle_write(This
->handle
, &This
->currentPosition
.u
.LowPart
, pv
, cb
);
317 if (pcbWritten
) *pcbWritten
= num_bytes
;
319 return (num_bytes
< cb
) ? E_OUTOFMEMORY
: S_OK
;
323 * This method is part of the IStream interface.
325 * It will move the current stream pointer according to the parameters
328 * See the documentation of IStream for more info.
330 static HRESULT WINAPI
HGLOBALStreamImpl_Seek(
332 LARGE_INTEGER dlibMove
, /* [in] */
333 DWORD dwOrigin
, /* [in] */
334 ULARGE_INTEGER
* plibNewPosition
) /* [out] */
336 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
338 ULARGE_INTEGER newPosition
= This
->currentPosition
;
341 TRACE("(%p, %x%08x, %d, %p)\n", iface
, dlibMove
.u
.HighPart
,
342 dlibMove
.u
.LowPart
, dwOrigin
, plibNewPosition
);
345 * The file pointer is moved depending on the given "function"
350 case STREAM_SEEK_SET
:
351 newPosition
.u
.HighPart
= 0;
352 newPosition
.u
.LowPart
= 0;
354 case STREAM_SEEK_CUR
:
356 case STREAM_SEEK_END
:
357 newPosition
.QuadPart
= handle_getsize(This
->handle
);
360 hr
= STG_E_SEEKERROR
;
365 * Move the actual file pointer
366 * If the file pointer ends-up after the end of the stream, the next Write operation will
367 * make the file larger. This is how it is documented.
369 newPosition
.u
.HighPart
= 0;
370 newPosition
.u
.LowPart
+= dlibMove
.QuadPart
;
372 if (dlibMove
.u
.LowPart
>= 0x80000000 &&
373 newPosition
.u
.LowPart
>= dlibMove
.u
.LowPart
)
375 /* We tried to seek backwards and went past the start. */
376 hr
= STG_E_SEEKERROR
;
380 This
->currentPosition
= newPosition
;
383 if (plibNewPosition
) *plibNewPosition
= This
->currentPosition
;
389 * This method is part of the IStream interface.
391 * It will change the size of a stream.
393 * TODO: Switch from small blocks to big blocks and vice versa.
395 * See the documentation of IStream for more info.
397 static HRESULT WINAPI
HGLOBALStreamImpl_SetSize(
399 ULARGE_INTEGER libNewSize
) /* [in] */
401 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
403 TRACE("(%p, %d)\n", iface
, libNewSize
.u
.LowPart
);
406 * HighPart is ignored as shown in tests
408 return handle_setsize(This
->handle
, libNewSize
.u
.LowPart
);
412 * This method is part of the IStream interface.
414 * It will copy the 'cb' Bytes to 'pstm' IStream.
416 * See the documentation of IStream for more info.
418 static HRESULT WINAPI
HGLOBALStreamImpl_CopyTo(
420 IStream
* pstm
, /* [unique][in] */
421 ULARGE_INTEGER cb
, /* [in] */
422 ULARGE_INTEGER
* pcbRead
, /* [out] */
423 ULARGE_INTEGER
* pcbWritten
) /* [out] */
427 ULONG bytesRead
, bytesWritten
, copySize
;
428 ULARGE_INTEGER totalBytesRead
;
429 ULARGE_INTEGER totalBytesWritten
;
431 TRACE("(%p, %p, %d, %p, %p)\n", iface
, pstm
,
432 cb
.u
.LowPart
, pcbRead
, pcbWritten
);
435 return STG_E_INVALIDPOINTER
;
437 totalBytesRead
.QuadPart
= 0;
438 totalBytesWritten
.QuadPart
= 0;
440 while ( cb
.QuadPart
> 0 )
442 if ( cb
.QuadPart
>= sizeof(tmpBuffer
) )
443 copySize
= sizeof(tmpBuffer
);
445 copySize
= cb
.u
.LowPart
;
447 hr
= IStream_Read(iface
, tmpBuffer
, copySize
, &bytesRead
);
451 totalBytesRead
.QuadPart
+= bytesRead
;
455 hr
= IStream_Write(pstm
, tmpBuffer
, bytesRead
, &bytesWritten
);
459 totalBytesWritten
.QuadPart
+= bytesWritten
;
462 if (bytesRead
!=copySize
)
465 cb
.QuadPart
-= bytesRead
;
468 if (pcbRead
) pcbRead
->QuadPart
= totalBytesRead
.QuadPart
;
469 if (pcbWritten
) pcbWritten
->QuadPart
= totalBytesWritten
.QuadPart
;
475 * This method is part of the IStream interface.
477 * For streams supported by HGLOBALS, this function does nothing.
478 * This is what the documentation tells us.
480 * See the documentation of IStream for more info.
482 static HRESULT WINAPI
HGLOBALStreamImpl_Commit(
484 DWORD grfCommitFlags
) /* [in] */
490 * This method is part of the IStream interface.
492 * For streams supported by HGLOBALS, this function does nothing.
493 * This is what the documentation tells us.
495 * See the documentation of IStream for more info.
497 static HRESULT WINAPI
HGLOBALStreamImpl_Revert(
504 * This method is part of the IStream interface.
506 * For streams supported by HGLOBALS, this function does nothing.
507 * This is what the documentation tells us.
509 * See the documentation of IStream for more info.
511 static HRESULT WINAPI
HGLOBALStreamImpl_LockRegion(
513 ULARGE_INTEGER libOffset
, /* [in] */
514 ULARGE_INTEGER cb
, /* [in] */
515 DWORD dwLockType
) /* [in] */
517 return STG_E_INVALIDFUNCTION
;
521 * This method is part of the IStream interface.
523 * For streams supported by HGLOBALS, this function does nothing.
524 * This is what the documentation tells us.
526 * See the documentation of IStream for more info.
528 static HRESULT WINAPI
HGLOBALStreamImpl_UnlockRegion(
530 ULARGE_INTEGER libOffset
, /* [in] */
531 ULARGE_INTEGER cb
, /* [in] */
532 DWORD dwLockType
) /* [in] */
538 * This method is part of the IStream interface.
540 * This method returns information about the current
543 * See the documentation of IStream for more info.
545 static HRESULT WINAPI
HGLOBALStreamImpl_Stat(
547 STATSTG
* pstatstg
, /* [out] */
548 DWORD grfStatFlag
) /* [in] */
550 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
552 memset(pstatstg
, 0, sizeof(STATSTG
));
554 pstatstg
->pwcsName
= NULL
;
555 pstatstg
->type
= STGTY_STREAM
;
556 pstatstg
->cbSize
.QuadPart
= handle_getsize(This
->handle
);
561 static const IStreamVtbl HGLOBALStreamImplVtbl
;
563 static HGLOBALStreamImpl
*HGLOBALStreamImpl_Create(void)
565 HGLOBALStreamImpl
*This
;
567 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
570 This
->IStream_iface
.lpVtbl
= &HGLOBALStreamImplVtbl
;
576 static HRESULT WINAPI
HGLOBALStreamImpl_Clone(
578 IStream
** ppstm
) /* [out] */
580 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
581 HGLOBALStreamImpl
* clone
;
582 ULARGE_INTEGER dummy
;
583 LARGE_INTEGER offset
;
585 if (!ppstm
) return E_INVALIDARG
;
589 TRACE(" Cloning %p (seek position=%d)\n", iface
, This
->currentPosition
.u
.LowPart
);
591 clone
= HGLOBALStreamImpl_Create();
592 if (!clone
) return E_OUTOFMEMORY
;
594 *ppstm
= &clone
->IStream_iface
;
596 handle_addref(This
->handle
);
597 clone
->handle
= This
->handle
;
599 offset
.QuadPart
= (LONGLONG
)This
->currentPosition
.QuadPart
;
600 IStream_Seek(*ppstm
, offset
, STREAM_SEEK_SET
, &dummy
);
604 static const IStreamVtbl HGLOBALStreamImplVtbl
=
606 HGLOBALStreamImpl_QueryInterface
,
607 HGLOBALStreamImpl_AddRef
,
608 HGLOBALStreamImpl_Release
,
609 HGLOBALStreamImpl_Read
,
610 HGLOBALStreamImpl_Write
,
611 HGLOBALStreamImpl_Seek
,
612 HGLOBALStreamImpl_SetSize
,
613 HGLOBALStreamImpl_CopyTo
,
614 HGLOBALStreamImpl_Commit
,
615 HGLOBALStreamImpl_Revert
,
616 HGLOBALStreamImpl_LockRegion
,
617 HGLOBALStreamImpl_UnlockRegion
,
618 HGLOBALStreamImpl_Stat
,
619 HGLOBALStreamImpl_Clone
622 /***********************************************************************
623 * CreateStreamOnHGlobal [OLE32.@]
625 HRESULT WINAPI
CreateStreamOnHGlobal(
627 BOOL fDeleteOnRelease
,
630 HGLOBALStreamImpl
* This
;
635 This
= HGLOBALStreamImpl_Create();
636 if (!This
) return E_OUTOFMEMORY
;
638 /* allocate a handle if one is not supplied */
640 hGlobal
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_NODISCARD
|GMEM_SHARE
, 0);
642 This
->handle
= handle_create(hGlobal
, fDeleteOnRelease
);
644 /* start at the beginning */
645 This
->currentPosition
.u
.HighPart
= 0;
646 This
->currentPosition
.u
.LowPart
= 0;
648 *ppstm
= &This
->IStream_iface
;
653 /***********************************************************************
654 * GetHGlobalFromStream [OLE32.@]
656 HRESULT WINAPI
GetHGlobalFromStream(IStream
* pstm
, HGLOBAL
* phglobal
)
658 HGLOBALStreamImpl
* pStream
;
660 if (!pstm
|| !phglobal
)
663 pStream
= impl_from_IStream(pstm
);
666 * Verify that the stream object was created with CreateStreamOnHGlobal.
668 if (pStream
->IStream_iface
.lpVtbl
== &HGLOBALStreamImplVtbl
)
669 *phglobal
= handle_gethglobal(pStream
->handle
);