Use wined3dformat when calling wined3d.
[wine] / dlls / urlmon / umstream.c
1 /*
2  * Based on ../shell32/memorystream.c
3  *
4  * Copyright 1999 Juergen Schmied
5  * Copyright 2003 Mike McCormack for CodeWeavers
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <stdarg.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winternl.h"
30 #include "winuser.h"
31 #include "objbase.h"
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34 #include "ole2.h"
35 #include "urlmon.h"
36 #include "wininet.h"
37 #include "shlwapi.h"
38 #include "urlmon_main.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
41
42 static const IStreamVtbl stvt;
43
44 HRESULT UMCreateStreamOnCacheFile(LPCWSTR pszURL,
45                                   DWORD dwSize,
46                                   LPWSTR pszFileName,
47                                   HANDLE *phfile,
48                                   IUMCacheStream **ppstr)
49 {
50     IUMCacheStream* ucstr;
51     HANDLE handle;
52     LPWSTR ext;
53     LPCWSTR c;
54     LPCWSTR eloc = 0;
55     HRESULT hr;
56
57     for (c = pszURL; *c && *c != '#' && *c != '?'; ++c)
58     {
59         if (*c == '.')
60            eloc = c + 1;
61         else if (*c == '/' || *c == '\\')
62            eloc = 0;
63     }
64
65     if (!eloc)
66        eloc = c;
67
68     ext = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * (c - eloc + 1));
69     memcpy(ext, eloc, sizeof(WCHAR) * (c - eloc));
70     ext[c - eloc] = 0;
71
72     if(!CreateUrlCacheEntryW(pszURL, dwSize, ext, pszFileName, 0))
73        hr = HRESULT_FROM_WIN32(GetLastError());
74     else
75        hr = 0;
76
77     HeapFree(GetProcessHeap(), 0, ext);
78
79     if (hr)
80        return hr;
81
82     TRACE("Opening %s\n", debugstr_w(pszFileName) );
83
84     handle = CreateFileW( pszFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL );
85     if( handle == INVALID_HANDLE_VALUE )
86        return HRESULT_FROM_WIN32(GetLastError());
87
88     if (phfile)
89     {
90        /* Call CreateFileW again because we need a handle with its own file pointer, and DuplicateHandle will return
91         * a handle that shares its file pointer with the original.
92         */
93            *phfile = CreateFileW( pszFileName, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
94
95        if (*phfile == (HANDLE) HFILE_ERROR)
96        {
97            DWORD dwError = GetLastError();
98
99            CloseHandle(handle);
100            return HRESULT_FROM_WIN32(dwError);
101        }
102     }
103
104     ucstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,sizeof(IUMCacheStream));
105     if(ucstr )
106     {
107        ucstr->pszURL = HeapAlloc(GetProcessHeap(),
108                                  HEAP_ZERO_MEMORY,
109                                  sizeof(WCHAR) * (lstrlenW(pszURL) + 1));
110        if (ucstr->pszURL)
111        {
112             ucstr->pszFileName = HeapAlloc(GetProcessHeap(),
113                                            HEAP_ZERO_MEMORY,
114                                            sizeof(WCHAR) * (lstrlenW(pszFileName) + 1));
115            if (ucstr->pszFileName)
116            {
117               ucstr->lpVtbl=&stvt;
118               ucstr->ref = 1;
119               ucstr->handle = handle;
120               ucstr->closed = 0;
121               lstrcpyW(ucstr->pszURL, pszURL);
122               lstrcpyW(ucstr->pszFileName, pszFileName);
123
124               *ppstr = ucstr;
125
126               return S_OK;
127            }
128            HeapFree(GetProcessHeap(), 0, ucstr->pszURL);
129        }
130        HeapFree(GetProcessHeap(), 0, ucstr);
131     }
132     CloseHandle(handle);
133     if (phfile)
134        CloseHandle(*phfile);
135     return E_OUTOFMEMORY;
136 }
137
138 void UMCloseCacheFileStream(IUMCacheStream *This)
139 {
140     if (!This->closed)
141     {
142        FILETIME ftZero;
143
144        ftZero.dwLowDateTime = ftZero.dwHighDateTime = 0;
145
146        This->closed = 1;
147        CommitUrlCacheEntryW(This->pszURL,
148                             This->pszFileName,
149                             ftZero,
150                             ftZero,
151                             NORMAL_CACHE_ENTRY,
152                             0,
153                             0,
154                             0,
155                             0);
156     }
157 }
158
159 /**************************************************************************
160 *  IStream_fnQueryInterface
161 */
162 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface,
163                                                REFIID riid,
164                                                LPVOID *ppvObj)
165 {
166     IUMCacheStream *This = (IUMCacheStream *)iface;
167
168     TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
169
170     *ppvObj = NULL;
171
172     if(IsEqualIID(riid, &IID_IUnknown) ||
173        IsEqualIID(riid, &IID_IStream))
174     {
175       *ppvObj = This;
176     }
177
178     if(*ppvObj)
179     {
180       IStream_AddRef((IStream*)*ppvObj);
181       TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
182       return S_OK;
183     }
184     TRACE("-- Interface: E_NOINTERFACE\n");
185     return E_NOINTERFACE;
186 }
187
188 /**************************************************************************
189 *  IStream_fnAddRef
190 */
191 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
192 {
193     IUMCacheStream *This = (IUMCacheStream *)iface;
194     ULONG refCount = InterlockedIncrement(&This->ref);
195
196     TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
197
198     return refCount;
199 }
200
201 /**************************************************************************
202 *  IStream_fnRelease
203 */
204 static ULONG WINAPI IStream_fnRelease(IStream *iface)
205 {
206     IUMCacheStream *This = (IUMCacheStream *)iface;
207     ULONG refCount = InterlockedDecrement(&This->ref);
208
209     TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
210
211     if (!refCount)
212     {
213        TRACE(" destroying UMCacheStream (%p)\n",This);
214        UMCloseCacheFileStream(This);
215        CloseHandle(This->handle);
216        HeapFree(GetProcessHeap(), 0, This->pszFileName);
217        HeapFree(GetProcessHeap(), 0, This->pszURL);
218        HeapFree(GetProcessHeap(),0,This);
219     }
220     return refCount;
221 }
222
223 static HRESULT WINAPI IStream_fnRead (IStream * iface, 
224                                       void* pv,
225                                       ULONG cb,
226                                       ULONG* pcbRead)
227 {
228     IUMCacheStream *This = (IUMCacheStream *)iface;
229
230     TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
231
232     if ( !pv )
233        return STG_E_INVALIDPOINTER;
234
235     if ( ! ReadFile( This->handle, pv, cb, pcbRead, NULL ) )
236        return S_FALSE;
237
238     if (!*pcbRead)
239         return This->closed ? S_FALSE : E_PENDING;
240     return S_OK;
241 }
242
243 static HRESULT WINAPI IStream_fnWrite (IStream * iface,
244                                        const void* pv,
245                                        ULONG cb,
246                                        ULONG* pcbWritten)
247 {
248     return E_NOTIMPL;
249 }
250
251 static HRESULT WINAPI IStream_fnSeek (       IStream * iface,
252                                    LARGE_INTEGER dlibMove,
253                                    DWORD dwOrigin,
254                                    ULARGE_INTEGER* plibNewPosition)
255 {
256     DWORD pos, newposlo;
257     LONG newposhi;
258
259     IUMCacheStream *This = (IUMCacheStream *)iface;
260
261     TRACE("(%p)\n",This);
262
263     pos = dlibMove.QuadPart; /* FIXME: truncates */
264     newposhi = 0;
265     newposlo = SetFilePointer( This->handle, pos, &newposhi, dwOrigin );
266     if( newposlo == INVALID_SET_FILE_POINTER && GetLastError())
267        return E_FAIL;
268
269     if (plibNewPosition)
270         plibNewPosition->QuadPart = newposlo | ( (LONGLONG)newposhi<<32);
271
272     return S_OK;
273 }
274
275 static HRESULT WINAPI IStream_fnSetSize (IStream * iface,
276                                          ULARGE_INTEGER libNewSize)
277 {
278     IUMCacheStream *This = (IUMCacheStream *)iface;
279
280     TRACE("(%p)\n",This);
281
282     if( ! SetFilePointer( This->handle, libNewSize.QuadPart, NULL, FILE_BEGIN ) )
283        return E_FAIL;
284
285     if( ! SetEndOfFile( This->handle ) )
286        return E_FAIL;
287
288     return S_OK;
289 }
290
291 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface,
292                                    IStream* pstm,
293                                    ULARGE_INTEGER cb,
294                                    ULARGE_INTEGER* pcbRead,
295                                    ULARGE_INTEGER* pcbWritten)
296 {
297     IUMCacheStream *This = (IUMCacheStream *)iface;
298
299     TRACE("(%p)\n",This);
300
301     return E_NOTIMPL;
302 }
303
304 static HRESULT WINAPI IStream_fnCommit (IStream * iface,
305                                    DWORD grfCommitFlags)
306 {
307     IUMCacheStream *This = (IUMCacheStream *)iface;
308
309     TRACE("(%p)\n",This);
310
311     return E_NOTIMPL;
312 }
313
314 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
315 {
316     IUMCacheStream *This = (IUMCacheStream *)iface;
317
318     TRACE("(%p)\n",This);
319
320     return E_NOTIMPL;
321 }
322 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface,
323                                             ULARGE_INTEGER libOffset,
324                                             ULARGE_INTEGER cb,
325                                             DWORD dwLockType)
326 {
327     IUMCacheStream *This = (IUMCacheStream *)iface;
328
329     TRACE("(%p)\n",This);
330
331     return E_NOTIMPL;
332 }
333 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface,
334                                               ULARGE_INTEGER libOffset,
335                                               ULARGE_INTEGER cb,
336                                               DWORD dwLockType)
337 {
338     IUMCacheStream *This = (IUMCacheStream *)iface;
339
340     TRACE("(%p)\n",This);
341
342     return E_NOTIMPL;
343 }
344 static HRESULT WINAPI IStream_fnStat (IStream * iface,
345                                       STATSTG*   pstatstg,
346                                       DWORD grfStatFlag)
347 {
348     IUMCacheStream *This = (IUMCacheStream *)iface;
349
350     TRACE("(%p)\n",This);
351
352     return E_NOTIMPL;
353 }
354 static HRESULT WINAPI IStream_fnClone (IStream * iface,
355                                        IStream** ppstm)
356 {
357     IUMCacheStream *This = (IUMCacheStream *)iface;
358
359     TRACE("(%p)\n",This);
360
361     return E_NOTIMPL;
362 }
363
364 static const IStreamVtbl stvt =
365 {
366     IStream_fnQueryInterface,
367     IStream_fnAddRef,
368     IStream_fnRelease,
369     IStream_fnRead,
370     IStream_fnWrite,
371     IStream_fnSeek,
372     IStream_fnSetSize,
373     IStream_fnCopyTo,
374     IStream_fnCommit,
375     IStream_fnRevert,
376     IStream_fnLockRegion,
377     IStream_fnUnlockRegion,
378     IStream_fnStat,
379     IStream_fnClone
380
381 };