2 * this class implements a pure IStream object
3 * and can be used for many purposes
5 * the main reason for implementing this was
6 * a cleaner implementation of IShellLink which
7 * needs to be able to load lnk's from a IStream
8 * interface so it was obvious to capsule the file
9 * access in a IStream to.
11 * Copyright 1999 Juergen Schmied
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include "wine/debug.h"
34 #include "shell32_main.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(shell);
38 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj);
39 static ULONG WINAPI IStream_fnAddRef(IStream *iface);
40 static ULONG WINAPI IStream_fnRelease(IStream *iface);
41 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead);
42 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten);
43 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition);
44 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize);
45 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten);
46 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags);
47 static HRESULT WINAPI IStream_fnRevert (IStream * iface);
48 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
49 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
50 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag);
51 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm);
53 static ICOM_VTABLE(IStream) stvt =
55 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
56 IStream_fnQueryInterface,
67 IStream_fnUnlockRegion,
74 { ICOM_VTABLE(IStream) *lpvtst;
82 /**************************************************************************
83 * CreateStreamOnFile()
85 * similar to CreateStreamOnHGlobal
87 HRESULT CreateStreamOnFile (LPCSTR pszFilename, IStream ** ppstm)
91 HFILE hFile = OpenFile( pszFilename, &ofs, OF_READ );
94 fstr = (ISHFileStream*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ISHFileStream));
97 fstr->dwLength = GetFileSize (hFile, NULL);
101 if (!(fstr->hMapping = CreateFileMappingA(hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL)))
103 WARN("failed to create filemap.\n");
107 if (!(fstr->pImage = MapViewOfFile(fstr->hMapping,FILE_MAP_READ,0,0,0)))
109 WARN("failed to mmap filemap.\n");
116 end_3: CloseHandle(fstr->hMapping);
117 end_2: HeapFree(GetProcessHeap(), 0, fstr);
120 end_1: _lclose(hFile);
121 (*ppstm) = (IStream*)fstr;
125 /**************************************************************************
126 * IStream_fnQueryInterface
128 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
130 ICOM_THIS(ISHFileStream, iface);
132 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
136 if(IsEqualIID(riid, &IID_IUnknown) ||
137 IsEqualIID(riid, &IID_IStream))
144 IStream_AddRef((IStream*)*ppvObj);
145 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
148 TRACE("-- Interface: E_NOINTERFACE\n");
149 return E_NOINTERFACE;
152 /**************************************************************************
155 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
157 ICOM_THIS(ISHFileStream, iface);
159 TRACE("(%p)->(count=%lu)\n",This, This->ref);
162 return ++(This->ref);
165 /**************************************************************************
168 static ULONG WINAPI IStream_fnRelease(IStream *iface)
170 ICOM_THIS(ISHFileStream, iface);
172 TRACE("(%p)->()\n",This);
177 { TRACE(" destroying SHFileStream (%p)\n",This);
179 UnmapViewOfFile(This->pImage);
180 CloseHandle(This->hMapping);
182 HeapFree(GetProcessHeap(),0,This);
188 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
190 ICOM_THIS(ISHFileStream, iface);
192 DWORD dwBytesToRead, dwBytesLeft;
194 TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
197 return STG_E_INVALIDPOINTER;
199 dwBytesLeft = This->dwLength - This->dwPos;
201 if ( 0 >= dwBytesLeft ) /* end of buffer */
204 dwBytesToRead = ( cb > dwBytesLeft) ? dwBytesLeft : cb;
206 memmove ( pv, (This->pImage) + (This->dwPos), dwBytesToRead);
208 This->dwPos += dwBytesToRead; /* adjust pointer */
211 *pcbRead = dwBytesToRead;
215 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
217 ICOM_THIS(ISHFileStream, iface);
219 TRACE("(%p)\n",This);
223 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
225 ICOM_THIS(ISHFileStream, iface);
227 TRACE("(%p)\n",This);
231 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
233 ICOM_THIS(ISHFileStream, iface);
235 TRACE("(%p)\n",This);
239 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
241 ICOM_THIS(ISHFileStream, iface);
243 TRACE("(%p)\n",This);
247 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
249 ICOM_THIS(ISHFileStream, iface);
251 TRACE("(%p)\n",This);
255 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
257 ICOM_THIS(ISHFileStream, iface);
259 TRACE("(%p)\n",This);
263 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
265 ICOM_THIS(ISHFileStream, iface);
267 TRACE("(%p)\n",This);
271 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
273 ICOM_THIS(ISHFileStream, iface);
275 TRACE("(%p)\n",This);
279 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag)
281 ICOM_THIS(ISHFileStream, iface);
283 TRACE("(%p)\n",This);
287 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
289 ICOM_THIS(ISHFileStream, iface);
291 TRACE("(%p)\n",This);