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