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