urlmon_main.c 33.2 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 "advpub.h"
30
#include "initguid.h"
31

32
#include "wine/debug.h"
33

34
#include "urlmon.h"
35 36 37

WINE_DEFAULT_DEBUG_CHANNEL(urlmon);

38 39
DEFINE_GUID(CLSID_CUri, 0xDF2FCE13, 0x25EC, 0x45BB, 0x9D,0x4C, 0xCE,0xCD,0x47,0xC2,0x43,0x0C);

40 41
LONG URLMON_refCount = 0;

42
static HMODULE hCabinet = NULL;
43
static DWORD urlmon_tls = TLS_OUT_OF_INDEXES;
44

45
static void init_session(void);
46

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
static struct list tls_list = LIST_INIT(tls_list);

static CRITICAL_SECTION tls_cs;
static CRITICAL_SECTION_DEBUG tls_cs_dbg =
{
    0, 0, &tls_cs,
    { &tls_cs_dbg.ProcessLocksList, &tls_cs_dbg.ProcessLocksList },
      0, 0, { (DWORD_PTR)(__FILE__ ": tls") }
};

static CRITICAL_SECTION tls_cs = { &tls_cs_dbg, -1, 0, 0, 0, 0 };

tls_data_t *get_tls_data(void)
{
    tls_data_t *data;

63
    if(urlmon_tls == TLS_OUT_OF_INDEXES) {
64
        DWORD tls = TlsAlloc();
65 66 67 68
        if(tls == TLS_OUT_OF_INDEXES)
            return NULL;

        tls = InterlockedCompareExchange((LONG*)&urlmon_tls, tls, TLS_OUT_OF_INDEXES);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
        if(tls != urlmon_tls)
            TlsFree(tls);
    }

    data = TlsGetValue(urlmon_tls);
    if(!data) {
        data = heap_alloc_zero(sizeof(tls_data_t));
        if(!data)
            return NULL;

        EnterCriticalSection(&tls_cs);
        list_add_tail(&tls_list, &data->entry);
        LeaveCriticalSection(&tls_cs);

        TlsSetValue(urlmon_tls, data);
    }

    return data;
}

static void free_tls_list(void)
{
    tls_data_t *data;

93
    if(urlmon_tls == TLS_OUT_OF_INDEXES)
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        return;

    while(!list_empty(&tls_list)) {
        data = LIST_ENTRY(list_head(&tls_list), tls_data_t, entry);
        list_remove(&data->entry);
        heap_free(data);
    }

    TlsFree(urlmon_tls);
}

static void detach_thread(void)
{
    tls_data_t *data;

109
    if(urlmon_tls == TLS_OUT_OF_INDEXES)
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
        return;

    data = TlsGetValue(urlmon_tls);
    if(!data)
        return;

    EnterCriticalSection(&tls_cs);
    list_remove(&data->entry);
    LeaveCriticalSection(&tls_cs);

    if(data->notif_hwnd) {
        WARN("notif_hwnd not destroyed\n");
        DestroyWindow(data->notif_hwnd);
    }

    heap_free(data);
}

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
static void process_detach(void)
{
    HINTERNET internet_session;

    internet_session = get_internet_session(NULL);
    if(internet_session)
        InternetCloseHandle(internet_session);

    if (hCabinet)
        FreeLibrary(hCabinet);

    free_session();
    free_tls_list();
}

143
/***********************************************************************
144
 *		DllMain (URLMON.init)
145
 */
146
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
147
{
148
    TRACE("%p 0x%x %p\n", hinstDLL, fdwReason, fImpLoad);
149

150 151
    URLMON_DllMain( hinstDLL, fdwReason, fImpLoad );

152 153
    switch(fdwReason) {
    case DLL_PROCESS_ATTACH:
154
        init_session();
155
        break;
156 157

    case DLL_PROCESS_DETACH:
158
        process_detach();
159
        DeleteCriticalSection(&tls_cs);
160
        break;
161 162 163 164

    case DLL_THREAD_DETACH:
        detach_thread();
        break;
165 166 167 168
    }
    return TRUE;
}

169 170

/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
171
 *		DllInstall (URLMON.@)
172
 */
173
HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
174
{
175
  FIXME("(%s, %s): stub\n", bInstall?"TRUE":"FALSE",
176 177 178 179 180
	debugstr_w(cmdline));

  return S_OK;
}

181 182 183
/***********************************************************************
 *		DllCanUnloadNow (URLMON.@)
 */
184
HRESULT WINAPI DllCanUnloadNow(void)
185
{
186
    return URLMON_refCount != 0 ? S_FALSE : S_OK;
187 188
}

189 190 191 192


/******************************************************************************
 * Urlmon ClassFactory
193
 */
194
typedef struct {
195
    IClassFactory IClassFactory_iface;
196 197

    HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
198
} ClassFactory;
199

200 201 202 203
static inline ClassFactory *impl_from_IClassFactory(IClassFactory *iface)
{
    return CONTAINING_RECORD(iface, ClassFactory, IClassFactory_iface);
}
204

205
static HRESULT WINAPI CF_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppv)
206
{
207 208 209 210 211 212 213 214 215
    *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;
    }
216

217 218
    if(*ppv) {
	IUnknown_AddRef((IUnknown*)*ppv);
219 220 221
	return S_OK;
    }

222
    WARN("(%p)->(%s,%p),not found\n", iface, debugstr_guid(riid), ppv);
223 224 225
    return E_NOINTERFACE;
}

226
static ULONG WINAPI CF_AddRef(IClassFactory *iface)
227
{
228 229
    URLMON_LockModule();
    return 2;
230 231
}

232
static ULONG WINAPI CF_Release(IClassFactory *iface)
233
{
234 235
    URLMON_UnlockModule();
    return 1;
236 237
}

238

239
static HRESULT WINAPI CF_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
240 241
                                        REFIID riid, LPVOID *ppobj)
{
242
    ClassFactory *This = impl_from_IClassFactory(iface);
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    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)
{
258 259 260 261 262 263 264
    TRACE("(%d)\n", dolock);

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

265 266 267
    return S_OK;
}

268
static const IClassFactoryVtbl ClassFactoryVtbl =
269 270 271 272 273 274 275 276
{
    CF_QueryInterface,
    CF_AddRef,
    CF_Release,
    CF_CreateInstance,
    CF_LockServer
};

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
static ClassFactory FileProtocolCF =
    { { &ClassFactoryVtbl }, FileProtocol_Construct};
static ClassFactory FtpProtocolCF =
    { { &ClassFactoryVtbl }, FtpProtocol_Construct};
static ClassFactory GopherProtocolCF =
    { { &ClassFactoryVtbl }, GopherProtocol_Construct};
static ClassFactory HttpProtocolCF =
    { { &ClassFactoryVtbl }, HttpProtocol_Construct};
static ClassFactory HttpSProtocolCF =
    { { &ClassFactoryVtbl }, HttpSProtocol_Construct};
static ClassFactory MkProtocolCF =
    { { &ClassFactoryVtbl }, MkProtocol_Construct};
static ClassFactory SecurityManagerCF =
    { { &ClassFactoryVtbl }, SecManagerImpl_Construct};
static ClassFactory ZoneManagerCF =
    { { &ClassFactoryVtbl }, ZoneMgrImpl_Construct};
static ClassFactory StdURLMonikerCF =
    { { &ClassFactoryVtbl }, StdURLMoniker_Construct};
static ClassFactory MimeFilterCF =
    { { &ClassFactoryVtbl }, MimeFilter_Construct};
297 298
static ClassFactory CUriCF =
    { { &ClassFactoryVtbl }, Uri_Construct};
299

300 301 302 303
struct object_creation_info
{
    const CLSID *clsid;
    IClassFactory *cf;
304
    LPCWSTR protocol;
305 306
};

307 308
static const WCHAR wszFile[] = {'f','i','l','e',0};
static const WCHAR wszFtp[]  = {'f','t','p',0};
309
static const WCHAR wszGopher[]  = {'g','o','p','h','e','r',0};
310
static const WCHAR wszHttp[] = {'h','t','t','p',0};
311
static const WCHAR wszHttps[] = {'h','t','t','p','s',0};
312
static const WCHAR wszMk[]   = {'m','k',0};
313

314 315
static const struct object_creation_info object_creation[] =
{
316 317 318 319 320 321 322 323 324
    { &CLSID_FileProtocol,            &FileProtocolCF.IClassFactory_iface,    wszFile },
    { &CLSID_FtpProtocol,             &FtpProtocolCF.IClassFactory_iface,     wszFtp  },
    { &CLSID_GopherProtocol,          &GopherProtocolCF.IClassFactory_iface,  wszGopher },
    { &CLSID_HttpProtocol,            &HttpProtocolCF.IClassFactory_iface,    wszHttp },
    { &CLSID_HttpSProtocol,           &HttpSProtocolCF.IClassFactory_iface,   wszHttps },
    { &CLSID_MkProtocol,              &MkProtocolCF.IClassFactory_iface,      wszMk },
    { &CLSID_InternetSecurityManager, &SecurityManagerCF.IClassFactory_iface, NULL    },
    { &CLSID_InternetZoneManager,     &ZoneManagerCF.IClassFactory_iface,     NULL    },
    { &CLSID_StdURLMoniker,           &StdURLMonikerCF.IClassFactory_iface,   NULL    },
325 326
    { &CLSID_DeCompMimeFilter,        &MimeFilterCF.IClassFactory_iface,      NULL    },
    { &CLSID_CUri,                    &CUriCF.IClassFactory_iface,            NULL    }
327 328
};

329
static void init_session(void)
330
{
331
    unsigned int i;
332 333

    for(i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++) {
334
        if(object_creation[i].protocol)
335 336
            register_namespace(object_creation[i].cf, object_creation[i].clsid,
                                      object_creation[i].protocol, TRUE);
337
    }
338 339
}

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
/*******************************************************************************
 * 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
 */

358
HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
359
{
360
    unsigned int i;
361
    HRESULT hr;
362 363 364 365 366 367
    
    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))
368
	    return IClassFactory_QueryInterface(object_creation[i].cf, riid, ppv);
369 370
    }

371 372 373 374
    hr = URLMON_DllGetClassObject(rclsid, riid, ppv);
    if(SUCCEEDED(hr))
        return hr;

375 376
    FIXME("%s: no class found.\n", debugstr_guid(rclsid));
    return CLASS_E_CLASSNOTAVAILABLE;
377 378
}

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
static HRESULT register_inf(BOOL doregister)
{
    HRESULT (WINAPI *pRegInstall)(HMODULE hm, LPCSTR pszSection, const STRTABLEA* pstTable);
    HMODULE hAdvpack;

    static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};

    hAdvpack = LoadLibraryW(wszAdvpack);
    pRegInstall = (void *)GetProcAddress(hAdvpack, "RegInstall");

    return pRegInstall(hProxyDll, doregister ? "RegisterDll" : "UnregisterDll", NULL);
}

/***********************************************************************
 *		DllRegisterServer (URLMON.@)
 */
HRESULT WINAPI DllRegisterServer(void)
{
    HRESULT hr;

    TRACE("\n");

    hr = URLMON_DllRegisterServer();
    return SUCCEEDED(hr) ? register_inf(TRUE) : hr;
}

/***********************************************************************
 *		DllUnregisterServer (URLMON.@)
 */
HRESULT WINAPI DllUnregisterServer(void)
{
    HRESULT hr;

    TRACE("\n");

    hr = URLMON_DllUnregisterServer();
    return SUCCEEDED(hr) ? register_inf(FALSE) : hr;
}
417

418 419 420
/***********************************************************************
 *		DllRegisterServerEx (URLMON.@)
 */
421
HRESULT WINAPI DllRegisterServerEx(void)
422 423 424 425 426 427
{
    FIXME("(void): stub\n");

    return E_FAIL;
}

428 429 430 431 432 433
/**************************************************************************
 *                 IsValidURL (URLMON.@)
 * 
 * Determines if a specified string is a valid URL.
 *
 * PARAMS
434
 *  pBC        [I] ignored, should be NULL.
435 436 437 438 439 440 441 442 443 444 445 446 447
 *  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)
{
448
    FIXME("(%p, %s, %d): stub\n", pBC, debugstr_w(szURL), dwReserved);
449

450
    if (dwReserved || !szURL)
451
        return E_INVALIDARG;
452

453 454
    return S_OK;
}
455 456 457 458 459 460 461 462 463

/**************************************************************************
 *                 FaultInIEFeature (URLMON.@)
 *
 *  Undocumented.  Appears to be used by native shdocvw.dll.
 */
HRESULT WINAPI FaultInIEFeature( HWND hwnd, uCLSSPEC * pClassSpec,
                                 QUERYCONTEXT *pQuery, DWORD flags )
{
464
    FIXME("%p %p %p %08x\n", hwnd, pClassSpec, pQuery, flags);
465 466
    return E_NOTIMPL;
}
467 468 469 470 471 472 473

/**************************************************************************
 *                 CoGetClassObjectFromURL (URLMON.@)
 */
HRESULT WINAPI CoGetClassObjectFromURL( REFCLSID rclsid, LPCWSTR szCodeURL, DWORD dwFileVersionMS,
                                        DWORD dwFileVersionLS, LPCWSTR szContentType,
                                        LPBINDCTX pBindCtx, DWORD dwClsContext, LPVOID pvReserved,
474
                                        REFIID riid, LPVOID *ppv )
475
{
476
    FIXME("(%s %s %d %d %s %p %d %p %s %p) Stub!\n", debugstr_guid(rclsid), debugstr_w(szCodeURL),
477 478 479 480
	dwFileVersionMS, dwFileVersionLS, debugstr_w(szContentType), pBindCtx, dwClsContext, pvReserved,
	debugstr_guid(riid), ppv);
    return E_NOINTERFACE;
}
481 482 483 484 485 486 487 488 489 490 491 492 493 494

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

497 498
    TRACE("(%p)\n", pbindinfo);

499
    if(!pbindinfo || !(size = pbindinfo->cbSize))
500 501 502
        return;

    CoTaskMemFree(pbindinfo->szExtraInfo);
503 504 505 506
    ReleaseStgMedium(&pbindinfo->stgmedData);

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

508
    if(pbindinfo->pUnk && offsetof(BINDINFO, pUnk) < size)
509
        IUnknown_Release(pbindinfo->pUnk);
510 511 512

    memset(pbindinfo, 0, size);
    pbindinfo->cbSize = size;
513
}
514

515 516 517 518 519 520 521 522 523 524
/***********************************************************************
 *           CopyStgMedium (URLMON.@)
 */
HRESULT WINAPI CopyStgMedium(const STGMEDIUM *src, STGMEDIUM *dst)
{
    TRACE("(%p %p)\n", src, dst);

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

525
    *dst = *src;
526 527 528 529

    switch(dst->tymed) {
    case TYMED_NULL:
        break;
530 531 532 533
    case TYMED_FILE:
        if(src->u.lpszFileName && !src->pUnkForRelease) {
            DWORD size = (strlenW(src->u.lpszFileName)+1)*sizeof(WCHAR);
            dst->u.lpszFileName = CoTaskMemAlloc(size);
534 535
            if(!dst->u.lpszFileName)
                return E_OUTOFMEMORY;
536 537 538
            memcpy(dst->u.lpszFileName, src->u.lpszFileName, size);
        }
        break;
539 540 541 542 543 544 545 546
    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;
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
    case TYMED_HGLOBAL:
        if(dst->u.hGlobal) {
            SIZE_T size = GlobalSize(src->u.hGlobal);
            char *src_ptr, *dst_ptr;

            dst->u.hGlobal = GlobalAlloc(GMEM_FIXED, size);
            if(!dst->u.hGlobal)
                return E_OUTOFMEMORY;
            dst_ptr = GlobalLock(dst->u.hGlobal);
            src_ptr = GlobalLock(src->u.hGlobal);
            memcpy(dst_ptr, src_ptr, size);
            GlobalUnlock(src_ptr);
            GlobalUnlock(dst_ptr);
        }
        break;
562 563 564 565 566 567 568 569 570 571
    default:
        FIXME("Unimplemented tymed %d\n", src->tymed);
    }

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

    return S_OK;
}

572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
/***********************************************************************
 *           CopyBindInfo (URLMON.@)
 */
HRESULT WINAPI CopyBindInfo(const BINDINFO *pcbiSrc, BINDINFO *pcbiDest)
{
    DWORD size;
    HRESULT hres;

    TRACE("(%p %p)\n", pcbiSrc, pcbiDest);

    if(!pcbiSrc || !pcbiDest)
        return E_POINTER;
    if(!pcbiSrc->cbSize || !pcbiDest->cbSize)
        return E_INVALIDARG;

    size = pcbiDest->cbSize;
    if(size > pcbiSrc->cbSize) {
        memcpy(pcbiDest, pcbiSrc, pcbiSrc->cbSize);
        memset((char*)pcbiDest+pcbiSrc->cbSize, 0, size-pcbiSrc->cbSize);
    } else {
        memcpy(pcbiDest, pcbiSrc, size);
    }
    pcbiDest->cbSize = size;

    size = FIELD_OFFSET(BINDINFO, szExtraInfo)+sizeof(void*);
    if(pcbiSrc->cbSize>=size && pcbiDest->cbSize>=size && pcbiSrc->szExtraInfo) {
        size = (strlenW(pcbiSrc->szExtraInfo)+1)*sizeof(WCHAR);
        pcbiDest->szExtraInfo = CoTaskMemAlloc(size);
        if(!pcbiDest->szExtraInfo)
            return E_OUTOFMEMORY;
        memcpy(pcbiDest->szExtraInfo, pcbiSrc->szExtraInfo, size);
    }

    size = FIELD_OFFSET(BINDINFO, stgmedData)+sizeof(STGMEDIUM);
    if(pcbiSrc->cbSize>=size && pcbiDest->cbSize>=size) {
        hres = CopyStgMedium(&pcbiSrc->stgmedData, &pcbiDest->stgmedData);
        if(FAILED(hres)) {
            CoTaskMemFree(pcbiDest->szExtraInfo);
            return hres;
        }
    }

    size = FIELD_OFFSET(BINDINFO, szCustomVerb)+sizeof(void*);
    if(pcbiSrc->cbSize>=size && pcbiDest->cbSize>=size && pcbiSrc->szCustomVerb) {
        size = (strlenW(pcbiSrc->szCustomVerb)+1)*sizeof(WCHAR);
        pcbiDest->szCustomVerb = CoTaskMemAlloc(size);
        if(!pcbiDest->szCustomVerb) {
            CoTaskMemFree(pcbiDest->szExtraInfo);
            ReleaseStgMedium(&pcbiDest->stgmedData);
            return E_OUTOFMEMORY;
        }
623
        memcpy(pcbiDest->szCustomVerb, pcbiSrc->szCustomVerb, size);
624 625 626 627 628 629 630 631 632 633 634 635
    }

    size = FIELD_OFFSET(BINDINFO, securityAttributes)+sizeof(SECURITY_ATTRIBUTES);
    if(pcbiDest->cbSize >= size)
        memset(&pcbiDest->securityAttributes, 0, sizeof(SECURITY_ATTRIBUTES));

    if(pcbiSrc->pUnk)
        IUnknown_AddRef(pcbiDest->pUnk);

    return S_OK;
}

636 637 638 639
static BOOL text_richtext_filter(const BYTE *b, DWORD size)
{
    return size > 5 && !memcmp(b, "{\\rtf", 5);
}
640

641
static BOOL text_html_filter(const BYTE *b, DWORD size)
642
{
643
    if(size < 6)
644 645
        return FALSE;

646 647 648 649 650 651 652 653 654 655
    if((b[0] == '<'
                && (b[1] == 'h' || b[1] == 'H')
                && (b[2] == 't' || b[2] == 'T')
                && (b[3] == 'm' || b[3] == 'M')
                && (b[4] == 'l' || b[4] == 'L'))
            || (b[0] == '<'
                && (b[1] == 'h' || b[1] == 'H')
                && (b[2] == 'e' || b[2] == 'E')
                && (b[3] == 'a' || b[3] == 'A')
                && (b[4] == 'd' || b[4] == 'D'))) return TRUE;
656 657 658 659

    return FALSE;
}

660 661
static BOOL text_xml_filter(const BYTE *b, DWORD size)
{
662
    if(size < 7)
663 664
        return FALSE;

665 666 667 668 669
    if(b[0] == '<' && b[1] == '?'
            && (b[2] == 'x' || b[2] == 'X')
            && (b[3] == 'm' || b[3] == 'M')
            && (b[4] == 'l' || b[4] == 'L')
            && b[5] == ' ') return TRUE;
670 671 672 673

    return FALSE;
}

674 675 676 677 678 679
static BOOL audio_basic_filter(const BYTE *b, DWORD size)
{
    return size > 4
        && b[0] == '.' && b[1] == 's' && b[2] == 'n' && b[3] == 'd';
}

680 681 682 683 684 685 686
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';
}

687
static BOOL image_gif_filter(const BYTE *b, DWORD size)
688 689 690 691 692 693 694 695 696 697
{
    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');
}

698
static BOOL image_pjpeg_filter(const BYTE *b, DWORD size)
699
{
Jacek Caban's avatar
Jacek Caban committed
700
    return size > 2 && b[0] == 0xff && b[1] == 0xd8;
701 702
}

703
static BOOL image_tiff_filter(const BYTE *b, DWORD size)
704
{
705 706 707 708
    static const BYTE magic1[] = {0x4d,0x4d,0x00,0x2a};
    static const BYTE magic2[] = {0x49,0x49,0x2a,0xff};

    return size >= 4 && (!memcmp(b, magic1, 4) || !memcmp(b, magic2, 4));
709 710
}

711
static BOOL image_xpng_filter(const BYTE *b, DWORD size)
712 713
{
    static const BYTE xpng_header[] = {0x89,'P','N','G',0x0d,0x0a,0x1a,0x0a};
Jacek Caban's avatar
Jacek Caban committed
714
    return size > sizeof(xpng_header) && !memcmp(b, xpng_header, sizeof(xpng_header));
715 716
}

717
static BOOL image_bmp_filter(const BYTE *b, DWORD size)
718 719
{
    return size >= 14
Jacek Caban's avatar
Jacek Caban committed
720
        && b[0] == 0x42 && b[1] == 0x4d
721
        && *(const DWORD *)(b+6) == 0;
722 723
}

724
static BOOL video_avi_filter(const BYTE *b, DWORD size)
725 726 727 728 729 730
{
    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;
}

731
static BOOL video_mpeg_filter(const BYTE *b, DWORD size)
732 733 734 735 736 737
{
    return size > 4
        && !b[0] && !b[1] && b[2] == 0x01
        && (b[3] == 0xb3 || b[3] == 0xba);
}

738 739 740 741 742
static BOOL application_postscript_filter(const BYTE *b, DWORD size)
{
    return size > 2 && b[0] == '%' && b[1] == '!';
}

743
static BOOL application_pdf_filter(const BYTE *b, DWORD size)
744 745 746 747
{
    return size > 4 && b[0] == 0x25 && b[1] == 0x50 && b[2] == 0x44 && b[3] == 0x46;
}

748
static BOOL application_xzip_filter(const BYTE *b, DWORD size)
749 750 751 752
{
    return size > 2 && b[0] == 0x50 && b[1] == 0x4b;
}

753
static BOOL application_xgzip_filter(const BYTE *b, DWORD size)
754 755 756 757
{
    return size > 2 && b[0] == 0x1f && b[1] == 0x8b;
}

758
static BOOL application_java_filter(const BYTE *b, DWORD size)
759 760 761 762
{
    return size > 4 && b[0] == 0xca && b[1] == 0xfe && b[2] == 0xba && b[3] == 0xbe;
}

763
static BOOL application_xmsdownload(const BYTE *b, DWORD size)
764 765 766 767
{
    return size > 2 && b[0] == 'M' && b[1] == 'Z';
}

768 769 770 771 772 773 774
static inline BOOL is_text_plain_char(BYTE b)
{
    if(b < 0x20 && b != '\n' && b != '\r' && b != '\t')
        return FALSE;
    return TRUE;
}

775
static BOOL text_plain_filter(const BYTE *b, DWORD size)
776
{
Jacek Caban's avatar
Jacek Caban committed
777
    const BYTE *ptr;
778

Jacek Caban's avatar
Jacek Caban committed
779
    for(ptr = b; ptr < b+size-1; ptr++) {
780
        if(!is_text_plain_char(*ptr))
781 782 783 784 785 786
            return FALSE;
    }

    return TRUE;
}

787
static BOOL application_octet_stream_filter(const BYTE *b, DWORD size)
788 789 790 791
{
    return TRUE;
}

792 793 794
static HRESULT find_mime_from_buffer(const BYTE *buf, DWORD size, const WCHAR *proposed_mime, WCHAR **ret_mime)
{
    LPCWSTR ret = NULL;
795
    int len, i, any_pos_mime = -1;
796 797 798

    static const WCHAR text_htmlW[] = {'t','e','x','t','/','h','t','m','l',0};
    static const WCHAR text_richtextW[] = {'t','e','x','t','/','r','i','c','h','t','e','x','t',0};
799
    static const WCHAR text_xmlW[] = {'t','e','x','t','/','x','m','l',0};
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
    static const WCHAR audio_basicW[] = {'a','u','d','i','o','/','b','a','s','i','c',0};
    static const WCHAR audio_wavW[] = {'a','u','d','i','o','/','w','a','v',0};
    static const WCHAR image_gifW[] = {'i','m','a','g','e','/','g','i','f',0};
    static const WCHAR image_pjpegW[] = {'i','m','a','g','e','/','p','j','p','e','g',0};
    static const WCHAR image_tiffW[] = {'i','m','a','g','e','/','t','i','f','f',0};
    static const WCHAR image_xpngW[] = {'i','m','a','g','e','/','x','-','p','n','g',0};
    static const WCHAR image_bmpW[] = {'i','m','a','g','e','/','b','m','p',0};
    static const WCHAR video_aviW[] = {'v','i','d','e','o','/','a','v','i',0};
    static const WCHAR video_mpegW[] = {'v','i','d','e','o','/','m','p','e','g',0};
    static const WCHAR app_postscriptW[] =
        {'a','p','p','l','i','c','a','t','i','o','n','/','p','o','s','t','s','c','r','i','p','t',0};
    static const WCHAR app_pdfW[] = {'a','p','p','l','i','c','a','t','i','o','n','/','p','d','f',0};
    static const WCHAR app_xzipW[] = {'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};
    static const WCHAR app_xgzipW[] = {'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};
    static const WCHAR app_javaW[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
        'j','a','v','a',0};
    static const WCHAR app_xmsdownloadW[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
        'x','-','m','s','d','o','w','n','l','o','a','d',0};
    static const WCHAR text_plainW[] = {'t','e','x','t','/','p','l','a','i','n','\0'};
    static const WCHAR app_octetstreamW[] = {'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;
        BOOL (*filter)(const BYTE *,DWORD);
827
    } mime_filters_any_pos[] = {
828
        {text_htmlW,       text_html_filter},
829 830
        {text_xmlW,        text_xml_filter}
    }, mime_filters[] = {
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
        {text_richtextW,   text_richtext_filter},
     /* {audio_xaiffW,     audio_xaiff_filter}, */
        {audio_basicW,     audio_basic_filter},
        {audio_wavW,       audio_wav_filter},
        {image_gifW,       image_gif_filter},
        {image_pjpegW,     image_pjpeg_filter},
        {image_tiffW,      image_tiff_filter},
        {image_xpngW,      image_xpng_filter},
     /* {image_xbitmapW,   image_xbitmap_filter}, */
        {image_bmpW,       image_bmp_filter},
     /* {image_xjgW,       image_xjg_filter}, */
     /* {image_xemfW,      image_xemf_filter}, */
     /* {image_xwmfW,      image_xwmf_filter}, */
        {video_aviW,       video_avi_filter},
        {video_mpegW,      video_mpeg_filter},
        {app_postscriptW,  application_postscript_filter},
     /* {app_base64W,      application_base64_filter}, */
     /* {app_macbinhex40W, application_macbinhex40_filter}, */
        {app_pdfW,         application_pdf_filter},
     /* {app_zcompressedW, application_xcompressed_filter}, */
        {app_xzipW,        application_xzip_filter},
        {app_xgzipW,       application_xgzip_filter},
        {app_javaW,        application_java_filter},
        {app_xmsdownloadW, application_xmsdownload},
        {text_plainW,      text_plain_filter},
        {app_octetstreamW, application_octet_stream_filter}
    };

    if(!buf || !size) {
        if(!proposed_mime)
            return E_FAIL;

        len = strlenW(proposed_mime)+1;
        *ret_mime = CoTaskMemAlloc(len*sizeof(WCHAR));
        if(!*ret_mime)
            return E_OUTOFMEMORY;

        memcpy(*ret_mime, proposed_mime, len*sizeof(WCHAR));
        return S_OK;
    }

872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
    if(proposed_mime && (!strcmpW(proposed_mime, app_octetstreamW)
                || !strcmpW(proposed_mime, text_plainW)))
        proposed_mime = NULL;

    if(proposed_mime) {
        ret = proposed_mime;

        for(i=0; i < sizeof(mime_filters_any_pos)/sizeof(*mime_filters_any_pos); i++) {
            if(!strcmpW(proposed_mime, mime_filters_any_pos[i].mime)) {
                any_pos_mime = i;
                for(len=size; len>0; len--) {
                    if(mime_filters_any_pos[i].filter(buf+size-len, len))
                        break;
                }
                if(!len)
                    ret = NULL;
888
                break;
889
            }
890 891
        }

892 893 894 895 896 897 898 899 900 901
        if(i == sizeof(mime_filters_any_pos)/sizeof(*mime_filters_any_pos)) {
            for(i=0; i < sizeof(mime_filters)/sizeof(*mime_filters); i++) {
                if(!strcmpW(proposed_mime, mime_filters[i].mime)) {
                    if(!mime_filters[i].filter(buf, size))
                        ret = NULL;
                    break;
                }
            }
        }
    }
902

903 904 905 906 907 908 909 910 911 912
    /* Looks like a bug in native implementation, html and xml mimes
     * are not looked for if none of them was proposed */
    if(!proposed_mime || any_pos_mime!=-1) {
        for(len=size; !ret && len>0; len--) {
            for(i=0; i<sizeof(mime_filters_any_pos)/sizeof(*mime_filters_any_pos); i++) {
                if(mime_filters_any_pos[i].filter(buf+size-len, len)) {
                    ret = mime_filters_any_pos[i].mime;
                    break;
                }
            }
913 914 915 916 917 918 919 920 921 922
        }
    }

    i=0;
    while(!ret) {
        if(mime_filters[i].filter(buf, size))
            ret = mime_filters[i].mime;
        i++;
    }

923 924 925 926 927 928 929 930 931 932 933 934 935
    if(any_pos_mime!=-1 && ret==text_plainW)
        ret = mime_filters_any_pos[any_pos_mime].mime;
    else if(proposed_mime && ret==app_octetstreamW) {
        for(len=size; ret==app_octetstreamW && len>0; len--) {
            if(!is_text_plain_char(buf[size-len]))
                break;
            for(i=0; i<sizeof(mime_filters_any_pos)/sizeof(*mime_filters_any_pos); i++) {
                if(mime_filters_any_pos[i].filter(buf+size-len, len)) {
                    ret = text_plainW;
                    break;
                }
            }
        }
936

937
        if(ret == app_octetstreamW)
938 939
            ret = proposed_mime;
    }
940
    TRACE("found %s for %s\n", debugstr_w(ret), debugstr_an((const char*)buf, min(32, size)));
941 942 943 944 945 946 947 948 949 950

    len = strlenW(ret)+1;
    *ret_mime = CoTaskMemAlloc(len*sizeof(WCHAR));
    if(!*ret_mime)
        return E_OUTOFMEMORY;

    memcpy(*ret_mime, ret, len*sizeof(WCHAR));
    return S_OK;
}

951 952 953 954 955
/***********************************************************************
 *           FindMimeFromData (URLMON.@)
 *
 * Determines the Multipurpose Internet Mail Extensions (MIME) type from the data provided.
 */
956 957 958 959
HRESULT WINAPI FindMimeFromData(LPBC pBC, LPCWSTR pwzUrl, LPVOID pBuffer,
        DWORD cbSize, LPCWSTR pwzMimeProposed, DWORD dwMimeFlags,
        LPWSTR* ppwzMimeOut, DWORD dwReserved)
{
960
    TRACE("(%p,%s,%p,%d,%s,0x%x,%p,0x%x)\n", pBC, debugstr_w(pwzUrl), pBuffer, cbSize,
961 962 963
            debugstr_w(pwzMimeProposed), dwMimeFlags, ppwzMimeOut, dwReserved);

    if(dwMimeFlags)
964
        WARN("dwMimeFlags=%08x\n", dwMimeFlags);
965
    if(dwReserved)
966
        WARN("dwReserved=%d\n", dwReserved);
967 968 969 970 971 972

    /* pBC seams to not be used */

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

973 974
    if(pwzMimeProposed || pBuffer)
        return find_mime_from_buffer(pBuffer, cbSize, pwzMimeProposed, ppwzMimeOut);
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990

    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)
991
            return HRESULT_FROM_WIN32(res);
992 993 994 995 996

        size = sizeof(mime);
        res = RegQueryValueExW(hkey, wszContentType, NULL, NULL, (LPBYTE)mime, &size);
        RegCloseKey(hkey);
        if(res != ERROR_SUCCESS)
997
            return HRESULT_FROM_WIN32(res);
998 999 1000 1001 1002 1003 1004 1005

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

    return E_FAIL;
}
1006

1007 1008 1009 1010 1011 1012 1013 1014 1015
/***********************************************************************
 *           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)
{
1016 1017
    FIXME("(%p, %s, %p, %d, %s, 0x%08x, %p): stub\n", pBC, debugstr_w(pszFilename), pBuffer,
            cbBuffer, debugstr_w(pszMimeType), dwReserved, pclsid);
1018 1019 1020
    return E_NOTIMPL;
}

1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
/***********************************************************************
 * 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);
}
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054

/***********************************************************************
 *           IsLoggingEnabledA (URLMON.@)
 */
BOOL WINAPI IsLoggingEnabledA(LPCSTR url)
{
    FIXME("(%s)\n", debugstr_a(url));
    return FALSE;
}

/***********************************************************************
 *           IsLoggingEnabledW (URLMON.@)
 */
BOOL WINAPI IsLoggingEnabledW(LPCWSTR url)
{
    FIXME("(%s)\n", debugstr_w(url));
    return FALSE;
}
1055

1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
/***********************************************************************
 *           IsProtectedModeURL (URLMON.111)
 *    Undocumented, added in IE7
 */
BOOL WINAPI IsProtectedModeURL(const WCHAR *url)
{
    FIXME("stub: %s\n", debugstr_w(url));
    return TRUE;
}

1066
/***********************************************************************
1067
 *           LogSqmBits (URLMON.410)
1068 1069
 *    Undocumented, added in IE8
 */
1070
int WINAPI LogSqmBits(DWORD unk1, DWORD unk2)
1071
{
1072 1073
    FIXME("stub: %d %d\n", unk1, unk2);
    return 0;
1074 1075 1076
}

/***********************************************************************
1077
 *           LogSqmUXCommandOffsetInternal (URLMON.423)
1078 1079
 *    Undocumented, added in IE8
 */
1080
void WINAPI LogSqmUXCommandOffsetInternal(DWORD unk1, DWORD unk2, DWORD unk3, DWORD unk4)
1081
{
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
    FIXME("stub: %d %d %d %d\n", unk1, unk2, unk3, unk4);
}

/***********************************************************************
 *           MapUriToBrowserEmulationState (URLMON.444)
 *    Undocumented, added in IE8
 */
int WINAPI MapUriToBrowserEmulationState(DWORD unk1, DWORD unk2, DWORD unk3)
{
    FIXME("stub: %d %d %d\n", unk1, unk2, unk3);
    return 0;
}

/***********************************************************************
 *           MapBrowserEmulationModeToUserAgent (URLMON.445)
 *    Undocumented, added in IE8
 */
int WINAPI MapBrowserEmulationModeToUserAgent(DWORD unk1, DWORD unk2)
{
    FIXME("stub: %d %d\n", unk1, unk2);
    return 0;
1103
}
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113

/***********************************************************************
 *            RegisterMediaTypes
 *    Added in IE3, registers known MIME-type strings.
 */
HRESULT WINAPI RegisterMediaTypes(UINT types, LPCSTR *szTypes, CLIPFORMAT *cfTypes)
{
   FIXME("stub: %u %p %p\n", types, szTypes, cfTypes);
   return E_INVALIDARG;
}