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