winspool: More error checks for XcvDataW.
[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 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28
29 #define NO_SHLWAPI_REG
30 #include "shlwapi.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33
34 #include "winuser.h"
35 #include "urlmon.h"
36 #include "urlmon_main.h"
37 #include "ole2.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
40
41 LONG URLMON_refCount = 0;
42
43 HINSTANCE URLMON_hInstance = 0;
44 static HMODULE hCabinet = NULL;
45
46 static void init_session(BOOL);
47
48 /***********************************************************************
49  *              DllMain (URLMON.init)
50  */
51 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
52 {
53     TRACE("%p 0x%x %p\n", hinstDLL, fdwReason, fImpLoad);
54
55     switch(fdwReason) {
56     case DLL_PROCESS_ATTACH:
57         DisableThreadLibraryCalls(hinstDLL);
58         URLMON_hInstance = hinstDLL;
59         init_session(TRUE);
60         break;
61
62     case DLL_PROCESS_DETACH:
63         if (hCabinet)
64             FreeLibrary(hCabinet);
65         hCabinet = NULL;
66         init_session(FALSE);
67         URLMON_hInstance = 0;
68         break;
69     }
70     return TRUE;
71 }
72
73
74 /***********************************************************************
75  *              DllInstall (URLMON.@)
76  */
77 HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
78 {
79   FIXME("(%s, %s): stub\n", bInstall?"TRUE":"FALSE",
80         debugstr_w(cmdline));
81
82   return S_OK;
83 }
84
85 /***********************************************************************
86  *              DllCanUnloadNow (URLMON.@)
87  */
88 HRESULT WINAPI DllCanUnloadNow(void)
89 {
90     return URLMON_refCount != 0 ? S_FALSE : S_OK;
91 }
92
93
94
95 /******************************************************************************
96  * Urlmon ClassFactory
97  */
98 typedef struct {
99     const IClassFactoryVtbl *lpClassFactoryVtbl;
100
101     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
102 } ClassFactory;
103
104 #define CLASSFACTORY(x)  ((IClassFactory*)  &(x)->lpClassFactoryVtbl)
105
106 static HRESULT WINAPI CF_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppv)
107 {
108     *ppv = NULL;
109
110     if(IsEqualGUID(riid, &IID_IUnknown)) {
111         TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
112         *ppv = iface;
113     }else if(IsEqualGUID(riid, &IID_IClassFactory)) {
114         TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
115         *ppv = iface;
116     }
117
118     if(*ppv) {
119         IUnknown_AddRef((IUnknown*)*ppv);
120         return S_OK;
121     }
122
123     WARN("(%p)->(%s,%p),not found\n", iface, debugstr_guid(riid), ppv);
124     return E_NOINTERFACE;
125 }
126
127 static ULONG WINAPI CF_AddRef(IClassFactory *iface)
128 {
129     URLMON_LockModule();
130     return 2;
131 }
132
133 static ULONG WINAPI CF_Release(IClassFactory *iface)
134 {
135     URLMON_UnlockModule();
136     return 1;
137 }
138
139
140 static HRESULT WINAPI CF_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
141                                         REFIID riid, LPVOID *ppobj)
142 {
143     ClassFactory *This = (ClassFactory*)iface;
144     HRESULT hres;
145     LPUNKNOWN punk;
146     
147     TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
148
149     *ppobj = NULL;
150     if(SUCCEEDED(hres = This->pfnCreateInstance(pOuter, (LPVOID *) &punk))) {
151         hres = IUnknown_QueryInterface(punk, riid, ppobj);
152         IUnknown_Release(punk);
153     }
154     return hres;
155 }
156
157 static HRESULT WINAPI CF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
158 {
159     TRACE("(%d)\n", dolock);
160
161     if (dolock)
162            URLMON_LockModule();
163     else
164            URLMON_UnlockModule();
165
166     return S_OK;
167 }
168
169 static const IClassFactoryVtbl ClassFactoryVtbl =
170 {
171     CF_QueryInterface,
172     CF_AddRef,
173     CF_Release,
174     CF_CreateInstance,
175     CF_LockServer
176 };
177
178 static const ClassFactory FileProtocolCF =
179     { &ClassFactoryVtbl, FileProtocol_Construct};
180 static const ClassFactory FtpProtocolCF =
181     { &ClassFactoryVtbl, FtpProtocol_Construct};
182 static const ClassFactory HttpProtocolCF =
183     { &ClassFactoryVtbl, HttpProtocol_Construct};
184 static const ClassFactory MkProtocolCF =
185     { &ClassFactoryVtbl, MkProtocol_Construct};
186 static const ClassFactory SecurityManagerCF =
187     { &ClassFactoryVtbl, SecManagerImpl_Construct};
188 static const ClassFactory ZoneManagerCF =
189     { &ClassFactoryVtbl, ZoneMgrImpl_Construct};
190  
191 struct object_creation_info
192 {
193     const CLSID *clsid;
194     IClassFactory *cf;
195     LPCWSTR protocol;
196 };
197
198 static const WCHAR wszFile[] = {'f','i','l','e',0};
199 static const WCHAR wszFtp[]  = {'f','t','p',0};
200 static const WCHAR wszHttp[] = {'h','t','t','p',0};
201 static const WCHAR wszMk[]   = {'m','k',0};
202
203 static const struct object_creation_info object_creation[] =
204 {
205     { &CLSID_FileProtocol,            CLASSFACTORY(&FileProtocolCF),    wszFile },
206     { &CLSID_FtpProtocol,             CLASSFACTORY(&FtpProtocolCF),     wszFtp  },
207     { &CLSID_HttpProtocol,            CLASSFACTORY(&HttpProtocolCF),    wszHttp },
208     { &CLSID_MkProtocol,              CLASSFACTORY(&MkProtocolCF),      wszMk },
209     { &CLSID_InternetSecurityManager, CLASSFACTORY(&SecurityManagerCF), NULL    },
210     { &CLSID_InternetZoneManager,     CLASSFACTORY(&ZoneManagerCF),     NULL    }
211 };
212
213 static void init_session(BOOL init)
214 {
215     IInternetSession *session;
216     int i;
217
218     CoInternetGetSession(0, &session, 0);
219
220     for(i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++) {
221         if(object_creation[i].protocol) {
222             if(init)
223                 IInternetSession_RegisterNameSpace(session, object_creation[i].cf,
224                         object_creation[i].clsid, object_creation[i].protocol, 0, NULL, 0);
225             else
226                 IInternetSession_UnregisterNameSpace(session, object_creation[i].cf,
227                         object_creation[i].protocol);
228         }
229     }
230
231     IInternetSession_Release(session);
232 }
233
234 /*******************************************************************************
235  * DllGetClassObject [URLMON.@]
236  * Retrieves class object from a DLL object
237  *
238  * NOTES
239  *    Docs say returns STDAPI
240  *
241  * PARAMS
242  *    rclsid [I] CLSID for the class object
243  *    riid   [I] Reference to identifier of interface for class object
244  *    ppv    [O] Address of variable to receive interface pointer for riid
245  *
246  * RETURNS
247  *    Success: S_OK
248  *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
249  *             E_UNEXPECTED
250  */
251
252 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
253 {
254     int i;
255     
256     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
257     
258     for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
259     {
260         if (IsEqualGUID(object_creation[i].clsid, rclsid))
261             return IClassFactory_QueryInterface(object_creation[i].cf, riid, ppv);
262     }
263
264     FIXME("%s: no class found.\n", debugstr_guid(rclsid));
265     return CLASS_E_CLASSNOTAVAILABLE;
266 }
267
268
269 /***********************************************************************
270  *              DllRegisterServerEx (URLMON.@)
271  */
272 HRESULT WINAPI DllRegisterServerEx(void)
273 {
274     FIXME("(void): stub\n");
275
276     return E_FAIL;
277 }
278
279 /**************************************************************************
280  *                 UrlMkSetSessionOption (URLMON.@)
281  */
282 HRESULT WINAPI UrlMkSetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
283                                         DWORD Reserved)
284 {
285     FIXME("(%#x, %p, %#x): stub\n", dwOption, pBuffer, dwBufferLength);
286
287     return S_OK;
288 }
289
290 static const CHAR Agent[] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
291
292 /**************************************************************************
293  *                 ObtainUserAgentString (URLMON.@)
294  */
295 HRESULT WINAPI ObtainUserAgentString(DWORD dwOption, LPSTR pcszUAOut, DWORD *cbSize)
296 {
297     FIXME("(%d, %p, %p): stub\n", dwOption, pcszUAOut, cbSize);
298
299     if(dwOption) {
300       ERR("dwOption: %d, must be zero\n", dwOption);
301     }
302
303     if (sizeof(Agent) < *cbSize)
304         *cbSize = sizeof(Agent);
305     lstrcpynA(pcszUAOut, Agent, *cbSize); 
306
307     return S_OK;
308 }
309
310 /**************************************************************************
311  *                 IsValidURL (URLMON.@)
312  * 
313  * Determines if a specified string is a valid URL.
314  *
315  * PARAMS
316  *  pBC        [I] ignored, must be NULL.
317  *  szURL      [I] string that represents the URL in question.
318  *  dwReserved [I] reserved and must be zero.
319  *
320  * RETURNS
321  *  Success: S_OK.
322  *  Failure: S_FALSE.
323  *  returns E_INVALIDARG if one or more of the args is invalid.
324  *
325  * TODO:
326  *  test functionality against windows to see what a valid URL is.
327  */
328 HRESULT WINAPI IsValidURL(LPBC pBC, LPCWSTR szURL, DWORD dwReserved)
329 {
330     FIXME("(%p, %s, %d): stub\n", pBC, debugstr_w(szURL), dwReserved);
331     
332     if (pBC != NULL || dwReserved != 0)
333         return E_INVALIDARG;
334     
335     return S_OK;
336 }
337
338 /**************************************************************************
339  *                 FaultInIEFeature (URLMON.@)
340  *
341  *  Undocumented.  Appears to be used by native shdocvw.dll.
342  */
343 HRESULT WINAPI FaultInIEFeature( HWND hwnd, uCLSSPEC * pClassSpec,
344                                  QUERYCONTEXT *pQuery, DWORD flags )
345 {
346     FIXME("%p %p %p %08x\n", hwnd, pClassSpec, pQuery, flags);
347     return E_NOTIMPL;
348 }
349
350 /**************************************************************************
351  *                 CoGetClassObjectFromURL (URLMON.@)
352  */
353 HRESULT WINAPI CoGetClassObjectFromURL( REFCLSID rclsid, LPCWSTR szCodeURL, DWORD dwFileVersionMS,
354                                         DWORD dwFileVersionLS, LPCWSTR szContentType,
355                                         LPBINDCTX pBindCtx, DWORD dwClsContext, LPVOID pvReserved,
356                                         REFIID riid, LPVOID *ppv )
357 {
358     FIXME("(%s %s %d %d %s %p %d %p %s %p) Stub!\n", debugstr_guid(rclsid), debugstr_w(szCodeURL),
359         dwFileVersionMS, dwFileVersionLS, debugstr_w(szContentType), pBindCtx, dwClsContext, pvReserved,
360         debugstr_guid(riid), ppv);
361     return E_NOINTERFACE;
362 }
363
364 /***********************************************************************
365  *           ReleaseBindInfo (URLMON.@)
366  *
367  * Release the resources used by the specified BINDINFO structure.
368  *
369  * PARAMS
370  *  pbindinfo [I] BINDINFO to release.
371  *
372  * RETURNS
373  *  Nothing.
374  */
375 void WINAPI ReleaseBindInfo(BINDINFO* pbindinfo)
376 {
377     DWORD size;
378
379     TRACE("(%p)\n", pbindinfo);
380
381     if(!pbindinfo || !(size = pbindinfo->cbSize))
382         return;
383
384     CoTaskMemFree(pbindinfo->szExtraInfo);
385     ReleaseStgMedium(&pbindinfo->stgmedData);
386
387     if(offsetof(BINDINFO, szExtraInfo) < size)
388         CoTaskMemFree(pbindinfo->szCustomVerb);
389
390
391     if(pbindinfo->pUnk && offsetof(BINDINFO, pUnk) < size)
392         IUnknown_Release(pbindinfo->pUnk);
393
394     memset(pbindinfo, 0, size);
395     pbindinfo->cbSize = size;
396 }
397
398 /***********************************************************************
399  *           FindMimeFromData (URLMON.@)
400  *
401  * Determines the Multipurpose Internet Mail Extensions (MIME) type from the data provided.
402  */
403 static BOOL text_html_filter(const BYTE const *b, DWORD size)
404 {
405     int i;
406
407     if(size < 5)
408         return FALSE;
409
410     for(i=0; i < size-5; i++) {
411         if(b[i] == '<'
412            && (b[i+1] == 'h' || b[i+1] == 'H')
413            && (b[i+2] == 't' || b[i+2] == 'T')
414            && (b[i+3] == 'm' || b[i+3] == 'M')
415            && (b[i+4] == 'l' || b[i+4] == 'L'))
416             return TRUE;
417     }
418
419     return FALSE;
420 }
421
422 static BOOL image_gif_filter(const BYTE const *b, DWORD size)
423 {
424     return size >= 6
425         && (b[0] == 'G' || b[0] == 'g')
426         && (b[1] == 'I' || b[1] == 'i')
427         && (b[2] == 'F' || b[2] == 'f')
428         &&  b[3] == '8'
429         && (b[4] == '7' || b[4] == '9')
430         && (b[5] == 'A' || b[5] == 'a');
431 }
432
433 static BOOL image_pjpeg_filter(const BYTE const *b, DWORD size)
434 {
435     return size > 2 && b[0] == 0xff && b[1] == 0xd8;
436 }
437
438 static BOOL image_tiff_filter(const BYTE const *b, DWORD size)
439 {
440     return size > 2 && b[0] == 0x4d && b[1] == 0x4d;
441 }
442
443 static BOOL image_xpng_filter(const BYTE const *b, DWORD size)
444 {
445     static const BYTE xpng_header[] = {0x89,'P','N','G',0x0d,0x0a,0x1a,0x0a};
446     return size > sizeof(xpng_header) && !memcmp(b, xpng_header, sizeof(xpng_header));
447 }
448
449 static BOOL image_bmp_filter(const BYTE const *b, DWORD size)
450 {
451     return size >= 14
452         && b[0] == 0x42 && b[1] == 0x4d
453         && *(const DWORD *)(b+6) == 0;
454 }
455
456 static BOOL video_avi_filter(const BYTE const *b, DWORD size)
457 {
458     return size > 12
459         && b[0] == 'R' && b[1] == 'I' && b[2] == 'F' && b[3] == 'F'
460         && b[8] == 'A' && b[9] == 'V' && b[10] == 'I' && b[11] == 0x20;
461 }
462
463 static BOOL video_mpeg_filter(const BYTE const *b, DWORD size)
464 {
465     return size > 4
466         && !b[0] && !b[1] && b[2] == 0x01
467         && (b[3] == 0xb3 || b[3] == 0xba);
468 }
469
470 static BOOL application_pdf_filter(const BYTE const *b, DWORD size)
471 {
472     return size > 4 && b[0] == 0x25 && b[1] == 0x50 && b[2] == 0x44 && b[3] == 0x46;
473 }
474
475 static BOOL application_xzip_filter(const BYTE const *b, DWORD size)
476 {
477     return size > 2 && b[0] == 0x50 && b[1] == 0x4b;
478 }
479
480 static BOOL application_xgzip_filter(const BYTE const *b, DWORD size)
481 {
482     return size > 2 && b[0] == 0x1f && b[1] == 0x8b;
483 }
484
485 static BOOL application_java_filter(const BYTE const *b, DWORD size)
486 {
487     return size > 4 && b[0] == 0xca && b[1] == 0xfe && b[2] == 0xba && b[3] == 0xbe;
488 }
489
490 static BOOL application_xmsdownload(const BYTE const *b, DWORD size)
491 {
492     return size > 2 && b[0] == 'M' && b[1] == 'Z';
493 }
494
495 static BOOL text_plain_filter(const BYTE const *b, DWORD size)
496 {
497     const BYTE *ptr;
498
499     for(ptr = b; ptr < b+size-1; ptr++) {
500         if(*ptr < 0x20 && *ptr != '\n' && *ptr != '\r' && *ptr != '\t')
501             return FALSE;
502     }
503
504     return TRUE;
505 }
506
507 static BOOL application_octet_stream_filter(const BYTE const *b, DWORD size)
508 {
509     return TRUE;
510 }
511
512 HRESULT WINAPI FindMimeFromData(LPBC pBC, LPCWSTR pwzUrl, LPVOID pBuffer,
513         DWORD cbSize, LPCWSTR pwzMimeProposed, DWORD dwMimeFlags,
514         LPWSTR* ppwzMimeOut, DWORD dwReserved)
515 {
516     TRACE("(%p,%s,%p,%d,%s,0x%x,%p,0x%x)\n", pBC, debugstr_w(pwzUrl), pBuffer, cbSize,
517             debugstr_w(pwzMimeProposed), dwMimeFlags, ppwzMimeOut, dwReserved);
518
519     if(dwMimeFlags)
520         WARN("dwMimeFlags=%08x\n", dwMimeFlags);
521     if(dwReserved)
522         WARN("dwReserved=%d\n", dwReserved);
523
524     /* pBC seams to not be used */
525
526     if(!ppwzMimeOut || (!pwzUrl && !pBuffer))
527         return E_INVALIDARG;
528
529     if(pwzMimeProposed && (!pBuffer || (pBuffer && !cbSize))) {
530         DWORD len;
531
532         if(!pwzMimeProposed)
533             return E_FAIL;
534
535         len = strlenW(pwzMimeProposed)+1;
536         *ppwzMimeOut = CoTaskMemAlloc(len*sizeof(WCHAR));
537         memcpy(*ppwzMimeOut, pwzMimeProposed, len*sizeof(WCHAR));
538         return S_OK;
539     }
540
541     if(pBuffer) {
542         const BYTE const *buf = pBuffer;
543         DWORD len;
544         LPCWSTR ret = NULL;
545         int i;
546
547         static const WCHAR wszTextHtml[] = {'t','e','x','t','/','h','t','m','l',0};
548         static const WCHAR wszImageGif[] = {'i','m','a','g','e','/','g','i','f',0};
549         static const WCHAR wszImagePjpeg[] = {'i','m','a','g','e','/','p','j','p','e','g',0};
550         static const WCHAR wszImageTiff[] = {'i','m','a','g','e','/','t','i','f','f',0};
551         static const WCHAR wszImageXPng[] = {'i','m','a','g','e','/','x','-','p','n','g',0};
552         static const WCHAR wszImageBmp[] = {'i','m','a','g','e','/','b','m','p',0};
553         static const WCHAR wszVideoAvi[] = {'v','i','d','e','o','/','a','v','i',0};
554         static const WCHAR wszVideoMpeg[] = {'v','i','d','e','o','/','m','p','e','g',0};
555         static const WCHAR wszAppPdf[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
556             'p','d','f',0};
557         static const WCHAR wszAppXZip[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
558             'x','-','z','i','p','-','c','o','m','p','r','e','s','s','e','d',0};
559         static const WCHAR wszAppXGzip[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
560             'x','-','g','z','i','p','-','c','o','m','p','r','e','s','s','e','d',0};
561         static const WCHAR wszAppJava[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
562             'j','a','v','a',0};
563         static const WCHAR wszAppXMSDownload[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
564             'x','-','m','s','d','o','w','n','l','o','a','d',0};
565         static const WCHAR wszTextPlain[] = {'t','e','x','t','/','p','l','a','i','n','\0'};
566         static const WCHAR wszAppOctetStream[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
567             'o','c','t','e','t','-','s','t','r','e','a','m','\0'};
568
569         static const struct {
570             LPCWSTR mime;
571             BOOL (*filter)(const BYTE const*,DWORD);
572         } mime_filters[] = {
573             {wszTextHtml,       text_html_filter},
574             {wszImageGif,       image_gif_filter},
575             {wszImagePjpeg,     image_pjpeg_filter},
576             {wszImageTiff,      image_tiff_filter},
577             {wszImageXPng,      image_xpng_filter},
578             {wszImageBmp,       image_bmp_filter},
579             {wszVideoAvi,       video_avi_filter},
580             {wszVideoMpeg,      video_mpeg_filter},
581             {wszAppPdf,         application_pdf_filter},
582             {wszAppXZip,        application_xzip_filter},
583             {wszAppXGzip,       application_xgzip_filter},
584             {wszAppJava,        application_java_filter},
585             {wszAppXMSDownload, application_xmsdownload},
586             {wszTextPlain,      text_plain_filter},
587             {wszAppOctetStream, application_octet_stream_filter}
588         };
589
590         if(!cbSize)
591             return E_FAIL;
592
593         if(pwzMimeProposed && strcmpW(pwzMimeProposed, wszAppOctetStream)) {
594             for(i=0; i < sizeof(mime_filters)/sizeof(*mime_filters); i++) {
595                 if(!strcmpW(pwzMimeProposed, mime_filters[i].mime))
596                     break;
597             }
598
599             if(i == sizeof(mime_filters)/sizeof(*mime_filters)
600                     || mime_filters[i].filter(buf, cbSize)) {
601                 len = strlenW(pwzMimeProposed)+1;
602                 *ppwzMimeOut = CoTaskMemAlloc(len*sizeof(WCHAR));
603                 memcpy(*ppwzMimeOut, pwzMimeProposed, len*sizeof(WCHAR));
604                 return S_OK;
605             }
606         }
607
608         i=0;
609         while(!ret) {
610             if(mime_filters[i].filter(buf, cbSize))
611                 ret = mime_filters[i].mime;
612             i++;
613         }
614
615         TRACE("found %s for data\n"
616               "%02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x\n",
617               debugstr_w(ret), buf[0],buf[1],buf[2],buf[3], buf[4],buf[5],buf[6],buf[7],
618               buf[8],buf[9],buf[10],buf[11], buf[12],buf[13],buf[14],buf[15]);
619
620         if(pwzMimeProposed) {
621             if(i == sizeof(mime_filters)/sizeof(*mime_filters))
622                 ret = pwzMimeProposed;
623
624             /* text/html is a special case */
625             if(!strcmpW(pwzMimeProposed, wszTextHtml) && !strcmpW(ret, wszTextPlain))
626                 ret = wszTextHtml;
627         }
628
629         len = strlenW(ret)+1;
630         *ppwzMimeOut = CoTaskMemAlloc(len*sizeof(WCHAR));
631         memcpy(*ppwzMimeOut, ret, len*sizeof(WCHAR));
632         return S_OK;
633     }
634
635     if(pwzUrl) {
636         HKEY hkey;
637         DWORD res, size;
638         LPCWSTR ptr;
639         WCHAR mime[64];
640
641         static const WCHAR wszContentType[] =
642                 {'C','o','n','t','e','n','t',' ','T','y','p','e','\0'};
643
644         ptr = strrchrW(pwzUrl, '.');
645         if(!ptr)
646             return E_FAIL;
647
648         res = RegOpenKeyW(HKEY_CLASSES_ROOT, ptr, &hkey);
649         if(res != ERROR_SUCCESS)
650             return E_FAIL;
651
652         size = sizeof(mime);
653         res = RegQueryValueExW(hkey, wszContentType, NULL, NULL, (LPBYTE)mime, &size);
654         RegCloseKey(hkey);
655         if(res != ERROR_SUCCESS)
656             return E_FAIL;
657
658         *ppwzMimeOut = CoTaskMemAlloc(size);
659         memcpy(*ppwzMimeOut, mime, size);
660         return S_OK;
661     }
662
663     return E_FAIL;
664 }
665
666 /***********************************************************************
667  * Extract (URLMON.@)
668  */
669 HRESULT WINAPI Extract(void *dest, LPCSTR szCabName)
670 {
671     HRESULT (WINAPI *pExtract)(void *, LPCSTR);
672
673     if (!hCabinet)
674         hCabinet = LoadLibraryA("cabinet.dll");
675
676     if (!hCabinet) return HRESULT_FROM_WIN32(GetLastError());
677     pExtract = (void *)GetProcAddress(hCabinet, "Extract");
678     if (!pExtract) return HRESULT_FROM_WIN32(GetLastError());
679
680     return pExtract(dest, szCabName);
681 }