Merge branch 'master' of git.distorted.org.uk:~mdw/publish/public-git/dvdrip
[dvdrip] / checkimg
CommitLineData
364b4bee
MW
1#! /usr/bin/perl
2
3use autodie;
4
5(my $prog = $0) =~ s!^.*/!!;
6
7die "usage: $prog ORIG COPY\n" unless @ARGV == 2;
8my ($orig, $copy) = @ARGV;
9
10open my $ofh, "<", $orig; binmode $ofh;
11open my $cfh, "<", $copy; binmode $cfh;
12
13my $SECSZ = 2048;
14my $ZERO = "\0" x $SECSZ;
15
16my $rc = 0;
17sub bad ($) {
18 my ($msg) = @_;
19 print STDERR "$prog: $msg\n";
20 $rc = 2 unless $rc > 2;
21}
22
23my ($obuf, $cbuf);
24my $i = 0;
25my $report = "";
26my $prev = undef;
27my $st = undef;
28sub update ($) {
29 my ($newst) = @_;
30 if ($newst ne $st) {
31 if (defined $st) {
32 $report .= ", " if $report;
33 if ($st eq 'copy') { $report .= "$prev .. $i"; }
34 elsif ($st eq 'zero') { $report .= "[$prev .. $i]"; }
35 else { $report .= "($st: $prev .. $i)"; }
36 }
37 $st = $newst; $prev = $i;
38 }
39}
40SECTOR: for (;;) {
41 my $on = read $ofh, $obuf, $SECSZ;
42 my $cn = read $cfh, $cbuf, $SECSZ;
43 if ($on < $SECSZ || $cn < $SECSZ) {
44 if ($on == $SECSZ) {
45 if (!$cn) { bad "premature end at sector #$i in `$copy'"; }
46 else { bad "short sector #$i in `$copy'"; }
47 } elsif (!on) {
48 bad "continuation from sector #$i in `$copy'";
49 } elsif ($on == $cn) {
50 bad "final short sector #$i in both files" if $on;
51 if ($cbuf eq $obuf) { $newst = 'copy'; }
52 elsif ($cbuf eq "\0" x $on) { $newst = 'zero'; }
53 else { bad "corrupted short sector #$i in `$copy'"; }
54 } else {
55 bad "short sector #$i in both files"
56 }
57 last SECTOR;
58 }
59
60 my $newst;
61 if ($cbuf eq $obuf) { $newst = 'copy'; }
62 elsif ($cbuf eq $ZERO) { $newst = 'zero'; }
4144e376 63 else { $newst = 'bogus'; }
364b4bee
MW
64 update $newst; $i++;
65}
66update 'end';
67print $report, "\n";
68exit $rc;