urlmon: Added image/tiff mime filter.
[wine/testsucceed.git] / dlls / urlmon / urlmon_main.c
blob044c2f8c0a758b6d49713f91db8be1c65e671377
1 /*
2 * UrlMon
4 * Copyright (c) 2000 Patrik Stridvall
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.
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.
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
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
29 #define NO_SHLWAPI_REG
30 #include "shlwapi.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
34 #include "winuser.h"
35 #include "urlmon.h"
36 #include "urlmon_main.h"
37 #include "ole2.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
41 LONG URLMON_refCount = 0;
43 HINSTANCE URLMON_hInstance = 0;
44 static HMODULE hCabinet = NULL;
46 static void init_session(BOOL);
48 /***********************************************************************
49 * DllMain (URLMON.init)
51 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
53 TRACE("%p 0x%x %p\n", hinstDLL, fdwReason, fImpLoad);
55 switch(fdwReason) {
56 case DLL_PROCESS_ATTACH:
57 DisableThreadLibraryCalls(hinstDLL);
58 URLMON_hInstance = hinstDLL;
59 init_session(TRUE);
60 break;
62 case DLL_PROCESS_DETACH:
63 if (hCabinet)
64 FreeLibrary(hCabinet);
65 hCabinet = NULL;
66 init_session(FALSE);
67 URLMON_hInstance = 0;
68 break;
70 return TRUE;
74 /***********************************************************************
75 * DllInstall (URLMON.@)
77 HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
79 FIXME("(%s, %s): stub\n", bInstall?"TRUE":"FALSE",
80 debugstr_w(cmdline));
82 return S_OK;
85 /***********************************************************************
86 * DllCanUnloadNow (URLMON.@)
88 HRESULT WINAPI DllCanUnloadNow(void)
90 return URLMON_refCount != 0 ? S_FALSE : S_OK;
95 /******************************************************************************
96 * Urlmon ClassFactory
98 typedef struct {
99 const IClassFactoryVtbl *lpClassFactoryVtbl;
101 HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
102 } ClassFactory;
104 #define CLASSFACTORY(x) ((IClassFactory*) &(x)->lpClassFactoryVtbl)
106 static HRESULT WINAPI CF_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppv)
108 *ppv = NULL;
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;
118 if(*ppv) {
119 IUnknown_AddRef((IUnknown*)*ppv);
120 return S_OK;
123 WARN("(%p)->(%s,%p),not found\n", iface, debugstr_guid(riid), ppv);
124 return E_NOINTERFACE;
127 static ULONG WINAPI CF_AddRef(IClassFactory *iface)
129 URLMON_LockModule();
130 return 2;
133 static ULONG WINAPI CF_Release(IClassFactory *iface)
135 URLMON_UnlockModule();
136 return 1;
140 static HRESULT WINAPI CF_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
141 REFIID riid, LPVOID *ppobj)
143 ClassFactory *This = (ClassFactory*)iface;
144 HRESULT hres;
145 LPUNKNOWN punk;
147 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
149 *ppobj = NULL;
150 if(SUCCEEDED(hres = This->pfnCreateInstance(pOuter, (LPVOID *) &punk))) {
151 hres = IUnknown_QueryInterface(punk, riid, ppobj);
152 IUnknown_Release(punk);
154 return hres;
157 static HRESULT WINAPI CF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
159 TRACE("(%d)\n", dolock);
161 if (dolock)
162 URLMON_LockModule();
163 else
164 URLMON_UnlockModule();
166 return S_OK;
169 static const IClassFactoryVtbl ClassFactoryVtbl =
171 CF_QueryInterface,
172 CF_AddRef,
173 CF_Release,
174 CF_CreateInstance,
175 CF_LockServer
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 SecurityManagerCF =
185 { &ClassFactoryVtbl, SecManagerImpl_Construct};
186 static const ClassFactory ZoneManagerCF =
187 { &ClassFactoryVtbl, ZoneMgrImpl_Construct};
189 struct object_creation_info
191 const CLSID *clsid;
192 IClassFactory *cf;
193 LPCWSTR protocol;
196 static const WCHAR wszFile[] = {'f','i','l','e',0};
197 static const WCHAR wszFtp[] = {'f','t','p',0};
198 static const WCHAR wszHttp[] = {'h','t','t','p',0};
200 static const struct object_creation_info object_creation[] =
202 { &CLSID_FileProtocol, CLASSFACTORY(&FileProtocolCF), wszFile },
203 { &CLSID_FtpProtocol, CLASSFACTORY(&FtpProtocolCF), wszFtp },
204 { &CLSID_HttpProtocol, CLASSFACTORY(&HttpProtocolCF), wszHttp },
205 { &CLSID_InternetSecurityManager, CLASSFACTORY(&SecurityManagerCF), NULL },
206 { &CLSID_InternetZoneManager, CLASSFACTORY(&ZoneManagerCF), NULL }
209 static void init_session(BOOL init)
211 IInternetSession *session;
212 int i;
214 CoInternetGetSession(0, &session, 0);
216 for(i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++) {
217 if(object_creation[i].protocol) {
218 if(init)
219 IInternetSession_RegisterNameSpace(session, object_creation[i].cf,
220 object_creation[i].clsid, object_creation[i].protocol, 0, NULL, 0);
221 else
222 IInternetSession_UnregisterNameSpace(session, object_creation[i].cf,
223 object_creation[i].protocol);
227 IInternetSession_Release(session);
230 /*******************************************************************************
231 * DllGetClassObject [URLMON.@]
232 * Retrieves class object from a DLL object
234 * NOTES
235 * Docs say returns STDAPI
237 * PARAMS
238 * rclsid [I] CLSID for the class object
239 * riid [I] Reference to identifier of interface for class object
240 * ppv [O] Address of variable to receive interface pointer for riid
242 * RETURNS
243 * Success: S_OK
244 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
245 * E_UNEXPECTED
248 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
250 int i;
252 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
254 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
256 if (IsEqualGUID(object_creation[i].clsid, rclsid))
257 return IClassFactory_QueryInterface(object_creation[i].cf, riid, ppv);
260 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
261 return CLASS_E_CLASSNOTAVAILABLE;
265 /***********************************************************************
266 * DllRegisterServerEx (URLMON.@)
268 HRESULT WINAPI DllRegisterServerEx(void)
270 FIXME("(void): stub\n");
272 return E_FAIL;
275 /**************************************************************************
276 * UrlMkSetSessionOption (URLMON.@)
278 HRESULT WINAPI UrlMkSetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
279 DWORD Reserved)
281 FIXME("(%#x, %p, %#x): stub\n", dwOption, pBuffer, dwBufferLength);
283 return S_OK;
286 static const CHAR Agent[] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
288 /**************************************************************************
289 * ObtainUserAgentString (URLMON.@)
291 HRESULT WINAPI ObtainUserAgentString(DWORD dwOption, LPSTR pcszUAOut, DWORD *cbSize)
293 FIXME("(%d, %p, %p): stub\n", dwOption, pcszUAOut, cbSize);
295 if(dwOption) {
296 ERR("dwOption: %d, must be zero\n", dwOption);
299 if (sizeof(Agent) < *cbSize)
300 *cbSize = sizeof(Agent);
301 lstrcpynA(pcszUAOut, Agent, *cbSize);
303 return S_OK;
306 HRESULT WINAPI CoInternetCompareUrl(LPCWSTR pwzUrl1, LPCWSTR pwzUrl2, DWORD dwCompareFlags)
308 TRACE("(%s,%s,%08x)\n", debugstr_w(pwzUrl1), debugstr_w(pwzUrl2), dwCompareFlags);
309 return UrlCompareW(pwzUrl1, pwzUrl2, dwCompareFlags)==0?S_OK:S_FALSE;
312 /**************************************************************************
313 * IsValidURL (URLMON.@)
315 * Determines if a specified string is a valid URL.
317 * PARAMS
318 * pBC [I] ignored, must be NULL.
319 * szURL [I] string that represents the URL in question.
320 * dwReserved [I] reserved and must be zero.
322 * RETURNS
323 * Success: S_OK.
324 * Failure: S_FALSE.
325 * returns E_INVALIDARG if one or more of the args is invalid.
327 * TODO:
328 * test functionality against windows to see what a valid URL is.
330 HRESULT WINAPI IsValidURL(LPBC pBC, LPCWSTR szURL, DWORD dwReserved)
332 FIXME("(%p, %s, %d): stub\n", pBC, debugstr_w(szURL), dwReserved);
334 if (pBC != NULL || dwReserved != 0)
335 return E_INVALIDARG;
337 return S_OK;
340 /**************************************************************************
341 * FaultInIEFeature (URLMON.@)
343 * Undocumented. Appears to be used by native shdocvw.dll.
345 HRESULT WINAPI FaultInIEFeature( HWND hwnd, uCLSSPEC * pClassSpec,
346 QUERYCONTEXT *pQuery, DWORD flags )
348 FIXME("%p %p %p %08x\n", hwnd, pClassSpec, pQuery, flags);
349 return E_NOTIMPL;
352 /**************************************************************************
353 * CoGetClassObjectFromURL (URLMON.@)
355 HRESULT WINAPI CoGetClassObjectFromURL( REFCLSID rclsid, LPCWSTR szCodeURL, DWORD dwFileVersionMS,
356 DWORD dwFileVersionLS, LPCWSTR szContentType,
357 LPBINDCTX pBindCtx, DWORD dwClsContext, LPVOID pvReserved,
358 REFIID riid, LPVOID *ppv )
360 FIXME("(%s %s %d %d %s %p %d %p %s %p) Stub!\n", debugstr_guid(rclsid), debugstr_w(szCodeURL),
361 dwFileVersionMS, dwFileVersionLS, debugstr_w(szContentType), pBindCtx, dwClsContext, pvReserved,
362 debugstr_guid(riid), ppv);
363 return E_NOINTERFACE;
366 /***********************************************************************
367 * ReleaseBindInfo (URLMON.@)
369 * Release the resources used by the specified BINDINFO structure.
371 * PARAMS
372 * pbindinfo [I] BINDINFO to release.
374 * RETURNS
375 * Nothing.
377 void WINAPI ReleaseBindInfo(BINDINFO* pbindinfo)
379 DWORD size;
381 TRACE("(%p)\n", pbindinfo);
383 if(!pbindinfo || !(size = pbindinfo->cbSize))
384 return;
386 CoTaskMemFree(pbindinfo->szExtraInfo);
387 ReleaseStgMedium(&pbindinfo->stgmedData);
389 if(offsetof(BINDINFO, szExtraInfo) < size)
390 CoTaskMemFree(pbindinfo->szCustomVerb);
393 if(pbindinfo->pUnk && offsetof(BINDINFO, pUnk) < size)
394 IUnknown_Release(pbindinfo->pUnk);
396 memset(pbindinfo, 0, size);
397 pbindinfo->cbSize = size;
400 /***********************************************************************
401 * FindMimeFromData (URLMON.@)
403 * Determines the Multipurpose Internet Mail Extensions (MIME) type from the data provided.
405 static BOOL text_html_filter(LPVOID buf, DWORD size)
407 const char *b = buf;
408 int i;
410 if(size < 5)
411 return FALSE;
413 for(i=0; i < size-5; i++) {
414 if(b[i] == '<'
415 && (b[i+1] == 'h' || b[i+1] == 'H')
416 && (b[i+2] == 't' || b[i+2] == 'T')
417 && (b[i+3] == 'm' || b[i+3] == 'M')
418 && (b[i+4] == 'l' || b[i+4] == 'L'))
419 return TRUE;
422 return FALSE;
425 static BOOL image_gif_filter(LPVOID buf, DWORD size)
427 const BYTE const *b = buf;
429 return size >= 6
430 && (b[0] == 'G' || b[0] == 'g')
431 && (b[1] == 'I' || b[1] == 'i')
432 && (b[2] == 'F' || b[2] == 'f')
433 && b[3] == '8'
434 && (b[4] == '7' || b[4] == '9')
435 && (b[5] == 'A' || b[5] == 'a');
438 static BOOL image_pjpeg_filter(LPVOID buf, DWORD size)
440 return size > 2 && *(BYTE*)buf == 0xff && *((BYTE*)buf+1) == 0xd8;
443 static BOOL image_tiff_filter(LPVOID buf, DWORD size)
445 return size > 2 && *(WORD*)buf == 0x4d4d;
448 static BOOL image_xpng_filter(LPVOID buf, DWORD size)
450 static const BYTE xpng_header[] = {0x89,'P','N','G',0x0d,0x0a,0x1a,0x0a};
451 return size > sizeof(xpng_header) && !memcmp(buf, xpng_header, sizeof(xpng_header));
454 static BOOL image_bmp_filter(LPVOID buf, DWORD size)
456 return size >= 14
457 && *(BYTE*)buf == 0x42
458 && *((BYTE*)buf+1) == 0x4d
459 && *(DWORD*)((BYTE*)buf+6) == 0;
462 static BOOL text_plain_filter(LPVOID buf, DWORD size)
464 UCHAR *ptr;
466 for(ptr = buf; ptr < (UCHAR*)buf+size-1; ptr++) {
467 if(*ptr < 0x20 && *ptr != '\n' && *ptr != '\r' && *ptr != '\t')
468 return FALSE;
471 return TRUE;
474 static BOOL application_octet_stream_filter(LPVOID buf, DWORD size)
476 return TRUE;
479 HRESULT WINAPI FindMimeFromData(LPBC pBC, LPCWSTR pwzUrl, LPVOID pBuffer,
480 DWORD cbSize, LPCWSTR pwzMimeProposed, DWORD dwMimeFlags,
481 LPWSTR* ppwzMimeOut, DWORD dwReserved)
483 TRACE("(%p,%s,%p,%d,%s,0x%x,%p,0x%x)\n", pBC, debugstr_w(pwzUrl), pBuffer, cbSize,
484 debugstr_w(pwzMimeProposed), dwMimeFlags, ppwzMimeOut, dwReserved);
486 if(dwMimeFlags)
487 WARN("dwMimeFlags=%08x\n", dwMimeFlags);
488 if(dwReserved)
489 WARN("dwReserved=%d\n", dwReserved);
491 /* pBC seams to not be used */
493 if(!ppwzMimeOut || (!pwzUrl && !pBuffer))
494 return E_INVALIDARG;
496 if(pwzMimeProposed && (!pBuffer || (pBuffer && !cbSize))) {
497 DWORD len;
499 if(!pwzMimeProposed)
500 return E_FAIL;
502 len = strlenW(pwzMimeProposed)+1;
503 *ppwzMimeOut = CoTaskMemAlloc(len*sizeof(WCHAR));
504 memcpy(*ppwzMimeOut, pwzMimeProposed, len*sizeof(WCHAR));
505 return S_OK;
508 if(pBuffer) {
509 DWORD len;
510 LPCWSTR ret = NULL;
511 int i;
513 static const WCHAR wszTextHtml[] = {'t','e','x','t','/','h','t','m','l',0};
514 static const WCHAR wszImageGif[] = {'i','m','a','g','e','/','g','i','f',0};
515 static const WCHAR wszImagePjpeg[] = {'i','m','a','g','e','/','p','j','p','e','g',0};
516 static const WCHAR wszImageTiff[] = {'i','m','a','g','e','/','t','i','f','f',0};
517 static const WCHAR wszImageXPng[] = {'i','m','a','g','e','/','x','-','p','n','g',0};
518 static const WCHAR wszImageBmp[] = {'i','m','a','g','e','/','b','m','p',0};
519 static const WCHAR wszTextPlain[] = {'t','e','x','t','/','p','l','a','i','n','\0'};
520 static const WCHAR wszAppOctetStream[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
521 'o','c','t','e','t','-','s','t','r','e','a','m','\0'};
523 static const struct {
524 LPCWSTR mime;
525 BOOL (*filter)(LPVOID,DWORD);
526 } mime_filters[] = {
527 {wszTextHtml, text_html_filter},
528 {wszImageGif, image_gif_filter},
529 {wszImagePjpeg, image_pjpeg_filter},
530 {wszImageTiff, image_tiff_filter},
531 {wszImageXPng, image_xpng_filter},
532 {wszImageBmp, image_bmp_filter},
533 {wszTextPlain, text_plain_filter},
534 {wszAppOctetStream, application_octet_stream_filter}
537 if(!cbSize)
538 return E_FAIL;
540 if(pwzMimeProposed && strcmpW(pwzMimeProposed, wszAppOctetStream)) {
541 for(i=0; i < sizeof(mime_filters)/sizeof(*mime_filters); i++) {
542 if(!strcmpW(pwzMimeProposed, mime_filters[i].mime))
543 break;
546 if(i == sizeof(mime_filters)/sizeof(*mime_filters)
547 || mime_filters[i].filter(pBuffer, cbSize)) {
548 len = strlenW(pwzMimeProposed)+1;
549 *ppwzMimeOut = CoTaskMemAlloc(len*sizeof(WCHAR));
550 memcpy(*ppwzMimeOut, pwzMimeProposed, len*sizeof(WCHAR));
551 return S_OK;
555 i=0;
556 while(!ret) {
557 if(mime_filters[i].filter(pBuffer, cbSize))
558 ret = mime_filters[i].mime;
559 i++;
562 if(pwzMimeProposed) {
563 if(i == sizeof(mime_filters)/sizeof(*mime_filters))
564 ret = pwzMimeProposed;
566 /* text/html is a special case */
567 if(!strcmpW(pwzMimeProposed, wszTextHtml) && !strcmpW(ret, wszTextPlain))
568 ret = wszTextHtml;
571 len = strlenW(ret)+1;
572 *ppwzMimeOut = CoTaskMemAlloc(len*sizeof(WCHAR));
573 memcpy(*ppwzMimeOut, ret, len*sizeof(WCHAR));
574 return S_OK;
577 if(pwzUrl) {
578 HKEY hkey;
579 DWORD res, size;
580 LPCWSTR ptr;
581 WCHAR mime[64];
583 static const WCHAR wszContentType[] =
584 {'C','o','n','t','e','n','t',' ','T','y','p','e','\0'};
586 ptr = strrchrW(pwzUrl, '.');
587 if(!ptr)
588 return E_FAIL;
590 res = RegOpenKeyW(HKEY_CLASSES_ROOT, ptr, &hkey);
591 if(res != ERROR_SUCCESS)
592 return E_FAIL;
594 size = sizeof(mime);
595 res = RegQueryValueExW(hkey, wszContentType, NULL, NULL, (LPBYTE)mime, &size);
596 RegCloseKey(hkey);
597 if(res != ERROR_SUCCESS)
598 return E_FAIL;
600 *ppwzMimeOut = CoTaskMemAlloc(size);
601 memcpy(*ppwzMimeOut, mime, size);
602 return S_OK;
605 return E_FAIL;
608 /***********************************************************************
609 * Extract (URLMON.@)
611 HRESULT WINAPI Extract(void *dest, LPCSTR szCabName)
613 HRESULT (WINAPI *pExtract)(void *, LPCSTR);
615 if (!hCabinet)
616 hCabinet = LoadLibraryA("cabinet.dll");
618 if (!hCabinet) return HRESULT_FROM_WIN32(GetLastError());
619 pExtract = (void *)GetProcAddress(hCabinet, "Extract");
620 if (!pExtract) return HRESULT_FROM_WIN32(GetLastError());
622 return pExtract(dest, szCabName);