windowscodecs: Add support for converting to 32bppPBGRA.
[wine.git] / dlls / mshtml / protocol.c
blob456c3fd8d31771381784cab976c85024e3362023
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 "config.h"
21 #include <stdarg.h>
22 #include <stdio.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "ole2.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
34 #include "mshtml_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38 /********************************************************************
39 * common ProtocolFactory implementation
42 typedef struct {
43 IInternetProtocolInfo IInternetProtocolInfo_iface;
44 IClassFactory IClassFactory_iface;
45 } ProtocolFactory;
47 static inline ProtocolFactory *impl_from_IInternetProtocolInfo(IInternetProtocolInfo *iface)
49 return CONTAINING_RECORD(iface, ProtocolFactory, IInternetProtocolInfo_iface);
52 static HRESULT WINAPI InternetProtocolInfo_QueryInterface(IInternetProtocolInfo *iface, REFIID riid, void **ppv)
54 ProtocolFactory *This = impl_from_IInternetProtocolInfo(iface);
56 *ppv = NULL;
57 if(IsEqualGUID(&IID_IUnknown, riid)) {
58 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
59 *ppv = &This->IInternetProtocolInfo_iface;
60 }else if(IsEqualGUID(&IID_IInternetProtocolInfo, riid)) {
61 TRACE("(%p)->(IID_IInternetProtocolInfo %p)\n", This, ppv);
62 *ppv = &This->IInternetProtocolInfo_iface;
63 }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
64 TRACE("(%p)->(IID_IClassFactory %p)\n", This, ppv);
65 *ppv = &This->IClassFactory_iface;
68 if(!*ppv) {
69 WARN("unknown interface %s\n", debugstr_guid(riid));
70 return E_NOINTERFACE;
73 IInternetProtocolInfo_AddRef(iface);
74 return S_OK;
77 static ULONG WINAPI InternetProtocolInfo_AddRef(IInternetProtocolInfo *iface)
79 TRACE("(%p)\n", iface);
80 return 2;
83 static ULONG WINAPI InternetProtocolInfo_Release(IInternetProtocolInfo *iface)
85 TRACE("(%p)\n", iface);
86 return 1;
89 static HRESULT WINAPI InternetProtocolInfo_CombineUrl(IInternetProtocolInfo *iface,
90 LPCWSTR pwzBaseUrl, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags, LPWSTR pwzResult,
91 DWORD cchResult, DWORD* pcchResult, DWORD dwReserved)
93 TRACE("%p)->(%s %s %08x %p %d %p %d)\n", iface, debugstr_w(pwzBaseUrl),
94 debugstr_w(pwzRelativeUrl), dwCombineFlags, pwzResult, cchResult,
95 pcchResult, dwReserved);
97 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
100 static HRESULT WINAPI InternetProtocolInfo_CompareUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl1,
101 LPCWSTR pwzUrl2, DWORD dwCompareFlags)
103 TRACE("%p)->(%s %s %08x)\n", iface, debugstr_w(pwzUrl1), debugstr_w(pwzUrl2), dwCompareFlags);
104 return E_NOTIMPL;
107 static inline ProtocolFactory *impl_from_IClassFactory(IClassFactory *iface)
109 return CONTAINING_RECORD(iface, ProtocolFactory, IClassFactory_iface);
112 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
114 ProtocolFactory *This = impl_from_IClassFactory(iface);
115 return IInternetProtocolInfo_QueryInterface(&This->IInternetProtocolInfo_iface, riid, ppv);
118 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
120 ProtocolFactory *This = impl_from_IClassFactory(iface);
121 return IInternetProtocolInfo_AddRef(&This->IInternetProtocolInfo_iface);
124 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
126 ProtocolFactory *This = impl_from_IClassFactory(iface);
127 return IInternetProtocolInfo_Release(&This->IInternetProtocolInfo_iface);
130 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
132 TRACE("(%p)->(%x)\n", iface, dolock);
133 return S_OK;
136 /********************************************************************
137 * AboutProtocol implementation
140 typedef struct {
141 IInternetProtocol IInternetProtocol_iface;
143 LONG ref;
145 BYTE *data;
146 ULONG data_len;
147 ULONG cur;
149 IUnknown *pUnkOuter;
150 } AboutProtocol;
152 static inline AboutProtocol *AboutProtocol_from_IInternetProtocol(IInternetProtocol *iface)
154 return CONTAINING_RECORD(iface, AboutProtocol, IInternetProtocol_iface);
157 static HRESULT WINAPI AboutProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
159 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
161 *ppv = NULL;
163 if(IsEqualGUID(&IID_IUnknown, riid)) {
164 TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
165 if(This->pUnkOuter)
166 return IUnknown_QueryInterface(This->pUnkOuter, riid, ppv);
167 *ppv = &This->IInternetProtocol_iface;
168 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
169 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", iface, ppv);
170 *ppv = &This->IInternetProtocol_iface;
171 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
172 TRACE("(%p)->(IID_IInternetProtocol %p)\n", iface, ppv);
173 *ppv = &This->IInternetProtocol_iface;
174 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
175 FIXME("IServiceProvider is not implemented\n");
176 return E_NOINTERFACE;
179 if(!*ppv) {
180 TRACE("unknown interface %s\n", debugstr_guid(riid));
181 return E_NOINTERFACE;
184 IInternetProtocol_AddRef(iface);
185 return S_OK;
188 static ULONG WINAPI AboutProtocol_AddRef(IInternetProtocol *iface)
190 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
191 ULONG ref = InterlockedIncrement(&This->ref);
192 TRACE("(%p) ref=%d\n", iface, ref);
193 return This->pUnkOuter ? IUnknown_AddRef(This->pUnkOuter) : ref;
196 static ULONG WINAPI AboutProtocol_Release(IInternetProtocol *iface)
198 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
199 IUnknown *pUnkOuter = This->pUnkOuter;
200 ULONG ref = InterlockedDecrement(&This->ref);
202 TRACE("(%p) ref=%x\n", iface, ref);
204 if(!ref) {
205 heap_free(This->data);
206 heap_free(This);
209 return pUnkOuter ? IUnknown_Release(pUnkOuter) : ref;
212 static HRESULT WINAPI AboutProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
213 IInternetProtocolSink* pOIProtSink, IInternetBindInfo* pOIBindInfo,
214 DWORD grfPI, HANDLE_PTR dwReserved)
216 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
217 BINDINFO bindinfo;
218 DWORD grfBINDF = 0;
219 LPCWSTR text = NULL;
220 DWORD data_len;
221 BYTE *data;
223 static const WCHAR html_begin[] = {0xfeff,'<','H','T','M','L','>',0};
224 static const WCHAR html_end[] = {'<','/','H','T','M','L','>',0};
225 static const WCHAR wszBlank[] = {'b','l','a','n','k',0};
226 static const WCHAR wszAbout[] = {'a','b','o','u','t',':'};
227 static const WCHAR wszTextHtml[] = {'t','e','x','t','/','h','t','m','l',0};
229 /* NOTE:
230 * the about protocol seems not to work as I would expect. It creates html document
231 * for a given url, eg. about:some_text -> <HTML>some_text</HTML> except for the case when
232 * some_text = "blank", when document is blank (<HTML></HMTL>). The same happens
233 * when the url does not have "about:" in the beginning.
236 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
237 pOIBindInfo, grfPI, dwReserved);
239 memset(&bindinfo, 0, sizeof(bindinfo));
240 bindinfo.cbSize = sizeof(BINDINFO);
241 IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
242 ReleaseBindInfo(&bindinfo);
244 TRACE("bindf %x\n", grfBINDF);
246 if(strlenW(szUrl)>=sizeof(wszAbout)/sizeof(WCHAR) && !memcmp(wszAbout, szUrl, sizeof(wszAbout))) {
247 text = szUrl + sizeof(wszAbout)/sizeof(WCHAR);
248 if(!strcmpW(wszBlank, text))
249 text = NULL;
252 data_len = sizeof(html_begin)+sizeof(html_end)-sizeof(WCHAR)
253 + (text ? strlenW(text)*sizeof(WCHAR) : 0);
254 data = heap_alloc(data_len);
255 if(!data)
256 return E_OUTOFMEMORY;
258 heap_free(This->data);
259 This->data = data;
260 This->data_len = data_len;
262 memcpy(This->data, html_begin, sizeof(html_begin));
263 if(text)
264 strcatW((LPWSTR)This->data, text);
265 strcatW((LPWSTR)This->data, html_end);
267 This->cur = 0;
269 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, wszTextHtml);
271 IInternetProtocolSink_ReportData(pOIProtSink,
272 BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE,
273 This->data_len, This->data_len);
275 IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
277 return S_OK;
280 static HRESULT WINAPI AboutProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA* pProtocolData)
282 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
283 FIXME("(%p)->(%p)\n", This, pProtocolData);
284 return E_NOTIMPL;
287 static HRESULT WINAPI AboutProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
288 DWORD dwOptions)
290 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
291 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
292 return E_NOTIMPL;
295 static HRESULT WINAPI AboutProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
297 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
298 TRACE("(%p)->(%08x)\n", This, dwOptions);
299 return S_OK;
302 static HRESULT WINAPI AboutProtocol_Suspend(IInternetProtocol *iface)
304 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
305 FIXME("(%p)\n", This);
306 return E_NOTIMPL;
309 static HRESULT WINAPI AboutProtocol_Resume(IInternetProtocol *iface)
311 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
312 FIXME("(%p)\n", This);
313 return E_NOTIMPL;
316 static HRESULT WINAPI AboutProtocol_Read(IInternetProtocol *iface, void* pv, ULONG cb, ULONG* pcbRead)
318 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
320 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
322 if(!This->data)
323 return E_FAIL;
325 *pcbRead = (cb > This->data_len-This->cur ? This->data_len-This->cur : cb);
327 if(!*pcbRead)
328 return S_FALSE;
330 memcpy(pv, This->data+This->cur, *pcbRead);
331 This->cur += *pcbRead;
333 return S_OK;
336 static HRESULT WINAPI AboutProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
337 DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
339 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
340 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
341 return E_NOTIMPL;
344 static HRESULT WINAPI AboutProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
346 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
348 TRACE("(%p)->(%d)\n", This, dwOptions);
350 return S_OK;
353 static HRESULT WINAPI AboutProtocol_UnlockRequest(IInternetProtocol *iface)
355 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
357 TRACE("(%p)\n", This);
359 return S_OK;
362 static const IInternetProtocolVtbl AboutProtocolVtbl = {
363 AboutProtocol_QueryInterface,
364 AboutProtocol_AddRef,
365 AboutProtocol_Release,
366 AboutProtocol_Start,
367 AboutProtocol_Continue,
368 AboutProtocol_Abort,
369 AboutProtocol_Terminate,
370 AboutProtocol_Suspend,
371 AboutProtocol_Resume,
372 AboutProtocol_Read,
373 AboutProtocol_Seek,
374 AboutProtocol_LockRequest,
375 AboutProtocol_UnlockRequest
378 static HRESULT WINAPI AboutProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
379 REFIID riid, void **ppv)
381 AboutProtocol *ret;
382 HRESULT hres = S_OK;
384 TRACE("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
386 ret = heap_alloc(sizeof(AboutProtocol));
387 ret->IInternetProtocol_iface.lpVtbl = &AboutProtocolVtbl;
388 ret->ref = 0;
390 ret->data = NULL;
391 ret->data_len = 0;
392 ret->cur = 0;
393 ret->pUnkOuter = pUnkOuter;
395 if(pUnkOuter) {
396 ret->ref = 1;
397 if(IsEqualGUID(&IID_IUnknown, riid))
398 *ppv = &ret->IInternetProtocol_iface;
399 else
400 hres = E_INVALIDARG;
401 }else {
402 hres = IInternetProtocol_QueryInterface(&ret->IInternetProtocol_iface, riid, ppv);
405 if(FAILED(hres))
406 heap_free(ret);
408 return hres;
411 static HRESULT WINAPI AboutProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
412 PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
413 DWORD* pcchResult, DWORD dwReserved)
415 TRACE("%p)->(%s %d %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
416 dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
418 if(ParseAction == PARSE_SECURITY_URL) {
419 unsigned int len = strlenW(pwzUrl)+1;
421 *pcchResult = len;
422 if(len > cchResult)
423 return S_FALSE;
425 memcpy(pwzResult, pwzUrl, len*sizeof(WCHAR));
426 return S_OK;
429 if(ParseAction == PARSE_DOMAIN) {
430 if(!pcchResult)
431 return E_POINTER;
433 if(pwzUrl)
434 *pcchResult = strlenW(pwzUrl)+1;
435 else
436 *pcchResult = 1;
437 return E_FAIL;
440 return INET_E_DEFAULT_ACTION;
443 static HRESULT WINAPI AboutProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
444 QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
445 DWORD dwReserved)
447 TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
448 cbBuffer, pcbBuf, dwReserved);
450 switch(QueryOption) {
451 case QUERY_CAN_NAVIGATE:
452 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
454 case QUERY_USES_NETWORK:
455 if(!pBuffer || cbBuffer < sizeof(DWORD))
456 return E_FAIL;
458 *(DWORD*)pBuffer = 0;
459 if(pcbBuf)
460 *pcbBuf = sizeof(DWORD);
462 break;
464 case QUERY_IS_CACHED:
465 FIXME("Unsupported option QUERY_IS_CACHED\n");
466 return E_NOTIMPL;
467 case QUERY_IS_INSTALLEDENTRY:
468 FIXME("Unsupported option QUERY_IS_INSTALLEDENTRY\n");
469 return E_NOTIMPL;
470 case QUERY_IS_CACHED_OR_MAPPED:
471 FIXME("Unsupported option QUERY_IS_CACHED_OR_MAPPED\n");
472 return E_NOTIMPL;
473 case QUERY_IS_SECURE:
474 FIXME("Unsupported option QUERY_IS_SECURE\n");
475 return E_NOTIMPL;
476 case QUERY_IS_SAFE:
477 FIXME("Unsupported option QUERY_IS_SAFE\n");
478 return E_NOTIMPL;
479 case QUERY_USES_HISTORYFOLDER:
480 FIXME("Unsupported option QUERY_USES_HISTORYFOLDER\n");
481 return E_FAIL;
482 default:
483 return E_FAIL;
486 return S_OK;
489 static const IInternetProtocolInfoVtbl AboutProtocolInfoVtbl = {
490 InternetProtocolInfo_QueryInterface,
491 InternetProtocolInfo_AddRef,
492 InternetProtocolInfo_Release,
493 AboutProtocolInfo_ParseUrl,
494 InternetProtocolInfo_CombineUrl,
495 InternetProtocolInfo_CompareUrl,
496 AboutProtocolInfo_QueryInfo
499 static const IClassFactoryVtbl AboutProtocolFactoryVtbl = {
500 ClassFactory_QueryInterface,
501 ClassFactory_AddRef,
502 ClassFactory_Release,
503 AboutProtocolFactory_CreateInstance,
504 ClassFactory_LockServer
507 static ProtocolFactory AboutProtocolFactory = {
508 { &AboutProtocolInfoVtbl },
509 { &AboutProtocolFactoryVtbl }
512 /********************************************************************
513 * ResProtocol implementation
516 typedef struct {
517 IInternetProtocol IInternetProtocol_iface;
518 LONG ref;
520 BYTE *data;
521 ULONG data_len;
522 ULONG cur;
524 IUnknown *pUnkOuter;
525 } ResProtocol;
527 static inline ResProtocol *ResProtocol_from_IInternetProtocol(IInternetProtocol *iface)
529 return CONTAINING_RECORD(iface, ResProtocol, IInternetProtocol_iface);
532 static HRESULT WINAPI ResProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
534 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
536 *ppv = NULL;
538 if(IsEqualGUID(&IID_IUnknown, riid)) {
539 TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
540 if(This->pUnkOuter)
541 return IUnknown_QueryInterface(This->pUnkOuter, &IID_IUnknown, ppv);
542 *ppv = &This->IInternetProtocol_iface;
543 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
544 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", iface, ppv);
545 *ppv = &This->IInternetProtocol_iface;
546 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
547 TRACE("(%p)->(IID_IInternetProtocol %p)\n", iface, ppv);
548 *ppv = &This->IInternetProtocol_iface;
549 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
550 FIXME("IServiceProvider is not implemented\n");
551 return E_NOINTERFACE;
554 if(!*ppv) {
555 TRACE("unknown interface %s\n", debugstr_guid(riid));
556 return E_NOINTERFACE;
559 IInternetProtocol_AddRef(iface);
560 return S_OK;
563 static ULONG WINAPI ResProtocol_AddRef(IInternetProtocol *iface)
565 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
566 ULONG ref = InterlockedIncrement(&This->ref);
567 TRACE("(%p) ref=%d\n", iface, ref);
568 return This->pUnkOuter ? IUnknown_AddRef(This->pUnkOuter) : ref;
571 static ULONG WINAPI ResProtocol_Release(IInternetProtocol *iface)
573 ResProtocol *This = (ResProtocol*)iface;
574 IUnknown *pUnkOuter = This->pUnkOuter;
575 ULONG ref = InterlockedDecrement(&This->ref);
577 TRACE("(%p) ref=%x\n", iface, ref);
579 if(!ref) {
580 heap_free(This->data);
581 heap_free(This);
584 return pUnkOuter ? IUnknown_Release(pUnkOuter) : ref;
587 static HRESULT WINAPI ResProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
588 IInternetProtocolSink* pOIProtSink, IInternetBindInfo* pOIBindInfo,
589 DWORD grfPI, HANDLE_PTR dwReserved)
591 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
592 DWORD grfBINDF = 0, len;
593 BINDINFO bindinfo;
594 LPWSTR url_dll, url_file, url, mime, res_type = (LPWSTR)RT_HTML;
595 HMODULE hdll;
596 HRSRC src;
597 HRESULT hres;
599 static const WCHAR wszRes[] = {'r','e','s',':','/','/'};
601 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
602 pOIBindInfo, grfPI, dwReserved);
604 memset(&bindinfo, 0, sizeof(bindinfo));
605 bindinfo.cbSize = sizeof(BINDINFO);
606 IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
607 ReleaseBindInfo(&bindinfo);
609 len = strlenW(szUrl)+16;
610 url = heap_alloc(len*sizeof(WCHAR));
611 hres = CoInternetParseUrl(szUrl, PARSE_ENCODE, 0, url, len, &len, 0);
612 if(FAILED(hres)) {
613 WARN("CoInternetParseUrl failed: %08x\n", hres);
614 heap_free(url);
615 IInternetProtocolSink_ReportResult(pOIProtSink, hres, 0, NULL);
616 return hres;
619 if(len < sizeof(wszRes)/sizeof(wszRes[0]) || memcmp(url, wszRes, sizeof(wszRes))) {
620 WARN("Wrong protocol of url: %s\n", debugstr_w(url));
621 IInternetProtocolSink_ReportResult(pOIProtSink, E_INVALIDARG, 0, NULL);
622 heap_free(url);
623 return E_INVALIDARG;
626 url_dll = url + sizeof(wszRes)/sizeof(wszRes[0]);
627 if(!(url_file = strrchrW(url_dll, '/'))) {
628 WARN("wrong url: %s\n", debugstr_w(url));
629 IInternetProtocolSink_ReportResult(pOIProtSink, MK_E_SYNTAX, 0, NULL);
630 heap_free(url);
631 return MK_E_SYNTAX;
634 *url_file++ = 0;
635 hdll = LoadLibraryExW(url_dll, NULL, LOAD_LIBRARY_AS_DATAFILE);
636 if(!hdll) {
637 if (!(res_type = strrchrW(url_dll, '/'))) {
638 WARN("Could not open dll: %s\n", debugstr_w(url_dll));
639 IInternetProtocolSink_ReportResult(pOIProtSink, HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
640 heap_free(url);
641 return HRESULT_FROM_WIN32(GetLastError());
643 *res_type++ = 0;
645 hdll = LoadLibraryExW(url_dll, NULL, LOAD_LIBRARY_AS_DATAFILE);
646 if(!hdll) {
647 WARN("Could not open dll: %s\n", debugstr_w(url_dll));
648 IInternetProtocolSink_ReportResult(pOIProtSink, HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
649 heap_free(url);
650 return HRESULT_FROM_WIN32(GetLastError());
654 TRACE("trying to find resource type %s, name %s\n", debugstr_w(res_type), debugstr_w(url_file));
656 src = FindResourceW(hdll, url_file, res_type);
657 if(!src) {
658 LPWSTR endpoint = NULL;
659 DWORD file_id = strtolW(url_file, &endpoint, 10);
660 if(endpoint == url_file+strlenW(url_file))
661 src = FindResourceW(hdll, MAKEINTRESOURCEW(file_id), MAKEINTRESOURCEW(RT_HTML));
663 if(!src) {
664 WARN("Could not find resource\n");
665 IInternetProtocolSink_ReportResult(pOIProtSink,
666 HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
667 heap_free(url);
668 return HRESULT_FROM_WIN32(GetLastError());
672 if(This->data) {
673 WARN("data already loaded\n");
674 heap_free(This->data);
677 This->data_len = SizeofResource(hdll, src);
678 This->data = heap_alloc(This->data_len);
679 memcpy(This->data, LoadResource(hdll, src), This->data_len);
680 This->cur = 0;
682 FreeLibrary(hdll);
684 hres = FindMimeFromData(NULL, url_file, This->data, This->data_len, NULL, 0, &mime, 0);
685 heap_free(url);
686 if(SUCCEEDED(hres)) {
687 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
688 CoTaskMemFree(mime);
691 IInternetProtocolSink_ReportData(pOIProtSink,
692 BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE,
693 This->data_len, This->data_len);
695 IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
697 return S_OK;
700 static HRESULT WINAPI ResProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA* pProtocolData)
702 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
703 FIXME("(%p)->(%p)\n", This, pProtocolData);
704 return E_NOTIMPL;
707 static HRESULT WINAPI ResProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
708 DWORD dwOptions)
710 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
711 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
712 return E_NOTIMPL;
715 static HRESULT WINAPI ResProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
717 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
719 TRACE("(%p)->(%08x)\n", This, dwOptions);
721 /* test show that we don't have to do anything here */
722 return S_OK;
725 static HRESULT WINAPI ResProtocol_Suspend(IInternetProtocol *iface)
727 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
728 FIXME("(%p)\n", This);
729 return E_NOTIMPL;
732 static HRESULT WINAPI ResProtocol_Resume(IInternetProtocol *iface)
734 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
735 FIXME("(%p)\n", This);
736 return E_NOTIMPL;
739 static HRESULT WINAPI ResProtocol_Read(IInternetProtocol *iface, void* pv, ULONG cb, ULONG* pcbRead)
741 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
743 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
745 if(!This->data)
746 return E_FAIL;
748 *pcbRead = (cb > This->data_len-This->cur ? This->data_len-This->cur : cb);
750 if(!*pcbRead)
751 return S_FALSE;
753 memcpy(pv, This->data+This->cur, *pcbRead);
754 This->cur += *pcbRead;
756 return S_OK;
759 static HRESULT WINAPI ResProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
760 DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
762 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
763 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
764 return E_NOTIMPL;
767 static HRESULT WINAPI ResProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
769 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
771 TRACE("(%p)->(%d)\n", This, dwOptions);
773 /* test show that we don't have to do anything here */
774 return S_OK;
777 static HRESULT WINAPI ResProtocol_UnlockRequest(IInternetProtocol *iface)
779 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
781 TRACE("(%p)\n", This);
783 /* test show that we don't have to do anything here */
784 return S_OK;
787 static const IInternetProtocolVtbl ResProtocolVtbl = {
788 ResProtocol_QueryInterface,
789 ResProtocol_AddRef,
790 ResProtocol_Release,
791 ResProtocol_Start,
792 ResProtocol_Continue,
793 ResProtocol_Abort,
794 ResProtocol_Terminate,
795 ResProtocol_Suspend,
796 ResProtocol_Resume,
797 ResProtocol_Read,
798 ResProtocol_Seek,
799 ResProtocol_LockRequest,
800 ResProtocol_UnlockRequest
803 static HRESULT WINAPI ResProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
804 REFIID riid, void **ppv)
806 ResProtocol *ret;
807 HRESULT hres = S_OK;
809 TRACE("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
811 ret = heap_alloc(sizeof(ResProtocol));
812 ret->IInternetProtocol_iface.lpVtbl = &ResProtocolVtbl;
813 ret->ref = 0;
814 ret->data = NULL;
815 ret->data_len = 0;
816 ret->cur = 0;
817 ret->pUnkOuter = pUnkOuter;
819 if(pUnkOuter) {
820 ret->ref = 1;
821 if(IsEqualGUID(&IID_IUnknown, riid))
822 *ppv = &ret->IInternetProtocol_iface;
823 else
824 hres = E_FAIL;
825 }else {
826 hres = IInternetProtocol_QueryInterface(&ret->IInternetProtocol_iface, riid, ppv);
829 if(FAILED(hres))
830 heap_free(ret);
832 return hres;
835 static HRESULT WINAPI ResProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
836 PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
837 DWORD* pcchResult, DWORD dwReserved)
839 TRACE("%p)->(%s %d %x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
840 dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
842 if(ParseAction == PARSE_SECURITY_URL) {
843 WCHAR file_part[MAX_PATH], full_path[MAX_PATH];
844 WCHAR *ptr;
845 DWORD size, len;
847 static const WCHAR wszFile[] = {'f','i','l','e',':','/','/'};
848 static const WCHAR wszRes[] = {'r','e','s',':','/','/'};
850 if(strlenW(pwzUrl) <= sizeof(wszRes)/sizeof(WCHAR) || memcmp(pwzUrl, wszRes, sizeof(wszRes)))
851 return E_INVALIDARG;
853 ptr = strchrW(pwzUrl + sizeof(wszRes)/sizeof(WCHAR), '/');
854 if(!ptr)
855 return E_INVALIDARG;
857 len = ptr - (pwzUrl + sizeof(wszRes)/sizeof(WCHAR));
858 if(len >= sizeof(file_part)/sizeof(WCHAR)) {
859 FIXME("Too long URL\n");
860 return MK_E_SYNTAX;
863 memcpy(file_part, pwzUrl + sizeof(wszRes)/sizeof(WCHAR), len*sizeof(WCHAR));
864 file_part[len] = 0;
866 len = SearchPathW(NULL, file_part, NULL, sizeof(full_path)/sizeof(WCHAR), full_path, NULL);
867 if(!len) {
868 HMODULE module;
870 /* SearchPath does not work well with winelib files (like our test executable),
871 * so we also try to load the library here */
872 module = LoadLibraryExW(file_part, NULL, LOAD_LIBRARY_AS_DATAFILE);
873 if(!module) {
874 WARN("Could not find file %s\n", debugstr_w(file_part));
875 return MK_E_SYNTAX;
878 len = GetModuleFileNameW(module, full_path, sizeof(full_path)/sizeof(WCHAR));
879 FreeLibrary(module);
880 if(!len)
881 return E_FAIL;
884 size = sizeof(wszFile)/sizeof(WCHAR) + len + 1;
885 if(pcchResult)
886 *pcchResult = size;
887 if(size > cchResult)
888 return S_FALSE;
890 memcpy(pwzResult, wszFile, sizeof(wszFile));
891 memcpy(pwzResult + sizeof(wszFile)/sizeof(WCHAR), full_path, (len+1)*sizeof(WCHAR));
892 return S_OK;
895 if(ParseAction == PARSE_DOMAIN) {
896 if(!pcchResult)
897 return E_POINTER;
899 if(pwzUrl)
900 *pcchResult = strlenW(pwzUrl)+1;
901 else
902 *pcchResult = 1;
903 return E_FAIL;
906 return INET_E_DEFAULT_ACTION;
909 static HRESULT WINAPI ResProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
910 QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
911 DWORD dwReserved)
913 TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
914 cbBuffer, pcbBuf, dwReserved);
916 switch(QueryOption) {
917 case QUERY_USES_NETWORK:
918 if(!pBuffer || cbBuffer < sizeof(DWORD))
919 return E_FAIL;
921 *(DWORD*)pBuffer = 0;
922 if(pcbBuf)
923 *pcbBuf = sizeof(DWORD);
924 break;
926 case QUERY_IS_SECURE:
927 FIXME("not supporte QUERY_IS_SECURE\n");
928 return E_NOTIMPL;
929 case QUERY_IS_SAFE:
930 FIXME("not supporte QUERY_IS_SAFE\n");
931 return E_NOTIMPL;
932 default:
933 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
936 return S_OK;
939 static const IInternetProtocolInfoVtbl ResProtocolInfoVtbl = {
940 InternetProtocolInfo_QueryInterface,
941 InternetProtocolInfo_AddRef,
942 InternetProtocolInfo_Release,
943 ResProtocolInfo_ParseUrl,
944 InternetProtocolInfo_CombineUrl,
945 InternetProtocolInfo_CompareUrl,
946 ResProtocolInfo_QueryInfo
949 static const IClassFactoryVtbl ResProtocolFactoryVtbl = {
950 ClassFactory_QueryInterface,
951 ClassFactory_AddRef,
952 ClassFactory_Release,
953 ResProtocolFactory_CreateInstance,
954 ClassFactory_LockServer
957 static ProtocolFactory ResProtocolFactory = {
958 { &ResProtocolInfoVtbl },
959 { &ResProtocolFactoryVtbl }
962 /********************************************************************
963 * JSProtocol implementation
966 static HRESULT WINAPI JSProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
967 REFIID riid, void **ppv)
969 FIXME("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
970 return E_NOTIMPL;
973 static HRESULT WINAPI JSProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
974 PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
975 DWORD* pcchResult, DWORD dwReserved)
977 TRACE("%p)->(%s %d %x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
978 dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
980 switch(ParseAction) {
981 case PARSE_SECURITY_URL:
982 FIXME("PARSE_SECURITY_URL\n");
983 return E_NOTIMPL;
984 case PARSE_DOMAIN:
985 FIXME("PARSE_DOMAIN\n");
986 return E_NOTIMPL;
987 default:
988 return INET_E_DEFAULT_ACTION;
991 return S_OK;
994 static HRESULT WINAPI JSProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
995 QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
996 DWORD dwReserved)
998 TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
999 cbBuffer, pcbBuf, dwReserved);
1001 switch(QueryOption) {
1002 case QUERY_USES_NETWORK:
1003 if(!pBuffer || cbBuffer < sizeof(DWORD))
1004 return E_FAIL;
1006 *(DWORD*)pBuffer = 0;
1007 if(pcbBuf)
1008 *pcbBuf = sizeof(DWORD);
1009 break;
1011 case QUERY_IS_SECURE:
1012 FIXME("not supporte QUERY_IS_SECURE\n");
1013 return E_NOTIMPL;
1015 default:
1016 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
1019 return S_OK;
1022 static const IInternetProtocolInfoVtbl JSProtocolInfoVtbl = {
1023 InternetProtocolInfo_QueryInterface,
1024 InternetProtocolInfo_AddRef,
1025 InternetProtocolInfo_Release,
1026 JSProtocolInfo_ParseUrl,
1027 InternetProtocolInfo_CombineUrl,
1028 InternetProtocolInfo_CompareUrl,
1029 JSProtocolInfo_QueryInfo
1032 static const IClassFactoryVtbl JSProtocolFactoryVtbl = {
1033 ClassFactory_QueryInterface,
1034 ClassFactory_AddRef,
1035 ClassFactory_Release,
1036 JSProtocolFactory_CreateInstance,
1037 ClassFactory_LockServer
1040 static ProtocolFactory JSProtocolFactory = {
1041 { &JSProtocolInfoVtbl },
1042 { &JSProtocolFactoryVtbl }
1045 HRESULT ProtocolFactory_Create(REFCLSID rclsid, REFIID riid, void **ppv)
1047 ProtocolFactory *cf = NULL;
1049 if(IsEqualGUID(&CLSID_AboutProtocol, rclsid))
1050 cf = &AboutProtocolFactory;
1051 else if(IsEqualGUID(&CLSID_ResProtocol, rclsid))
1052 cf = &ResProtocolFactory;
1053 else if(IsEqualGUID(&CLSID_JSProtocol, rclsid))
1054 cf = &JSProtocolFactory;
1056 if(!cf) {
1057 FIXME("not implemented protocol %s\n", debugstr_guid(rclsid));
1058 return CLASS_E_CLASSNOTAVAILABLE;
1061 return IInternetProtocolInfo_QueryInterface(&cf->IInternetProtocolInfo_iface, riid, ppv);