Initial Commit
[Projects.git] / pkgbuilds / ips.pl / ips.txt
blobefc9fd84c74e2e1d75599676ded04c63ee2c6dd4
1 #!/usr/bin/perl
3 # ips.pl
4 # version 0.01
6 # This is a quick hack to apply IPS patches. It is distributed under
7 # the terms of the GNU General Public License.
9 if (@ARGV != 2)
11 print STDERR <<EOF;
12 Usage:
14 $0 datafile ipsfile
16 There are no options. Your original datafile is modified.
17 EOF
18 exit;
21 open PAT, "$ARGV[1]" or die "Can't open $ARGV[1]";
22 open DAT, "+<$ARGV[0]" or die "Can't open $ARGV[0]";
24 read PAT, $data, 5;
25 die "Bad magic bytes in $ARGV[1]" if $data ne "PATCH";
26 while(1)
28 read PAT, $data, 3 or die "Read error";
29 if ($data eq "EOF")
31 print STDERR "Done!n";
32 exit;
34 # This is ugly, but unpack doesn't have anything that's
35 # very helpful for THREE-byte numbers.
36 $address = ord(substr($data,0,1))*256*256 +
37 ord(substr($data,1,1))*256 +
38 ord(substr($data,2,1));
39 print STDERR "At address $address, ";
40 seek DAT, $address, SEEK_SET or die "Failed seek";
42 read PAT, $data, 2 or die "Read error";
43 $length = ord(substr($data,0,1))*256 + ord(substr($data,1,1));
44 if ($length)
46 print STDERR "Writing $length bytes, ";
47 read(PAT, $data, $length) == $length or die "Read error";
48 print DAT $data;
50 else # RLE mode
52 read PAT, $data, 2 or die "Read error";
53 $length = ord(substr($data,0,1))*256 + ord(substr($data,1,1));
54 print STDERR "Writing $length bytes of RLE, ";
55 read PAT, $byte, 1 or die "Read error";
56 print DAT ($byte)x$length;
58 print STDERR "donen";