lib/Win32/Exe.pm

filter: Statementstmt 41.2% 135 / 327 Branchbran 17.5% 28 / 160 Conditioncond 17.8% 5 / 28 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 58.3% 28 / 48 Podpod 54.5% 18 / 33 totaltotal 34.8% 214 / 614 CC 101 SCAR 35.6
CC
101
Cov
33%
set_version_info
132.0
update_debug_directory
108.1
create_resource_section
72.0
CRAP 35.2
« lib/Win32/Exe/Resource/Version.pm ↑ Summary lib/Win32/Exe/InsertResourceSection.pm »
1 package Win32::Exe;
2 5 $Win32::Exe::VERSION = '0.17';
3  
4 =head1 NAME
5  
6 Win32::Exe - Manipulate Win32 executable files
7  
8 =head1 VERSION
9  
10 This document describes version 0.17 of Win32::Exe, released
11 July 19, 2011.
12  
13 =head1 SYNOPSIS
14  
15 use Win32::Exe;
16 my $exe = Win32::Exe->new('c:/windows/notepad.exe');
17
18 # add a default resource structure if none exists
19 # create_resource_section only works on MSWin and
20 # does not work on Windows XP - requires Vista or
21 # above
22
23 $exe = $exe->create_resource_section if $exe->can_create_resource_section;
24  
25 # Get version information
26 my $info = $exe->get_version_info;
27 print qq($_ = $info->{$_}\n) for (sort keys(%$info));
28  
29 # Extract icons from an executable
30 my @iconnames = $exe->get_group_icon_names;
31 for ( my $i = 0; $i < @iconnames; $i++ ) {
32 my $filename = 'icon' . $i . '.ico';
33 my $iconname = $iconnames[$i];
34 $exe->extract_group_icon($iconname,$filename);
35 }
36  
37 # Import icons from a .exe or .ico file and write back the file
38 $exe->update( icon => '/c/windows/taskman.exe' );
39 $exe->update( icon => 'myicon.ico' );
40  
41 # Change it to a console application, then save to another .exe
42 $exe->set_subsystem_console;
43 $exe->write('c:/windows/another.exe');
44
45 # Add a manifest section
46 $exe->update( manifest => $mymanifestxml );
47 # or a default
48 $exe->update( defaultmanifest => 1 );
49
50 # or specify manifest args
51 $exe->update( manifestargs => { ExecLevel => 'requireAdministrator' } );
52
53 # Get manifest object
54 $manifest = $exe->get_manifest if $exe->has_manifest;
55
56 # change execution level
57 $manifest->set_execution_level('requireAdministrator');
58 $exe->set_manifest($manifest);
59 $exe->write;
60
61  
62 =head1 DESCRIPTION
63  
64 This module parses and manipulating Win32 PE/COFF executable headers,
65 including version information, icons, manifest and other resources.
66 The module Win32::Exe::Manifest can be used for manifest handling.
67  
68 A script exe_update.pl is provided for simple file updates.
69  
70 Also, please see the test files in the source distributions
71 F<t/> directory for examples of using this module.
72  
73 =head1 METHODS
74  
75 =head2 new
76  
77 my $exe = Win32::Exe->new($filename);
78  
79 Create a Win32::Exe object from $filename. Filename can be an executable
80 or a DLL.
81  
82 =head2 update
83  
84 $exe->update( icon => 'c:/my/icon.ico',
85 gui => 1,
86 info => [ 'FileDescription=My File', 'FileVersion=1.4.3.3567' ],
87 manifest => 'c:/my/manifest.xml',
88 manifestargs => [ 'ExecLevel=asInvoker', 'CommonControls=1' ],
89 );
90  
91 The B<update> method provides a convenience method for the most common actions. It
92 writes the information you provide to the file opened by Win32::Exe->new($filename).
93 You do not have to call $exe->write - the method automatically updates the opened
94 file.
95  
96 Param detail:
97  
98 B<icon> Pass the name of an executable, dll or ico file to extract the icon and
99 make it the main icon for the Win32 executable.
100  
101 B<info> Pass a reference to an array of strings containing key - value pairs
102 separated by '='.
103  
104 e.g. info => [ 'FileDescription=My File', 'FileVersion=1.4.3.3567' ]
105  
106 Recognised keys are
107  
108 Comments CompanyName FileDescription FileVersion
109 InternalName LegalCopyright LegalTrademarks OriginalFilename
110 ProductName ProductVersion
111  
112 B<gui or console> Use parameter 'gui' or 'console' to set the executable
113 subsystem to Windows or Console. You can, of course, only use one or the other
114 of gui / console, not both.
115  
116 B<manifest> Specify a manifest file to add to the executable resources.
117  
118 B<manifestargs> As an alternative to specifying a manifest file, pass a
119 reference to an array of strings containing key - value pairs separated
120 by '='.
121  
122 e.g. manifestargs => [ 'ExecLevel=asInvoker', 'CommonControls=1' ]
123  
124 Recognised keys are
125  
126 ExecutionLevel UIAccess ExecName Description
127 CommonControls Version
128  
129 =head2 create_resource_section
130  
131 $exe = $exe->create_resource_section if $exe->can_create_resource_section;
132  
133 If an executable file does not have an existing resource section, you must create
134 one before attempting to add version, icon or manifest resources. The method
135 create_resource_section is only available on MSWin platforms. Also, the method
136 will fail if your windows version is Windows XP or below. You can check if it is
137 possible to call create_resource_section by calling $exe->can_create_resource_section.
138 After calling create_resource_section, the original Win32::Exe object does not
139 reference the updated data. The method therefore returns a reference to a new
140 Win32::Exe object that references the updated data.
141  
142 Always call as :
143  
144 $exe = $exe->create_resource_section if $exe->can_create_resource_section;
145  
146 if the $exe already has a resource section, this call will safely return a reference
147 to the original object without updating the original exe.
148  
149 =head2 can_create_resource_section
150  
151 $exe = $exe->create_resource_section if $exe->can_create_resource_section;
152  
153 Check if the operating system and version allow addition of a resource section
154 when none exists in the target executable.
155  
156 =head2 get_manifest
157  
158 my $manifest = $exe->get_manifest;
159  
160 Retrieves a Win32::Exe::Manifest object. (See docs for Win32::Exe::Manifest)
161  
162 =head2 set_manifest
163  
164 $exe->set_manifest($manifest);
165 $exe->write;
166
167 Takes a Win32::Exe::Manifest object. You must explicitly call 'write' to commit
168 changes to file. Also takes a filepath to an xml file containing raw manifest
169 information.
170  
171 =head2 set_manifest_args
172  
173 $exe->set_manifest_args($argref);
174 $exe->write;
175  
176 Accepts a reference to a hash with one or more of the the keys
177  
178 ExecutionLevel UIAccess ExecName Description
179 CommonControls Version
180  
181 Also accepts a reference to an array of strings of the format:
182 [ 'key1=value1', 'key2=value2' ]
183  
184 Example Values:
185  
186 ExecutionLevel=asInvoker
187 UIAccess=false
188 CommonControls=1
189 Version=6.8.67.334534
190 ExecName=My.Application
191 Description=My Application
192  
193 The CommonControls argument can be specified to add a dependency on
194 Common Controls Library version 6.0.0.0
195  
196 =head2 get_version_info
197  
198 my $inforef = $exe->get_version_info;
199  
200 Returns a reference to a hash with the keys:
201  
202 Comments CompanyName FileDescription FileVersion
203 InternalName LegalCopyright LegalTrademarks OriginalFilename
204 ProductName ProductVersion
205  
206 =head2 set_version_info
207  
208 $exe->set_version_info($inforef);
209 $exe->write;
210  
211 Accepts a reference to a hash with one or more of the the keys
212  
213 Comments CompanyName FileDescription FileVersion
214 InternalName LegalCopyright LegalTrademarks OriginalFilename
215 ProductName ProductVersion
216  
217 Also accepts a reference to an array of strings of the format:
218 [ 'key1=value1', 'key2=value2' ]
219  
220 =head2 set_subsystem_windows
221  
222 $exe->set_subsystem_windows;
223 $exe->write;
224  
225 Sets the executable system as 'windows'. (GUI).
226 You may also call $exe->SetSubsystem('windows);
227 This is the equivalent of $exe->update( gui => 1);
228  
229 =head2 set_subsystem_console
230  
231 $exe->set_subsystem_console;
232 $exe->write;
233  
234 Sets the executable system as 'console'.
235 You may also call $exe->SetSubsystem('console);
236 This is the equivalent of $exe->update( console => 1);
237  
238 =head2 get_subsystem
239  
240 my $subsys = $exe->get_subsystem;
241  
242 Returns a descriptive string for the subsystem.
243 Possible values: windows | console | posix | windowsce | native
244 You can usefully update executables with the windows or console
245 subsystem
246  
247 =head2 set_single_group_icon
248  
249 $exe->set_single_group_icon($iconfile);
250 $exe->write;
251  
252 Accepts the path to an icon file. Replaces all the icons in the
253 exec with the icons from the file.
254  
255 =head2 get_group_icon_names
256  
257 my @iconnames = $exe->get_group_icon_names;
258  
259 Returns a list of the names of all the group icons in the
260 executable or dll. If there are no group icons, returns an empty
261 list. The names returned can be used as parameters to other icon
262 handling methods.
263  
264 =head2 get_group_icon
265  
266 $exe->get_group_icon($groupiconname);
267  
268 Accepts a group icon name as returned by get_group_icon_names.
269 Returns the Wx::Exe::Resource::GroupIcon object for the named
270 GroupIcon
271  
272 =head2 add_group_icon
273  
274 $exe->add_group_icon($groupiconname, $iconfilepath);
275 $exe->write;
276  
277 Accepts a group icon name and a path to an icon file. Adds the
278 icon to the exec without affecting existing icons. The group
279 icon name must not already exist.
280  
281 =head2 replace_group_icon
282  
283 $exe->replace_group_icon($groupiconname, $iconfilepath);
284 $exe->write;
285  
286 Accepts a group icon name and a path to an icon file.
287 Replaces the groupicon named with the contents of the icon file.
288  
289 =head2 remove_group_icon
290  
291 $exe->remove_group_icon($groupiconname);
292 $exe->write;
293  
294 Accepts a group icon name. Removes the group icon with that name
295 from the exec or dll.
296  
297 =head2 export_group_icon
298  
299 $exe->export_group_icon($groupiconname, $iconfilepath);
300  
301 Accepts a group icon name and a .ico filepath. Writes the named
302 icon group to the file in filepath.
303  
304  
305 =cut
306  
307 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
308 5 ▶use base 'Win32::Exe::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
309 5 ▶use constant FORMAT => (
Subroutine BEGIN: called CC=1 SCAR=0.0
310 Magic => 'a2', # "MZ"
311 _ => 'a58',
312 PosPE => 'V',
313 _ => 'a{($PosPE > 64) ? $PosPE - 64 : "*"}',
314 PESig => 'a4',
315 Data => 'a*',
316 5 );
317 5 ▶use constant DELEGATE_SUBS => (
Subroutine BEGIN: called CC=1 SCAR=0.0
318 'IconFile' => [ 'dump_iconfile', 'write_iconfile' ],
319 5 );
320 5 ▶use constant DISPATCH_FIELD => 'PESig';
Subroutine BEGIN: called CC=1 SCAR=0.0
321 5 ▶use constant DISPATCH_TABLE => (
Subroutine BEGIN: called CC=1 SCAR=0.0
322 "PE\0\0" => "PE",
323 2 ▶ '*' => sub { die "Incorrect PE header -- not a valid .exe file" },
Subroutine __ANON__: called CC=1 SCAR=0.0
324 5 );
325 5 ▶use constant DEBUG_INDEX => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
326 5 ▶use constant DEBUG_ENTRY_SIZE => 28;
Subroutine BEGIN: called CC=1 SCAR=0.0
327  
328 5 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
329 5 ▶use Win32::Exe::IconFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
330 5 ▶use Win32::Exe::DebugTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
331 5 ▶use Win32::Exe::Manifest;
Subroutine BEGIN: called CC=1 SCAR=0.0
332  
333 0 ▶sub is_application { ( $_[0]->is_assembly ) ? 0 : 1; }
Subroutine is_application: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->is_assembly ? : 0 0 0
334  
335 3 ▶sub is_assembly { $_[0]->Characteristics & 0x2000; }
Subroutine is_assembly: called CC=1 SCAR=0.0
Pod undocumented
336  
337 1 ▶sub has_resource_section {
Subroutine has_resource_section: called CC=2 SCAR=7.1
Pod undocumented
338 1 my ($self) = @_;
339 1 my $section = $self->first_member('Resources');
340 1 ▶ return( $section ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$section ? : 1 0 1
341 }
342  
343 0 ▶sub can_create_resource_section {
Subroutine can_create_resource_section: not called CC=3 SCAR=24.8
Pod documented
344 0 my $self = shift;
345 0 ▶ return 0 if ($^O !~ /^mswin/i );
Branch: 1 branch
BranchTrueFalseTotal
unless $^O =~ /^mswin/i 0 0 0
346 0 require Win32;
347 0 my ($winstring, $winmajor, $winminor, $winbuild, $winid) = Win32::GetOSVersion();
348 0 ▶ return ( $winmajor > 5 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$winmajor > 5 ? : 0 0 0
349 }
350  
351 0 ▶sub create_resource_section {
Subroutine create_resource_section: not called CC=8 SCAR=42.8
Pod documented
352 0 my $self = shift;
353 0 ▶ return $self if($self->has_resource_section || ( $^O !~ /^mswin/i ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_resource_section or not $^O =~ /^mswin/i 0 0 0
Condition0%0/3
$self->has_resource_section or not $^O =~ /^mswin/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->has_resource_section or not $^O =~ /^mswin/i
$self->has_resource_section not $^O =~ /^mswin/i
Truth table0%0/3
A or not(B)
A= $self->has_resource_sectionB= not $^O =~ /^mswin/i
ABresult
1X1
011
000
354 0 ▶ if(!$self->can_create_resource_section) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can_create_resource_section) 0 0 0
355 0 die('Cannot create resource section on this version of Windows');
356 }
357 0 require Win32::Exe::InsertResourceSection;
358 0 ▶ my $filename = (exists($self->{filename}) && (-f $self->{filename} )) ? $self->{filename} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'filename'} && -f $self->{'filename'} ? : 0 0 0
Condition0%0/3
exists $self->{"filename"} and -f $self->{"filename"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"filename"} and -f $self->{"filename"}
exists $self->{"filename"} -f $self->{"filename"}
Truth table0%0/3
A and B
A= exists $self->{"filename"}B= -f $self->{"filename"}
ABresult
0X0
100
111
359 0 ▶ return $self if !$filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 0 0
360 0 ▶ if(my $newref = Win32::Exe::InsertResourceSection::insert_pe_resource_section($filename)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $newref = Win32::Exe::InsertResourceSection::insert_pe_resource_section($filename)) { } 0 0 0
361 0 return $newref;
362 } else {
363 0 return $self;
364 }
365 }
366  
367 17 ▶sub resource_section {
Subroutine resource_section: called CC=4 SCAR=22.9
Pod undocumented
368 17 my ($self) = @_;
369 17 my $section = $self->first_member('Resources');
370 17 ▶ return $section if $section;
Branch: 1 branch
BranchTrueFalseTotal
if $section 17 0 17
371 0 my $wmsg = 'No resource section found in file ';
372 0 ▶ $wmsg .= $self->{filename} if(exists($self->{filename}) && $self->{filename});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"filename"} and $self->{"filename"} 0 0 0
Condition0%0/3
exists $self->{"filename"} and $self->{"filename"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"filename"} and $self->{"filename"}
exists $self->{"filename"} $self->{"filename"}
Truth table0%0/3
A and B
A= exists $self->{"filename"}B= $self->{"filename"}
ABresult
0X0
100
111
373 0 warn $wmsg;
374 0 return undef;
375 }
376  
377 4 ▶sub sections {
Subroutine sections: called CC=2 SCAR=7.1
Pod undocumented
378 4 my ($self) = @_;
379 4 ▶ my $method = (wantarray ? 'members' : 'first_member');
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 0 4
380 4 return $self->members('Section');
381 }
382  
383 11 ▶sub data_directories {
Subroutine data_directories: called CC=1 SCAR=0.0
Pod undocumented
384 11 my ($self) = @_;
385 11 return $self->members('DataDirectory');
386 }
387  
388 2 ▶sub update_debug_directory {
Subroutine update_debug_directory: called CC=15 SCAR=46.8
Pod undocumented
389 2 my ($self, $boundary, $extra) = @_;
390  
391 2 ▶ $self->SetSymbolTable( $self->SymbolTable + $extra )
Branch: 1 branch
BranchTrueFalseTotal
if $boundary <= $self->SymbolTable 0 2 2
392 if ($boundary <= $self->SymbolTable);
393  
394 2 my @dirs = $self->data_directories;
395 2 ▶ return if DEBUG_INDEX > $#dirs;
Branch: 1 branch
BranchTrueFalseTotal
if 6 > $#dirs 0 2 2
396  
397 2 ▶ my $dir = $dirs[DEBUG_INDEX] or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dir = $dirs[6] or return 2 0 2
398 2 my $size = $dir->Size;
399 2 my $addr = $dir->VirtualAddress;
400  
401 2 ▶ return unless $size or $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless $size or $addr 2 0 2
Condition33%1/3
$size or $addr
l!l&&r!l&&!r
002
MC/DC0%0/2
$size or $addr
$size $addr
Truth table33%1/3
A or B
A= $sizeB= $addr
ABresult
1X1
011
000
402  
403 0 ▶ my $count = $size / DEBUG_ENTRY_SIZE or return;
Branch: 1 branch
BranchTrueFalseTotal
my $count = $size / 28 or return 0 0 0
404  
405 0 ▶ (($size % DEBUG_ENTRY_SIZE) == 0) or return;
Branch: 1 branch
BranchTrueFalseTotal
$size % 28 == 0 or return 0 0 0
406  
407 0 foreach my $section ($self->sections) {
408 0 my $offset = $section->FileOffset;
409 0 my $f_size = $section->FileSize;
410 0 my $v_addr = $section->VirtualAddress;
411  
412 0 ▶ next unless $v_addr <= $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless $v_addr <= $addr 0 0 0
413 0 ▶ next unless $addr < ($v_addr + $f_size);
Branch: 1 branch
BranchTrueFalseTotal
unless $addr < $v_addr + $f_size 0 0 0
414 0 ▶ next unless ($addr + $size) < ($v_addr + $f_size);
Branch: 1 branch
BranchTrueFalseTotal
unless $addr + $size < $v_addr + $f_size 0 0 0
415  
416 0 $offset += $addr - $v_addr;
417 0 my $data = $self->substr($offset, $size);
418  
419 0 my $table = Win32::Exe::DebugTable->new(\$data);
420  
421 0 foreach my $dir ($table->members) {
422 0 ▶ next unless $boundary <= $dir->Offset;
Branch: 1 branch
BranchTrueFalseTotal
unless $boundary <= $dir->Offset 0 0 0
423  
424 0 $dir->SetOffset($dir->Offset + $extra);
425 0 ▶ $dir->SetVirtualAddress($dir->VirtualAddress + $extra)
Branch: 1 branch
BranchTrueFalseTotal
if $dir->VirtualAddress > 0 0 0 0
426 if $dir->VirtualAddress > 0;
427 }
428  
429 0 $self->substr($offset, $size, $table->dump);
430 0 last;
431 }
432 }
433  
434 0 ▶sub default_info {
Subroutine default_info: not called CC=1 SCAR=6.9
Pod undocumented
435 0 my $self = shift;
436  
437 0 my $filename = File::Basename::basename($self->filename);
438  
439 0 return join(';',
440 "CompanyName= ",
441 "FileDescription= ",
442 "FileVersion=0.0.0.0",
443 "InternalName=$filename",
444 "LegalCopyright= ",
445 "LegalTrademarks= ",
446 "OriginalFilename=$filename",
447 "ProductName= ",
448 "ProductVersion=0.0.0.0",
449 );
450 }
451  
452 2 ▶sub update {
Subroutine update: called CC=11 SCAR=28.7
Pod documented
453 2 my ($self, %args) = @_;
454
455 2 ▶ if ($args{defaultmanifest}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"defaultmanifest"}) 0 2 2
456 0 $self->add_default_manifest();
457 }
458
459 2 ▶ if (my $manifest = $args{manifest}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $manifest = $args{"manifest"}) 1 1 2
460 1 $self->set_manifest($manifest);
461 }
462
463 2 ▶ if (my $manifestargs = $args{manifestargs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $manifestargs = $args{"manifestargs"}) 1 1 2
464 1 $self->set_manifest_args($manifestargs);
465 }
466  
467 1 ▶ if (my $icon = $args{icon}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $icon = $args{"icon"}) 0 1 1
468 0 my @icons = Win32::Exe::IconFile->new($icon)->icons;
469 0 ▶ $self->set_icons(\@icons) if @icons;
Branch: 1 branch
BranchTrueFalseTotal
if @icons 0 0 0
470 }
471  
472 1 ▶ if (my $info = $args{info}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $info = $args{"info"}) 0 1 1
473 0 $self->set_version_info( $info);
474 }
475  
476 die "'gui' and 'console' cannot both be true"
477 1 ▶ if $args{gui} and $args{console};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"gui"} and $args{"console"} 0 1 1
Condition33%1/3
$args{"gui"} and $args{"console"}
!ll&&!rl&&r
100
MC/DC0%0/2
$args{"gui"} and $args{"console"}
$args{"gui"} $args{"console"}
Truth table33%1/3
A and B
A= $args{"gui"}B= $args{"console"}
ABresult
0X0
100
111
478  
479 1 ▶ $self->SetSubsystem("windows") if $args{gui};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"gui"} 0 1 1
480 1 ▶ $self->SetSubsystem("console") if $args{console};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"console"} 0 1 1
481 1 $self->write;
482 }
483  
484 4 ▶sub icons {
Subroutine icons: called CC=3 SCAR=11.4
Pod undocumented
485 4 my ($self) = @_;
486 4 ▶ my $rsrc = $self->resource_section or return;
Branch: 1 branch
BranchTrueFalseTotal
my $rsrc = $self->resource_section or return 4 0 4
487 4 my @icons = map $_->members, $rsrc->objects('GroupIcon');
488 4 ▶ wantarray ? @icons : \@icons;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 0 4
489 }
490  
491 4 ▶sub set_icons {
Subroutine set_icons: called CC=2 SCAR=6.9
Pod undocumented
492 4 my ($self, $icons) = @_;
493  
494 4 my $rsrc = $self->resource_section;
495 4 ▶ my $name = eval { $rsrc->first_object('GroupIcon')->PathName }
Condition50%1/2
eval { do { $rsrc->first_object('GroupIcon')->PathName } } || '/#RT_GROUP_ICON/#1/#0'
l!l
40
MC/DC0%0/1
eval { do { $rsrc->first_object('GroupIcon')->PathName } } || '/#RT_GROUP_ICON/#1/#0'
eval { do { $rsrc->first_object('GroupIcon')->PathName } }
Truth table50%1/2
A
A= eval { do { $rsrc->first_object('GroupIcon')->PathName } }
Aresult
11
00
496 || '/#RT_GROUP_ICON/#1/#0';
497  
498 4 $rsrc->remove('/#RT_GROUP_ICON');
499 4 $rsrc->remove('/#RT_ICON');
500  
501 4 my $group = $self->require_class('Resource::GroupIcon')->new;
502 4 $group->SetPathName($name);
503 4 $group->set_parent($rsrc);
504 4 $rsrc->insert($group->PathName, $group);
505  
506 4 $group->set_icons($icons);
507 4 $group->refresh;
508 }
509  
510 0 ▶sub version_info {
Subroutine version_info: not called CC=2 SCAR=17.9
Pod undocumented
511 0 my ($self) = @_;
512 0 ▶ my $rsrc = $self->resource_section or return;
Branch: 1 branch
BranchTrueFalseTotal
my $rsrc = $self->resource_section or return 0 0 0
513  
514 # XXX - return a hash in list context?
515  
516 0 return $rsrc->first_object('Version');
517 }
518  
519 0 ▶sub get_version_info {
Subroutine get_version_info: not called CC=6 SCAR=37.4
Pod documented
520 0 my $self = shift;
521 0 ▶ my $vinfo = $self->version_info or return;
Branch: 1 branch
BranchTrueFalseTotal
my $vinfo = $self->version_info or return 0 0 0
522 0 my @keys = qw(
523 Comments CompanyName FileDescription FileVersion
524 InternalName LegalCopyright LegalTrademarks OriginalFilename
525 ProductName ProductVersion
526 );
527 0 my $rval = {};
528 0 for my $key (@keys) {
529 0 my $val = $vinfo->get($key);
530 0 ▶ $val =~ s/,/\./g if(defined($val) && ( $key =~ /version/i ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $val and $key =~ /version/i 0 0 0
Condition0%0/3
defined $val and $key =~ /version/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $val and $key =~ /version/i
defined $val $key =~ /version/i
Truth table0%0/3
A and B
A= defined $valB= $key =~ /version/i
ABresult
0X0
100
111
531 0 ▶ $rval->{$key} = $val if defined($val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 0 0
532 }
533 0 return $rval
534 }
535  
536 0 ▶sub set_version_info {
Subroutine set_version_info: not called CC=11 SCAR=48.8
Pod documented
537 0 my ($self, $inputpairs) = @_;
538 0 my $inputref;
539 0 ▶ if(ref($inputpairs) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $inputpairs eq 'HASH') { } 0 0 0
540 0 my @newinput = ();
541 0 push(@newinput, qq($_=$inputpairs->{$_})) for (sort keys(%$inputpairs));
542 0 $inputref = \@newinput;
543 } else {
544 0 $inputref = $inputpairs;
545 }
546 0 my @info = ($self->default_info, @$inputref);
547 0 my @pairs;
548 0 foreach my $pairs (map split(/\s*;\s*(?=[\w\\\/]+\s*=)/, $_), @info) {
549 0 my ($key, $val) = split(/\s*=\s*/, $pairs, 2);
550 0 ▶ next if $key =~ /language/i;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /language/i 0 0 0
551  
552 0 ▶ if ($key =~ /^(product|file)version$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(product|file)version$/i) { } 0 0 0
553 0 $key = "\u$1Version";
554 0 ▶ $val =~ /^(?:\d+\.)+\d+$/ or die "$key cannot be '$val'";
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /^(?:\d+\.)+\d+$/ or die "$key cannot be '${val}'" 0 0 0
555 0 ▶ $val .= '.0' while $val =~ y/.,// < 3;
Branch: 1 branch
BranchTrueFalseTotal
$val =~ tr/,.// < 3 and do { $val .= '.0' } 0 0 0
556  
557 0 push(@pairs,
558 [ $key => $val ],
559 [ "/StringFileInfo/#1/$key", $val ]);
560 } else {
561 0 push(@pairs, [ $key => $val ]);
562 }
563 }
564 0 ▶ my $rsrc = $self->resource_section or return;
Branch: 1 branch
BranchTrueFalseTotal
my $rsrc = $self->resource_section or return 0 0 0
565 0 ▶ my $version = $rsrc->first_object('Version') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $version = $rsrc->first_object("Version") or return 0 0 0
566 0 $version->set(@$_) for @pairs;
567 0 $version->refresh;
568 }
569  
570 4 ▶sub manifest {
Subroutine manifest: called CC=3 SCAR=12.0
Pod undocumented
571 4 my ($self) = @_;
572 4 ▶ my $rsrc = $self->resource_section or return;
Branch: 1 branch
BranchTrueFalseTotal
my $rsrc = $self->resource_section or return 4 0 4
573 4 ▶ if( my $obj = $rsrc->first_object('Manifest') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $rsrc->first_object('Manifest')) { } 4 0 4
574 4 return $obj;
575 } else {
576 0 return $self->require_class('Resource::Manifest')->new;
577 }
578 }
579  
580 2 ▶sub has_manifest {
Subroutine has_manifest: called CC=3 SCAR=12.0
Pod undocumented
581 2 my ($self) = @_;
582 2 ▶ my $rsrc = $self->resource_section or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $rsrc = $self->resource_section or return 0 2 0 2
583 2 ▶ if( my $obj = $rsrc->first_object('Manifest') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $rsrc->first_object('Manifest')) { } 2 0 2
584 2 return 1;
585 } else {
586 0 return 0;
587 }
588 }
589  
590 1 ▶sub set_manifest {
Subroutine set_manifest: called CC=7 SCAR=22.3
Pod documented
591 1 my ($self, $input) = @_;
592 # support code that passes xml, filepaths and objects
593 1 my $resid = 0;
594 1 my $xml;
595 1 ▶ if(ref($input) && $input->isa('Win32::Exe::Manifest')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input and $input->isa('Win32::Exe::Manifest')) { } 0 1 1
Condition33%1/3
ref $input and $input->isa("Win32::Exe::Manifest")
!ll&&!rl&&r
100
MC/DC0%0/2
ref $input and $input->isa("Win32::Exe::Manifest")
ref $input $input->isa("Win32::Exe::Manifest")
Truth table33%1/3
A and B
A= ref $inputB= $input->isa("Win32::Exe::Manifest")
ABresult
0X0
100
111
596 0 $resid = $input->get_resource_id;
597 0 $xml = $input->output ;
598 } else {
599 1 my $filecontent;
600 1 eval {
601 1 my $paramisfile = 0;
602 {
603 5 ▶ no warnings qw( io );
Subroutine BEGIN: called CC=1 SCAR=0.0
604 1 $paramisfile = (-f $input);
605 }
606 1 ▶ if($paramisfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paramisfile) { } 0 1 1
607 0 open my $fh, '<', $input;
608 0 $filecontent = do { local $/; <$fh> };
609 0 my $errors = $@;
610 0 close($fh);
611 0 ▶ die $errors if $errors;
Branch: 1 branch
BranchTrueFalseTotal
if $errors 0 0 0
612 } else {
613 1 $filecontent = $input;
614 }
615 };
616 1 ▶ $xml = ( $@ ) ? $input : $filecontent;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 1 1
617 }
618 1 ▶ $resid ||= 1;
Condition50%1/2
$resid ||= 1
l!l
01
MC/DC0%0/1
$resid ||= 1
$resid
Truth table50%1/2
A
A= $resid
Aresult
11
00
619 1 my $rsrc = $self->resource_section;
620 1 my $name = '/#RT_MANIFEST/#' . $resid . '/#0';
621 1 $rsrc->remove("/#RT_MANIFEST");
622 1 my $manifest = $self->require_class('Resource::Manifest')->new;
623 1 $manifest->SetPathName( $name );
624 1 $manifest->set_parent( $rsrc );
625 1 $manifest->update_manifest( $xml );
626 1 $rsrc->insert($manifest->PathName, $manifest);
627 1 $rsrc->refresh;
628 }
629  
630 1 ▶sub set_manifest_args {
Subroutine set_manifest_args: called CC=10 SCAR=36.5
Pod documented
631 1 my ($self, $inputpairs) = @_;
632 1 my $inputref;
633 1 ▶ if(ref($inputpairs eq 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($inputpairs eq 'HASH')) { } 0 1 1
634 0 my @newinput = ();
635 0 push(@newinput, qq($_ = $inputpairs->{$_})) for (sort keys(%$inputpairs));
636 0 $inputref = \@newinput;
637 } else {
638 1 $inputref = $inputpairs;
639 }
640 1 my @manifestargs = @$inputpairs;
641 1 my %arghash;
642 1 foreach my $pairs (map split(/\s*;\s*(?=[\w\\\/]+\s*=)/, $_), @manifestargs) {
643 5 my ($key, $val) = split(/\s*=\s*/, $pairs, 2);
644 5 my $addkey = lc($key);
645 5 $arghash{$addkey} = $val;
646 }
647 1 my $manifest = $self->get_manifest;
648 0 ▶ $manifest->set_execution_level($arghash{executionlevel}) if exists($arghash{executionlevel});
Branch: 1 branch
BranchTrueFalseTotal
if exists $arghash{"executionlevel"} 0 0 0
649 0 ▶ $manifest->set_uiaccess($arghash{uiaccess}) if exists($arghash{uiaccess});
Branch: 1 branch
BranchTrueFalseTotal
if exists $arghash{"uiaccess"} 0 0 0
650 0 ▶ $manifest->set_assembly_name($arghash{execname}) if exists($arghash{execname});
Branch: 1 branch
BranchTrueFalseTotal
if exists $arghash{"execname"} 0 0 0
651 0 ▶ $manifest->set_assembly_description($arghash{description}) if exists($arghash{description});
Branch: 1 branch
BranchTrueFalseTotal
if exists $arghash{"description"} 0 0 0
652 0 ▶ $manifest->set_assembly_version($arghash{version}) if exists($arghash{version});
Branch: 1 branch
BranchTrueFalseTotal
if exists $arghash{"version"} 0 0 0
653 0 ▶ $manifest->add_common_controls() if $arghash{commoncontrols};
Branch: 1 branch
BranchTrueFalseTotal
if $arghash{"commoncontrols"} 0 0 0
654
655 0 $self->set_manifest($manifest);
656 }
657  
658 3 ▶sub get_manifest {
Subroutine get_manifest: called CC=2 SCAR=7.9
Pod documented
659 3 my ($self) = @_;
660 3 ▶ my $mtype = ($self->is_assembly) ? 'assembly' : 'application';
Branch: 1 branch
BranchTrueFalseTotal
$self->is_assembly ? : 0 3 3
661 3 my $mfestxml = $self->manifest->get_manifest;
662 3 my $mfest = Win32::Exe::Manifest->new($mfestxml, $mtype);
663 0 $mfest->set_resource_id( $self->manifest->get_manifest_id );
664 0 return $mfest;
665 }
666  
667 0 ▶sub add_default_manifest {
Subroutine add_default_manifest: not called CC=1 SCAR=6.9
Pod undocumented
668 0 my ($self) = @_;
669 0 my $rsrc = $self->resource_section;
670 0 my $name = '/#RT_MANIFEST/#1/#0';
671 0 $rsrc->remove("/#RT_MANIFEST");
672 0 my $manifest = $self->require_class('Resource::Manifest')->new;
673 0 my $xml = $manifest->default_manifest;
674 0 $manifest->SetPathName( $name );
675 0 $manifest->set_parent( $rsrc );
676 0 $manifest->update_manifest( $xml );
677 0 $rsrc->insert($manifest->PathName, $manifest);
678 0 $rsrc->refresh;
679 }
680  
681 0 ▶sub merge_manifest {
Subroutine merge_manifest: not called CC=3 SCAR=24.8
Pod undocumented
682 0 my ($self, $mnf) = @_;
683 0 ▶ return if !(ref($mnf) && $mnf->isa('Win32::Exe::Manifest'));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $mnf and $mnf->isa("Win32::Exe::Manifest") 0 0 0
Condition0%0/3
ref $mnf and $mnf->isa("Win32::Exe::Manifest")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $mnf and $mnf->isa("Win32::Exe::Manifest")
ref $mnf $mnf->isa("Win32::Exe::Manifest")
Truth table0%0/3
A and B
A= ref $mnfB= $mnf->isa("Win32::Exe::Manifest")
ABresult
0X0
100
111
684 0 my $main = $self->get_manifest;
685 0 $main->merge_manifest($mnf);
686 0 $self->set_manifest($main);
687 }
688  
689 0 ▶sub set_subsystem_windows {
Subroutine set_subsystem_windows: not called CC=2 SCAR=17.9
Pod documented
690 0 my $self = shift;
691 0 ▶ return if !$self->is_application;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_application 0 0 0
692 0 $self->SetSubsystem("windows")
693 }
694  
695 0 ▶sub set_subsystem_console {
Subroutine set_subsystem_console: not called CC=2 SCAR=17.9
Pod documented
696 0 my $self = shift;
697 0 ▶ return if !$self->is_application;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_application 0 0 0
698 0 $self->SetSubsystem("console")
699 }
700  
701 0 ▶sub get_subsystem { $_[0]->Subsystem; }
Subroutine get_subsystem: not called CC=1 SCAR=6.9
Pod documented
702  
703 0 ▶sub get_group_icon_names {
Subroutine get_group_icon_names: not called CC=3 SCAR=24.8
Pod documented
704 0 my $self = shift;
705 0 my @names = ();
706 0 ▶ my $section = $self->resource_section or return @names;
Branch: 1 branch
BranchTrueFalseTotal
my $section = $self->resource_section or return @names 0 0 0
707 0 for my $resource ( $section->objects('GroupIcon')) {
708 0 my $path = $resource->PathName;
709 0 my($_null, $_rtgi, $name, $_langid) = split(/\//, $path);
710 0 push(@names, $name);# if $resource->isa('Win32::Exe::Resource::GroupIcon');
711 }
712 0 return @names;
713 }
714  
715 0 ▶sub set_single_group_icon {
Subroutine set_single_group_icon: not called CC=2 SCAR=17.9
Pod documented
716 0 my($self, $iconfile) = @_;
717 0 my @icons = Win32::Exe::IconFile->new($iconfile)->icons;
718 0 ▶ $self->set_icons(\@icons) if @icons;
Branch: 1 branch
BranchTrueFalseTotal
if @icons 0 0 0
719 }
720  
721 0 ▶sub get_group_icon {
Subroutine get_group_icon: not called CC=3 SCAR=24.8
Pod documented
722 0 my ($self, $getname) = @_;
723 0 ▶ return undef if !$getname;
Branch: 1 branch
BranchTrueFalseTotal
unless $getname 0 0 0
724 0 my $res = undef;
725 0 ▶ my $section = $self->resource_section or return $res;
Branch: 1 branch
BranchTrueFalseTotal
my $section = $self->resource_section or return $res 0 0 0
726 0 $res = $self->_exists_group_icon($getname);
727 0 return $res;
728 }
729  
730 0 ▶sub add_group_icon {
Subroutine add_group_icon: not called CC=4 SCAR=30.0
Pod documented
731 0 my ($self, $newname, $filename) = @_;
732 0 my $exists = 0;
733 0 ▶ my $section = $self->resource_section or return;
Branch: 1 branch
BranchTrueFalseTotal
my $section = $self->resource_section or return 0 0 0
734 0 my ($res, $langid) = $self->_exists_group_icon($newname);
735 0 ▶ return undef if $res; # it already exists
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 0 0
736
737 0 my @icons = Win32::Exe::IconFile->new($filename)->icons;
738 0 ▶ return if !(scalar @icons);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @icons 0 0 0
739
740 0 my $group = $self->require_class('Resource::GroupIcon')->new;
741 0 my $pathname = '/#RT_GROUP_ICON/' . $newname . '/' . $langid;
742 0 $group->SetPathName($pathname);
743 0 $group->set_parent($section);
744 0 $section->insert($group->PathName, $group);
745 0 $group->set_icons(\@icons);
746 0 $group->refresh;
747 }
748  
749 0 ▶sub replace_group_icon {
Subroutine replace_group_icon: not called CC=4 SCAR=30.0
Pod documented
750 0 my ($self, $getname, $filename) = @_;
751 0 ▶ my $section = $self->resource_section or return;
Branch: 1 branch
BranchTrueFalseTotal
my $section = $self->resource_section or return 0 0 0
752 0 my @icons = Win32::Exe::IconFile->new($filename)->icons;
753 0 ▶ return if !(scalar @icons);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @icons 0 0 0
754 0 ▶ my $group = $self->get_group_icon($getname) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $group = $self->get_group_icon($getname) or return 0 0 0
755 0 my $pathname = $group->PathName;
756 0 $section->remove($pathname);
757 0 my $newgroup = $self->require_class('Resource::GroupIcon')->new;
758 0 $newgroup->SetPathName($pathname);
759 0 $newgroup->set_parent($section);
760 0 $section->insert($newgroup->PathName, $newgroup);
761 0 $newgroup->set_icons(\@icons);
762 0 $newgroup->refresh;
763 }
764  
765 0 ▶sub remove_group_icon {
Subroutine remove_group_icon: not called CC=3 SCAR=24.8
Pod documented
766 0 my ($self, $matchname) = @_;
767 0 ▶ my $section = $self->resource_section or return;
Branch: 1 branch
BranchTrueFalseTotal
my $section = $self->resource_section or return 0 0 0
768 0 ▶ my $existing = $self->get_group_icon($matchname) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $existing = $self->get_group_icon($matchname) or return 0 0 0
769 0 $section->remove($existing->PathName);
770 0 $section->refresh;
771 }
772  
773 0 ▶sub _exists_group_icon {
Subroutine _exists_group_icon: not called CC=5 SCAR=34.0
774 0 my ($self, $matchname) = @_;
775 0 ▶ my $section = $self->resource_section or return;
Branch: 1 branch
BranchTrueFalseTotal
my $section = $self->resource_section or return 0 0 0
776 0 my $langid = '#0';
777 0 my $res = undef;
778 0 for my $resource ( $section->objects('GroupIcon')) {
779 0 my $path = $resource->PathName;
780 0 my($_null, $_rtgi, $name, $_langid) = split(/\//, $path);
781 0 $langid = $_langid;
782 0 ▶ if($name eq $matchname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq $matchname) 0 0 0
783 0 $res = $resource;
784 0 last;
785 }
786 }
787 0 ▶ return ( wantarray ) ? ( $res, $langid ) : $res;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
788 }
789  
790 0 ▶sub export_group_icon {
Subroutine export_group_icon: not called CC=2 SCAR=17.9
Pod documented
791 0 my ($self, $matchname, $filename) = @_;
792 0 ▶ my $existing = $self->get_group_icon($matchname) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $existing = $self->get_group_icon($matchname) or return 0 0 0
793 0 my $iconobject = Win32::Exe::IconFile->new();
794 0 my @icons = $existing->icons;
795 0 $iconobject->set_icons(\@icons);
796 0 $iconobject->write_file($filename, $iconobject->dump);
797 }
798  
799  
800 5 1;
801  
802 __END__
« lib/Win32/Exe/Resource/Version.pm ↑ Summary lib/Win32/Exe/InsertResourceSection.pm »