quartz: Make dwSamplesProcessed a longlong.
[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(lstrlenW(szUrl) < sizeof(wszFile)/sizeof(WCHAR)
123             || memcmp(szUrl, wszFile, sizeof(wszFile)))
124         return MK_E_SYNTAX;
125
126     len = lstrlenW(szUrl)+16;
127     url = heap_alloc(len*sizeof(WCHAR));
128     hres = CoInternetParseUrl(szUrl, PARSE_ENCODE, 0, url, len, &len, 0);
129     if(FAILED(hres)) {
130         heap_free(url);
131         return hres;
132     }
133
134     if(!(grfBINDF & BINDF_FROMURLMON))
135         IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_DIRECTBIND, NULL);
136
137     if(!This->file) {
138         WCHAR *ptr;
139
140         first_call = TRUE;
141
142         IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, &null_char);
143
144         file_name = url+sizeof(wszFile)/sizeof(WCHAR);
145         if(file_name[0] == '/' && file_name[1] == '/')
146             file_name += 2;
147         if(*file_name == '/')
148             file_name++;
149
150         for(ptr = file_name; *ptr; ptr++) {
151             if(*ptr == '?' || *ptr == '#') {
152                 *ptr = 0;
153                 break;
154             }
155         }
156
157         This->file = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
158                                  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
159
160         if(This->file == INVALID_HANDLE_VALUE) {
161             This->file = NULL;
162             IInternetProtocolSink_ReportResult(pOIProtSink, INET_E_RESOURCE_NOT_FOUND,
163                     GetLastError(), NULL);
164             heap_free(url);
165             return INET_E_RESOURCE_NOT_FOUND;
166         }
167
168         IInternetProtocolSink_ReportProgress(pOIProtSink,
169                 BINDSTATUS_CACHEFILENAMEAVAILABLE, file_name);
170
171         hres = FindMimeFromData(NULL, url, NULL, 0, NULL, 0, &mime, 0);
172         if(SUCCEEDED(hres)) {
173             IInternetProtocolSink_ReportProgress(pOIProtSink,
174                     (grfBINDF & BINDF_FROMURLMON) ?
175                     BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE : BINDSTATUS_MIMETYPEAVAILABLE,
176                     mime);
177             CoTaskMemFree(mime);
178         }
179     }
180
181     heap_free(url);
182
183     if(GetFileSizeEx(This->file, &size))
184         IInternetProtocolSink_ReportData(pOIProtSink,
185                 BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION,
186                 size.u.LowPart, size.u.LowPart);
187
188     if(first_call)
189         IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
190
191     return S_OK;
192 }
193
194 static HRESULT WINAPI FileProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
195 {
196     FileProtocol *This = PROTOCOL_THIS(iface);
197     FIXME("(%p)->(%p)\n", This, pProtocolData);
198     return E_NOTIMPL;
199 }
200
201 static HRESULT WINAPI FileProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
202         DWORD dwOptions)
203 {
204     FileProtocol *This = PROTOCOL_THIS(iface);
205     FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
206     return E_NOTIMPL;
207 }
208
209 static HRESULT WINAPI FileProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
210 {
211     FileProtocol *This = PROTOCOL_THIS(iface);
212
213     TRACE("(%p)->(%08x)\n", This, dwOptions);
214
215     return S_OK;
216 }
217
218 static HRESULT WINAPI FileProtocol_Suspend(IInternetProtocol *iface)
219 {
220     FileProtocol *This = PROTOCOL_THIS(iface);
221     FIXME("(%p)\n", This);
222     return E_NOTIMPL;
223 }
224
225 static HRESULT WINAPI FileProtocol_Resume(IInternetProtocol *iface)
226 {
227     FileProtocol *This = PROTOCOL_THIS(iface);
228     FIXME("(%p)\n", This);
229     return E_NOTIMPL;
230 }
231
232 static HRESULT WINAPI FileProtocol_Read(IInternetProtocol *iface, void *pv,
233         ULONG cb, ULONG *pcbRead)
234 {
235     FileProtocol *This = PROTOCOL_THIS(iface);
236     DWORD read = 0;
237
238     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
239
240     if (pcbRead)
241         *pcbRead = 0;
242
243     if(!This->file)
244         return INET_E_DATA_NOT_AVAILABLE;
245
246     if (!ReadFile(This->file, pv, cb, &read, NULL))
247         return INET_E_DOWNLOAD_FAILURE;
248
249     if(pcbRead)
250         *pcbRead = read;
251     
252     return cb == read ? S_OK : S_FALSE;
253 }
254
255 static HRESULT WINAPI FileProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
256         DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
257 {
258     FileProtocol *This = PROTOCOL_THIS(iface);
259     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
260     return E_NOTIMPL;
261 }
262
263 static HRESULT WINAPI FileProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
264 {
265     FileProtocol *This = PROTOCOL_THIS(iface);
266
267     TRACE("(%p)->(%08x)\n", This, dwOptions);
268
269     return S_OK;
270 }
271
272 static HRESULT WINAPI FileProtocol_UnlockRequest(IInternetProtocol *iface)
273 {
274     FileProtocol *This = PROTOCOL_THIS(iface);
275
276     TRACE("(%p)\n", This);
277
278     return S_OK;
279 }
280
281 #undef PROTOCOL_THIS
282
283 static const IInternetProtocolVtbl FileProtocolVtbl = {
284     FileProtocol_QueryInterface,
285     FileProtocol_AddRef,
286     FileProtocol_Release,
287     FileProtocol_Start,
288     FileProtocol_Continue,
289     FileProtocol_Abort,
290     FileProtocol_Terminate,
291     FileProtocol_Suspend,
292     FileProtocol_Resume,
293     FileProtocol_Read,
294     FileProtocol_Seek,
295     FileProtocol_LockRequest,
296     FileProtocol_UnlockRequest
297 };
298
299 #define PRIORITY_THIS(iface) DEFINE_THIS(FileProtocol, InternetPriority, iface)
300
301 static HRESULT WINAPI FilePriority_QueryInterface(IInternetPriority *iface,
302                                                   REFIID riid, void **ppv)
303 {
304     FileProtocol *This = PRIORITY_THIS(iface);
305     return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
306 }
307
308 static ULONG WINAPI FilePriority_AddRef(IInternetPriority *iface)
309 {
310     FileProtocol *This = PRIORITY_THIS(iface);
311     return IInternetProtocol_AddRef(PROTOCOL(This));
312 }
313
314 static ULONG WINAPI FilePriority_Release(IInternetPriority *iface)
315 {
316     FileProtocol *This = PRIORITY_THIS(iface);
317     return IInternetProtocol_Release(PROTOCOL(This));
318 }
319
320 static HRESULT WINAPI FilePriority_SetPriority(IInternetPriority *iface, LONG nPriority)
321 {
322     FileProtocol *This = PRIORITY_THIS(iface);
323
324     TRACE("(%p)->(%d)\n", This, nPriority);
325
326     This->priority = nPriority;
327     return S_OK;
328 }
329
330 static HRESULT WINAPI FilePriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
331 {
332     FileProtocol *This = PRIORITY_THIS(iface);
333
334     TRACE("(%p)->(%p)\n", This, pnPriority);
335
336     *pnPriority = This->priority;
337     return S_OK;
338 }
339
340 #undef PRIORITY_THIS
341
342 static const IInternetPriorityVtbl FilePriorityVtbl = {
343     FilePriority_QueryInterface,
344     FilePriority_AddRef,
345     FilePriority_Release,
346     FilePriority_SetPriority,
347     FilePriority_GetPriority
348 };
349
350 HRESULT FileProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
351 {
352     FileProtocol *ret;
353
354     TRACE("(%p %p)\n", pUnkOuter, ppobj);
355
356     URLMON_LockModule();
357
358     ret = heap_alloc(sizeof(FileProtocol));
359
360     ret->lpInternetProtocolVtbl = &FileProtocolVtbl;
361     ret->lpInternetPriorityVtbl = &FilePriorityVtbl;
362     ret->file = NULL;
363     ret->priority = 0;
364     ret->ref = 1;
365
366     *ppobj = PROTOCOL(ret);
367     
368     return S_OK;
369 }