1999-01-30 14:24:23 +01:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
2010-07-17 18:56:17 +02:00
|
|
|
* File-based ILockBytes implementation
|
1999-01-30 14:24:23 +01:00
|
|
|
*
|
|
|
|
* Copyright 1999 Thuy Nguyen
|
2010-07-17 18:56:17 +02:00
|
|
|
* Copyright 2010 Vincent Povirk for CodeWeavers
|
1999-01-30 14:24:23 +01:00
|
|
|
*
|
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-01-30 14:24:23 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
1999-01-30 14:24:23 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2000-03-17 16:08:25 +01:00
|
|
|
#include <limits.h>
|
1999-01-30 14:24:23 +01:00
|
|
|
|
2004-10-07 05:06:48 +02:00
|
|
|
#define COBJMACROS
|
2003-01-07 21:36:20 +01:00
|
|
|
#define NONAMELESSUNION
|
|
|
|
#define NONAMELESSSTRUCT
|
2004-10-07 05:06:48 +02:00
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
1999-02-28 20:14:33 +01:00
|
|
|
#include "winbase.h"
|
2003-09-09 21:39:31 +02:00
|
|
|
#include "winuser.h"
|
1999-01-30 14:24:23 +01:00
|
|
|
#include "winerror.h"
|
2002-12-05 21:33:07 +01:00
|
|
|
#include "objbase.h"
|
1999-05-08 12:05:08 +02:00
|
|
|
#include "ole2.h"
|
1999-01-30 14:24:23 +01:00
|
|
|
|
|
|
|
#include "storage32.h"
|
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
#include "wine/debug.h"
|
1999-05-08 12:05:08 +02:00
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(storage);
|
1999-05-08 12:05:08 +02:00
|
|
|
|
2010-07-17 18:56:17 +02:00
|
|
|
typedef struct FileLockBytesImpl
|
2009-01-27 16:27:08 +01:00
|
|
|
{
|
2010-07-17 18:56:17 +02:00
|
|
|
const ILockBytesVtbl *lpVtbl;
|
|
|
|
LONG ref;
|
2009-01-27 16:27:08 +01:00
|
|
|
ULARGE_INTEGER filesize;
|
|
|
|
HANDLE hfile;
|
|
|
|
DWORD flProtect;
|
2010-07-17 18:56:17 +02:00
|
|
|
} FileLockBytesImpl;
|
|
|
|
|
|
|
|
static const ILockBytesVtbl FileLockBytesImpl_Vtbl;
|
2009-01-27 16:27:08 +01:00
|
|
|
|
1999-01-30 14:24:23 +01:00
|
|
|
/***********************************************************
|
|
|
|
* Prototypes for private methods
|
|
|
|
*/
|
2000-03-17 16:08:25 +01:00
|
|
|
|
|
|
|
/* Note that this evaluates a and b multiple times, so don't
|
|
|
|
* pass expressions with side effects. */
|
|
|
|
#define ROUND_UP(a, b) ((((a) + (b) - 1)/(b))*(b))
|
|
|
|
|
2010-07-17 18:56:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* GetProtectMode
|
|
|
|
*
|
|
|
|
* This function will return a protection mode flag for a file-mapping object
|
|
|
|
* from the open flags of a file.
|
|
|
|
*/
|
|
|
|
static DWORD GetProtectMode(DWORD openFlags)
|
|
|
|
{
|
|
|
|
switch(STGM_ACCESS_MODE(openFlags))
|
|
|
|
{
|
|
|
|
case STGM_WRITE:
|
|
|
|
case STGM_READWRITE:
|
|
|
|
return PAGE_READWRITE;
|
|
|
|
}
|
|
|
|
return PAGE_READONLY;
|
|
|
|
}
|
|
|
|
|
1999-05-08 12:05:08 +02:00
|
|
|
/******************************************************************************
|
2010-07-17 18:56:17 +02:00
|
|
|
* FileLockBytesImpl_Construct
|
1999-05-08 12:05:08 +02:00
|
|
|
*
|
|
|
|
* Initialize a big block object supported by a file.
|
|
|
|
*/
|
2010-07-17 18:56:17 +02:00
|
|
|
HRESULT FileLockBytesImpl_Construct(HANDLE hFile, DWORD openFlags, ILockBytes **pLockBytes)
|
1999-05-08 12:05:08 +02:00
|
|
|
{
|
2010-07-17 18:56:17 +02:00
|
|
|
FileLockBytesImpl *This;
|
1999-05-08 12:05:08 +02:00
|
|
|
|
2010-07-17 18:56:17 +02:00
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
return E_FAIL;
|
1999-05-08 12:05:08 +02:00
|
|
|
|
2010-07-17 18:56:17 +02:00
|
|
|
This = HeapAlloc(GetProcessHeap(), 0, sizeof(FileLockBytesImpl));
|
|
|
|
|
|
|
|
if (!This)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
This->lpVtbl = &FileLockBytesImpl_Vtbl;
|
|
|
|
This->ref = 1;
|
|
|
|
This->hfile = hFile;
|
2004-01-23 02:51:33 +01:00
|
|
|
This->filesize.u.LowPart = GetFileSize(This->hfile,
|
|
|
|
&This->filesize.u.HighPart);
|
2010-07-17 18:56:17 +02:00
|
|
|
This->flProtect = GetProtectMode(openFlags);
|
1999-05-08 12:05:08 +02:00
|
|
|
|
2006-10-15 12:28:08 +02:00
|
|
|
TRACE("file len %u\n", This->filesize.u.LowPart);
|
1999-01-30 14:24:23 +01:00
|
|
|
|
2010-07-17 18:56:17 +02:00
|
|
|
*pLockBytes = (ILockBytes*)This;
|
|
|
|
|
|
|
|
return S_OK;
|
1999-05-08 12:05:08 +02:00
|
|
|
}
|
|
|
|
|
2010-07-17 18:56:17 +02:00
|
|
|
/* ILockByte Interfaces */
|
|
|
|
|
|
|
|
static HRESULT WINAPI FileLockBytesImpl_QueryInterface(ILockBytes *iface, REFIID riid,
|
|
|
|
void **ppvObject)
|
1999-05-08 12:05:08 +02:00
|
|
|
{
|
2010-07-17 18:56:17 +02:00
|
|
|
if (IsEqualIID(riid, &IID_ILockBytes) || IsEqualIID(riid, &IID_ILockBytes))
|
|
|
|
*ppvObject = iface;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*ppvObject = NULL;
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
1999-05-08 12:05:08 +02:00
|
|
|
|
2010-07-17 18:56:17 +02:00
|
|
|
IUnknown_AddRef((IUnknown*)*ppvObject);
|
2000-03-17 16:08:25 +01:00
|
|
|
|
2010-07-17 18:56:17 +02:00
|
|
|
return S_OK;
|
1999-01-30 14:24:23 +01:00
|
|
|
}
|
|
|
|
|
2010-07-17 18:56:17 +02:00
|
|
|
static ULONG WINAPI FileLockBytesImpl_AddRef(ILockBytes *iface)
|
1999-02-18 16:55:53 +01:00
|
|
|
{
|
2010-07-17 18:56:17 +02:00
|
|
|
FileLockBytesImpl* This = (FileLockBytesImpl*)iface;
|
|
|
|
return InterlockedIncrement(&This->ref);
|
1999-02-18 16:55:53 +01:00
|
|
|
}
|
2007-03-16 19:34:28 +01:00
|
|
|
|
2010-07-17 18:56:17 +02:00
|
|
|
static ULONG WINAPI FileLockBytesImpl_Release(ILockBytes *iface)
|
|
|
|
{
|
|
|
|
FileLockBytesImpl* This = (FileLockBytesImpl*)iface;
|
|
|
|
ULONG ref;
|
2007-03-16 19:34:28 +01:00
|
|
|
|
2010-07-17 18:56:17 +02:00
|
|
|
ref = InterlockedDecrement(&This->ref);
|
|
|
|
|
|
|
|
if (ref == 0)
|
|
|
|
{
|
|
|
|
CloseHandle(This->hfile);
|
|
|
|
HeapFree(GetProcessHeap(), 0, This);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ref;
|
|
|
|
}
|
2007-03-16 19:34:28 +01:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* This method is part of the ILockBytes interface.
|
|
|
|
*
|
|
|
|
* It reads a block of information from the byte array at the specified
|
|
|
|
* offset.
|
|
|
|
*
|
|
|
|
* See the documentation of ILockBytes for more info.
|
|
|
|
*/
|
2010-07-17 18:56:17 +02:00
|
|
|
static HRESULT WINAPI FileLockBytesImpl_ReadAt(
|
|
|
|
ILockBytes* iface,
|
2007-03-16 19:34:28 +01:00
|
|
|
ULARGE_INTEGER ulOffset, /* [in] */
|
|
|
|
void* pv, /* [length_is][size_is][out] */
|
|
|
|
ULONG cb, /* [in] */
|
|
|
|
ULONG* pcbRead) /* [out] */
|
|
|
|
{
|
2010-07-17 18:56:17 +02:00
|
|
|
FileLockBytesImpl* This = (FileLockBytesImpl*)iface;
|
2007-03-16 19:34:28 +01:00
|
|
|
ULONG bytes_left = cb;
|
2010-04-07 16:54:51 +02:00
|
|
|
LPBYTE readPtr = pv;
|
|
|
|
BOOL ret;
|
|
|
|
LARGE_INTEGER offset;
|
|
|
|
ULONG cbRead;
|
2007-03-16 19:34:28 +01:00
|
|
|
|
|
|
|
TRACE("(%p)-> %i %p %i %p\n",This, ulOffset.u.LowPart, pv, cb, pcbRead);
|
|
|
|
|
2007-04-02 19:17:56 +02:00
|
|
|
/* verify a sane environment */
|
2007-03-16 19:34:28 +01:00
|
|
|
if (!This) return E_FAIL;
|
|
|
|
|
|
|
|
if (pcbRead)
|
|
|
|
*pcbRead = 0;
|
|
|
|
|
2010-04-07 16:54:51 +02:00
|
|
|
offset.QuadPart = ulOffset.QuadPart;
|
2007-03-16 19:34:28 +01:00
|
|
|
|
2010-04-07 16:54:51 +02:00
|
|
|
ret = SetFilePointerEx(This->hfile, offset, NULL, FILE_BEGIN);
|
2007-03-16 19:34:28 +01:00
|
|
|
|
2010-04-07 16:54:51 +02:00
|
|
|
if (!ret)
|
|
|
|
return STG_E_READFAULT;
|
2007-03-16 19:34:28 +01:00
|
|
|
|
2010-04-07 16:54:51 +02:00
|
|
|
while (bytes_left)
|
|
|
|
{
|
|
|
|
ret = ReadFile(This->hfile, readPtr, bytes_left, &cbRead, NULL);
|
2007-03-16 19:34:28 +01:00
|
|
|
|
2010-04-07 16:54:51 +02:00
|
|
|
if (!ret || cbRead == 0)
|
|
|
|
return STG_E_READFAULT;
|
2007-03-16 19:34:28 +01:00
|
|
|
|
|
|
|
if (pcbRead)
|
2010-04-07 16:54:51 +02:00
|
|
|
*pcbRead += cbRead;
|
2007-03-16 19:34:28 +01:00
|
|
|
|
2010-04-07 16:54:51 +02:00
|
|
|
bytes_left -= cbRead;
|
|
|
|
readPtr += cbRead;
|
2007-03-16 19:34:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("finished\n");
|
2010-04-07 16:54:51 +02:00
|
|
|
return S_OK;
|
2007-03-16 19:34:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* This method is part of the ILockBytes interface.
|
|
|
|
*
|
|
|
|
* It writes the specified bytes at the specified offset.
|
|
|
|
* position. If the file is too small, it will be resized.
|
|
|
|
*
|
|
|
|
* See the documentation of ILockBytes for more info.
|
|
|
|
*/
|
2010-07-17 18:56:17 +02:00
|
|
|
static HRESULT WINAPI FileLockBytesImpl_WriteAt(
|
|
|
|
ILockBytes* iface,
|
2007-03-16 19:34:28 +01:00
|
|
|
ULARGE_INTEGER ulOffset, /* [in] */
|
|
|
|
const void* pv, /* [size_is][in] */
|
|
|
|
ULONG cb, /* [in] */
|
|
|
|
ULONG* pcbWritten) /* [out] */
|
|
|
|
{
|
2010-07-17 18:56:17 +02:00
|
|
|
FileLockBytesImpl* This = (FileLockBytesImpl*)iface;
|
2007-03-16 19:34:28 +01:00
|
|
|
ULONG size_needed = ulOffset.u.LowPart + cb;
|
|
|
|
ULONG bytes_left = cb;
|
2010-04-07 16:54:51 +02:00
|
|
|
const BYTE *writePtr = pv;
|
|
|
|
BOOL ret;
|
|
|
|
LARGE_INTEGER offset;
|
|
|
|
ULONG cbWritten;
|
2007-03-16 19:34:28 +01:00
|
|
|
|
|
|
|
TRACE("(%p)-> %i %p %i %p\n",This, ulOffset.u.LowPart, pv, cb, pcbWritten);
|
|
|
|
|
2007-04-02 19:17:56 +02:00
|
|
|
/* verify a sane environment */
|
2007-03-16 19:34:28 +01:00
|
|
|
if (!This) return E_FAIL;
|
|
|
|
|
|
|
|
if (This->flProtect != PAGE_READWRITE)
|
|
|
|
return STG_E_ACCESSDENIED;
|
|
|
|
|
2010-04-07 16:54:51 +02:00
|
|
|
if (pcbWritten)
|
|
|
|
*pcbWritten = 0;
|
|
|
|
|
2007-03-16 19:34:28 +01:00
|
|
|
if (size_needed > This->filesize.u.LowPart)
|
|
|
|
{
|
|
|
|
ULARGE_INTEGER newSize;
|
|
|
|
newSize.u.HighPart = 0;
|
|
|
|
newSize.u.LowPart = size_needed;
|
2010-07-17 18:56:17 +02:00
|
|
|
ILockBytes_SetSize(iface, newSize);
|
2007-03-16 19:34:28 +01:00
|
|
|
}
|
|
|
|
|
2010-04-07 16:54:51 +02:00
|
|
|
offset.QuadPart = ulOffset.QuadPart;
|
2007-03-16 19:34:28 +01:00
|
|
|
|
2010-04-07 16:54:51 +02:00
|
|
|
ret = SetFilePointerEx(This->hfile, offset, NULL, FILE_BEGIN);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
return STG_E_READFAULT;
|
2007-03-16 19:34:28 +01:00
|
|
|
|
|
|
|
while (bytes_left)
|
|
|
|
{
|
2010-04-07 16:54:51 +02:00
|
|
|
ret = WriteFile(This->hfile, writePtr, bytes_left, &cbWritten, NULL);
|
2007-03-16 19:34:28 +01:00
|
|
|
|
2010-04-07 16:54:51 +02:00
|
|
|
if (!ret)
|
|
|
|
return STG_E_READFAULT;
|
2007-03-16 19:34:28 +01:00
|
|
|
|
|
|
|
if (pcbWritten)
|
2010-04-07 16:54:51 +02:00
|
|
|
*pcbWritten += cbWritten;
|
2007-03-16 19:34:28 +01:00
|
|
|
|
2010-04-07 16:54:51 +02:00
|
|
|
bytes_left -= cbWritten;
|
|
|
|
writePtr += cbWritten;
|
2007-03-16 19:34:28 +01:00
|
|
|
}
|
|
|
|
|
2010-04-07 16:54:51 +02:00
|
|
|
TRACE("finished\n");
|
|
|
|
return S_OK;
|
2007-03-16 19:34:28 +01:00
|
|
|
}
|
|
|
|
|
2010-07-17 18:56:17 +02:00
|
|
|
static HRESULT WINAPI FileLockBytesImpl_Flush(ILockBytes* iface)
|
2007-03-16 19:34:28 +01:00
|
|
|
{
|
2010-07-17 18:56:17 +02:00
|
|
|
return S_OK;
|
2007-03-16 19:34:28 +01:00
|
|
|
}
|
2009-01-27 18:03:55 +01:00
|
|
|
|
|
|
|
/******************************************************************************
|
2010-07-17 18:56:17 +02:00
|
|
|
* ILockBytes_SetSize
|
2009-01-27 18:03:55 +01:00
|
|
|
*
|
|
|
|
* Sets the size of the file.
|
|
|
|
*
|
|
|
|
*/
|
2010-07-17 18:56:17 +02:00
|
|
|
static HRESULT WINAPI FileLockBytesImpl_SetSize(ILockBytes* iface, ULARGE_INTEGER newSize)
|
2009-01-27 18:03:55 +01:00
|
|
|
{
|
2010-07-17 18:56:17 +02:00
|
|
|
FileLockBytesImpl* This = (FileLockBytesImpl*)iface;
|
2009-01-27 18:13:28 +01:00
|
|
|
HRESULT hr = S_OK;
|
2009-01-27 18:35:18 +01:00
|
|
|
LARGE_INTEGER newpos;
|
|
|
|
|
2009-01-27 18:03:55 +01:00
|
|
|
if (This->filesize.u.LowPart == newSize.u.LowPart)
|
2009-01-27 18:13:28 +01:00
|
|
|
return hr;
|
2009-01-27 18:03:55 +01:00
|
|
|
|
|
|
|
TRACE("from %u to %u\n", This->filesize.u.LowPart, newSize.u.LowPart);
|
|
|
|
|
2009-01-27 18:35:18 +01:00
|
|
|
newpos.QuadPart = newSize.QuadPart;
|
|
|
|
if (SetFilePointerEx(This->hfile, newpos, NULL, FILE_BEGIN))
|
2009-01-27 18:03:55 +01:00
|
|
|
{
|
2009-01-27 18:35:18 +01:00
|
|
|
SetEndOfFile(This->hfile);
|
2009-01-27 18:03:55 +01:00
|
|
|
}
|
|
|
|
|
2009-01-27 18:35:18 +01:00
|
|
|
This->filesize = newSize;
|
2009-01-27 18:13:28 +01:00
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2010-07-17 18:56:17 +02:00
|
|
|
static HRESULT WINAPI FileLockBytesImpl_LockRegion(ILockBytes* iface,
|
|
|
|
ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
|
2009-01-27 18:13:28 +01:00
|
|
|
{
|
2010-07-17 18:56:17 +02:00
|
|
|
FIXME("stub\n");
|
|
|
|
return E_NOTIMPL;
|
2009-01-27 18:03:55 +01:00
|
|
|
}
|
|
|
|
|
2010-07-17 18:56:17 +02:00
|
|
|
static HRESULT WINAPI FileLockBytesImpl_UnlockRegion(ILockBytes* iface,
|
|
|
|
ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
|
2009-01-27 18:03:55 +01:00
|
|
|
{
|
2010-07-17 18:56:17 +02:00
|
|
|
FIXME("stub\n");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
2009-01-27 18:13:28 +01:00
|
|
|
|
2010-07-17 18:56:17 +02:00
|
|
|
static HRESULT WINAPI FileLockBytesImpl_Stat(ILockBytes* iface,
|
|
|
|
STATSTG *pstatstg, DWORD grfStatFlag)
|
|
|
|
{
|
|
|
|
FileLockBytesImpl* This = (FileLockBytesImpl*)iface;
|
2009-01-27 18:13:28 +01:00
|
|
|
|
2010-07-17 18:56:17 +02:00
|
|
|
if (!(STATFLAG_NONAME & grfStatFlag))
|
|
|
|
{
|
|
|
|
FIXME("reading filename not supported\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
pstatstg->pwcsName = NULL;
|
|
|
|
pstatstg->type = STGTY_LOCKBYTES;
|
|
|
|
pstatstg->cbSize = This->filesize;
|
|
|
|
/* FIXME: If the implementation is exported, we'll need to set other fields. */
|
|
|
|
|
|
|
|
return S_OK;
|
2009-01-27 18:03:55 +01:00
|
|
|
}
|
2010-07-17 18:56:17 +02:00
|
|
|
|
|
|
|
static const ILockBytesVtbl FileLockBytesImpl_Vtbl = {
|
|
|
|
FileLockBytesImpl_QueryInterface,
|
|
|
|
FileLockBytesImpl_AddRef,
|
|
|
|
FileLockBytesImpl_Release,
|
|
|
|
FileLockBytesImpl_ReadAt,
|
|
|
|
FileLockBytesImpl_WriteAt,
|
|
|
|
FileLockBytesImpl_Flush,
|
|
|
|
FileLockBytesImpl_SetSize,
|
|
|
|
FileLockBytesImpl_LockRegion,
|
|
|
|
FileLockBytesImpl_UnlockRegion,
|
|
|
|
FileLockBytesImpl_Stat
|
|
|
|
};
|