winhttp, wininet: Load i2d_X509 from libcrypto.so.
[wine] / dlls / urlmon / umon.c
1 /*
2  * UrlMon
3  *
4  * Copyright 1999 Ulrich Czekalla for Corel Corporation
5  * Copyright 2002 Huw D M Davies for CodeWeavers
6  * Copyright 2005 Jacek Caban for CodeWeavers
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include <stdio.h>
24
25 #include "urlmon_main.h"
26
27 #include "winreg.h"
28 #include "winternl.h"
29 #include "wininet.h"
30 #include "shlwapi.h"
31
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
35
36 typedef struct {
37
38     const IMonikerVtbl* lpvtbl;  /* VTable relative to the IMoniker interface.*/
39
40     LONG ref; /* reference counter for this object */
41
42     LPOLESTR URLName; /* URL string identified by this URLmoniker */
43 } URLMonikerImpl;
44
45 /*******************************************************************************
46  *        URLMoniker_QueryInterface
47  *******************************************************************************/
48 static HRESULT WINAPI URLMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
49 {
50     URLMonikerImpl *This = (URLMonikerImpl *)iface;
51
52     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
53
54     /* Perform a sanity check on the parameters.*/
55     if ( (This==0) || (ppvObject==0) )
56         return E_INVALIDARG;
57
58     /* Initialize the return parameter */
59     *ppvObject = 0;
60
61     /* Compare the riid with the interface IDs implemented by this object.*/
62     if (IsEqualIID(&IID_IUnknown, riid)      ||
63         IsEqualIID(&IID_IPersist, riid)      ||
64         IsEqualIID(&IID_IPersistStream,riid) ||
65         IsEqualIID(&IID_IMoniker, riid)
66        )
67         *ppvObject = iface;
68
69     /* Check that we obtained an interface.*/
70     if ((*ppvObject)==0)
71         return E_NOINTERFACE;
72
73     /* Query Interface always increases the reference count by one when it is successful */
74     IMoniker_AddRef(iface);
75
76     return S_OK;
77 }
78
79 /******************************************************************************
80  *        URLMoniker_AddRef
81  ******************************************************************************/
82 static ULONG WINAPI URLMonikerImpl_AddRef(IMoniker* iface)
83 {
84     URLMonikerImpl *This = (URLMonikerImpl *)iface;
85     ULONG refCount = InterlockedIncrement(&This->ref);
86
87     TRACE("(%p) ref=%u\n",This, refCount);
88
89     return refCount;
90 }
91
92 /******************************************************************************
93  *        URLMoniker_Release
94  ******************************************************************************/
95 static ULONG WINAPI URLMonikerImpl_Release(IMoniker* iface)
96 {
97     URLMonikerImpl *This = (URLMonikerImpl *)iface;
98     ULONG refCount = InterlockedDecrement(&This->ref);
99
100     TRACE("(%p) ref=%u\n",This, refCount);
101
102     /* destroy the object if there's no more reference on it */
103     if (!refCount) {
104         heap_free(This->URLName);
105         heap_free(This);
106
107         URLMON_UnlockModule();
108     }
109
110     return refCount;
111 }
112
113
114 /******************************************************************************
115  *        URLMoniker_GetClassID
116  ******************************************************************************/
117 static HRESULT WINAPI URLMonikerImpl_GetClassID(IMoniker* iface,
118                                                 CLSID *pClassID)/* Pointer to CLSID of object */
119 {
120     URLMonikerImpl *This = (URLMonikerImpl *)iface;
121
122     TRACE("(%p,%p)\n",This,pClassID);
123
124     if (pClassID==NULL)
125         return E_POINTER;
126     /* Windows always returns CLSID_StdURLMoniker */
127     *pClassID = CLSID_StdURLMoniker;
128     return S_OK;
129 }
130
131 /******************************************************************************
132  *        URLMoniker_IsDirty
133  ******************************************************************************/
134 static HRESULT WINAPI URLMonikerImpl_IsDirty(IMoniker* iface)
135 {
136     URLMonikerImpl *This = (URLMonikerImpl *)iface;
137     /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
138        method in the OLE-provided moniker interfaces always return S_FALSE because
139        their internal state never changes. */
140
141     TRACE("(%p)\n",This);
142
143     return S_FALSE;
144 }
145
146 /******************************************************************************
147  *        URLMoniker_Load
148  *
149  * NOTE
150  *  Writes a ULONG containing length of unicode string, followed
151  *  by that many unicode characters
152  ******************************************************************************/
153 static HRESULT WINAPI URLMonikerImpl_Load(IMoniker* iface,IStream* pStm)
154 {
155     URLMonikerImpl *This = (URLMonikerImpl *)iface;
156     
157     HRESULT res;
158     ULONG size;
159     ULONG got;
160     TRACE("(%p,%p)\n",This,pStm);
161
162     if(!pStm)
163         return E_INVALIDARG;
164
165     res = IStream_Read(pStm, &size, sizeof(ULONG), &got);
166     if(SUCCEEDED(res)) {
167         if(got == sizeof(ULONG)) {
168             heap_free(This->URLName);
169             This->URLName = heap_alloc(size);
170             if(!This->URLName)
171                 res = E_OUTOFMEMORY;
172             else {
173                 res = IStream_Read(pStm, This->URLName, size, NULL);
174                 This->URLName[size/sizeof(WCHAR) - 1] = 0;
175             }
176         }
177         else
178             res = E_FAIL;
179     }
180     return res;
181 }
182
183 /******************************************************************************
184  *        URLMoniker_Save
185  ******************************************************************************/
186 static HRESULT WINAPI URLMonikerImpl_Save(IMoniker* iface,
187                                           IStream* pStm,/* pointer to the stream where the object is to be saved */
188                                           BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
189 {
190     URLMonikerImpl *This = (URLMonikerImpl *)iface;
191
192     HRESULT res;
193     ULONG size;
194     TRACE("(%p,%p,%d)\n",This,pStm,fClearDirty);
195
196     if(!pStm)
197         return E_INVALIDARG;
198
199     size = (strlenW(This->URLName) + 1)*sizeof(WCHAR);
200     res=IStream_Write(pStm,&size,sizeof(ULONG),NULL);
201     if(SUCCEEDED(res))
202         res=IStream_Write(pStm,This->URLName,size,NULL);
203     return res;
204
205 }
206
207 /******************************************************************************
208  *        URLMoniker_GetSizeMax
209  ******************************************************************************/
210 static HRESULT WINAPI URLMonikerImpl_GetSizeMax(IMoniker* iface,
211                                                 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
212 {
213     URLMonikerImpl *This = (URLMonikerImpl *)iface;
214
215     TRACE("(%p,%p)\n",This,pcbSize);
216
217     if(!pcbSize)
218         return E_INVALIDARG;
219
220     pcbSize->QuadPart = sizeof(ULONG) + ((strlenW(This->URLName)+1) * sizeof(WCHAR));
221     return S_OK;
222 }
223
224 /******************************************************************************
225  *                  URLMoniker_BindToObject
226  ******************************************************************************/
227 static HRESULT WINAPI URLMonikerImpl_BindToObject(IMoniker* iface,
228                                                   IBindCtx* pbc,
229                                                   IMoniker* pmkToLeft,
230                                                   REFIID riid,
231                                                   VOID** ppv)
232 {
233     URLMonikerImpl *This = (URLMonikerImpl *)iface;
234     IRunningObjectTable *obj_tbl;
235     HRESULT hres;
236
237     TRACE("(%p)->(%p,%p,%s,%p): stub\n", This, pbc, pmkToLeft, debugstr_guid(riid), ppv);
238
239     hres = IBindCtx_GetRunningObjectTable(pbc, &obj_tbl);
240     if(SUCCEEDED(hres)) {
241         FIXME("use running object table\n");
242         IRunningObjectTable_Release(obj_tbl);
243     }
244
245     return bind_to_object(iface, This->URLName, pbc, riid, ppv);
246 }
247
248 /******************************************************************************
249  *        URLMoniker_BindToStorage
250  ******************************************************************************/
251 static HRESULT WINAPI URLMonikerImpl_BindToStorage(IMoniker* iface,
252                                                    IBindCtx* pbc,
253                                                    IMoniker* pmkToLeft,
254                                                    REFIID riid,
255                                                    VOID** ppvObject)
256 {
257     URLMonikerImpl *This = (URLMonikerImpl*)iface;
258
259     TRACE("(%p)->(%p %p %s %p)\n", This, pbc, pmkToLeft, debugstr_guid(riid), ppvObject);
260
261     if(pmkToLeft)
262         FIXME("Unsupported pmkToLeft\n");
263
264     return bind_to_storage(This->URLName, pbc, riid, ppvObject);
265 }
266
267 /******************************************************************************
268  *        URLMoniker_Reduce
269  ******************************************************************************/
270 static HRESULT WINAPI URLMonikerImpl_Reduce(IMoniker* iface,
271                                             IBindCtx* pbc,
272                                             DWORD dwReduceHowFar,
273                                             IMoniker** ppmkToLeft,
274                                             IMoniker** ppmkReduced)
275 {
276     URLMonikerImpl *This = (URLMonikerImpl *)iface;
277     
278     TRACE("(%p,%p,%d,%p,%p)\n",This,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
279
280     if(!ppmkReduced)
281         return E_INVALIDARG;
282
283     URLMonikerImpl_AddRef(iface);
284     *ppmkReduced = iface;
285     return MK_S_REDUCED_TO_SELF;
286 }
287
288 /******************************************************************************
289  *        URLMoniker_ComposeWith
290  ******************************************************************************/
291 static HRESULT WINAPI URLMonikerImpl_ComposeWith(IMoniker* iface,
292                                                  IMoniker* pmkRight,
293                                                  BOOL fOnlyIfNotGeneric,
294                                                  IMoniker** ppmkComposite)
295 {
296     URLMonikerImpl *This = (URLMonikerImpl *)iface;
297     FIXME("(%p)->(%p,%d,%p): stub\n",This,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
298
299     return E_NOTIMPL;
300 }
301
302 /******************************************************************************
303  *        URLMoniker_Enum
304  ******************************************************************************/
305 static HRESULT WINAPI URLMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
306 {
307     URLMonikerImpl *This = (URLMonikerImpl *)iface;
308     TRACE("(%p,%d,%p)\n",This,fForward,ppenumMoniker);
309
310     if(!ppenumMoniker)
311         return E_INVALIDARG;
312
313     /* Does not support sub-monikers */
314     *ppenumMoniker = NULL;
315     return S_OK;
316 }
317
318 /******************************************************************************
319  *        URLMoniker_IsEqual
320  ******************************************************************************/
321 static HRESULT WINAPI URLMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
322 {
323     URLMonikerImpl *This = (URLMonikerImpl *)iface;
324     CLSID clsid;
325     LPOLESTR urlPath;
326     IBindCtx* bind;
327     HRESULT res;
328
329     TRACE("(%p,%p)\n",This,pmkOtherMoniker);
330
331     if(pmkOtherMoniker==NULL)
332         return E_INVALIDARG;
333
334     IMoniker_GetClassID(pmkOtherMoniker,&clsid);
335
336     if(!IsEqualCLSID(&clsid,&CLSID_StdURLMoniker))
337         return S_FALSE;
338
339     res = CreateBindCtx(0,&bind);
340     if(FAILED(res))
341         return res;
342
343     res = S_FALSE;
344     if(SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&urlPath))) {
345         int result = lstrcmpiW(urlPath, This->URLName);
346         CoTaskMemFree(urlPath);
347         if(result == 0)
348             res = S_OK;
349     }
350     IUnknown_Release(bind);
351     return res;
352 }
353
354
355 /******************************************************************************
356  *        URLMoniker_Hash
357  ******************************************************************************/
358 static HRESULT WINAPI URLMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
359 {
360     URLMonikerImpl *This = (URLMonikerImpl *)iface;
361     
362     int  h = 0,i,skip,len;
363     int  off = 0;
364     LPOLESTR val;
365
366     TRACE("(%p,%p)\n",This,pdwHash);
367
368     if(!pdwHash)
369         return E_INVALIDARG;
370
371     val = This->URLName;
372     len = lstrlenW(val);
373
374     if(len < 16) {
375         for(i = len ; i > 0; i--) {
376             h = (h * 37) + val[off++];
377         }
378     }
379     else {
380         /* only sample some characters */
381         skip = len / 8;
382         for(i = len; i > 0; i -= skip, off += skip) {
383             h = (h * 39) + val[off];
384         }
385     }
386     *pdwHash = h;
387     return S_OK;
388 }
389
390 /******************************************************************************
391  *        URLMoniker_IsRunning
392  ******************************************************************************/
393 static HRESULT WINAPI URLMonikerImpl_IsRunning(IMoniker* iface,
394                                                IBindCtx* pbc,
395                                                IMoniker* pmkToLeft,
396                                                IMoniker* pmkNewlyRunning)
397 {
398     URLMonikerImpl *This = (URLMonikerImpl *)iface;
399     FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pmkNewlyRunning);
400
401     return E_NOTIMPL;
402 }
403
404 /******************************************************************************
405  *        URLMoniker_GetTimeOfLastChange
406  ******************************************************************************/
407 static HRESULT WINAPI URLMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
408                                                          IBindCtx* pbc,
409                                                          IMoniker* pmkToLeft,
410                                                          FILETIME* pFileTime)
411 {
412     URLMonikerImpl *This = (URLMonikerImpl *)iface;
413     FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pFileTime);
414
415     return E_NOTIMPL;
416 }
417
418 /******************************************************************************
419  *        URLMoniker_Inverse
420  ******************************************************************************/
421 static HRESULT WINAPI URLMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
422 {
423     URLMonikerImpl *This = (URLMonikerImpl *)iface;
424     TRACE("(%p,%p)\n",This,ppmk);
425
426     return MK_E_NOINVERSE;
427 }
428
429 /******************************************************************************
430  *        URLMoniker_CommonPrefixWith
431  ******************************************************************************/
432 static HRESULT WINAPI URLMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
433 {
434     URLMonikerImpl *This = (URLMonikerImpl *)iface;
435     FIXME("(%p)->(%p,%p): stub\n",This,pmkOther,ppmkPrefix);
436
437     return E_NOTIMPL;
438 }
439
440 /******************************************************************************
441  *        URLMoniker_RelativePathTo
442  ******************************************************************************/
443 static HRESULT WINAPI URLMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
444 {
445     URLMonikerImpl *This = (URLMonikerImpl *)iface;
446     FIXME("(%p)->(%p,%p): stub\n",This,pmOther,ppmkRelPath);
447
448     return E_NOTIMPL;
449 }
450
451 /******************************************************************************
452  *        URLMoniker_GetDisplayName
453  ******************************************************************************/
454 static HRESULT WINAPI URLMonikerImpl_GetDisplayName(IMoniker* iface,
455                                                     IBindCtx* pbc,
456                                                     IMoniker* pmkToLeft,
457                                                     LPOLESTR *ppszDisplayName)
458 {
459     URLMonikerImpl *This = (URLMonikerImpl *)iface;
460     
461     int len;
462     
463     TRACE("(%p,%p,%p,%p)\n",This,pbc,pmkToLeft,ppszDisplayName);
464     
465     if(!ppszDisplayName)
466         return E_INVALIDARG;
467     
468     /* FIXME: If this is a partial URL, try and get a URL moniker from SZ_URLCONTEXT in the bind context,
469         then look at pmkToLeft to try and complete the URL
470     */
471     len = lstrlenW(This->URLName)+1;
472     *ppszDisplayName = CoTaskMemAlloc(len*sizeof(WCHAR));
473     if(!*ppszDisplayName)
474         return E_OUTOFMEMORY;
475     lstrcpyW(*ppszDisplayName, This->URLName);
476     return S_OK;
477 }
478
479 /******************************************************************************
480  *        URLMoniker_ParseDisplayName
481  ******************************************************************************/
482 static HRESULT WINAPI URLMonikerImpl_ParseDisplayName(IMoniker* iface,
483                                                       IBindCtx* pbc,
484                                                       IMoniker* pmkToLeft,
485                                                       LPOLESTR pszDisplayName,
486                                                       ULONG* pchEaten,
487                                                       IMoniker** ppmkOut)
488 {
489     URLMonikerImpl *This = (URLMonikerImpl *)iface;
490     FIXME("(%p)->(%p,%p,%p,%p,%p): stub\n",This,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
491
492     return E_NOTIMPL;
493 }
494
495 /******************************************************************************
496  *        URLMoniker_IsSystemMoniker
497  ******************************************************************************/
498 static HRESULT WINAPI URLMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
499 {
500     URLMonikerImpl *This = (URLMonikerImpl *)iface;
501     TRACE("(%p,%p)\n",This,pwdMksys);
502
503     if(!pwdMksys)
504         return E_INVALIDARG;
505
506     *pwdMksys = MKSYS_URLMONIKER;
507     return S_OK;
508 }
509
510 /********************************************************************************/
511 /* Virtual function table for the URLMonikerImpl class which  include IPersist,*/
512 /* IPersistStream and IMoniker functions.                                       */
513 static const IMonikerVtbl VT_URLMonikerImpl =
514 {
515     URLMonikerImpl_QueryInterface,
516     URLMonikerImpl_AddRef,
517     URLMonikerImpl_Release,
518     URLMonikerImpl_GetClassID,
519     URLMonikerImpl_IsDirty,
520     URLMonikerImpl_Load,
521     URLMonikerImpl_Save,
522     URLMonikerImpl_GetSizeMax,
523     URLMonikerImpl_BindToObject,
524     URLMonikerImpl_BindToStorage,
525     URLMonikerImpl_Reduce,
526     URLMonikerImpl_ComposeWith,
527     URLMonikerImpl_Enum,
528     URLMonikerImpl_IsEqual,
529     URLMonikerImpl_Hash,
530     URLMonikerImpl_IsRunning,
531     URLMonikerImpl_GetTimeOfLastChange,
532     URLMonikerImpl_Inverse,
533     URLMonikerImpl_CommonPrefixWith,
534     URLMonikerImpl_RelativePathTo,
535     URLMonikerImpl_GetDisplayName,
536     URLMonikerImpl_ParseDisplayName,
537     URLMonikerImpl_IsSystemMoniker
538 };
539
540 /******************************************************************************
541  *         URLMoniker_Construct (local function)
542  *******************************************************************************/
543 static HRESULT URLMonikerImpl_Construct(URLMonikerImpl* This, LPCOLESTR lpszLeftURLName, LPCOLESTR lpszURLName)
544 {
545     HRESULT hres;
546     DWORD sizeStr = 0;
547
548     TRACE("(%p,%s,%s)\n",This,debugstr_w(lpszLeftURLName),debugstr_w(lpszURLName));
549
550     This->lpvtbl = &VT_URLMonikerImpl;
551     This->ref = 0;
552
553     This->URLName = heap_alloc(INTERNET_MAX_URL_LENGTH*sizeof(WCHAR));
554
555     if(lpszLeftURLName)
556         hres = CoInternetCombineUrl(lpszLeftURLName, lpszURLName, URL_FILE_USE_PATHURL,
557                 This->URLName, INTERNET_MAX_URL_LENGTH, &sizeStr, 0);
558     else
559         hres = CoInternetParseUrl(lpszURLName, PARSE_CANONICALIZE, URL_FILE_USE_PATHURL,
560                 This->URLName, INTERNET_MAX_URL_LENGTH, &sizeStr, 0);
561
562     if(FAILED(hres)) {
563         heap_free(This->URLName);
564         return hres;
565     }
566
567     URLMON_LockModule();
568
569     if(sizeStr != INTERNET_MAX_URL_LENGTH)
570         This->URLName = heap_realloc(This->URLName, (sizeStr+1)*sizeof(WCHAR));
571
572     TRACE("URLName = %s\n", debugstr_w(This->URLName));
573
574     return S_OK;
575 }
576
577 /***********************************************************************
578  *           CreateURLMonikerEx (URLMON.@)
579  *
580  * Create a url moniker.
581  *
582  * PARAMS
583  *    pmkContext [I] Context
584  *    szURL      [I] Url to create the moniker for
585  *    ppmk       [O] Destination for created moniker.
586  *    dwFlags    [I] Flags.
587  *
588  * RETURNS
589  *    Success: S_OK. ppmk contains the created IMoniker object.
590  *    Failure: MK_E_SYNTAX if szURL is not a valid url, or
591  *             E_OUTOFMEMORY if memory allocation fails.
592  */
593 HRESULT WINAPI CreateURLMonikerEx(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk, DWORD dwFlags)
594 {
595     URLMonikerImpl *obj;
596     HRESULT hres;
597     LPOLESTR lefturl = NULL;
598
599     TRACE("(%p, %s, %p, %08x)\n", pmkContext, debugstr_w(szURL), ppmk, dwFlags);
600
601     if (dwFlags & URL_MK_UNIFORM) FIXME("ignoring flag URL_MK_UNIFORM\n");
602
603     if(!(obj = heap_alloc(sizeof(*obj))))
604         return E_OUTOFMEMORY;
605
606     if(pmkContext) {
607         IBindCtx* bind;
608         DWORD dwMksys = 0;
609         IMoniker_IsSystemMoniker(pmkContext, &dwMksys);
610         if(dwMksys == MKSYS_URLMONIKER && SUCCEEDED(CreateBindCtx(0, &bind))) {
611             IMoniker_GetDisplayName(pmkContext, bind, NULL, &lefturl);
612             TRACE("lefturl = %s\n", debugstr_w(lefturl));
613             IBindCtx_Release(bind);
614         }
615     }
616         
617     hres = URLMonikerImpl_Construct(obj, lefturl, szURL);
618     CoTaskMemFree(lefturl);
619     if(SUCCEEDED(hres))
620         hres = URLMonikerImpl_QueryInterface((IMoniker*)obj, &IID_IMoniker, (void**)ppmk);
621     else
622         heap_free(obj);
623     return hres;
624 }
625
626 /**********************************************************************
627  *           CreateURLMoniker (URLMON.@)
628  *
629  * Create a url moniker.
630  *
631  * PARAMS
632  *    pmkContext [I] Context
633  *    szURL      [I] Url to create the moniker for
634  *    ppmk       [O] Destination for created moniker.
635  *
636  * RETURNS
637  *    Success: S_OK. ppmk contains the created IMoniker object.
638  *    Failure: MK_E_SYNTAX if szURL is not a valid url, or
639  *             E_OUTOFMEMORY if memory allocation fails.
640  */
641 HRESULT WINAPI CreateURLMoniker(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk)
642 {
643     return CreateURLMonikerEx(pmkContext, szURL, ppmk, URL_MK_LEGACY);
644 }
645
646 /***********************************************************************
647  *           IsAsyncMoniker (URLMON.@)
648  */
649 HRESULT WINAPI IsAsyncMoniker(IMoniker *pmk)
650 {
651     IUnknown *am;
652     
653     TRACE("(%p)\n", pmk);
654     if(!pmk)
655         return E_INVALIDARG;
656     if(SUCCEEDED(IMoniker_QueryInterface(pmk, &IID_IAsyncMoniker, (void**)&am))) {
657         IUnknown_Release(am);
658         return S_OK;
659     }
660     return S_FALSE;
661 }
662
663 /***********************************************************************
664  *           BindAsyncMoniker (URLMON.@)
665  *
666  * Bind a bind status callback to an asynchronous URL Moniker.
667  *
668  * PARAMS
669  *  pmk           [I] Moniker object to bind status callback to
670  *  grfOpt        [I] Options, seems not used
671  *  pbsc          [I] Status callback to bind
672  *  iidResult     [I] Interface to return
673  *  ppvResult     [O] Resulting asynchronous moniker object
674  *
675  * RETURNS
676  *    Success: S_OK.
677  *    Failure: E_INVALIDARG, if any argument is invalid, or
678  *             E_OUTOFMEMORY if memory allocation fails.
679  */
680 HRESULT WINAPI BindAsyncMoniker(IMoniker *pmk, DWORD grfOpt, IBindStatusCallback *pbsc, REFIID iidResult, LPVOID *ppvResult)
681 {
682     LPBC pbc = NULL;
683     HRESULT hr = E_INVALIDARG;
684
685     TRACE("(%p %08x %p %s %p)\n", pmk, grfOpt, pbsc, debugstr_guid(iidResult), ppvResult);
686
687     if (pmk && ppvResult)
688     {
689         *ppvResult = NULL;
690
691         hr = CreateAsyncBindCtx(0, pbsc, NULL, &pbc);
692         if (hr == NOERROR)
693         {
694             hr = IMoniker_BindToObject(pmk, pbc, NULL, iidResult, ppvResult);
695             IBindCtx_Release(pbc);
696         }
697     }
698     return hr;
699 }
700
701 /***********************************************************************
702  *           MkParseDisplayNameEx (URLMON.@)
703  */
704 HRESULT WINAPI MkParseDisplayNameEx(IBindCtx *pbc, LPCWSTR szDisplayName, ULONG *pchEaten, LPMONIKER *ppmk)
705 {
706     TRACE("(%p %s %p %p)\n", pbc, debugstr_w(szDisplayName), pchEaten, ppmk);
707
708     if(is_registered_protocol(szDisplayName)) {
709         HRESULT hres;
710
711         hres = CreateURLMoniker(NULL, szDisplayName, ppmk);
712         if(SUCCEEDED(hres)) {
713             *pchEaten = strlenW(szDisplayName);
714             return hres;
715         }
716     }
717
718     return MkParseDisplayName(pbc, szDisplayName, pchEaten, ppmk);
719 }
720
721
722 /***********************************************************************
723  *           URLDownloadToCacheFileA (URLMON.@)
724  */
725 HRESULT WINAPI URLDownloadToCacheFileA(LPUNKNOWN lpUnkCaller, LPCSTR szURL, LPSTR szFileName,
726         DWORD dwBufLength, DWORD dwReserved, LPBINDSTATUSCALLBACK pBSC)
727 {
728     LPWSTR url = NULL, file_name = NULL;
729     int len;
730     HRESULT hres;
731
732     TRACE("(%p %s %p %d %d %p)\n", lpUnkCaller, debugstr_a(szURL), szFileName,
733             dwBufLength, dwReserved, pBSC);
734
735     if(szURL) {
736         len = MultiByteToWideChar(CP_ACP, 0, szURL, -1, NULL, 0);
737         url = heap_alloc(len*sizeof(WCHAR));
738         MultiByteToWideChar(CP_ACP, 0, szURL, -1, url, len);
739     }
740
741     if(szFileName)
742         file_name = heap_alloc(dwBufLength*sizeof(WCHAR));
743
744     hres = URLDownloadToCacheFileW(lpUnkCaller, url, file_name, dwBufLength*sizeof(WCHAR),
745             dwReserved, pBSC);
746
747     if(SUCCEEDED(hres) && file_name)
748         WideCharToMultiByte(CP_ACP, 0, file_name, -1, szFileName, dwBufLength, NULL, NULL);
749
750     heap_free(url);
751     heap_free(file_name);
752
753     return hres;
754 }
755
756 /***********************************************************************
757  *           URLDownloadToCacheFileW (URLMON.@)
758  */
759 HRESULT WINAPI URLDownloadToCacheFileW(LPUNKNOWN lpUnkCaller, LPCWSTR szURL, LPWSTR szFileName,
760                 DWORD dwBufLength, DWORD dwReserved, LPBINDSTATUSCALLBACK pBSC)
761 {
762     WCHAR cache_path[MAX_PATH + 1];
763     FILETIME expire, modified;
764     HRESULT hr;
765     LPWSTR ext;
766
767     static WCHAR header[] = {
768         'H','T','T','P','/','1','.','0',' ','2','0','0',' ',
769         'O','K','\\','r','\\','n','\\','r','\\','n',0
770     };
771
772     TRACE("(%p, %s, %p, %d, %d, %p)\n", lpUnkCaller, debugstr_w(szURL),
773           szFileName, dwBufLength, dwReserved, pBSC);
774
775     if (!szURL || !szFileName)
776         return E_INVALIDARG;
777
778     ext = PathFindExtensionW(szURL);
779
780     if (!CreateUrlCacheEntryW(szURL, 0, ext, cache_path, 0))
781         return E_FAIL;
782
783     hr = URLDownloadToFileW(lpUnkCaller, szURL, cache_path, 0, pBSC);
784     if (FAILED(hr))
785         return hr;
786
787     expire.dwHighDateTime = 0;
788     expire.dwLowDateTime = 0;
789     modified.dwHighDateTime = 0;
790     modified.dwLowDateTime = 0;
791
792     if (!CommitUrlCacheEntryW(szURL, cache_path, expire, modified, NORMAL_CACHE_ENTRY,
793                               header, sizeof(header), NULL, NULL))
794         return E_FAIL;
795
796     if (strlenW(cache_path) > dwBufLength)
797         return E_OUTOFMEMORY;
798
799     lstrcpyW(szFileName, cache_path);
800
801     return S_OK;
802 }
803
804 /***********************************************************************
805  *           HlinkSimpleNavigateToMoniker (URLMON.@)
806  */
807 HRESULT WINAPI HlinkSimpleNavigateToMoniker(IMoniker *pmkTarget,
808     LPCWSTR szLocation, LPCWSTR szTargetFrameName, IUnknown *pUnk,
809     IBindCtx *pbc, IBindStatusCallback *pbsc, DWORD grfHLNF, DWORD dwReserved)
810 {
811     FIXME("stub\n");
812     return E_NOTIMPL;
813 }
814
815 /***********************************************************************
816  *           HlinkSimpleNavigateToString (URLMON.@)
817  */
818 HRESULT WINAPI HlinkSimpleNavigateToString( LPCWSTR szTarget,
819     LPCWSTR szLocation, LPCWSTR szTargetFrameName, IUnknown *pUnk,
820     IBindCtx *pbc, IBindStatusCallback *pbsc, DWORD grfHLNF, DWORD dwReserved)
821 {
822     FIXME("%s\n", debugstr_w( szTarget ) );
823     return E_NOTIMPL;
824 }
825
826 /***********************************************************************
827  *           HlinkNavigateString (URLMON.@)
828  */
829 HRESULT WINAPI HlinkNavigateString( IUnknown *pUnk, LPCWSTR szTarget )
830 {
831     TRACE("%p %s\n", pUnk, debugstr_w( szTarget ) );
832     return HlinkSimpleNavigateToString( 
833                szTarget, NULL, NULL, pUnk, NULL, NULL, 0, 0 );
834 }
835
836 /***********************************************************************
837  *           GetSoftwareUpdateInfo (URLMON.@)
838  */
839 HRESULT WINAPI GetSoftwareUpdateInfo( LPCWSTR szDistUnit, LPSOFTDISTINFO psdi )
840 {
841     FIXME("%s %p\n", debugstr_w(szDistUnit), psdi );
842     return E_FAIL;
843 }