Release 20030408.
[wine/gsoc-2012-control.git] / dlls / shell32 / memorystream.c
blob9b225af6566f4981cee432c2f6ba0eebb3ee8f76
1 /*
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
28 #include <string.h>
30 #include "winbase.h"
31 #include "winerror.h"
32 #include "shlobj.h"
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,
57 IStream_fnAddRef,
58 IStream_fnRelease,
59 IStream_fnRead,
60 IStream_fnWrite,
61 IStream_fnSeek,
62 IStream_fnSetSize,
63 IStream_fnCopyTo,
64 IStream_fnCommit,
65 IStream_fnRevert,
66 IStream_fnLockRegion,
67 IStream_fnUnlockRegion,
68 IStream_fnStat,
69 IStream_fnClone
73 typedef struct
74 { ICOM_VTABLE(IStream) *lpvtst;
75 DWORD ref;
76 LPBYTE pImage;
77 HANDLE hMapping;
78 DWORD dwLength;
79 DWORD dwPos;
80 } ISHFileStream;
82 /**************************************************************************
83 * CreateStreamOnFile()
85 * similar to CreateStreamOnHGlobal
87 HRESULT CreateStreamOnFile (LPCSTR pszFilename, IStream ** ppstm)
89 ISHFileStream* fstr;
90 OFSTRUCT ofs;
91 HFILE hFile = OpenFile( pszFilename, &ofs, OF_READ );
92 HRESULT ret = E_FAIL;
94 fstr = (ISHFileStream*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ISHFileStream));
95 fstr->lpvtst=&stvt;
96 fstr->ref = 1;
97 fstr->dwLength = GetFileSize ((HANDLE)hFile, NULL);
99 if (!(fstr->hMapping = CreateFileMappingA((HANDLE)hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL)))
101 WARN("failed to create filemap.\n");
102 goto end_2;
105 if (!(fstr->pImage = MapViewOfFile(fstr->hMapping,FILE_MAP_READ,0,0,0)))
107 WARN("failed to mmap filemap.\n");
108 goto end_3;
111 ret = S_OK;
112 goto end_1;
114 end_3: CloseHandle(fstr->hMapping);
115 end_2: HeapFree(GetProcessHeap(), 0, fstr);
116 fstr = NULL;
118 end_1: _lclose(hFile);
119 (*ppstm) = (IStream*)fstr;
120 return ret;
123 /**************************************************************************
124 * IStream_fnQueryInterface
126 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
128 ICOM_THIS(ISHFileStream, iface);
130 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
132 *ppvObj = NULL;
134 if(IsEqualIID(riid, &IID_IUnknown) ||
135 IsEqualIID(riid, &IID_IStream))
137 *ppvObj = This;
140 if(*ppvObj)
142 IStream_AddRef((IStream*)*ppvObj);
143 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
144 return S_OK;
146 TRACE("-- Interface: E_NOINTERFACE\n");
147 return E_NOINTERFACE;
150 /**************************************************************************
151 * IStream_fnAddRef
153 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
155 ICOM_THIS(ISHFileStream, iface);
157 TRACE("(%p)->(count=%lu)\n",This, This->ref);
159 return ++(This->ref);
162 /**************************************************************************
163 * IStream_fnRelease
165 static ULONG WINAPI IStream_fnRelease(IStream *iface)
167 ICOM_THIS(ISHFileStream, iface);
169 TRACE("(%p)->()\n",This);
171 if (!--(This->ref))
172 { TRACE(" destroying SHFileStream (%p)\n",This);
174 UnmapViewOfFile(This->pImage);
175 CloseHandle(This->hMapping);
177 HeapFree(GetProcessHeap(),0,This);
178 return 0;
180 return This->ref;
183 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
185 ICOM_THIS(ISHFileStream, iface);
187 DWORD dwBytesToRead, dwBytesLeft;
189 TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
191 if ( !pv )
192 return STG_E_INVALIDPOINTER;
194 dwBytesLeft = This->dwLength - This->dwPos;
196 if ( 0 >= dwBytesLeft ) /* end of buffer */
197 return S_FALSE;
199 dwBytesToRead = ( cb > dwBytesLeft) ? dwBytesLeft : cb;
201 memmove ( pv, (This->pImage) + (This->dwPos), dwBytesToRead);
203 This->dwPos += dwBytesToRead; /* adjust pointer */
205 if (pcbRead)
206 *pcbRead = dwBytesToRead;
208 return S_OK;
210 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
212 ICOM_THIS(ISHFileStream, iface);
214 TRACE("(%p)\n",This);
216 return E_NOTIMPL;
218 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
220 ICOM_THIS(ISHFileStream, iface);
222 TRACE("(%p)\n",This);
224 return E_NOTIMPL;
226 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
228 ICOM_THIS(ISHFileStream, iface);
230 TRACE("(%p)\n",This);
232 return E_NOTIMPL;
234 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
236 ICOM_THIS(ISHFileStream, iface);
238 TRACE("(%p)\n",This);
240 return E_NOTIMPL;
242 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
244 ICOM_THIS(ISHFileStream, iface);
246 TRACE("(%p)\n",This);
248 return E_NOTIMPL;
250 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
252 ICOM_THIS(ISHFileStream, iface);
254 TRACE("(%p)\n",This);
256 return E_NOTIMPL;
258 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
260 ICOM_THIS(ISHFileStream, iface);
262 TRACE("(%p)\n",This);
264 return E_NOTIMPL;
266 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
268 ICOM_THIS(ISHFileStream, iface);
270 TRACE("(%p)\n",This);
272 return E_NOTIMPL;
274 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag)
276 ICOM_THIS(ISHFileStream, iface);
278 TRACE("(%p)\n",This);
280 return E_NOTIMPL;
282 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
284 ICOM_THIS(ISHFileStream, iface);
286 TRACE("(%p)\n",This);
288 return E_NOTIMPL;