Implement RtlInitAnsiStringEx.
[wine/multimedia.git] / dlls / urlmon / file.c
blob16e3f10d89968954209cb46aa127bed4bd5a8d1e
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27 #include "urlmon.h"
28 #include "urlmon_main.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
34 typedef struct {
35 const IInternetProtocolVtbl *lpInternetProtocolVtbl;
37 HANDLE file;
39 LONG ref;
40 } FileProtocol;
42 #define PROTOCOL_THIS(iface) DEFINE_THIS(FileProtocol, InternetProtocol, iface)
44 #define PROTOCOL(x) ((IInternetProtocol*) &(x)->lpInternetProtocolVtbl)
46 static HRESULT WINAPI FileProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
48 FileProtocol *This = PROTOCOL_THIS(iface);
50 *ppv = NULL;
51 if(IsEqualGUID(&IID_IUnknown, riid)) {
52 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
53 *ppv = PROTOCOL(This);
54 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
55 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
56 *ppv = PROTOCOL(This);
57 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
58 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
59 *ppv = PROTOCOL(This);
62 if(*ppv) {
63 IInternetProtocol_AddRef(iface);
64 return S_OK;
67 WARN("not supported interface %s\n", debugstr_guid(riid));
68 return E_NOINTERFACE;
71 static ULONG WINAPI FileProtocol_AddRef(IInternetProtocol *iface)
73 FileProtocol *This = PROTOCOL_THIS(iface);
74 LONG ref = InterlockedIncrement(&This->ref);
75 TRACE("(%p) ref=%ld\n", This, ref);
76 return ref;
79 static ULONG WINAPI FileProtocol_Release(IInternetProtocol *iface)
81 FileProtocol *This = PROTOCOL_THIS(iface);
82 LONG ref = InterlockedDecrement(&This->ref);
84 TRACE("(%p) ref=%ld\n", This, ref);
86 if(!ref) {
87 if(This->file)
88 CloseHandle(This->file);
89 HeapFree(GetProcessHeap(), 0, This);
91 URLMON_UnlockModule();
94 return ref;
97 static HRESULT WINAPI FileProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
98 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
99 DWORD grfPI, DWORD dwReserved)
101 FileProtocol *This = PROTOCOL_THIS(iface);
102 BINDINFO bindinfo;
103 DWORD grfBINDF = 0;
104 LARGE_INTEGER size;
105 DWORD len;
106 LPWSTR url, mime = NULL;
107 WCHAR null_char = 0;
108 HRESULT hres;
110 static const WCHAR wszFile[] = {'f','i','l','e',':'};
112 TRACE("(%p)->(%s %p %p %08lx %ld)\n", This, debugstr_w(szUrl), pOIProtSink,
113 pOIBindInfo, grfPI, dwReserved);
115 memset(&bindinfo, 0, sizeof(bindinfo));
116 bindinfo.cbSize = sizeof(BINDINFO);
117 IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
119 if(lstrlenW(szUrl) < sizeof(wszFile)/sizeof(WCHAR)
120 || memcmp(szUrl, wszFile, sizeof(wszFile)))
121 return MK_E_SYNTAX;
123 len = lstrlenW(szUrl)+16;
124 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
125 hres = CoInternetParseUrl(szUrl, PARSE_ENCODE, 0, url, len, &len, 0);
126 if(FAILED(hres)) {
127 HeapFree(GetProcessHeap(), 0, url);
128 return hres;
131 hres = FindMimeFromData(NULL, url, NULL, 0, NULL, 0, &mime, 0);
132 if(SUCCEEDED(hres))
133 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_DIRECTBIND, mime);
135 if(!This->file) {
136 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, &null_char);
138 This->file = CreateFileW(url+sizeof(wszFile)/sizeof(WCHAR), GENERIC_READ, FILE_SHARE_READ,
139 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
141 if(This->file == INVALID_HANDLE_VALUE) {
142 This->file = NULL;
143 IInternetProtocolSink_ReportResult(pOIProtSink, INET_E_RESOURCE_NOT_FOUND,
144 GetLastError(), NULL);
145 HeapFree(GetProcessHeap(), 0, url);
146 CoTaskMemFree(mime);
147 return INET_E_RESOURCE_NOT_FOUND;
150 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_CACHEFILENAMEAVAILABLE,
151 url+sizeof(wszFile)/sizeof(WCHAR));
152 if(mime)
153 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
154 IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
157 CoTaskMemFree(mime);
158 HeapFree(GetProcessHeap(), 0, url);
160 if(GetFileSizeEx(This->file, &size))
161 IInternetProtocolSink_ReportData(pOIProtSink,
162 BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION,
163 size.u.LowPart, size.u.LowPart);
165 return S_OK;
168 static HRESULT WINAPI FileProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
170 FileProtocol *This = PROTOCOL_THIS(iface);
171 FIXME("(%p)->(%p)\n", This, pProtocolData);
172 return E_NOTIMPL;
175 static HRESULT WINAPI FileProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
176 DWORD dwOptions)
178 FileProtocol *This = PROTOCOL_THIS(iface);
179 FIXME("(%p)->(%08lx %08lx)\n", This, hrReason, dwOptions);
180 return E_NOTIMPL;
183 static HRESULT WINAPI FileProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
185 FileProtocol *This = PROTOCOL_THIS(iface);
187 TRACE("(%p)->(%08lx)\n", This, dwOptions);
189 return S_OK;
192 static HRESULT WINAPI FileProtocol_Suspend(IInternetProtocol *iface)
194 FileProtocol *This = PROTOCOL_THIS(iface);
195 FIXME("(%p)\n", This);
196 return E_NOTIMPL;
199 static HRESULT WINAPI FileProtocol_Resume(IInternetProtocol *iface)
201 FileProtocol *This = PROTOCOL_THIS(iface);
202 FIXME("(%p)\n", This);
203 return E_NOTIMPL;
206 static HRESULT WINAPI FileProtocol_Read(IInternetProtocol *iface, void *pv,
207 ULONG cb, ULONG *pcbRead)
209 FileProtocol *This = PROTOCOL_THIS(iface);
210 DWORD read = 0;
212 TRACE("(%p)->(%p %lu %p)\n", This, pv, cb, pcbRead);
214 if(!This->file)
215 return INET_E_DATA_NOT_AVAILABLE;
217 ReadFile(This->file, pv, cb, &read, NULL);
219 if(pcbRead)
220 *pcbRead = read;
222 return cb == read ? S_OK : S_FALSE;
225 static HRESULT WINAPI FileProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
226 DWORD dwOrgin, ULARGE_INTEGER *plibNewPosition)
228 FileProtocol *This = PROTOCOL_THIS(iface);
229 FIXME("(%p)->(%ld %ld %p)\n", This, dlibMove.u.LowPart, dwOrgin, plibNewPosition);
230 return E_NOTIMPL;
233 static HRESULT WINAPI FileProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
235 FileProtocol *This = PROTOCOL_THIS(iface);
237 TRACE("(%p)->(%08lx)\n", This, dwOptions);
239 return S_OK;
242 static HRESULT WINAPI FileProtocol_UnlockRequest(IInternetProtocol *iface)
244 FileProtocol *This = PROTOCOL_THIS(iface);
246 TRACE("(%p)\n", This);
248 return S_OK;
251 #undef PROTOCOL_THIS
253 static const IInternetProtocolVtbl FileProtocolVtbl = {
254 FileProtocol_QueryInterface,
255 FileProtocol_AddRef,
256 FileProtocol_Release,
257 FileProtocol_Start,
258 FileProtocol_Continue,
259 FileProtocol_Abort,
260 FileProtocol_Terminate,
261 FileProtocol_Suspend,
262 FileProtocol_Resume,
263 FileProtocol_Read,
264 FileProtocol_Seek,
265 FileProtocol_LockRequest,
266 FileProtocol_UnlockRequest
269 HRESULT FileProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
271 FileProtocol *ret;
273 TRACE("(%p %p)\n", pUnkOuter, ppobj);
275 URLMON_LockModule();
277 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(FileProtocol));
279 ret->lpInternetProtocolVtbl = &FileProtocolVtbl;
280 ret->file = NULL;
281 ret->ref = 1;
283 *ppobj = PROTOCOL(ret);
285 return S_OK;