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