2 * Dump a shortcut (lnk) file
4 * Copyright 2005 Mike McCormack
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.
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.
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
22 #include "wine/port.h"
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
42 SLDF_HAS_ID_LIST
= 0x00000001,
43 SLDF_HAS_LINK_INFO
= 0x00000002,
44 SLDF_HAS_NAME
= 0x00000004,
45 SLDF_HAS_RELPATH
= 0x00000008,
46 SLDF_HAS_WORKINGDIR
= 0x00000010,
47 SLDF_HAS_ARGS
= 0x00000020,
48 SLDF_HAS_ICONLOCATION
= 0x00000040,
49 SLDF_UNICODE
= 0x00000080,
50 SLDF_FORCE_NO_LINKINFO
= 0x00000100,
51 SLDF_HAS_EXP_SZ
= 0x00000200,
52 SLDF_RUN_IN_SEPARATE
= 0x00000400,
53 SLDF_HAS_LOGO3ID
= 0x00000800,
54 SLDF_HAS_DARWINID
= 0x00001000,
55 SLDF_RUNAS_USER
= 0x00002000,
56 SLDF_HAS_EXP_ICON_SZ
= 0x00004000,
57 SLDF_NO_PIDL_ALIAS
= 0x00008000,
58 SLDF_FORCE_UNCNAME
= 0x00010000,
59 SLDF_RUN_WITH_SHIMLAYER
= 0x00020000,
60 SLDF_FORCE_NO_LINKTRACK
= 0x00040000,
61 SLDF_ENABLE_TARGET_METADATA
= 0x00080000,
62 SLDF_DISABLE_KNOWNFOLDER_RELATIVE_TRACKING
= 0x00200000,
63 SLDF_RESERVED
= 0x80000000,
64 } SHELL_LINK_DATA_FLAGS
;
66 #define EXP_SZ_LINK_SIG 0xa0000001
67 #define EXP_SPECIAL_FOLDER_SIG 0xa0000005
68 #define EXP_DARWIN_ID_SIG 0xa0000006
69 #define EXP_SZ_ICON_SIG 0xa0000007
70 #define EXP_PROPERTYSTORAGE_SIG 0xa0000009
72 typedef struct tagDATABLOCKHEADER
78 typedef struct _LINK_HEADER
80 DWORD dwSize
; /* 0x00 size of the header - 0x4c */
81 GUID MagicGuid
; /* 0x04 is CLSID_ShellLink */
82 DWORD dwFlags
; /* 0x14 describes elements following */
83 DWORD dwFileAttr
; /* 0x18 attributes of the target file */
84 FILETIME Time1
; /* 0x1c */
85 FILETIME Time2
; /* 0x24 */
86 FILETIME Time3
; /* 0x2c */
87 DWORD dwFileLength
; /* 0x34 File length */
88 DWORD nIcon
; /* 0x38 icon number */
89 DWORD fStartup
; /* 0x3c startup type */
90 DWORD wHotKey
; /* 0x40 hotkey */
91 DWORD Unknown5
; /* 0x44 */
92 DWORD Unknown6
; /* 0x48 */
93 } LINK_HEADER
, * PLINK_HEADER
;
95 typedef struct tagLINK_SZ_BLOCK
100 WCHAR bufW
[MAX_PATH
];
103 typedef struct tagLINK_PROPERTYSTORAGE_GUID
108 } LINK_PROPERTYSTORAGE_GUID
;
110 typedef struct tagLINK_PROPERTYSTORAGE_VALUE
117 } LINK_PROPERTYSTORAGE_VALUE
;
119 typedef struct _LOCATION_INFO
125 DWORD dwLocalPathOfs
;
126 DWORD dwNetworkVolTableOfs
;
127 DWORD dwFinalPathOfs
;
130 typedef struct _LOCAL_VOLUME_INFO
138 typedef struct _NETWORK_VOLUME_INFO
142 DWORD dwShareNameOfs
;
145 } NETWORK_VOLUME_INFO
;
151 DWORD idSpecialFolder
;
153 } EXP_SPECIAL_FOLDER
;
155 typedef struct lnk_string_tag
166 static unsigned offset
;
168 static const void* fetch_block(void)
173 if (!(u
= PRD(offset
, sizeof(*u
)))) return 0;
174 if ((ret
= PRD(offset
, *u
))) offset
+= *u
;
178 static const lnk_string
* fetch_string(int unicode
)
180 const unsigned short* s
;
184 if (!(s
= PRD(offset
, sizeof(*s
)))) return 0;
185 len
= *s
* (unicode
? sizeof(WCHAR
) : sizeof(char));
186 if ((ret
= PRD(offset
, sizeof(*s
) + len
))) offset
+= sizeof(*s
) + len
;
191 static void dump_pidl(void)
193 const lnk_string
*pidl
;
194 int i
, n
= 0, sz
= 0;
196 pidl
= fetch_string(FALSE
);
205 const lnk_string
*segment
= (const lnk_string
*) &pidl
->str
.a
[sz
];
212 printf("bad pidl\n");
216 printf("segment %d (%2d bytes) : ",n
,segment
->size
);
217 for(i
=0; i
<segment
->size
; i
++)
218 printf("%02x ",segment
->str
.a
[i
]);
224 static void dump_string(const char *what
, int unicode
)
226 const lnk_string
*data
;
229 data
= fetch_string(unicode
);
232 printf("%s : ", what
);
235 while (sz
) printf("%c", data
->str
.w
[data
->size
- sz
--]);
237 while (sz
) printf("%c", data
->str
.a
[data
->size
- sz
--]);
241 static void dump_location(void)
243 const LOCATION_INFO
*loc
;
249 p
= (const char*)loc
;
251 printf("Location\n");
252 printf("--------\n\n");
253 printf("Total size = %d\n", loc
->dwTotalSize
);
254 printf("Header size = %d\n", loc
->dwHeaderSize
);
255 printf("Flags = %08x\n", loc
->dwFlags
);
257 /* dump information about the local volume the link points to */
258 printf("Local volume ofs = %08x ", loc
->dwVolTableOfs
);
259 if (loc
->dwVolTableOfs
&&
260 loc
->dwVolTableOfs
+ sizeof(LOCAL_VOLUME_INFO
) < loc
->dwTotalSize
)
262 const LOCAL_VOLUME_INFO
*vol
= (const LOCAL_VOLUME_INFO
*)&p
[loc
->dwVolTableOfs
];
264 printf("size %d type %d serial %08x label %d ",
265 vol
->dwSize
, vol
->dwType
, vol
->dwVolSerial
, vol
->dwVolLabelOfs
);
266 if(vol
->dwVolLabelOfs
)
267 printf("(\"%s\")", &p
[loc
->dwVolTableOfs
+ vol
->dwVolLabelOfs
]);
271 /* dump information about the network volume the link points to */
272 printf("Network volume ofs = %08x ", loc
->dwNetworkVolTableOfs
);
273 if (loc
->dwNetworkVolTableOfs
&&
274 loc
->dwNetworkVolTableOfs
+ sizeof(NETWORK_VOLUME_INFO
) < loc
->dwTotalSize
)
276 const NETWORK_VOLUME_INFO
*vol
= (const NETWORK_VOLUME_INFO
*)&p
[loc
->dwNetworkVolTableOfs
];
278 printf("size %d name %d ", vol
->dwSize
, vol
->dwShareNameOfs
);
279 if(vol
->dwShareNameOfs
)
280 printf("(\"%s\")", &p
[loc
->dwNetworkVolTableOfs
+ vol
->dwShareNameOfs
]);
284 /* dump out the path the link points to */
285 printf("LocalPath ofs = %08x ", loc
->dwLocalPathOfs
);
286 if( loc
->dwLocalPathOfs
&& (loc
->dwLocalPathOfs
< loc
->dwTotalSize
) )
287 printf("(\"%s\")", &p
[loc
->dwLocalPathOfs
]);
290 printf("Net Path ofs = %08x\n", loc
->dwNetworkVolTableOfs
);
291 printf("Final Path = %08x ", loc
->dwFinalPathOfs
);
292 if( loc
->dwFinalPathOfs
&& (loc
->dwFinalPathOfs
< loc
->dwTotalSize
) )
293 printf("(\"%s\")", &p
[loc
->dwFinalPathOfs
]);
298 static const unsigned char table_dec85
[0x80] = {
299 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
300 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
301 0xff,0x00,0xff,0xff,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0xff,
302 0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0xff,0xff,0xff,0x16,0xff,0x17,
303 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
304 0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0x34,0x35,0x36,
305 0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,0x46,
306 0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0xff,0x53,0x54,0xff,
309 static BOOL
base85_to_guid( const char *str
, LPGUID guid
)
311 DWORD i
, val
= 0, base
= 1, *p
;
315 for( i
=0; i
<20; i
++ )
325 val
+= table_dec85
[ch
] * base
;
326 if( table_dec85
[ch
] == 0xff )
335 static void dump_special_folder_block(const DATABLOCK_HEADER
* bhdr
)
337 const EXP_SPECIAL_FOLDER
*sfb
= (const EXP_SPECIAL_FOLDER
*)bhdr
;
338 printf("Special folder block\n");
339 printf("--------------------\n\n");
340 printf("folder = 0x%04x\n", sfb
->idSpecialFolder
);
341 printf("offset = %d\n", sfb
->cbOffset
);
345 static void dump_sz_block(const DATABLOCK_HEADER
* bhdr
, const char* label
)
347 const LINK_SZ_BLOCK
*szp
= (const LINK_SZ_BLOCK
*)bhdr
;
348 printf("String block\n");
349 printf("-----------\n\n");
350 printf("magic = %x\n", szp
->magic
);
351 printf("%s = %s\n", label
, szp
->bufA
);
355 static void dump_darwin_id(const DATABLOCK_HEADER
* bhdr
)
357 const LINK_SZ_BLOCK
*szp
= (const LINK_SZ_BLOCK
*)bhdr
;
359 const char *feat
, *comp
, *prod_str
, *feat_str
;
362 printf("Advertise Info\n");
363 printf("--------------\n\n");
364 printf("msi string = %s\n", szp
->bufA
);
366 if (base85_to_guid(szp
->bufA
, &guid
))
367 prod_str
= get_guid_str(&guid
);
371 comp
= &szp
->bufA
[20];
372 feat
= strchr(comp
, '>');
374 feat
= strchr(comp
, '<');
377 memcpy(comp_str
, comp
, feat
- comp
);
378 comp_str
[feat
-comp
] = 0;
382 strcpy(comp_str
, "?");
385 if (feat
&& feat
[0] == '>' && base85_to_guid( &feat
[1], &guid
))
386 feat_str
= get_guid_str( &guid
);
390 printf(" product: %s\n", prod_str
);
391 printf(" component: %s\n", comp_str
);
392 printf(" feature: %s\n", feat_str
);
396 static void dump_property_storage_value(const LINK_PROPERTYSTORAGE_VALUE
*lnk_value_hdr
,
399 BOOL got_terminator
= FALSE
;
401 const unsigned char *value
;
403 while (data_size
>= sizeof(DWORD
))
405 if (!lnk_value_hdr
->size
)
407 got_terminator
= TRUE
;
411 if (lnk_value_hdr
->size
> data_size
|| lnk_value_hdr
->size
< sizeof(*lnk_value_hdr
))
413 printf(" size: %d (invalid)\n", lnk_value_hdr
->size
);
417 printf(" pid: %d\n", lnk_value_hdr
->pid
);
418 printf(" unknown8: %d\n", lnk_value_hdr
->unknown8
);
419 printf(" vartype: %d\n", lnk_value_hdr
->vt
);
420 printf(" unknown25: %d\n", lnk_value_hdr
->unknown25
);
422 value_size
= lnk_value_hdr
->size
- sizeof(*lnk_value_hdr
);
423 value
= (const unsigned char*)(lnk_value_hdr
+1);
425 printf(" value (%2d bytes) : ",value_size
);
426 for(i
=0; i
<value_size
; i
++)
427 printf("%02x ",value
[i
]);
430 data_size
-= lnk_value_hdr
->size
;
431 lnk_value_hdr
= (void*)((char*)lnk_value_hdr
+ lnk_value_hdr
->size
);
435 printf(" missing terminator!\n");
438 static void dump_property_storage(const DATABLOCK_HEADER
* bhdr
)
441 const LINK_PROPERTYSTORAGE_GUID
*lnk_guid_hdr
;
442 BOOL got_terminator
= FALSE
;
444 printf("Property Storage\n");
445 printf("--------------\n\n");
447 data_size
=bhdr
->cbSize
-sizeof(*bhdr
);
449 lnk_guid_hdr
=(void*)((const char*)bhdr
+sizeof(*bhdr
));
451 while (data_size
>= sizeof(DWORD
))
453 if (!lnk_guid_hdr
->size
)
455 got_terminator
= TRUE
;
459 if (lnk_guid_hdr
->size
> data_size
|| lnk_guid_hdr
->size
< sizeof(*lnk_guid_hdr
))
461 printf("size: %d (invalid)\n", lnk_guid_hdr
->size
);
465 if (lnk_guid_hdr
->magic
!= 0x53505331)
466 printf("magic: %x\n", lnk_guid_hdr
->magic
);
468 printf("fmtid: %s\n", get_guid_str(&lnk_guid_hdr
->fmtid
));
470 dump_property_storage_value((void*)(lnk_guid_hdr
+ 1), lnk_guid_hdr
->size
- sizeof(*lnk_guid_hdr
));
472 data_size
-= lnk_guid_hdr
->size
;
474 lnk_guid_hdr
= (void*)((char*)lnk_guid_hdr
+ lnk_guid_hdr
->size
);
478 printf("missing terminator!\n");
483 static void dump_raw_block(const DATABLOCK_HEADER
* bhdr
)
487 printf("Raw Block\n");
488 printf("---------\n\n");
489 printf("size = %d\n", bhdr
->cbSize
);
490 printf("magic = %x\n", bhdr
->dwSignature
);
492 data_size
=bhdr
->cbSize
-sizeof(*bhdr
);
496 const unsigned char *data
;
499 data
=(const unsigned char*)bhdr
+sizeof(*bhdr
);
500 while (data_size
> 0)
502 for (i
=0; i
< 16; i
++)
505 printf("%02x ", data
[i
]);
509 for (i
=0; i
< data_size
&& i
< 16; i
++)
510 printf("%c", (data
[i
] >= 32 && data
[i
] < 128 ? data
[i
] : '.'));
522 static const GUID CLSID_ShellLink
= {0x00021401L
, 0, 0, {0xC0,0,0,0,0,0,0,0x46}};
524 enum FileSig
get_kind_lnk(void)
526 const LINK_HEADER
* hdr
;
528 hdr
= PRD(0, sizeof(*hdr
));
529 if (hdr
&& hdr
->dwSize
== sizeof(LINK_HEADER
) &&
530 !memcmp(&hdr
->MagicGuid
, &CLSID_ShellLink
, sizeof(GUID
)))
537 const LINK_HEADER
* hdr
;
538 const DATABLOCK_HEADER
* bhdr
;
547 printf("------\n\n");
548 printf("Size: %04x\n", hdr
->dwSize
);
549 printf("GUID: %s\n", get_guid_str(&hdr
->MagicGuid
));
551 printf("FileAttr: %08x\n", hdr
->dwFileAttr
);
552 printf("FileLength: %08x\n", hdr
->dwFileLength
);
553 printf("nIcon: %d\n", hdr
->nIcon
);
554 printf("Startup: %d\n", hdr
->fStartup
);
555 printf("HotKey: %08x\n", hdr
->wHotKey
);
556 printf("Unknown5: %08x\n", hdr
->Unknown5
);
557 printf("Unknown6: %08x\n", hdr
->Unknown6
);
559 /* dump out all the flags */
560 printf("Flags: %04x ( ", hdr
->dwFlags
);
561 dwFlags
=hdr
->dwFlags
;
564 if (dwFlags & SLDF_##x) \
567 dwFlags&=~SLDF_##x; \
574 FLAG(HAS_WORKINGDIR
);
576 FLAG(HAS_ICONLOCATION
);
578 FLAG(FORCE_NO_LINKINFO
);
580 FLAG(RUN_IN_SEPARATE
);
584 FLAG(HAS_EXP_ICON_SZ
);
587 FLAG(RUN_WITH_SHIMLAYER
);
588 FLAG(FORCE_NO_LINKTRACK
);
589 FLAG(ENABLE_TARGET_METADATA
);
590 FLAG(DISABLE_KNOWNFOLDER_RELATIVE_TRACKING
);
594 printf("+%04x", dwFlags
);
597 printf("Length: %04x\n", hdr
->dwFileLength
);
600 if (hdr
->dwFlags
& SLDF_HAS_ID_LIST
)
602 if (hdr
->dwFlags
& SLDF_HAS_LINK_INFO
)
604 if (hdr
->dwFlags
& SLDF_HAS_NAME
)
605 dump_string("Description", hdr
->dwFlags
& SLDF_UNICODE
);
606 if (hdr
->dwFlags
& SLDF_HAS_RELPATH
)
607 dump_string("Relative path", hdr
->dwFlags
& SLDF_UNICODE
);
608 if (hdr
->dwFlags
& SLDF_HAS_WORKINGDIR
)
609 dump_string("Working directory", hdr
->dwFlags
& SLDF_UNICODE
);
610 if (hdr
->dwFlags
& SLDF_HAS_ARGS
)
611 dump_string("Arguments", hdr
->dwFlags
& SLDF_UNICODE
);
612 if (hdr
->dwFlags
& SLDF_HAS_ICONLOCATION
)
613 dump_string("Icon path", hdr
->dwFlags
& SLDF_UNICODE
);
620 switch (bhdr
->dwSignature
)
622 case EXP_SZ_LINK_SIG
:
623 dump_sz_block(bhdr
, "exp.link");
625 case EXP_SPECIAL_FOLDER_SIG
:
626 dump_special_folder_block(bhdr
);
628 case EXP_SZ_ICON_SIG
:
629 dump_sz_block(bhdr
, "icon");
631 case EXP_DARWIN_ID_SIG
:
632 dump_darwin_id(bhdr
);
634 case EXP_PROPERTYSTORAGE_SIG
:
635 dump_property_storage(bhdr
);
638 dump_raw_block(bhdr
);