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
27 WINE_DEFAULT_DEBUG_CHANNEL(hglobalstream
);
34 BOOL delete_on_release
;
35 CRITICAL_SECTION lock
;
38 static void handle_addref(struct handle_wrapper
*handle
)
40 InterlockedIncrement(&handle
->ref
);
43 static void handle_release(struct handle_wrapper
*handle
)
45 ULONG ref
= InterlockedDecrement(&handle
->ref
);
49 if (handle
->delete_on_release
)
51 GlobalFree(handle
->hglobal
);
52 handle
->hglobal
= NULL
;
55 handle
->lock
.DebugInfo
->Spare
[0] = 0;
56 DeleteCriticalSection(&handle
->lock
);
57 HeapFree(GetProcessHeap(), 0, handle
);
61 static ULONG
handle_read(struct handle_wrapper
*handle
, ULONG
*pos
, void *dest
, ULONG len
)
65 EnterCriticalSection(&handle
->lock
);
67 if (*pos
< handle
->size
)
68 len
= min(handle
->size
- *pos
, len
);
72 source
= GlobalLock(handle
->hglobal
);
75 memcpy(dest
, (char *)source
+ *pos
, len
);
77 GlobalUnlock(handle
->hglobal
);
81 WARN("read from invalid hglobal %p\n", handle
->hglobal
);
85 LeaveCriticalSection(&handle
->lock
);
89 static ULONG
handle_write(struct handle_wrapper
*handle
, ULONG
*pos
, const void *source
, ULONG len
)
96 EnterCriticalSection(&handle
->lock
);
98 if (*pos
+ len
> handle
->size
)
100 HGLOBAL hglobal
= GlobalReAlloc(handle
->hglobal
, *pos
+ len
, GMEM_MOVEABLE
);
103 handle
->hglobal
= hglobal
;
104 handle
->size
= *pos
+ len
;
113 dest
= GlobalLock(handle
->hglobal
);
116 memcpy((char *)dest
+ *pos
, source
, len
);
118 GlobalUnlock(handle
->hglobal
);
122 WARN("write to invalid hglobal %p\n", handle
->hglobal
);
127 LeaveCriticalSection(&handle
->lock
);
131 static HGLOBAL
handle_gethglobal(struct handle_wrapper
*handle
)
133 return handle
->hglobal
;
136 static HRESULT
handle_setsize(struct handle_wrapper
*handle
, ULONG size
)
140 EnterCriticalSection(&handle
->lock
);
142 if (handle
->size
!= size
)
144 HGLOBAL hglobal
= GlobalReAlloc(handle
->hglobal
, size
, GMEM_MOVEABLE
);
147 handle
->hglobal
= hglobal
;
154 LeaveCriticalSection(&handle
->lock
);
158 static ULONG
handle_getsize(struct handle_wrapper
*handle
)
163 static struct handle_wrapper
*handle_create(HGLOBAL hglobal
, BOOL delete_on_release
)
165 struct handle_wrapper
*handle
;
167 handle
= HeapAlloc(GetProcessHeap(), 0, sizeof(*handle
));
171 handle
->hglobal
= hglobal
;
172 handle
->size
= GlobalSize(hglobal
);
173 handle
->delete_on_release
= delete_on_release
;
174 InitializeCriticalSection(&handle
->lock
);
175 handle
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": handle_wrapper.lock");
180 /****************************************************************************
181 * HGLOBALStreamImpl definition.
183 * This class implements the IStream interface and represents a stream
184 * supported by an HGLOBAL pointer.
188 IStream IStream_iface
;
191 struct handle_wrapper
*handle
;
193 /* current position of the cursor */
194 ULARGE_INTEGER currentPosition
;
197 static inline HGLOBALStreamImpl
*impl_from_IStream(IStream
*iface
)
199 return CONTAINING_RECORD(iface
, HGLOBALStreamImpl
, IStream_iface
);
202 static HRESULT WINAPI
HGLOBALStreamImpl_QueryInterface(
204 REFIID riid
, /* [in] */
205 void** ppvObject
) /* [iid_is][out] */
207 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
214 if (IsEqualIID(&IID_IUnknown
, riid
) ||
215 IsEqualIID(&IID_ISequentialStream
, riid
) ||
216 IsEqualIID(&IID_IStream
, riid
))
218 *ppvObject
= &This
->IStream_iface
;
222 return E_NOINTERFACE
;
224 IStream_AddRef(iface
);
229 static ULONG WINAPI
HGLOBALStreamImpl_AddRef(IStream
* iface
)
231 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
232 return InterlockedIncrement(&This
->ref
);
235 static ULONG WINAPI
HGLOBALStreamImpl_Release(
238 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
239 ULONG ref
= InterlockedDecrement(&This
->ref
);
243 handle_release(This
->handle
);
244 HeapFree(GetProcessHeap(), 0, This
);
251 * This method is part of the ISequentialStream interface.
253 * If reads a block of information from the stream at the current
254 * position. It then moves the current position at the end of the
257 * See the documentation of ISequentialStream for more info.
259 static HRESULT WINAPI
HGLOBALStreamImpl_Read(
261 void* pv
, /* [length_is][size_is][out] */
263 ULONG
* pcbRead
) /* [out] */
265 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
268 TRACE("(%p, %p, %d, %p)\n", iface
, pv
, cb
, pcbRead
);
270 num_bytes
= handle_read(This
->handle
, &This
->currentPosition
.u
.LowPart
, pv
, cb
);
271 if (pcbRead
) *pcbRead
= num_bytes
;
277 * This method is part of the ISequentialStream interface.
279 * It writes a block of information to the stream at the current
280 * position. It then moves the current position at the end of the
281 * written block. If the stream is too small to fit the block,
282 * the stream is grown to fit.
284 * See the documentation of ISequentialStream for more info.
286 static HRESULT WINAPI
HGLOBALStreamImpl_Write(
288 const void* pv
, /* [size_is][in] */
290 ULONG
* pcbWritten
) /* [out] */
292 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
295 TRACE("(%p, %p, %d, %p)\n", iface
, pv
, cb
, pcbWritten
);
297 num_bytes
= handle_write(This
->handle
, &This
->currentPosition
.u
.LowPart
, pv
, cb
);
298 if (pcbWritten
) *pcbWritten
= num_bytes
;
300 return (num_bytes
< cb
) ? E_OUTOFMEMORY
: S_OK
;
304 * This method is part of the IStream interface.
306 * It will move the current stream pointer according to the parameters
309 * See the documentation of IStream for more info.
311 static HRESULT WINAPI
HGLOBALStreamImpl_Seek(
313 LARGE_INTEGER dlibMove
, /* [in] */
314 DWORD dwOrigin
, /* [in] */
315 ULARGE_INTEGER
* plibNewPosition
) /* [out] */
317 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
319 ULARGE_INTEGER newPosition
= This
->currentPosition
;
322 TRACE("(%p, %x%08x, %d, %p)\n", iface
, dlibMove
.u
.HighPart
,
323 dlibMove
.u
.LowPart
, dwOrigin
, plibNewPosition
);
326 * The file pointer is moved depending on the given "function"
331 case STREAM_SEEK_SET
:
332 newPosition
.u
.HighPart
= 0;
333 newPosition
.u
.LowPart
= 0;
335 case STREAM_SEEK_CUR
:
337 case STREAM_SEEK_END
:
338 newPosition
.QuadPart
= handle_getsize(This
->handle
);
341 hr
= STG_E_SEEKERROR
;
346 * Move the actual file pointer
347 * If the file pointer ends-up after the end of the stream, the next Write operation will
348 * make the file larger. This is how it is documented.
350 newPosition
.u
.HighPart
= 0;
351 newPosition
.u
.LowPart
+= dlibMove
.QuadPart
;
353 if (dlibMove
.u
.LowPart
>= 0x80000000 &&
354 newPosition
.u
.LowPart
>= dlibMove
.u
.LowPart
)
356 /* We tried to seek backwards and went past the start. */
357 hr
= STG_E_SEEKERROR
;
361 This
->currentPosition
= newPosition
;
364 if (plibNewPosition
) *plibNewPosition
= This
->currentPosition
;
370 * This method is part of the IStream interface.
372 * It will change the size of a stream.
374 * TODO: Switch from small blocks to big blocks and vice versa.
376 * See the documentation of IStream for more info.
378 static HRESULT WINAPI
HGLOBALStreamImpl_SetSize(
380 ULARGE_INTEGER libNewSize
) /* [in] */
382 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
384 TRACE("(%p, %d)\n", iface
, libNewSize
.u
.LowPart
);
387 * HighPart is ignored as shown in tests
389 return handle_setsize(This
->handle
, libNewSize
.u
.LowPart
);
393 * This method is part of the IStream interface.
395 * It will copy the 'cb' Bytes to 'pstm' IStream.
397 * See the documentation of IStream for more info.
399 static HRESULT WINAPI
HGLOBALStreamImpl_CopyTo(
401 IStream
* pstm
, /* [unique][in] */
402 ULARGE_INTEGER cb
, /* [in] */
403 ULARGE_INTEGER
* pcbRead
, /* [out] */
404 ULARGE_INTEGER
* pcbWritten
) /* [out] */
408 ULONG bytesRead
, bytesWritten
, copySize
;
409 ULARGE_INTEGER totalBytesRead
;
410 ULARGE_INTEGER totalBytesWritten
;
412 TRACE("(%p, %p, %d, %p, %p)\n", iface
, pstm
,
413 cb
.u
.LowPart
, pcbRead
, pcbWritten
);
416 return STG_E_INVALIDPOINTER
;
418 totalBytesRead
.QuadPart
= 0;
419 totalBytesWritten
.QuadPart
= 0;
421 while ( cb
.QuadPart
> 0 )
423 if ( cb
.QuadPart
>= sizeof(tmpBuffer
) )
424 copySize
= sizeof(tmpBuffer
);
426 copySize
= cb
.u
.LowPart
;
428 hr
= IStream_Read(iface
, tmpBuffer
, copySize
, &bytesRead
);
432 totalBytesRead
.QuadPart
+= bytesRead
;
436 hr
= IStream_Write(pstm
, tmpBuffer
, bytesRead
, &bytesWritten
);
440 totalBytesWritten
.QuadPart
+= bytesWritten
;
443 if (bytesRead
!=copySize
)
446 cb
.QuadPart
-= bytesRead
;
449 if (pcbRead
) pcbRead
->QuadPart
= totalBytesRead
.QuadPart
;
450 if (pcbWritten
) pcbWritten
->QuadPart
= totalBytesWritten
.QuadPart
;
456 * This method is part of the IStream interface.
458 * For streams supported by HGLOBALS, this function does nothing.
459 * This is what the documentation tells us.
461 * See the documentation of IStream for more info.
463 static HRESULT WINAPI
HGLOBALStreamImpl_Commit(
465 DWORD grfCommitFlags
) /* [in] */
471 * This method is part of the IStream interface.
473 * For streams supported by HGLOBALS, this function does nothing.
474 * This is what the documentation tells us.
476 * See the documentation of IStream for more info.
478 static HRESULT WINAPI
HGLOBALStreamImpl_Revert(
485 * This method is part of the IStream interface.
487 * For streams supported by HGLOBALS, this function does nothing.
488 * This is what the documentation tells us.
490 * See the documentation of IStream for more info.
492 static HRESULT WINAPI
HGLOBALStreamImpl_LockRegion(
494 ULARGE_INTEGER libOffset
, /* [in] */
495 ULARGE_INTEGER cb
, /* [in] */
496 DWORD dwLockType
) /* [in] */
498 return STG_E_INVALIDFUNCTION
;
502 * This method is part of the IStream interface.
504 * For streams supported by HGLOBALS, this function does nothing.
505 * This is what the documentation tells us.
507 * See the documentation of IStream for more info.
509 static HRESULT WINAPI
HGLOBALStreamImpl_UnlockRegion(
511 ULARGE_INTEGER libOffset
, /* [in] */
512 ULARGE_INTEGER cb
, /* [in] */
513 DWORD dwLockType
) /* [in] */
519 * This method is part of the IStream interface.
521 * This method returns information about the current
524 * See the documentation of IStream for more info.
526 static HRESULT WINAPI
HGLOBALStreamImpl_Stat(
528 STATSTG
* pstatstg
, /* [out] */
529 DWORD grfStatFlag
) /* [in] */
531 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
533 memset(pstatstg
, 0, sizeof(STATSTG
));
535 pstatstg
->pwcsName
= NULL
;
536 pstatstg
->type
= STGTY_STREAM
;
537 pstatstg
->cbSize
.QuadPart
= handle_getsize(This
->handle
);
542 static const IStreamVtbl HGLOBALStreamImplVtbl
;
544 static HGLOBALStreamImpl
*HGLOBALStreamImpl_Create(void)
546 HGLOBALStreamImpl
*This
;
548 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
551 This
->IStream_iface
.lpVtbl
= &HGLOBALStreamImplVtbl
;
557 static HRESULT WINAPI
HGLOBALStreamImpl_Clone(
559 IStream
** ppstm
) /* [out] */
561 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
562 HGLOBALStreamImpl
* clone
;
563 ULARGE_INTEGER dummy
;
564 LARGE_INTEGER offset
;
566 if (!ppstm
) return E_INVALIDARG
;
570 TRACE(" Cloning %p (seek position=%d)\n", iface
, This
->currentPosition
.u
.LowPart
);
572 clone
= HGLOBALStreamImpl_Create();
573 if (!clone
) return E_OUTOFMEMORY
;
575 *ppstm
= &clone
->IStream_iface
;
577 handle_addref(This
->handle
);
578 clone
->handle
= This
->handle
;
580 offset
.QuadPart
= (LONGLONG
)This
->currentPosition
.QuadPart
;
581 IStream_Seek(*ppstm
, offset
, STREAM_SEEK_SET
, &dummy
);
585 static const IStreamVtbl HGLOBALStreamImplVtbl
=
587 HGLOBALStreamImpl_QueryInterface
,
588 HGLOBALStreamImpl_AddRef
,
589 HGLOBALStreamImpl_Release
,
590 HGLOBALStreamImpl_Read
,
591 HGLOBALStreamImpl_Write
,
592 HGLOBALStreamImpl_Seek
,
593 HGLOBALStreamImpl_SetSize
,
594 HGLOBALStreamImpl_CopyTo
,
595 HGLOBALStreamImpl_Commit
,
596 HGLOBALStreamImpl_Revert
,
597 HGLOBALStreamImpl_LockRegion
,
598 HGLOBALStreamImpl_UnlockRegion
,
599 HGLOBALStreamImpl_Stat
,
600 HGLOBALStreamImpl_Clone
603 /***********************************************************************
604 * CreateStreamOnHGlobal [OLE32.@]
606 HRESULT WINAPI
CreateStreamOnHGlobal(
608 BOOL fDeleteOnRelease
,
611 HGLOBALStreamImpl
* This
;
616 This
= HGLOBALStreamImpl_Create();
617 if (!This
) return E_OUTOFMEMORY
;
619 /* allocate a handle if one is not supplied */
621 hGlobal
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_NODISCARD
|GMEM_SHARE
, 0);
623 This
->handle
= handle_create(hGlobal
, fDeleteOnRelease
);
625 /* start at the beginning */
626 This
->currentPosition
.u
.HighPart
= 0;
627 This
->currentPosition
.u
.LowPart
= 0;
629 *ppstm
= &This
->IStream_iface
;
634 /***********************************************************************
635 * GetHGlobalFromStream [OLE32.@]
637 HRESULT WINAPI
GetHGlobalFromStream(IStream
* pstm
, HGLOBAL
* phglobal
)
639 HGLOBALStreamImpl
* pStream
;
641 if (!pstm
|| !phglobal
)
644 pStream
= impl_from_IStream(pstm
);
647 * Verify that the stream object was created with CreateStreamOnHGlobal.
649 if (pStream
->IStream_iface
.lpVtbl
== &HGLOBALStreamImplVtbl
)
650 *phglobal
= handle_gethglobal(pStream
->handle
);