summaryrefslogtreecommitdiff
path: root/core/checksumiso.pl
blob: 9b8c3ee50aa2e6f850649e7fe2c46d19ae3c2711 (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
#!/usr/bin/perl
#
# Construct a checksum for isolinux*.bin, compatible
# with an mkisofs boot-info-table
#

use bytes;
use integer;

($file) = @ARGV;

open(FILE, '+<', $file) or die "$0: cannot open $file: $!\n";
binmode FILE;

@fstat = stat(FILE) or die "$0: stat $file: $!\n";
if (!$fstat[7]) {
    die "$0: $file: cannot query length\n";
}

# Pad file to a multiple of 2048 bytes
$frac = $fstat[7] % 2048;
if ($frac) {
    seek(FILE,$fstat[7],0)
	or die "$0: $file: cannot seek to end\n";
    print FILE "\0" x (2048-$frac);
}

# Checksum the file post header
if ( !seek(FILE,64,0) ) {
    die "$0: $file: cannot seek past header\n";
}

$csum  = 0;
$bytes = 64;
while ( ($n = read(FILE, $dw, 4)) > 0 ) {
    $dw .= "\0\0\0\0";		# Pad to at least 32 bits
    ($v) = unpack("V", $dw);
    $csum  = ($csum + $v) & 0xffffffff;
    $bytes += $n;
}

# Update header
if ( !seek(FILE,16,0) ) {
    die "$0: $file: cannot seek to header\n";
}

print FILE pack("VV", $bytes, $csum);

close(FILE);

exit 0;