summaryrefslogtreecommitdiff
path: root/nsis/getpearch.pl
blob: e524372b288bddc52aa4753cc4c2a5cabb0e5281 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/perl
#
# Get the appropriate variables to make an NSIS installer file
# based on the PE architecture of a specific file
#

use strict;
use bytes;

my %archnames = (
    0x01de => 'am33',
    0x8664 => 'x64',
    0x01c0 => 'arm32',
    0x01c4 => 'thumb',
    0xaa64 => 'arm64',
    0x0ebc => 'efi',
    0x014c => 'x86',
    0x0200 => 'ia64',
    0x9041 => 'm32r',
    0x0266 => 'mips16',
    0x0366 => 'mips',
    0x0466 => 'mips16',
    0x01f0 => 'powerpc',
    0x01f1 => 'powerpc',
    0x0166 => 'mips',
    0x01a2 => 'sh3',
    0x01a3 => 'sh3',
    0x01a6 => 'sh4',
    0x01a8 => 'sh5',
    0x01c2 => 'arm32',
    0x0169 => 'wcemipsv2'
);

my ($file) = @ARGV;
open(my $fh, '<', $file)
    or die "$0: cannot open file: $file: $!\n";

read($fh, my $mz, 2);
exit 1 if ($mz ne 'MZ');

exit 0 unless (seek($fh, 0x3c, 0));
exit 0 unless (read($fh, my $pe_offset, 4) == 4);
$pe_offset = unpack("V", $pe_offset);

exit 1 unless (seek($fh, $pe_offset, 0));
read($fh, my $pe, 4);
exit 1 unless ($pe eq "PE\0\0");

exit 1 unless (read($fh, my $arch, 2) == 2);
$arch = $archnames{unpack("v", $arch)};
if (defined($arch)) {
    print "!define ARCH ${arch}\n";
}

exit 1 unless (seek($fh, 14, 1));
exit 1 unless (read($fh, my $auxheaderlen, 2) == 2);
exit 1 unless (unpack("v", $auxheaderlen) >= 2);

exit 1 unless (seek($fh, 2, 1));
exit 1 unless (read($fh, my $petype, 2) == 2);
$petype = unpack("v", $petype);
if ($petype == 0x010b) {
    # It is a 32-bit PE32 file
    print "!define BITS 32\n";
    print "!define GLOBALINSTDIR \$PROGRAMFILES\n";
} elsif ($petype == 0x020b) {
    # It is a 64-bit PE32+ file
    print "!define BITS 64\n";
    print "!define GLOBALINSTDIR \$PROGRAMFILES64\n";
} else {
    # No idea...
    exit 1;
}

close($fh);
exit 0;