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