1999-04-24 14:00:31 +02:00
|
|
|
/*
|
|
|
|
* HGLOBAL Stream implementation
|
|
|
|
*
|
|
|
|
* This file contains the implementation of the stream interface
|
1999-12-27 06:26:00 +01:00
|
|
|
* for streams contained supported by an HGLOBAL pointer.
|
1999-04-24 14:00:31 +02:00
|
|
|
*
|
|
|
|
* Copyright 1999 Francis Beaudet
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
1999-04-24 14:00:31 +02:00
|
|
|
*/
|
2001-07-20 20:00:00 +02:00
|
|
|
|
1999-04-24 14:00:31 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
1999-04-24 14:00:31 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2004-10-07 05:06:48 +02:00
|
|
|
#define COBJMACROS
|
2003-01-07 21:36:20 +01:00
|
|
|
#define NONAMELESSUNION
|
2004-10-07 05:06:48 +02:00
|
|
|
|
2001-07-20 20:00:00 +02:00
|
|
|
#include "windef.h"
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "winbase.h"
|
2003-09-09 21:39:31 +02:00
|
|
|
#include "winuser.h"
|
2001-07-20 20:00:00 +02:00
|
|
|
#include "objbase.h"
|
|
|
|
#include "ole2.h"
|
1999-04-24 14:00:31 +02:00
|
|
|
#include "winerror.h"
|
2002-09-13 00:07:02 +02:00
|
|
|
#include "winternl.h"
|
1999-04-24 14:00:31 +02:00
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
#include "wine/debug.h"
|
1999-04-24 14:00:31 +02:00
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(storage);
|
1999-04-24 14:00:31 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* HGLOBALStreamImpl definition.
|
|
|
|
*
|
2006-11-21 21:44:54 +01:00
|
|
|
* This class implements the IStream interface and represents a stream
|
1999-04-24 14:00:31 +02:00
|
|
|
* supported by an HGLOBAL pointer.
|
|
|
|
*/
|
2011-12-12 22:22:03 +01:00
|
|
|
typedef struct
|
1999-04-24 14:00:31 +02:00
|
|
|
{
|
2011-12-12 22:22:03 +01:00
|
|
|
IStream IStream_iface;
|
|
|
|
LONG ref;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2011-12-12 22:22:03 +01:00
|
|
|
/* support for the stream */
|
1999-04-24 14:00:31 +02:00
|
|
|
HGLOBAL supportHandle;
|
|
|
|
|
2011-12-12 22:22:03 +01:00
|
|
|
/* if TRUE the HGLOBAL is destroyed when the stream is finally released */
|
|
|
|
BOOL deleteOnRelease;
|
1999-04-24 14:00:31 +02:00
|
|
|
|
2011-12-12 22:22:03 +01:00
|
|
|
/* size of the stream */
|
|
|
|
ULARGE_INTEGER streamSize;
|
1999-04-24 14:00:31 +02:00
|
|
|
|
2011-12-12 22:22:03 +01:00
|
|
|
/* current position of the cursor */
|
|
|
|
ULARGE_INTEGER currentPosition;
|
|
|
|
} HGLOBALStreamImpl;
|
1999-04-24 14:00:31 +02:00
|
|
|
|
2011-12-12 22:22:03 +01:00
|
|
|
static inline HGLOBALStreamImpl *impl_from_IStream(IStream *iface)
|
|
|
|
{
|
|
|
|
return CONTAINING_RECORD(iface, HGLOBALStreamImpl, IStream_iface);
|
|
|
|
}
|
1999-04-24 14:00:31 +02:00
|
|
|
|
2005-05-16 10:47:14 +02:00
|
|
|
static HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
|
1999-04-24 14:00:31 +02:00
|
|
|
IStream* iface,
|
2002-06-01 01:06:46 +02:00
|
|
|
REFIID riid, /* [in] */
|
|
|
|
void** ppvObject) /* [iid_is][out] */
|
1999-04-24 14:00:31 +02:00
|
|
|
{
|
2011-12-12 22:22:03 +01:00
|
|
|
HGLOBALStreamImpl* This = impl_from_IStream(iface);
|
1999-04-24 14:00:31 +02:00
|
|
|
|
|
|
|
if (ppvObject==0)
|
|
|
|
return E_INVALIDARG;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-04-24 14:00:31 +02:00
|
|
|
*ppvObject = 0;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2007-01-07 13:15:59 +01:00
|
|
|
if (IsEqualIID(&IID_IUnknown, riid) ||
|
|
|
|
IsEqualIID(&IID_ISequentialStream, riid) ||
|
|
|
|
IsEqualIID(&IID_IStream, riid))
|
1999-04-24 14:00:31 +02:00
|
|
|
{
|
2015-03-20 08:15:42 +01:00
|
|
|
*ppvObject = &This->IStream_iface;
|
1999-04-24 14:00:31 +02:00
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-04-24 14:00:31 +02:00
|
|
|
if ((*ppvObject)==0)
|
|
|
|
return E_NOINTERFACE;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2011-12-12 22:43:24 +01:00
|
|
|
IStream_AddRef(iface);
|
2002-06-01 01:06:46 +02:00
|
|
|
|
|
|
|
return S_OK;
|
1999-04-24 14:00:31 +02:00
|
|
|
}
|
|
|
|
|
2011-12-12 22:43:24 +01:00
|
|
|
static ULONG WINAPI HGLOBALStreamImpl_AddRef(IStream* iface)
|
|
|
|
{
|
|
|
|
HGLOBALStreamImpl* This = impl_from_IStream(iface);
|
|
|
|
return InterlockedIncrement(&This->ref);
|
|
|
|
}
|
|
|
|
|
2005-05-16 10:47:14 +02:00
|
|
|
static ULONG WINAPI HGLOBALStreamImpl_Release(
|
1999-04-24 14:00:31 +02:00
|
|
|
IStream* iface)
|
|
|
|
{
|
2011-12-12 22:22:03 +01:00
|
|
|
HGLOBALStreamImpl* This= impl_from_IStream(iface);
|
|
|
|
ULONG ref = InterlockedDecrement(&This->ref);
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2011-12-12 22:22:03 +01:00
|
|
|
if (!ref)
|
2011-12-12 22:43:24 +01:00
|
|
|
{
|
|
|
|
if (This->deleteOnRelease)
|
|
|
|
{
|
|
|
|
GlobalFree(This->supportHandle);
|
|
|
|
This->supportHandle = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, This);
|
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2011-12-12 22:22:03 +01:00
|
|
|
return ref;
|
1999-04-24 14:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* This method is part of the ISequentialStream interface.
|
|
|
|
*
|
|
|
|
* If reads a block of information from the stream at the current
|
|
|
|
* position. It then moves the current position at the end of the
|
|
|
|
* read block
|
|
|
|
*
|
|
|
|
* See the documentation of ISequentialStream for more info.
|
|
|
|
*/
|
2005-05-16 10:47:14 +02:00
|
|
|
static HRESULT WINAPI HGLOBALStreamImpl_Read(
|
1999-04-24 14:00:31 +02:00
|
|
|
IStream* iface,
|
|
|
|
void* pv, /* [length_is][size_is][out] */
|
2002-06-01 01:06:46 +02:00
|
|
|
ULONG cb, /* [in] */
|
|
|
|
ULONG* pcbRead) /* [out] */
|
1999-04-24 14:00:31 +02:00
|
|
|
{
|
2011-12-12 22:22:03 +01:00
|
|
|
HGLOBALStreamImpl* This = impl_from_IStream(iface);
|
1999-04-24 14:00:31 +02:00
|
|
|
|
|
|
|
void* supportBuffer;
|
|
|
|
ULONG bytesReadBuffer;
|
|
|
|
ULONG bytesToReadFromBuffer;
|
1999-05-02 13:41:10 +02:00
|
|
|
|
2006-10-15 12:28:08 +02:00
|
|
|
TRACE("(%p, %p, %d, %p)\n", iface,
|
1999-05-02 13:41:10 +02:00
|
|
|
pv, cb, pcbRead);
|
2002-06-01 01:06:46 +02:00
|
|
|
|
|
|
|
/*
|
2011-08-02 22:20:32 +02:00
|
|
|
* If the caller is not interested in the number of bytes read,
|
1999-04-24 14:00:31 +02:00
|
|
|
* we use another buffer to avoid "if" statements in the code.
|
|
|
|
*/
|
|
|
|
if (pcbRead==0)
|
|
|
|
pcbRead = &bytesReadBuffer;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-04-24 14:00:31 +02:00
|
|
|
/*
|
|
|
|
* Using the known size of the stream, calculate the number of bytes
|
|
|
|
* to read from the block chain
|
|
|
|
*/
|
2004-01-23 02:51:33 +01:00
|
|
|
bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
|
1999-04-24 14:00:31 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Lock the buffer in position and copy the data.
|
|
|
|
*/
|
|
|
|
supportBuffer = GlobalLock(This->supportHandle);
|
2007-05-17 18:06:56 +02:00
|
|
|
if (!supportBuffer)
|
|
|
|
{
|
|
|
|
WARN("read from invalid hglobal %p\n", This->supportHandle);
|
|
|
|
*pcbRead = 0;
|
|
|
|
return S_OK;
|
|
|
|
}
|
1999-04-24 14:00:31 +02:00
|
|
|
|
2004-01-23 02:51:33 +01:00
|
|
|
memcpy(pv, (char *) supportBuffer+This->currentPosition.u.LowPart, bytesToReadFromBuffer);
|
1999-04-24 14:00:31 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Move the current position to the new position
|
|
|
|
*/
|
2004-01-23 02:51:33 +01:00
|
|
|
This->currentPosition.u.LowPart+=bytesToReadFromBuffer;
|
1999-04-24 14:00:31 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the number of bytes read.
|
|
|
|
*/
|
|
|
|
*pcbRead = bytesToReadFromBuffer;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cleanup
|
|
|
|
*/
|
|
|
|
GlobalUnlock(This->supportHandle);
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-04-24 14:00:31 +02:00
|
|
|
/*
|
2006-09-11 12:11:56 +02:00
|
|
|
* Always returns S_OK even if the end of the stream is reached before the
|
1999-04-24 14:00:31 +02:00
|
|
|
* buffer is filled
|
|
|
|
*/
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2006-09-11 12:11:56 +02:00
|
|
|
return S_OK;
|
1999-04-24 14:00:31 +02:00
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-04-24 14:00:31 +02:00
|
|
|
/***
|
|
|
|
* This method is part of the ISequentialStream interface.
|
|
|
|
*
|
|
|
|
* It writes a block of information to the stream at the current
|
|
|
|
* position. It then moves the current position at the end of the
|
|
|
|
* written block. If the stream is too small to fit the block,
|
|
|
|
* the stream is grown to fit.
|
|
|
|
*
|
|
|
|
* See the documentation of ISequentialStream for more info.
|
|
|
|
*/
|
2005-05-16 10:47:14 +02:00
|
|
|
static HRESULT WINAPI HGLOBALStreamImpl_Write(
|
1999-04-24 14:00:31 +02:00
|
|
|
IStream* iface,
|
2002-06-01 01:06:46 +02:00
|
|
|
const void* pv, /* [size_is][in] */
|
|
|
|
ULONG cb, /* [in] */
|
|
|
|
ULONG* pcbWritten) /* [out] */
|
1999-04-24 14:00:31 +02:00
|
|
|
{
|
2011-12-12 22:22:03 +01:00
|
|
|
HGLOBALStreamImpl* This = impl_from_IStream(iface);
|
1999-04-24 14:00:31 +02:00
|
|
|
|
|
|
|
void* supportBuffer;
|
|
|
|
ULARGE_INTEGER newSize;
|
|
|
|
ULONG bytesWritten = 0;
|
1999-05-02 13:41:10 +02:00
|
|
|
|
2006-10-15 12:28:08 +02:00
|
|
|
TRACE("(%p, %p, %d, %p)\n", iface, pv, cb, pcbWritten);
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-04-24 14:00:31 +02:00
|
|
|
/*
|
|
|
|
* If the caller is not interested in the number of bytes written,
|
|
|
|
* we use another buffer to avoid "if" statements in the code.
|
|
|
|
*/
|
|
|
|
if (pcbWritten == 0)
|
|
|
|
pcbWritten = &bytesWritten;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-04-24 14:00:31 +02:00
|
|
|
if (cb == 0)
|
2006-08-14 07:06:53 +02:00
|
|
|
goto out;
|
|
|
|
|
2007-05-17 18:06:56 +02:00
|
|
|
*pcbWritten = 0;
|
|
|
|
|
2006-08-14 07:06:53 +02:00
|
|
|
newSize.u.HighPart = 0;
|
|
|
|
newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-04-24 14:00:31 +02:00
|
|
|
/*
|
|
|
|
* Verify if we need to grow the stream
|
|
|
|
*/
|
2004-01-23 02:51:33 +01:00
|
|
|
if (newSize.u.LowPart > This->streamSize.u.LowPart)
|
1999-04-24 14:00:31 +02:00
|
|
|
{
|
|
|
|
/* grow stream */
|
2006-01-03 12:07:49 +01:00
|
|
|
HRESULT hr = IStream_SetSize(iface, newSize);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2006-10-15 12:28:08 +02:00
|
|
|
ERR("IStream_SetSize failed with error 0x%08x\n", hr);
|
2006-01-03 12:07:49 +01:00
|
|
|
return hr;
|
|
|
|
}
|
1999-04-24 14:00:31 +02:00
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-04-24 14:00:31 +02:00
|
|
|
/*
|
|
|
|
* Lock the buffer in position and copy the data.
|
|
|
|
*/
|
|
|
|
supportBuffer = GlobalLock(This->supportHandle);
|
2007-05-17 18:06:56 +02:00
|
|
|
if (!supportBuffer)
|
|
|
|
{
|
|
|
|
WARN("write to invalid hglobal %p\n", This->supportHandle);
|
|
|
|
return S_OK;
|
|
|
|
}
|
1999-04-24 14:00:31 +02:00
|
|
|
|
2004-01-23 02:51:33 +01:00
|
|
|
memcpy((char *) supportBuffer+This->currentPosition.u.LowPart, pv, cb);
|
1999-04-24 14:00:31 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Move the current position to the new position
|
|
|
|
*/
|
2004-01-23 02:51:33 +01:00
|
|
|
This->currentPosition.u.LowPart+=cb;
|
1999-04-24 14:00:31 +02:00
|
|
|
|
|
|
|
/*
|
2006-08-14 07:06:53 +02:00
|
|
|
* Cleanup
|
1999-04-24 14:00:31 +02:00
|
|
|
*/
|
2006-08-14 07:06:53 +02:00
|
|
|
GlobalUnlock(This->supportHandle);
|
1999-04-24 14:00:31 +02:00
|
|
|
|
2006-08-14 07:06:53 +02:00
|
|
|
out:
|
1999-04-24 14:00:31 +02:00
|
|
|
/*
|
2006-08-14 07:06:53 +02:00
|
|
|
* Return the number of bytes read.
|
1999-04-24 14:00:31 +02:00
|
|
|
*/
|
2006-08-14 07:06:53 +02:00
|
|
|
*pcbWritten = cb;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-04-24 14:00:31 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* This method is part of the IStream interface.
|
|
|
|
*
|
|
|
|
* It will move the current stream pointer according to the parameters
|
|
|
|
* given.
|
|
|
|
*
|
|
|
|
* See the documentation of IStream for more info.
|
2002-06-01 01:06:46 +02:00
|
|
|
*/
|
2005-05-16 10:47:14 +02:00
|
|
|
static HRESULT WINAPI HGLOBALStreamImpl_Seek(
|
1999-04-24 14:00:31 +02:00
|
|
|
IStream* iface,
|
2002-06-01 01:06:46 +02:00
|
|
|
LARGE_INTEGER dlibMove, /* [in] */
|
|
|
|
DWORD dwOrigin, /* [in] */
|
1999-04-24 14:00:31 +02:00
|
|
|
ULARGE_INTEGER* plibNewPosition) /* [out] */
|
|
|
|
{
|
2011-12-12 22:22:03 +01:00
|
|
|
HGLOBALStreamImpl* This = impl_from_IStream(iface);
|
1999-04-24 14:00:31 +02:00
|
|
|
|
2010-02-25 10:14:37 +01:00
|
|
|
ULARGE_INTEGER newPosition = This->currentPosition;
|
|
|
|
HRESULT hr = S_OK;
|
1999-04-24 14:00:31 +02:00
|
|
|
|
2006-10-15 12:28:08 +02:00
|
|
|
TRACE("(%p, %x%08x, %d, %p)\n", iface, dlibMove.u.HighPart,
|
2004-01-23 02:51:33 +01:00
|
|
|
dlibMove.u.LowPart, dwOrigin, plibNewPosition);
|
1999-05-02 13:41:10 +02:00
|
|
|
|
1999-04-24 14:00:31 +02:00
|
|
|
/*
|
|
|
|
* The file pointer is moved depending on the given "function"
|
|
|
|
* parameter.
|
|
|
|
*/
|
|
|
|
switch (dwOrigin)
|
|
|
|
{
|
|
|
|
case STREAM_SEEK_SET:
|
2004-01-23 02:51:33 +01:00
|
|
|
newPosition.u.HighPart = 0;
|
|
|
|
newPosition.u.LowPart = 0;
|
1999-04-24 14:00:31 +02:00
|
|
|
break;
|
|
|
|
case STREAM_SEEK_CUR:
|
|
|
|
break;
|
|
|
|
case STREAM_SEEK_END:
|
2002-07-01 20:10:34 +02:00
|
|
|
newPosition = This->streamSize;
|
1999-04-24 14:00:31 +02:00
|
|
|
break;
|
|
|
|
default:
|
2010-02-25 10:14:37 +01:00
|
|
|
hr = STG_E_SEEKERROR;
|
|
|
|
goto end;
|
1999-04-24 14:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move the actual file pointer
|
|
|
|
* If the file pointer ends-up after the end of the stream, the next Write operation will
|
|
|
|
* make the file larger. This is how it is documented.
|
|
|
|
*/
|
2010-02-25 10:14:37 +01:00
|
|
|
newPosition.u.HighPart = 0;
|
|
|
|
newPosition.u.LowPart += dlibMove.QuadPart;
|
2002-07-01 20:10:34 +02:00
|
|
|
|
2010-05-26 19:15:11 +02:00
|
|
|
if (dlibMove.u.LowPart >= 0x80000000 &&
|
|
|
|
newPosition.u.LowPart >= dlibMove.u.LowPart)
|
|
|
|
{
|
|
|
|
/* We tried to seek backwards and went past the start. */
|
|
|
|
hr = STG_E_SEEKERROR;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2002-07-01 20:10:34 +02:00
|
|
|
This->currentPosition = newPosition;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2010-05-26 19:15:11 +02:00
|
|
|
end:
|
|
|
|
if (plibNewPosition) *plibNewPosition = This->currentPosition;
|
|
|
|
|
2010-02-25 10:14:37 +01:00
|
|
|
return hr;
|
1999-04-24 14:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* This method is part of the IStream interface.
|
|
|
|
*
|
|
|
|
* It will change the size of a stream.
|
|
|
|
*
|
|
|
|
* TODO: Switch from small blocks to big blocks and vice versa.
|
|
|
|
*
|
|
|
|
* See the documentation of IStream for more info.
|
|
|
|
*/
|
2005-05-16 10:47:14 +02:00
|
|
|
static HRESULT WINAPI HGLOBALStreamImpl_SetSize(
|
1999-04-24 14:00:31 +02:00
|
|
|
IStream* iface,
|
2002-06-01 01:06:46 +02:00
|
|
|
ULARGE_INTEGER libNewSize) /* [in] */
|
1999-04-24 14:00:31 +02:00
|
|
|
{
|
2011-12-12 22:22:03 +01:00
|
|
|
HGLOBALStreamImpl* This = impl_from_IStream(iface);
|
2003-11-26 04:36:18 +01:00
|
|
|
HGLOBAL supportHandle;
|
1999-04-24 14:00:31 +02:00
|
|
|
|
2006-10-15 12:28:08 +02:00
|
|
|
TRACE("(%p, %d)\n", iface, libNewSize.u.LowPart);
|
1999-05-02 13:41:10 +02:00
|
|
|
|
1999-04-24 14:00:31 +02:00
|
|
|
/*
|
2006-09-11 12:13:34 +02:00
|
|
|
* HighPart is ignored as shown in tests
|
1999-04-24 14:00:31 +02:00
|
|
|
*/
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2004-01-23 02:51:33 +01:00
|
|
|
if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
|
1999-04-24 14:00:31 +02:00
|
|
|
return S_OK;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Re allocate the HGlobal to fit the new size of the stream.
|
|
|
|
*/
|
2004-01-23 02:51:33 +01:00
|
|
|
supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
|
2003-11-26 04:36:18 +01:00
|
|
|
|
|
|
|
if (supportHandle == 0)
|
2006-12-18 00:48:25 +01:00
|
|
|
return E_OUTOFMEMORY;
|
1999-04-24 14:00:31 +02:00
|
|
|
|
2003-11-26 04:36:18 +01:00
|
|
|
This->supportHandle = supportHandle;
|
2004-01-23 02:51:33 +01:00
|
|
|
This->streamSize.u.LowPart = libNewSize.u.LowPart;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-04-24 14:00:31 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-04-24 14:00:31 +02:00
|
|
|
/***
|
|
|
|
* This method is part of the IStream interface.
|
|
|
|
*
|
|
|
|
* It will copy the 'cb' Bytes to 'pstm' IStream.
|
|
|
|
*
|
|
|
|
* See the documentation of IStream for more info.
|
|
|
|
*/
|
2005-05-16 10:47:14 +02:00
|
|
|
static HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
|
1999-04-24 14:00:31 +02:00
|
|
|
IStream* iface,
|
2002-06-01 01:06:46 +02:00
|
|
|
IStream* pstm, /* [unique][in] */
|
|
|
|
ULARGE_INTEGER cb, /* [in] */
|
|
|
|
ULARGE_INTEGER* pcbRead, /* [out] */
|
|
|
|
ULARGE_INTEGER* pcbWritten) /* [out] */
|
1999-04-24 14:00:31 +02:00
|
|
|
{
|
|
|
|
HRESULT hr = S_OK;
|
|
|
|
BYTE tmpBuffer[128];
|
|
|
|
ULONG bytesRead, bytesWritten, copySize;
|
|
|
|
ULARGE_INTEGER totalBytesRead;
|
|
|
|
ULARGE_INTEGER totalBytesWritten;
|
|
|
|
|
2006-10-15 12:28:08 +02:00
|
|
|
TRACE("(%p, %p, %d, %p, %p)\n", iface, pstm,
|
2004-01-23 02:51:33 +01:00
|
|
|
cb.u.LowPart, pcbRead, pcbWritten);
|
1999-05-02 13:41:10 +02:00
|
|
|
|
1999-04-24 14:00:31 +02:00
|
|
|
if ( pstm == 0 )
|
|
|
|
return STG_E_INVALIDPOINTER;
|
|
|
|
|
2008-03-06 13:05:52 +01:00
|
|
|
totalBytesRead.QuadPart = 0;
|
|
|
|
totalBytesWritten.QuadPart = 0;
|
1999-04-24 14:00:31 +02:00
|
|
|
|
2008-03-06 13:05:52 +01:00
|
|
|
while ( cb.QuadPart > 0 )
|
1999-04-24 14:00:31 +02:00
|
|
|
{
|
2008-03-06 13:05:52 +01:00
|
|
|
if ( cb.QuadPart >= sizeof(tmpBuffer) )
|
|
|
|
copySize = sizeof(tmpBuffer);
|
1999-04-24 14:00:31 +02:00
|
|
|
else
|
2004-01-23 02:51:33 +01:00
|
|
|
copySize = cb.u.LowPart;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2007-01-09 18:16:07 +01:00
|
|
|
hr = IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
|
|
|
|
if (FAILED(hr))
|
|
|
|
break;
|
1999-04-24 14:00:31 +02:00
|
|
|
|
2008-03-06 13:05:52 +01:00
|
|
|
totalBytesRead.QuadPart += bytesRead;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2007-01-09 18:16:07 +01:00
|
|
|
if (bytesRead)
|
1999-04-24 14:00:31 +02:00
|
|
|
{
|
2007-01-09 18:16:07 +01:00
|
|
|
hr = IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
|
|
|
|
if (FAILED(hr))
|
|
|
|
break;
|
|
|
|
|
2008-03-06 13:05:52 +01:00
|
|
|
totalBytesWritten.QuadPart += bytesWritten;
|
1999-04-24 14:00:31 +02:00
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-05-02 13:41:10 +02:00
|
|
|
if (bytesRead!=copySize)
|
2008-03-06 13:05:52 +01:00
|
|
|
cb.QuadPart = 0;
|
1999-05-02 13:41:10 +02:00
|
|
|
else
|
2008-03-06 13:05:52 +01:00
|
|
|
cb.QuadPart -= bytesRead;
|
1999-04-24 14:00:31 +02:00
|
|
|
}
|
|
|
|
|
2008-03-06 13:05:52 +01:00
|
|
|
if (pcbRead) pcbRead->QuadPart = totalBytesRead.QuadPart;
|
|
|
|
if (pcbWritten) pcbWritten->QuadPart = totalBytesWritten.QuadPart;
|
1999-04-24 14:00:31 +02:00
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* This method is part of the IStream interface.
|
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* For streams supported by HGLOBALS, this function does nothing.
|
1999-04-24 14:00:31 +02:00
|
|
|
* This is what the documentation tells us.
|
|
|
|
*
|
|
|
|
* See the documentation of IStream for more info.
|
2002-06-01 01:06:46 +02:00
|
|
|
*/
|
2005-05-16 10:47:14 +02:00
|
|
|
static HRESULT WINAPI HGLOBALStreamImpl_Commit(
|
1999-04-24 14:00:31 +02:00
|
|
|
IStream* iface,
|
2002-06-01 01:06:46 +02:00
|
|
|
DWORD grfCommitFlags) /* [in] */
|
1999-04-24 14:00:31 +02:00
|
|
|
{
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* This method is part of the IStream interface.
|
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* For streams supported by HGLOBALS, this function does nothing.
|
1999-04-24 14:00:31 +02:00
|
|
|
* This is what the documentation tells us.
|
|
|
|
*
|
|
|
|
* See the documentation of IStream for more info.
|
2002-06-01 01:06:46 +02:00
|
|
|
*/
|
2005-05-16 10:47:14 +02:00
|
|
|
static HRESULT WINAPI HGLOBALStreamImpl_Revert(
|
1999-04-24 14:00:31 +02:00
|
|
|
IStream* iface)
|
|
|
|
{
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* This method is part of the IStream interface.
|
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* For streams supported by HGLOBALS, this function does nothing.
|
1999-04-24 14:00:31 +02:00
|
|
|
* This is what the documentation tells us.
|
|
|
|
*
|
|
|
|
* See the documentation of IStream for more info.
|
2002-06-01 01:06:46 +02:00
|
|
|
*/
|
2005-05-16 10:47:14 +02:00
|
|
|
static HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
|
1999-04-24 14:00:31 +02:00
|
|
|
IStream* iface,
|
2002-06-01 01:06:46 +02:00
|
|
|
ULARGE_INTEGER libOffset, /* [in] */
|
|
|
|
ULARGE_INTEGER cb, /* [in] */
|
|
|
|
DWORD dwLockType) /* [in] */
|
1999-04-24 14:00:31 +02:00
|
|
|
{
|
2006-09-11 12:12:32 +02:00
|
|
|
return STG_E_INVALIDFUNCTION;
|
1999-04-24 14:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This method is part of the IStream interface.
|
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* For streams supported by HGLOBALS, this function does nothing.
|
1999-04-24 14:00:31 +02:00
|
|
|
* This is what the documentation tells us.
|
|
|
|
*
|
|
|
|
* See the documentation of IStream for more info.
|
2002-06-01 01:06:46 +02:00
|
|
|
*/
|
2005-05-16 10:47:14 +02:00
|
|
|
static HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
|
1999-04-24 14:00:31 +02:00
|
|
|
IStream* iface,
|
2002-06-01 01:06:46 +02:00
|
|
|
ULARGE_INTEGER libOffset, /* [in] */
|
|
|
|
ULARGE_INTEGER cb, /* [in] */
|
|
|
|
DWORD dwLockType) /* [in] */
|
1999-04-24 14:00:31 +02:00
|
|
|
{
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* This method is part of the IStream interface.
|
|
|
|
*
|
|
|
|
* This method returns information about the current
|
|
|
|
* stream.
|
|
|
|
*
|
|
|
|
* See the documentation of IStream for more info.
|
2002-06-01 01:06:46 +02:00
|
|
|
*/
|
2005-05-16 10:47:14 +02:00
|
|
|
static HRESULT WINAPI HGLOBALStreamImpl_Stat(
|
1999-04-24 14:00:31 +02:00
|
|
|
IStream* iface,
|
|
|
|
STATSTG* pstatstg, /* [out] */
|
2002-06-01 01:06:46 +02:00
|
|
|
DWORD grfStatFlag) /* [in] */
|
1999-04-24 14:00:31 +02:00
|
|
|
{
|
2011-12-12 22:22:03 +01:00
|
|
|
HGLOBALStreamImpl* This = impl_from_IStream(iface);
|
1999-04-24 14:00:31 +02:00
|
|
|
|
|
|
|
memset(pstatstg, 0, sizeof(STATSTG));
|
|
|
|
|
|
|
|
pstatstg->pwcsName = NULL;
|
|
|
|
pstatstg->type = STGTY_STREAM;
|
|
|
|
pstatstg->cbSize = This->streamSize;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2005-05-16 10:47:14 +02:00
|
|
|
static HRESULT WINAPI HGLOBALStreamImpl_Clone(
|
1999-04-24 14:00:31 +02:00
|
|
|
IStream* iface,
|
2002-06-01 01:06:46 +02:00
|
|
|
IStream** ppstm) /* [out] */
|
1999-04-24 14:00:31 +02:00
|
|
|
{
|
2011-12-12 22:22:03 +01:00
|
|
|
HGLOBALStreamImpl* This = impl_from_IStream(iface);
|
2002-11-12 03:13:49 +01:00
|
|
|
ULARGE_INTEGER dummy;
|
|
|
|
LARGE_INTEGER offset;
|
|
|
|
HRESULT hr;
|
2011-12-12 22:22:03 +01:00
|
|
|
|
2002-11-12 03:13:49 +01:00
|
|
|
TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface,This->deleteOnRelease,(long)This->currentPosition.QuadPart);
|
2011-12-12 22:22:03 +01:00
|
|
|
hr = CreateStreamOnHGlobal(This->supportHandle, FALSE, ppstm);
|
2002-11-12 03:13:49 +01:00
|
|
|
if(FAILED(hr))
|
|
|
|
return hr;
|
2011-12-12 22:43:24 +01:00
|
|
|
offset.QuadPart = (LONGLONG)This->currentPosition.QuadPart;
|
|
|
|
IStream_Seek(*ppstm, offset, STREAM_SEEK_SET, &dummy);
|
2002-11-12 03:13:49 +01:00
|
|
|
return S_OK;
|
1999-04-24 14:00:31 +02:00
|
|
|
}
|
2005-05-16 10:47:14 +02:00
|
|
|
|
2011-12-12 22:22:03 +01:00
|
|
|
static const IStreamVtbl HGLOBALStreamImplVtbl =
|
2005-05-16 10:47:14 +02:00
|
|
|
{
|
|
|
|
HGLOBALStreamImpl_QueryInterface,
|
|
|
|
HGLOBALStreamImpl_AddRef,
|
|
|
|
HGLOBALStreamImpl_Release,
|
|
|
|
HGLOBALStreamImpl_Read,
|
|
|
|
HGLOBALStreamImpl_Write,
|
|
|
|
HGLOBALStreamImpl_Seek,
|
|
|
|
HGLOBALStreamImpl_SetSize,
|
|
|
|
HGLOBALStreamImpl_CopyTo,
|
|
|
|
HGLOBALStreamImpl_Commit,
|
|
|
|
HGLOBALStreamImpl_Revert,
|
|
|
|
HGLOBALStreamImpl_LockRegion,
|
|
|
|
HGLOBALStreamImpl_UnlockRegion,
|
|
|
|
HGLOBALStreamImpl_Stat,
|
|
|
|
HGLOBALStreamImpl_Clone
|
|
|
|
};
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* CreateStreamOnHGlobal [OLE32.@]
|
|
|
|
*/
|
|
|
|
HRESULT WINAPI CreateStreamOnHGlobal(
|
|
|
|
HGLOBAL hGlobal,
|
|
|
|
BOOL fDeleteOnRelease,
|
|
|
|
LPSTREAM* ppstm)
|
|
|
|
{
|
2011-12-12 22:43:24 +01:00
|
|
|
HGLOBALStreamImpl* This;
|
2005-05-16 10:47:14 +02:00
|
|
|
|
2007-11-12 21:10:32 +01:00
|
|
|
if (!ppstm)
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2011-12-12 22:43:24 +01:00
|
|
|
This = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl));
|
|
|
|
if (!This) return E_OUTOFMEMORY;
|
2005-05-16 10:47:14 +02:00
|
|
|
|
2011-12-12 22:43:24 +01:00
|
|
|
This->IStream_iface.lpVtbl = &HGLOBALStreamImplVtbl;
|
|
|
|
This->ref = 1;
|
|
|
|
|
|
|
|
/* initialize the support */
|
|
|
|
This->supportHandle = hGlobal;
|
|
|
|
This->deleteOnRelease = fDeleteOnRelease;
|
|
|
|
|
|
|
|
/* allocate a handle if one is not supplied */
|
|
|
|
if (!This->supportHandle)
|
|
|
|
This->supportHandle = GlobalAlloc(GMEM_MOVEABLE|GMEM_NODISCARD|GMEM_SHARE, 0);
|
2005-05-16 10:47:14 +02:00
|
|
|
|
2011-12-12 22:43:24 +01:00
|
|
|
/* start at the beginning */
|
|
|
|
This->currentPosition.u.HighPart = 0;
|
|
|
|
This->currentPosition.u.LowPart = 0;
|
|
|
|
|
|
|
|
/* initialize the size of the stream to the size of the handle */
|
|
|
|
This->streamSize.u.HighPart = 0;
|
|
|
|
This->streamSize.u.LowPart = GlobalSize(This->supportHandle);
|
|
|
|
|
|
|
|
*ppstm = &This->IStream_iface;
|
|
|
|
|
|
|
|
return S_OK;
|
2005-05-16 10:47:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* GetHGlobalFromStream [OLE32.@]
|
|
|
|
*/
|
|
|
|
HRESULT WINAPI GetHGlobalFromStream(IStream* pstm, HGLOBAL* phglobal)
|
|
|
|
{
|
|
|
|
HGLOBALStreamImpl* pStream;
|
|
|
|
|
|
|
|
if (pstm == NULL)
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
|
|
|
pStream = (HGLOBALStreamImpl*) pstm;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify that the stream object was created with CreateStreamOnHGlobal.
|
|
|
|
*/
|
2011-12-12 22:22:03 +01:00
|
|
|
if (pStream->IStream_iface.lpVtbl == &HGLOBALStreamImplVtbl)
|
2005-05-16 10:47:14 +02:00
|
|
|
*phglobal = pStream->supportHandle;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*phglobal = 0;
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|