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
26 WINE_DEFAULT_DEBUG_CHANNEL(storage
);
28 /****************************************************************************
29 * HGLOBALStreamImpl definition.
31 * This class implements the IStream interface and represents a stream
32 * supported by an HGLOBAL pointer.
36 IStream IStream_iface
;
39 /* support for the stream */
40 HGLOBAL supportHandle
;
42 /* if TRUE the HGLOBAL is destroyed when the stream is finally released */
45 /* size of the stream */
46 ULARGE_INTEGER streamSize
;
48 /* current position of the cursor */
49 ULARGE_INTEGER currentPosition
;
52 static inline HGLOBALStreamImpl
*impl_from_IStream(IStream
*iface
)
54 return CONTAINING_RECORD(iface
, HGLOBALStreamImpl
, IStream_iface
);
57 static HRESULT WINAPI
HGLOBALStreamImpl_QueryInterface(
59 REFIID riid
, /* [in] */
60 void** ppvObject
) /* [iid_is][out] */
62 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
69 if (IsEqualIID(&IID_IUnknown
, riid
) ||
70 IsEqualIID(&IID_ISequentialStream
, riid
) ||
71 IsEqualIID(&IID_IStream
, riid
))
73 *ppvObject
= &This
->IStream_iface
;
79 IStream_AddRef(iface
);
84 static ULONG WINAPI
HGLOBALStreamImpl_AddRef(IStream
* iface
)
86 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
87 return InterlockedIncrement(&This
->ref
);
90 static ULONG WINAPI
HGLOBALStreamImpl_Release(
93 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
94 ULONG ref
= InterlockedDecrement(&This
->ref
);
98 if (This
->deleteOnRelease
)
100 GlobalFree(This
->supportHandle
);
101 This
->supportHandle
= NULL
;
104 HeapFree(GetProcessHeap(), 0, This
);
111 * This method is part of the ISequentialStream interface.
113 * If reads a block of information from the stream at the current
114 * position. It then moves the current position at the end of the
117 * See the documentation of ISequentialStream for more info.
119 static HRESULT WINAPI
HGLOBALStreamImpl_Read(
121 void* pv
, /* [length_is][size_is][out] */
123 ULONG
* pcbRead
) /* [out] */
125 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
128 ULONG bytesReadBuffer
;
129 ULONG bytesToReadFromBuffer
;
131 TRACE("(%p, %p, %d, %p)\n", iface
,
135 * If the caller is not interested in the number of bytes read,
136 * we use another buffer to avoid "if" statements in the code.
139 pcbRead
= &bytesReadBuffer
;
142 * Using the known size of the stream, calculate the number of bytes
143 * to read from the block chain
145 bytesToReadFromBuffer
= min( This
->streamSize
.u
.LowPart
- This
->currentPosition
.u
.LowPart
, cb
);
148 * Lock the buffer in position and copy the data.
150 supportBuffer
= GlobalLock(This
->supportHandle
);
153 WARN("read from invalid hglobal %p\n", This
->supportHandle
);
158 memcpy(pv
, (char *) supportBuffer
+This
->currentPosition
.u
.LowPart
, bytesToReadFromBuffer
);
161 * Move the current position to the new position
163 This
->currentPosition
.u
.LowPart
+=bytesToReadFromBuffer
;
166 * Return the number of bytes read.
168 *pcbRead
= bytesToReadFromBuffer
;
173 GlobalUnlock(This
->supportHandle
);
176 * Always returns S_OK even if the end of the stream is reached before the
184 * This method is part of the ISequentialStream interface.
186 * It writes a block of information to the stream at the current
187 * position. It then moves the current position at the end of the
188 * written block. If the stream is too small to fit the block,
189 * the stream is grown to fit.
191 * See the documentation of ISequentialStream for more info.
193 static HRESULT WINAPI
HGLOBALStreamImpl_Write(
195 const void* pv
, /* [size_is][in] */
197 ULONG
* pcbWritten
) /* [out] */
199 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
202 ULARGE_INTEGER newSize
;
203 ULONG bytesWritten
= 0;
205 TRACE("(%p, %p, %d, %p)\n", iface
, pv
, cb
, pcbWritten
);
208 * If the caller is not interested in the number of bytes written,
209 * we use another buffer to avoid "if" statements in the code.
212 pcbWritten
= &bytesWritten
;
219 newSize
.u
.HighPart
= 0;
220 newSize
.u
.LowPart
= This
->currentPosition
.u
.LowPart
+ cb
;
223 * Verify if we need to grow the stream
225 if (newSize
.u
.LowPart
> This
->streamSize
.u
.LowPart
)
228 HRESULT hr
= IStream_SetSize(iface
, newSize
);
231 ERR("IStream_SetSize failed with error 0x%08x\n", hr
);
237 * Lock the buffer in position and copy the data.
239 supportBuffer
= GlobalLock(This
->supportHandle
);
242 WARN("write to invalid hglobal %p\n", This
->supportHandle
);
246 memcpy((char *) supportBuffer
+This
->currentPosition
.u
.LowPart
, pv
, cb
);
249 * Move the current position to the new position
251 This
->currentPosition
.u
.LowPart
+=cb
;
256 GlobalUnlock(This
->supportHandle
);
260 * Return the number of bytes read.
268 * This method is part of the IStream interface.
270 * It will move the current stream pointer according to the parameters
273 * See the documentation of IStream for more info.
275 static HRESULT WINAPI
HGLOBALStreamImpl_Seek(
277 LARGE_INTEGER dlibMove
, /* [in] */
278 DWORD dwOrigin
, /* [in] */
279 ULARGE_INTEGER
* plibNewPosition
) /* [out] */
281 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
283 ULARGE_INTEGER newPosition
= This
->currentPosition
;
286 TRACE("(%p, %x%08x, %d, %p)\n", iface
, dlibMove
.u
.HighPart
,
287 dlibMove
.u
.LowPart
, dwOrigin
, plibNewPosition
);
290 * The file pointer is moved depending on the given "function"
295 case STREAM_SEEK_SET
:
296 newPosition
.u
.HighPart
= 0;
297 newPosition
.u
.LowPart
= 0;
299 case STREAM_SEEK_CUR
:
301 case STREAM_SEEK_END
:
302 newPosition
= This
->streamSize
;
305 hr
= STG_E_SEEKERROR
;
310 * Move the actual file pointer
311 * If the file pointer ends-up after the end of the stream, the next Write operation will
312 * make the file larger. This is how it is documented.
314 newPosition
.u
.HighPart
= 0;
315 newPosition
.u
.LowPart
+= dlibMove
.QuadPart
;
317 if (dlibMove
.u
.LowPart
>= 0x80000000 &&
318 newPosition
.u
.LowPart
>= dlibMove
.u
.LowPart
)
320 /* We tried to seek backwards and went past the start. */
321 hr
= STG_E_SEEKERROR
;
325 This
->currentPosition
= newPosition
;
328 if (plibNewPosition
) *plibNewPosition
= This
->currentPosition
;
334 * This method is part of the IStream interface.
336 * It will change the size of a stream.
338 * TODO: Switch from small blocks to big blocks and vice versa.
340 * See the documentation of IStream for more info.
342 static HRESULT WINAPI
HGLOBALStreamImpl_SetSize(
344 ULARGE_INTEGER libNewSize
) /* [in] */
346 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
347 HGLOBAL supportHandle
;
349 TRACE("(%p, %d)\n", iface
, libNewSize
.u
.LowPart
);
352 * HighPart is ignored as shown in tests
355 if (This
->streamSize
.u
.LowPart
== libNewSize
.u
.LowPart
)
359 * Re allocate the HGlobal to fit the new size of the stream.
361 supportHandle
= GlobalReAlloc(This
->supportHandle
, libNewSize
.u
.LowPart
, 0);
363 if (supportHandle
== 0)
364 return E_OUTOFMEMORY
;
366 This
->supportHandle
= supportHandle
;
367 This
->streamSize
.u
.LowPart
= libNewSize
.u
.LowPart
;
373 * This method is part of the IStream interface.
375 * It will copy the 'cb' Bytes to 'pstm' IStream.
377 * See the documentation of IStream for more info.
379 static HRESULT WINAPI
HGLOBALStreamImpl_CopyTo(
381 IStream
* pstm
, /* [unique][in] */
382 ULARGE_INTEGER cb
, /* [in] */
383 ULARGE_INTEGER
* pcbRead
, /* [out] */
384 ULARGE_INTEGER
* pcbWritten
) /* [out] */
388 ULONG bytesRead
, bytesWritten
, copySize
;
389 ULARGE_INTEGER totalBytesRead
;
390 ULARGE_INTEGER totalBytesWritten
;
392 TRACE("(%p, %p, %d, %p, %p)\n", iface
, pstm
,
393 cb
.u
.LowPart
, pcbRead
, pcbWritten
);
396 return STG_E_INVALIDPOINTER
;
398 totalBytesRead
.QuadPart
= 0;
399 totalBytesWritten
.QuadPart
= 0;
401 while ( cb
.QuadPart
> 0 )
403 if ( cb
.QuadPart
>= sizeof(tmpBuffer
) )
404 copySize
= sizeof(tmpBuffer
);
406 copySize
= cb
.u
.LowPart
;
408 hr
= IStream_Read(iface
, tmpBuffer
, copySize
, &bytesRead
);
412 totalBytesRead
.QuadPart
+= bytesRead
;
416 hr
= IStream_Write(pstm
, tmpBuffer
, bytesRead
, &bytesWritten
);
420 totalBytesWritten
.QuadPart
+= bytesWritten
;
423 if (bytesRead
!=copySize
)
426 cb
.QuadPart
-= bytesRead
;
429 if (pcbRead
) pcbRead
->QuadPart
= totalBytesRead
.QuadPart
;
430 if (pcbWritten
) pcbWritten
->QuadPart
= totalBytesWritten
.QuadPart
;
436 * This method is part of the IStream interface.
438 * For streams supported by HGLOBALS, this function does nothing.
439 * This is what the documentation tells us.
441 * See the documentation of IStream for more info.
443 static HRESULT WINAPI
HGLOBALStreamImpl_Commit(
445 DWORD grfCommitFlags
) /* [in] */
451 * This method is part of the IStream interface.
453 * For streams supported by HGLOBALS, this function does nothing.
454 * This is what the documentation tells us.
456 * See the documentation of IStream for more info.
458 static HRESULT WINAPI
HGLOBALStreamImpl_Revert(
465 * This method is part of the IStream interface.
467 * For streams supported by HGLOBALS, this function does nothing.
468 * This is what the documentation tells us.
470 * See the documentation of IStream for more info.
472 static HRESULT WINAPI
HGLOBALStreamImpl_LockRegion(
474 ULARGE_INTEGER libOffset
, /* [in] */
475 ULARGE_INTEGER cb
, /* [in] */
476 DWORD dwLockType
) /* [in] */
478 return STG_E_INVALIDFUNCTION
;
482 * This method is part of the IStream interface.
484 * For streams supported by HGLOBALS, this function does nothing.
485 * This is what the documentation tells us.
487 * See the documentation of IStream for more info.
489 static HRESULT WINAPI
HGLOBALStreamImpl_UnlockRegion(
491 ULARGE_INTEGER libOffset
, /* [in] */
492 ULARGE_INTEGER cb
, /* [in] */
493 DWORD dwLockType
) /* [in] */
499 * This method is part of the IStream interface.
501 * This method returns information about the current
504 * See the documentation of IStream for more info.
506 static HRESULT WINAPI
HGLOBALStreamImpl_Stat(
508 STATSTG
* pstatstg
, /* [out] */
509 DWORD grfStatFlag
) /* [in] */
511 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
513 memset(pstatstg
, 0, sizeof(STATSTG
));
515 pstatstg
->pwcsName
= NULL
;
516 pstatstg
->type
= STGTY_STREAM
;
517 pstatstg
->cbSize
= This
->streamSize
;
522 static HRESULT WINAPI
HGLOBALStreamImpl_Clone(
524 IStream
** ppstm
) /* [out] */
526 HGLOBALStreamImpl
* This
= impl_from_IStream(iface
);
527 ULARGE_INTEGER dummy
;
528 LARGE_INTEGER offset
;
531 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface
,This
->deleteOnRelease
,(long)This
->currentPosition
.QuadPart
);
532 hr
= CreateStreamOnHGlobal(This
->supportHandle
, FALSE
, ppstm
);
535 offset
.QuadPart
= (LONGLONG
)This
->currentPosition
.QuadPart
;
536 IStream_Seek(*ppstm
, offset
, STREAM_SEEK_SET
, &dummy
);
540 static const IStreamVtbl HGLOBALStreamImplVtbl
=
542 HGLOBALStreamImpl_QueryInterface
,
543 HGLOBALStreamImpl_AddRef
,
544 HGLOBALStreamImpl_Release
,
545 HGLOBALStreamImpl_Read
,
546 HGLOBALStreamImpl_Write
,
547 HGLOBALStreamImpl_Seek
,
548 HGLOBALStreamImpl_SetSize
,
549 HGLOBALStreamImpl_CopyTo
,
550 HGLOBALStreamImpl_Commit
,
551 HGLOBALStreamImpl_Revert
,
552 HGLOBALStreamImpl_LockRegion
,
553 HGLOBALStreamImpl_UnlockRegion
,
554 HGLOBALStreamImpl_Stat
,
555 HGLOBALStreamImpl_Clone
558 /***********************************************************************
559 * CreateStreamOnHGlobal [OLE32.@]
561 HRESULT WINAPI
CreateStreamOnHGlobal(
563 BOOL fDeleteOnRelease
,
566 HGLOBALStreamImpl
* This
;
571 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl
));
572 if (!This
) return E_OUTOFMEMORY
;
574 This
->IStream_iface
.lpVtbl
= &HGLOBALStreamImplVtbl
;
577 /* initialize the support */
578 This
->supportHandle
= hGlobal
;
579 This
->deleteOnRelease
= fDeleteOnRelease
;
581 /* allocate a handle if one is not supplied */
582 if (!This
->supportHandle
)
583 This
->supportHandle
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_NODISCARD
|GMEM_SHARE
, 0);
585 /* start at the beginning */
586 This
->currentPosition
.u
.HighPart
= 0;
587 This
->currentPosition
.u
.LowPart
= 0;
589 /* initialize the size of the stream to the size of the handle */
590 This
->streamSize
.u
.HighPart
= 0;
591 This
->streamSize
.u
.LowPart
= GlobalSize(This
->supportHandle
);
593 *ppstm
= &This
->IStream_iface
;
598 /***********************************************************************
599 * GetHGlobalFromStream [OLE32.@]
601 HRESULT WINAPI
GetHGlobalFromStream(IStream
* pstm
, HGLOBAL
* phglobal
)
603 HGLOBALStreamImpl
* pStream
;
608 pStream
= (HGLOBALStreamImpl
*) pstm
;
611 * Verify that the stream object was created with CreateStreamOnHGlobal.
613 if (pStream
->IStream_iface
.lpVtbl
== &HGLOBALStreamImplVtbl
)
614 *phglobal
= pStream
->supportHandle
;