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