urlmon_main.c 23.5 KB
Newer Older
1 2 3 4 5
/*
 * UrlMon
 *
 * Copyright (c) 2000 Patrik Stridvall
 *
6 7 8 9 10 11 12 13 14 15 16 17
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22
#include <stdarg.h>

Jacek Caban's avatar
Jacek Caban committed
23
#include "urlmon_main.h"
24

25 26
#include "winreg.h"

27 28
#define NO_SHLWAPI_REG
#include "shlwapi.h"
29
#include "wine/debug.h"
30

31
#include "urlmon.h"
32 33 34

WINE_DEFAULT_DEBUG_CHANNEL(urlmon);

35 36
LONG URLMON_refCount = 0;

37
HINSTANCE URLMON_hInstance = 0;
38
static HMODULE hCabinet = NULL;
39

40 41
static void init_session(BOOL);

42
/***********************************************************************
43
 *		DllMain (URLMON.init)
44
 */
45
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
46
{
47
    TRACE("%p 0x%x %p\n", hinstDLL, fdwReason, fImpLoad);
48 49 50

    switch(fdwReason) {
    case DLL_PROCESS_ATTACH:
51
        DisableThreadLibraryCalls(hinstDLL);
52
        URLMON_hInstance = hinstDLL;
53
        init_session(TRUE);
54 55 56
	break;

    case DLL_PROCESS_DETACH:
57 58 59
        if (hCabinet)
            FreeLibrary(hCabinet);
        hCabinet = NULL;
60
        init_session(FALSE);
61 62 63 64 65 66
        URLMON_hInstance = 0;
	break;
    }
    return TRUE;
}

67 68

/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
69
 *		DllInstall (URLMON.@)
70
 */
71
HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
72
{
73
  FIXME("(%s, %s): stub\n", bInstall?"TRUE":"FALSE",
74 75 76 77 78
	debugstr_w(cmdline));

  return S_OK;
}

79 80 81
/***********************************************************************
 *		DllCanUnloadNow (URLMON.@)
 */
82
HRESULT WINAPI DllCanUnloadNow(void)
83
{
84
    return URLMON_refCount != 0 ? S_FALSE : S_OK;
85 86
}

87 88 89 90


/******************************************************************************
 * Urlmon ClassFactory
91
 */
92
typedef struct {
93
    const IClassFactoryVtbl *lpClassFactoryVtbl;
94 95

    HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
96
} ClassFactory;
97

98
#define CLASSFACTORY(x)  ((IClassFactory*)  &(x)->lpClassFactoryVtbl)
99

100
static HRESULT WINAPI CF_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppv)
101
{
102 103 104 105 106 107 108 109 110
    *ppv = NULL;

    if(IsEqualGUID(riid, &IID_IUnknown)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
        *ppv = iface;
    }else if(IsEqualGUID(riid, &IID_IClassFactory)) {
        TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
        *ppv = iface;
    }
111

112 113
    if(*ppv) {
	IUnknown_AddRef((IUnknown*)*ppv);
114 115 116
	return S_OK;
    }

117
    WARN("(%p)->(%s,%p),not found\n", iface, debugstr_guid(riid), ppv);
118 119 120
    return E_NOINTERFACE;
}

121
static ULONG WINAPI CF_AddRef(IClassFactory *iface)
122
{
123 124
    URLMON_LockModule();
    return 2;
125 126
}

127
static ULONG WINAPI CF_Release(IClassFactory *iface)
128
{
129 130
    URLMON_UnlockModule();
    return 1;
131 132
}

133

134
static HRESULT WINAPI CF_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
135 136
                                        REFIID riid, LPVOID *ppobj)
{
137
    ClassFactory *This = (ClassFactory*)iface;
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    HRESULT hres;
    LPUNKNOWN punk;
    
    TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);

    *ppobj = NULL;
    if(SUCCEEDED(hres = This->pfnCreateInstance(pOuter, (LPVOID *) &punk))) {
        hres = IUnknown_QueryInterface(punk, riid, ppobj);
        IUnknown_Release(punk);
    }
    return hres;
}

static HRESULT WINAPI CF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
{
153 154 155 156 157 158 159
    TRACE("(%d)\n", dolock);

    if (dolock)
	   URLMON_LockModule();
    else
	   URLMON_UnlockModule();

160 161 162
    return S_OK;
}

163
static const IClassFactoryVtbl ClassFactoryVtbl =
164 165 166 167 168 169 170 171
{
    CF_QueryInterface,
    CF_AddRef,
    CF_Release,
    CF_CreateInstance,
    CF_LockServer
};

172 173 174 175 176 177
static const ClassFactory FileProtocolCF =
    { &ClassFactoryVtbl, FileProtocol_Construct};
static const ClassFactory FtpProtocolCF =
    { &ClassFactoryVtbl, FtpProtocol_Construct};
static const ClassFactory HttpProtocolCF =
    { &ClassFactoryVtbl, HttpProtocol_Construct};
178 179
static const ClassFactory HttpSProtocolCF =
    { &ClassFactoryVtbl, HttpSProtocol_Construct};
180 181
static const ClassFactory MkProtocolCF =
    { &ClassFactoryVtbl, MkProtocol_Construct};
182 183 184 185 186 187 188 189 190
static const ClassFactory SecurityManagerCF =
    { &ClassFactoryVtbl, SecManagerImpl_Construct};
static const ClassFactory ZoneManagerCF =
    { &ClassFactoryVtbl, ZoneMgrImpl_Construct};
 
struct object_creation_info
{
    const CLSID *clsid;
    IClassFactory *cf;
191
    LPCWSTR protocol;
192 193
};

194 195 196
static const WCHAR wszFile[] = {'f','i','l','e',0};
static const WCHAR wszFtp[]  = {'f','t','p',0};
static const WCHAR wszHttp[] = {'h','t','t','p',0};
197
static const WCHAR wszHttps[] = {'h','t','t','p','s',0};
198
static const WCHAR wszMk[]   = {'m','k',0};
199

200 201
static const struct object_creation_info object_creation[] =
{
202 203 204
    { &CLSID_FileProtocol,            CLASSFACTORY(&FileProtocolCF),    wszFile },
    { &CLSID_FtpProtocol,             CLASSFACTORY(&FtpProtocolCF),     wszFtp  },
    { &CLSID_HttpProtocol,            CLASSFACTORY(&HttpProtocolCF),    wszHttp },
205
    { &CLSID_HttpSProtocol,           CLASSFACTORY(&HttpSProtocolCF),   wszHttps },
206
    { &CLSID_MkProtocol,              CLASSFACTORY(&MkProtocolCF),      wszMk },
207 208
    { &CLSID_InternetSecurityManager, CLASSFACTORY(&SecurityManagerCF), NULL    },
    { &CLSID_InternetZoneManager,     CLASSFACTORY(&ZoneManagerCF),     NULL    }
209 210
};

211 212 213 214 215 216
static void init_session(BOOL init)
{
    int i;

    for(i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++) {

217 218 219 220
        if(object_creation[i].protocol)
            register_urlmon_namespace(object_creation[i].cf, object_creation[i].clsid,
                                      object_creation[i].protocol, init);
    }
221 222
}

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
/*******************************************************************************
 * DllGetClassObject [URLMON.@]
 * Retrieves class object from a DLL object
 *
 * NOTES
 *    Docs say returns STDAPI
 *
 * PARAMS
 *    rclsid [I] CLSID for the class object
 *    riid   [I] Reference to identifier of interface for class object
 *    ppv    [O] Address of variable to receive interface pointer for riid
 *
 * RETURNS
 *    Success: S_OK
 *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
 *             E_UNEXPECTED
 */

241
HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
242 243 244 245 246 247 248 249
{
    int i;
    
    TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
    
    for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
    {
	if (IsEqualGUID(object_creation[i].clsid, rclsid))
250
	    return IClassFactory_QueryInterface(object_creation[i].cf, riid, ppv);
251 252
    }

253 254
    FIXME("%s: no class found.\n", debugstr_guid(rclsid));
    return CLASS_E_CLASSNOTAVAILABLE;
255 256 257
}


258 259 260
/***********************************************************************
 *		DllRegisterServerEx (URLMON.@)
 */
261
HRESULT WINAPI DllRegisterServerEx(void)
262 263 264 265 266 267
{
    FIXME("(void): stub\n");

    return E_FAIL;
}

268 269 270
/**************************************************************************
 *                 UrlMkSetSessionOption (URLMON.@)
 */
271
HRESULT WINAPI UrlMkSetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
272
 					DWORD Reserved)
273
{
274
    FIXME("(%#x, %p, %#x): stub\n", dwOption, pBuffer, dwBufferLength);
275 276 277 278

    return S_OK;
}

279 280
static const CHAR Agent[] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";

Michael Cardenas's avatar
Michael Cardenas committed
281 282 283
/**************************************************************************
 *                 ObtainUserAgentString (URLMON.@)
 */
284
HRESULT WINAPI ObtainUserAgentString(DWORD dwOption, LPSTR pcszUAOut, DWORD *cbSize)
Michael Cardenas's avatar
Michael Cardenas committed
285
{
286
    FIXME("(%d, %p, %p): stub\n", dwOption, pcszUAOut, cbSize);
Michael Cardenas's avatar
Michael Cardenas committed
287

288 289 290 291 292 293 294
    if (pcszUAOut == NULL || cbSize == NULL)
        return E_INVALIDARG;

    if (*cbSize < sizeof(Agent))
    {
        *cbSize = sizeof(Agent);
        return E_OUTOFMEMORY;
Michael Cardenas's avatar
Michael Cardenas committed
295 296
    }

297 298
    if (sizeof(Agent) < *cbSize)
        *cbSize = sizeof(Agent);
299
    lstrcpynA(pcszUAOut, Agent, *cbSize);
300

301
    return S_OK;
Hidenori Takeshima's avatar
Hidenori Takeshima committed
302
}
303

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
/**************************************************************************
 *                 IsValidURL (URLMON.@)
 * 
 * Determines if a specified string is a valid URL.
 *
 * PARAMS
 *  pBC        [I] ignored, must be NULL.
 *  szURL      [I] string that represents the URL in question.
 *  dwReserved [I] reserved and must be zero.
 *
 * RETURNS
 *  Success: S_OK.
 *  Failure: S_FALSE.
 *  returns E_INVALIDARG if one or more of the args is invalid.
 *
 * TODO:
 *  test functionality against windows to see what a valid URL is.
 */
HRESULT WINAPI IsValidURL(LPBC pBC, LPCWSTR szURL, DWORD dwReserved)
{
324
    FIXME("(%p, %s, %d): stub\n", pBC, debugstr_w(szURL), dwReserved);
325 326 327 328 329 330
    
    if (pBC != NULL || dwReserved != 0)
        return E_INVALIDARG;
    
    return S_OK;
}
331 332 333 334 335 336 337 338 339

/**************************************************************************
 *                 FaultInIEFeature (URLMON.@)
 *
 *  Undocumented.  Appears to be used by native shdocvw.dll.
 */
HRESULT WINAPI FaultInIEFeature( HWND hwnd, uCLSSPEC * pClassSpec,
                                 QUERYCONTEXT *pQuery, DWORD flags )
{
340
    FIXME("%p %p %p %08x\n", hwnd, pClassSpec, pQuery, flags);
341 342
    return E_NOTIMPL;
}
343 344 345 346 347 348 349

/**************************************************************************
 *                 CoGetClassObjectFromURL (URLMON.@)
 */
HRESULT WINAPI CoGetClassObjectFromURL( REFCLSID rclsid, LPCWSTR szCodeURL, DWORD dwFileVersionMS,
                                        DWORD dwFileVersionLS, LPCWSTR szContentType,
                                        LPBINDCTX pBindCtx, DWORD dwClsContext, LPVOID pvReserved,
350
                                        REFIID riid, LPVOID *ppv )
351
{
352
    FIXME("(%s %s %d %d %s %p %d %p %s %p) Stub!\n", debugstr_guid(rclsid), debugstr_w(szCodeURL),
353 354 355 356
	dwFileVersionMS, dwFileVersionLS, debugstr_w(szContentType), pBindCtx, dwClsContext, pvReserved,
	debugstr_guid(riid), ppv);
    return E_NOINTERFACE;
}
357 358 359 360 361 362 363 364 365 366 367 368 369 370

/***********************************************************************
 *           ReleaseBindInfo (URLMON.@)
 *
 * Release the resources used by the specified BINDINFO structure.
 *
 * PARAMS
 *  pbindinfo [I] BINDINFO to release.
 *
 * RETURNS
 *  Nothing.
 */
void WINAPI ReleaseBindInfo(BINDINFO* pbindinfo)
{
371 372
    DWORD size;

373 374
    TRACE("(%p)\n", pbindinfo);

375
    if(!pbindinfo || !(size = pbindinfo->cbSize))
376 377 378
        return;

    CoTaskMemFree(pbindinfo->szExtraInfo);
379 380 381 382
    ReleaseStgMedium(&pbindinfo->stgmedData);

    if(offsetof(BINDINFO, szExtraInfo) < size)
        CoTaskMemFree(pbindinfo->szCustomVerb);
383

384
    if(pbindinfo->pUnk && offsetof(BINDINFO, pUnk) < size)
385
        IUnknown_Release(pbindinfo->pUnk);
386 387 388

    memset(pbindinfo, 0, size);
    pbindinfo->cbSize = size;
389
}
390

391 392 393 394 395 396 397 398 399 400
/***********************************************************************
 *           CopyStgMedium (URLMON.@)
 */
HRESULT WINAPI CopyStgMedium(const STGMEDIUM *src, STGMEDIUM *dst)
{
    TRACE("(%p %p)\n", src, dst);

    if(!src || !dst)
        return E_POINTER;

401
    *dst = *src;
402 403 404 405

    switch(dst->tymed) {
    case TYMED_NULL:
        break;
406 407 408 409 410 411 412
    case TYMED_FILE:
        if(src->u.lpszFileName && !src->pUnkForRelease) {
            DWORD size = (strlenW(src->u.lpszFileName)+1)*sizeof(WCHAR);
            dst->u.lpszFileName = CoTaskMemAlloc(size);
            memcpy(dst->u.lpszFileName, src->u.lpszFileName, size);
        }
        break;
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
    case TYMED_ISTREAM:
        if(dst->u.pstm)
            IStream_AddRef(dst->u.pstm);
        break;
    case TYMED_ISTORAGE:
        if(dst->u.pstg)
            IStorage_AddRef(dst->u.pstg);
        break;
    default:
        FIXME("Unimplemented tymed %d\n", src->tymed);
    }

    if(dst->pUnkForRelease)
        IUnknown_AddRef(dst->pUnkForRelease);

    return S_OK;
}

431 432 433 434
static BOOL text_richtext_filter(const BYTE *b, DWORD size)
{
    return size > 5 && !memcmp(b, "{\\rtf", 5);
}
435

436
static BOOL text_html_filter(const BYTE *b, DWORD size)
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
{
    int i;

    if(size < 5)
        return FALSE;

    for(i=0; i < size-5; i++) {
        if(b[i] == '<'
           && (b[i+1] == 'h' || b[i+1] == 'H')
           && (b[i+2] == 't' || b[i+2] == 'T')
           && (b[i+3] == 'm' || b[i+3] == 'M')
           && (b[i+4] == 'l' || b[i+4] == 'L'))
            return TRUE;
    }

    return FALSE;
}

455 456 457 458 459 460
static BOOL audio_basic_filter(const BYTE *b, DWORD size)
{
    return size > 4
        && b[0] == '.' && b[1] == 's' && b[2] == 'n' && b[3] == 'd';
}

461 462 463 464 465 466 467
static BOOL audio_wav_filter(const BYTE *b, DWORD size)
{
    return size > 12
        && b[0] == 'R' && b[1] == 'I' && b[2] == 'F' && b[3] == 'F'
        && b[8] == 'W' && b[9] == 'A' && b[10] == 'V' && b[11] == 'E';
}

468
static BOOL image_gif_filter(const BYTE *b, DWORD size)
469 470 471 472 473 474 475 476 477 478
{
    return size >= 6
        && (b[0] == 'G' || b[0] == 'g')
        && (b[1] == 'I' || b[1] == 'i')
        && (b[2] == 'F' || b[2] == 'f')
        &&  b[3] == '8'
        && (b[4] == '7' || b[4] == '9')
        && (b[5] == 'A' || b[5] == 'a');
}

479
static BOOL image_pjpeg_filter(const BYTE *b, DWORD size)
480
{
Jacek Caban's avatar
Jacek Caban committed
481
    return size > 2 && b[0] == 0xff && b[1] == 0xd8;
482 483
}

484
static BOOL image_tiff_filter(const BYTE *b, DWORD size)
485
{
Jacek Caban's avatar
Jacek Caban committed
486
    return size > 2 && b[0] == 0x4d && b[1] == 0x4d;
487 488
}

489
static BOOL image_xpng_filter(const BYTE *b, DWORD size)
490 491
{
    static const BYTE xpng_header[] = {0x89,'P','N','G',0x0d,0x0a,0x1a,0x0a};
Jacek Caban's avatar
Jacek Caban committed
492
    return size > sizeof(xpng_header) && !memcmp(b, xpng_header, sizeof(xpng_header));
493 494
}

495
static BOOL image_bmp_filter(const BYTE *b, DWORD size)
496 497
{
    return size >= 14
Jacek Caban's avatar
Jacek Caban committed
498
        && b[0] == 0x42 && b[1] == 0x4d
499
        && *(const DWORD *)(b+6) == 0;
500 501
}

502
static BOOL video_avi_filter(const BYTE *b, DWORD size)
503 504 505 506 507 508
{
    return size > 12
        && b[0] == 'R' && b[1] == 'I' && b[2] == 'F' && b[3] == 'F'
        && b[8] == 'A' && b[9] == 'V' && b[10] == 'I' && b[11] == 0x20;
}

509
static BOOL video_mpeg_filter(const BYTE *b, DWORD size)
510 511 512 513 514 515
{
    return size > 4
        && !b[0] && !b[1] && b[2] == 0x01
        && (b[3] == 0xb3 || b[3] == 0xba);
}

516 517 518 519 520
static BOOL application_postscript_filter(const BYTE *b, DWORD size)
{
    return size > 2 && b[0] == '%' && b[1] == '!';
}

521
static BOOL application_pdf_filter(const BYTE *b, DWORD size)
522 523 524 525
{
    return size > 4 && b[0] == 0x25 && b[1] == 0x50 && b[2] == 0x44 && b[3] == 0x46;
}

526
static BOOL application_xzip_filter(const BYTE *b, DWORD size)
527 528 529 530
{
    return size > 2 && b[0] == 0x50 && b[1] == 0x4b;
}

531
static BOOL application_xgzip_filter(const BYTE *b, DWORD size)
532 533 534 535
{
    return size > 2 && b[0] == 0x1f && b[1] == 0x8b;
}

536
static BOOL application_java_filter(const BYTE *b, DWORD size)
537 538 539 540
{
    return size > 4 && b[0] == 0xca && b[1] == 0xfe && b[2] == 0xba && b[3] == 0xbe;
}

541
static BOOL application_xmsdownload(const BYTE *b, DWORD size)
542 543 544 545
{
    return size > 2 && b[0] == 'M' && b[1] == 'Z';
}

546
static BOOL text_plain_filter(const BYTE *b, DWORD size)
547
{
Jacek Caban's avatar
Jacek Caban committed
548
    const BYTE *ptr;
549

Jacek Caban's avatar
Jacek Caban committed
550
    for(ptr = b; ptr < b+size-1; ptr++) {
551 552 553 554 555 556 557
        if(*ptr < 0x20 && *ptr != '\n' && *ptr != '\r' && *ptr != '\t')
            return FALSE;
    }

    return TRUE;
}

558
static BOOL application_octet_stream_filter(const BYTE *b, DWORD size)
559 560 561 562
{
    return TRUE;
}

563 564 565 566 567
/***********************************************************************
 *           FindMimeFromData (URLMON.@)
 *
 * Determines the Multipurpose Internet Mail Extensions (MIME) type from the data provided.
 */
568 569 570 571
HRESULT WINAPI FindMimeFromData(LPBC pBC, LPCWSTR pwzUrl, LPVOID pBuffer,
        DWORD cbSize, LPCWSTR pwzMimeProposed, DWORD dwMimeFlags,
        LPWSTR* ppwzMimeOut, DWORD dwReserved)
{
572
    TRACE("(%p,%s,%p,%d,%s,0x%x,%p,0x%x)\n", pBC, debugstr_w(pwzUrl), pBuffer, cbSize,
573 574 575
            debugstr_w(pwzMimeProposed), dwMimeFlags, ppwzMimeOut, dwReserved);

    if(dwMimeFlags)
576
        WARN("dwMimeFlags=%08x\n", dwMimeFlags);
577
    if(dwReserved)
578
        WARN("dwReserved=%d\n", dwReserved);
579 580 581 582 583 584

    /* pBC seams to not be used */

    if(!ppwzMimeOut || (!pwzUrl && !pBuffer))
        return E_INVALIDARG;

585
    if(pwzMimeProposed && (!pBuffer || (pBuffer && !cbSize))) {
586 587 588 589 590 591 592 593 594 595 596 597
        DWORD len;

        if(!pwzMimeProposed)
            return E_FAIL;

        len = strlenW(pwzMimeProposed)+1;
        *ppwzMimeOut = CoTaskMemAlloc(len*sizeof(WCHAR));
        memcpy(*ppwzMimeOut, pwzMimeProposed, len*sizeof(WCHAR));
        return S_OK;
    }

    if(pBuffer) {
598
        const BYTE *buf = pBuffer;
599
        DWORD len;
600
        LPCWSTR ret = NULL;
601
        int i;
602

603
        static const WCHAR wszTextHtml[] = {'t','e','x','t','/','h','t','m','l',0};
604
        static const WCHAR wszTextRichtext[] = {'t','e','x','t','/','r','i','c','h','t','e','x','t',0};
605
        static const WCHAR wszAudioBasic[] = {'a','u','d','i','o','/','b','a','s','i','c',0};
606
        static const WCHAR wszAudioWav[] = {'a','u','d','i','o','/','w','a','v',0};
607
        static const WCHAR wszImageGif[] = {'i','m','a','g','e','/','g','i','f',0};
608
        static const WCHAR wszImagePjpeg[] = {'i','m','a','g','e','/','p','j','p','e','g',0};
609
        static const WCHAR wszImageTiff[] = {'i','m','a','g','e','/','t','i','f','f',0};
610
        static const WCHAR wszImageXPng[] = {'i','m','a','g','e','/','x','-','p','n','g',0};
611
        static const WCHAR wszImageBmp[] = {'i','m','a','g','e','/','b','m','p',0};
612
        static const WCHAR wszVideoAvi[] = {'v','i','d','e','o','/','a','v','i',0};
613
        static const WCHAR wszVideoMpeg[] = {'v','i','d','e','o','/','m','p','e','g',0};
614 615
        static const WCHAR wszAppPostscript[] =
            {'a','p','p','l','i','c','a','t','i','o','n','/','p','o','s','t','s','c','r','i','p','t',0};
616 617
        static const WCHAR wszAppPdf[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
            'p','d','f',0};
618 619
        static const WCHAR wszAppXZip[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
            'x','-','z','i','p','-','c','o','m','p','r','e','s','s','e','d',0};
620 621
        static const WCHAR wszAppXGzip[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
            'x','-','g','z','i','p','-','c','o','m','p','r','e','s','s','e','d',0};
622 623
        static const WCHAR wszAppJava[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
            'j','a','v','a',0};
624 625
        static const WCHAR wszAppXMSDownload[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
            'x','-','m','s','d','o','w','n','l','o','a','d',0};
626
        static const WCHAR wszTextPlain[] = {'t','e','x','t','/','p','l','a','i','n','\0'};
627 628 629 630 631
        static const WCHAR wszAppOctetStream[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
            'o','c','t','e','t','-','s','t','r','e','a','m','\0'};

        static const struct {
            LPCWSTR mime;
632
            BOOL (*filter)(const BYTE *,DWORD);
633
        } mime_filters[] = {
634
            {wszTextHtml,       text_html_filter},
635
            {wszTextRichtext,   text_richtext_filter},
636
         /* {wszAudioXAiff,     audio_xaiff_filter}, */
637
            {wszAudioBasic,     audio_basic_filter},
638
            {wszAudioWav,       audio_wav_filter},
639
            {wszImageGif,       image_gif_filter},
640
            {wszImagePjpeg,     image_pjpeg_filter},
641
            {wszImageTiff,      image_tiff_filter},
642
            {wszImageXPng,      image_xpng_filter},
643
         /* {wszImageXBitmap,   image_xbitmap_filter}, */
644
            {wszImageBmp,       image_bmp_filter},
645 646 647
         /* {wszImageXJg,       image_xjg_filter}, */
         /* {wszImageXEmf,      image_xemf_filter}, */
         /* {wszImageXWmf,      image_xwmf_filter}, */
648
            {wszVideoAvi,       video_avi_filter},
649
            {wszVideoMpeg,      video_mpeg_filter},
650
            {wszAppPostscript,  application_postscript_filter},
651 652
         /* {wszAppBase64,      application_base64_filter}, */
         /* {wszAppMacbinhex40, application_macbinhex40_filter}, */
653
            {wszAppPdf,         application_pdf_filter},
654
         /* {wszAppXCompressed, application_xcompressed_filter}, */
655
            {wszAppXZip,        application_xzip_filter},
656
            {wszAppXGzip,       application_xgzip_filter},
657
            {wszAppJava,        application_java_filter},
658
            {wszAppXMSDownload, application_xmsdownload},
659 660 661
            {wszTextPlain,      text_plain_filter},
            {wszAppOctetStream, application_octet_stream_filter}
        };
662 663 664 665

        if(!cbSize)
            return E_FAIL;

666 667 668 669 670 671 672
        if(pwzMimeProposed && strcmpW(pwzMimeProposed, wszAppOctetStream)) {
            for(i=0; i < sizeof(mime_filters)/sizeof(*mime_filters); i++) {
                if(!strcmpW(pwzMimeProposed, mime_filters[i].mime))
                    break;
            }

            if(i == sizeof(mime_filters)/sizeof(*mime_filters)
Jacek Caban's avatar
Jacek Caban committed
673
                    || mime_filters[i].filter(buf, cbSize)) {
674 675 676 677 678 679 680 681
                len = strlenW(pwzMimeProposed)+1;
                *ppwzMimeOut = CoTaskMemAlloc(len*sizeof(WCHAR));
                memcpy(*ppwzMimeOut, pwzMimeProposed, len*sizeof(WCHAR));
                return S_OK;
            }
        }

        i=0;
682
        while(!ret) {
Jacek Caban's avatar
Jacek Caban committed
683
            if(mime_filters[i].filter(buf, cbSize))
684 685
                ret = mime_filters[i].mime;
            i++;
686 687
        }

Jacek Caban's avatar
Jacek Caban committed
688 689 690 691 692
        TRACE("found %s for data\n"
              "%02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x\n",
              debugstr_w(ret), buf[0],buf[1],buf[2],buf[3], buf[4],buf[5],buf[6],buf[7],
              buf[8],buf[9],buf[10],buf[11], buf[12],buf[13],buf[14],buf[15]);

693 694 695 696 697
        if(pwzMimeProposed) {
            if(i == sizeof(mime_filters)/sizeof(*mime_filters))
                ret = pwzMimeProposed;

            /* text/html is a special case */
698
            if(!strcmpW(pwzMimeProposed, wszTextHtml) && !strcmpW(ret, wszTextPlain))
699 700 701
                ret = wszTextHtml;
        }

702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
        len = strlenW(ret)+1;
        *ppwzMimeOut = CoTaskMemAlloc(len*sizeof(WCHAR));
        memcpy(*ppwzMimeOut, ret, len*sizeof(WCHAR));
        return S_OK;
    }

    if(pwzUrl) {
        HKEY hkey;
        DWORD res, size;
        LPCWSTR ptr;
        WCHAR mime[64];

        static const WCHAR wszContentType[] =
                {'C','o','n','t','e','n','t',' ','T','y','p','e','\0'};

        ptr = strrchrW(pwzUrl, '.');
        if(!ptr)
            return E_FAIL;

        res = RegOpenKeyW(HKEY_CLASSES_ROOT, ptr, &hkey);
        if(res != ERROR_SUCCESS)
723
            return HRESULT_FROM_WIN32(res);
724 725 726 727 728

        size = sizeof(mime);
        res = RegQueryValueExW(hkey, wszContentType, NULL, NULL, (LPBYTE)mime, &size);
        RegCloseKey(hkey);
        if(res != ERROR_SUCCESS)
729
            return HRESULT_FROM_WIN32(res);
730 731 732 733 734 735 736 737

        *ppwzMimeOut = CoTaskMemAlloc(size);
        memcpy(*ppwzMimeOut, mime, size);
        return S_OK;
    }

    return E_FAIL;
}
738

739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
/***********************************************************************
 *           GetClassFileOrMime (URLMON.@)
 *
 * Determines the class ID from the bind context, file name or MIME type.
 */
HRESULT WINAPI GetClassFileOrMime(LPBC pBC, LPCWSTR pszFilename,
        LPVOID pBuffer, DWORD cbBuffer, LPCWSTR pszMimeType, DWORD dwReserved,
        CLSID *pclsid)
{
    FIXME("(%p, %s, %p, %d, %p, 0x%08x, %p): stub\n", pBC,
        debugstr_w(pszFilename), pBuffer, cbBuffer, debugstr_w(pszMimeType),
        dwReserved, pclsid);
    return E_NOTIMPL;
}

754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
/***********************************************************************
 * Extract (URLMON.@)
 */
HRESULT WINAPI Extract(void *dest, LPCSTR szCabName)
{
    HRESULT (WINAPI *pExtract)(void *, LPCSTR);

    if (!hCabinet)
        hCabinet = LoadLibraryA("cabinet.dll");

    if (!hCabinet) return HRESULT_FROM_WIN32(GetLastError());
    pExtract = (void *)GetProcAddress(hCabinet, "Extract");
    if (!pExtract) return HRESULT_FROM_WIN32(GetLastError());

    return pExtract(dest, szCabName);
}