memorystream.c 8.26 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 *	this class implements a pure IStream object
 *	and can be used for many purposes
 *
 *	the main reason for implementing this was
 *	a cleaner implementation of IShellLink which
 *	needs to be able to load lnk's from a IStream
 *	interface so it was obvious to capsule the file
 *	access in a IStream to.
10 11
 *
 * Copyright 1999 Juergen Schmied
12
 * Copyright 2003 Mike McCormack for CodeWeavers
13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27 28
 */

29
#include <stdarg.h>
30 31
#include <string.h>

32
#include "windef.h"
33
#include "winbase.h"
34
#include "winerror.h"
35 36
#include "winuser.h"
#include "wingdi.h"
37
#include "shlobj.h"
38
#include "wine/debug.h"
39 40
#include "shell32_main.h"

41
WINE_DEFAULT_DEBUG_CHANNEL(shell);
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj);
static ULONG WINAPI IStream_fnAddRef(IStream *iface);
static ULONG WINAPI IStream_fnRelease(IStream *iface);
static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead);
static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten);
static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition);
static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize);
static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten);
static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags);
static HRESULT WINAPI IStream_fnRevert (IStream * iface);
static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG*   pstatstg, DWORD grfStatFlag);
static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm);

58
static IStreamVtbl stvt =
59
{
60 61 62 63 64 65 66 67 68 69 70 71 72 73
	IStream_fnQueryInterface,
	IStream_fnAddRef,
	IStream_fnRelease,
	IStream_fnRead,
	IStream_fnWrite,
	IStream_fnSeek,
	IStream_fnSetSize,
	IStream_fnCopyTo,
	IStream_fnCommit,
	IStream_fnRevert,
	IStream_fnLockRegion,
	IStream_fnUnlockRegion,
	IStream_fnStat,
	IStream_fnClone
74

75 76
};

77
typedef struct
78
{	IStreamVtbl	*lpvtst;
79
	DWORD		ref;
80
	HANDLE		handle;
81 82 83 84 85 86 87
} ISHFileStream;

/**************************************************************************
 *   CreateStreamOnFile()
 *
 *   similar to CreateStreamOnHGlobal
 */
88
HRESULT CreateStreamOnFile (LPCWSTR pszFilename, DWORD grfMode, IStream ** ppstm)
89 90
{
	ISHFileStream*	fstr;
91 92
	HANDLE		handle;
	DWORD		access = GENERIC_READ, creat;
93

94 95
	if( grfMode & STGM_TRANSACTED )
		return E_INVALIDARG;
96

97 98 99 100
	if( grfMode & STGM_WRITE )
		access |= GENERIC_WRITE;
        if( grfMode & STGM_READWRITE )
		access = GENERIC_WRITE | GENERIC_READ;
101

102 103 104 105 106 107
	if( grfMode & STGM_CREATE )
		creat = CREATE_ALWAYS;
	else
		creat = OPEN_EXISTING;

	TRACE("Opening %s\n", debugstr_w(pszFilename) );
108

109
       handle = CreateFileW( pszFilename, access, FILE_SHARE_READ, NULL, creat, 0, NULL );
110 111
	if( handle == INVALID_HANDLE_VALUE )
		return E_FAIL;
112

113 114 115 116 117 118 119
	fstr = (ISHFileStream*)HeapAlloc(GetProcessHeap(),
		HEAP_ZERO_MEMORY,sizeof(ISHFileStream));
	if( !fstr )
		return E_FAIL;
	fstr->lpvtst=&stvt;
	fstr->ref = 1;
	fstr->handle = handle;
120 121

	(*ppstm) = (IStream*)fstr;
122 123

	return S_OK;
124 125 126 127 128 129 130
}

/**************************************************************************
*  IStream_fnQueryInterface
*/
static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
{
131
	ISHFileStream *This = (ISHFileStream *)iface;
132

133
	TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
134 135 136 137 138 139 140 141 142 143

	*ppvObj = NULL;

	if(IsEqualIID(riid, &IID_IUnknown) ||
	   IsEqualIID(riid, &IID_IStream))
	{
	  *ppvObj = This;
	}

	if(*ppvObj)
144 145
	{
	  IStream_AddRef((IStream*)*ppvObj);
146 147 148 149 150 151 152 153 154 155 156 157
	  TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
	  return S_OK;
	}
	TRACE("-- Interface: E_NOINTERFACE\n");
	return E_NOINTERFACE;
}

/**************************************************************************
*  IStream_fnAddRef
*/
static ULONG WINAPI IStream_fnAddRef(IStream *iface)
{
158
	ISHFileStream *This = (ISHFileStream *)iface;
159 160 161 162 163 164 165 166 167 168 169

	TRACE("(%p)->(count=%lu)\n",This, This->ref);

	return ++(This->ref);
}

/**************************************************************************
*  IStream_fnRelease
*/
static ULONG WINAPI IStream_fnRelease(IStream *iface)
{
170
	ISHFileStream *This = (ISHFileStream *)iface;
171 172 173

	TRACE("(%p)->()\n",This);

174
	if (!--(This->ref))
175 176 177 178
	{
		TRACE(" destroying SHFileStream (%p)\n",This);
		CloseHandle(This->handle);
		HeapFree(GetProcessHeap(),0,This);
179 180 181 182 183 184
	}
	return This->ref;
}

static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
{
185
	ISHFileStream *This = (ISHFileStream *)iface;
186 187

	TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
188

189
	if ( !pv )
190
		return STG_E_INVALIDPOINTER;
191

192 193
	if ( ! ReadFile( This->handle, pv, cb, pcbRead, NULL ) )
		return E_FAIL;
194 195 196

	return S_OK;
}
197

198 199
static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
{
200
       DWORD dummy_count;
201
	ISHFileStream *This = (ISHFileStream *)iface;
202 203 204

	TRACE("(%p)\n",This);

205 206 207
	if( !pv )
		return STG_E_INVALIDPOINTER;

208 209 210 211
       /* WriteFile() doesn't allow to specify NULL as write count pointer */
       if (!pcbWritten)
               pcbWritten = &dummy_count;

212 213 214 215
	if( ! WriteFile( This->handle, pv, cb, pcbWritten, NULL ) )
		return E_FAIL;

	return S_OK;
216
}
217

218 219
static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
{
220 221
	DWORD pos, newposlo, newposhi;

222
	ISHFileStream *This = (ISHFileStream *)iface;
223 224 225

	TRACE("(%p)\n",This);

226 227 228 229 230 231 232 233 234
	pos = dlibMove.QuadPart; /* FIXME: truncates */
	newposhi = 0;
	newposlo = SetFilePointer( This->handle, pos, &newposhi, dwOrigin );
	if( newposlo == INVALID_SET_FILE_POINTER )
		return E_FAIL;

	plibNewPosition->QuadPart = newposlo | ( (LONGLONG)newposhi<<32);

	return S_OK;
235
}
236

237 238
static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
{
239
	ISHFileStream *This = (ISHFileStream *)iface;
240 241 242

	TRACE("(%p)\n",This);

243 244 245 246 247 248 249
	if( ! SetFilePointer( This->handle, libNewSize.QuadPart, NULL, FILE_BEGIN ) )
		return E_FAIL;

	if( ! SetEndOfFile( This->handle ) )
		return E_FAIL;

	return S_OK;
250 251 252
}
static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
{
253
	ISHFileStream *This = (ISHFileStream *)iface;
254 255 256 257 258 259 260

	TRACE("(%p)\n",This);

	return E_NOTIMPL;
}
static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
{
261
	ISHFileStream *This = (ISHFileStream *)iface;
262 263 264 265 266 267 268

	TRACE("(%p)\n",This);

	return E_NOTIMPL;
}
static HRESULT WINAPI IStream_fnRevert (IStream * iface)
{
269
	ISHFileStream *This = (ISHFileStream *)iface;
270 271 272 273 274 275 276

	TRACE("(%p)\n",This);

	return E_NOTIMPL;
}
static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
{
277
	ISHFileStream *This = (ISHFileStream *)iface;
278 279 280 281 282 283 284

	TRACE("(%p)\n",This);

	return E_NOTIMPL;
}
static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
{
285
	ISHFileStream *This = (ISHFileStream *)iface;
286 287 288 289 290 291 292

	TRACE("(%p)\n",This);

	return E_NOTIMPL;
}
static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG*   pstatstg, DWORD grfStatFlag)
{
293
	ISHFileStream *This = (ISHFileStream *)iface;
294 295 296 297 298 299 300

	TRACE("(%p)\n",This);

	return E_NOTIMPL;
}
static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
{
301
	ISHFileStream *This = (ISHFileStream *)iface;
302 303 304 305 306

	TRACE("(%p)\n",This);

	return E_NOTIMPL;
}