3 # Get the appropriate variables to make an NSIS installer file
4 # based on the PE architecture of a specific file
34 open(my $fh, '<', $file)
35 or die "$0: cannot open file: $file: $!\n";
38 exit 0 if ($mz ne 'MZ');
40 exit 0 unless (seek($fh, 0x3c, 0));
41 exit 0 unless (read($fh, my $pe_offset, 1) == 1);
42 $pe_offset = unpack("C", $pe_offset);
44 exit 0 unless (seek($fh, $pe_offset, 0));
46 exit 0 unless ($pe eq "PE\0\0");
48 exit 0 unless (read($fh, my $arch, 2) == 2);
49 $arch = $archnames{unpack("v", $arch)};
51 print "!define ARCH ${arch}\n";
54 exit 0 unless (seek($fh, 14, 1));
55 exit 0 unless (read($fh, my $auxheaderlen, 2) == 2);
56 exit 0 unless (unpack("v", $auxheaderlen) >= 2);
58 exit 0 unless (seek($fh, 2, 1));
59 exit 0 unless (read($fh, my $petype, 2) == 2);
60 $petype = unpack("v", $petype);
61 if ($petype == 0x010b) {
62 # It is a 32-bit PE32 file
63 print "!define BITS 32\n";
64 print "!define GLOBALINSTDIR \$PROGRAMFILES\n";
65 } elsif ($petype == 0x020b) {
66 # It is a 64-bit PE32+ file
67 print "!define BITS 64\n";
68 print "!define GLOBALINSTDIR \$PROGRAMFILES64\n";