wininet: Avoid accessing uninitialized memory in ConvertUrlComponentValue.
[wine] / dlls / hlink / link.c
1 /*
2  * Implementation of hyperlinking (hlink.dll)
3  *
4  * Copyright 2005 Aric Stewart for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "hlink_private.h"
22
23 #include "shellapi.h"
24 #include "hlguids.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(hlink);
29
30 #define HLINK_SAVE_MAGIC    0x00000002
31 #define HLINK_SAVE_MONIKER_PRESENT      0x01
32 #define HLINK_SAVE_MONIKER_IS_ABSOLUTE  0x02
33 #define HLINK_SAVE_LOCATION_PRESENT     0x08
34 #define HLINK_SAVE_FRIENDLY_PRESENT     0x10
35 /* 0x20, 0x40 unknown */
36 #define HLINK_SAVE_TARGET_FRAME_PRESENT 0x80
37 /* known flags */
38 #define HLINK_SAVE_ALL (HLINK_SAVE_TARGET_FRAME_PRESENT|HLINK_SAVE_FRIENDLY_PRESENT|HLINK_SAVE_LOCATION_PRESENT|0x04|HLINK_SAVE_MONIKER_IS_ABSOLUTE|HLINK_SAVE_MONIKER_PRESENT)
39
40 static const IHlinkVtbl              hlvt;
41 static const IPersistStreamVtbl      psvt;
42 static const IDataObjectVtbl         dovt;
43
44 typedef struct
45 {
46     const IHlinkVtbl    *lpVtbl;
47     LONG                ref;
48
49     const IPersistStreamVtbl    *lpPSVtbl;
50     const IDataObjectVtbl       *lpDOVtbl;
51
52     LPWSTR              FriendlyName;
53     LPWSTR              Location;
54     LPWSTR              Target;
55     LPWSTR              TargetFrameName;
56     IMoniker            *Moniker;
57     IHlinkSite          *Site;
58     DWORD               SiteData;
59     BOOL                absolute;
60 } HlinkImpl;
61
62
63 static inline HlinkImpl* HlinkImpl_from_IPersistStream( IPersistStream* iface)
64 {
65     return (HlinkImpl*) ((CHAR*)iface - FIELD_OFFSET(HlinkImpl, lpPSVtbl));
66 }
67
68 static inline HlinkImpl* HlinkImpl_from_IDataObject( IDataObject* iface)
69 {
70     return (HlinkImpl*) ((CHAR*)iface - FIELD_OFFSET(HlinkImpl, lpDOVtbl));
71 }
72
73 static inline void __GetMoniker(HlinkImpl* This, IMoniker** moniker)
74 {
75     *moniker = NULL;
76     if (This->Moniker)
77     {
78         *moniker = This->Moniker;
79         if (*moniker)
80             IMoniker_AddRef(*moniker);
81     }
82     else if (This->Site)
83     {
84         IHlinkSite_GetMoniker(This->Site, This->SiteData,
85                 OLEGETMONIKER_FORCEASSIGN, OLEWHICHMK_CONTAINER, moniker);
86     }
87 }
88
89 HRESULT WINAPI HLink_Constructor(IUnknown *pUnkOuter, REFIID riid,
90         LPVOID *ppv)
91 {
92     HlinkImpl * hl;
93
94     TRACE("unkOut=%p riid=%s\n", pUnkOuter, debugstr_guid(riid));
95     *ppv = NULL;
96
97     if (pUnkOuter)
98         return CLASS_E_NOAGGREGATION;
99
100     hl = heap_alloc_zero(sizeof(HlinkImpl));
101     if (!hl)
102         return E_OUTOFMEMORY;
103
104     hl->ref = 1;
105     hl->lpVtbl = &hlvt;
106     hl->lpPSVtbl = &psvt;
107     hl->lpDOVtbl = &dovt;
108
109     *ppv = hl;
110     return S_OK;
111 }
112
113 static HRESULT WINAPI IHlink_fnQueryInterface(IHlink* iface, REFIID riid,
114         LPVOID *ppvObj)
115 {
116     HlinkImpl  *This = (HlinkImpl*)iface;
117
118     TRACE ("(%p)->(%s,%p)\n", This, debugstr_guid (riid), ppvObj);
119
120     *ppvObj = NULL;
121
122     if (IsEqualIID(riid, &IID_IUnknown) || (IsEqualIID(riid, &IID_IHlink)))
123         *ppvObj = This;
124     else if (IsEqualIID(riid, &IID_IPersistStream))
125         *ppvObj = &(This->lpPSVtbl);
126     else if (IsEqualIID(riid, &IID_IDataObject))
127         *ppvObj = &(This->lpDOVtbl);
128
129     if (*ppvObj)
130     {
131         IUnknown_AddRef((IUnknown*)(*ppvObj));
132         return S_OK;
133     }
134     return E_NOINTERFACE;
135 }
136
137 static ULONG WINAPI IHlink_fnAddRef (IHlink* iface)
138 {
139     HlinkImpl  *This = (HlinkImpl*)iface;
140     ULONG refCount = InterlockedIncrement(&This->ref);
141
142     TRACE("(%p)->(count=%u)\n", This, refCount - 1);
143
144     return refCount;
145 }
146
147 static ULONG WINAPI IHlink_fnRelease (IHlink* iface)
148 {
149     HlinkImpl  *This = (HlinkImpl*)iface;
150     ULONG refCount = InterlockedDecrement(&This->ref);
151
152     TRACE("(%p)->(count=%u)\n", This, refCount + 1);
153     if (refCount)
154         return refCount;
155
156     TRACE("-- destroying IHlink (%p)\n", This);
157     heap_free(This->FriendlyName);
158     heap_free(This->Target);
159     heap_free(This->TargetFrameName);
160     heap_free(This->Location);
161     if (This->Moniker)
162         IMoniker_Release(This->Moniker);
163     if (This->Site)
164         IHlinkSite_Release(This->Site);
165     heap_free(This);
166     return 0;
167 }
168
169 static HRESULT WINAPI IHlink_fnSetHlinkSite( IHlink* iface,
170         IHlinkSite* pihlSite, DWORD dwSiteData)
171 {
172     HlinkImpl  *This = (HlinkImpl*)iface;
173
174     TRACE("(%p)->(%p %i)\n", This, pihlSite, dwSiteData);
175
176     if (This->Site)
177         IHlinkSite_Release(This->Site);
178
179     This->Site = pihlSite;
180     if (This->Site)
181         IHlinkSite_AddRef(This->Site);
182
183     This->SiteData = dwSiteData;
184
185     return S_OK;
186 }
187
188 static HRESULT WINAPI IHlink_fnGetHlinkSite( IHlink* iface,
189         IHlinkSite** ppihlSite, DWORD *pdwSiteData)
190 {
191     HlinkImpl  *This = (HlinkImpl*)iface;
192
193     TRACE("(%p)->(%p %p)\n", This, ppihlSite, pdwSiteData);
194
195     *ppihlSite = This->Site;
196     *pdwSiteData = This->SiteData;
197
198     if (This->Site)
199         IHlinkSite_AddRef(This->Site);
200
201     return S_OK;
202 }
203
204 static HRESULT WINAPI IHlink_fnSetMonikerReference( IHlink* iface,
205         DWORD rfHLSETF, IMoniker *pmkTarget, LPCWSTR pwzLocation)
206 {
207     HlinkImpl  *This = (HlinkImpl*)iface;
208
209     TRACE("(%p)->(%i %p %s)\n", This, rfHLSETF, pmkTarget,
210             debugstr_w(pwzLocation));
211
212     if(rfHLSETF == 0)
213         return E_INVALIDARG;
214     if(!(rfHLSETF & (HLINKSETF_TARGET | HLINKSETF_LOCATION)))
215         return rfHLSETF;
216
217     if(rfHLSETF & HLINKSETF_TARGET){
218         if (This->Moniker)
219             IMoniker_Release(This->Moniker);
220
221         This->Moniker = pmkTarget;
222         if (This->Moniker)
223         {
224             LPOLESTR display_name;
225             IMoniker_AddRef(This->Moniker);
226             IMoniker_GetDisplayName(This->Moniker, NULL, NULL, &display_name);
227             This->absolute = display_name && strchrW(display_name, ':');
228             CoTaskMemFree(display_name);
229         }
230     }
231
232     if(rfHLSETF & HLINKSETF_LOCATION){
233         heap_free(This->Location);
234         This->Location = hlink_strdupW( pwzLocation );
235     }
236
237     return S_OK;
238 }
239
240 static HRESULT WINAPI IHlink_fnSetStringReference(IHlink* iface,
241         DWORD grfHLSETF, LPCWSTR pwzTarget, LPCWSTR pwzLocation)
242 {
243     HlinkImpl  *This = (HlinkImpl*)iface;
244
245     TRACE("(%p)->(%i %s %s)\n", This, grfHLSETF, debugstr_w(pwzTarget),
246             debugstr_w(pwzLocation));
247
248     if (grfHLSETF & HLINKSETF_TARGET)
249     {
250         heap_free(This->Target);
251         This->Target = hlink_strdupW( pwzTarget );
252     }
253     if (grfHLSETF & HLINKSETF_LOCATION)
254     {
255         heap_free(This->Location);
256         This->Location = hlink_strdupW( pwzLocation );
257     }
258
259     return S_OK;
260 }
261
262 static HRESULT WINAPI IHlink_fnGetMonikerReference(IHlink* iface,
263         DWORD dwWhichRef, IMoniker **ppimkTarget, LPWSTR *ppwzLocation)
264 {
265     HlinkImpl  *This = (HlinkImpl*)iface;
266
267     TRACE("(%p) -> (%i %p %p)\n", This, dwWhichRef, ppimkTarget,
268             ppwzLocation);
269
270     if(ppimkTarget)
271         __GetMoniker(This, ppimkTarget);
272
273     if (ppwzLocation)
274         IHlink_GetStringReference(iface, dwWhichRef, NULL, ppwzLocation);
275
276     return S_OK;
277 }
278
279 static HRESULT WINAPI IHlink_fnGetStringReference (IHlink* iface,
280         DWORD dwWhichRef, LPWSTR *ppwzTarget, LPWSTR *ppwzLocation)
281 {
282     HlinkImpl  *This = (HlinkImpl*)iface;
283
284     FIXME("(%p) -> (%i %p %p)\n", This, dwWhichRef, ppwzTarget, ppwzLocation);
285
286     if (ppwzTarget)
287     {
288         *ppwzTarget = hlink_co_strdupW( This->Target );
289
290         if (!This->Target)
291         {
292             IMoniker* mon;
293             __GetMoniker(This, &mon);
294             if (mon)
295             {
296                 IBindCtx *pbc;
297
298                 CreateBindCtx( 0, &pbc);
299                 IMoniker_GetDisplayName(mon, pbc, NULL, ppwzTarget);
300                 IBindCtx_Release(pbc);
301                 IMoniker_Release(mon);
302             }
303             else
304                 FIXME("Unhandled case, no set Target and no moniker\n");
305         }
306     }
307     if (ppwzLocation)
308         *ppwzLocation = hlink_co_strdupW( This->Location );
309
310     TRACE("(Target: %s Location: %s)\n",
311             (ppwzTarget)?debugstr_w(*ppwzTarget):"<NULL>",
312             (ppwzLocation)?debugstr_w(*ppwzLocation):"<NULL>");
313
314     return S_OK;
315 }
316
317 static HRESULT WINAPI IHlink_fnSetFriendlyName (IHlink *iface,
318         LPCWSTR pwzFriendlyName)
319 {
320     HlinkImpl  *This = (HlinkImpl*)iface;
321
322     TRACE("(%p) -> (%s)\n", This, debugstr_w(pwzFriendlyName));
323
324     heap_free(This->FriendlyName);
325     This->FriendlyName = hlink_strdupW( pwzFriendlyName );
326
327     return S_OK;
328 }
329
330 static HRESULT WINAPI IHlink_fnGetFriendlyName (IHlink* iface,
331         DWORD grfHLFNAMEF, LPWSTR* ppwzFriendlyName)
332 {
333     HlinkImpl  *This = (HlinkImpl*)iface;
334
335     TRACE("(%p) -> (%i %p)\n", This, grfHLFNAMEF, ppwzFriendlyName);
336
337     /* FIXME: Only using explicitly set and cached friendly names */
338
339     if (This->FriendlyName)
340         *ppwzFriendlyName = hlink_co_strdupW( This->FriendlyName );
341     else
342     {
343         IMoniker *moniker;
344         __GetMoniker(This, &moniker);
345         if (moniker)
346         {
347             IBindCtx *bcxt;
348             CreateBindCtx(0, &bcxt);
349
350             IMoniker_GetDisplayName(moniker, bcxt, NULL, ppwzFriendlyName);
351             IBindCtx_Release(bcxt);
352             IMoniker_Release(moniker);
353         }
354         else
355             *ppwzFriendlyName = NULL;
356     }
357
358     return S_OK;
359 }
360
361 static HRESULT WINAPI IHlink_fnSetTargetFrameName(IHlink* iface,
362         LPCWSTR pwzTargetFramename)
363 {
364     HlinkImpl  *This = (HlinkImpl*)iface;
365     TRACE("(%p)->(%s)\n", This, debugstr_w(pwzTargetFramename));
366
367     heap_free(This->TargetFrameName);
368     This->TargetFrameName = hlink_strdupW( pwzTargetFramename );
369
370     return S_OK;
371 }
372
373 static HRESULT WINAPI IHlink_fnGetTargetFrameName(IHlink* iface,
374         LPWSTR *ppwzTargetFrameName)
375 {
376     HlinkImpl  *This = (HlinkImpl*)iface;
377
378     TRACE("(%p)->(%p)\n", This, ppwzTargetFrameName);
379     *ppwzTargetFrameName = hlink_co_strdupW( This->TargetFrameName );
380
381     return S_OK;
382 }
383
384 static HRESULT WINAPI IHlink_fnGetMiscStatus(IHlink* iface, DWORD* pdwStatus)
385 {
386     FIXME("\n");
387     return E_NOTIMPL;
388 }
389
390 static HRESULT WINAPI IHlink_fnNavigate(IHlink* iface, DWORD grfHLNF, LPBC pbc,
391         IBindStatusCallback *pbsc, IHlinkBrowseContext *phbc)
392 {
393     HlinkImpl  *This = (HlinkImpl*)iface;
394     IMoniker *mon = NULL;
395
396     FIXME("Semi-Stub:(%p)->(%i %p %p %p)\n", This, grfHLNF, pbc, pbsc, phbc);
397
398     if (This->Site)
399         IHlinkSite_ReadyToNavigate(This->Site, This->SiteData, 0);
400
401     __GetMoniker(This, &mon);
402     TRACE("Moniker %p\n", mon);
403
404     if (mon)
405     {
406         IBindCtx *bcxt;
407         IHlinkTarget *target = NULL;
408         HRESULT r = S_OK;
409
410         CreateBindCtx(0, &bcxt);
411
412         RegisterBindStatusCallback(bcxt, pbsc, NULL, 0);
413
414         r = IMoniker_BindToObject(mon, bcxt, NULL, &IID_IHlinkTarget,
415                 (LPVOID*)&target);
416         TRACE("IHlinkTarget returned 0x%x\n", r);
417         if (r == S_OK)
418         {
419             IHlinkTarget_SetBrowseContext(target, phbc);
420             IHlinkTarget_Navigate(target, grfHLNF, This->Location);
421             IHlinkTarget_Release(target);
422         }
423         else
424         {
425             static const WCHAR szOpen[] = {'o','p','e','n',0};
426             LPWSTR target = NULL;
427
428             r = IHlink_GetStringReference(iface, HLINKGETREF_DEFAULT, &target, NULL);
429             if (SUCCEEDED(r) && target)
430             {
431                 ShellExecuteW(NULL, szOpen, target, NULL, NULL, SW_SHOW);
432                 CoTaskMemFree(target);
433             }
434         }
435
436         RevokeBindStatusCallback(bcxt, pbsc);
437
438         IBindCtx_Release(bcxt);
439         IMoniker_Release(mon);
440     }
441
442     if (This->Site)
443         IHlinkSite_OnNavigationComplete(This->Site, This->SiteData, 0, 0, NULL);
444
445     TRACE("Finished Navigation\n");
446     return S_OK;
447 }
448
449 static HRESULT WINAPI IHlink_fnSetAdditonalParams(IHlink* iface,
450         LPCWSTR pwzAdditionalParams)
451 {
452     TRACE("Not implemented in native IHlink\n");
453     return E_NOTIMPL;
454 }
455
456 static HRESULT WINAPI IHlink_fnGetAdditionalParams(IHlink* iface,
457         LPWSTR* ppwzAdditionalParams)
458 {
459     TRACE("Not implemented in native IHlink\n");
460     return E_NOTIMPL;
461 }
462
463 static const IHlinkVtbl hlvt =
464 {
465     IHlink_fnQueryInterface,
466     IHlink_fnAddRef,
467     IHlink_fnRelease,
468     IHlink_fnSetHlinkSite,
469     IHlink_fnGetHlinkSite,
470     IHlink_fnSetMonikerReference,
471     IHlink_fnGetMonikerReference,
472     IHlink_fnSetStringReference,
473     IHlink_fnGetStringReference,
474     IHlink_fnSetFriendlyName,
475     IHlink_fnGetFriendlyName,
476     IHlink_fnSetTargetFrameName,
477     IHlink_fnGetTargetFrameName,
478     IHlink_fnGetMiscStatus,
479     IHlink_fnNavigate,
480     IHlink_fnSetAdditonalParams,
481     IHlink_fnGetAdditionalParams
482 };
483
484 static HRESULT WINAPI IDataObject_fnQueryInterface(IDataObject* iface,
485         REFIID riid, LPVOID *ppvObj)
486 {
487     HlinkImpl *This = HlinkImpl_from_IDataObject(iface);
488     TRACE("%p\n", This);
489     return IHlink_QueryInterface((IHlink*)This, riid, ppvObj);
490 }
491
492 static ULONG WINAPI IDataObject_fnAddRef (IDataObject* iface)
493 {
494     HlinkImpl *This = HlinkImpl_from_IDataObject(iface);
495     TRACE("%p\n", This);
496     return IHlink_AddRef((IHlink*)This);
497 }
498
499 static ULONG WINAPI IDataObject_fnRelease (IDataObject* iface)
500 {
501     HlinkImpl *This = HlinkImpl_from_IDataObject(iface);
502     TRACE("%p\n", This);
503     return IHlink_Release((IHlink*)This);
504 }
505
506 static HRESULT WINAPI IDataObject_fnGetData(IDataObject* iface,
507         FORMATETC* pformatetcIn, STGMEDIUM* pmedium)
508 {
509     FIXME("\n");
510     return E_NOTIMPL;
511 }
512
513 static HRESULT WINAPI IDataObject_fnGetDataHere(IDataObject* iface,
514         FORMATETC* pformatetc, STGMEDIUM* pmedium)
515 {
516     FIXME("\n");
517     return E_NOTIMPL;
518 }
519
520 static HRESULT WINAPI IDataObject_fnQueryGetData(IDataObject* iface,
521         FORMATETC* pformatetc)
522 {
523     FIXME("\n");
524     return E_NOTIMPL;
525 }
526
527 static HRESULT WINAPI IDataObject_fnGetConicalFormatEtc(IDataObject* iface,
528         FORMATETC* pformatetcIn, FORMATETC* pformatetcOut)
529 {
530     FIXME("\n");
531     return E_NOTIMPL;
532 }
533
534 static HRESULT WINAPI IDataObject_fnSetData(IDataObject* iface,
535         FORMATETC* pformatetc, STGMEDIUM* pmedium, BOOL fRelease)
536 {
537     FIXME("\n");
538     return E_NOTIMPL;
539 }
540
541 static HRESULT WINAPI IDataObject_fnEnumFormatEtc(IDataObject* iface,
542         DWORD dwDirection, IEnumFORMATETC** ppenumFormatEtc)
543 {
544     FIXME("\n");
545     return E_NOTIMPL;
546 }
547
548 static HRESULT WINAPI IDataObject_fnDAdvise(IDataObject* iface,
549         FORMATETC* pformatetc, DWORD advf, IAdviseSink* pAdvSink,
550         DWORD* pdwConnection)
551 {
552     FIXME("\n");
553     return E_NOTIMPL;
554 }
555
556 static HRESULT WINAPI IDataObject_fnDUnadvise(IDataObject* iface,
557         DWORD dwConnection)
558 {
559     FIXME("\n");
560     return E_NOTIMPL;
561 }
562
563 static HRESULT WINAPI IDataObject_fnEnumDAdvise(IDataObject* iface,
564         IEnumSTATDATA** ppenumAdvise)
565 {
566     FIXME("\n");
567     return E_NOTIMPL;
568 }
569
570 static const IDataObjectVtbl dovt =
571 {
572     IDataObject_fnQueryInterface,
573     IDataObject_fnAddRef,
574     IDataObject_fnRelease,
575     IDataObject_fnGetData,
576     IDataObject_fnGetDataHere,
577     IDataObject_fnQueryGetData,
578     IDataObject_fnGetConicalFormatEtc,
579     IDataObject_fnSetData,
580     IDataObject_fnEnumFormatEtc,
581     IDataObject_fnDAdvise,
582     IDataObject_fnDUnadvise,
583     IDataObject_fnEnumDAdvise
584 };
585
586 static HRESULT WINAPI IPersistStream_fnQueryInterface(IPersistStream* iface,
587         REFIID riid, LPVOID *ppvObj)
588 {
589     HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
590     TRACE("(%p)\n", This);
591     return IHlink_QueryInterface((IHlink*)This, riid, ppvObj);
592 }
593
594 static ULONG WINAPI IPersistStream_fnAddRef (IPersistStream* iface)
595 {
596     HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
597     TRACE("(%p)\n", This);
598     return IHlink_AddRef((IHlink*)This);
599 }
600
601 static ULONG WINAPI IPersistStream_fnRelease (IPersistStream* iface)
602 {
603     HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
604     TRACE("(%p)\n", This);
605     return IHlink_Release((IHlink*)This);
606 }
607
608 static HRESULT WINAPI IPersistStream_fnGetClassID(IPersistStream* iface,
609         CLSID* pClassID)
610 {
611     HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
612     TRACE("(%p)\n", This);
613     *pClassID = CLSID_StdHlink;
614     return S_OK;
615 }
616
617 static HRESULT WINAPI IPersistStream_fnIsDirty(IPersistStream* iface)
618 {
619     FIXME("\n");
620     return E_NOTIMPL;
621 }
622
623 static HRESULT write_hlink_string(IStream *pStm, LPCWSTR str)
624 {
625     DWORD len;
626     HRESULT hr;
627
628     TRACE("(%p, %s)\n", pStm, debugstr_w(str));
629
630     len = strlenW(str) + 1;
631
632     hr = IStream_Write(pStm, &len, sizeof(len), NULL);
633     if (FAILED(hr)) return hr;
634
635     hr = IStream_Write(pStm, str, len * sizeof(WCHAR), NULL);
636     if (FAILED(hr)) return hr;
637
638     return S_OK;
639 }
640
641 static inline ULONG size_hlink_string(LPCWSTR str)
642 {
643     return sizeof(DWORD) + (strlenW(str) + 1) * sizeof(WCHAR);
644 }
645
646 static HRESULT read_hlink_string(IStream *pStm, LPWSTR *out_str)
647 {
648     LPWSTR str;
649     DWORD len;
650     ULONG read;
651     HRESULT hr;
652
653     hr = IStream_Read(pStm, &len, sizeof(len), &read);
654     if (FAILED(hr)) return hr;
655     if (read != sizeof(len)) return STG_E_READFAULT;
656
657     TRACE("read len %d\n", len);
658
659     str = heap_alloc(len * sizeof(WCHAR));
660     if (!str) return E_OUTOFMEMORY;
661
662     hr = IStream_Read(pStm, str, len * sizeof(WCHAR), &read);
663     if (FAILED(hr))
664     {
665         heap_free(str);
666         return hr;
667     }
668     if (read != len * sizeof(WCHAR))
669     {
670         heap_free(str);
671         return STG_E_READFAULT;
672     }
673     TRACE("read string %s\n", debugstr_w(str));
674
675     *out_str = str;
676     return S_OK;
677 }
678
679 static HRESULT WINAPI IPersistStream_fnLoad(IPersistStream* iface,
680         IStream* pStm)
681 {
682     HRESULT r;
683     DWORD hdr[2];
684     DWORD read;
685     HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
686
687     r = IStream_Read(pStm, hdr, sizeof(hdr), &read);
688     if (read != sizeof(hdr) || (hdr[0] != HLINK_SAVE_MAGIC))
689     {
690         r = E_FAIL;
691         goto end;
692     }
693     if (hdr[1] & ~HLINK_SAVE_ALL)
694         FIXME("unknown flag(s) 0x%x\n", hdr[1] & ~HLINK_SAVE_ALL);
695
696     if (hdr[1] & HLINK_SAVE_TARGET_FRAME_PRESENT)
697     {
698         TRACE("loading target frame name\n");
699         r = read_hlink_string(pStm, &This->TargetFrameName);
700         if (FAILED(r)) goto end;
701     }
702
703     if (hdr[1] & HLINK_SAVE_FRIENDLY_PRESENT)
704     {
705         TRACE("loading target friendly name\n");
706         if (!(hdr[1] & 0x4))
707             FIXME("0x4 flag not present with friendly name flag - not sure what this means\n");
708         r = read_hlink_string(pStm, &This->FriendlyName);
709         if (FAILED(r)) goto end;
710     }
711
712     if (hdr[1] & HLINK_SAVE_MONIKER_PRESENT)
713     {
714         TRACE("loading moniker\n");
715         r = OleLoadFromStream(pStm, &IID_IMoniker, (LPVOID*)&(This->Moniker));
716         if (FAILED(r))
717             goto end;
718         This->absolute = hdr[1] & HLINK_SAVE_MONIKER_IS_ABSOLUTE ? TRUE : FALSE;
719     }
720
721     if (hdr[1] & HLINK_SAVE_LOCATION_PRESENT)
722     {
723         TRACE("loading location\n");
724         r = read_hlink_string(pStm, &This->Location);
725         if (FAILED(r)) goto end;
726     }
727
728 end:
729     TRACE("Load Result 0x%x (%p)\n", r, This->Moniker);
730
731     return r;
732 }
733
734 static HRESULT WINAPI IPersistStream_fnSave(IPersistStream* iface,
735         IStream* pStm, BOOL fClearDirty)
736 {
737     HRESULT r = E_FAIL;
738     HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
739     DWORD hdr[2];
740     IMoniker *moniker;
741
742     TRACE("(%p) Moniker(%p)\n", This, This->Moniker);
743
744     __GetMoniker(This, &moniker);
745
746     hdr[0] = HLINK_SAVE_MAGIC;
747     hdr[1] = 0;
748
749     if (moniker)
750         hdr[1] |= HLINK_SAVE_MONIKER_PRESENT;
751     if (This->absolute)
752         hdr[1] |= HLINK_SAVE_MONIKER_IS_ABSOLUTE;
753     if (This->Location)
754         hdr[1] |= HLINK_SAVE_LOCATION_PRESENT;
755     if (This->FriendlyName)
756         hdr[1] |= HLINK_SAVE_FRIENDLY_PRESENT | 4 /* FIXME */;
757     if (This->TargetFrameName)
758         hdr[1] |= HLINK_SAVE_TARGET_FRAME_PRESENT;
759
760     IStream_Write(pStm, hdr, sizeof(hdr), NULL);
761
762     if (This->TargetFrameName)
763     {
764         r = write_hlink_string(pStm, This->TargetFrameName);
765         if (FAILED(r)) goto end;
766     }
767
768     if (This->FriendlyName)
769     {
770         r = write_hlink_string(pStm, This->FriendlyName);
771         if (FAILED(r)) goto end;
772     }
773
774     if (moniker)
775     {
776         IPersistStream* monstream;
777
778         monstream = NULL;
779         IMoniker_QueryInterface(moniker, &IID_IPersistStream,
780                 (LPVOID*)&monstream);
781         if (monstream)
782         {
783             r = OleSaveToStream(monstream, pStm);
784             IPersistStream_Release(monstream);
785         }
786         if (FAILED(r)) goto end;
787     }
788
789     if (This->Location)
790     {
791         r = write_hlink_string(pStm, This->Location);
792         if (FAILED(r)) goto end;
793     }
794
795 end:
796     if (moniker) IMoniker_Release(moniker);
797     TRACE("Save Result 0x%x\n", r);
798
799     return r;
800 }
801
802 static HRESULT WINAPI IPersistStream_fnGetSizeMax(IPersistStream* iface,
803         ULARGE_INTEGER* pcbSize)
804 {
805     HRESULT r = E_FAIL;
806     HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
807     IMoniker *moniker;
808
809     TRACE("(%p) Moniker(%p)\n", This, This->Moniker);
810
811     pcbSize->QuadPart = sizeof(DWORD)*2;
812
813     if (This->TargetFrameName)
814         pcbSize->QuadPart += size_hlink_string(This->TargetFrameName);
815
816     if (This->FriendlyName)
817         pcbSize->QuadPart += size_hlink_string(This->FriendlyName);
818
819     __GetMoniker(This, &moniker);
820     if (moniker)
821     {
822         IPersistStream* monstream = NULL;
823         IMoniker_QueryInterface(moniker, &IID_IPersistStream,
824                 (LPVOID*)&monstream);
825         if (monstream)
826         {
827             ULARGE_INTEGER mon_size;
828             r = IPersistStream_GetSizeMax(monstream, &mon_size);
829             pcbSize->QuadPart += mon_size.QuadPart;
830             IPersistStream_Release(monstream);
831         }
832         IMoniker_Release(moniker);
833     }
834
835     if (This->Location)
836         pcbSize->QuadPart += size_hlink_string(This->Location);
837
838     return r;
839 }
840
841 static const IPersistStreamVtbl psvt =
842 {
843     IPersistStream_fnQueryInterface,
844     IPersistStream_fnAddRef,
845     IPersistStream_fnRelease,
846     IPersistStream_fnGetClassID,
847     IPersistStream_fnIsDirty,
848     IPersistStream_fnLoad,
849     IPersistStream_fnSave,
850     IPersistStream_fnGetSizeMax,
851 };