SHGetFileInfo should tolerate null pointers.
[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 <string.h>
30
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "shlobj.h"
34 #include "wine/debug.h"
35 #include "shell32_main.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(shell);
38
39 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj);
40 static ULONG WINAPI IStream_fnAddRef(IStream *iface);
41 static ULONG WINAPI IStream_fnRelease(IStream *iface);
42 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead);
43 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten);
44 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition);
45 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize);
46 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten);
47 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags);
48 static HRESULT WINAPI IStream_fnRevert (IStream * iface);
49 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
50 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
51 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG*   pstatstg, DWORD grfStatFlag);
52 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm);
53
54 static ICOM_VTABLE(IStream) stvt =
55 {
56         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
57         IStream_fnQueryInterface,
58         IStream_fnAddRef,
59         IStream_fnRelease,
60         IStream_fnRead,
61         IStream_fnWrite,
62         IStream_fnSeek,
63         IStream_fnSetSize,
64         IStream_fnCopyTo,
65         IStream_fnCommit,
66         IStream_fnRevert,
67         IStream_fnLockRegion,
68         IStream_fnUnlockRegion,
69         IStream_fnStat,
70         IStream_fnClone
71
72 };
73
74 typedef struct
75 {       ICOM_VTABLE(IStream)    *lpvtst;
76         DWORD           ref;
77         HANDLE          handle;
78 } ISHFileStream;
79
80 /**************************************************************************
81  *   CreateStreamOnFile()
82  *
83  *   similar to CreateStreamOnHGlobal
84  */
85 HRESULT CreateStreamOnFile (LPCWSTR pszFilename, DWORD grfMode, IStream ** ppstm)
86 {
87         ISHFileStream*  fstr;
88         HANDLE          handle;
89         DWORD           access = GENERIC_READ, creat;
90
91         if( grfMode & STGM_TRANSACTED )
92                 return E_INVALIDARG;
93
94         if( grfMode & STGM_WRITE )
95                 access |= GENERIC_WRITE;
96         if( grfMode & STGM_READWRITE )
97                 access = GENERIC_WRITE | GENERIC_READ;
98
99         if( grfMode & STGM_CREATE )
100                 creat = CREATE_ALWAYS;
101         else
102                 creat = OPEN_EXISTING;
103
104         TRACE("Opening %s\n", debugstr_w(pszFilename) );
105
106         handle = CreateFileW( pszFilename, access, 0, NULL, creat, 0, NULL );
107         if( handle == INVALID_HANDLE_VALUE )
108                 return E_FAIL;
109
110         fstr = (ISHFileStream*)HeapAlloc(GetProcessHeap(),
111                 HEAP_ZERO_MEMORY,sizeof(ISHFileStream));
112         if( !fstr )
113                 return E_FAIL;
114         fstr->lpvtst=&stvt;
115         fstr->ref = 1;
116         fstr->handle = handle;
117
118         (*ppstm) = (IStream*)fstr;
119
120         return S_OK;
121 }
122
123 /**************************************************************************
124 *  IStream_fnQueryInterface
125 */
126 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
127 {
128         ICOM_THIS(ISHFileStream, iface);
129
130         TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
131
132         *ppvObj = NULL;
133
134         if(IsEqualIID(riid, &IID_IUnknown) ||
135            IsEqualIID(riid, &IID_IStream))
136         {
137           *ppvObj = This;
138         }
139
140         if(*ppvObj)
141         {
142           IStream_AddRef((IStream*)*ppvObj);
143           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
144           return S_OK;
145         }
146         TRACE("-- Interface: E_NOINTERFACE\n");
147         return E_NOINTERFACE;
148 }
149
150 /**************************************************************************
151 *  IStream_fnAddRef
152 */
153 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
154 {
155         ICOM_THIS(ISHFileStream, iface);
156
157         TRACE("(%p)->(count=%lu)\n",This, This->ref);
158
159         return ++(This->ref);
160 }
161
162 /**************************************************************************
163 *  IStream_fnRelease
164 */
165 static ULONG WINAPI IStream_fnRelease(IStream *iface)
166 {
167         ICOM_THIS(ISHFileStream, iface);
168
169         TRACE("(%p)->()\n",This);
170
171         if (!--(This->ref))
172         {
173                 TRACE(" destroying SHFileStream (%p)\n",This);
174                 CloseHandle(This->handle);
175                 HeapFree(GetProcessHeap(),0,This);
176         }
177         return This->ref;
178 }
179
180 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
181 {
182         ICOM_THIS(ISHFileStream, iface);
183
184         TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
185
186         if ( !pv )
187                 return STG_E_INVALIDPOINTER;
188
189         if ( ! ReadFile( This->handle, pv, cb, pcbRead, NULL ) )
190                 return E_FAIL;
191
192         return S_OK;
193 }
194
195 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
196 {
197         ICOM_THIS(ISHFileStream, iface);
198
199         TRACE("(%p)\n",This);
200
201         if( !pv )
202                 return STG_E_INVALIDPOINTER;
203
204         if( ! WriteFile( This->handle, pv, cb, pcbWritten, NULL ) )
205                 return E_FAIL;
206
207         return S_OK;
208 }
209
210 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
211 {
212         DWORD pos, newposlo, newposhi;
213
214         ICOM_THIS(ISHFileStream, iface);
215
216         TRACE("(%p)\n",This);
217
218         pos = dlibMove.QuadPart; /* FIXME: truncates */
219         newposhi = 0;
220         newposlo = SetFilePointer( This->handle, pos, &newposhi, dwOrigin );
221         if( newposlo == INVALID_SET_FILE_POINTER )
222                 return E_FAIL;
223
224         plibNewPosition->QuadPart = newposlo | ( (LONGLONG)newposhi<<32);
225
226         return S_OK;
227 }
228
229 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
230 {
231         ICOM_THIS(ISHFileStream, iface);
232
233         TRACE("(%p)\n",This);
234
235         if( ! SetFilePointer( This->handle, libNewSize.QuadPart, NULL, FILE_BEGIN ) )
236                 return E_FAIL;
237
238         if( ! SetEndOfFile( This->handle ) )
239                 return E_FAIL;
240
241         return S_OK;
242 }
243 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
244 {
245         ICOM_THIS(ISHFileStream, iface);
246
247         TRACE("(%p)\n",This);
248
249         return E_NOTIMPL;
250 }
251 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
252 {
253         ICOM_THIS(ISHFileStream, iface);
254
255         TRACE("(%p)\n",This);
256
257         return E_NOTIMPL;
258 }
259 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
260 {
261         ICOM_THIS(ISHFileStream, iface);
262
263         TRACE("(%p)\n",This);
264
265         return E_NOTIMPL;
266 }
267 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
268 {
269         ICOM_THIS(ISHFileStream, iface);
270
271         TRACE("(%p)\n",This);
272
273         return E_NOTIMPL;
274 }
275 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
276 {
277         ICOM_THIS(ISHFileStream, iface);
278
279         TRACE("(%p)\n",This);
280
281         return E_NOTIMPL;
282 }
283 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG*   pstatstg, DWORD grfStatFlag)
284 {
285         ICOM_THIS(ISHFileStream, iface);
286
287         TRACE("(%p)\n",This);
288
289         return E_NOTIMPL;
290 }
291 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
292 {
293         ICOM_THIS(ISHFileStream, iface);
294
295         TRACE("(%p)\n",This);
296
297         return E_NOTIMPL;
298 }