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