mshtml: Moved screen property to HTMLInnerWindow.
[wine] / dlls / urlmon / urlmon_main.c
1 /*
2  * UrlMon
3  *
4  * Copyright (c) 2000 Patrik Stridvall
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22
23 #include "urlmon_main.h"
24
25 #include "winreg.h"
26
27 #define NO_SHLWAPI_REG
28 #include "shlwapi.h"
29 #include "advpub.h"
30 #include "initguid.h"
31
32 #include "wine/debug.h"
33
34 #include "urlmon.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
37
38 DEFINE_GUID(CLSID_CUri, 0xDF2FCE13, 0x25EC, 0x45BB, 0x9D,0x4C, 0xCE,0xCD,0x47,0xC2,0x43,0x0C);
39
40 LONG URLMON_refCount = 0;
41
42 static HMODULE hCabinet = NULL;
43 static DWORD urlmon_tls = TLS_OUT_OF_INDEXES;
44
45 static void init_session(void);
46
47 static struct list tls_list = LIST_INIT(tls_list);
48
49 static CRITICAL_SECTION tls_cs;
50 static CRITICAL_SECTION_DEBUG tls_cs_dbg =
51 {
52     0, 0, &tls_cs,
53     { &tls_cs_dbg.ProcessLocksList, &tls_cs_dbg.ProcessLocksList },
54       0, 0, { (DWORD_PTR)(__FILE__ ": tls") }
55 };
56
57 static CRITICAL_SECTION tls_cs = { &tls_cs_dbg, -1, 0, 0, 0, 0 };
58
59 tls_data_t *get_tls_data(void)
60 {
61     tls_data_t *data;
62
63     if(urlmon_tls == TLS_OUT_OF_INDEXES) {
64         DWORD tls = TlsAlloc();
65         if(tls == TLS_OUT_OF_INDEXES)
66             return NULL;
67
68         tls = InterlockedCompareExchange((LONG*)&urlmon_tls, tls, TLS_OUT_OF_INDEXES);
69         if(tls != urlmon_tls)
70             TlsFree(tls);
71     }
72
73     data = TlsGetValue(urlmon_tls);
74     if(!data) {
75         data = heap_alloc_zero(sizeof(tls_data_t));
76         if(!data)
77             return NULL;
78
79         EnterCriticalSection(&tls_cs);
80         list_add_tail(&tls_list, &data->entry);
81         LeaveCriticalSection(&tls_cs);
82
83         TlsSetValue(urlmon_tls, data);
84     }
85
86     return data;
87 }
88
89 static void free_tls_list(void)
90 {
91     tls_data_t *data;
92
93     if(urlmon_tls == TLS_OUT_OF_INDEXES)
94         return;
95
96     while(!list_empty(&tls_list)) {
97         data = LIST_ENTRY(list_head(&tls_list), tls_data_t, entry);
98         list_remove(&data->entry);
99         heap_free(data);
100     }
101
102     TlsFree(urlmon_tls);
103 }
104
105 static void detach_thread(void)
106 {
107     tls_data_t *data;
108
109     if(urlmon_tls == TLS_OUT_OF_INDEXES)
110         return;
111
112     data = TlsGetValue(urlmon_tls);
113     if(!data)
114         return;
115
116     EnterCriticalSection(&tls_cs);
117     list_remove(&data->entry);
118     LeaveCriticalSection(&tls_cs);
119
120     if(data->notif_hwnd) {
121         WARN("notif_hwnd not destroyed\n");
122         DestroyWindow(data->notif_hwnd);
123     }
124
125     heap_free(data);
126 }
127
128 static void process_detach(void)
129 {
130     HINTERNET internet_session;
131
132     internet_session = get_internet_session(NULL);
133     if(internet_session)
134         InternetCloseHandle(internet_session);
135
136     if (hCabinet)
137         FreeLibrary(hCabinet);
138
139     free_session();
140     free_tls_list();
141 }
142
143 /***********************************************************************
144  *              DllMain (URLMON.init)
145  */
146 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
147 {
148     TRACE("%p 0x%x %p\n", hinstDLL, fdwReason, fImpLoad);
149
150     URLMON_DllMain( hinstDLL, fdwReason, fImpLoad );
151
152     switch(fdwReason) {
153     case DLL_PROCESS_ATTACH:
154         init_session();
155         break;
156
157     case DLL_PROCESS_DETACH:
158         process_detach();
159         DeleteCriticalSection(&tls_cs);
160         break;
161
162     case DLL_THREAD_DETACH:
163         detach_thread();
164         break;
165     }
166     return TRUE;
167 }
168
169
170 /***********************************************************************
171  *              DllInstall (URLMON.@)
172  */
173 HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
174 {
175   FIXME("(%s, %s): stub\n", bInstall?"TRUE":"FALSE",
176         debugstr_w(cmdline));
177
178   return S_OK;
179 }
180
181 /***********************************************************************
182  *              DllCanUnloadNow (URLMON.@)
183  */
184 HRESULT WINAPI DllCanUnloadNow(void)
185 {
186     return URLMON_refCount != 0 ? S_FALSE : S_OK;
187 }
188
189
190
191 /******************************************************************************
192  * Urlmon ClassFactory
193  */
194 typedef struct {
195     IClassFactory IClassFactory_iface;
196
197     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
198 } ClassFactory;
199
200 static inline ClassFactory *impl_from_IClassFactory(IClassFactory *iface)
201 {
202     return CONTAINING_RECORD(iface, ClassFactory, IClassFactory_iface);
203 }
204
205 static HRESULT WINAPI CF_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppv)
206 {
207     *ppv = NULL;
208
209     if(IsEqualGUID(riid, &IID_IUnknown)) {
210         TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
211         *ppv = iface;
212     }else if(IsEqualGUID(riid, &IID_IClassFactory)) {
213         TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
214         *ppv = iface;
215     }
216
217     if(*ppv) {
218         IUnknown_AddRef((IUnknown*)*ppv);
219         return S_OK;
220     }
221
222     WARN("(%p)->(%s,%p),not found\n", iface, debugstr_guid(riid), ppv);
223     return E_NOINTERFACE;
224 }
225
226 static ULONG WINAPI CF_AddRef(IClassFactory *iface)
227 {
228     URLMON_LockModule();
229     return 2;
230 }
231
232 static ULONG WINAPI CF_Release(IClassFactory *iface)
233 {
234     URLMON_UnlockModule();
235     return 1;
236 }
237
238
239 static HRESULT WINAPI CF_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
240                                         REFIID riid, LPVOID *ppobj)
241 {
242     ClassFactory *This = impl_from_IClassFactory(iface);
243     HRESULT hres;
244     LPUNKNOWN punk;
245     
246     TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
247
248     *ppobj = NULL;
249     if(SUCCEEDED(hres = This->pfnCreateInstance(pOuter, (LPVOID *) &punk))) {
250         hres = IUnknown_QueryInterface(punk, riid, ppobj);
251         IUnknown_Release(punk);
252     }
253     return hres;
254 }
255
256 static HRESULT WINAPI CF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
257 {
258     TRACE("(%d)\n", dolock);
259
260     if (dolock)
261            URLMON_LockModule();
262     else
263            URLMON_UnlockModule();
264
265     return S_OK;
266 }
267
268 static const IClassFactoryVtbl ClassFactoryVtbl =
269 {
270     CF_QueryInterface,
271     CF_AddRef,
272     CF_Release,
273     CF_CreateInstance,
274     CF_LockServer
275 };
276
277 static ClassFactory FileProtocolCF =
278     { { &ClassFactoryVtbl }, FileProtocol_Construct};
279 static ClassFactory FtpProtocolCF =
280     { { &ClassFactoryVtbl }, FtpProtocol_Construct};
281 static ClassFactory GopherProtocolCF =
282     { { &ClassFactoryVtbl }, GopherProtocol_Construct};
283 static ClassFactory HttpProtocolCF =
284     { { &ClassFactoryVtbl }, HttpProtocol_Construct};
285 static ClassFactory HttpSProtocolCF =
286     { { &ClassFactoryVtbl }, HttpSProtocol_Construct};
287 static ClassFactory MkProtocolCF =
288     { { &ClassFactoryVtbl }, MkProtocol_Construct};
289 static ClassFactory SecurityManagerCF =
290     { { &ClassFactoryVtbl }, SecManagerImpl_Construct};
291 static ClassFactory ZoneManagerCF =
292     { { &ClassFactoryVtbl }, ZoneMgrImpl_Construct};
293 static ClassFactory StdURLMonikerCF =
294     { { &ClassFactoryVtbl }, StdURLMoniker_Construct};
295 static ClassFactory MimeFilterCF =
296     { { &ClassFactoryVtbl }, MimeFilter_Construct};
297 static ClassFactory CUriCF =
298     { { &ClassFactoryVtbl }, Uri_Construct};
299
300 struct object_creation_info
301 {
302     const CLSID *clsid;
303     IClassFactory *cf;
304     LPCWSTR protocol;
305 };
306
307 static const WCHAR wszFile[] = {'f','i','l','e',0};
308 static const WCHAR wszFtp[]  = {'f','t','p',0};
309 static const WCHAR wszGopher[]  = {'g','o','p','h','e','r',0};
310 static const WCHAR wszHttp[] = {'h','t','t','p',0};
311 static const WCHAR wszHttps[] = {'h','t','t','p','s',0};
312 static const WCHAR wszMk[]   = {'m','k',0};
313
314 static const struct object_creation_info object_creation[] =
315 {
316     { &CLSID_FileProtocol,            &FileProtocolCF.IClassFactory_iface,    wszFile },
317     { &CLSID_FtpProtocol,             &FtpProtocolCF.IClassFactory_iface,     wszFtp  },
318     { &CLSID_GopherProtocol,          &GopherProtocolCF.IClassFactory_iface,  wszGopher },
319     { &CLSID_HttpProtocol,            &HttpProtocolCF.IClassFactory_iface,    wszHttp },
320     { &CLSID_HttpSProtocol,           &HttpSProtocolCF.IClassFactory_iface,   wszHttps },
321     { &CLSID_MkProtocol,              &MkProtocolCF.IClassFactory_iface,      wszMk },
322     { &CLSID_InternetSecurityManager, &SecurityManagerCF.IClassFactory_iface, NULL    },
323     { &CLSID_InternetZoneManager,     &ZoneManagerCF.IClassFactory_iface,     NULL    },
324     { &CLSID_StdURLMoniker,           &StdURLMonikerCF.IClassFactory_iface,   NULL    },
325     { &CLSID_DeCompMimeFilter,        &MimeFilterCF.IClassFactory_iface,      NULL    },
326     { &CLSID_CUri,                    &CUriCF.IClassFactory_iface,            NULL    }
327 };
328
329 static void init_session(void)
330 {
331     unsigned int i;
332
333     for(i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++) {
334         if(object_creation[i].protocol)
335             register_namespace(object_creation[i].cf, object_creation[i].clsid,
336                                       object_creation[i].protocol, TRUE);
337     }
338 }
339
340 /*******************************************************************************
341  * DllGetClassObject [URLMON.@]
342  * Retrieves class object from a DLL object
343  *
344  * NOTES
345  *    Docs say returns STDAPI
346  *
347  * PARAMS
348  *    rclsid [I] CLSID for the class object
349  *    riid   [I] Reference to identifier of interface for class object
350  *    ppv    [O] Address of variable to receive interface pointer for riid
351  *
352  * RETURNS
353  *    Success: S_OK
354  *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
355  *             E_UNEXPECTED
356  */
357
358 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
359 {
360     unsigned int i;
361     HRESULT hr;
362     
363     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
364     
365     for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
366     {
367         if (IsEqualGUID(object_creation[i].clsid, rclsid))
368             return IClassFactory_QueryInterface(object_creation[i].cf, riid, ppv);
369     }
370
371     hr = URLMON_DllGetClassObject(rclsid, riid, ppv);
372     if(SUCCEEDED(hr))
373         return hr;
374
375     FIXME("%s: no class found.\n", debugstr_guid(rclsid));
376     return CLASS_E_CLASSNOTAVAILABLE;
377 }
378
379 static HRESULT register_inf(BOOL doregister)
380 {
381     HRESULT (WINAPI *pRegInstall)(HMODULE hm, LPCSTR pszSection, const STRTABLEA* pstTable);
382     HMODULE hAdvpack;
383
384     static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
385
386     hAdvpack = LoadLibraryW(wszAdvpack);
387     pRegInstall = (void *)GetProcAddress(hAdvpack, "RegInstall");
388
389     return pRegInstall(hProxyDll, doregister ? "RegisterDll" : "UnregisterDll", NULL);
390 }
391
392 /***********************************************************************
393  *              DllRegisterServer (URLMON.@)
394  */
395 HRESULT WINAPI DllRegisterServer(void)
396 {
397     HRESULT hr;
398
399     TRACE("\n");
400
401     hr = URLMON_DllRegisterServer();
402     return SUCCEEDED(hr) ? register_inf(TRUE) : hr;
403 }
404
405 /***********************************************************************
406  *              DllUnregisterServer (URLMON.@)
407  */
408 HRESULT WINAPI DllUnregisterServer(void)
409 {
410     HRESULT hr;
411
412     TRACE("\n");
413
414     hr = URLMON_DllUnregisterServer();
415     return SUCCEEDED(hr) ? register_inf(FALSE) : hr;
416 }
417
418 /***********************************************************************
419  *              DllRegisterServerEx (URLMON.@)
420  */
421 HRESULT WINAPI DllRegisterServerEx(void)
422 {
423     FIXME("(void): stub\n");
424
425     return E_FAIL;
426 }
427
428 /**************************************************************************
429  *                 IsValidURL (URLMON.@)
430  * 
431  * Determines if a specified string is a valid URL.
432  *
433  * PARAMS
434  *  pBC        [I] ignored, should be NULL.
435  *  szURL      [I] string that represents the URL in question.
436  *  dwReserved [I] reserved and must be zero.
437  *
438  * RETURNS
439  *  Success: S_OK.
440  *  Failure: S_FALSE.
441  *  returns E_INVALIDARG if one or more of the args is invalid.
442  *
443  * TODO:
444  *  test functionality against windows to see what a valid URL is.
445  */
446 HRESULT WINAPI IsValidURL(LPBC pBC, LPCWSTR szURL, DWORD dwReserved)
447 {
448     FIXME("(%p, %s, %d): stub\n", pBC, debugstr_w(szURL), dwReserved);
449
450     if (dwReserved || !szURL)
451         return E_INVALIDARG;
452
453     return S_OK;
454 }
455
456 /**************************************************************************
457  *                 FaultInIEFeature (URLMON.@)
458  *
459  *  Undocumented.  Appears to be used by native shdocvw.dll.
460  */
461 HRESULT WINAPI FaultInIEFeature( HWND hwnd, uCLSSPEC * pClassSpec,
462                                  QUERYCONTEXT *pQuery, DWORD flags )
463 {
464     FIXME("%p %p %p %08x\n", hwnd, pClassSpec, pQuery, flags);
465     return E_NOTIMPL;
466 }
467
468 /**************************************************************************
469  *                 CoGetClassObjectFromURL (URLMON.@)
470  */
471 HRESULT WINAPI CoGetClassObjectFromURL( REFCLSID rclsid, LPCWSTR szCodeURL, DWORD dwFileVersionMS,
472                                         DWORD dwFileVersionLS, LPCWSTR szContentType,
473                                         LPBINDCTX pBindCtx, DWORD dwClsContext, LPVOID pvReserved,
474                                         REFIID riid, LPVOID *ppv )
475 {
476     FIXME("(%s %s %d %d %s %p %d %p %s %p) Stub!\n", debugstr_guid(rclsid), debugstr_w(szCodeURL),
477         dwFileVersionMS, dwFileVersionLS, debugstr_w(szContentType), pBindCtx, dwClsContext, pvReserved,
478         debugstr_guid(riid), ppv);
479     return E_NOINTERFACE;
480 }
481
482 /***********************************************************************
483  *           ReleaseBindInfo (URLMON.@)
484  *
485  * Release the resources used by the specified BINDINFO structure.
486  *
487  * PARAMS
488  *  pbindinfo [I] BINDINFO to release.
489  *
490  * RETURNS
491  *  Nothing.
492  */
493 void WINAPI ReleaseBindInfo(BINDINFO* pbindinfo)
494 {
495     DWORD size;
496
497     TRACE("(%p)\n", pbindinfo);
498
499     if(!pbindinfo || !(size = pbindinfo->cbSize))
500         return;
501
502     CoTaskMemFree(pbindinfo->szExtraInfo);
503     ReleaseStgMedium(&pbindinfo->stgmedData);
504
505     if(offsetof(BINDINFO, szExtraInfo) < size)
506         CoTaskMemFree(pbindinfo->szCustomVerb);
507
508     if(pbindinfo->pUnk && offsetof(BINDINFO, pUnk) < size)
509         IUnknown_Release(pbindinfo->pUnk);
510
511     memset(pbindinfo, 0, size);
512     pbindinfo->cbSize = size;
513 }
514
515 /***********************************************************************
516  *           CopyStgMedium (URLMON.@)
517  */
518 HRESULT WINAPI CopyStgMedium(const STGMEDIUM *src, STGMEDIUM *dst)
519 {
520     TRACE("(%p %p)\n", src, dst);
521
522     if(!src || !dst)
523         return E_POINTER;
524
525     *dst = *src;
526
527     switch(dst->tymed) {
528     case TYMED_NULL:
529         break;
530     case TYMED_FILE:
531         if(src->u.lpszFileName && !src->pUnkForRelease) {
532             DWORD size = (strlenW(src->u.lpszFileName)+1)*sizeof(WCHAR);
533             dst->u.lpszFileName = CoTaskMemAlloc(size);
534             if(!dst->u.lpszFileName)
535                 return E_OUTOFMEMORY;
536             memcpy(dst->u.lpszFileName, src->u.lpszFileName, size);
537         }
538         break;
539     case TYMED_ISTREAM:
540         if(dst->u.pstm)
541             IStream_AddRef(dst->u.pstm);
542         break;
543     case TYMED_ISTORAGE:
544         if(dst->u.pstg)
545             IStorage_AddRef(dst->u.pstg);
546         break;
547     case TYMED_HGLOBAL:
548         if(dst->u.hGlobal) {
549             SIZE_T size = GlobalSize(src->u.hGlobal);
550             char *src_ptr, *dst_ptr;
551
552             dst->u.hGlobal = GlobalAlloc(GMEM_FIXED, size);
553             if(!dst->u.hGlobal)
554                 return E_OUTOFMEMORY;
555             dst_ptr = GlobalLock(dst->u.hGlobal);
556             src_ptr = GlobalLock(src->u.hGlobal);
557             memcpy(dst_ptr, src_ptr, size);
558             GlobalUnlock(src_ptr);
559             GlobalUnlock(dst_ptr);
560         }
561         break;
562     default:
563         FIXME("Unimplemented tymed %d\n", src->tymed);
564     }
565
566     if(dst->pUnkForRelease)
567         IUnknown_AddRef(dst->pUnkForRelease);
568
569     return S_OK;
570 }
571
572 /***********************************************************************
573  *           CopyBindInfo (URLMON.@)
574  */
575 HRESULT WINAPI CopyBindInfo(const BINDINFO *pcbiSrc, BINDINFO *pcbiDest)
576 {
577     DWORD size;
578     HRESULT hres;
579
580     TRACE("(%p %p)\n", pcbiSrc, pcbiDest);
581
582     if(!pcbiSrc || !pcbiDest)
583         return E_POINTER;
584     if(!pcbiSrc->cbSize || !pcbiDest->cbSize)
585         return E_INVALIDARG;
586
587     size = pcbiDest->cbSize;
588     if(size > pcbiSrc->cbSize) {
589         memcpy(pcbiDest, pcbiSrc, pcbiSrc->cbSize);
590         memset((char*)pcbiDest+pcbiSrc->cbSize, 0, size-pcbiSrc->cbSize);
591     } else {
592         memcpy(pcbiDest, pcbiSrc, size);
593     }
594     pcbiDest->cbSize = size;
595
596     size = FIELD_OFFSET(BINDINFO, szExtraInfo)+sizeof(void*);
597     if(pcbiSrc->cbSize>=size && pcbiDest->cbSize>=size && pcbiSrc->szExtraInfo) {
598         size = (strlenW(pcbiSrc->szExtraInfo)+1)*sizeof(WCHAR);
599         pcbiDest->szExtraInfo = CoTaskMemAlloc(size);
600         if(!pcbiDest->szExtraInfo)
601             return E_OUTOFMEMORY;
602         memcpy(pcbiDest->szExtraInfo, pcbiSrc->szExtraInfo, size);
603     }
604
605     size = FIELD_OFFSET(BINDINFO, stgmedData)+sizeof(STGMEDIUM);
606     if(pcbiSrc->cbSize>=size && pcbiDest->cbSize>=size) {
607         hres = CopyStgMedium(&pcbiSrc->stgmedData, &pcbiDest->stgmedData);
608         if(FAILED(hres)) {
609             CoTaskMemFree(pcbiDest->szExtraInfo);
610             return hres;
611         }
612     }
613
614     size = FIELD_OFFSET(BINDINFO, szCustomVerb)+sizeof(void*);
615     if(pcbiSrc->cbSize>=size && pcbiDest->cbSize>=size && pcbiSrc->szCustomVerb) {
616         size = (strlenW(pcbiSrc->szCustomVerb)+1)*sizeof(WCHAR);
617         pcbiDest->szCustomVerb = CoTaskMemAlloc(size);
618         if(!pcbiDest->szCustomVerb) {
619             CoTaskMemFree(pcbiDest->szExtraInfo);
620             ReleaseStgMedium(&pcbiDest->stgmedData);
621             return E_OUTOFMEMORY;
622         }
623         memcpy(pcbiDest->szCustomVerb, pcbiSrc->szCustomVerb, size);
624     }
625
626     size = FIELD_OFFSET(BINDINFO, securityAttributes)+sizeof(SECURITY_ATTRIBUTES);
627     if(pcbiDest->cbSize >= size)
628         memset(&pcbiDest->securityAttributes, 0, sizeof(SECURITY_ATTRIBUTES));
629
630     if(pcbiSrc->pUnk)
631         IUnknown_AddRef(pcbiDest->pUnk);
632
633     return S_OK;
634 }
635
636 static BOOL text_richtext_filter(const BYTE *b, DWORD size)
637 {
638     return size > 5 && !memcmp(b, "{\\rtf", 5);
639 }
640
641 static BOOL text_html_filter(const BYTE *b, DWORD size)
642 {
643     if(size < 6)
644         return FALSE;
645
646     if((b[0] == '<'
647                 && (b[1] == 'h' || b[1] == 'H')
648                 && (b[2] == 't' || b[2] == 'T')
649                 && (b[3] == 'm' || b[3] == 'M')
650                 && (b[4] == 'l' || b[4] == 'L'))
651             || (b[0] == '<'
652                 && (b[1] == 'h' || b[1] == 'H')
653                 && (b[2] == 'e' || b[2] == 'E')
654                 && (b[3] == 'a' || b[3] == 'A')
655                 && (b[4] == 'd' || b[4] == 'D'))) return TRUE;
656
657     return FALSE;
658 }
659
660 static BOOL text_xml_filter(const BYTE *b, DWORD size)
661 {
662     if(size < 7)
663         return FALSE;
664
665     if(b[0] == '<' && b[1] == '?'
666             && (b[2] == 'x' || b[2] == 'X')
667             && (b[3] == 'm' || b[3] == 'M')
668             && (b[4] == 'l' || b[4] == 'L')
669             && b[5] == ' ') return TRUE;
670
671     return FALSE;
672 }
673
674 static BOOL audio_basic_filter(const BYTE *b, DWORD size)
675 {
676     return size > 4
677         && b[0] == '.' && b[1] == 's' && b[2] == 'n' && b[3] == 'd';
678 }
679
680 static BOOL audio_wav_filter(const BYTE *b, DWORD size)
681 {
682     return size > 12
683         && b[0] == 'R' && b[1] == 'I' && b[2] == 'F' && b[3] == 'F'
684         && b[8] == 'W' && b[9] == 'A' && b[10] == 'V' && b[11] == 'E';
685 }
686
687 static BOOL image_gif_filter(const BYTE *b, DWORD size)
688 {
689     return size >= 6
690         && (b[0] == 'G' || b[0] == 'g')
691         && (b[1] == 'I' || b[1] == 'i')
692         && (b[2] == 'F' || b[2] == 'f')
693         &&  b[3] == '8'
694         && (b[4] == '7' || b[4] == '9')
695         && (b[5] == 'A' || b[5] == 'a');
696 }
697
698 static BOOL image_pjpeg_filter(const BYTE *b, DWORD size)
699 {
700     return size > 2 && b[0] == 0xff && b[1] == 0xd8;
701 }
702
703 static BOOL image_tiff_filter(const BYTE *b, DWORD size)
704 {
705     static const BYTE magic1[] = {0x4d,0x4d,0x00,0x2a};
706     static const BYTE magic2[] = {0x49,0x49,0x2a,0xff};
707
708     return size >= 4 && (!memcmp(b, magic1, 4) || !memcmp(b, magic2, 4));
709 }
710
711 static BOOL image_xpng_filter(const BYTE *b, DWORD size)
712 {
713     static const BYTE xpng_header[] = {0x89,'P','N','G',0x0d,0x0a,0x1a,0x0a};
714     return size > sizeof(xpng_header) && !memcmp(b, xpng_header, sizeof(xpng_header));
715 }
716
717 static BOOL image_bmp_filter(const BYTE *b, DWORD size)
718 {
719     return size >= 14
720         && b[0] == 0x42 && b[1] == 0x4d
721         && *(const DWORD *)(b+6) == 0;
722 }
723
724 static BOOL video_avi_filter(const BYTE *b, DWORD size)
725 {
726     return size > 12
727         && b[0] == 'R' && b[1] == 'I' && b[2] == 'F' && b[3] == 'F'
728         && b[8] == 'A' && b[9] == 'V' && b[10] == 'I' && b[11] == 0x20;
729 }
730
731 static BOOL video_mpeg_filter(const BYTE *b, DWORD size)
732 {
733     return size > 4
734         && !b[0] && !b[1] && b[2] == 0x01
735         && (b[3] == 0xb3 || b[3] == 0xba);
736 }
737
738 static BOOL application_postscript_filter(const BYTE *b, DWORD size)
739 {
740     return size > 2 && b[0] == '%' && b[1] == '!';
741 }
742
743 static BOOL application_pdf_filter(const BYTE *b, DWORD size)
744 {
745     return size > 4 && b[0] == 0x25 && b[1] == 0x50 && b[2] == 0x44 && b[3] == 0x46;
746 }
747
748 static BOOL application_xzip_filter(const BYTE *b, DWORD size)
749 {
750     return size > 2 && b[0] == 0x50 && b[1] == 0x4b;
751 }
752
753 static BOOL application_xgzip_filter(const BYTE *b, DWORD size)
754 {
755     return size > 2 && b[0] == 0x1f && b[1] == 0x8b;
756 }
757
758 static BOOL application_java_filter(const BYTE *b, DWORD size)
759 {
760     return size > 4 && b[0] == 0xca && b[1] == 0xfe && b[2] == 0xba && b[3] == 0xbe;
761 }
762
763 static BOOL application_xmsdownload(const BYTE *b, DWORD size)
764 {
765     return size > 2 && b[0] == 'M' && b[1] == 'Z';
766 }
767
768 static inline BOOL is_text_plain_char(BYTE b)
769 {
770     if(b < 0x20 && b != '\n' && b != '\r' && b != '\t')
771         return FALSE;
772     return TRUE;
773 }
774
775 static BOOL text_plain_filter(const BYTE *b, DWORD size)
776 {
777     const BYTE *ptr;
778
779     for(ptr = b; ptr < b+size-1; ptr++) {
780         if(!is_text_plain_char(*ptr))
781             return FALSE;
782     }
783
784     return TRUE;
785 }
786
787 static BOOL application_octet_stream_filter(const BYTE *b, DWORD size)
788 {
789     return TRUE;
790 }
791
792 static HRESULT find_mime_from_buffer(const BYTE *buf, DWORD size, const WCHAR *proposed_mime, WCHAR **ret_mime)
793 {
794     LPCWSTR ret = NULL;
795     int len, i, any_pos_mime = -1;
796
797     static const WCHAR text_htmlW[] = {'t','e','x','t','/','h','t','m','l',0};
798     static const WCHAR text_richtextW[] = {'t','e','x','t','/','r','i','c','h','t','e','x','t',0};
799     static const WCHAR text_xmlW[] = {'t','e','x','t','/','x','m','l',0};
800     static const WCHAR audio_basicW[] = {'a','u','d','i','o','/','b','a','s','i','c',0};
801     static const WCHAR audio_wavW[] = {'a','u','d','i','o','/','w','a','v',0};
802     static const WCHAR image_gifW[] = {'i','m','a','g','e','/','g','i','f',0};
803     static const WCHAR image_pjpegW[] = {'i','m','a','g','e','/','p','j','p','e','g',0};
804     static const WCHAR image_tiffW[] = {'i','m','a','g','e','/','t','i','f','f',0};
805     static const WCHAR image_xpngW[] = {'i','m','a','g','e','/','x','-','p','n','g',0};
806     static const WCHAR image_bmpW[] = {'i','m','a','g','e','/','b','m','p',0};
807     static const WCHAR video_aviW[] = {'v','i','d','e','o','/','a','v','i',0};
808     static const WCHAR video_mpegW[] = {'v','i','d','e','o','/','m','p','e','g',0};
809     static const WCHAR app_postscriptW[] =
810         {'a','p','p','l','i','c','a','t','i','o','n','/','p','o','s','t','s','c','r','i','p','t',0};
811     static const WCHAR app_pdfW[] = {'a','p','p','l','i','c','a','t','i','o','n','/','p','d','f',0};
812     static const WCHAR app_xzipW[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
813         'x','-','z','i','p','-','c','o','m','p','r','e','s','s','e','d',0};
814     static const WCHAR app_xgzipW[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
815         'x','-','g','z','i','p','-','c','o','m','p','r','e','s','s','e','d',0};
816     static const WCHAR app_javaW[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
817         'j','a','v','a',0};
818     static const WCHAR app_xmsdownloadW[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
819         'x','-','m','s','d','o','w','n','l','o','a','d',0};
820     static const WCHAR text_plainW[] = {'t','e','x','t','/','p','l','a','i','n','\0'};
821     static const WCHAR app_octetstreamW[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
822         'o','c','t','e','t','-','s','t','r','e','a','m','\0'};
823
824     static const struct {
825         LPCWSTR mime;
826         BOOL (*filter)(const BYTE *,DWORD);
827     } mime_filters_any_pos[] = {
828         {text_htmlW,       text_html_filter},
829         {text_xmlW,        text_xml_filter}
830     }, mime_filters[] = {
831         {text_richtextW,   text_richtext_filter},
832      /* {audio_xaiffW,     audio_xaiff_filter}, */
833         {audio_basicW,     audio_basic_filter},
834         {audio_wavW,       audio_wav_filter},
835         {image_gifW,       image_gif_filter},
836         {image_pjpegW,     image_pjpeg_filter},
837         {image_tiffW,      image_tiff_filter},
838         {image_xpngW,      image_xpng_filter},
839      /* {image_xbitmapW,   image_xbitmap_filter}, */
840         {image_bmpW,       image_bmp_filter},
841      /* {image_xjgW,       image_xjg_filter}, */
842      /* {image_xemfW,      image_xemf_filter}, */
843      /* {image_xwmfW,      image_xwmf_filter}, */
844         {video_aviW,       video_avi_filter},
845         {video_mpegW,      video_mpeg_filter},
846         {app_postscriptW,  application_postscript_filter},
847      /* {app_base64W,      application_base64_filter}, */
848      /* {app_macbinhex40W, application_macbinhex40_filter}, */
849         {app_pdfW,         application_pdf_filter},
850      /* {app_zcompressedW, application_xcompressed_filter}, */
851         {app_xzipW,        application_xzip_filter},
852         {app_xgzipW,       application_xgzip_filter},
853         {app_javaW,        application_java_filter},
854         {app_xmsdownloadW, application_xmsdownload},
855         {text_plainW,      text_plain_filter},
856         {app_octetstreamW, application_octet_stream_filter}
857     };
858
859     if(!buf || !size) {
860         if(!proposed_mime)
861             return E_FAIL;
862
863         len = strlenW(proposed_mime)+1;
864         *ret_mime = CoTaskMemAlloc(len*sizeof(WCHAR));
865         if(!*ret_mime)
866             return E_OUTOFMEMORY;
867
868         memcpy(*ret_mime, proposed_mime, len*sizeof(WCHAR));
869         return S_OK;
870     }
871
872     if(proposed_mime && (!strcmpW(proposed_mime, app_octetstreamW)
873                 || !strcmpW(proposed_mime, text_plainW)))
874         proposed_mime = NULL;
875
876     if(proposed_mime) {
877         ret = proposed_mime;
878
879         for(i=0; i < sizeof(mime_filters_any_pos)/sizeof(*mime_filters_any_pos); i++) {
880             if(!strcmpW(proposed_mime, mime_filters_any_pos[i].mime)) {
881                 any_pos_mime = i;
882                 for(len=size; len>0; len--) {
883                     if(mime_filters_any_pos[i].filter(buf+size-len, len))
884                         break;
885                 }
886                 if(!len)
887                     ret = NULL;
888                 break;
889             }
890         }
891
892         if(i == sizeof(mime_filters_any_pos)/sizeof(*mime_filters_any_pos)) {
893             for(i=0; i < sizeof(mime_filters)/sizeof(*mime_filters); i++) {
894                 if(!strcmpW(proposed_mime, mime_filters[i].mime)) {
895                     if(!mime_filters[i].filter(buf, size))
896                         ret = NULL;
897                     break;
898                 }
899             }
900         }
901     }
902
903     /* Looks like a bug in native implementation, html and xml mimes
904      * are not looked for if none of them was proposed */
905     if(!proposed_mime || any_pos_mime!=-1) {
906         for(len=size; !ret && len>0; len--) {
907             for(i=0; i<sizeof(mime_filters_any_pos)/sizeof(*mime_filters_any_pos); i++) {
908                 if(mime_filters_any_pos[i].filter(buf+size-len, len)) {
909                     ret = mime_filters_any_pos[i].mime;
910                     break;
911                 }
912             }
913         }
914     }
915
916     i=0;
917     while(!ret) {
918         if(mime_filters[i].filter(buf, size))
919             ret = mime_filters[i].mime;
920         i++;
921     }
922
923     if(any_pos_mime!=-1 && ret==text_plainW)
924         ret = mime_filters_any_pos[any_pos_mime].mime;
925     else if(proposed_mime && ret==app_octetstreamW) {
926         for(len=size; ret==app_octetstreamW && len>0; len--) {
927             if(!is_text_plain_char(buf[size-len]))
928                 break;
929             for(i=0; i<sizeof(mime_filters_any_pos)/sizeof(*mime_filters_any_pos); i++) {
930                 if(mime_filters_any_pos[i].filter(buf+size-len, len)) {
931                     ret = text_plainW;
932                     break;
933                 }
934             }
935         }
936
937         if(ret == app_octetstreamW)
938             ret = proposed_mime;
939     }
940     TRACE("found %s for %s\n", debugstr_w(ret), debugstr_an((const char*)buf, min(32, size)));
941
942     len = strlenW(ret)+1;
943     *ret_mime = CoTaskMemAlloc(len*sizeof(WCHAR));
944     if(!*ret_mime)
945         return E_OUTOFMEMORY;
946
947     memcpy(*ret_mime, ret, len*sizeof(WCHAR));
948     return S_OK;
949 }
950
951 /***********************************************************************
952  *           FindMimeFromData (URLMON.@)
953  *
954  * Determines the Multipurpose Internet Mail Extensions (MIME) type from the data provided.
955  */
956 HRESULT WINAPI FindMimeFromData(LPBC pBC, LPCWSTR pwzUrl, LPVOID pBuffer,
957         DWORD cbSize, LPCWSTR pwzMimeProposed, DWORD dwMimeFlags,
958         LPWSTR* ppwzMimeOut, DWORD dwReserved)
959 {
960     TRACE("(%p,%s,%p,%d,%s,0x%x,%p,0x%x)\n", pBC, debugstr_w(pwzUrl), pBuffer, cbSize,
961             debugstr_w(pwzMimeProposed), dwMimeFlags, ppwzMimeOut, dwReserved);
962
963     if(dwMimeFlags)
964         WARN("dwMimeFlags=%08x\n", dwMimeFlags);
965     if(dwReserved)
966         WARN("dwReserved=%d\n", dwReserved);
967
968     /* pBC seams to not be used */
969
970     if(!ppwzMimeOut || (!pwzUrl && !pBuffer))
971         return E_INVALIDARG;
972
973     if(pwzMimeProposed || pBuffer)
974         return find_mime_from_buffer(pBuffer, cbSize, pwzMimeProposed, ppwzMimeOut);
975
976     if(pwzUrl) {
977         HKEY hkey;
978         DWORD res, size;
979         LPCWSTR ptr;
980         WCHAR mime[64];
981
982         static const WCHAR wszContentType[] =
983                 {'C','o','n','t','e','n','t',' ','T','y','p','e','\0'};
984
985         ptr = strrchrW(pwzUrl, '.');
986         if(!ptr)
987             return E_FAIL;
988
989         res = RegOpenKeyW(HKEY_CLASSES_ROOT, ptr, &hkey);
990         if(res != ERROR_SUCCESS)
991             return HRESULT_FROM_WIN32(res);
992
993         size = sizeof(mime);
994         res = RegQueryValueExW(hkey, wszContentType, NULL, NULL, (LPBYTE)mime, &size);
995         RegCloseKey(hkey);
996         if(res != ERROR_SUCCESS)
997             return HRESULT_FROM_WIN32(res);
998
999         *ppwzMimeOut = CoTaskMemAlloc(size);
1000         memcpy(*ppwzMimeOut, mime, size);
1001         return S_OK;
1002     }
1003
1004     return E_FAIL;
1005 }
1006
1007 /***********************************************************************
1008  *           GetClassFileOrMime (URLMON.@)
1009  *
1010  * Determines the class ID from the bind context, file name or MIME type.
1011  */
1012 HRESULT WINAPI GetClassFileOrMime(LPBC pBC, LPCWSTR pszFilename,
1013         LPVOID pBuffer, DWORD cbBuffer, LPCWSTR pszMimeType, DWORD dwReserved,
1014         CLSID *pclsid)
1015 {
1016     FIXME("(%p, %s, %p, %d, %s, 0x%08x, %p): stub\n", pBC, debugstr_w(pszFilename), pBuffer,
1017             cbBuffer, debugstr_w(pszMimeType), dwReserved, pclsid);
1018     return E_NOTIMPL;
1019 }
1020
1021 /***********************************************************************
1022  * Extract (URLMON.@)
1023  */
1024 HRESULT WINAPI Extract(void *dest, LPCSTR szCabName)
1025 {
1026     HRESULT (WINAPI *pExtract)(void *, LPCSTR);
1027
1028     if (!hCabinet)
1029         hCabinet = LoadLibraryA("cabinet.dll");
1030
1031     if (!hCabinet) return HRESULT_FROM_WIN32(GetLastError());
1032     pExtract = (void *)GetProcAddress(hCabinet, "Extract");
1033     if (!pExtract) return HRESULT_FROM_WIN32(GetLastError());
1034
1035     return pExtract(dest, szCabName);
1036 }
1037
1038 /***********************************************************************
1039  *           IsLoggingEnabledA (URLMON.@)
1040  */
1041 BOOL WINAPI IsLoggingEnabledA(LPCSTR url)
1042 {
1043     FIXME("(%s)\n", debugstr_a(url));
1044     return FALSE;
1045 }
1046
1047 /***********************************************************************
1048  *           IsLoggingEnabledW (URLMON.@)
1049  */
1050 BOOL WINAPI IsLoggingEnabledW(LPCWSTR url)
1051 {
1052     FIXME("(%s)\n", debugstr_w(url));
1053     return FALSE;
1054 }
1055
1056 /***********************************************************************
1057  *           IsProtectedModeURL (URLMON.111)
1058  *    Undocumented, added in IE7
1059  */
1060 BOOL WINAPI IsProtectedModeURL(const WCHAR *url)
1061 {
1062     FIXME("stub: %s\n", debugstr_w(url));
1063     return TRUE;
1064 }
1065
1066 /***********************************************************************
1067  *           LogSqmBits (URLMON.410)
1068  *    Undocumented, added in IE8
1069  */
1070 int WINAPI LogSqmBits(DWORD unk1, DWORD unk2)
1071 {
1072     FIXME("stub: %d %d\n", unk1, unk2);
1073     return 0;
1074 }
1075
1076 /***********************************************************************
1077  *           LogSqmUXCommandOffsetInternal (URLMON.423)
1078  *    Undocumented, added in IE8
1079  */
1080 void WINAPI LogSqmUXCommandOffsetInternal(DWORD unk1, DWORD unk2, DWORD unk3, DWORD unk4)
1081 {
1082     FIXME("stub: %d %d %d %d\n", unk1, unk2, unk3, unk4);
1083 }
1084
1085 /***********************************************************************
1086  *           MapUriToBrowserEmulationState (URLMON.444)
1087  *    Undocumented, added in IE8
1088  */
1089 int WINAPI MapUriToBrowserEmulationState(DWORD unk1, DWORD unk2, DWORD unk3)
1090 {
1091     FIXME("stub: %d %d %d\n", unk1, unk2, unk3);
1092     return 0;
1093 }
1094
1095 /***********************************************************************
1096  *           MapBrowserEmulationModeToUserAgent (URLMON.445)
1097  *    Undocumented, added in IE8
1098  */
1099 int WINAPI MapBrowserEmulationModeToUserAgent(DWORD unk1, DWORD unk2)
1100 {
1101     FIXME("stub: %d %d\n", unk1, unk2);
1102     return 0;
1103 }
1104
1105 /***********************************************************************
1106  *            RegisterMediaTypes
1107  *    Added in IE3, registers known MIME-type strings.
1108  */
1109 HRESULT WINAPI RegisterMediaTypes(UINT types, LPCSTR *szTypes, CLIPFORMAT *cfTypes)
1110 {
1111    FIXME("stub: %u %p %p\n", types, szTypes, cfTypes);
1112    return E_INVALIDARG;
1113 }