📄 stg_stream.c
字号:
/*
* Compound Storage (32 bit version)
* Stream implementation
*
* This file contains the implementation of the stream interface
* for streams contained in a compound storage.
*
* Copyright 1999 Francis Beaudet
* Copyright 1999 Thuy Nguyen
*
* 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
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <assert.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#define COBJMACROS
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
#include "windef.h"
#include "winbase.h"
#include "winerror.h"
#include "winternl.h"
#include "wine/debug.h"
#include "storage32.h"
WINE_DEFAULT_DEBUG_CHANNEL(storage);
/***
* This is the destructor of the StgStreamImpl class.
*
* This method will clean-up all the resources used-up by the given StgStreamImpl
* class. The pointer passed-in to this function will be freed and will not
* be valid anymore.
*/
static void StgStreamImpl_Destroy(StgStreamImpl* This)
{
TRACE("(%p)\n", This);
/*
* Release the reference we are holding on the parent storage.
* IStorage_Release((IStorage*)This->parentStorage);
*
* No, don't do this. Some apps call IStorage_Release without
* calling IStream_Release first. If we grab a reference the
* file is not closed, and the app fails when it tries to
* reopen the file (Easy-PC, for example). Just inform the
* storage that we have closed the stream
*/
if(This->parentStorage) {
StorageBaseImpl_RemoveStream(This->parentStorage, This);
}
This->parentStorage = 0;
/*
* Make sure we clean-up the block chain stream objects that we were using.
*/
if (This->bigBlockChain != 0)
{
BlockChainStream_Destroy(This->bigBlockChain);
This->bigBlockChain = 0;
}
if (This->smallBlockChain != 0)
{
SmallBlockChainStream_Destroy(This->smallBlockChain);
This->smallBlockChain = 0;
}
/*
* Finally, free the memory used-up by the class.
*/
HeapFree(GetProcessHeap(), 0, This);
}
/***
* This implements the IUnknown method QueryInterface for this
* class
*/
static HRESULT WINAPI StgStreamImpl_QueryInterface(
IStream* iface,
REFIID riid, /* [in] */
void** ppvObject) /* [iid_is][out] */
{
StgStreamImpl* const This=(StgStreamImpl*)iface;
/*
* Perform a sanity check on the parameters.
*/
if (ppvObject==0)
return E_INVALIDARG;
/*
* Initialize the return parameter.
*/
*ppvObject = 0;
/*
* Compare the riid with the interface IDs implemented by this object.
*/
if (IsEqualIID(&IID_IUnknown, riid) ||
IsEqualIID(&IID_IPersist, riid) ||
IsEqualIID(&IID_IPersistStream, riid) ||
IsEqualIID(&IID_ISequentialStream, riid) ||
IsEqualIID(&IID_IStream, riid))
{
*ppvObject = (IStream*)This;
}
/*
* Check that we obtained an interface.
*/
if ((*ppvObject)==0)
return E_NOINTERFACE;
/*
* Query Interface always increases the reference count by one when it is
* successful
*/
IStream_AddRef(iface);
return S_OK;
}
/***
* This implements the IUnknown method AddRef for this
* class
*/
static ULONG WINAPI StgStreamImpl_AddRef(
IStream* iface)
{
StgStreamImpl* const This=(StgStreamImpl*)iface;
return InterlockedIncrement(&This->ref);
}
/***
* This implements the IUnknown method Release for this
* class
*/
static ULONG WINAPI StgStreamImpl_Release(
IStream* iface)
{
StgStreamImpl* const This=(StgStreamImpl*)iface;
ULONG ref;
ref = InterlockedDecrement(&This->ref);
/*
* If the reference count goes down to 0, perform suicide.
*/
if (ref==0)
{
StgStreamImpl_Destroy(This);
}
return ref;
}
/***
* This method will open the block chain pointed by the property
* that describes the stream.
* If the stream's size is null, no chain is opened.
*/
static void StgStreamImpl_OpenBlockChain(
StgStreamImpl* This)
{
StgProperty curProperty;
BOOL readSuccessful;
/*
* Make sure no old object is left over.
*/
if (This->smallBlockChain != 0)
{
SmallBlockChainStream_Destroy(This->smallBlockChain);
This->smallBlockChain = 0;
}
if (This->bigBlockChain != 0)
{
BlockChainStream_Destroy(This->bigBlockChain);
This->bigBlockChain = 0;
}
/*
* Read the information from the property.
*/
readSuccessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
This->ownerProperty,
&curProperty);
if (readSuccessful)
{
This->streamSize = curProperty.size;
/*
* This code supports only streams that are <32 bits in size.
*/
assert(This->streamSize.u.HighPart == 0);
if(curProperty.startingBlock == BLOCK_END_OF_CHAIN)
{
assert( (This->streamSize.u.HighPart == 0) && (This->streamSize.u.LowPart == 0) );
}
else
{
if ( (This->streamSize.u.HighPart == 0) &&
(This->streamSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
{
This->smallBlockChain = SmallBlockChainStream_Construct(
This->parentStorage->ancestorStorage,
This->ownerProperty);
}
else
{
This->bigBlockChain = BlockChainStream_Construct(
This->parentStorage->ancestorStorage,
NULL,
This->ownerProperty);
}
}
}
}
/***
* This method is part of the ISequentialStream interface.
*
* It 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.
*/
static HRESULT WINAPI StgStreamImpl_Read(
IStream* iface,
void* pv, /* [length_is][size_is][out] */
ULONG cb, /* [in] */
ULONG* pcbRead) /* [out] */
{
StgStreamImpl* const This=(StgStreamImpl*)iface;
ULONG bytesReadBuffer;
ULONG bytesToReadFromBuffer;
HRESULT res;
TRACE("(%p, %p, %d, %p)\n",
iface, pv, cb, pcbRead);
if (!This->parentStorage)
{
WARN("storage reverted\n");
return STG_E_REVERTED;
}
/*
* If the caller is not interested in the number of bytes read,
* we use another buffer to avoid "if" statements in the code.
*/
if (pcbRead==0)
pcbRead = &bytesReadBuffer;
/*
* Using the known size of the stream, calculate the number of bytes
* to read from the block chain
*/
bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
/*
* Depending on the type of chain that was opened when the stream was constructed,
* we delegate the work to the method that reads the block chains.
*/
if (This->smallBlockChain!=0)
{
res = SmallBlockChainStream_ReadAt(This->smallBlockChain,
This->currentPosition,
bytesToReadFromBuffer,
pv,
pcbRead);
}
else if (This->bigBlockChain!=0)
{
res = BlockChainStream_ReadAt(This->bigBlockChain,
This->currentPosition,
bytesToReadFromBuffer,
pv,
pcbRead);
}
else
{
/*
* Small and big block chains are both NULL. This case will happen
* when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
*/
*pcbRead = 0;
res = S_OK;
goto end;
}
if (SUCCEEDED(res))
{
/*
* We should always be able to read the proper amount of data from the
* chain.
*/
assert(bytesToReadFromBuffer == *pcbRead);
/*
* Advance the pointer for the number of positions read.
*/
This->currentPosition.u.LowPart += *pcbRead;
}
end:
TRACE("<-- %08x\n", res);
return res;
}
/***
* 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.
*/
static HRESULT WINAPI StgStreamImpl_Write(
IStream* iface,
const void* pv, /* [size_is][in] */
ULONG cb, /* [in] */
ULONG* pcbWritten) /* [out] */
{
StgStreamImpl* const This=(StgStreamImpl*)iface;
ULARGE_INTEGER newSize;
ULONG bytesWritten = 0;
HRESULT res;
TRACE("(%p, %p, %d, %p)\n",
iface, pv, cb, pcbWritten);
/*
* Do we have permission to write to this stream?
*/
switch(STGM_ACCESS_MODE(This->grfMode))
{
case STGM_WRITE:
case STGM_READWRITE:
break;
default:
WARN("access denied by flags: 0x%x\n", STGM_ACCESS_MODE(This->grfMode));
return STG_E_ACCESSDENIED;
}
if (!pv)
return STG_E_INVALIDPOINTER;
if (!This->parentStorage)
{
WARN("storage reverted\n");
return STG_E_REVERTED;
}
/*
* 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;
/*
* Initialize the out parameter
*/
*pcbWritten = 0;
if (cb == 0)
{
TRACE("<-- S_OK, written 0\n");
return S_OK;
}
else
{
newSize.u.HighPart = 0;
newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
}
/*
* Verify if we need to grow the stream
*/
if (newSize.u.LowPart > This->streamSize.u.LowPart)
{
/* grow stream */
res = IStream_SetSize(iface, newSize);
if (FAILED(res))
return res;
}
/*
* Depending on the type of chain that was opened when the stream was constructed,
* we delegate the work to the method that readwrites to the block chains.
*/
if (This->smallBlockChain!=0)
{
res = SmallBlockChainStream_WriteAt(This->smallBlockChain,
This->currentPosition,
cb,
pv,
pcbWritten);
}
else if (This->bigBlockChain!=0)
{
res = BlockChainStream_WriteAt(This->bigBlockChain,
This->currentPosition,
cb,
pv,
pcbWritten);
}
else
{
/* this should never happen because the IStream_SetSize call above will
* make sure a big or small block chain is created */
assert(FALSE);
res = 0;
}
/*
* Advance the position pointer for the number of positions written.
*/
This->currentPosition.u.LowPart += *pcbWritten;
TRACE("<-- S_OK, written %u\n", *pcbWritten);
return res;
}
/***
* 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.
*/
static HRESULT WINAPI StgStreamImpl_Seek(
IStream* iface,
LARGE_INTEGER dlibMove, /* [in] */
DWORD dwOrigin, /* [in] */
ULARGE_INTEGER* plibNewPosition) /* [out] */
{
StgStreamImpl* const This=(StgStreamImpl*)iface;
ULARGE_INTEGER newPosition;
TRACE("(%p, %d, %d, %p)\n",
iface, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
/*
* fail if the stream has no parent (as does windows)
*/
if (!This->parentStorage)
{
WARN("storage reverted\n");
return STG_E_REVERTED;
}
/*
* The caller is allowed to pass in NULL as the new position return value.
* If it happens, we assign it to a dynamic variable to avoid special cases
* in the code below.
*/
if (plibNewPosition == 0)
{
plibNewPosition = &newPosition;
}
/*
* The file pointer is moved depending on the given "function"
* parameter.
*/
switch (dwOrigin)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -