wintrust/tests: Fix test on win9x.
[wine] / dlls / urlmon / file.c
1 /*
2  * Copyright 2005 Jacek Caban
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "urlmon_main.h"
20 #include "wine/debug.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
23
24 typedef struct {
25     const IInternetProtocolVtbl  *lpInternetProtocolVtbl;
26     const IInternetPriorityVtbl  *lpInternetPriorityVtbl;
27
28     HANDLE file;
29     LONG priority;
30
31     LONG ref;
32 } FileProtocol;
33
34 #define PROTOCOL(x)  ((IInternetProtocol*)  &(x)->lpInternetProtocolVtbl)
35 #define PRIORITY(x)  ((IInternetPriority*)  &(x)->lpInternetPriorityVtbl)
36
37 #define PROTOCOL_THIS(iface) DEFINE_THIS(FileProtocol, InternetProtocol, iface)
38
39 static HRESULT WINAPI FileProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
40 {
41     FileProtocol *This = PROTOCOL_THIS(iface);
42
43     *ppv = NULL;
44     if(IsEqualGUID(&IID_IUnknown, riid)) {
45         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
46         *ppv = PROTOCOL(This);
47     }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
48         TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
49         *ppv = PROTOCOL(This);
50     }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
51         TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
52         *ppv = PROTOCOL(This);
53     }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
54         TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
55         *ppv = PRIORITY(This);
56     }
57
58     if(*ppv) {
59         IInternetProtocol_AddRef(iface);
60         return S_OK;
61     }
62
63     WARN("not supported interface %s\n", debugstr_guid(riid));
64     return E_NOINTERFACE;
65 }
66
67 static ULONG WINAPI FileProtocol_AddRef(IInternetProtocol *iface)
68 {
69     FileProtocol *This = PROTOCOL_THIS(iface);
70     LONG ref = InterlockedIncrement(&This->ref);
71     TRACE("(%p) ref=%d\n", This, ref);
72     return ref;
73 }
74
75 static ULONG WINAPI FileProtocol_Release(IInternetProtocol *iface)
76 {
77     FileProtocol *This = PROTOCOL_THIS(iface);
78     LONG ref = InterlockedDecrement(&This->ref);
79
80     TRACE("(%p) ref=%d\n", This, ref);
81
82     if(!ref) {
83         if(This->file)
84             CloseHandle(This->file);
85         heap_free(This);
86
87         URLMON_UnlockModule();
88     }
89
90     return ref;
91 }
92
93 static HRESULT WINAPI FileProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
94         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
95         DWORD grfPI, DWORD dwReserved)
96 {
97     FileProtocol *This = PROTOCOL_THIS(iface);
98     BINDINFO bindinfo;
99     DWORD grfBINDF = 0;
100     LARGE_INTEGER size;
101     DWORD len;
102     LPWSTR url, mime = NULL, file_name;
103     WCHAR null_char = 0;
104     BOOL first_call = FALSE;
105     HRESULT hres;
106
107     static const WCHAR wszFile[]  = {'f','i','l','e',':'};
108
109     TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
110             pOIBindInfo, grfPI, dwReserved);
111
112     memset(&bindinfo, 0, sizeof(bindinfo));
113     bindinfo.cbSize = sizeof(BINDINFO);
114     hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
115     if(FAILED(hres)) {
116         WARN("GetBindInfo failed: %08x\n", hres);
117         return hres;
118     }
119
120     ReleaseBindInfo(&bindinfo);
121
122     if(!szUrl || !*szUrl)
123         return E_INVALIDARG;
124
125     if(lstrlenW(szUrl) < sizeof(wszFile)/sizeof(WCHAR)
126             || memcmp(szUrl, wszFile, sizeof(wszFile)))
127         return MK_E_SYNTAX;
128
129     len = lstrlenW(szUrl)+16;
130     url = heap_alloc(len*sizeof(WCHAR));
131     hres = CoInternetParseUrl(szUrl, PARSE_ENCODE, 0, url, len, &len, 0);
132     if(FAILED(hres)) {
133         heap_free(url);
134         return hres;
135     }
136
137     if(!(grfBINDF & BINDF_FROMURLMON))
138         IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_DIRECTBIND, NULL);
139
140     if(!This->file) {
141         WCHAR *ptr;
142
143         first_call = TRUE;
144
145         IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, &null_char);
146
147         file_name = url+sizeof(wszFile)/sizeof(WCHAR);
148         if(file_name[0] == '/' && file_name[1] == '/')
149             file_name += 2;
150         if(*file_name == '/')
151             file_name++;
152
153         for(ptr = file_name; *ptr; ptr++) {
154             if(*ptr == '?' || *ptr == '#') {
155                 *ptr = 0;
156                 break;
157             }
158         }
159
160         if(file_name[1] == '|')
161             file_name[1] = ':';
162
163         This->file = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
164                                  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
165
166         if(This->file == INVALID_HANDLE_VALUE) {
167             This->file = NULL;
168             IInternetProtocolSink_ReportResult(pOIProtSink, INET_E_RESOURCE_NOT_FOUND,
169                     GetLastError(), NULL);
170             heap_free(url);
171             return INET_E_RESOURCE_NOT_FOUND;
172         }
173
174         IInternetProtocolSink_ReportProgress(pOIProtSink,
175                 BINDSTATUS_CACHEFILENAMEAVAILABLE, file_name);
176
177         hres = FindMimeFromData(NULL, url, NULL, 0, NULL, 0, &mime, 0);
178         if(SUCCEEDED(hres)) {
179             IInternetProtocolSink_ReportProgress(pOIProtSink,
180                     (grfBINDF & BINDF_FROMURLMON) ?
181                     BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE : BINDSTATUS_MIMETYPEAVAILABLE,
182                     mime);
183             CoTaskMemFree(mime);
184         }
185     }
186
187     heap_free(url);
188
189     if(GetFileSizeEx(This->file, &size))
190         IInternetProtocolSink_ReportData(pOIProtSink,
191                 BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION,
192                 size.u.LowPart, size.u.LowPart);
193
194     if(first_call)
195         IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
196
197     return S_OK;
198 }
199
200 static HRESULT WINAPI FileProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
201 {
202     FileProtocol *This = PROTOCOL_THIS(iface);
203     FIXME("(%p)->(%p)\n", This, pProtocolData);
204     return E_NOTIMPL;
205 }
206
207 static HRESULT WINAPI FileProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
208         DWORD dwOptions)
209 {
210     FileProtocol *This = PROTOCOL_THIS(iface);
211     FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
212     return E_NOTIMPL;
213 }
214
215 static HRESULT WINAPI FileProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
216 {
217     FileProtocol *This = PROTOCOL_THIS(iface);
218
219     TRACE("(%p)->(%08x)\n", This, dwOptions);
220
221     return S_OK;
222 }
223
224 static HRESULT WINAPI FileProtocol_Suspend(IInternetProtocol *iface)
225 {
226     FileProtocol *This = PROTOCOL_THIS(iface);
227     FIXME("(%p)\n", This);
228     return E_NOTIMPL;
229 }
230
231 static HRESULT WINAPI FileProtocol_Resume(IInternetProtocol *iface)
232 {
233     FileProtocol *This = PROTOCOL_THIS(iface);
234     FIXME("(%p)\n", This);
235     return E_NOTIMPL;
236 }
237
238 static HRESULT WINAPI FileProtocol_Read(IInternetProtocol *iface, void *pv,
239         ULONG cb, ULONG *pcbRead)
240 {
241     FileProtocol *This = PROTOCOL_THIS(iface);
242     DWORD read = 0;
243
244     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
245
246     if (pcbRead)
247         *pcbRead = 0;
248
249     if(!This->file)
250         return INET_E_DATA_NOT_AVAILABLE;
251
252     if (!ReadFile(This->file, pv, cb, &read, NULL))
253         return INET_E_DOWNLOAD_FAILURE;
254
255     if(pcbRead)
256         *pcbRead = read;
257     
258     return cb == read ? S_OK : S_FALSE;
259 }
260
261 static HRESULT WINAPI FileProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
262         DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
263 {
264     FileProtocol *This = PROTOCOL_THIS(iface);
265     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
266     return E_NOTIMPL;
267 }
268
269 static HRESULT WINAPI FileProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
270 {
271     FileProtocol *This = PROTOCOL_THIS(iface);
272
273     TRACE("(%p)->(%08x)\n", This, dwOptions);
274
275     return S_OK;
276 }
277
278 static HRESULT WINAPI FileProtocol_UnlockRequest(IInternetProtocol *iface)
279 {
280     FileProtocol *This = PROTOCOL_THIS(iface);
281
282     TRACE("(%p)\n", This);
283
284     return S_OK;
285 }
286
287 #undef PROTOCOL_THIS
288
289 static const IInternetProtocolVtbl FileProtocolVtbl = {
290     FileProtocol_QueryInterface,
291     FileProtocol_AddRef,
292     FileProtocol_Release,
293     FileProtocol_Start,
294     FileProtocol_Continue,
295     FileProtocol_Abort,
296     FileProtocol_Terminate,
297     FileProtocol_Suspend,
298     FileProtocol_Resume,
299     FileProtocol_Read,
300     FileProtocol_Seek,
301     FileProtocol_LockRequest,
302     FileProtocol_UnlockRequest
303 };
304
305 #define PRIORITY_THIS(iface) DEFINE_THIS(FileProtocol, InternetPriority, iface)
306
307 static HRESULT WINAPI FilePriority_QueryInterface(IInternetPriority *iface,
308                                                   REFIID riid, void **ppv)
309 {
310     FileProtocol *This = PRIORITY_THIS(iface);
311     return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
312 }
313
314 static ULONG WINAPI FilePriority_AddRef(IInternetPriority *iface)
315 {
316     FileProtocol *This = PRIORITY_THIS(iface);
317     return IInternetProtocol_AddRef(PROTOCOL(This));
318 }
319
320 static ULONG WINAPI FilePriority_Release(IInternetPriority *iface)
321 {
322     FileProtocol *This = PRIORITY_THIS(iface);
323     return IInternetProtocol_Release(PROTOCOL(This));
324 }
325
326 static HRESULT WINAPI FilePriority_SetPriority(IInternetPriority *iface, LONG nPriority)
327 {
328     FileProtocol *This = PRIORITY_THIS(iface);
329
330     TRACE("(%p)->(%d)\n", This, nPriority);
331
332     This->priority = nPriority;
333     return S_OK;
334 }
335
336 static HRESULT WINAPI FilePriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
337 {
338     FileProtocol *This = PRIORITY_THIS(iface);
339
340     TRACE("(%p)->(%p)\n", This, pnPriority);
341
342     *pnPriority = This->priority;
343     return S_OK;
344 }
345
346 #undef PRIORITY_THIS
347
348 static const IInternetPriorityVtbl FilePriorityVtbl = {
349     FilePriority_QueryInterface,
350     FilePriority_AddRef,
351     FilePriority_Release,
352     FilePriority_SetPriority,
353     FilePriority_GetPriority
354 };
355
356 HRESULT FileProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
357 {
358     FileProtocol *ret;
359
360     TRACE("(%p %p)\n", pUnkOuter, ppobj);
361
362     URLMON_LockModule();
363
364     ret = heap_alloc(sizeof(FileProtocol));
365
366     ret->lpInternetProtocolVtbl = &FileProtocolVtbl;
367     ret->lpInternetPriorityVtbl = &FilePriorityVtbl;
368     ret->file = NULL;
369     ret->priority = 0;
370     ret->ref = 1;
371
372     *ppobj = PROTOCOL(ret);
373     
374     return S_OK;
375 }