riched20/tests: Add tests for OLE interface.
[wine/gsoc_dplay.git] / tools / winedump / lnk.c
blobde1e95afe9917ce3ddd49bef7c223cad414809e3
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
23 #include "winedump.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33 #include <fcntl.h>
34 #include <stdarg.h>
36 #include "windef.h"
37 #include "winbase.h"
39 #include "pshpack1.h"
41 typedef enum {
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
71 typedef struct tagDATABLOCKHEADER
73 DWORD cbSize;
74 DWORD dwSignature;
75 } DATABLOCK_HEADER;
77 typedef struct _LINK_HEADER
79 DWORD dwSize; /* 0x00 size of the header - 0x4c */
80 GUID MagicGuid; /* 0x04 is CLSID_ShellLink */
81 DWORD dwFlags; /* 0x14 describes elements following */
82 DWORD dwFileAttr; /* 0x18 attributes of the target file */
83 FILETIME Time1; /* 0x1c */
84 FILETIME Time2; /* 0x24 */
85 FILETIME Time3; /* 0x2c */
86 DWORD dwFileLength; /* 0x34 File length */
87 DWORD nIcon; /* 0x38 icon number */
88 DWORD fStartup; /* 0x3c startup type */
89 DWORD wHotKey; /* 0x40 hotkey */
90 DWORD Unknown5; /* 0x44 */
91 DWORD Unknown6; /* 0x48 */
92 } LINK_HEADER, * PLINK_HEADER;
94 typedef struct tagLINK_SZ_BLOCK
96 DWORD size;
97 DWORD magic;
98 CHAR bufA[MAX_PATH];
99 WCHAR bufW[MAX_PATH];
100 } LINK_SZ_BLOCK;
102 typedef struct _LOCATION_INFO
104 DWORD dwTotalSize;
105 DWORD dwHeaderSize;
106 DWORD dwFlags;
107 DWORD dwVolTableOfs;
108 DWORD dwLocalPathOfs;
109 DWORD dwNetworkVolTableOfs;
110 DWORD dwFinalPathOfs;
111 } LOCATION_INFO;
113 typedef struct _LOCAL_VOLUME_INFO
115 DWORD dwSize;
116 DWORD dwType;
117 DWORD dwVolSerial;
118 DWORD dwVolLabelOfs;
119 } LOCAL_VOLUME_INFO;
121 typedef struct
123 DWORD cbSize;
124 DWORD dwSignature;
125 DWORD idSpecialFolder;
126 DWORD cbOffset;
127 } EXP_SPECIAL_FOLDER;
129 typedef struct lnk_string_tag
131 unsigned short size;
132 union {
133 unsigned short w[1];
134 unsigned char a[1];
135 } str;
136 } lnk_string;
138 #include "poppack.h"
140 static unsigned offset;
142 static const void* fetch_block(void)
144 const unsigned* u;
145 const void* ret;
147 if (!(u = PRD(offset, sizeof(*u)))) return 0;
148 if ((ret = PRD(offset, *u))) offset += *u;
149 return ret;
152 static const lnk_string* fetch_string(int unicode)
154 const unsigned short* s;
155 unsigned short len;
156 const void* ret;
158 if (!(s = PRD(offset, sizeof(*s)))) return 0;
159 len = *s * (unicode ? sizeof(WCHAR) : sizeof(char));
160 if ((ret = PRD(offset, sizeof(*s) + len))) offset += sizeof(*s) + len;
161 return ret;
165 static int dump_pidl(void)
167 const lnk_string *pidl;
168 int i, n = 0, sz = 0;
170 pidl = fetch_string(FALSE);
171 if (!pidl)
172 return -1;
174 printf("PIDL\n");
175 printf("----\n\n");
177 while(sz<pidl->size)
179 const lnk_string *segment = (const lnk_string*) &pidl->str.a[sz];
181 if(!segment->size)
182 break;
183 sz+=segment->size;
184 if(sz>pidl->size)
186 printf("bad pidl\n");
187 break;
189 n++;
190 printf("segment %d (%2d bytes) : ",n,segment->size);
191 for(i=0; i<segment->size; i++)
192 printf("%02x ",segment->str.a[i]);
193 printf("\n");
195 printf("\n");
197 return 0;
200 static int dump_string(const char *what, int unicode)
202 const lnk_string *data;
203 unsigned sz;
205 data = fetch_string(unicode);
206 if (!data)
207 return -1;
208 printf("%s : ", what);
209 sz = data->size;
210 if (unicode)
211 while (sz) printf("%c", data->str.w[data->size - sz--]);
212 else
213 while (sz) printf("%c", data->str.a[data->size - sz--]);
214 printf("\n");
216 return 0;
219 static int dump_location(void)
221 const LOCATION_INFO *loc;
222 const char *p;
224 loc = fetch_block();
225 if (!loc)
226 return -1;
227 p = (const char*)loc;
229 printf("Location\n");
230 printf("--------\n\n");
231 printf("Total size = %d\n", loc->dwTotalSize);
232 printf("Header size = %d\n", loc->dwHeaderSize);
233 printf("Flags = %08x\n", loc->dwFlags);
235 /* dump out information about the volume the link points to */
236 printf("Volume ofs = %08x ", loc->dwVolTableOfs);
237 if (loc->dwVolTableOfs && (loc->dwVolTableOfs<loc->dwTotalSize))
239 const LOCAL_VOLUME_INFO *vol = (const LOCAL_VOLUME_INFO *)&p[loc->dwVolTableOfs];
241 printf("size %d type %d serial %08x label %d ",
242 vol->dwSize, vol->dwType, vol->dwVolSerial, vol->dwVolLabelOfs);
243 if(vol->dwVolLabelOfs)
244 printf("(\"%s\")", &p[loc->dwVolTableOfs + vol->dwVolLabelOfs]);
246 printf("\n");
248 /* dump out the path the link points to */
249 printf("LocalPath ofs = %08x ", loc->dwLocalPathOfs);
250 if( loc->dwLocalPathOfs && (loc->dwLocalPathOfs < loc->dwTotalSize) )
251 printf("(\"%s\")", &p[loc->dwLocalPathOfs]);
252 printf("\n");
254 printf("Net Path ofs = %08x\n", loc->dwNetworkVolTableOfs);
255 printf("Final Path = %08x ", loc->dwFinalPathOfs);
256 if( loc->dwFinalPathOfs && (loc->dwFinalPathOfs < loc->dwTotalSize) )
257 printf("(\"%s\")", &p[loc->dwFinalPathOfs]);
258 printf("\n");
259 printf("\n");
261 return 0;
264 static const unsigned char table_dec85[0x80] = {
265 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
266 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
267 0xff,0x00,0xff,0xff,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0xff,
268 0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0xff,0xff,0xff,0x16,0xff,0x17,
269 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
270 0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0x34,0x35,0x36,
271 0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,0x46,
272 0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0xff,0x53,0x54,0xff,
275 static int base85_to_guid( const char *str, LPGUID guid )
277 DWORD i, val = 0, base = 1, *p;
278 unsigned char ch;
280 p = (DWORD*) guid;
281 for( i=0; i<20; i++ )
283 if( (i%5) == 0 )
285 val = 0;
286 base = 1;
288 ch = str[i];
289 if( ch >= 0x80 )
290 return 0;
291 val += table_dec85[ch] * base;
292 if( table_dec85[ch] == 0xff )
293 return 0;
294 if( (i%5) == 4 )
295 p[i/5] = val;
296 base *= 85;
298 return 1;
301 static int dump_special_folder_block(const DATABLOCK_HEADER* bhdr)
303 const EXP_SPECIAL_FOLDER *sfb = (const EXP_SPECIAL_FOLDER*)bhdr;
304 printf("Special folder block\n");
305 printf("--------------------\n\n");
306 printf("folder = 0x%04x\n", sfb->idSpecialFolder);
307 printf("offset = %d\n", sfb->cbOffset);
308 printf("\n");
309 return 0;
312 static int dump_sz_block(const DATABLOCK_HEADER* bhdr, const char* label)
314 const LINK_SZ_BLOCK *szp = (const LINK_SZ_BLOCK*)bhdr;
315 printf("String block\n");
316 printf("-----------\n\n");
317 printf("magic = %x\n", szp->magic);
318 printf("%s = %s\n", label, szp->bufA);
319 printf("\n");
320 return 0;
323 static int dump_darwin_id(const DATABLOCK_HEADER* bhdr)
325 const LINK_SZ_BLOCK *szp = (const LINK_SZ_BLOCK*)bhdr;
326 char comp_str[40];
327 const char *feat, *comp, *prod_str, *feat_str;
328 GUID guid;
330 printf("Advertise Info\n");
331 printf("--------------\n\n");
332 printf("msi string = %s\n", szp->bufA);
334 if (base85_to_guid(szp->bufA, &guid))
335 prod_str = get_guid_str(&guid);
336 else
337 prod_str = "?";
339 comp = &szp->bufA[20];
340 feat = strchr(comp, '>');
341 if (!feat)
342 feat = strchr(comp, '<');
343 if (feat)
345 memcpy(comp_str, comp, feat - comp);
346 comp_str[feat-comp] = 0;
348 else
350 strcpy(comp_str, "?");
353 if (feat && feat[0] == '>' && base85_to_guid( &feat[1], &guid ))
354 feat_str = get_guid_str( &guid );
355 else
356 feat_str = "";
358 printf(" product: %s\n", prod_str);
359 printf(" component: %s\n", comp_str );
360 printf(" feature: %s\n", feat_str);
361 printf("\n");
363 return 0;
366 static int dump_raw_block(const DATABLOCK_HEADER* bhdr)
368 int data_size;
370 printf("Raw Block\n");
371 printf("---------\n\n");
372 printf("size = %d\n", bhdr->cbSize);
373 printf("magic = %x\n", bhdr->dwSignature);
375 data_size=bhdr->cbSize-sizeof(*bhdr);
376 if (data_size > 0)
378 unsigned int i;
379 const unsigned char *data;
381 printf("data = ");
382 data=(const unsigned char*)bhdr+sizeof(*bhdr);
383 while (data_size > 0)
385 for (i=0; i < 16; i++)
387 if (i < data_size)
388 printf("%02x ", data[i]);
389 else
390 printf(" ");
392 for (i=0; i < data_size && i < 16; i++)
393 printf("%c", (data[i] >= 32 && data[i] < 128 ? data[i] : '.'));
394 printf("\n");
395 data_size-=16;
396 if (data_size <= 0)
397 break;
398 data+=16;
399 printf(" ");
402 printf("\n");
404 return 1;
407 static const GUID CLSID_ShellLink = {0x00021401L, 0, 0, {0xC0,0,0,0,0,0,0,0x46}};
409 enum FileSig get_kind_lnk(void)
411 const LINK_HEADER* hdr;
413 hdr = PRD(0, sizeof(*hdr));
414 if (hdr && hdr->dwSize == sizeof(LINK_HEADER) &&
415 !memcmp(&hdr->MagicGuid, &CLSID_ShellLink, sizeof(GUID)))
416 return SIG_LNK;
417 return SIG_UNKNOWN;
420 void lnk_dump(void)
422 const LINK_HEADER* hdr;
423 const DATABLOCK_HEADER* bhdr;
424 DWORD dwFlags;
426 offset = 0;
427 hdr = fetch_block();
428 if (!hdr)
429 return;
431 printf("Header\n");
432 printf("------\n\n");
433 printf("Size: %04x\n", hdr->dwSize);
434 printf("GUID: %s\n", get_guid_str(&hdr->MagicGuid));
436 printf("FileAttr: %08x\n", hdr->dwFileAttr);
437 printf("FileLength: %08x\n", hdr->dwFileLength);
438 printf("nIcon: %d\n", hdr->nIcon);
439 printf("Startup: %d\n", hdr->fStartup);
440 printf("HotKey: %08x\n", hdr->wHotKey);
441 printf("Unknown5: %08x\n", hdr->Unknown5);
442 printf("Unknown6: %08x\n", hdr->Unknown6);
444 /* dump out all the flags */
445 printf("Flags: %04x ( ", hdr->dwFlags);
446 dwFlags=hdr->dwFlags;
447 #define FLAG(x) do \
449 if (dwFlags & SLDF_##x) \
451 printf("%s ", #x); \
452 dwFlags&=~SLDF_##x; \
454 } while (0)
455 FLAG(HAS_ID_LIST);
456 FLAG(HAS_LINK_INFO);
457 FLAG(HAS_NAME);
458 FLAG(HAS_RELPATH);
459 FLAG(HAS_WORKINGDIR);
460 FLAG(HAS_ARGS);
461 FLAG(HAS_ICONLOCATION);
462 FLAG(UNICODE);
463 FLAG(FORCE_NO_LINKINFO);
464 FLAG(HAS_EXP_SZ);
465 FLAG(RUN_IN_SEPARATE);
466 FLAG(HAS_LOGO3ID);
467 FLAG(HAS_DARWINID);
468 FLAG(RUNAS_USER);
469 FLAG(HAS_EXP_ICON_SZ);
470 FLAG(NO_PIDL_ALIAS);
471 FLAG(FORCE_UNCNAME);
472 FLAG(RUN_WITH_SHIMLAYER);
473 FLAG(FORCE_NO_LINKTRACK);
474 FLAG(ENABLE_TARGET_METADATA);
475 FLAG(DISABLE_KNOWNFOLDER_RELATIVE_TRACKING);
476 FLAG(RESERVED);
477 #undef FLAG
478 if (dwFlags)
479 printf("+%04x", dwFlags);
480 printf(")\n");
482 printf("Length: %04x\n", hdr->dwFileLength);
483 printf("\n");
485 if (hdr->dwFlags & SLDF_HAS_ID_LIST)
486 dump_pidl();
487 if (hdr->dwFlags & SLDF_HAS_LINK_INFO)
488 dump_location();
489 if (hdr->dwFlags & SLDF_HAS_NAME)
490 dump_string("Description", hdr->dwFlags & SLDF_UNICODE);
491 if (hdr->dwFlags & SLDF_HAS_RELPATH)
492 dump_string("Relative path", hdr->dwFlags & SLDF_UNICODE);
493 if (hdr->dwFlags & SLDF_HAS_WORKINGDIR)
494 dump_string("Working directory", hdr->dwFlags & SLDF_UNICODE);
495 if (hdr->dwFlags & SLDF_HAS_ARGS)
496 dump_string("Arguments", hdr->dwFlags & SLDF_UNICODE);
497 if (hdr->dwFlags & SLDF_HAS_ICONLOCATION)
498 dump_string("Icon path", hdr->dwFlags & SLDF_UNICODE);
500 bhdr=fetch_block();
501 while (bhdr)
503 if (!bhdr->cbSize)
504 break;
505 switch (bhdr->dwSignature)
507 case EXP_SZ_LINK_SIG:
508 dump_sz_block(bhdr, "exp.link");
509 break;
510 case EXP_SPECIAL_FOLDER_SIG:
511 dump_special_folder_block(bhdr);
512 break;
513 case EXP_SZ_ICON_SIG:
514 dump_sz_block(bhdr, "icon");
515 break;
516 case EXP_DARWIN_ID_SIG:
517 dump_darwin_id(bhdr);
518 break;
519 default:
520 dump_raw_block(bhdr);
522 bhdr=fetch_block();