Correct 2 bugs connected to asynchronous shutdown():
[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  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2.1 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with this library; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27
28 #include <string.h>
29
30 #include "winbase.h"
31 #include "winerror.h"
32 #include "shlobj.h"
33 #include "wine/debug.h"
34 #include "shell32_main.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(shell);
37
38 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj);
39 static ULONG WINAPI IStream_fnAddRef(IStream *iface);
40 static ULONG WINAPI IStream_fnRelease(IStream *iface);
41 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead);
42 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten);
43 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition);
44 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize);
45 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten);
46 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags);
47 static HRESULT WINAPI IStream_fnRevert (IStream * iface);
48 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
49 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
50 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG*   pstatstg, DWORD grfStatFlag);
51 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm);
52
53 static ICOM_VTABLE(IStream) stvt =
54 {
55         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
56         IStream_fnQueryInterface,
57         IStream_fnAddRef,
58         IStream_fnRelease,
59         IStream_fnRead,
60         IStream_fnWrite,
61         IStream_fnSeek,
62         IStream_fnSetSize,
63         IStream_fnCopyTo,
64         IStream_fnCommit,
65         IStream_fnRevert,
66         IStream_fnLockRegion,
67         IStream_fnUnlockRegion,
68         IStream_fnStat,
69         IStream_fnClone
70
71 };
72
73 typedef struct
74 {       ICOM_VTABLE(IStream)    *lpvtst;
75         DWORD           ref;
76         LPBYTE          pImage;
77         HANDLE          hMapping;
78         DWORD           dwLength;
79         DWORD           dwPos;
80 } ISHFileStream;
81
82 /**************************************************************************
83  *   CreateStreamOnFile()
84  *
85  *   similar to CreateStreamOnHGlobal
86  */
87 HRESULT CreateStreamOnFile (LPCSTR pszFilename, IStream ** ppstm)
88 {
89         ISHFileStream*  fstr;
90         OFSTRUCT        ofs;
91         HFILE           hFile = OpenFile( pszFilename, &ofs, OF_READ );
92         HRESULT         ret = E_FAIL;
93
94         fstr = (ISHFileStream*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ISHFileStream));
95         fstr->lpvtst=&stvt;
96         fstr->ref = 1;
97         fstr->dwLength = GetFileSize ((HANDLE)hFile, NULL);
98
99         if (!(fstr->hMapping = CreateFileMappingA((HANDLE)hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL)))
100         {
101           WARN("failed to create filemap.\n");
102           goto end_2;
103         }
104
105         if (!(fstr->pImage = MapViewOfFile(fstr->hMapping,FILE_MAP_READ,0,0,0)))
106         {
107           WARN("failed to mmap filemap.\n");
108           goto end_3;
109         }
110
111         ret = S_OK;
112         goto end_1;
113
114 end_3:  CloseHandle(fstr->hMapping);
115 end_2:  HeapFree(GetProcessHeap(), 0, fstr);
116         fstr = NULL;
117
118 end_1:  _lclose(hFile);
119         (*ppstm) = (IStream*)fstr;
120         return ret;
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         { TRACE(" destroying SHFileStream (%p)\n",This);
173
174           UnmapViewOfFile(This->pImage);
175           CloseHandle(This->hMapping);
176
177           HeapFree(GetProcessHeap(),0,This);
178           return 0;
179         }
180         return This->ref;
181 }
182
183 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
184 {
185         ICOM_THIS(ISHFileStream, iface);
186
187         DWORD dwBytesToRead, dwBytesLeft;
188
189         TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
190
191         if ( !pv )
192           return STG_E_INVALIDPOINTER;
193
194         dwBytesLeft = This->dwLength - This->dwPos;
195
196         if ( 0 >= dwBytesLeft )                                         /* end of buffer */
197           return S_FALSE;
198
199         dwBytesToRead = ( cb > dwBytesLeft) ? dwBytesLeft : cb;
200
201         memmove ( pv, (This->pImage) + (This->dwPos), dwBytesToRead);
202
203         This->dwPos += dwBytesToRead;                                   /* adjust pointer */
204
205         if (pcbRead)
206           *pcbRead = dwBytesToRead;
207
208         return S_OK;
209 }
210 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
211 {
212         ICOM_THIS(ISHFileStream, iface);
213
214         TRACE("(%p)\n",This);
215
216         return E_NOTIMPL;
217 }
218 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
219 {
220         ICOM_THIS(ISHFileStream, iface);
221
222         TRACE("(%p)\n",This);
223
224         return E_NOTIMPL;
225 }
226 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
227 {
228         ICOM_THIS(ISHFileStream, iface);
229
230         TRACE("(%p)\n",This);
231
232         return E_NOTIMPL;
233 }
234 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
235 {
236         ICOM_THIS(ISHFileStream, iface);
237
238         TRACE("(%p)\n",This);
239
240         return E_NOTIMPL;
241 }
242 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
243 {
244         ICOM_THIS(ISHFileStream, iface);
245
246         TRACE("(%p)\n",This);
247
248         return E_NOTIMPL;
249 }
250 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
251 {
252         ICOM_THIS(ISHFileStream, iface);
253
254         TRACE("(%p)\n",This);
255
256         return E_NOTIMPL;
257 }
258 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
259 {
260         ICOM_THIS(ISHFileStream, iface);
261
262         TRACE("(%p)\n",This);
263
264         return E_NOTIMPL;
265 }
266 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
267 {
268         ICOM_THIS(ISHFileStream, iface);
269
270         TRACE("(%p)\n",This);
271
272         return E_NOTIMPL;
273 }
274 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG*   pstatstg, DWORD grfStatFlag)
275 {
276         ICOM_THIS(ISHFileStream, iface);
277
278         TRACE("(%p)\n",This);
279
280         return E_NOTIMPL;
281 }
282 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
283 {
284         ICOM_THIS(ISHFileStream, iface);
285
286         TRACE("(%p)\n",This);
287
288         return E_NOTIMPL;
289 }