forked from reticulatedpines/magiclantern_hg_02
-
Notifications
You must be signed in to change notification settings - Fork 6
/
patch-bin
executable file
·54 lines (42 loc) · 872 Bytes
/
patch-bin
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
#!/usr/bin/perl
# Apply a patch to a binary image.
use warnings;
use strict;
use File::Slurp;
my $fail = 0;
my $pass = 0;
my $file = shift;
my $image = read_file( $file, {binmode => ':raw'} )
or die "$0: Unable to read $file: $!\n";
while(<>)
{
s/[\r\n]//g;
next if /^Comparing files/;
my ($offset,$from,$to) = m/
^
([0-9A-Fa-f]+):
\s+
([0-9A-Fa-f]+)
\s+
([0-9A-Fa-f]+)
$/x or warn "Unable to parse: '$_'\n" and next;
$offset = hex $offset;
$from = chr( hex $from );
$to = chr( hex $to );
if( substr( $image, $offset, 1) eq $from )
{
substr( $image, $offset, 1 ) = $to;
$pass++;
next;
}
printf STDERR "%08x: %02x != %02x!\n",
$offset,
ord( $from ),
ord( substr( $image, $offset, 1 ) ),
;
$fail++;
}
die "$0: $fail errors reported\n" if $fail;
warn "$0: $file: patched $pass bytes\n";
write_file( $file, $image );
__END__