blib/lib/Parse/Lnk.pm

filter: Statementstmt 91.4% 299 / 327 Branchbran 77.2% 68 / 88 Conditioncond 66.6% 10 / 15 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 100% 19 / 19 Podpod 100% 5 / 5 totaltotal 87.5% 406 / 464 CC 52 SCAR 30.3
CC
52
Cov
88%
_parse
39.0
new
3.6
_reset
3.0
CRAP 20.7
↑ Summary
1 package Parse::Lnk;
2
3 # Based on the contents of the document:
4 # http://www.i2s-lab.com/Papers/The_Windows_Shortcut_File_Format.pdf
5
6 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 2 our $VERSION = '0.10';
12
13 =pod
14
15 =encoding latin1
16
17 =head1 NAME
18
19 Parse::Lnk - A cross-platform, depencency free, Windows shortcut (.lnk) meta data parser.
20
21 =head1 VERSION
22
23 Version 0.10
24
25 =cut
26
27 2 require Exporter;
28 2 our @ISA = qw(Exporter);
29 2 our @EXPORT_OK = qw(parse_lnk resolve_lnk);
30
31 2 our $map = { # Tag names made up based on the docs
32 flag => {
33 0 => {
34 0 => 'NO SHELLIDLIST',
35 1 => 'HAS SHELLIDLIST',
36 },
37 1 => {
38 0 => 'NOT POINT TO FILE/DIR',
39 1 => 'POINTS TO FILE/DIR',
40 },
41 2 => {
42 0 => 'NO DESCRIPTION',
43 1 => 'HAS DESCRIPTION',
44 },
45 3 => {
46 0 => 'NO RELATIVE PATH STRING',
47 1 => 'HAS RELATIVE PATH STRING',
48 },
49 4 => {
50 0 => 'NO WORKING DIRECTORY',
51 1 => 'HAS WORKING DIRECTORY',
52 },
53 5 => {
54 0 => 'NO CMD LINE ARGS',
55 1 => 'HAS CMD LINE ARGS',
56 },
57 6 => {
58 0 => 'NO CUSTOM ICON',
59 1 => 'HAS CUSTOM ICON',
60 },
61 },
62 file => {
63 0 => 'READ ONLY TARGET',
64 1 => 'HIDDEN TARGET',
65 2 => 'SYSTEM FILE TARGET',
66 3 => 'VOLUME LABEL TARGET (not possible)',
67 4 => 'DIRECTORY TARGET',
68 5 => 'ARCHIVE',
69 6 => 'NTFS EFS',
70 7 => 'NORMAL TARGET',
71 8 => 'TEMP. TARGET',
72 9 => 'SPARSE TARGET',
73 10 => 'REPARSE POINT DATA TARGET',
74 11 => 'COMPRESSED TARGET',
75 12 => 'TARGET OFFLINE',
76 },
77 show_wnd => {
78 0 => 'SW_HIDE',
79 1 => 'SW_NORMAL',
80 2 => 'SW_SHOWMINIMIZED',
81 3 => 'SW_SHOWMAXIMIZED',
82 4 => 'SW_SHOWNOACTIVE',
83 5 => 'SW_SHOW',
84 6 => 'SW_MINIMIZE',
85 7 => 'SW_SHOWMINNOACTIVE',
86 8 => 'SW_SHOWNA',
87 9 => 'SW_RESTORE',
88 10 => 'SW_SHOWDEFAULT',
89 },
90 vol_type => {
91 0 => 'Unknown',
92 1 => 'No root directory',
93 2 => 'Removable (Floppy, Zip, USB, etc.)',
94 3 => 'Fixed (Hard Disk)',
95 4 => 'Remote (Network Drive)',
96 5 => 'CD-ROM',
97 6 => 'RAM Drive',
98 },
99 };
100
101 10 ▶sub resolve_lnk {
Subroutine resolve_lnk: called CC=1 SCAR=0.0
Pod documented
102 10 my $filename = shift;
103 10 my $l = __PACKAGE__->new (
104 filename => $filename,
105 resolve => 1,
106 );
107 10 $l->_parse;
108 10 $l->{base_path};
109 }
110
111 10 ▶sub parse_lnk {
Subroutine parse_lnk: called CC=2 SCAR=6.9
Pod documented
112 10 my $filename = shift;
113 10 my $l = __PACKAGE__->new (
114 filename => $filename,
115 );
116 10 $l->_parse;
117 10 ▶ return if $l->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $l->{"error"} 5 5 10
118 5 $l;
119 }
120
121 40 ▶sub new {
Subroutine new: called CC=3 SCAR=12.7
Pod documented
122 40 my $class = shift;
123 40 ▶ if (@_ and @_ % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and @_ % 2) 0 40 40
Condition33%1/3
@_ and @_ % 2
!ll&&!rl&&r
0400
MC/DC0%0/2
@_ and @_ % 2
@_ @_ % 2
Truth table33%1/3
A and B
A= @_B= @_ % 2
ABresult
0X0
100
111
124 0 croak "This method expects (name => value) arguments. Odd number of arguments received";
125 }
126 40 my $self = {
127 @_,
128 };
129 40 bless $self, $class;
130 }
131
132 10 ▶sub from {
Subroutine from: called CC=3 SCAR=11.0
Pod documented
133 10 my $self = shift;
134 10 my $filename = shift;
135 10 ▶ $self = $self->new (
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 10 0 10
136 filename => $filename,
137 ) unless ref $self;
138 10 $self->_parse;
139 10 ▶ return if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 5 5 10
140 5 $self;
141 }
142
143 10 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.0
Pod documented
144 10 my $self = shift;
145 10 my $filename = shift;
146 10 ▶ $self = $self->new (
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 10 10
147 filename => $filename,
148 ) unless ref $self;
149 10 $self->_parse;
150 10 ▶ return if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 5 5 10
151 5 $self;
152 }
153
154 40 ▶sub _reset {
Subroutine _reset: called CC=3 SCAR=11.1
155 40 my $self = shift;
156 40 ▶ return unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 40 40
157 40 for my $k (keys %$self) {
158 50 delete $self->{$k};
159 }
160 40 $self;
161 }
162
163 40 ▶sub _parse {
Subroutine _parse: called CC=35 SCAR=36.6
164 40 my $self = shift;
165 40 my $filename = $self->{filename};
166 40 my $resolve = $self->{resolve};
167 40 $self->_reset;
168 40 ▶ if (not defined $filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename) 0 40 40
169 0 $self->{error} = 'A filename is required';
170 0 return;
171 }
172 40 ▶ if (not -f $filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $filename) 8 32 40
173 8 $self->{error} = "Not a file";
174 8 return;
175 }
176 32 ▶ if (open my $in, '<', $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $in, '<', $filename) { } 32 0 32
177 32 binmode $in;
178 32 $self->{_fh} = $in;
179 } else {
180 # We set error before croak, in case this call is being eval'ed
181 0 $self->{error} = "Can't open file '$filename' for reading";
182 0 croak $self->{error};
183 }
184
185 32 my $header = $self->_read_unpack(0, 1);
186 32 ▶ if ($header ne '4c') {
Branch: 1 branch
BranchTrueFalseTotal
if ($header ne "4c") 12 20 32
187 12 $self->{error} = 'Invalid Lnk file header';
188 12 close $self->{_fh};
189 12 delete $self->{_fh};
190 12 return;
191 }
192
193 20 $self->{guid} = $self->_read_unpack(4, 16);
194
195 20 my $flags = $self->_read_unpack_bin(20, 1);
196 20 my $flag_cnt = 0;
197 20 my @flag_bits = (0, 0, 0, 0, 0, 0, 0, 0);
198 20 ▶ while ($flag_cnt < 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag_cnt < 7) 140 20 160
199 140 my $flag_bit = substr $flags, $flag_cnt, 1;
200 140 push @{$self->{flags}}, $map->{flag}->{$flag_cnt}->{$flag_bit};
201 140 ▶ if ($flag_bit eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag_bit eq 1) 40 100 140
202 40 ▶ if ($flag_cnt >= 0 and $flag_cnt <= 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag_cnt >= 0 and $flag_cnt <= 6) 40 0 40
Condition33%1/3
$flag_cnt >= 0 and $flag_cnt <= 6
!ll&&!rl&&r
0040
MC/DC0%0/2
$flag_cnt >= 0 and $flag_cnt <= 6
$flag_cnt >= 0 $flag_cnt <= 6
Truth table33%1/3
A and B
A= $flag_cnt >= 0B= $flag_cnt <= 6
ABresult
0X0
100
111
203 40 $flag_bits[$flag_cnt] = 1;
204 }
205 }
206 140 $flag_cnt++;
207 }
208
209 # File Attributes 4bytes@18h = 24d
210 # Only a non-zero if "Flag bit 1" above is set to 1
211 #
212 20 ▶ if ($flag_bits[1] == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag_bits[1] == 1) 20 0 20
213 20 my $file_attrib = $self->_read_unpack_bin(24, 2);
214 20 my $file_att_cnt = 0;
215 20 ▶ while ($file_att_cnt < 13) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_att_cnt < 13) 260 20 280
216 260 my $file_bit = substr $file_attrib, $file_att_cnt, 1;
217 260 ▶ push @{$self->{attributes}}, $map->{file}->{$file_att_cnt} if $file_bit;
Branch: 1 branch
BranchTrueFalseTotal
if $file_bit 28 232 260
218 260 $file_att_cnt++;
219 }
220 }
221
222 # Create time 8bytes @ 1ch = 28
223 20 my $ctime = $self->_read_unpack(28, 8);
224 20 $ctime = Parse::Windows::Shortcut::Bigint::bighex($self->_reverse_hex($ctime));
225 20 $ctime = $self->_MStime_to_unix($ctime);
226 20 $self->{create_time} = $ctime;
227
228 # Access time 8 bytes@ 0x24 = 36D
229 20 my $atime = $self->_read_unpack(36, 8);
230 20 $atime = Parse::Windows::Shortcut::Bigint::bighex($self->_reverse_hex($atime));
231 20 $atime = $self->_MStime_to_unix($atime);
232 20 $self->{last_accessed_time} = $atime;
233
234 # Mod Time8b @ 0x2C = 44D
235 20 my $mtime = $self->_read_unpack(44, 8);
236 20 $mtime = Parse::Windows::Shortcut::Bigint::bighex($self->_reverse_hex($mtime));
237 20 $mtime = $self->_MStime_to_unix($mtime);
238 20 $self->{modified_time} = $mtime;
239
240 # Target File length starts @ 34h = 52d
241 20 my $f_len = $self->_read_unpack(52, 4);
242 20 $f_len = hex $self->_reverse_hex($f_len);
243 20 $self->{target_length} = $f_len;
244
245 # Icon File info starts @ 38h = 56d
246 20 my $ico_num = $self->_read_unpack(56, 4);
247 20 $ico_num = hex $ico_num;
248 20 $self->{icon_index} = $ico_num;
249
250 # ShowWnd val to pass to target
251 # Starts @3Ch = 60d
252 20 my $show_wnd = $self->_read_unpack(60, 1);
253 20 $show_wnd = hex $show_wnd;
254 20 $self->{show_wnd} = $show_wnd;
255 20 $self->{show_wnd_flag} = $map->{show_wnd}->{$show_wnd};
256
257 # Hot key
258 # Starts @40h = 64d
259 20 my $hot_key = $self->_read_unpack(64, 4);
260 20 $hot_key = hex $hot_key;
261 20 $self->{hot_key} = $hot_key;
262
263 # ItemID List
264 # Read size of item ID list
265 20 my $i_len = $self->_read_unpack(76, 2);
266 20 $i_len = hex $self->_reverse_hex($i_len);
267 # skip to end of list
268 20 my $end_of_list = (78 + $i_len);
269
270 # FileInfo structure
271 #
272 20 my $struc_start = $end_of_list;
273 20 my $first_off_off = ($struc_start + 4);
274 20 my $vol_flags_off = ($struc_start + 8);
275 20 my $local_vol_off = ($struc_start + 12);
276 20 my $base_path_off = ($struc_start + 16);
277 20 my $net_vol_off = ($struc_start + 20);
278 20 my $rem_path_off = ($struc_start + 24);
279
280 # Structure length
281 20 my $struc_len = $self->_read_unpack($struc_start, 4);
282 20 $struc_len = hex $self->_reverse_hex($struc_len);
283 20 my $struc_end = $struc_start + $struc_len;
284
285 # First offset after struct - Should be 1C under normal circumstances
286 20 my $first_off = $self->_read_unpack($first_off_off, 1);
287
288 # File location flags
289 20 my $vol_flags = $self->_read_unpack_bin($vol_flags_off, 1);
290 20 $vol_flags = substr $vol_flags, 0, 2;
291 20 my @vol_bits = (0, 0);
292 20 ▶ if ($vol_flags =~ /10/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vol_flags =~ /10/) 8 12 20
293 8 $self->{target_type} = 'local';
294 8 $vol_bits[0] = 1;
295 8 $vol_bits[1] = 0;
296 }
297 # Haven't found this case yet...
298 20 ▶ if ($vol_flags =~ /01/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vol_flags =~ /01/) 4 16 20
299 4 $self->{target_type} = 'network';
300 4 $vol_bits[0] = 0;
301 4 $vol_bits[1] = 1;
302 }
303 # But this one I did:
304 20 ▶ if ($vol_flags =~ /11/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vol_flags =~ /11/) 8 12 20
305 8 $self->{target_type} = 'network';
306 8 $vol_bits[0] = 1;
307 8 $vol_bits[1] = 1;
308 }
309
310 # Local volume table
311 # Random garbage if bit0 is clear in volume flags
312 20 ▶ if ($vol_bits[0] == 1 and $vol_bits[1] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vol_bits[0] == 1 and $vol_bits[1] == 0) 8 12 20
Condition100%3/3
$vol_bits[0] == 1 and $vol_bits[1] == 0
!ll&&!rl&&r
488
MC/DC100%2/2
$vol_bits[0] == 1 and $vol_bits[1] == 0
$vol_bits[0] == 1 $vol_bits[1] == 0
Truth table100%3/3
A and B
A= $vol_bits[0] == 1B= $vol_bits[1] == 0
ABresult
0X0
100
111
313 # This is the offset of the local volume table within the
314 #File Info Location Structure
315 8 my $loc_vol_tab_off = $self->_read_unpack($local_vol_off, 4);
316 8 $loc_vol_tab_off = hex $self->_reverse_hex($loc_vol_tab_off);
317
318 # This is the asolute start location of the local volume table
319 8 my $loc_vol_tab_start = $loc_vol_tab_off + $struc_start;
320
321 # This is the length of the local volume table
322 8 my $local_vol_len = $self->_read_unpack(($loc_vol_tab_off + $struc_start), 4);
323 8 $local_vol_len = hex $self->_reverse_hex($local_vol_len);
324
325 # We now have enough info to
326 # Calculate the end of the local volume table.
327 8 my $local_vol_tab_end = $loc_vol_tab_start + $local_vol_len;
328
329 # This is the volume type
330 8 my $curr_tab_offset = $loc_vol_tab_off + $struc_start + 4;
331 8 my $vol_type = $self->_read_unpack($curr_tab_offset, 4);
332 8 $vol_type = hex $self->_reverse_hex($vol_type);
333 8 $self->{volume_type} = $map->{vol_type}->{$vol_type};
334
335 # Volume Serial Number
336 8 $curr_tab_offset = $loc_vol_tab_off + $struc_start + 8;
337 8 my $vol_serial = $self->_read_unpack($curr_tab_offset, 4);
338 8 $vol_serial = $self->_reverse_hex($vol_serial);
339 8 $self->{volume_serial} = $vol_serial;
340
341 # Get the location, and length of the volume label
342 # we should really read the vol_label_loc from offset Ch
343 8 my $vol_label_loc = $loc_vol_tab_off + $struc_start + 16;
344 8 my $vol_label_len = $local_vol_tab_end - $vol_label_loc;
345 8 my $vol_label = $self->_read_unpack_ascii($vol_label_loc, $vol_label_len);
346 8 $self->{volume_label} = $vol_label;
347
348 # This is the offset of the base path info within the
349 # File Info structure
350 # Random Garbage when bit0 is clear in volume flags
351 8 my $base_path_off = $self->_read_unpack($base_path_off, 4);
352 8 $base_path_off = hex $self->_reverse_hex($base_path_off);
353 8 $base_path_off = $struc_start + $base_path_off;
354
355 # Read base path data upto NULL term
356 8 my $bp_data = $self->_read_null_term($base_path_off);
357 8 $self->{base_path} = $bp_data;
358 8 ▶ if ($resolve) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resolve) 2 6 8
359 2 close $self->{_fh};
360 2 delete $self->{_fh};
361 2 return $self;
362 }
363 }
364
365 # Network Volume Table
366 18 ▶ if ($vol_bits[0] == 0 and $vol_bits[1] == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vol_bits[0] == 0 and $vol_bits[1] == 1) 4 14 18
Condition66%2/3
$vol_bits[0] == 0 and $vol_bits[1] == 1
!ll&&!rl&&r
1404
MC/DC50%1/2
$vol_bits[0] == 0 and $vol_bits[1] == 1
$vol_bits[0] == 0 $vol_bits[1] == 1
Truth table66%2/3
A and B
A= $vol_bits[0] == 0B= $vol_bits[1] == 1
ABresult
0X0
100
111
367 4 $net_vol_off = hex $self->_reverse_hex($self->_read_unpack($net_vol_off, 4));
368 4 $net_vol_off = $struc_start + $net_vol_off;
369 4 my $net_vol_len = $self->_read_unpack($net_vol_off, 4);
370 4 $net_vol_len = hex $self->_reverse_hex($net_vol_len);
371
372 # Network Share Name
373 4 my $net_share_name_off = $net_vol_off + 8;
374 4 my $net_share_name_loc = hex $self->_reverse_hex($self->_read_unpack($net_share_name_off, 4));
375 4 ▶ if ($net_share_name_loc ne "20") {
Branch: 1 branch
BranchTrueFalseTotal
if ($net_share_name_loc ne 20) 0 4 4
376 0 close delete $self->{_fh};
377 0 $self->{error} = 'Error: NSN ofset should always be 14h';
378 0 close $self->{_fh};
379 0 delete $self->{_fh};
380 0 return $self;
381 }
382 4 $net_share_name_loc = $net_vol_off + $net_share_name_loc;
383 4 my $net_share_name = $self->_read_null_term($net_share_name_loc);
384 4 $self->{base_path} = $net_share_name;
385 4 ▶ if ($resolve) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resolve) 1 3 4
386 1 close $self->{_fh};
387 1 delete $self->{_fh};
388 1 return $self;
389 }
390
391 # Mapped Network Drive Info
392 3 my $net_share_mdrive = $net_vol_off + 12;
393 3 $net_share_mdrive = $self->_read_unpack($net_share_mdrive, 4);
394 3 $net_share_mdrive = hex $self->_reverse_hex($net_share_mdrive);
395 3 ▶ if ($net_share_mdrive ne "0") {
Branch: 1 branch
BranchTrueFalseTotal
if ($net_share_mdrive ne 0) 3 0 3
396 3 $net_share_mdrive = $net_vol_off + $net_share_mdrive;
397 3 $net_share_mdrive = $self->_read_null_term($net_share_mdrive);
398 3 $self->{mapped_drive} = $net_share_mdrive;
399 }
400 }
401
402 17 ▶ if ($vol_bits[0] == 1 and $vol_bits[1] == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vol_bits[0] == 1 and $vol_bits[1] == 1) 8 9 17
Condition100%3/3
$vol_bits[0] == 1 and $vol_bits[1] == 1
!ll&&!rl&&r
368
MC/DC100%2/2
$vol_bits[0] == 1 and $vol_bits[1] == 1
$vol_bits[0] == 1 $vol_bits[1] == 1
Truth table100%3/3
A and B
A= $vol_bits[0] == 1B= $vol_bits[1] == 1
ABresult
0X0
100
111
403 # Finding the location, as I'm not sure this is always 104
404 8 for my $i (1..10000) {
405 208 my $n = 4 * $i;
406 208 my $l = $self->_read_unpack($n, 4);
407 208 $l = hex $self->_reverse_hex($l);
408 208 my $net_share_name_off = $n + 8;
409 208 my $net_share_name_loc = hex $self->_reverse_hex($self->_read_unpack($net_share_name_off, 4));
410 208 ▶ if ($net_share_name_loc ne "20") {
Branch: 1 branch
BranchTrueFalseTotal
if ($net_share_name_loc ne 20) 200 8 208
411 200 next;
412 }
413 8 $net_vol_off = $n;
414 8 last;
415 }
416
417 8 my $net_vol_len = $self->_read_unpack($net_vol_off, 4);
418 8 $net_vol_len = hex $self->_reverse_hex($net_vol_len);
419
420 # Network Share Name
421 8 my $net_share_name_off = $net_vol_off + 8;
422 8 my $net_share_name_loc = hex $self->_reverse_hex($self->_read_unpack($net_share_name_off, 4));
423 8 ▶ if ($net_share_name_loc ne "20") {
Branch: 1 branch
BranchTrueFalseTotal
if ($net_share_name_loc ne 20) 0 8 8
424 0 close delete $self->{_fh};
425 0 $self->{error} = 'Error: NSN ofset should always be 14h';
426 0 close $self->{_fh};
427 0 delete $self->{_fh};
428 0 return $self;
429 }
430 8 $net_share_name_loc = $net_vol_off + $net_share_name_loc;
431 8 my $net_share_name = $self->_read_null_term($net_share_name_loc);
432 8 $self->{base_path} = $net_share_name;
433 8 ▶ if ($resolve) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resolve) 2 6 8
434 2 close $self->{_fh};
435 2 delete $self->{_fh};
436 2 return $self;
437 }
438
439 # Mapped Network Drive Info
440 6 my $net_share_mdrive = $net_vol_off + 12;
441 6 $net_share_mdrive = $self->_read_unpack($net_share_mdrive, 4);
442 6 $net_share_mdrive = hex $self->_reverse_hex($net_share_mdrive);
443 6 ▶ if ($net_share_mdrive ne "0") {
Branch: 1 branch
BranchTrueFalseTotal
if ($net_share_mdrive ne 0) 0 6 6
444 0 $net_share_mdrive = $net_vol_off + $net_share_mdrive;
445 0 $net_share_mdrive = $self->_read_null_term($net_share_mdrive);
446 0 $self->{mapped_drive} = $net_share_mdrive;
447 }
448 }
449
450 #Remaining Path
451 15 $rem_path_off = $self->_read_unpack($rem_path_off, 4);
452 15 $rem_path_off = hex $self->_reverse_hex($rem_path_off);
453 15 $rem_path_off = $struc_start + $rem_path_off;
454 15 my $rem_data = $self->_read_null_term($rem_path_off);
455 15 $self->{remaining_path} = $rem_data;
456
457 # The next starting location is the end of the structure
458 15 my $next_loc = $struc_end;
459 15 my $addnl_text;
460
461 # Description String
462 # present if bit2 is set in header flags.
463 15 ▶ if ($flag_bits[2] eq "1") {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag_bits[2] eq 1) 0 15 15
464 0 ($addnl_text, $next_loc) = $self->_add_info($next_loc);
465 0 $self->{description} = $addnl_text;
466 0 $next_loc = $next_loc + 1;
467 }
468
469 # Relative Path
470 15 ▶ if ($flag_bits[3] eq "1") {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag_bits[3] eq 1) 0 15 15
471 0 ($addnl_text, $next_loc) = $self->_add_info($next_loc);
472 0 $self->{relative_path} = $addnl_text;
473 0 $next_loc = $next_loc + 1;
474 }
475 # Working Dir
476 15 ▶ if ($flag_bits[4] eq "1") {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag_bits[4] eq 1) 6 9 15
477 6 ($addnl_text, $next_loc) = $self->_add_info($next_loc);
478 6 ($self->{working_directory} = $addnl_text) =~ s/\x00//g;
479 6 $next_loc = $next_loc + 1;
480 }
481 # CMD Line
482 15 ▶ if ($flag_bits[5] eq "1") {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag_bits[5] eq 1) 0 15 15
483 0 ($addnl_text, $next_loc) = $self->_add_info($next_loc);
484 0 $self->{command_line} = $addnl_text;
485 0 $next_loc = $next_loc + 1;
486 }
487 #Icon filename
488 15 ($addnl_text, $next_loc) = $self->_add_info($next_loc);
489 15 ▶ if ($flag_bits[6] eq "1") {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag_bits[6] eq 1) 0 15 15
490 0 $self->{icon_filename} = $addnl_text;
491 }
492 15 close delete $self->{_fh};
493 15 $self;
494 }
495
496 21 ▶sub _add_info {
Subroutine _add_info: called CC=2 SCAR=6.9
497 21 my $self = shift;
498 21 my ($tmp_start_loc) = shift;
499 21 my $tmp_len = 2 * hex $self->_reverse_hex($self->_read_unpack($tmp_start_loc, 1));
500 21 $tmp_start_loc++;
501 21 ▶ if ($tmp_len ne "0") {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp_len ne '0') { } 15 6 21
502 15 my $tmp_string = $self->_read_unpack_ascii($tmp_start_loc, $tmp_len);
503 15 my $now_loc = tell;
504 15 return ($tmp_string, $now_loc);
505 } else {
506 6 my $now_loc = tell;
507 6 my $tmp_string = 'Null';
508 6 return ($tmp_string, $now_loc);
509 }
510 }
511
512 781 ▶sub _read_unpack {
Subroutine _read_unpack: called CC=2 SCAR=7.0
513 781 my $self = shift;
514 781 my ($loc, $bites) = @_;
515 781 my $tmp_data;
516 781 ▶ seek ($self->{_fh}, $loc, 0) or croak "Can't seek to $loc";
Branch: 1 branch
BranchTrueFalseTotal
seek $self->{"_fh"}, $loc, 0 or croak("Can't seek to $loc") 781 0 781
517 781 read $self->{_fh}, $tmp_data, $bites;
518 781 $tmp_data = unpack 'H*', $tmp_data;
519 781 return $tmp_data;
520 }
521
522 23 ▶sub _read_unpack_ascii {
Subroutine _read_unpack_ascii: called CC=2 SCAR=7.0
523 23 my $self = shift;
524 23 my ($loc, $bites) = @_;
525 23 my $tmp_data;
526 23 ▶ seek ($self->{_fh}, $loc, 0) or croak "Can't seek to $loc\n";
Branch: 1 branch
BranchTrueFalseTotal
seek $self->{"_fh"}, $loc, 0 or croak("Can't seek to $loc\n") 23 0 23
527 23 read $self->{_fh}, $tmp_data, $bites;
528 23 $tmp_data = unpack 'A*', $tmp_data;
529 23 return $tmp_data;
530 }
531
532 60 ▶sub _read_unpack_bin {
Subroutine _read_unpack_bin: called CC=2 SCAR=7.0
533 60 my $self = shift;
534 60 my ($loc, $bites) = @_;
535 60 my $tmp_data;
536 60 ▶ seek ($self->{_fh}, $loc, 0) or croak "Can't seek to $loc\n";
Branch: 1 branch
BranchTrueFalseTotal
seek $self->{"_fh"}, $loc, 0 or croak("Can't seek to $loc\n") 60 0 60
537 60 read $self->{_fh}, $tmp_data, $bites;
538 60 $tmp_data = unpack 'b*', $tmp_data;
539 60 return $tmp_data;
540 }
541
542 60 ▶sub _MStime_to_unix {
Subroutine _MStime_to_unix: called CC=1 SCAR=0.0
543 60 my $self = shift;
544 60 my $mstime_dec = shift;
545 # The number of seconds between Unix/FILETIME epochs
546 60 my $MSConversion = '11644473600';
547 # Convert 100ms increments to Seconds.
548 60 $mstime_dec *= .0000001;
549 # Add difference in epochs
550 60 $mstime_dec -= $MSConversion;
551 60 sprintf '%0.3f', $mstime_dec;
552 }
553
554 649 ▶sub _reverse_hex {
Subroutine _reverse_hex: called CC=3 SCAR=11.0
555 649 my $self = shift;
556 649 my $HEXDATE = shift;
557 649 my @bytearry;
558 649 my $byte_cnt = 0;
559 649 ▶ my $max_byte_cnt = length($HEXDATE) < 16 ? int(length($HEXDATE) / 2) : 8;
Branch: 1 branch
BranchTrueFalseTotal
length $HEXDATE < 16 ? : 589 60 649
560 649 my $byte_offset = 0;
561 649 ▶ while ($byte_cnt < $max_byte_cnt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byte_cnt < $max_byte_cnt) 2727 649 3376
562 2727 my $tmp_str = substr $HEXDATE, $byte_offset, 2;
563 2727 push @bytearry, $tmp_str;
564 2727 $byte_cnt++;
565 2727 $byte_offset += 2;
566 }
567 649 return join '', reverse @bytearry;
568 }
569
570 38 ▶sub _read_null_term {
Subroutine _read_null_term: called CC=3 SCAR=11.0
571 38 my $self = shift;
572 38 my $loc = shift;
573 # Save old record seperator
574 38 my $old_rs = $/;
575 # Set new seperator to NULL term.
576 38 $/ = "\0";
577 38 ▶ seek ($self->{_fh}, $loc, 0) or die "Can't seek to $loc\n";
Branch: 1 branch
BranchTrueFalseTotal
seek $self->{"_fh"}, $loc, 0 or die "Can't seek to $loc\n" 38 0 38
578 38 my $fh = $self->{_fh};
579 38 my $term_data = <$fh>;
580 38 ▶ chomp $term_data if $term_data;
Branch: 1 branch
BranchTrueFalseTotal
if $term_data 32 6 38
581 # Reset
582 38 $/ = $old_rs;
583 38 return $term_data;
584 }
585
586 {
587 package Parse::Windows::Shortcut::Bigint;
588 2 require Math::BigInt;
589 2 require bigint;
590
591 60 ▶ sub bighex {
Subroutine bighex: called CC=1 SCAR=0.0
592 60 my $v = shift;
593 60 my $h = bigint::hex $v;
594 60 $h.'';
595 }
596 }
597
598
599 =head1 SYNOPSIS
600
601 This module reads Win32 shortcuts (*.lnk files) to obtain the meta data in them.
602
603 Its goal is to be able to resolve the path they point to (along with other data),
604 from any platform/OS, without the need for extra dependencies.
605
606 Some examples of usage:
607
608 use Parse::Lnk;
609
610 my $data = Parse::Lnk->from($filename);
611
612 # $data is now a hashref if the file was parsed successfully.
613 # undef if not.
614
615 ##########
616 # Or ... #
617 ##########
618
619 use Parse::Lnk qw(parse_lnk);
620
621 my $data = parse_lnk $filename;
622
623 # $data is now a hashref if the file was parsed successfully.
624 # undef if not.
625
626 ##########
627 # Or ... #
628 ##########
629
630 use Parse::Lnk qw(resolve_lnk);
631
632 my $path = resolve_lnk $filename;
633
634 # $path is now a string with the path the lnk file points to.
635 # undef if the lnk file was not parsed successfully.
636
637 ###############################################################
638 # Or, if you want a little more information/control on errors #
639 ###############################################################
640
641 use Parse::Lnk;
642
643 my $lnk = Parse::Lnk->new;
644
645 $lnk->parse($filename) or die $lnk->{error};
646
647 # Or:
648
649 $lnk->parse($filename);
650
651 if ($lnk->{error}) {
652 # ... do your own error handling;
653 }
654
655
656
657
658 =head1 EXPORT
659
660 Nothing is exported by default. You can explicitly import this functions:
661
662 =head2 parse_lnk($filename)
663
664 This will return a Parse::Lnk instance, which is a hashref. The keys in that
665 hashref depend on the data that was parsed from the .lnk file.
666
667 It will return C<undef> on error.
668
669 use Parse::Lnk qw(parse_lnk);
670
671 my $lnk = parse_lnk $filename;
672
673 if ($lnk) {
674 print "$filename points to path $lnk->{base_path}\n";
675
676 my $create_date = localtime $lnk->{create_time};
677 print "$filename was created on $create_date";
678 } else {
679 print "Could not parse $filename";
680 }
681
682 =head2 resolve_lnk($filename)
683
684 This will return the path the .lnk file is pointing to.
685
686 It will return C<undef> on error.
687
688 use Parse::Lnk qw(resolve_lnk);
689
690 my $path = resolve_lnk $filename;
691
692 if ($path) {
693 print "$filename points to path $path";
694 } else {
695 print "Could not parse $filename";
696 }
697
698
699 =head1 METHODS
700
701 You can create a C<Parse::Lnk> instance and call a few methods on it. This
702 may give you more control/information when something goes wrong while parsing
703 the file.
704
705 =head2 new
706
707 This creates a new instance. You can pass the C<filename> value as argument,
708 or you can set/change it later.
709
710 use Parse::Lnk;
711
712 my $lnk = Parse::Lnk->new(filename => $filename);
713
714 # or
715
716 my $lnk = Parse::Lnk->new;
717 $lnk->{filename} = $filename;
718
719 =head2 parse
720
721 This method will parse the current C<filename> in the instance. You can change
722 the value of C<filename> and parse again at any point.
723
724 use Parse::Lnk;
725
726 my $lnk = Parse::Lnk->new(filename => $filename);
727
728 $lnk->parse;
729
730 if ($lnk->{error}) {
731 # handle the error
732 } else {
733 print "$filename points to $lnk->{base_path}";
734 }
735
736 for my $other_filename (@filenames) {
737 $lnk->{filename} = $other_filename;
738 $lnk->parse;
739
740 if ($lnk->{error}) {
741 # handle the error
742 next;
743 }
744
745 print "$other_filename points to $lnk->{base_path}";
746 }
747
748 =head2 from
749
750 It will return a C<Parse::Lnk> instance, or undef on error. This method was
751 written with plain package name calling in mind:
752
753 use Parse::Lnk;
754
755 my $lnk = Parse::Lnk->from($filename);
756
757 if ($lnk) {
758 print "$filename points to path $lnk->{base_path}\n";
759
760 my $create_date = localtime $lnk->{create_time};
761 print "$filename was created on $create_date";
762 } else {
763 print "Could not parse $filename";
764 }
765
766
767 =head1 AUTHOR
768
769 Francisco Zarabozo, C<< <zarabozo at cpan.org> >>
770
771 =head1 BUGS
772
773 I'm sure there are many. I haven't found bugs with the lnk files I've tested
774 it. If you find a bug or you have a problem reading a shortcut/lnk file,
775 please don't hesitate to report it and don't forget to include the file in
776 question. If you are on Windows, you will have to zip the file in a way that
777 is the lnk file the one being zipped and not the actual directory/file it
778 is pointing to. I promise to look at any report and work on a solution as
779 fast as I can.
780
781 Please report any bugs or feature requests to C<bug-parse-lnk at rt.cpan.org>, or through
782 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Parse-Lnk>. I will be notified, and then you'll
783 automatically be notified of progress on your bug as I make changes.
784
785
786 =head1 SUPPORT
787
788 You can find documentation for this module with the perldoc command.
789
790 perldoc Parse::Lnk
791
792
793 You can also look for information at:
794
795 =over 4
796
797 =item * RT: CPAN's request tracker (report bugs here)
798
799 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Parse-Lnk>
800
801 =item * AnnoCPAN: Annotated CPAN documentation
802
803 L<http://annocpan.org/dist/Parse-Lnk>
804
805 =item * CPAN Ratings
806
807 L<https://cpanratings.perl.org/d/Parse-Lnk>
808
809 =item * Search CPAN
810
811 L<https://metacpan.org/release/Parse-Lnk>
812
813 =back
814
815
816 =head1 ACKNOWLEDGEMENTS
817
818 Many sections of the code were adapted from Jacob Cunningham's
819 L<Windows LNK File Parser|https://sourceforge.net/projects/jafat/files/lnk-parse/lnk-parse-1.0/lnk-parse-1.0.tar.gz>,
820 licensed under the GNU General Public License Version 2.
821
822
823 =head1 LICENSE AND COPYRIGHT
824
825 This software is copyright (c) 2021-2024 by Francisco Zarabozo.
826
827 This is free software; you can redistribute it and/or modify it under
828 the same terms as the Perl 5 programming language system itself.
829
830
831 =cut
832
833 2 1;
↑ Summary