d3dcompiler: Add D3D_BLOB_LEGACY_SHADER to D3DGetBlobPart().
[wine.git] / dlls / urlmon / file.c
bloba190156533692b42be41171c2c2051849b45c12b
1 /*
2 * Copyright 2005 Jacek Caban
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.
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.
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
19 #include "urlmon_main.h"
20 #include "winreg.h"
21 #include "shlwapi.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
27 typedef struct {
28 const IInternetProtocolExVtbl *lpIInternetProtocolExVtbl;
29 const IInternetPriorityVtbl *lpInternetPriorityVtbl;
31 HANDLE file;
32 ULONG size;
33 LONG priority;
35 LONG ref;
36 } FileProtocol;
38 #define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
39 #define PROTOCOLEX(x) ((IInternetProtocolEx*) &(x)->lpIInternetProtocolExVtbl)
41 #define PROTOCOL_THIS(iface) DEFINE_THIS(FileProtocol, IInternetProtocolEx, iface)
43 static HRESULT WINAPI FileProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
45 FileProtocol *This = PROTOCOL_THIS(iface);
47 *ppv = NULL;
48 if(IsEqualGUID(&IID_IUnknown, riid)) {
49 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
50 *ppv = PROTOCOLEX(This);
51 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
52 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
53 *ppv = PROTOCOLEX(This);
54 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
55 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
56 *ppv = PROTOCOLEX(This);
57 }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
58 TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
59 *ppv = PROTOCOLEX(This);
60 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
61 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
62 *ppv = PRIORITY(This);
65 if(*ppv) {
66 IInternetProtocol_AddRef(iface);
67 return S_OK;
70 WARN("not supported interface %s\n", debugstr_guid(riid));
71 return E_NOINTERFACE;
74 static ULONG WINAPI FileProtocol_AddRef(IInternetProtocolEx *iface)
76 FileProtocol *This = PROTOCOL_THIS(iface);
77 LONG ref = InterlockedIncrement(&This->ref);
78 TRACE("(%p) ref=%d\n", This, ref);
79 return ref;
82 static ULONG WINAPI FileProtocol_Release(IInternetProtocolEx *iface)
84 FileProtocol *This = PROTOCOL_THIS(iface);
85 LONG ref = InterlockedDecrement(&This->ref);
87 TRACE("(%p) ref=%d\n", This, ref);
89 if(!ref) {
90 if(This->file != INVALID_HANDLE_VALUE)
91 CloseHandle(This->file);
92 heap_free(This);
94 URLMON_UnlockModule();
97 return ref;
100 static HRESULT WINAPI FileProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
101 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
102 DWORD grfPI, HANDLE_PTR dwReserved)
104 FileProtocol *This = PROTOCOL_THIS(iface);
105 IUri *uri;
106 HRESULT hres;
108 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
109 pOIBindInfo, grfPI, dwReserved);
111 hres = CreateUri(szUrl, Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
112 if(FAILED(hres))
113 return hres;
115 hres = IInternetProtocolEx_StartEx(PROTOCOLEX(This), uri, pOIProtSink, pOIBindInfo,
116 grfPI, (HANDLE*)dwReserved);
118 IUri_Release(uri);
119 return hres;
122 static HRESULT WINAPI FileProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
124 FileProtocol *This = PROTOCOL_THIS(iface);
125 FIXME("(%p)->(%p)\n", This, pProtocolData);
126 return E_NOTIMPL;
129 static HRESULT WINAPI FileProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
130 DWORD dwOptions)
132 FileProtocol *This = PROTOCOL_THIS(iface);
133 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
134 return E_NOTIMPL;
137 static HRESULT WINAPI FileProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
139 FileProtocol *This = PROTOCOL_THIS(iface);
141 TRACE("(%p)->(%08x)\n", This, dwOptions);
143 return S_OK;
146 static HRESULT WINAPI FileProtocol_Suspend(IInternetProtocolEx *iface)
148 FileProtocol *This = PROTOCOL_THIS(iface);
149 FIXME("(%p)\n", This);
150 return E_NOTIMPL;
153 static HRESULT WINAPI FileProtocol_Resume(IInternetProtocolEx *iface)
155 FileProtocol *This = PROTOCOL_THIS(iface);
156 FIXME("(%p)\n", This);
157 return E_NOTIMPL;
160 static HRESULT WINAPI FileProtocol_Read(IInternetProtocolEx *iface, void *pv,
161 ULONG cb, ULONG *pcbRead)
163 FileProtocol *This = PROTOCOL_THIS(iface);
164 DWORD read = 0;
166 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
168 if (pcbRead)
169 *pcbRead = 0;
171 if(This->file == INVALID_HANDLE_VALUE)
172 return INET_E_DATA_NOT_AVAILABLE;
174 if (!ReadFile(This->file, pv, cb, &read, NULL))
175 return INET_E_DOWNLOAD_FAILURE;
177 if(pcbRead)
178 *pcbRead = read;
180 return cb == read ? S_OK : S_FALSE;
183 static HRESULT WINAPI FileProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
184 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
186 FileProtocol *This = PROTOCOL_THIS(iface);
187 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
188 return E_NOTIMPL;
191 static HRESULT WINAPI FileProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
193 FileProtocol *This = PROTOCOL_THIS(iface);
195 TRACE("(%p)->(%08x)\n", This, dwOptions);
197 return S_OK;
200 static HRESULT WINAPI FileProtocol_UnlockRequest(IInternetProtocolEx *iface)
202 FileProtocol *This = PROTOCOL_THIS(iface);
204 TRACE("(%p)\n", This);
206 return S_OK;
209 static inline HRESULT report_result(IInternetProtocolSink *protocol_sink, HRESULT hres, DWORD res)
211 IInternetProtocolSink_ReportResult(protocol_sink, hres, res, NULL);
212 return hres;
215 static HRESULT open_file(FileProtocol *This, const WCHAR *path, IInternetProtocolSink *protocol_sink)
217 LARGE_INTEGER size;
218 HANDLE file;
220 file = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL,
221 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
222 if(file == INVALID_HANDLE_VALUE)
223 return report_result(protocol_sink, INET_E_RESOURCE_NOT_FOUND, GetLastError());
225 if(!GetFileSizeEx(file, &size)) {
226 CloseHandle(file);
227 return report_result(protocol_sink, INET_E_RESOURCE_NOT_FOUND, GetLastError());
230 This->file = file;
231 This->size = size.u.LowPart;
233 IInternetProtocolSink_ReportProgress(protocol_sink,
234 BINDSTATUS_CACHEFILENAMEAVAILABLE, path);
235 return S_OK;
238 static HRESULT WINAPI FileProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
239 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
240 DWORD grfPI, HANDLE *dwReserved)
242 FileProtocol *This = PROTOCOL_THIS(iface);
243 BINDINFO bindinfo;
244 DWORD grfBINDF = 0;
245 DWORD scheme;
246 LPWSTR mime = NULL;
247 WCHAR null_char = 0;
248 BSTR path, url;
249 HRESULT hres;
251 TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink,
252 pOIBindInfo, grfPI, dwReserved);
254 if(!pUri)
255 return E_INVALIDARG;
257 scheme = 0;
258 hres = IUri_GetScheme(pUri, &scheme);
259 if(FAILED(hres))
260 return hres;
261 if(scheme != URL_SCHEME_FILE)
262 return E_INVALIDARG;
264 memset(&bindinfo, 0, sizeof(bindinfo));
265 bindinfo.cbSize = sizeof(BINDINFO);
266 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
267 if(FAILED(hres)) {
268 WARN("GetBindInfo failed: %08x\n", hres);
269 return hres;
272 ReleaseBindInfo(&bindinfo);
274 if(!(grfBINDF & BINDF_FROMURLMON))
275 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_DIRECTBIND, NULL);
277 if(This->file != INVALID_HANDLE_VALUE) {
278 IInternetProtocolSink_ReportData(pOIProtSink,
279 BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION,
280 This->size, This->size);
281 return S_OK;
284 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, &null_char);
286 hres = IUri_GetPath(pUri, &path);
287 if(FAILED(hres)) {
288 ERR("GetPath failed: %08x\n", hres);
289 return report_result(pOIProtSink, hres, 0);
292 hres = open_file(This, path, pOIProtSink);
293 SysFreeString(path);
294 if(FAILED(hres))
295 return hres;
297 hres = IUri_GetDisplayUri(pUri, &url);
298 if(hres == S_OK) {
299 hres = FindMimeFromData(NULL, url, NULL, 0, NULL, 0, &mime, 0);
300 SysFreeString(url);
301 if(SUCCEEDED(hres)) {
302 IInternetProtocolSink_ReportProgress(pOIProtSink,
303 (grfBINDF & BINDF_FROMURLMON) ?
304 BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE : BINDSTATUS_MIMETYPEAVAILABLE,
305 mime);
306 CoTaskMemFree(mime);
310 IInternetProtocolSink_ReportData(pOIProtSink,
311 BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION,
312 This->size, This->size);
314 return report_result(pOIProtSink, S_OK, 0);
317 #undef PROTOCOL_THIS
319 static const IInternetProtocolExVtbl FileProtocolExVtbl = {
320 FileProtocol_QueryInterface,
321 FileProtocol_AddRef,
322 FileProtocol_Release,
323 FileProtocol_Start,
324 FileProtocol_Continue,
325 FileProtocol_Abort,
326 FileProtocol_Terminate,
327 FileProtocol_Suspend,
328 FileProtocol_Resume,
329 FileProtocol_Read,
330 FileProtocol_Seek,
331 FileProtocol_LockRequest,
332 FileProtocol_UnlockRequest,
333 FileProtocol_StartEx
336 #define PRIORITY_THIS(iface) DEFINE_THIS(FileProtocol, InternetPriority, iface)
338 static HRESULT WINAPI FilePriority_QueryInterface(IInternetPriority *iface,
339 REFIID riid, void **ppv)
341 FileProtocol *This = PRIORITY_THIS(iface);
342 return IInternetProtocolEx_QueryInterface(PROTOCOLEX(This), riid, ppv);
345 static ULONG WINAPI FilePriority_AddRef(IInternetPriority *iface)
347 FileProtocol *This = PRIORITY_THIS(iface);
348 return IInternetProtocolEx_AddRef(PROTOCOLEX(This));
351 static ULONG WINAPI FilePriority_Release(IInternetPriority *iface)
353 FileProtocol *This = PRIORITY_THIS(iface);
354 return IInternetProtocolEx_Release(PROTOCOLEX(This));
357 static HRESULT WINAPI FilePriority_SetPriority(IInternetPriority *iface, LONG nPriority)
359 FileProtocol *This = PRIORITY_THIS(iface);
361 TRACE("(%p)->(%d)\n", This, nPriority);
363 This->priority = nPriority;
364 return S_OK;
367 static HRESULT WINAPI FilePriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
369 FileProtocol *This = PRIORITY_THIS(iface);
371 TRACE("(%p)->(%p)\n", This, pnPriority);
373 *pnPriority = This->priority;
374 return S_OK;
377 #undef PRIORITY_THIS
379 static const IInternetPriorityVtbl FilePriorityVtbl = {
380 FilePriority_QueryInterface,
381 FilePriority_AddRef,
382 FilePriority_Release,
383 FilePriority_SetPriority,
384 FilePriority_GetPriority
387 HRESULT FileProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
389 FileProtocol *ret;
391 TRACE("(%p %p)\n", pUnkOuter, ppobj);
393 URLMON_LockModule();
395 ret = heap_alloc(sizeof(FileProtocol));
397 ret->lpIInternetProtocolExVtbl = &FileProtocolExVtbl;
398 ret->lpInternetPriorityVtbl = &FilePriorityVtbl;
399 ret->file = INVALID_HANDLE_VALUE;
400 ret->priority = 0;
401 ret->ref = 1;
403 *ppobj = PROTOCOLEX(ret);
404 return S_OK;