Removed some more uses of the non-standard ICOM_THIS macro.
[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  * Copyright 1999 Juergen Schmied
12  * Copyright 2003 Mike McCormack for CodeWeavers
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  */
28
29 #include <stdarg.h>
30 #include <string.h>
31
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winerror.h"
35 #include "winuser.h"
36 #include "wingdi.h"
37 #include "shlobj.h"
38 #include "wine/debug.h"
39 #include "shell32_main.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(shell);
42
43 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj);
44 static ULONG WINAPI IStream_fnAddRef(IStream *iface);
45 static ULONG WINAPI IStream_fnRelease(IStream *iface);
46 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead);
47 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten);
48 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition);
49 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize);
50 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten);
51 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags);
52 static HRESULT WINAPI IStream_fnRevert (IStream * iface);
53 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
54 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
55 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG*   pstatstg, DWORD grfStatFlag);
56 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm);
57
58 static IStreamVtbl stvt =
59 {
60         IStream_fnQueryInterface,
61         IStream_fnAddRef,
62         IStream_fnRelease,
63         IStream_fnRead,
64         IStream_fnWrite,
65         IStream_fnSeek,
66         IStream_fnSetSize,
67         IStream_fnCopyTo,
68         IStream_fnCommit,
69         IStream_fnRevert,
70         IStream_fnLockRegion,
71         IStream_fnUnlockRegion,
72         IStream_fnStat,
73         IStream_fnClone
74
75 };
76
77 typedef struct
78 {       IStreamVtbl     *lpvtst;
79         DWORD           ref;
80         HANDLE          handle;
81 } ISHFileStream;
82
83 /**************************************************************************
84  *   CreateStreamOnFile()
85  *
86  *   similar to CreateStreamOnHGlobal
87  */
88 HRESULT CreateStreamOnFile (LPCWSTR pszFilename, DWORD grfMode, IStream ** ppstm)
89 {
90         ISHFileStream*  fstr;
91         HANDLE          handle;
92         DWORD           access = GENERIC_READ, creat;
93
94         if( grfMode & STGM_TRANSACTED )
95                 return E_INVALIDARG;
96
97         if( grfMode & STGM_WRITE )
98                 access |= GENERIC_WRITE;
99         if( grfMode & STGM_READWRITE )
100                 access = GENERIC_WRITE | GENERIC_READ;
101
102         if( grfMode & STGM_CREATE )
103                 creat = CREATE_ALWAYS;
104         else
105                 creat = OPEN_EXISTING;
106
107         TRACE("Opening %s\n", debugstr_w(pszFilename) );
108
109        handle = CreateFileW( pszFilename, access, FILE_SHARE_READ, NULL, creat, 0, NULL );
110         if( handle == INVALID_HANDLE_VALUE )
111                 return E_FAIL;
112
113         fstr = (ISHFileStream*)HeapAlloc(GetProcessHeap(),
114                 HEAP_ZERO_MEMORY,sizeof(ISHFileStream));
115         if( !fstr )
116                 return E_FAIL;
117         fstr->lpvtst=&stvt;
118         fstr->ref = 1;
119         fstr->handle = handle;
120
121         (*ppstm) = (IStream*)fstr;
122
123         return S_OK;
124 }
125
126 /**************************************************************************
127 *  IStream_fnQueryInterface
128 */
129 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
130 {
131         ISHFileStream *This = (ISHFileStream *)iface;
132
133         TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
134
135         *ppvObj = NULL;
136
137         if(IsEqualIID(riid, &IID_IUnknown) ||
138            IsEqualIID(riid, &IID_IStream))
139         {
140           *ppvObj = This;
141         }
142
143         if(*ppvObj)
144         {
145           IStream_AddRef((IStream*)*ppvObj);
146           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
147           return S_OK;
148         }
149         TRACE("-- Interface: E_NOINTERFACE\n");
150         return E_NOINTERFACE;
151 }
152
153 /**************************************************************************
154 *  IStream_fnAddRef
155 */
156 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
157 {
158         ISHFileStream *This = (ISHFileStream *)iface;
159
160         TRACE("(%p)->(count=%lu)\n",This, This->ref);
161
162         return ++(This->ref);
163 }
164
165 /**************************************************************************
166 *  IStream_fnRelease
167 */
168 static ULONG WINAPI IStream_fnRelease(IStream *iface)
169 {
170         ISHFileStream *This = (ISHFileStream *)iface;
171
172         TRACE("(%p)->()\n",This);
173
174         if (!--(This->ref))
175         {
176                 TRACE(" destroying SHFileStream (%p)\n",This);
177                 CloseHandle(This->handle);
178                 HeapFree(GetProcessHeap(),0,This);
179         }
180         return This->ref;
181 }
182
183 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
184 {
185         ISHFileStream *This = (ISHFileStream *)iface;
186
187         TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
188
189         if ( !pv )
190                 return STG_E_INVALIDPOINTER;
191
192         if ( ! ReadFile( This->handle, pv, cb, pcbRead, NULL ) )
193                 return E_FAIL;
194
195         return S_OK;
196 }
197
198 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
199 {
200        DWORD dummy_count;
201         ISHFileStream *This = (ISHFileStream *)iface;
202
203         TRACE("(%p)\n",This);
204
205         if( !pv )
206                 return STG_E_INVALIDPOINTER;
207
208        /* WriteFile() doesn't allow to specify NULL as write count pointer */
209        if (!pcbWritten)
210                pcbWritten = &dummy_count;
211
212         if( ! WriteFile( This->handle, pv, cb, pcbWritten, NULL ) )
213                 return E_FAIL;
214
215         return S_OK;
216 }
217
218 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
219 {
220         DWORD pos, newposlo, newposhi;
221
222         ISHFileStream *This = (ISHFileStream *)iface;
223
224         TRACE("(%p)\n",This);
225
226         pos = dlibMove.QuadPart; /* FIXME: truncates */
227         newposhi = 0;
228         newposlo = SetFilePointer( This->handle, pos, &newposhi, dwOrigin );
229         if( newposlo == INVALID_SET_FILE_POINTER )
230                 return E_FAIL;
231
232         plibNewPosition->QuadPart = newposlo | ( (LONGLONG)newposhi<<32);
233
234         return S_OK;
235 }
236
237 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
238 {
239         ISHFileStream *This = (ISHFileStream *)iface;
240
241         TRACE("(%p)\n",This);
242
243         if( ! SetFilePointer( This->handle, libNewSize.QuadPart, NULL, FILE_BEGIN ) )
244                 return E_FAIL;
245
246         if( ! SetEndOfFile( This->handle ) )
247                 return E_FAIL;
248
249         return S_OK;
250 }
251 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
252 {
253         ISHFileStream *This = (ISHFileStream *)iface;
254
255         TRACE("(%p)\n",This);
256
257         return E_NOTIMPL;
258 }
259 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
260 {
261         ISHFileStream *This = (ISHFileStream *)iface;
262
263         TRACE("(%p)\n",This);
264
265         return E_NOTIMPL;
266 }
267 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
268 {
269         ISHFileStream *This = (ISHFileStream *)iface;
270
271         TRACE("(%p)\n",This);
272
273         return E_NOTIMPL;
274 }
275 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
276 {
277         ISHFileStream *This = (ISHFileStream *)iface;
278
279         TRACE("(%p)\n",This);
280
281         return E_NOTIMPL;
282 }
283 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
284 {
285         ISHFileStream *This = (ISHFileStream *)iface;
286
287         TRACE("(%p)\n",This);
288
289         return E_NOTIMPL;
290 }
291 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG*   pstatstg, DWORD grfStatFlag)
292 {
293         ISHFileStream *This = (ISHFileStream *)iface;
294
295         TRACE("(%p)\n",This);
296
297         return E_NOTIMPL;
298 }
299 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
300 {
301         ISHFileStream *This = (ISHFileStream *)iface;
302
303         TRACE("(%p)\n",This);
304
305         return E_NOTIMPL;
306 }