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.
14 #include "wine/obj_storage.h"
17 #include "debugtools.h"
18 #include "shell32_main.h"
20 DEFAULT_DEBUG_CHANNEL(shell)
22 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj);
23 static ULONG WINAPI IStream_fnAddRef(IStream *iface);
24 static ULONG WINAPI IStream_fnRelease(IStream *iface);
25 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead);
26 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten);
27 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition);
28 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize);
29 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten);
30 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags);
31 static HRESULT WINAPI IStream_fnRevert (IStream * iface);
32 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
33 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
34 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag);
35 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm);
37 static ICOM_VTABLE(IStream) stvt =
39 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
40 IStream_fnQueryInterface,
51 IStream_fnUnlockRegion,
58 { ICOM_VTABLE(IStream) *lpvtst;
66 /**************************************************************************
67 * CreateStreamOnFile()
69 * similar to CreateStreamOnHGlobal
71 HRESULT CreateStreamOnFile (LPCSTR pszFilename, IStream ** ppstm)
75 HFILE hFile = OpenFile( pszFilename, &ofs, OF_READ );
78 fstr = (ISHFileStream*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ISHFileStream));
81 fstr->dwLength = GetFileSize (hFile, NULL);
85 if (!(fstr->hMapping = CreateFileMappingA(hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL)))
87 WARN("failed to create filemap.\n");
91 if (!(fstr->pImage = MapViewOfFile(fstr->hMapping,FILE_MAP_READ,0,0,0)))
93 WARN("failed to mmap filemap.\n");
100 end_3: CloseHandle(fstr->hMapping);
101 end_2: HeapFree(GetProcessHeap(), 0, fstr);
104 end_1: _lclose(hFile);
105 (*ppstm) = (IStream*)fstr;
109 /**************************************************************************
110 * IStream_fnQueryInterface
112 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
114 ICOM_THIS(ISHFileStream, iface);
116 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
120 if(IsEqualIID(riid, &IID_IUnknown) ||
121 IsEqualIID(riid, &IID_IStream))
128 IStream_AddRef((IStream*)*ppvObj);
129 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
132 TRACE("-- Interface: E_NOINTERFACE\n");
133 return E_NOINTERFACE;
136 /**************************************************************************
139 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
141 ICOM_THIS(ISHFileStream, iface);
143 TRACE("(%p)->(count=%lu)\n",This, This->ref);
146 return ++(This->ref);
149 /**************************************************************************
152 static ULONG WINAPI IStream_fnRelease(IStream *iface)
154 ICOM_THIS(ISHFileStream, iface);
156 TRACE("(%p)->()\n",This);
161 { TRACE(" destroying SHFileStream (%p)\n",This);
163 UnmapViewOfFile(This->pImage);
164 CloseHandle(This->hMapping);
166 HeapFree(GetProcessHeap(),0,This);
172 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
174 ICOM_THIS(ISHFileStream, iface);
176 DWORD dwBytesToRead, dwBytesLeft;
178 TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
181 return STG_E_INVALIDPOINTER;
183 dwBytesLeft = This->dwLength - This->dwPos;
185 if ( 0 >= dwBytesLeft ) /* end of buffer */
188 dwBytesToRead = ( cb > dwBytesLeft) ? dwBytesLeft : cb;
190 memmove ( pv, (This->pImage) + (This->dwPos), dwBytesToRead);
192 This->dwPos += dwBytesToRead; /* adjust pointer */
195 *pcbRead = dwBytesToRead;
199 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
201 ICOM_THIS(ISHFileStream, iface);
203 TRACE("(%p)\n",This);
207 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
209 ICOM_THIS(ISHFileStream, iface);
211 TRACE("(%p)\n",This);
215 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
217 ICOM_THIS(ISHFileStream, iface);
219 TRACE("(%p)\n",This);
223 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
225 ICOM_THIS(ISHFileStream, iface);
227 TRACE("(%p)\n",This);
231 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
233 ICOM_THIS(ISHFileStream, iface);
235 TRACE("(%p)\n",This);
239 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
241 ICOM_THIS(ISHFileStream, iface);
243 TRACE("(%p)\n",This);
247 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
249 ICOM_THIS(ISHFileStream, iface);
251 TRACE("(%p)\n",This);
255 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
257 ICOM_THIS(ISHFileStream, iface);
259 TRACE("(%p)\n",This);
263 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag)
265 ICOM_THIS(ISHFileStream, iface);
267 TRACE("(%p)\n",This);
271 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
273 ICOM_THIS(ISHFileStream, iface);
275 TRACE("(%p)\n",This);