mshtml: Pass DispatchEx pointer instead of outer IUnknown to DispatchEx's vtbl functions.
[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
31 #include "wine/debug.h"
32
33 #include "urlmon.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
36
37 LONG URLMON_refCount = 0;
38
39 static HMODULE hCabinet = NULL;
40 static DWORD urlmon_tls = TLS_OUT_OF_INDEXES;
41
42 static void init_session(BOOL);
43
44 static struct list tls_list = LIST_INIT(tls_list);
45
46 static CRITICAL_SECTION tls_cs;
47 static CRITICAL_SECTION_DEBUG tls_cs_dbg =
48 {
49     0, 0, &tls_cs,
50     { &tls_cs_dbg.ProcessLocksList, &tls_cs_dbg.ProcessLocksList },
51       0, 0, { (DWORD_PTR)(__FILE__ ": tls") }
52 };
53
54 static CRITICAL_SECTION tls_cs = { &tls_cs_dbg, -1, 0, 0, 0, 0 };
55
56 tls_data_t *get_tls_data(void)
57 {
58     tls_data_t *data;
59
60     if(urlmon_tls == TLS_OUT_OF_INDEXES) {
61         DWORD tls = TlsAlloc();
62         if(tls == TLS_OUT_OF_INDEXES)
63             return NULL;
64
65         tls = InterlockedCompareExchange((LONG*)&urlmon_tls, tls, TLS_OUT_OF_INDEXES);
66         if(tls != urlmon_tls)
67             TlsFree(tls);
68     }
69
70     data = TlsGetValue(urlmon_tls);
71     if(!data) {
72         data = heap_alloc_zero(sizeof(tls_data_t));
73         if(!data)
74             return NULL;
75
76         EnterCriticalSection(&tls_cs);
77         list_add_tail(&tls_list, &data->entry);
78         LeaveCriticalSection(&tls_cs);
79
80         TlsSetValue(urlmon_tls, data);
81     }
82
83     return data;
84 }
85
86 static void free_tls_list(void)
87 {
88     tls_data_t *data;
89
90     if(urlmon_tls == TLS_OUT_OF_INDEXES)
91         return;
92
93     while(!list_empty(&tls_list)) {
94         data = LIST_ENTRY(list_head(&tls_list), tls_data_t, entry);
95         list_remove(&data->entry);
96         heap_free(data);
97     }
98
99     TlsFree(urlmon_tls);
100 }
101
102 static void detach_thread(void)
103 {
104     tls_data_t *data;
105
106     if(urlmon_tls == TLS_OUT_OF_INDEXES)
107         return;
108
109     data = TlsGetValue(urlmon_tls);
110     if(!data)
111         return;
112
113     EnterCriticalSection(&tls_cs);
114     list_remove(&data->entry);
115     LeaveCriticalSection(&tls_cs);
116
117     if(data->notif_hwnd) {
118         WARN("notif_hwnd not destroyed\n");
119         DestroyWindow(data->notif_hwnd);
120     }
121
122     heap_free(data);
123 }
124
125 static void process_detach(void)
126 {
127     HINTERNET internet_session;
128
129     internet_session = get_internet_session(NULL);
130     if(internet_session)
131         InternetCloseHandle(internet_session);
132
133     if (hCabinet)
134         FreeLibrary(hCabinet);
135
136     init_session(FALSE);
137     free_session();
138     free_tls_list();
139 }
140
141 /***********************************************************************
142  *              DllMain (URLMON.init)
143  */
144 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
145 {
146     TRACE("%p 0x%x %p\n", hinstDLL, fdwReason, fImpLoad);
147
148     URLMON_DllMain( hinstDLL, fdwReason, fImpLoad );
149
150     switch(fdwReason) {
151     case DLL_PROCESS_ATTACH:
152         init_session(TRUE);
153         break;
154
155     case DLL_PROCESS_DETACH:
156         process_detach();
157         break;
158
159     case DLL_THREAD_DETACH:
160         detach_thread();
161         break;
162     }
163     return TRUE;
164 }
165
166
167 /***********************************************************************
168  *              DllInstall (URLMON.@)
169  */
170 HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
171 {
172   FIXME("(%s, %s): stub\n", bInstall?"TRUE":"FALSE",
173         debugstr_w(cmdline));
174
175   return S_OK;
176 }
177
178 /***********************************************************************
179  *              DllCanUnloadNow (URLMON.@)
180  */
181 HRESULT WINAPI DllCanUnloadNow(void)
182 {
183     return URLMON_refCount != 0 ? S_FALSE : S_OK;
184 }
185
186
187
188 /******************************************************************************
189  * Urlmon ClassFactory
190  */
191 typedef struct {
192     const IClassFactoryVtbl *lpClassFactoryVtbl;
193
194     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
195 } ClassFactory;
196
197 #define CLASSFACTORY(x)  ((IClassFactory*)  &(x)->lpClassFactoryVtbl)
198
199 static HRESULT WINAPI CF_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppv)
200 {
201     *ppv = NULL;
202
203     if(IsEqualGUID(riid, &IID_IUnknown)) {
204         TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
205         *ppv = iface;
206     }else if(IsEqualGUID(riid, &IID_IClassFactory)) {
207         TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
208         *ppv = iface;
209     }
210
211     if(*ppv) {
212         IUnknown_AddRef((IUnknown*)*ppv);
213         return S_OK;
214     }
215
216     WARN("(%p)->(%s,%p),not found\n", iface, debugstr_guid(riid), ppv);
217     return E_NOINTERFACE;
218 }
219
220 static ULONG WINAPI CF_AddRef(IClassFactory *iface)
221 {
222     URLMON_LockModule();
223     return 2;
224 }
225
226 static ULONG WINAPI CF_Release(IClassFactory *iface)
227 {
228     URLMON_UnlockModule();
229     return 1;
230 }
231
232
233 static HRESULT WINAPI CF_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
234                                         REFIID riid, LPVOID *ppobj)
235 {
236     ClassFactory *This = (ClassFactory*)iface;
237     HRESULT hres;
238     LPUNKNOWN punk;
239     
240     TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
241
242     *ppobj = NULL;
243     if(SUCCEEDED(hres = This->pfnCreateInstance(pOuter, (LPVOID *) &punk))) {
244         hres = IUnknown_QueryInterface(punk, riid, ppobj);
245         IUnknown_Release(punk);
246     }
247     return hres;
248 }
249
250 static HRESULT WINAPI CF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
251 {
252     TRACE("(%d)\n", dolock);
253
254     if (dolock)
255            URLMON_LockModule();
256     else
257            URLMON_UnlockModule();
258
259     return S_OK;
260 }
261
262 static const IClassFactoryVtbl ClassFactoryVtbl =
263 {
264     CF_QueryInterface,
265     CF_AddRef,
266     CF_Release,
267     CF_CreateInstance,
268     CF_LockServer
269 };
270
271 static const ClassFactory FileProtocolCF =
272     { &ClassFactoryVtbl, FileProtocol_Construct};
273 static const ClassFactory FtpProtocolCF =
274     { &ClassFactoryVtbl, FtpProtocol_Construct};
275 static const ClassFactory GopherProtocolCF =
276     { &ClassFactoryVtbl, GopherProtocol_Construct};
277 static const ClassFactory HttpProtocolCF =
278     { &ClassFactoryVtbl, HttpProtocol_Construct};
279 static const ClassFactory HttpSProtocolCF =
280     { &ClassFactoryVtbl, HttpSProtocol_Construct};
281 static const ClassFactory MkProtocolCF =
282     { &ClassFactoryVtbl, MkProtocol_Construct};
283 static const ClassFactory SecurityManagerCF =
284     { &ClassFactoryVtbl, SecManagerImpl_Construct};
285 static const ClassFactory ZoneManagerCF =
286     { &ClassFactoryVtbl, ZoneMgrImpl_Construct};
287 static const ClassFactory StdURLMonikerCF =
288     { &ClassFactoryVtbl, StdURLMoniker_Construct};
289 static const ClassFactory MimeFilterCF =
290     { &ClassFactoryVtbl, MimeFilter_Construct};
291  
292 struct object_creation_info
293 {
294     const CLSID *clsid;
295     IClassFactory *cf;
296     LPCWSTR protocol;
297 };
298
299 static const WCHAR wszFile[] = {'f','i','l','e',0};
300 static const WCHAR wszFtp[]  = {'f','t','p',0};
301 static const WCHAR wszGopher[]  = {'g','o','p','h','e','r',0};
302 static const WCHAR wszHttp[] = {'h','t','t','p',0};
303 static const WCHAR wszHttps[] = {'h','t','t','p','s',0};
304 static const WCHAR wszMk[]   = {'m','k',0};
305
306 static const struct object_creation_info object_creation[] =
307 {
308     { &CLSID_FileProtocol,            CLASSFACTORY(&FileProtocolCF),    wszFile },
309     { &CLSID_FtpProtocol,             CLASSFACTORY(&FtpProtocolCF),     wszFtp  },
310     { &CLSID_GopherProtocol,          CLASSFACTORY(&GopherProtocolCF),  wszGopher },
311     { &CLSID_HttpProtocol,            CLASSFACTORY(&HttpProtocolCF),    wszHttp },
312     { &CLSID_HttpSProtocol,           CLASSFACTORY(&HttpSProtocolCF),   wszHttps },
313     { &CLSID_MkProtocol,              CLASSFACTORY(&MkProtocolCF),      wszMk },
314     { &CLSID_InternetSecurityManager, CLASSFACTORY(&SecurityManagerCF), NULL    },
315     { &CLSID_InternetZoneManager,     CLASSFACTORY(&ZoneManagerCF),     NULL    },
316     { &CLSID_StdURLMoniker,           CLASSFACTORY(&StdURLMonikerCF),   NULL    },
317     { &CLSID_DeCompMimeFilter,        CLASSFACTORY(&MimeFilterCF),      NULL    }
318 };
319
320 static void init_session(BOOL init)
321 {
322     unsigned int i;
323
324     for(i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++) {
325
326         if(object_creation[i].protocol)
327             register_urlmon_namespace(object_creation[i].cf, object_creation[i].clsid,
328                                       object_creation[i].protocol, init);
329     }
330 }
331
332 /*******************************************************************************
333  * DllGetClassObject [URLMON.@]
334  * Retrieves class object from a DLL object
335  *
336  * NOTES
337  *    Docs say returns STDAPI
338  *
339  * PARAMS
340  *    rclsid [I] CLSID for the class object
341  *    riid   [I] Reference to identifier of interface for class object
342  *    ppv    [O] Address of variable to receive interface pointer for riid
343  *
344  * RETURNS
345  *    Success: S_OK
346  *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
347  *             E_UNEXPECTED
348  */
349
350 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
351 {
352     unsigned int i;
353     HRESULT hr;
354     
355     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
356     
357     for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
358     {
359         if (IsEqualGUID(object_creation[i].clsid, rclsid))
360             return IClassFactory_QueryInterface(object_creation[i].cf, riid, ppv);
361     }
362
363     hr = URLMON_DllGetClassObject(rclsid, riid, ppv);
364     if(SUCCEEDED(hr))
365         return hr;
366
367     FIXME("%s: no class found.\n", debugstr_guid(rclsid));
368     return CLASS_E_CLASSNOTAVAILABLE;
369 }
370
371 static HRESULT register_inf(BOOL doregister)
372 {
373     HRESULT (WINAPI *pRegInstall)(HMODULE hm, LPCSTR pszSection, const STRTABLEA* pstTable);
374     HMODULE hAdvpack;
375
376     static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
377
378     hAdvpack = LoadLibraryW(wszAdvpack);
379     pRegInstall = (void *)GetProcAddress(hAdvpack, "RegInstall");
380
381     return pRegInstall(hProxyDll, doregister ? "RegisterDll" : "UnregisterDll", NULL);
382 }
383
384 /***********************************************************************
385  *              DllRegisterServer (URLMON.@)
386  */
387 HRESULT WINAPI DllRegisterServer(void)
388 {
389     HRESULT hr;
390
391     TRACE("\n");
392
393     hr = URLMON_DllRegisterServer();
394     return SUCCEEDED(hr) ? register_inf(TRUE) : hr;
395 }
396
397 /***********************************************************************
398  *              DllUnregisterServer (URLMON.@)
399  */
400 HRESULT WINAPI DllUnregisterServer(void)
401 {
402     HRESULT hr;
403
404     TRACE("\n");
405
406     hr = URLMON_DllUnregisterServer();
407     return SUCCEEDED(hr) ? register_inf(FALSE) : hr;
408 }
409
410 /***********************************************************************
411  *              DllRegisterServerEx (URLMON.@)
412  */
413 HRESULT WINAPI DllRegisterServerEx(void)
414 {
415     FIXME("(void): stub\n");
416
417     return E_FAIL;
418 }
419
420 /**************************************************************************
421  *                 IsValidURL (URLMON.@)
422  * 
423  * Determines if a specified string is a valid URL.
424  *
425  * PARAMS
426  *  pBC        [I] ignored, must be NULL.
427  *  szURL      [I] string that represents the URL in question.
428  *  dwReserved [I] reserved and must be zero.
429  *
430  * RETURNS
431  *  Success: S_OK.
432  *  Failure: S_FALSE.
433  *  returns E_INVALIDARG if one or more of the args is invalid.
434  *
435  * TODO:
436  *  test functionality against windows to see what a valid URL is.
437  */
438 HRESULT WINAPI IsValidURL(LPBC pBC, LPCWSTR szURL, DWORD dwReserved)
439 {
440     FIXME("(%p, %s, %d): stub\n", pBC, debugstr_w(szURL), dwReserved);
441
442     if (pBC || dwReserved || !szURL)
443         return E_INVALIDARG;
444
445     return S_OK;
446 }
447
448 /**************************************************************************
449  *                 FaultInIEFeature (URLMON.@)
450  *
451  *  Undocumented.  Appears to be used by native shdocvw.dll.
452  */
453 HRESULT WINAPI FaultInIEFeature( HWND hwnd, uCLSSPEC * pClassSpec,
454                                  QUERYCONTEXT *pQuery, DWORD flags )
455 {
456     FIXME("%p %p %p %08x\n", hwnd, pClassSpec, pQuery, flags);
457     return E_NOTIMPL;
458 }
459
460 /**************************************************************************
461  *                 CoGetClassObjectFromURL (URLMON.@)
462  */
463 HRESULT WINAPI CoGetClassObjectFromURL( REFCLSID rclsid, LPCWSTR szCodeURL, DWORD dwFileVersionMS,
464                                         DWORD dwFileVersionLS, LPCWSTR szContentType,
465                                         LPBINDCTX pBindCtx, DWORD dwClsContext, LPVOID pvReserved,
466                                         REFIID riid, LPVOID *ppv )
467 {
468     FIXME("(%s %s %d %d %s %p %d %p %s %p) Stub!\n", debugstr_guid(rclsid), debugstr_w(szCodeURL),
469         dwFileVersionMS, dwFileVersionLS, debugstr_w(szContentType), pBindCtx, dwClsContext, pvReserved,
470         debugstr_guid(riid), ppv);
471     return E_NOINTERFACE;
472 }
473
474 /***********************************************************************
475  *           ReleaseBindInfo (URLMON.@)
476  *
477  * Release the resources used by the specified BINDINFO structure.
478  *
479  * PARAMS
480  *  pbindinfo [I] BINDINFO to release.
481  *
482  * RETURNS
483  *  Nothing.
484  */
485 void WINAPI ReleaseBindInfo(BINDINFO* pbindinfo)
486 {
487     DWORD size;
488
489     TRACE("(%p)\n", pbindinfo);
490
491     if(!pbindinfo || !(size = pbindinfo->cbSize))
492         return;
493
494     CoTaskMemFree(pbindinfo->szExtraInfo);
495     ReleaseStgMedium(&pbindinfo->stgmedData);
496
497     if(offsetof(BINDINFO, szExtraInfo) < size)
498         CoTaskMemFree(pbindinfo->szCustomVerb);
499
500     if(pbindinfo->pUnk && offsetof(BINDINFO, pUnk) < size)
501         IUnknown_Release(pbindinfo->pUnk);
502
503     memset(pbindinfo, 0, size);
504     pbindinfo->cbSize = size;
505 }
506
507 /***********************************************************************
508  *           CopyStgMedium (URLMON.@)
509  */
510 HRESULT WINAPI CopyStgMedium(const STGMEDIUM *src, STGMEDIUM *dst)
511 {
512     TRACE("(%p %p)\n", src, dst);
513
514     if(!src || !dst)
515         return E_POINTER;
516
517     *dst = *src;
518
519     switch(dst->tymed) {
520     case TYMED_NULL:
521         break;
522     case TYMED_FILE:
523         if(src->u.lpszFileName && !src->pUnkForRelease) {
524             DWORD size = (strlenW(src->u.lpszFileName)+1)*sizeof(WCHAR);
525             dst->u.lpszFileName = CoTaskMemAlloc(size);
526             memcpy(dst->u.lpszFileName, src->u.lpszFileName, size);
527         }
528         break;
529     case TYMED_ISTREAM:
530         if(dst->u.pstm)
531             IStream_AddRef(dst->u.pstm);
532         break;
533     case TYMED_ISTORAGE:
534         if(dst->u.pstg)
535             IStorage_AddRef(dst->u.pstg);
536         break;
537     default:
538         FIXME("Unimplemented tymed %d\n", src->tymed);
539     }
540
541     if(dst->pUnkForRelease)
542         IUnknown_AddRef(dst->pUnkForRelease);
543
544     return S_OK;
545 }
546
547 static BOOL text_richtext_filter(const BYTE *b, DWORD size)
548 {
549     return size > 5 && !memcmp(b, "{\\rtf", 5);
550 }
551
552 static BOOL text_html_filter(const BYTE *b, DWORD size)
553 {
554     DWORD i;
555
556     if(size < 5)
557         return FALSE;
558
559     for(i=0; i < size-5; i++) {
560         if(b[i] == '<'
561            && (b[i+1] == 'h' || b[i+1] == 'H')
562            && (b[i+2] == 't' || b[i+2] == 'T')
563            && (b[i+3] == 'm' || b[i+3] == 'M')
564            && (b[i+4] == 'l' || b[i+4] == 'L'))
565             return TRUE;
566     }
567
568     return FALSE;
569 }
570
571 static BOOL audio_basic_filter(const BYTE *b, DWORD size)
572 {
573     return size > 4
574         && b[0] == '.' && b[1] == 's' && b[2] == 'n' && b[3] == 'd';
575 }
576
577 static BOOL audio_wav_filter(const BYTE *b, DWORD size)
578 {
579     return size > 12
580         && b[0] == 'R' && b[1] == 'I' && b[2] == 'F' && b[3] == 'F'
581         && b[8] == 'W' && b[9] == 'A' && b[10] == 'V' && b[11] == 'E';
582 }
583
584 static BOOL image_gif_filter(const BYTE *b, DWORD size)
585 {
586     return size >= 6
587         && (b[0] == 'G' || b[0] == 'g')
588         && (b[1] == 'I' || b[1] == 'i')
589         && (b[2] == 'F' || b[2] == 'f')
590         &&  b[3] == '8'
591         && (b[4] == '7' || b[4] == '9')
592         && (b[5] == 'A' || b[5] == 'a');
593 }
594
595 static BOOL image_pjpeg_filter(const BYTE *b, DWORD size)
596 {
597     return size > 2 && b[0] == 0xff && b[1] == 0xd8;
598 }
599
600 static BOOL image_tiff_filter(const BYTE *b, DWORD size)
601 {
602     return size > 2 && b[0] == 0x4d && b[1] == 0x4d;
603 }
604
605 static BOOL image_xpng_filter(const BYTE *b, DWORD size)
606 {
607     static const BYTE xpng_header[] = {0x89,'P','N','G',0x0d,0x0a,0x1a,0x0a};
608     return size > sizeof(xpng_header) && !memcmp(b, xpng_header, sizeof(xpng_header));
609 }
610
611 static BOOL image_bmp_filter(const BYTE *b, DWORD size)
612 {
613     return size >= 14
614         && b[0] == 0x42 && b[1] == 0x4d
615         && *(const DWORD *)(b+6) == 0;
616 }
617
618 static BOOL video_avi_filter(const BYTE *b, DWORD size)
619 {
620     return size > 12
621         && b[0] == 'R' && b[1] == 'I' && b[2] == 'F' && b[3] == 'F'
622         && b[8] == 'A' && b[9] == 'V' && b[10] == 'I' && b[11] == 0x20;
623 }
624
625 static BOOL video_mpeg_filter(const BYTE *b, DWORD size)
626 {
627     return size > 4
628         && !b[0] && !b[1] && b[2] == 0x01
629         && (b[3] == 0xb3 || b[3] == 0xba);
630 }
631
632 static BOOL application_postscript_filter(const BYTE *b, DWORD size)
633 {
634     return size > 2 && b[0] == '%' && b[1] == '!';
635 }
636
637 static BOOL application_pdf_filter(const BYTE *b, DWORD size)
638 {
639     return size > 4 && b[0] == 0x25 && b[1] == 0x50 && b[2] == 0x44 && b[3] == 0x46;
640 }
641
642 static BOOL application_xzip_filter(const BYTE *b, DWORD size)
643 {
644     return size > 2 && b[0] == 0x50 && b[1] == 0x4b;
645 }
646
647 static BOOL application_xgzip_filter(const BYTE *b, DWORD size)
648 {
649     return size > 2 && b[0] == 0x1f && b[1] == 0x8b;
650 }
651
652 static BOOL application_java_filter(const BYTE *b, DWORD size)
653 {
654     return size > 4 && b[0] == 0xca && b[1] == 0xfe && b[2] == 0xba && b[3] == 0xbe;
655 }
656
657 static BOOL application_xmsdownload(const BYTE *b, DWORD size)
658 {
659     return size > 2 && b[0] == 'M' && b[1] == 'Z';
660 }
661
662 static BOOL text_plain_filter(const BYTE *b, DWORD size)
663 {
664     const BYTE *ptr;
665
666     for(ptr = b; ptr < b+size-1; ptr++) {
667         if(*ptr < 0x20 && *ptr != '\n' && *ptr != '\r' && *ptr != '\t')
668             return FALSE;
669     }
670
671     return TRUE;
672 }
673
674 static BOOL application_octet_stream_filter(const BYTE *b, DWORD size)
675 {
676     return TRUE;
677 }
678
679 static HRESULT find_mime_from_buffer(const BYTE *buf, DWORD size, const WCHAR *proposed_mime, WCHAR **ret_mime)
680 {
681     LPCWSTR ret = NULL;
682     DWORD len, i;
683
684     static const WCHAR text_htmlW[] = {'t','e','x','t','/','h','t','m','l',0};
685     static const WCHAR text_richtextW[] = {'t','e','x','t','/','r','i','c','h','t','e','x','t',0};
686     static const WCHAR audio_basicW[] = {'a','u','d','i','o','/','b','a','s','i','c',0};
687     static const WCHAR audio_wavW[] = {'a','u','d','i','o','/','w','a','v',0};
688     static const WCHAR image_gifW[] = {'i','m','a','g','e','/','g','i','f',0};
689     static const WCHAR image_pjpegW[] = {'i','m','a','g','e','/','p','j','p','e','g',0};
690     static const WCHAR image_tiffW[] = {'i','m','a','g','e','/','t','i','f','f',0};
691     static const WCHAR image_xpngW[] = {'i','m','a','g','e','/','x','-','p','n','g',0};
692     static const WCHAR image_bmpW[] = {'i','m','a','g','e','/','b','m','p',0};
693     static const WCHAR video_aviW[] = {'v','i','d','e','o','/','a','v','i',0};
694     static const WCHAR video_mpegW[] = {'v','i','d','e','o','/','m','p','e','g',0};
695     static const WCHAR app_postscriptW[] =
696         {'a','p','p','l','i','c','a','t','i','o','n','/','p','o','s','t','s','c','r','i','p','t',0};
697     static const WCHAR app_pdfW[] = {'a','p','p','l','i','c','a','t','i','o','n','/','p','d','f',0};
698     static const WCHAR app_xzipW[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
699         'x','-','z','i','p','-','c','o','m','p','r','e','s','s','e','d',0};
700     static const WCHAR app_xgzipW[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
701         'x','-','g','z','i','p','-','c','o','m','p','r','e','s','s','e','d',0};
702     static const WCHAR app_javaW[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
703         'j','a','v','a',0};
704     static const WCHAR app_xmsdownloadW[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
705         'x','-','m','s','d','o','w','n','l','o','a','d',0};
706     static const WCHAR text_plainW[] = {'t','e','x','t','/','p','l','a','i','n','\0'};
707     static const WCHAR app_octetstreamW[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
708         'o','c','t','e','t','-','s','t','r','e','a','m','\0'};
709
710     static const struct {
711         LPCWSTR mime;
712         BOOL (*filter)(const BYTE *,DWORD);
713     } mime_filters[] = {
714         {text_htmlW,       text_html_filter},
715         {text_richtextW,   text_richtext_filter},
716      /* {audio_xaiffW,     audio_xaiff_filter}, */
717         {audio_basicW,     audio_basic_filter},
718         {audio_wavW,       audio_wav_filter},
719         {image_gifW,       image_gif_filter},
720         {image_pjpegW,     image_pjpeg_filter},
721         {image_tiffW,      image_tiff_filter},
722         {image_xpngW,      image_xpng_filter},
723      /* {image_xbitmapW,   image_xbitmap_filter}, */
724         {image_bmpW,       image_bmp_filter},
725      /* {image_xjgW,       image_xjg_filter}, */
726      /* {image_xemfW,      image_xemf_filter}, */
727      /* {image_xwmfW,      image_xwmf_filter}, */
728         {video_aviW,       video_avi_filter},
729         {video_mpegW,      video_mpeg_filter},
730         {app_postscriptW,  application_postscript_filter},
731      /* {app_base64W,      application_base64_filter}, */
732      /* {app_macbinhex40W, application_macbinhex40_filter}, */
733         {app_pdfW,         application_pdf_filter},
734      /* {app_zcompressedW, application_xcompressed_filter}, */
735         {app_xzipW,        application_xzip_filter},
736         {app_xgzipW,       application_xgzip_filter},
737         {app_javaW,        application_java_filter},
738         {app_xmsdownloadW, application_xmsdownload},
739         {text_plainW,      text_plain_filter},
740         {app_octetstreamW, application_octet_stream_filter}
741     };
742
743     if(!buf || !size) {
744         if(!proposed_mime)
745             return E_FAIL;
746
747         len = strlenW(proposed_mime)+1;
748         *ret_mime = CoTaskMemAlloc(len*sizeof(WCHAR));
749         if(!*ret_mime)
750             return E_OUTOFMEMORY;
751
752         memcpy(*ret_mime, proposed_mime, len*sizeof(WCHAR));
753         return S_OK;
754     }
755
756     if(proposed_mime && strcmpW(proposed_mime, app_octetstreamW)) {
757         for(i=0; i < sizeof(mime_filters)/sizeof(*mime_filters); i++) {
758             if(!strcmpW(proposed_mime, mime_filters[i].mime))
759                 break;
760         }
761
762         if(i == sizeof(mime_filters)/sizeof(*mime_filters) || mime_filters[i].filter(buf, size)) {
763             len = strlenW(proposed_mime)+1;
764             *ret_mime = CoTaskMemAlloc(len*sizeof(WCHAR));
765             if(!*ret_mime)
766                 return E_OUTOFMEMORY;
767
768             memcpy(*ret_mime, proposed_mime, len*sizeof(WCHAR));
769             return S_OK;
770         }
771     }
772
773     i=0;
774     while(!ret) {
775         if(mime_filters[i].filter(buf, size))
776             ret = mime_filters[i].mime;
777         i++;
778     }
779
780     TRACE("found %s for %s\n", debugstr_w(ret), debugstr_an((const char*)buf, min(32, size)));
781
782     if(proposed_mime) {
783         if(i == sizeof(mime_filters)/sizeof(*mime_filters))
784             ret = proposed_mime;
785
786         /* text/html is a special case */
787         if(!strcmpW(proposed_mime, text_htmlW) && !strcmpW(ret, text_plainW))
788             ret = text_htmlW;
789     }
790
791     len = strlenW(ret)+1;
792     *ret_mime = CoTaskMemAlloc(len*sizeof(WCHAR));
793     if(!*ret_mime)
794         return E_OUTOFMEMORY;
795
796     memcpy(*ret_mime, ret, len*sizeof(WCHAR));
797     return S_OK;
798 }
799
800 /***********************************************************************
801  *           FindMimeFromData (URLMON.@)
802  *
803  * Determines the Multipurpose Internet Mail Extensions (MIME) type from the data provided.
804  */
805 HRESULT WINAPI FindMimeFromData(LPBC pBC, LPCWSTR pwzUrl, LPVOID pBuffer,
806         DWORD cbSize, LPCWSTR pwzMimeProposed, DWORD dwMimeFlags,
807         LPWSTR* ppwzMimeOut, DWORD dwReserved)
808 {
809     TRACE("(%p,%s,%p,%d,%s,0x%x,%p,0x%x)\n", pBC, debugstr_w(pwzUrl), pBuffer, cbSize,
810             debugstr_w(pwzMimeProposed), dwMimeFlags, ppwzMimeOut, dwReserved);
811
812     if(dwMimeFlags)
813         WARN("dwMimeFlags=%08x\n", dwMimeFlags);
814     if(dwReserved)
815         WARN("dwReserved=%d\n", dwReserved);
816
817     /* pBC seams to not be used */
818
819     if(!ppwzMimeOut || (!pwzUrl && !pBuffer))
820         return E_INVALIDARG;
821
822     if(pwzMimeProposed || pBuffer)
823         return find_mime_from_buffer(pBuffer, cbSize, pwzMimeProposed, ppwzMimeOut);
824
825     if(pwzUrl) {
826         HKEY hkey;
827         DWORD res, size;
828         LPCWSTR ptr;
829         WCHAR mime[64];
830
831         static const WCHAR wszContentType[] =
832                 {'C','o','n','t','e','n','t',' ','T','y','p','e','\0'};
833
834         ptr = strrchrW(pwzUrl, '.');
835         if(!ptr)
836             return E_FAIL;
837
838         res = RegOpenKeyW(HKEY_CLASSES_ROOT, ptr, &hkey);
839         if(res != ERROR_SUCCESS)
840             return HRESULT_FROM_WIN32(res);
841
842         size = sizeof(mime);
843         res = RegQueryValueExW(hkey, wszContentType, NULL, NULL, (LPBYTE)mime, &size);
844         RegCloseKey(hkey);
845         if(res != ERROR_SUCCESS)
846             return HRESULT_FROM_WIN32(res);
847
848         *ppwzMimeOut = CoTaskMemAlloc(size);
849         memcpy(*ppwzMimeOut, mime, size);
850         return S_OK;
851     }
852
853     return E_FAIL;
854 }
855
856 /***********************************************************************
857  *           GetClassFileOrMime (URLMON.@)
858  *
859  * Determines the class ID from the bind context, file name or MIME type.
860  */
861 HRESULT WINAPI GetClassFileOrMime(LPBC pBC, LPCWSTR pszFilename,
862         LPVOID pBuffer, DWORD cbBuffer, LPCWSTR pszMimeType, DWORD dwReserved,
863         CLSID *pclsid)
864 {
865     FIXME("(%p, %s, %p, %d, %p, 0x%08x, %p): stub\n", pBC,
866         debugstr_w(pszFilename), pBuffer, cbBuffer, debugstr_w(pszMimeType),
867         dwReserved, pclsid);
868     return E_NOTIMPL;
869 }
870
871 /***********************************************************************
872  * Extract (URLMON.@)
873  */
874 HRESULT WINAPI Extract(void *dest, LPCSTR szCabName)
875 {
876     HRESULT (WINAPI *pExtract)(void *, LPCSTR);
877
878     if (!hCabinet)
879         hCabinet = LoadLibraryA("cabinet.dll");
880
881     if (!hCabinet) return HRESULT_FROM_WIN32(GetLastError());
882     pExtract = (void *)GetProcAddress(hCabinet, "Extract");
883     if (!pExtract) return HRESULT_FROM_WIN32(GetLastError());
884
885     return pExtract(dest, szCabName);
886 }
887
888 /***********************************************************************
889  *           IsLoggingEnabledA (URLMON.@)
890  */
891 BOOL WINAPI IsLoggingEnabledA(LPCSTR url)
892 {
893     FIXME("(%s)\n", debugstr_a(url));
894     return FALSE;
895 }
896
897 /***********************************************************************
898  *           IsLoggingEnabledW (URLMON.@)
899  */
900 BOOL WINAPI IsLoggingEnabledW(LPCWSTR url)
901 {
902     FIXME("(%s)\n", debugstr_w(url));
903     return FALSE;
904 }
905
906 /***********************************************************************
907  *           URLMON_410 (URLMON.410)
908  *    Undocumented, added in IE8
909  */
910 BOOL WINAPI URLMON_410(DWORD unknown1, DWORD unknown2)
911 {
912     FIXME("stub: %d %d\n", unknown1, unknown2);
913     return FALSE;
914 }
915
916 /***********************************************************************
917  *           URLMON_423 (URLMON.423)
918  *    Undocumented, added in IE8
919  */
920 BOOL WINAPI URLMON_423(DWORD unknown1, DWORD unknown2, DWORD unknown3, DWORD unknown4)
921 {
922     FIXME("stub: %d %d %d %d\n", unknown1, unknown2, unknown3, unknown4);
923     return FALSE;
924 }