| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Get the appropriate variables to make an NSIS installer file |
| 4 |
# based on the PE architecture of a specific file |
| 5 |
# |
| 6 |
|
| 7 |
use strict; |
| 8 |
use bytes; |
| 9 |
|
| 10 |
my %archnames = ( |
| 11 |
0x01de => 'am33', |
| 12 |
0x8664 => 'x64', |
| 13 |
0x01c0 => 'arm32', |
| 14 |
0x01c4 => 'thumb', |
| 15 |
0xaa64 => 'arm64', |
| 16 |
0x0ebc => 'efi', |
| 17 |
0x014c => 'x86', |
| 18 |
0x0200 => 'ia64', |
| 19 |
0x9041 => 'm32r', |
| 20 |
0x0266 => 'mips16', |
| 21 |
0x0366 => 'mips', |
| 22 |
0x0466 => 'mips16', |
| 23 |
0x01f0 => 'powerpc', |
| 24 |
0x01f1 => 'powerpc', |
| 25 |
0x0166 => 'mips', |
| 26 |
0x01a2 => 'sh3', |
| 27 |
0x01a3 => 'sh3', |
| 28 |
0x01a6 => 'sh4', |
| 29 |
0x01a8 => 'sh5', |
| 30 |
0x01c2 => 'arm32', |
| 31 |
0x0169 => 'wcemipsv2' |
| 32 |
); |
| 33 |
|
| 34 |
my ($file) = @ARGV; |
| 35 |
open(my $fh, '<', $file) |
| 36 |
or die "$0: cannot open file: $file: $!\n"; |
| 37 |
|
| 38 |
read($fh, my $mz, 2); |
| 39 |
exit 1 if ($mz ne 'MZ'); |
| 40 |
|
| 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); |
| 44 |
|
| 45 |
exit 1 unless (seek($fh, $pe_offset, 0)); |
| 46 |
read($fh, my $pe, 4); |
| 47 |
exit 1 unless ($pe eq "PE\0\0"); |
| 48 |
|
| 49 |
exit 1 unless (read($fh, my $arch, 2) == 2); |
| 50 |
$arch = $archnames{unpack("v", $arch)}; |
| 51 |
if (defined($arch)) { |
| 52 |
print "!define ARCH ${arch}\n"; |
| 53 |
} |
| 54 |
|
| 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); |
| 58 |
|
| 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"; |
| 70 |
} else { |
| 71 |
# No idea... |
| 72 |
exit 1; |
| 73 |
} |
| 74 |
|
| 75 |
close($fh); |
| 76 |
exit 0; |