wintrust: Don't assume dwStateAction is set in WINTRUST_DATA.
[wine] / dlls / mshtml / protocol.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 "config.h"
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "ole2.h"
30
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33
34 #include "mshtml_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37
38 /********************************************************************
39  * common ProtocolFactory implementation
40  */
41
42 #define PROTOCOLINFO(x) ((IInternetProtocolInfo*) &(x)->lpInternetProtocolInfoVtbl)
43 #define CLASSFACTORY(x) ((IClassFactory*)         &(x)->lpClassFactoryVtbl)
44 #define PROTOCOL(x)     ((IInternetProtocol*)     &(x)->lpInternetProtocolVtbl)
45
46 typedef struct {
47     const IInternetProtocolInfoVtbl *lpInternetProtocolInfoVtbl;
48     const IClassFactoryVtbl         *lpClassFactoryVtbl;
49 } ProtocolFactory;
50
51 #define PROTOCOLINFO_THIS(iface) DEFINE_THIS(ProtocolFactory, InternetProtocolInfo, iface)
52
53 static HRESULT WINAPI InternetProtocolInfo_QueryInterface(IInternetProtocolInfo *iface, REFIID riid, void **ppv)
54 {
55     ProtocolFactory *This = PROTOCOLINFO_THIS(iface);
56
57     *ppv = NULL;
58     if(IsEqualGUID(&IID_IUnknown, riid)) {
59         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
60         *ppv = PROTOCOLINFO(This);
61     }else if(IsEqualGUID(&IID_IInternetProtocolInfo, riid)) {
62         TRACE("(%p)->(IID_IInternetProtocolInfo %p)\n", This, ppv);
63         *ppv = PROTOCOLINFO(This);
64     }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
65         TRACE("(%p)->(IID_IClassFactory %p)\n", This, ppv);
66         *ppv = CLASSFACTORY(This);
67     }
68
69     if(!*ppv) {
70         WARN("unknown interface %s\n", debugstr_guid(riid));
71         return E_NOINTERFACE;
72     }
73
74     IInternetProtocolInfo_AddRef(iface);
75     return S_OK;
76 }
77
78 static ULONG WINAPI InternetProtocolInfo_AddRef(IInternetProtocolInfo *iface)
79 {
80     ProtocolFactory *This = PROTOCOLINFO_THIS(iface);
81     TRACE("(%p)\n", This);
82     LOCK_MODULE();
83     return 2;
84 }
85
86 static ULONG WINAPI InternetProtocolInfo_Release(IInternetProtocolInfo *iface)
87 {
88     ProtocolFactory *This = PROTOCOLINFO_THIS(iface);
89     TRACE("(%p)\n", This);
90     UNLOCK_MODULE();
91     return 1;
92 }
93
94 static HRESULT WINAPI InternetProtocolInfo_CombineUrl(IInternetProtocolInfo *iface,
95         LPCWSTR pwzBaseUrl, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags, LPWSTR pwzResult,
96         DWORD cchResult, DWORD* pcchResult, DWORD dwReserved)
97 {
98     TRACE("%p)->(%s %s %08x %p %d %p %d)\n", iface, debugstr_w(pwzBaseUrl),
99             debugstr_w(pwzRelativeUrl), dwCombineFlags, pwzResult, cchResult,
100             pcchResult, dwReserved);
101
102     return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
103 }
104
105 static HRESULT WINAPI InternetProtocolInfo_CompareUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl1,
106         LPCWSTR pwzUrl2, DWORD dwCompareFlags)
107 {
108     TRACE("%p)->(%s %s %08x)\n", iface, debugstr_w(pwzUrl1), debugstr_w(pwzUrl2), dwCompareFlags);
109     return E_NOTIMPL;
110 }
111
112 #undef PROTOCOLINFO_THIS
113
114 #define CLASSFACTORY_THIS(iface) DEFINE_THIS(ProtocolFactory, ClassFactory, iface)
115
116 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
117 {
118     ProtocolFactory *This = CLASSFACTORY_THIS(iface);
119     return IInternetProtocolInfo_QueryInterface(PROTOCOLINFO(This), riid, ppv);
120 }
121
122 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
123 {
124     ProtocolFactory *This = CLASSFACTORY_THIS(iface);
125     return IInternetProtocolInfo_AddRef(PROTOCOLINFO(This));
126 }
127
128 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
129 {
130     ProtocolFactory *This = CLASSFACTORY_THIS(iface);
131     return IInternetProtocolInfo_Release(PROTOCOLINFO(This));
132 }
133
134 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
135 {
136     ProtocolFactory *This = CLASSFACTORY_THIS(iface);
137
138     TRACE("(%p)->(%x)\n", This, dolock);
139
140     if(dolock)
141         LOCK_MODULE();
142     else
143         UNLOCK_MODULE();
144
145     return S_OK;
146 }
147
148 #undef CLASSFACTORY_THIS
149
150 /********************************************************************
151  * AboutProtocol implementation
152  */
153
154 typedef struct {
155     const IInternetProtocolVtbl *lpInternetProtocolVtbl;
156
157     LONG ref;
158
159     BYTE *data;
160     ULONG data_len;
161     ULONG cur;
162
163     IUnknown *pUnkOuter;
164 } AboutProtocol;
165
166 #define PROTOCOL_THIS(iface) DEFINE_THIS(AboutProtocol, InternetProtocol, iface)
167
168 static HRESULT WINAPI AboutProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
169 {
170     AboutProtocol *This = PROTOCOL_THIS(iface);
171
172     *ppv = NULL;
173
174     if(IsEqualGUID(&IID_IUnknown, riid)) {
175         TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
176         if(This->pUnkOuter)
177             return IUnknown_QueryInterface(This->pUnkOuter, riid, ppv);
178         *ppv = PROTOCOL(This);
179     }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
180         TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", iface, ppv);
181         *ppv = PROTOCOL(This);
182     }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
183         TRACE("(%p)->(IID_IInternetProtocol %p)\n", iface, ppv);
184         *ppv = PROTOCOL(This);
185     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
186         FIXME("IServiceProvider is not implemented\n");
187         return E_NOINTERFACE;
188     }
189
190     if(!*ppv) {
191         TRACE("unknown interface %s\n", debugstr_guid(riid));
192         return E_NOINTERFACE;
193     }
194
195     IInternetProtocol_AddRef(iface);
196     return S_OK;
197 }
198
199 static ULONG WINAPI AboutProtocol_AddRef(IInternetProtocol *iface)
200 {
201     AboutProtocol *This = PROTOCOL_THIS(iface);
202     ULONG ref = InterlockedIncrement(&This->ref);
203     TRACE("(%p) ref=%d\n", iface, ref);
204     return This->pUnkOuter ? IUnknown_AddRef(This->pUnkOuter) : ref;
205 }
206
207 static ULONG WINAPI AboutProtocol_Release(IInternetProtocol *iface)
208 {
209     AboutProtocol *This = PROTOCOL_THIS(iface);
210     IUnknown *pUnkOuter = This->pUnkOuter;
211     ULONG ref = InterlockedDecrement(&This->ref);
212
213     TRACE("(%p) ref=%x\n", iface, ref);
214
215     if(!ref) {
216         heap_free(This->data);
217         heap_free(This);
218         UNLOCK_MODULE();
219     }
220
221     return pUnkOuter ? IUnknown_Release(pUnkOuter) : ref;
222 }
223
224 static HRESULT WINAPI AboutProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
225         IInternetProtocolSink* pOIProtSink, IInternetBindInfo* pOIBindInfo,
226         DWORD grfPI, DWORD dwReserved)
227 {
228     AboutProtocol *This = PROTOCOL_THIS(iface);
229     BINDINFO bindinfo;
230     DWORD grfBINDF = 0;
231     LPCWSTR text = NULL;
232
233     static const WCHAR html_begin[] = {0xfeff,'<','H','T','M','L','>',0};
234     static const WCHAR html_end[] = {'<','/','H','T','M','L','>',0};
235     static const WCHAR wszBlank[] = {'b','l','a','n','k',0};
236     static const WCHAR wszAbout[] = {'a','b','o','u','t',':'};
237     static const WCHAR wszTextHtml[] = {'t','e','x','t','/','h','t','m','l',0};
238
239     /* NOTE:
240      * the about protocol seems not to work as I would expect. It creates html document
241      * for a given url, eg. about:some_text -> <HTML>some_text</HTML> except for the case when
242      * some_text = "blank", when document is blank (<HTML></HMTL>). The same happens
243      * when the url does not have "about:" in the beginning.
244      */
245
246     TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
247             pOIBindInfo, grfPI, dwReserved);
248
249     memset(&bindinfo, 0, sizeof(bindinfo));
250     bindinfo.cbSize = sizeof(BINDINFO);
251     IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
252     ReleaseBindInfo(&bindinfo);
253
254     TRACE("bindf %x\n", grfBINDF);
255
256     if(strlenW(szUrl)>=sizeof(wszAbout)/sizeof(WCHAR) && !memcmp(wszAbout, szUrl, sizeof(wszAbout))) {
257         text = szUrl + sizeof(wszAbout)/sizeof(WCHAR);
258         if(!strcmpW(wszBlank, text))
259             text = NULL;
260     }
261
262     This->data_len = sizeof(html_begin)+sizeof(html_end)-sizeof(WCHAR) 
263         + (text ? strlenW(text)*sizeof(WCHAR) : 0);
264     This->data = heap_alloc(This->data_len);
265
266     memcpy(This->data, html_begin, sizeof(html_begin));
267     if(text)
268         strcatW((LPWSTR)This->data, text);
269     strcatW((LPWSTR)This->data, html_end);
270     
271     This->cur = 0;
272
273     IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, wszTextHtml);
274
275     IInternetProtocolSink_ReportData(pOIProtSink,
276             BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE,
277             This->data_len, This->data_len);
278
279     IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
280
281     return S_OK;
282 }
283
284 static HRESULT WINAPI AboutProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA* pProtocolData)
285 {
286     AboutProtocol *This = PROTOCOL_THIS(iface);
287     FIXME("(%p)->(%p)\n", This, pProtocolData);
288     return E_NOTIMPL;
289 }
290
291 static HRESULT WINAPI AboutProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
292         DWORD dwOptions)
293 {
294     AboutProtocol *This = PROTOCOL_THIS(iface);
295     FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
296     return E_NOTIMPL;
297 }
298
299 static HRESULT WINAPI AboutProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
300 {
301     AboutProtocol *This = PROTOCOL_THIS(iface);
302     TRACE("(%p)->(%08x)\n", This, dwOptions);
303     return S_OK;
304 }
305
306 static HRESULT WINAPI AboutProtocol_Suspend(IInternetProtocol *iface)
307 {
308     AboutProtocol *This = PROTOCOL_THIS(iface);
309     FIXME("(%p)\n", This);
310     return E_NOTIMPL;
311 }
312
313 static HRESULT WINAPI AboutProtocol_Resume(IInternetProtocol *iface)
314 {
315     AboutProtocol *This = PROTOCOL_THIS(iface);
316     FIXME("(%p)\n", This);
317     return E_NOTIMPL;
318 }
319
320 static HRESULT WINAPI AboutProtocol_Read(IInternetProtocol *iface, void* pv, ULONG cb, ULONG* pcbRead)
321 {
322     AboutProtocol *This = PROTOCOL_THIS(iface);
323
324     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
325
326     if(!This->data)
327         return E_FAIL;
328
329     *pcbRead = (cb > This->data_len-This->cur ? This->data_len-This->cur : cb);
330
331     if(!*pcbRead)
332         return S_FALSE;
333
334     memcpy(pv, This->data+This->cur, *pcbRead);
335     This->cur += *pcbRead;
336
337     return S_OK;
338 }
339
340 static HRESULT WINAPI AboutProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
341         DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
342 {
343     AboutProtocol *This = PROTOCOL_THIS(iface);
344     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
345     return E_NOTIMPL;
346 }
347
348 static HRESULT WINAPI AboutProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
349 {
350     AboutProtocol *This = PROTOCOL_THIS(iface);
351
352     TRACE("(%p)->(%d)\n", This, dwOptions);
353
354     return S_OK;
355 }
356
357 static HRESULT WINAPI AboutProtocol_UnlockRequest(IInternetProtocol *iface)
358 {
359     AboutProtocol *This = PROTOCOL_THIS(iface);
360
361     TRACE("(%p)\n", This);
362
363     return S_OK;
364 }
365
366 #undef PROTOCOL_THIS
367
368 static const IInternetProtocolVtbl AboutProtocolVtbl = {
369     AboutProtocol_QueryInterface,
370     AboutProtocol_AddRef,
371     AboutProtocol_Release,
372     AboutProtocol_Start,
373     AboutProtocol_Continue,
374     AboutProtocol_Abort,
375     AboutProtocol_Terminate,
376     AboutProtocol_Suspend,
377     AboutProtocol_Resume,
378     AboutProtocol_Read,
379     AboutProtocol_Seek,
380     AboutProtocol_LockRequest,
381     AboutProtocol_UnlockRequest
382 };
383
384 static HRESULT WINAPI AboutProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
385         REFIID riid, void **ppv)
386 {
387     AboutProtocol *ret;
388     HRESULT hres = S_OK;
389
390     TRACE("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
391
392     ret = heap_alloc(sizeof(AboutProtocol));
393     ret->lpInternetProtocolVtbl = &AboutProtocolVtbl;
394     ret->ref = 0;
395
396     ret->data = NULL;
397     ret->data_len = 0;
398     ret->cur = 0;
399     ret->pUnkOuter = pUnkOuter;
400
401     if(pUnkOuter) {
402         ret->ref = 1;
403         if(IsEqualGUID(&IID_IUnknown, riid))
404             *ppv = PROTOCOL(ret);
405         else
406             hres = E_INVALIDARG;
407     }else {
408         hres = IInternetProtocol_QueryInterface(PROTOCOL(ret), riid, ppv);
409     }
410
411     if(SUCCEEDED(hres))
412         LOCK_MODULE();
413     else
414         heap_free(ret);
415
416     return hres;
417 }
418
419 static HRESULT WINAPI AboutProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
420         PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
421         DWORD* pcchResult, DWORD dwReserved)
422 {
423     TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
424             dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
425
426     if(ParseAction == PARSE_SECURITY_URL) {
427         int len = lstrlenW(pwzUrl);
428
429         if(len >= cchResult)
430             return S_FALSE;
431
432         memcpy(pwzResult, pwzUrl, (len+1)*sizeof(WCHAR));
433         return S_OK;
434     }
435
436     if(ParseAction == PARSE_DOMAIN) {
437         if(!pcchResult)
438             return E_POINTER;
439
440         if(pwzUrl)
441             *pcchResult = strlenW(pwzUrl)+1;
442         else
443             *pcchResult = 1;
444         return E_FAIL;
445     }
446
447     return INET_E_DEFAULT_ACTION;
448 }
449
450 static HRESULT WINAPI AboutProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
451         QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
452         DWORD dwReserved)
453 {
454     TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
455           cbBuffer, pcbBuf, dwReserved);
456
457     switch(QueryOption) {
458     case QUERY_CAN_NAVIGATE:
459         return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
460
461     case QUERY_USES_NETWORK:
462         if(!pBuffer || cbBuffer < sizeof(DWORD))
463             return E_FAIL;
464
465         *(DWORD*)pBuffer = 0;
466         if(pcbBuf)
467             *pcbBuf = sizeof(DWORD);
468
469         break;
470
471     case QUERY_IS_CACHED:
472         FIXME("Unsupported option QUERY_IS_CACHED\n");
473         return E_NOTIMPL;
474     case QUERY_IS_INSTALLEDENTRY:
475         FIXME("Unsupported option QUERY_IS_INSTALLEDENTRY\n");
476         return E_NOTIMPL;
477     case QUERY_IS_CACHED_OR_MAPPED:
478         FIXME("Unsupported option QUERY_IS_CACHED_OR_MAPPED\n");
479         return E_NOTIMPL;
480     case QUERY_IS_SECURE:
481         FIXME("Unsupported option QUERY_IS_SECURE\n");
482         return E_NOTIMPL;
483     case QUERY_IS_SAFE:
484         FIXME("Unsupported option QUERY_IS_SAFE\n");
485         return E_NOTIMPL;
486     case QUERY_USES_HISTORYFOLDER:
487         FIXME("Unsupported option QUERY_USES_HISTORYFOLDER\n");
488         return E_FAIL;
489     default:
490         return E_FAIL;
491     }
492
493     return S_OK;
494 }
495
496 static const IInternetProtocolInfoVtbl AboutProtocolInfoVtbl = {
497     InternetProtocolInfo_QueryInterface,
498     InternetProtocolInfo_AddRef,
499     InternetProtocolInfo_Release,
500     AboutProtocolInfo_ParseUrl,
501     InternetProtocolInfo_CombineUrl,
502     InternetProtocolInfo_CompareUrl,
503     AboutProtocolInfo_QueryInfo
504 };
505
506 static const IClassFactoryVtbl AboutProtocolFactoryVtbl = {
507     ClassFactory_QueryInterface,
508     ClassFactory_AddRef,
509     ClassFactory_Release,
510     AboutProtocolFactory_CreateInstance,
511     ClassFactory_LockServer
512 };
513
514 static ProtocolFactory AboutProtocolFactory = {
515     &AboutProtocolInfoVtbl,
516     &AboutProtocolFactoryVtbl
517 };
518
519 /********************************************************************
520  * ResProtocol implementation
521  */
522
523 typedef struct {
524     const IInternetProtocolVtbl *lpInternetProtocolVtbl;
525     LONG ref;
526
527     BYTE *data;
528     ULONG data_len;
529     ULONG cur;
530
531     IUnknown *pUnkOuter;
532 } ResProtocol;
533
534 #define PROTOCOL_THIS(iface) DEFINE_THIS(ResProtocol, InternetProtocol, iface)
535
536 static HRESULT WINAPI ResProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
537 {
538     ResProtocol *This = PROTOCOL_THIS(iface);
539
540     *ppv = NULL;
541
542     if(IsEqualGUID(&IID_IUnknown, riid)) {
543         TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
544         if(This->pUnkOuter)
545             return IUnknown_QueryInterface(This->pUnkOuter, &IID_IUnknown, ppv);
546         *ppv = PROTOCOL(This);
547     }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
548         TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", iface, ppv);
549         *ppv = PROTOCOL(This);
550     }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
551         TRACE("(%p)->(IID_IInternetProtocol %p)\n", iface, ppv);
552         *ppv = PROTOCOL(This);
553     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
554         FIXME("IServiceProvider is not implemented\n");
555         return E_NOINTERFACE;
556     }
557
558     if(!*ppv) {
559         TRACE("unknown interface %s\n", debugstr_guid(riid));
560         return E_NOINTERFACE;
561     }
562
563     IInternetProtocol_AddRef(iface);
564     return S_OK;
565 }
566
567 static ULONG WINAPI ResProtocol_AddRef(IInternetProtocol *iface)
568 {
569     ResProtocol *This = PROTOCOL_THIS(iface);
570     ULONG ref = InterlockedIncrement(&This->ref);
571     TRACE("(%p) ref=%d\n", iface, ref);
572     return This->pUnkOuter ? IUnknown_AddRef(This->pUnkOuter) : ref;
573 }
574
575 static ULONG WINAPI ResProtocol_Release(IInternetProtocol *iface)
576 {
577     ResProtocol *This = (ResProtocol*)iface;
578     IUnknown *pUnkOuter = This->pUnkOuter;
579     ULONG ref = InterlockedDecrement(&This->ref);
580
581     TRACE("(%p) ref=%x\n", iface, ref);
582
583     if(!ref) {
584         heap_free(This->data);
585         heap_free(This);
586         UNLOCK_MODULE();
587     }
588
589     return pUnkOuter ? IUnknown_Release(pUnkOuter) : ref;
590 }
591
592 static HRESULT WINAPI ResProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
593         IInternetProtocolSink* pOIProtSink, IInternetBindInfo* pOIBindInfo,
594         DWORD grfPI, DWORD dwReserved)
595 {
596     ResProtocol *This = PROTOCOL_THIS(iface);
597     DWORD grfBINDF = 0, len;
598     BINDINFO bindinfo;
599     LPWSTR url_dll, url_file, url, mime, res_type = (LPWSTR)RT_HTML;
600     HMODULE hdll;
601     HRSRC src;
602     HRESULT hres;
603
604     static const WCHAR wszRes[] = {'r','e','s',':','/','/'};
605
606     TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
607             pOIBindInfo, grfPI, dwReserved);
608
609     memset(&bindinfo, 0, sizeof(bindinfo));
610     bindinfo.cbSize = sizeof(BINDINFO);
611     IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
612     ReleaseBindInfo(&bindinfo);
613
614     len = strlenW(szUrl)+16;
615     url = heap_alloc(len*sizeof(WCHAR));
616     hres = CoInternetParseUrl(szUrl, PARSE_ENCODE, 0, url, len, &len, 0);
617     if(FAILED(hres)) {
618         WARN("CoInternetParseUrl failed: %08x\n", hres);
619         heap_free(url);
620         IInternetProtocolSink_ReportResult(pOIProtSink, hres, 0, NULL);
621         return hres;
622     }
623
624     if(len < sizeof(wszRes)/sizeof(wszRes[0]) || memcmp(url, wszRes, sizeof(wszRes))) {
625         WARN("Wrong protocol of url: %s\n", debugstr_w(url));
626         IInternetProtocolSink_ReportResult(pOIProtSink, E_INVALIDARG, 0, NULL);
627         heap_free(url);
628         return E_INVALIDARG;
629     }
630
631     url_dll = url + sizeof(wszRes)/sizeof(wszRes[0]);
632     if(!(url_file = strrchrW(url_dll, '/'))) {
633         WARN("wrong url: %s\n", debugstr_w(url));
634         IInternetProtocolSink_ReportResult(pOIProtSink, MK_E_SYNTAX, 0, NULL);
635         heap_free(url);
636         return MK_E_SYNTAX;
637     }
638
639     *url_file++ = 0;
640     hdll = LoadLibraryExW(url_dll, NULL, LOAD_LIBRARY_AS_DATAFILE);
641     if(!hdll) {
642         if (!(res_type = strrchrW(url_dll, '/'))) {
643             WARN("Could not open dll: %s\n", debugstr_w(url_dll));
644             IInternetProtocolSink_ReportResult(pOIProtSink, HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
645             heap_free(url);
646             return HRESULT_FROM_WIN32(GetLastError());
647         }
648         *res_type++ = 0;
649
650         hdll = LoadLibraryExW(url_dll, NULL, LOAD_LIBRARY_AS_DATAFILE);
651         if(!hdll) {
652             WARN("Could not open dll: %s\n", debugstr_w(url_dll));
653             IInternetProtocolSink_ReportResult(pOIProtSink, HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
654             heap_free(url);
655             return HRESULT_FROM_WIN32(GetLastError());
656         }
657     }
658
659     TRACE("trying to find resource type %s, name %s\n", debugstr_w(res_type), debugstr_w(url_file));
660
661     src = FindResourceW(hdll, url_file, res_type);
662     if(!src) {
663         LPWSTR endpoint = NULL;
664         DWORD file_id = strtolW(url_file, &endpoint, 10);
665         if(endpoint == url_file+strlenW(url_file))
666             src = FindResourceW(hdll, (LPCWSTR)file_id, (LPCWSTR)RT_HTML);
667
668         if(!src) {
669             WARN("Could not find resource\n");
670             IInternetProtocolSink_ReportResult(pOIProtSink,
671                     HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
672             heap_free(url);
673             return HRESULT_FROM_WIN32(GetLastError());
674         }
675     }
676
677     if(This->data) {
678         WARN("data already loaded\n");
679         heap_free(This->data);
680     }
681
682     This->data_len = SizeofResource(hdll, src);
683     This->data = heap_alloc(This->data_len);
684     memcpy(This->data, LoadResource(hdll, src), This->data_len);
685     This->cur = 0;
686
687     FreeLibrary(hdll);
688
689     hres = FindMimeFromData(NULL, url_file, NULL, 0, NULL, 0, &mime, 0);
690     heap_free(url);
691     if(SUCCEEDED(hres)) {
692         IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
693         CoTaskMemFree(mime);
694     }
695
696     IInternetProtocolSink_ReportData(pOIProtSink,
697             BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE,
698             This->data_len, This->data_len);
699
700     IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
701     
702     return S_OK;
703 }
704
705 static HRESULT WINAPI ResProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA* pProtocolData)
706 {
707     ResProtocol *This = PROTOCOL_THIS(iface);
708     FIXME("(%p)->(%p)\n", This, pProtocolData);
709     return E_NOTIMPL;
710 }
711
712 static HRESULT WINAPI ResProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
713         DWORD dwOptions)
714 {
715     ResProtocol *This = PROTOCOL_THIS(iface);
716     FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
717     return E_NOTIMPL;
718 }
719
720 static HRESULT WINAPI ResProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
721 {
722     ResProtocol *This = PROTOCOL_THIS(iface);
723
724     TRACE("(%p)->(%08x)\n", This, dwOptions);
725
726     /* test show that we don't have to do anything here */
727     return S_OK;
728 }
729
730 static HRESULT WINAPI ResProtocol_Suspend(IInternetProtocol *iface)
731 {
732     ResProtocol *This = PROTOCOL_THIS(iface);
733     FIXME("(%p)\n", This);
734     return E_NOTIMPL;
735 }
736
737 static HRESULT WINAPI ResProtocol_Resume(IInternetProtocol *iface)
738 {
739     ResProtocol *This = PROTOCOL_THIS(iface);
740     FIXME("(%p)\n", This);
741     return E_NOTIMPL;
742 }
743
744 static HRESULT WINAPI ResProtocol_Read(IInternetProtocol *iface, void* pv, ULONG cb, ULONG* pcbRead)
745 {
746     ResProtocol *This = PROTOCOL_THIS(iface);
747
748     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
749
750     if(!This->data)
751         return E_FAIL;
752
753     *pcbRead = (cb > This->data_len-This->cur ? This->data_len-This->cur : cb);
754
755     if(!*pcbRead)
756         return S_FALSE;
757
758     memcpy(pv, This->data+This->cur, *pcbRead);
759     This->cur += *pcbRead;
760
761     return S_OK;
762 }
763
764 static HRESULT WINAPI ResProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
765         DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
766 {
767     ResProtocol *This = PROTOCOL_THIS(iface);
768     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
769     return E_NOTIMPL;
770 }
771
772 static HRESULT WINAPI ResProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
773 {
774     ResProtocol *This = PROTOCOL_THIS(iface);
775
776     TRACE("(%p)->(%d)\n", This, dwOptions);
777
778     /* test show that we don't have to do anything here */
779     return S_OK;
780 }
781
782 static HRESULT WINAPI ResProtocol_UnlockRequest(IInternetProtocol *iface)
783 {
784     ResProtocol *This = PROTOCOL_THIS(iface);
785
786     TRACE("(%p)\n", This);
787
788     /* test show that we don't have to do anything here */
789     return S_OK;
790 }
791
792 #undef PROTOCOL_THIS
793
794 static const IInternetProtocolVtbl ResProtocolVtbl = {
795     ResProtocol_QueryInterface,
796     ResProtocol_AddRef,
797     ResProtocol_Release,
798     ResProtocol_Start,
799     ResProtocol_Continue,
800     ResProtocol_Abort,
801     ResProtocol_Terminate,
802     ResProtocol_Suspend,
803     ResProtocol_Resume,
804     ResProtocol_Read,
805     ResProtocol_Seek,
806     ResProtocol_LockRequest,
807     ResProtocol_UnlockRequest
808 };
809
810 static HRESULT WINAPI ResProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
811         REFIID riid, void **ppv)
812 {
813     ResProtocol *ret;
814     HRESULT hres = S_OK;
815
816     TRACE("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
817
818     ret = heap_alloc(sizeof(ResProtocol));
819     ret->lpInternetProtocolVtbl = &ResProtocolVtbl;
820     ret->ref = 0;
821     ret->data = NULL;
822     ret->data_len = 0;
823     ret->cur = 0;
824     ret->pUnkOuter = pUnkOuter;
825
826     if(pUnkOuter) {
827         ret->ref = 1;
828         if(IsEqualGUID(&IID_IUnknown, riid))
829             *ppv = PROTOCOL(ret);
830         else
831             hres = E_FAIL;
832     }else {
833         hres = IInternetProtocol_QueryInterface(PROTOCOL(ret), riid, ppv);
834     }
835
836     if(SUCCEEDED(hres))
837         LOCK_MODULE();
838     else
839         heap_free(ret);
840
841     return hres;
842 }
843
844 static HRESULT WINAPI ResProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
845         PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
846         DWORD* pcchResult, DWORD dwReserved)
847 {
848     TRACE("%p)->(%s %d %x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
849             dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
850
851     if(ParseAction == PARSE_SECURITY_URL) {
852         WCHAR file_part[MAX_PATH], full_path[MAX_PATH];
853         WCHAR *ptr;
854         DWORD size, len;
855
856         static const WCHAR wszFile[] = {'f','i','l','e',':','/','/'};
857         static const WCHAR wszRes[] = {'r','e','s',':','/','/'};
858
859         if(strlenW(pwzUrl) <= sizeof(wszRes)/sizeof(WCHAR) || memcmp(pwzUrl, wszRes, sizeof(wszRes)))
860             return E_INVALIDARG;
861
862         ptr = strchrW(pwzUrl + sizeof(wszRes)/sizeof(WCHAR), '/');
863         if(!ptr)
864             return E_INVALIDARG;
865
866         len = ptr - (pwzUrl + sizeof(wszRes)/sizeof(WCHAR));
867         if(len >= sizeof(file_part)/sizeof(WCHAR)) {
868             FIXME("Too long URL\n");
869             return MK_E_SYNTAX;
870         }
871
872         memcpy(file_part, pwzUrl + sizeof(wszRes)/sizeof(WCHAR), len*sizeof(WCHAR));
873         file_part[len] = 0;
874
875         len = SearchPathW(NULL, file_part, NULL, sizeof(full_path)/sizeof(WCHAR), full_path, NULL);
876         if(!len) {
877             WARN("Could not find file %s\n", debugstr_w(file_part));
878             return MK_E_SYNTAX;
879         }
880
881         size = sizeof(wszFile)/sizeof(WCHAR) + len + 1;
882         if(pcchResult)
883             *pcchResult = size;
884         if(size >= cchResult)
885             return S_FALSE;
886
887         memcpy(pwzResult, wszFile, sizeof(wszFile));
888         memcpy(pwzResult + sizeof(wszFile)/sizeof(WCHAR), full_path, (len+1)*sizeof(WCHAR));
889         return S_OK;
890     }
891
892     if(ParseAction == PARSE_DOMAIN) {
893         if(!pcchResult)
894             return E_POINTER;
895
896         if(pwzUrl)
897             *pcchResult = strlenW(pwzUrl)+1;
898         else
899             *pcchResult = 1;
900         return E_FAIL;
901     }
902
903     return INET_E_DEFAULT_ACTION;
904 }
905
906 static HRESULT WINAPI ResProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
907         QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
908         DWORD dwReserved)
909 {
910     TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
911           cbBuffer, pcbBuf, dwReserved);
912
913     switch(QueryOption) {
914     case QUERY_USES_NETWORK:
915         if(!pBuffer || cbBuffer < sizeof(DWORD))
916             return E_FAIL;
917
918         *(DWORD*)pBuffer = 0;
919         if(pcbBuf)
920             *pcbBuf = sizeof(DWORD);
921         break;
922
923     case QUERY_IS_SECURE:
924         FIXME("not supporte QUERY_IS_SECURE\n");
925         return E_NOTIMPL;
926     case QUERY_IS_SAFE:
927         FIXME("not supporte QUERY_IS_SAFE\n");
928         return E_NOTIMPL;
929     default:
930         return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
931     }
932
933     return S_OK;
934 }
935
936 static const IInternetProtocolInfoVtbl ResProtocolInfoVtbl = {
937     InternetProtocolInfo_QueryInterface,
938     InternetProtocolInfo_AddRef,
939     InternetProtocolInfo_Release,
940     ResProtocolInfo_ParseUrl,
941     InternetProtocolInfo_CombineUrl,
942     InternetProtocolInfo_CompareUrl,
943     ResProtocolInfo_QueryInfo
944 };
945
946 static const IClassFactoryVtbl ResProtocolFactoryVtbl = {
947     ClassFactory_QueryInterface,
948     ClassFactory_AddRef,
949     ClassFactory_Release,
950     ResProtocolFactory_CreateInstance,
951     ClassFactory_LockServer
952 };
953
954 static ProtocolFactory ResProtocolFactory = {
955     &ResProtocolInfoVtbl,
956     &ResProtocolFactoryVtbl
957 };
958
959 HRESULT ProtocolFactory_Create(REFCLSID rclsid, REFIID riid, void **ppv)
960 {
961     ProtocolFactory *cf = NULL;
962
963     if(IsEqualGUID(&CLSID_AboutProtocol, rclsid))
964         cf = &AboutProtocolFactory;
965     else if(IsEqualGUID(&CLSID_ResProtocol, rclsid))
966         cf = &ResProtocolFactory;
967
968     if(!cf) {
969         FIXME("not implemented protocol %s\n", debugstr_guid(rclsid));
970         return CLASS_E_CLASSNOTAVAILABLE;
971     }
972  
973     return IUnknown_QueryInterface((IUnknown*)cf, riid, ppv);
974 }