source: trunk/src/shell32/memorystream.cpp@ 1214

Last change on this file since 1214 was 1214, checked in by sandervl, 26 years ago

Moved new shell32 to current dir

File size: 7.1 KB
Line 
1/*
2 * this class implements a pure IStream object
3 * and can be used for many purposes
4 *
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.
10 */
11
12#include <string.h>
13#include <odin.h>
14
15#define ICOM_CINTERFACE 1
16#define CINTERFACE 1
17
18#include "wine/obj_storage.h"
19#include "heap.h"
20#include "winerror.h"
21#include "debugtools.h"
22#include "shell32_main.h"
23
24#include <misc.h>
25
26DEFAULT_DEBUG_CHANNEL(shell)
27
28static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj);
29static ULONG WINAPI IStream_fnAddRef(IStream *iface);
30static ULONG WINAPI IStream_fnRelease(IStream *iface);
31static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead);
32static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten);
33static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition);
34static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize);
35static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten);
36static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags);
37static HRESULT WINAPI IStream_fnRevert (IStream * iface);
38static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
39static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
40static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag);
41static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm);
42
43static ICOM_VTABLE(IStream) stvt =
44{
45 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
46 IStream_fnQueryInterface,
47 IStream_fnAddRef,
48 IStream_fnRelease,
49 IStream_fnRead,
50 IStream_fnWrite,
51 IStream_fnSeek,
52 IStream_fnSetSize,
53 IStream_fnCopyTo,
54 IStream_fnCommit,
55 IStream_fnRevert,
56 IStream_fnLockRegion,
57 IStream_fnUnlockRegion,
58 IStream_fnStat,
59 IStream_fnClone
60
61};
62
63typedef struct
64{ ICOM_VTABLE(IStream) *lpvtst;
65 DWORD ref;
66 LPBYTE pImage;
67 HANDLE hMapping;
68 DWORD dwLength;
69 DWORD dwPos;
70} ISHFileStream;
71
72/**************************************************************************
73 * CreateStreamOnFile()
74 *
75 * similar to CreateStreamOnHGlobal
76 */
77HRESULT CreateStreamOnFile (LPCSTR pszFilename, IStream ** ppstm)
78{
79 ISHFileStream* fstr;
80 OFSTRUCT ofs;
81 HFILE hFile = OpenFile( pszFilename, &ofs, OF_READ );
82 HRESULT ret = E_FAIL;
83
84 fstr = (ISHFileStream*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ISHFileStream));
85 fstr->lpvtst=&stvt;
86 fstr->ref = 1;
87 fstr->dwLength = GetFileSize (hFile, NULL);
88
89 shell32_ObjCount++;
90
91 if (!(fstr->hMapping = CreateFileMappingA(hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL)))
92 {
93 WARN("failed to create filemap.\n");
94 goto end_2;
95 }
96
97 if (!(fstr->pImage = (BYTE*)MapViewOfFile(fstr->hMapping,FILE_MAP_READ,0,0,0)))
98 {
99 WARN("failed to mmap filemap.\n");
100 goto end_3;
101 }
102
103 ret = S_OK;
104 goto end_1;
105
106end_3: CloseHandle(fstr->hMapping);
107end_2: HeapFree(GetProcessHeap(), 0, fstr);
108 fstr = NULL;
109
110end_1: _lclose(hFile);
111 (*ppstm) = (IStream*)fstr;
112 return ret;
113}
114
115/**************************************************************************
116* IStream_fnQueryInterface
117*/
118static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
119{
120 ICOM_THIS(ISHFileStream, iface);
121
122 char xriid[50];
123 WINE_StringFromCLSID((LPCLSID)riid,xriid);
124
125 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,xriid,ppvObj);
126
127 *ppvObj = NULL;
128
129 if(IsEqualIID(riid, &IID_IUnknown) ||
130 IsEqualIID(riid, &IID_IStream))
131 {
132 *ppvObj = This;
133 }
134
135 if(*ppvObj)
136 {
137 IStream_AddRef((IStream*)*ppvObj);
138 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
139 return S_OK;
140 }
141 TRACE("-- Interface: E_NOINTERFACE\n");
142 return E_NOINTERFACE;
143}
144
145/**************************************************************************
146* IStream_fnAddRef
147*/
148static ULONG WINAPI IStream_fnAddRef(IStream *iface)
149{
150 ICOM_THIS(ISHFileStream, iface);
151
152 TRACE("(%p)->(count=%lu)\n",This, This->ref);
153
154 shell32_ObjCount++;
155 return ++(This->ref);
156}
157
158/**************************************************************************
159* IStream_fnRelease
160*/
161static ULONG WINAPI IStream_fnRelease(IStream *iface)
162{
163 ICOM_THIS(ISHFileStream, iface);
164
165 TRACE("(%p)->()\n",This);
166
167 shell32_ObjCount--;
168
169 if (!--(This->ref))
170 { TRACE(" destroying SHFileStream (%p)\n",This);
171
172 UnmapViewOfFile(This->pImage);
173 CloseHandle(This->hMapping);
174
175 HeapFree(GetProcessHeap(),0,This);
176 return 0;
177 }
178 return This->ref;
179}
180
181static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
182{
183 ICOM_THIS(ISHFileStream, iface);
184
185 DWORD dwBytesToRead, dwBytesLeft;
186
187 TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
188
189 if ( !pv )
190 return STG_E_INVALIDPOINTER;
191
192 dwBytesLeft = This->dwLength - This->dwPos;
193
194 if ( 0 >= dwBytesLeft ) /* end of buffer */
195 return S_FALSE;
196
197 dwBytesToRead = ( cb > dwBytesLeft) ? dwBytesLeft : cb;
198
199 memmove ( pv, (This->pImage) + (This->dwPos), dwBytesToRead);
200
201 This->dwPos += dwBytesToRead; /* adjust pointer */
202
203 if (pcbRead)
204 *pcbRead = dwBytesToRead;
205
206 return S_OK;
207}
208static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
209{
210 ICOM_THIS(ISHFileStream, iface);
211
212 TRACE("(%p)\n",This);
213
214 return E_NOTIMPL;
215}
216static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
217{
218 ICOM_THIS(ISHFileStream, iface);
219
220 TRACE("(%p)\n",This);
221
222 return E_NOTIMPL;
223}
224static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
225{
226 ICOM_THIS(ISHFileStream, iface);
227
228 TRACE("(%p)\n",This);
229
230 return E_NOTIMPL;
231}
232static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
233{
234 ICOM_THIS(ISHFileStream, iface);
235
236 TRACE("(%p)\n",This);
237
238 return E_NOTIMPL;
239}
240static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
241{
242 ICOM_THIS(ISHFileStream, iface);
243
244 TRACE("(%p)\n",This);
245
246 return E_NOTIMPL;
247}
248static HRESULT WINAPI IStream_fnRevert (IStream * iface)
249{
250 ICOM_THIS(ISHFileStream, iface);
251
252 TRACE("(%p)\n",This);
253
254 return E_NOTIMPL;
255}
256static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
257{
258 ICOM_THIS(ISHFileStream, iface);
259
260 TRACE("(%p)\n",This);
261
262 return E_NOTIMPL;
263}
264static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
265{
266 ICOM_THIS(ISHFileStream, iface);
267
268 TRACE("(%p)\n",This);
269
270 return E_NOTIMPL;
271}
272static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag)
273{
274 ICOM_THIS(ISHFileStream, iface);
275
276 TRACE("(%p)\n",This);
277
278 return E_NOTIMPL;
279}
280static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
281{
282 ICOM_THIS(ISHFileStream, iface);
283
284 TRACE("(%p)\n",This);
285
286 return E_NOTIMPL;
287}
Note: See TracBrowser for help on using the repository browser.