lib/Win32/Exe/InsertResourceSection.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 82 CC 9 SCAR 32.1
CC
9
Cov
0%
create_resource_section
56.0
insert_pe_resource_section
12.0
_is_win
2.0
CRAP 24.7
1 #########################################################################################
2 # Package Win32::Exe::InsertResourceSection
3 # Description: Insert Resource Section
4 # Created Sun May 02 17:32:55 2010
5 # SVN Id $Id: InsertResourceSection.pm 2 2010-11-30 16:40:31Z mark.dootson $
6 # Copyright: Copyright (c) 2010 Mark Dootson
7 # Licence: This program is free software; you can redistribute it
8 # and/or modify it under the same terms as Perl itself
9 #########################################################################################
10  
11 package Win32::Exe::InsertResourceSection;
12  
13 #########################################################################################
14  
15 use strict;
16 use warnings;
17 use Exporter;
18 use base qw( Exporter );
19 use Carp;
20 use Win32::Exe;
21  
22 our $VERSION = '0.17';
23 our @EXPORT = qw( insert_pe_resource_section );
24  
25 sub _is_win { ($^O =~ /^mswin/i) }
26  
27 if (_is_win()) {
28 require XSLoader;
29 XSLoader::load('Win32::Exe::InsertResourceSection', $VERSION);
30 }
31  
32 sub create_resource_section {
33 my $filename = shift;
34
35 croak('Invalid filename') if $filename !~ /\.(dll|exe)$/i;
36 croak('Filename not found') if !-f $filename;
37
38 if(!_is_win()) {
39 warn 'Cannot add resource section to PE files on this platform. Requires MSWin';
40 return undef;
41 }
42
43 my $replacecode;
44 if($filename =~ /\.(dll|exe)$/i) {
45 #VFT_APP = 0x1
46 #VFT_DLL = 0x2
47 #VFT_DRV = 0x3
48 #VFT_FONT = 0x4
49 #VFT_VXD = 0x5
50 #VFT_STATIC_LIB = 0x7
51 if(lc($1) eq 'exe') {
52 $replacecode = '01';
53 } elsif(lc($1) eq 'dll') {
54 $replacecode = '02';
55 } else {
56 croak('Invalid filename');
57 }
58 } else {
59 croak('Invalid filename');
60 }
61  
62 my @verdata = qw(
63 400234000000560053005F0056004500
64 5200530049004F004E005F0049004E00
65 46004F0000000000BD04EFFE00000100
66 00000000000000000000000000000000
67 3F0000000000000004000400XX000000
68 000000000000000000000000A0010000
69 010053007400720069006E0067004600
70 69006C00650049006E0066006F000000
71 7C010000010030003000300030003000
72 34004200300000002400020001004300
73 6F006D00700061006E0079004E006100
74 6D00650000000000200000002C000200
75 0100460069006C006500440065007300
76 6300720069007000740069006F006E00
77 00000000200000002400020001004600
78 69006C00650056006500720073006900
79 6F006E00000000002000000024000200
80 010049006E007400650072006E006100
81 6C004E0061006D006500000020000000
82 2800020001004C006500670061006C00
83 43006F00700079007200690067006800
84 74000000200000002C00020001004C00
85 6500670061006C005400720061006400
86 65006D00610072006B00730000000000
87 200000002C00020001004F0072006900
88 670069006E0061006C00460069006C00
89 65006E0061006D006500000020000000
90 240002000100500072006F0064007500
91 630074004E0061006D00650000000000
92 20000000280002000100500072006F00
93 64007500630074005600650072007300
94 69006F006E0000002000000044000000
95 0100560061007200460069006C006500
96 49006E0066006F000000000024000400
97 00005400720061006E0073006C006100
98 740069006F006E00000000000000B004
99 );
100
101 my $verdatahex = join('', @verdata);
102 $verdatahex =~ s/XX/$replacecode/;
103
104 my $verdataraw = pack('H*', $verdatahex);
105 my $verlen = length($verdataraw);
106 _insert_resource_section($filename, $verdataraw, $verlen);
107 }
108  
109 sub insert_pe_resource_section {
110 my $filename = shift;
111 if(create_resource_section($filename)) {
112 # basic version info resource has been created
113 # we now have to replace the language and original
114 # filename / filename
115 my $exe = Win32::Exe->new($filename);
116 return ($exe->update( info => [ "FileVersion=0.0.0.0" ] )) ? $exe : undef;
117 } else {
118 return undef;
119 }
120 }
121  
122 1;