Commit 0f063c72 authored by Rob Shearman's avatar Rob Shearman Committed by Alexandre Julliard

hlink: Improve the saving of hlinks by documenting the unknown header values and…

hlink: Improve the saving of hlinks by documenting the unknown header values and saving out more data. Add tests for saving hlinks with different properties.
parent bc061064
......@@ -32,11 +32,21 @@
#include "shellapi.h"
#include "wine/debug.h"
#include "wine/unicode.h"
#include "hlink.h"
#include "hlguids.h"
WINE_DEFAULT_DEBUG_CHANNEL(hlink);
#define HLINK_SAVE_MAGIC 0x00000002
#define HLINK_SAVE_MONIKER_PRESENT 0x01
#define HLINK_SAVE_MONIKER_IS_ABSOLUTE 0x02
#define HLINK_SAVE_LOCATION_PRESENT 0x08
#define HLINK_SAVE_FRIENDLY_PRESENT 0x10
/* 0x20, 0x40 unknown */
#define HLINK_SAVE_TARGET_FRAME_PRESENT 0x80
static const IHlinkVtbl hlvt;
static const IPersistStreamVtbl psvt;
static const IDataObjectVtbl dovt;
......@@ -56,6 +66,7 @@ typedef struct
IMoniker *Moniker;
IHlinkSite *Site;
DWORD SiteData;
BOOL absolute;
} HlinkImpl;
......@@ -242,7 +253,13 @@ static HRESULT WINAPI IHlink_fnSetMonikerReference( IHlink* iface,
This->Moniker = pmkTarget;
if (This->Moniker)
{
LPOLESTR display_name;
IMoniker_AddRef(This->Moniker);
IMoniker_GetDisplayName(This->Moniker, NULL, NULL, &display_name);
This->absolute = display_name && strchrW(display_name, ':');
CoTaskMemFree(display_name);
}
HeapFree(GetProcessHeap(), 0, This->Location);
This->Location = strdupW( pwzLocation );
......@@ -650,6 +667,29 @@ static HRESULT WINAPI IPersistStream_fnLoad(IPersistStream* iface,
return r;
}
static HRESULT write_hlink_string(IStream *pStm, LPCWSTR str)
{
DWORD len;
HRESULT hr;
TRACE("(%p, %s)\n", pStm, debugstr_w(str));
len = strlenW(str) + 1;
hr = IStream_Write(pStm, &len, sizeof(len), NULL);
/* FIXME: error checking */
hr = IStream_Write(pStm, str, len * sizeof(WCHAR), NULL);
/* FIXME: error checking */
return S_OK;
}
static inline ULONG size_hlink_string(LPCWSTR str)
{
return sizeof(DWORD) + (strlenW(str) + 1) * sizeof(WCHAR);
}
static HRESULT WINAPI IPersistStream_fnSave(IPersistStream* iface,
IStream* pStm, BOOL fClearDirty)
{
......@@ -658,17 +698,41 @@ static HRESULT WINAPI IPersistStream_fnSave(IPersistStream* iface,
DWORD hdr[2];
IMoniker *moniker;
FIXME("(%p) Moniker(%p)\n", This, This->Moniker);
TRACE("(%p) Moniker(%p)\n", This, This->Moniker);
__GetMoniker(This, &moniker);
hdr[0] = HLINK_SAVE_MAGIC;
hdr[1] = 0;
if (moniker)
hdr[1] |= HLINK_SAVE_MONIKER_PRESENT;
if (This->absolute)
hdr[1] |= HLINK_SAVE_MONIKER_IS_ABSOLUTE;
if (This->Location)
hdr[1] |= HLINK_SAVE_LOCATION_PRESENT;
if (This->FriendlyName)
hdr[1] |= HLINK_SAVE_FRIENDLY_PRESENT | 4 /* FIXME */;
if (This->TargetFrameName)
hdr[1] |= HLINK_SAVE_TARGET_FRAME_PRESENT;
IStream_Write(pStm, &hdr, sizeof(hdr), NULL);
if (This->TargetFrameName)
{
IPersistStream* monstream;
/* FIXME: Unknown values in the header */
hdr[0] = 2;
hdr[1] = 2;
r = write_hlink_string(pStm, This->TargetFrameName);
if (FAILED(r)) goto end;
}
if (This->FriendlyName)
{
r = write_hlink_string(pStm, This->FriendlyName);
if (FAILED(r)) goto end;
}
IStream_Write(pStm, &hdr, sizeof(hdr), NULL);
if (moniker)
{
IPersistStream* monstream;
monstream = NULL;
IMoniker_QueryInterface(moniker, &IID_IPersistStream,
......@@ -680,6 +744,14 @@ static HRESULT WINAPI IPersistStream_fnSave(IPersistStream* iface,
}
IMoniker_Release(moniker);
}
if (This->Location)
{
r = write_hlink_string(pStm, This->Location);
if (FAILED(r)) goto end;
}
end:
TRACE("Save Result 0x%x\n", r);
return r;
......@@ -692,7 +764,15 @@ static HRESULT WINAPI IPersistStream_fnGetSizeMax(IPersistStream* iface,
HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
IMoniker *moniker;
FIXME("(%p) Moniker(%p)\n", This, This->Moniker);
TRACE("(%p) Moniker(%p)\n", This, This->Moniker);
pcbSize->QuadPart = sizeof(DWORD)*2;
if (This->TargetFrameName)
pcbSize->QuadPart += size_hlink_string(This->TargetFrameName);
if (This->FriendlyName)
pcbSize->QuadPart += size_hlink_string(This->FriendlyName);
__GetMoniker(This, &moniker);
if (moniker)
......@@ -702,14 +782,17 @@ static HRESULT WINAPI IPersistStream_fnGetSizeMax(IPersistStream* iface,
(LPVOID*)&monstream);
if (monstream)
{
r = IPersistStream_GetSizeMax(monstream, pcbSize);
/* FIXME: Handle ULARGE_INTEGER correctly */
pcbSize->u.LowPart += sizeof(DWORD)*2;
ULARGE_INTEGER mon_size;
r = IPersistStream_GetSizeMax(monstream, &mon_size);
pcbSize->QuadPart += mon_size.QuadPart;
IPersistStream_Release(monstream);
}
IMoniker_Release(moniker);
}
if (This->Location)
pcbSize->QuadPart += size_hlink_string(This->Location);
return r;
}
......
......@@ -20,6 +20,8 @@
#define COBJMACROS
#include <stdio.h>
#include <hlink.h>
#include <hlguids.h>
......@@ -61,7 +63,7 @@ static void test_HlinkIsShortcut(void)
}
}
START_TEST(hlink)
static void test_reference(void)
{
HRESULT r;
IHlink *lnk = NULL;
......@@ -70,10 +72,8 @@ START_TEST(hlink)
const WCHAR url2[] = { 'h','t','t','p',':','/','/','w','i','n','e','h','q','.','o','r','g','/',0 };
LPWSTR str = NULL;
CoInitialize(NULL);
r = HlinkCreateFromString(url, NULL, NULL, NULL,
0, NULL, &IID_IHlink, (LPVOID*) &lnk);
0, NULL, &IID_IHlink, (LPVOID*) &lnk);
ok(r == S_OK, "failed to create link\n");
if (FAILED(r))
return;
......@@ -105,6 +105,224 @@ START_TEST(hlink)
r = IHlink_GetStringReference(lnk, HLINKGETREF_DEFAULT, NULL, &str);
ok(r == S_OK, "failed\n");
ok(str == NULL, "string should be null\n");
}
/* url only */
static const unsigned char expected_hlink_data[] =
{
0x02,0x00,0x00,0x00,0x03,0x00,0x00,0x00,
0xe0,0xc9,0xea,0x79,0xf9,0xba,0xce,0x11,
0x8c,0x82,0x00,0xaa,0x00,0x4b,0xa9,0x0b,
0x26,0x00,0x00,0x00,0x68,0x00,0x74,0x00,
0x74,0x00,0x70,0x00,0x3a,0x00,0x2f,0x00,
0x2f,0x00,0x77,0x00,0x69,0x00,0x6e,0x00,
0x65,0x00,0x68,0x00,0x71,0x00,0x2e,0x00,
0x6f,0x00,0x72,0x00,0x67,0x00,0x2f,0x00,
0x00,0x00,
};
/* url + friendly name */
static const unsigned char expected_hlink_data2[] =
{
0x02,0x00,0x00,0x00,0x17,0x00,0x00,0x00,
0x08,0x00,0x00,0x00,0x57,0x00,0x69,0x00,
0x6e,0x00,0x65,0x00,0x20,0x00,0x48,0x00,
0x51,0x00,0x00,0x00,0xe0,0xc9,0xea,0x79,
0xf9,0xba,0xce,0x11,0x8c,0x82,0x00,0xaa,
0x00,0x4b,0xa9,0x0b,0x26,0x00,0x00,0x00,
0x68,0x00,0x74,0x00,0x74,0x00,0x70,0x00,
0x3a,0x00,0x2f,0x00,0x2f,0x00,0x77,0x00,
0x69,0x00,0x6e,0x00,0x65,0x00,0x68,0x00,
0x71,0x00,0x2e,0x00,0x6f,0x00,0x72,0x00,
0x67,0x00,0x2f,0x00,0x00,0x00,
};
/* url + friendly name + location */
static const unsigned char expected_hlink_data3[] =
{
0x02,0x00,0x00,0x00,0x1f,0x00,0x00,0x00,
0x08,0x00,0x00,0x00,0x57,0x00,0x69,0x00,
0x6e,0x00,0x65,0x00,0x20,0x00,0x48,0x00,
0x51,0x00,0x00,0x00,0xe0,0xc9,0xea,0x79,
0xf9,0xba,0xce,0x11,0x8c,0x82,0x00,0xaa,
0x00,0x4b,0xa9,0x0b,0x26,0x00,0x00,0x00,
0x68,0x00,0x74,0x00,0x74,0x00,0x70,0x00,
0x3a,0x00,0x2f,0x00,0x2f,0x00,0x77,0x00,
0x69,0x00,0x6e,0x00,0x65,0x00,0x68,0x00,
0x71,0x00,0x2e,0x00,0x6f,0x00,0x72,0x00,
0x67,0x00,0x2f,0x00,0x00,0x00,0x07,0x00,
0x00,0x00,0x5f,0x00,0x62,0x00,0x6c,0x00,
0x61,0x00,0x6e,0x00,0x6b,0x00,0x00,0x00,
};
/* relative url */
static const unsigned char expected_hlink_data4[] =
{
0x02,0x00,0x00,0x00,0x01,0x00,0x00,0x00,
0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x00,
0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46,
0x00,0x00,0x0b,0x00,0x00,0x00,0x69,0x6e,
0x64,0x65,0x78,0x2e,0x68,0x74,0x6d,0x6c,
0x00,0xff,0xff,0xad,0xde,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,
};
/* url + target frame name */
static const unsigned char expected_hlink_data5[] =
{
0x02,0x00,0x00,0x00,0x83,0x00,0x00,0x00,
0x07,0x00,0x00,0x00,0x74,0x00,0x67,0x00,
0x74,0x00,0x66,0x00,0x72,0x00,0x6d,0x00,
0x00,0x00,0xe0,0xc9,0xea,0x79,0xf9,0xba,
0xce,0x11,0x8c,0x82,0x00,0xaa,0x00,0x4b,
0xa9,0x0b,0x26,0x00,0x00,0x00,0x68,0x00,
0x74,0x00,0x74,0x00,0x70,0x00,0x3a,0x00,
0x2f,0x00,0x2f,0x00,0x77,0x00,0x69,0x00,
0x6e,0x00,0x65,0x00,0x68,0x00,0x71,0x00,
0x2e,0x00,0x6f,0x00,0x72,0x00,0x67,0x00,
0x2f,0x00,0x00,0x00,
};
/* filename */
static const unsigned char expected_hlink_data6[] =
{
0x02,0x00,0x00,0x00,0x03,0x00,0x00,0x00,
0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x00,
0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46,
0x00,0x00,0x04,0x00,0x00,0x00,0x63,0x3a,
0x5c,0x00,0xff,0xff,0xad,0xde,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x0c,0x00,0x00,0x00,0x06,0x00,
0x00,0x00,0x03,0x00,0x63,0x00,0x3a,0x00,
0x5c,0x00,
};
static void test_persist_save_data(const char *testname, IHlink *lnk,
const unsigned char *expected_data,
unsigned int expected_data_size)
{
HRESULT hr;
IStream *stream;
IPersistStream *ps;
HGLOBAL hglobal;
DWORD data_size;
const unsigned char *data;
DWORD i;
BOOL same;
hr = IHlink_QueryInterface(lnk, &IID_IPersistStream, (void **)&ps);
ok(hr == S_OK, "IHlink_QueryInterface failed with error 0x%08x\n", hr);
hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
ok(hr == S_OK, "CreateStreamOnHGlobal failed with error 0x%08x\n", hr);
hr = IPersistStream_Save(ps, stream, TRUE);
ok(hr == S_OK, "IPersistStream_Save failed with error 0x%08x\n", hr);
hr = GetHGlobalFromStream(stream, &hglobal);
ok(hr == S_OK, "GetHGlobalFromStream failed with error 0x%08x\n", hr);
data_size = GlobalSize(hglobal);
data = GlobalLock(hglobal);
/* first check we have the right amount of data */
ok(data_size == expected_data_size,
"%s: Size of saved data differs (expected %d, actual %d)\n",
testname, expected_data_size, data_size);
same = TRUE;
/* then do a byte-by-byte comparison */
for (i = 0; i < min(data_size, expected_data_size); i++)
{
if ((expected_data[i] != data[i]) &&
(((expected_data != expected_hlink_data2) &&
(expected_data != expected_hlink_data3)) ||
((i < 52 || i >= 56) && (i < 80 || i >= 84))))
{
same = FALSE;
break;
}
}
ok(same, "%s: Saved data differs\n", testname);
if (!same)
{
for (i = 0; i < data_size; i++)
{
if (i % 8 == 0) printf(" ");
printf("0x%02x,", data[i]);
if (i % 8 == 7) printf("\n");
}
printf("\n");
}
GlobalUnlock(hglobal);
IStream_Release(stream);
IPersistStream_Release(ps);
}
static void test_persist(void)
{
static const WCHAR url[] = { 'h','t','t','p',':','/','/','w','i','n','e','h','q','.','o','r','g',0 };
static const WCHAR rel_url[] = { 'i','n','d','e','x','.','h','t','m','l',0 };
static const WCHAR filename[] = { 'c',':','\\',0 };
static const WCHAR friendly_name[] = { 'W','i','n','e',' ','H','Q',0 };
static const WCHAR location[] = { '_','b','l','a','n','k',0 };
static const WCHAR target_frame_name[] = { 't','g','t','f','r','m',0 };
HRESULT hr;
IHlink *lnk;
hr = HlinkCreateFromString(url, NULL, NULL, NULL,
0, NULL, &IID_IHlink, (LPVOID*) &lnk);
ok(hr == S_OK, "IHlinCreateFromString failed with error 0x%08x\n", hr);
test_persist_save_data("url only", lnk, expected_hlink_data, sizeof(expected_hlink_data));
IHlink_Release(lnk);
hr = HlinkCreateFromString(url, NULL, friendly_name, NULL,
0, NULL, &IID_IHlink, (LPVOID*) &lnk);
ok(hr == S_OK, "IHlinCreateFromString failed with error 0x%08x\n", hr);
test_persist_save_data("url + friendly name", lnk, expected_hlink_data2, sizeof(expected_hlink_data2));
IHlink_Release(lnk);
hr = HlinkCreateFromString(url, location, friendly_name, NULL,
0, NULL, &IID_IHlink, (LPVOID*) &lnk);
ok(hr == S_OK, "IHlinCreateFromString failed with error 0x%08x\n", hr);
test_persist_save_data("url + friendly_name + location", lnk, expected_hlink_data3, sizeof(expected_hlink_data3));
IHlink_Release(lnk);
hr = HlinkCreateFromString(rel_url, NULL, NULL, NULL,
0, NULL, &IID_IHlink, (LPVOID*) &lnk);
ok(hr == S_OK, "IHlinCreateFromString failed with error 0x%08x\n", hr);
test_persist_save_data("relative url", lnk, expected_hlink_data4, sizeof(expected_hlink_data4));
IHlink_Release(lnk);
hr = HlinkCreateFromString(url, NULL, NULL, NULL,
0, NULL, &IID_IHlink, (LPVOID*) &lnk);
ok(hr == S_OK, "IHlinCreateFromString failed with error 0x%08x\n", hr);
hr = IHlink_SetTargetFrameName(lnk, target_frame_name);
ok(hr == S_OK, "IHlink_SetTargetFrameName failed with error 0x%08x\n", hr);
test_persist_save_data("url + target frame name", lnk, expected_hlink_data5, sizeof(expected_hlink_data5));
IHlink_Release(lnk);
hr = HlinkCreateFromString(filename, NULL, NULL, NULL,
0, NULL, &IID_IHlink, (LPVOID*) &lnk);
ok(hr == S_OK, "IHlinCreateFromString failed with error 0x%08x\n", hr);
test_persist_save_data("filename", lnk, expected_hlink_data6, sizeof(expected_hlink_data6));
IHlink_Release(lnk);
}
START_TEST(hlink)
{
CoInitialize(NULL);
test_HlinkIsShortcut();
test_reference();
test_persist();
CoUninitialize();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment