ws2_32/tests: Avoid sizeof in traces.
[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     FIXME("(%p)->(%i %p %s)\n", This, rfHLSETF, pmkTarget,
210             debugstr_w(pwzLocation));
211
212     if (This->Moniker)
213         IMoniker_Release(This->Moniker);
214
215     This->Moniker = pmkTarget;
216     if (This->Moniker)
217     {
218         LPOLESTR display_name;
219         IMoniker_AddRef(This->Moniker);
220         IMoniker_GetDisplayName(This->Moniker, NULL, NULL, &display_name);
221         This->absolute = display_name && strchrW(display_name, ':');
222         CoTaskMemFree(display_name);
223     }
224
225     heap_free(This->Location);
226     This->Location = hlink_strdupW( pwzLocation );
227
228     return S_OK;
229 }
230
231 static HRESULT WINAPI IHlink_fnSetStringReference(IHlink* iface,
232         DWORD grfHLSETF, LPCWSTR pwzTarget, LPCWSTR pwzLocation)
233 {
234     HlinkImpl  *This = (HlinkImpl*)iface;
235
236     TRACE("(%p)->(%i %s %s)\n", This, grfHLSETF, debugstr_w(pwzTarget),
237             debugstr_w(pwzLocation));
238
239     if (grfHLSETF & HLINKSETF_TARGET)
240     {
241         heap_free(This->Target);
242         This->Target = hlink_strdupW( pwzTarget );
243     }
244     if (grfHLSETF & HLINKSETF_LOCATION)
245     {
246         heap_free(This->Location);
247         This->Location = hlink_strdupW( pwzLocation );
248     }
249
250     return S_OK;
251 }
252
253 static HRESULT WINAPI IHlink_fnGetMonikerReference(IHlink* iface,
254         DWORD dwWhichRef, IMoniker **ppimkTarget, LPWSTR *ppwzLocation)
255 {
256     HlinkImpl  *This = (HlinkImpl*)iface;
257
258     TRACE("(%p) -> (%i %p %p)\n", This, dwWhichRef, ppimkTarget,
259             ppwzLocation);
260
261     if(ppimkTarget)
262         __GetMoniker(This, ppimkTarget);
263
264     if (ppwzLocation)
265         IHlink_GetStringReference(iface, dwWhichRef, NULL, ppwzLocation);
266
267     return S_OK;
268 }
269
270 static HRESULT WINAPI IHlink_fnGetStringReference (IHlink* iface,
271         DWORD dwWhichRef, LPWSTR *ppwzTarget, LPWSTR *ppwzLocation)
272 {
273     HlinkImpl  *This = (HlinkImpl*)iface;
274
275     FIXME("(%p) -> (%i %p %p)\n", This, dwWhichRef, ppwzTarget, ppwzLocation);
276
277     if (ppwzTarget)
278     {
279         *ppwzTarget = hlink_co_strdupW( This->Target );
280
281         if (!This->Target)
282         {
283             IMoniker* mon;
284             __GetMoniker(This, &mon);
285             if (mon)
286             {
287                 IBindCtx *pbc;
288
289                 CreateBindCtx( 0, &pbc);
290                 IMoniker_GetDisplayName(mon, pbc, NULL, ppwzTarget);
291                 IBindCtx_Release(pbc);
292                 IMoniker_Release(mon);
293             }
294             else
295                 FIXME("Unhandled case, no set Target and no moniker\n");
296         }
297     }
298     if (ppwzLocation)
299         *ppwzLocation = hlink_co_strdupW( This->Location );
300
301     TRACE("(Target: %s Location: %s)\n",
302             (ppwzTarget)?debugstr_w(*ppwzTarget):"<NULL>",
303             (ppwzLocation)?debugstr_w(*ppwzLocation):"<NULL>");
304
305     return S_OK;
306 }
307
308 static HRESULT WINAPI IHlink_fnSetFriendlyName (IHlink *iface,
309         LPCWSTR pwzFriendlyName)
310 {
311     HlinkImpl  *This = (HlinkImpl*)iface;
312
313     TRACE("(%p) -> (%s)\n", This, debugstr_w(pwzFriendlyName));
314
315     heap_free(This->FriendlyName);
316     This->FriendlyName = hlink_strdupW( pwzFriendlyName );
317
318     return S_OK;
319 }
320
321 static HRESULT WINAPI IHlink_fnGetFriendlyName (IHlink* iface,
322         DWORD grfHLFNAMEF, LPWSTR* ppwzFriendlyName)
323 {
324     HlinkImpl  *This = (HlinkImpl*)iface;
325
326     TRACE("(%p) -> (%i %p)\n", This, grfHLFNAMEF, ppwzFriendlyName);
327
328     /* FIXME: Only using explicitly set and cached friendly names */
329
330     if (This->FriendlyName)
331         *ppwzFriendlyName = hlink_co_strdupW( This->FriendlyName );
332     else
333     {
334         IMoniker *moniker;
335         __GetMoniker(This, &moniker);
336         if (moniker)
337         {
338             IBindCtx *bcxt;
339             CreateBindCtx(0, &bcxt);
340
341             IMoniker_GetDisplayName(moniker, bcxt, NULL, ppwzFriendlyName);
342             IBindCtx_Release(bcxt);
343             IMoniker_Release(moniker);
344         }
345         else
346             *ppwzFriendlyName = NULL;
347     }
348
349     return S_OK;
350 }
351
352 static HRESULT WINAPI IHlink_fnSetTargetFrameName(IHlink* iface,
353         LPCWSTR pwzTargetFramename)
354 {
355     HlinkImpl  *This = (HlinkImpl*)iface;
356     TRACE("(%p)->(%s)\n", This, debugstr_w(pwzTargetFramename));
357
358     heap_free(This->TargetFrameName);
359     This->TargetFrameName = hlink_strdupW( pwzTargetFramename );
360
361     return S_OK;
362 }
363
364 static HRESULT WINAPI IHlink_fnGetTargetFrameName(IHlink* iface,
365         LPWSTR *ppwzTargetFrameName)
366 {
367     HlinkImpl  *This = (HlinkImpl*)iface;
368
369     TRACE("(%p)->(%p)\n", This, ppwzTargetFrameName);
370     *ppwzTargetFrameName = hlink_co_strdupW( This->TargetFrameName );
371
372     return S_OK;
373 }
374
375 static HRESULT WINAPI IHlink_fnGetMiscStatus(IHlink* iface, DWORD* pdwStatus)
376 {
377     FIXME("\n");
378     return E_NOTIMPL;
379 }
380
381 static HRESULT WINAPI IHlink_fnNavigate(IHlink* iface, DWORD grfHLNF, LPBC pbc,
382         IBindStatusCallback *pbsc, IHlinkBrowseContext *phbc)
383 {
384     HlinkImpl  *This = (HlinkImpl*)iface;
385     IMoniker *mon = NULL;
386
387     FIXME("Semi-Stub:(%p)->(%i %p %p %p)\n", This, grfHLNF, pbc, pbsc, phbc);
388
389     if (This->Site)
390         IHlinkSite_ReadyToNavigate(This->Site, This->SiteData, 0);
391
392     __GetMoniker(This, &mon);
393     TRACE("Moniker %p\n", mon);
394
395     if (mon)
396     {
397         IBindCtx *bcxt;
398         IHlinkTarget *target = NULL;
399         HRESULT r = S_OK;
400
401         CreateBindCtx(0, &bcxt);
402
403         RegisterBindStatusCallback(bcxt, pbsc, NULL, 0);
404
405         r = IMoniker_BindToObject(mon, bcxt, NULL, &IID_IHlinkTarget,
406                 (LPVOID*)&target);
407         TRACE("IHlinkTarget returned 0x%x\n", r);
408         if (r == S_OK)
409         {
410             IHlinkTarget_SetBrowseContext(target, phbc);
411             IHlinkTarget_Navigate(target, grfHLNF, This->Location);
412             IHlinkTarget_Release(target);
413         }
414         else
415         {
416             static const WCHAR szOpen[] = {'o','p','e','n',0};
417             LPWSTR target = NULL;
418
419             r = IHlink_GetStringReference(iface, HLINKGETREF_DEFAULT, &target, NULL);
420             if (SUCCEEDED(r) && target)
421             {
422                 ShellExecuteW(NULL, szOpen, target, NULL, NULL, SW_SHOW);
423                 CoTaskMemFree(target);
424             }
425         }
426
427         RevokeBindStatusCallback(bcxt, pbsc);
428
429         IBindCtx_Release(bcxt);
430         IMoniker_Release(mon);
431     }
432
433     if (This->Site)
434         IHlinkSite_OnNavigationComplete(This->Site, This->SiteData, 0, 0, NULL);
435
436     TRACE("Finished Navigation\n");
437     return S_OK;
438 }
439
440 static HRESULT WINAPI IHlink_fnSetAdditonalParams(IHlink* iface,
441         LPCWSTR pwzAdditionalParams)
442 {
443     TRACE("Not implemented in native IHlink\n");
444     return E_NOTIMPL;
445 }
446
447 static HRESULT WINAPI IHlink_fnGetAdditionalParams(IHlink* iface,
448         LPWSTR* ppwzAdditionalParams)
449 {
450     TRACE("Not implemented in native IHlink\n");
451     return E_NOTIMPL;
452 }
453
454 static const IHlinkVtbl hlvt =
455 {
456     IHlink_fnQueryInterface,
457     IHlink_fnAddRef,
458     IHlink_fnRelease,
459     IHlink_fnSetHlinkSite,
460     IHlink_fnGetHlinkSite,
461     IHlink_fnSetMonikerReference,
462     IHlink_fnGetMonikerReference,
463     IHlink_fnSetStringReference,
464     IHlink_fnGetStringReference,
465     IHlink_fnSetFriendlyName,
466     IHlink_fnGetFriendlyName,
467     IHlink_fnSetTargetFrameName,
468     IHlink_fnGetTargetFrameName,
469     IHlink_fnGetMiscStatus,
470     IHlink_fnNavigate,
471     IHlink_fnSetAdditonalParams,
472     IHlink_fnGetAdditionalParams
473 };
474
475 static HRESULT WINAPI IDataObject_fnQueryInterface(IDataObject* iface,
476         REFIID riid, LPVOID *ppvObj)
477 {
478     HlinkImpl *This = HlinkImpl_from_IDataObject(iface);
479     TRACE("%p\n", This);
480     return IHlink_QueryInterface((IHlink*)This, riid, ppvObj);
481 }
482
483 static ULONG WINAPI IDataObject_fnAddRef (IDataObject* iface)
484 {
485     HlinkImpl *This = HlinkImpl_from_IDataObject(iface);
486     TRACE("%p\n", This);
487     return IHlink_AddRef((IHlink*)This);
488 }
489
490 static ULONG WINAPI IDataObject_fnRelease (IDataObject* iface)
491 {
492     HlinkImpl *This = HlinkImpl_from_IDataObject(iface);
493     TRACE("%p\n", This);
494     return IHlink_Release((IHlink*)This);
495 }
496
497 static HRESULT WINAPI IDataObject_fnGetData(IDataObject* iface,
498         FORMATETC* pformatetcIn, STGMEDIUM* pmedium)
499 {
500     FIXME("\n");
501     return E_NOTIMPL;
502 }
503
504 static HRESULT WINAPI IDataObject_fnGetDataHere(IDataObject* iface,
505         FORMATETC* pformatetc, STGMEDIUM* pmedium)
506 {
507     FIXME("\n");
508     return E_NOTIMPL;
509 }
510
511 static HRESULT WINAPI IDataObject_fnQueryGetData(IDataObject* iface,
512         FORMATETC* pformatetc)
513 {
514     FIXME("\n");
515     return E_NOTIMPL;
516 }
517
518 static HRESULT WINAPI IDataObject_fnGetConicalFormatEtc(IDataObject* iface,
519         FORMATETC* pformatetcIn, FORMATETC* pformatetcOut)
520 {
521     FIXME("\n");
522     return E_NOTIMPL;
523 }
524
525 static HRESULT WINAPI IDataObject_fnSetData(IDataObject* iface,
526         FORMATETC* pformatetc, STGMEDIUM* pmedium, BOOL fRelease)
527 {
528     FIXME("\n");
529     return E_NOTIMPL;
530 }
531
532 static HRESULT WINAPI IDataObject_fnEnumFormatEtc(IDataObject* iface,
533         DWORD dwDirection, IEnumFORMATETC** ppenumFormatEtc)
534 {
535     FIXME("\n");
536     return E_NOTIMPL;
537 }
538
539 static HRESULT WINAPI IDataObject_fnDAdvise(IDataObject* iface,
540         FORMATETC* pformatetc, DWORD advf, IAdviseSink* pAdvSink,
541         DWORD* pdwConnection)
542 {
543     FIXME("\n");
544     return E_NOTIMPL;
545 }
546
547 static HRESULT WINAPI IDataObject_fnDUnadvise(IDataObject* iface,
548         DWORD dwConnection)
549 {
550     FIXME("\n");
551     return E_NOTIMPL;
552 }
553
554 static HRESULT WINAPI IDataObject_fnEnumDAdvise(IDataObject* iface,
555         IEnumSTATDATA** ppenumAdvise)
556 {
557     FIXME("\n");
558     return E_NOTIMPL;
559 }
560
561 static const IDataObjectVtbl dovt =
562 {
563     IDataObject_fnQueryInterface,
564     IDataObject_fnAddRef,
565     IDataObject_fnRelease,
566     IDataObject_fnGetData,
567     IDataObject_fnGetDataHere,
568     IDataObject_fnQueryGetData,
569     IDataObject_fnGetConicalFormatEtc,
570     IDataObject_fnSetData,
571     IDataObject_fnEnumFormatEtc,
572     IDataObject_fnDAdvise,
573     IDataObject_fnDUnadvise,
574     IDataObject_fnEnumDAdvise
575 };
576
577 static HRESULT WINAPI IPersistStream_fnQueryInterface(IPersistStream* iface,
578         REFIID riid, LPVOID *ppvObj)
579 {
580     HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
581     TRACE("(%p)\n", This);
582     return IHlink_QueryInterface((IHlink*)This, riid, ppvObj);
583 }
584
585 static ULONG WINAPI IPersistStream_fnAddRef (IPersistStream* iface)
586 {
587     HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
588     TRACE("(%p)\n", This);
589     return IHlink_AddRef((IHlink*)This);
590 }
591
592 static ULONG WINAPI IPersistStream_fnRelease (IPersistStream* iface)
593 {
594     HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
595     TRACE("(%p)\n", This);
596     return IHlink_Release((IHlink*)This);
597 }
598
599 static HRESULT WINAPI IPersistStream_fnGetClassID(IPersistStream* iface,
600         CLSID* pClassID)
601 {
602     HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
603     TRACE("(%p)\n", This);
604     *pClassID = CLSID_StdHlink;
605     return S_OK;
606 }
607
608 static HRESULT WINAPI IPersistStream_fnIsDirty(IPersistStream* iface)
609 {
610     FIXME("\n");
611     return E_NOTIMPL;
612 }
613
614 static HRESULT write_hlink_string(IStream *pStm, LPCWSTR str)
615 {
616     DWORD len;
617     HRESULT hr;
618
619     TRACE("(%p, %s)\n", pStm, debugstr_w(str));
620
621     len = strlenW(str) + 1;
622
623     hr = IStream_Write(pStm, &len, sizeof(len), NULL);
624     if (FAILED(hr)) return hr;
625
626     hr = IStream_Write(pStm, str, len * sizeof(WCHAR), NULL);
627     if (FAILED(hr)) return hr;
628
629     return S_OK;
630 }
631
632 static inline ULONG size_hlink_string(LPCWSTR str)
633 {
634     return sizeof(DWORD) + (strlenW(str) + 1) * sizeof(WCHAR);
635 }
636
637 static HRESULT read_hlink_string(IStream *pStm, LPWSTR *out_str)
638 {
639     LPWSTR str;
640     DWORD len;
641     ULONG read;
642     HRESULT hr;
643
644     hr = IStream_Read(pStm, &len, sizeof(len), &read);
645     if (FAILED(hr)) return hr;
646     if (read != sizeof(len)) return STG_E_READFAULT;
647
648     TRACE("read len %d\n", len);
649
650     str = heap_alloc(len * sizeof(WCHAR));
651     if (!str) return E_OUTOFMEMORY;
652
653     hr = IStream_Read(pStm, str, len * sizeof(WCHAR), &read);
654     if (FAILED(hr))
655     {
656         heap_free(str);
657         return hr;
658     }
659     if (read != len * sizeof(WCHAR))
660     {
661         heap_free(str);
662         return STG_E_READFAULT;
663     }
664     TRACE("read string %s\n", debugstr_w(str));
665
666     *out_str = str;
667     return S_OK;
668 }
669
670 static HRESULT WINAPI IPersistStream_fnLoad(IPersistStream* iface,
671         IStream* pStm)
672 {
673     HRESULT r;
674     DWORD hdr[2];
675     DWORD read;
676     HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
677
678     r = IStream_Read(pStm, hdr, sizeof(hdr), &read);
679     if (read != sizeof(hdr) || (hdr[0] != HLINK_SAVE_MAGIC))
680     {
681         r = E_FAIL;
682         goto end;
683     }
684     if (hdr[1] & ~HLINK_SAVE_ALL)
685         FIXME("unknown flag(s) 0x%x\n", hdr[1] & ~HLINK_SAVE_ALL);
686
687     if (hdr[1] & HLINK_SAVE_TARGET_FRAME_PRESENT)
688     {
689         TRACE("loading target frame name\n");
690         r = read_hlink_string(pStm, &This->TargetFrameName);
691         if (FAILED(r)) goto end;
692     }
693
694     if (hdr[1] & HLINK_SAVE_FRIENDLY_PRESENT)
695     {
696         TRACE("loading target friendly name\n");
697         if (!(hdr[1] & 0x4))
698             FIXME("0x4 flag not present with friendly name flag - not sure what this means\n");
699         r = read_hlink_string(pStm, &This->FriendlyName);
700         if (FAILED(r)) goto end;
701     }
702
703     if (hdr[1] & HLINK_SAVE_MONIKER_PRESENT)
704     {
705         TRACE("loading moniker\n");
706         r = OleLoadFromStream(pStm, &IID_IMoniker, (LPVOID*)&(This->Moniker));
707         if (FAILED(r))
708             goto end;
709         This->absolute = hdr[1] & HLINK_SAVE_MONIKER_IS_ABSOLUTE ? TRUE : FALSE;
710     }
711
712     if (hdr[1] & HLINK_SAVE_LOCATION_PRESENT)
713     {
714         TRACE("loading location\n");
715         r = read_hlink_string(pStm, &This->Location);
716         if (FAILED(r)) goto end;
717     }
718
719 end:
720     TRACE("Load Result 0x%x (%p)\n", r, This->Moniker);
721
722     return r;
723 }
724
725 static HRESULT WINAPI IPersistStream_fnSave(IPersistStream* iface,
726         IStream* pStm, BOOL fClearDirty)
727 {
728     HRESULT r = E_FAIL;
729     HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
730     DWORD hdr[2];
731     IMoniker *moniker;
732
733     TRACE("(%p) Moniker(%p)\n", This, This->Moniker);
734
735     __GetMoniker(This, &moniker);
736
737     hdr[0] = HLINK_SAVE_MAGIC;
738     hdr[1] = 0;
739
740     if (moniker)
741         hdr[1] |= HLINK_SAVE_MONIKER_PRESENT;
742     if (This->absolute)
743         hdr[1] |= HLINK_SAVE_MONIKER_IS_ABSOLUTE;
744     if (This->Location)
745         hdr[1] |= HLINK_SAVE_LOCATION_PRESENT;
746     if (This->FriendlyName)
747         hdr[1] |= HLINK_SAVE_FRIENDLY_PRESENT | 4 /* FIXME */;
748     if (This->TargetFrameName)
749         hdr[1] |= HLINK_SAVE_TARGET_FRAME_PRESENT;
750
751     IStream_Write(pStm, hdr, sizeof(hdr), NULL);
752
753     if (This->TargetFrameName)
754     {
755         r = write_hlink_string(pStm, This->TargetFrameName);
756         if (FAILED(r)) goto end;
757     }
758
759     if (This->FriendlyName)
760     {
761         r = write_hlink_string(pStm, This->FriendlyName);
762         if (FAILED(r)) goto end;
763     }
764
765     if (moniker)
766     {
767         IPersistStream* monstream;
768
769         monstream = NULL;
770         IMoniker_QueryInterface(moniker, &IID_IPersistStream,
771                 (LPVOID*)&monstream);
772         if (monstream)
773         {
774             r = OleSaveToStream(monstream, pStm);
775             IPersistStream_Release(monstream);
776         }
777         if (FAILED(r)) goto end;
778     }
779
780     if (This->Location)
781     {
782         r = write_hlink_string(pStm, This->Location);
783         if (FAILED(r)) goto end;
784     }
785
786 end:
787     if (moniker) IMoniker_Release(moniker);
788     TRACE("Save Result 0x%x\n", r);
789
790     return r;
791 }
792
793 static HRESULT WINAPI IPersistStream_fnGetSizeMax(IPersistStream* iface,
794         ULARGE_INTEGER* pcbSize)
795 {
796     HRESULT r = E_FAIL;
797     HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
798     IMoniker *moniker;
799
800     TRACE("(%p) Moniker(%p)\n", This, This->Moniker);
801
802     pcbSize->QuadPart = sizeof(DWORD)*2;
803
804     if (This->TargetFrameName)
805         pcbSize->QuadPart += size_hlink_string(This->TargetFrameName);
806
807     if (This->FriendlyName)
808         pcbSize->QuadPart += size_hlink_string(This->FriendlyName);
809
810     __GetMoniker(This, &moniker);
811     if (moniker)
812     {
813         IPersistStream* monstream = NULL;
814         IMoniker_QueryInterface(moniker, &IID_IPersistStream,
815                 (LPVOID*)&monstream);
816         if (monstream)
817         {
818             ULARGE_INTEGER mon_size;
819             r = IPersistStream_GetSizeMax(monstream, &mon_size);
820             pcbSize->QuadPart += mon_size.QuadPart;
821             IPersistStream_Release(monstream);
822         }
823         IMoniker_Release(moniker);
824     }
825
826     if (This->Location)
827         pcbSize->QuadPart += size_hlink_string(This->Location);
828
829     return r;
830 }
831
832 static const IPersistStreamVtbl psvt =
833 {
834     IPersistStream_fnQueryInterface,
835     IPersistStream_fnAddRef,
836     IPersistStream_fnRelease,
837     IPersistStream_fnGetClassID,
838     IPersistStream_fnIsDirty,
839     IPersistStream_fnLoad,
840     IPersistStream_fnSave,
841     IPersistStream_fnGetSizeMax,
842 };