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.
56 IStream IStream_iface
;
59 /* support for the stream */
60 HGLOBAL supportHandle
;
62 /* if TRUE the HGLOBAL is destroyed when the stream is finally released */
65 /* size of the stream */
66 ULARGE_INTEGER streamSize
;
68 /* current position of the cursor */
69 ULARGE_INTEGER currentPosition
;
72 static inline HGLOBALStreamImpl
*impl_from_IStream(IStream
*iface
)
74 return CONTAINING_RECORD(iface
, HGLOBALStreamImpl
, IStream_iface
);
77 static HRESULT WINAPI
HGLOBALStreamImpl_QueryInterface(
79 REFIID riid
, /* [in] */
80 void** ppvObject
) /* [iid_is][out] */
82 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
89 if (IsEqualIID(&IID_IUnknown
, riid
) ||
90 IsEqualIID(&IID_ISequentialStream
, riid
) ||
91 IsEqualIID(&IID_IStream
, riid
))
99 IStream_AddRef(iface
);
104 static ULONG WINAPI
HGLOBALStreamImpl_AddRef(IStream
* iface
)
106 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
107 return InterlockedIncrement(&This
->ref
);
110 static ULONG WINAPI
HGLOBALStreamImpl_Release(
113 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
114 ULONG ref
= InterlockedDecrement(&This
->ref
);
118 if (This
->deleteOnRelease
)
120 GlobalFree(This
->supportHandle
);
121 This
->supportHandle
= NULL
;
124 HeapFree(GetProcessHeap(), 0, This
);
131 * This method is part of the ISequentialStream interface.
133 * If reads a block of information from the stream at the current
134 * position. It then moves the current position at the end of the
137 * See the documentation of ISequentialStream for more info.
139 static HRESULT WINAPI
HGLOBALStreamImpl_Read(
141 void* pv
, /* [length_is][size_is][out] */
143 ULONG
* pcbRead
) /* [out] */
145 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
148 ULONG bytesReadBuffer
;
149 ULONG bytesToReadFromBuffer
;
151 TRACE("(%p, %p, %d, %p)\n", iface
,
155 * If the caller is not interested in the number of bytes read,
156 * we use another buffer to avoid "if" statements in the code.
159 pcbRead
= &bytesReadBuffer
;
162 * Using the known size of the stream, calculate the number of bytes
163 * to read from the block chain
165 bytesToReadFromBuffer
= min( This
->streamSize
.u
.LowPart
- This
->currentPosition
.u
.LowPart
, cb
);
168 * Lock the buffer in position and copy the data.
170 supportBuffer
= GlobalLock(This
->supportHandle
);
173 WARN("read from invalid hglobal %p\n", This
->supportHandle
);
178 memcpy(pv
, (char *) supportBuffer
+This
->currentPosition
.u
.LowPart
, bytesToReadFromBuffer
);
181 * Move the current position to the new position
183 This
->currentPosition
.u
.LowPart
+=bytesToReadFromBuffer
;
186 * Return the number of bytes read.
188 *pcbRead
= bytesToReadFromBuffer
;
193 GlobalUnlock(This
->supportHandle
);
196 * Always returns S_OK even if the end of the stream is reached before the
204 * This method is part of the ISequentialStream interface.
206 * It writes a block of information to the stream at the current
207 * position. It then moves the current position at the end of the
208 * written block. If the stream is too small to fit the block,
209 * the stream is grown to fit.
211 * See the documentation of ISequentialStream for more info.
213 static HRESULT WINAPI
HGLOBALStreamImpl_Write(
215 const void* pv
, /* [size_is][in] */
217 ULONG
* pcbWritten
) /* [out] */
219 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
222 ULARGE_INTEGER newSize
;
223 ULONG bytesWritten
= 0;
225 TRACE("(%p, %p, %d, %p)\n", iface
, pv
, cb
, pcbWritten
);
228 * If the caller is not interested in the number of bytes written,
229 * we use another buffer to avoid "if" statements in the code.
232 pcbWritten
= &bytesWritten
;
239 newSize
.u
.HighPart
= 0;
240 newSize
.u
.LowPart
= This
->currentPosition
.u
.LowPart
+ cb
;
243 * Verify if we need to grow the stream
245 if (newSize
.u
.LowPart
> This
->streamSize
.u
.LowPart
)
248 HRESULT hr
= IStream_SetSize(iface
, newSize
);
251 ERR("IStream_SetSize failed with error 0x%08x\n", hr
);
257 * Lock the buffer in position and copy the data.
259 supportBuffer
= GlobalLock(This
->supportHandle
);
262 WARN("write to invalid hglobal %p\n", This
->supportHandle
);
266 memcpy((char *) supportBuffer
+This
->currentPosition
.u
.LowPart
, pv
, cb
);
269 * Move the current position to the new position
271 This
->currentPosition
.u
.LowPart
+=cb
;
276 GlobalUnlock(This
->supportHandle
);
280 * Return the number of bytes read.
288 * This method is part of the IStream interface.
290 * It will move the current stream pointer according to the parameters
293 * See the documentation of IStream for more info.
295 static HRESULT WINAPI
HGLOBALStreamImpl_Seek(
297 LARGE_INTEGER dlibMove
, /* [in] */
298 DWORD dwOrigin
, /* [in] */
299 ULARGE_INTEGER
* plibNewPosition
) /* [out] */
301 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
303 ULARGE_INTEGER newPosition
= This
->currentPosition
;
306 TRACE("(%p, %x%08x, %d, %p)\n", iface
, dlibMove
.u
.HighPart
,
307 dlibMove
.u
.LowPart
, dwOrigin
, plibNewPosition
);
310 * The file pointer is moved depending on the given "function"
315 case STREAM_SEEK_SET
:
316 newPosition
.u
.HighPart
= 0;
317 newPosition
.u
.LowPart
= 0;
319 case STREAM_SEEK_CUR
:
321 case STREAM_SEEK_END
:
322 newPosition
= This
->streamSize
;
325 hr
= STG_E_SEEKERROR
;
330 * Move the actual file pointer
331 * If the file pointer ends-up after the end of the stream, the next Write operation will
332 * make the file larger. This is how it is documented.
334 newPosition
.u
.HighPart
= 0;
335 newPosition
.u
.LowPart
+= dlibMove
.QuadPart
;
337 if (dlibMove
.u
.LowPart
>= 0x80000000 &&
338 newPosition
.u
.LowPart
>= dlibMove
.u
.LowPart
)
340 /* We tried to seek backwards and went past the start. */
341 hr
= STG_E_SEEKERROR
;
345 This
->currentPosition
= newPosition
;
348 if (plibNewPosition
) *plibNewPosition
= This
->currentPosition
;
354 * This method is part of the IStream interface.
356 * It will change the size of a stream.
358 * TODO: Switch from small blocks to big blocks and vice versa.
360 * See the documentation of IStream for more info.
362 static HRESULT WINAPI
HGLOBALStreamImpl_SetSize(
364 ULARGE_INTEGER libNewSize
) /* [in] */
366 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
367 HGLOBAL supportHandle
;
369 TRACE("(%p, %d)\n", iface
, libNewSize
.u
.LowPart
);
372 * HighPart is ignored as shown in tests
375 if (This
->streamSize
.u
.LowPart
== libNewSize
.u
.LowPart
)
379 * Re allocate the HGlobal to fit the new size of the stream.
381 supportHandle
= GlobalReAlloc(This
->supportHandle
, libNewSize
.u
.LowPart
, 0);
383 if (supportHandle
== 0)
384 return E_OUTOFMEMORY
;
386 This
->supportHandle
= supportHandle
;
387 This
->streamSize
.u
.LowPart
= 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
= This
->streamSize
;
542 static HRESULT WINAPI
HGLOBALStreamImpl_Clone(
544 IStream
** ppstm
) /* [out] */
546 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
547 ULARGE_INTEGER dummy
;
548 LARGE_INTEGER offset
;
551 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface
,This
->deleteOnRelease
,(long)This
->currentPosition
.QuadPart
);
552 hr
= CreateStreamOnHGlobal(This
->supportHandle
, FALSE
, ppstm
);
555 offset
.QuadPart
= (LONGLONG
)This
->currentPosition
.QuadPart
;
556 IStream_Seek(*ppstm
, offset
, STREAM_SEEK_SET
, &dummy
);
560 static const IStreamVtbl HGLOBALStreamImplVtbl
=
562 HGLOBALStreamImpl_QueryInterface
,
563 HGLOBALStreamImpl_AddRef
,
564 HGLOBALStreamImpl_Release
,
565 HGLOBALStreamImpl_Read
,
566 HGLOBALStreamImpl_Write
,
567 HGLOBALStreamImpl_Seek
,
568 HGLOBALStreamImpl_SetSize
,
569 HGLOBALStreamImpl_CopyTo
,
570 HGLOBALStreamImpl_Commit
,
571 HGLOBALStreamImpl_Revert
,
572 HGLOBALStreamImpl_LockRegion
,
573 HGLOBALStreamImpl_UnlockRegion
,
574 HGLOBALStreamImpl_Stat
,
575 HGLOBALStreamImpl_Clone
578 /***********************************************************************
579 * CreateStreamOnHGlobal [OLE32.@]
581 HRESULT WINAPI
CreateStreamOnHGlobal(
583 BOOL fDeleteOnRelease
,
586 HGLOBALStreamImpl
* This
;
591 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl
));
592 if (!This
) return E_OUTOFMEMORY
;
594 This
->IStream_iface
.lpVtbl
= &HGLOBALStreamImplVtbl
;
597 /* initialize the support */
598 This
->supportHandle
= hGlobal
;
599 This
->deleteOnRelease
= fDeleteOnRelease
;
601 /* allocate a handle if one is not supplied */
602 if (!This
->supportHandle
)
603 This
->supportHandle
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_NODISCARD
|GMEM_SHARE
, 0);
605 /* start at the beginning */
606 This
->currentPosition
.u
.HighPart
= 0;
607 This
->currentPosition
.u
.LowPart
= 0;
609 /* initialize the size of the stream to the size of the handle */
610 This
->streamSize
.u
.HighPart
= 0;
611 This
->streamSize
.u
.LowPart
= GlobalSize(This
->supportHandle
);
613 *ppstm
= &This
->IStream_iface
;
618 /***********************************************************************
619 * GetHGlobalFromStream [OLE32.@]
621 HRESULT WINAPI
GetHGlobalFromStream(IStream
* pstm
, HGLOBAL
* phglobal
)
623 HGLOBALStreamImpl
* pStream
;
628 pStream
= (HGLOBALStreamImpl
*) pstm
;
631 * Verify that the stream object was created with CreateStreamOnHGlobal.
633 if (pStream
->IStream_iface
.lpVtbl
== &HGLOBALStreamImplVtbl
)
634 *phglobal
= pStream
->supportHandle
;