5 |
|
|
6 |
require Exporter; |
require Exporter; |
7 |
use URPM; |
use URPM; |
8 |
our @ISA = qw(Exporter);; |
our @ISA = qw(Exporter); |
9 |
our @EXPORT = qw(include_md5); |
our @EXPORT = qw(include_md5); |
10 |
our ($INFO_OFFSET, $SIZE_OFFSET, $SKIP); |
our ( $INFO_OFFSET, $SIZE_OFFSET, $SKIP ); |
11 |
$INFO_OFFSET = 883; |
$INFO_OFFSET = 883; |
12 |
$SIZE_OFFSET = 84; |
$SIZE_OFFSET = 84; |
13 |
$SKIP = 15; |
$SKIP = 15; |
14 |
|
|
15 |
=head1 NAME |
=head1 NAME |
16 |
|
|
52 |
|
|
53 |
=cut |
=cut |
54 |
|
|
|
|
|
55 |
# function copied from Mkcd::Tools |
# function copied from Mkcd::Tools |
56 |
# TODO must add some check of maximum authorized size |
# TODO must add some check of maximum authorized size |
57 |
sub include_md5 { |
sub include_md5 { |
58 |
my ($iso, $write, $verbose) = @_; |
my ( $iso, $write, $verbose ) = @_; |
59 |
my $ISO; |
my $ISO; |
60 |
if ($write) { |
if ($write) { |
61 |
open $ISO, "+<$iso" or return "ERROR include_md5: unable to open $iso ($!)\n"; |
open $ISO, "+<$iso" |
62 |
} else { |
or return "ERROR include_md5: unable to open $iso ($!)\n"; |
63 |
open $ISO, $iso or return "ERROR include_md5: unable to open $iso ($!)\n"; |
} |
64 |
|
else { |
65 |
|
open $ISO, $iso |
66 |
|
or return "ERROR include_md5: unable to open $iso ($!)\n"; |
67 |
} |
} |
68 |
binmode $ISO; |
binmode $ISO; |
69 |
my $offset = 16*2048; |
my $offset = 16 * 2048; |
70 |
|
|
71 |
# blank header |
# blank header |
72 |
seek $ISO, $offset, 0; |
seek $ISO, $offset, 0; |
73 |
my ($buf, $msg); |
my ( $buf, $msg ); |
74 |
while (1) { |
while (1) { |
75 |
read $ISO,$buf,2048; |
read $ISO, $buf, 2048; |
76 |
my $c = ord $buf; |
my $c = ord $buf; |
77 |
last if $c == 1; |
last if $c == 1; |
78 |
return "ERROR include_md5: could not find primary volume descriptor\n" if $c == 255; |
return "ERROR include_md5: could not find primary volume descriptor\n" |
79 |
$offset += 2048 |
if $c == 255; |
80 |
|
$offset += 2048; |
81 |
} |
} |
82 |
my $size = ((ord substr $buf, $SIZE_OFFSET, 1) * 0x1000000 + |
my $size = |
83 |
(ord substr $buf, $SIZE_OFFSET + 1, 1) * 0x10000 + |
( ( ord substr $buf, $SIZE_OFFSET, 1 ) * 0x1000000 + |
84 |
(ord substr $buf, $SIZE_OFFSET + 2, 1) * 0x100 + |
( ord substr $buf, $SIZE_OFFSET + 1, 1 ) * 0x10000 + |
85 |
(ord substr $buf, $SIZE_OFFSET + 3, 1)) * 2048; |
( ord substr $buf, $SIZE_OFFSET + 2, 1 ) * 0x100 + |
86 |
my ($system, $volume, $publisher, $prep, $app) = map { $a = $_ ; $a =~ s/^\s*//; $a =~ s/\s*$//; $a } (substr($buf, 8, 22), substr($buf, 30, 40), substr($buf, 318, 128), substr($buf, 446, 32), substr($buf, 574, 128)); |
( ord substr $buf, $SIZE_OFFSET + 3, 1 ) ) * 2048; |
87 |
print "include_md5:\nSystem: \t$system\nVolume: \t$volume\nPublisher: \t$publisher\nData preparer: \t$prep\nApplication: \t$app\nISO size: \t$size\n" if $verbose; |
my ( $system, $volume, $publisher, $prep, $app ) = |
88 |
|
map { $a = $_; $a =~ s/^\s*//; $a =~ s/\s*$//; $a } ( |
89 |
|
substr( $buf, 8, 22 ), |
90 |
|
substr( $buf, 30, 40 ), |
91 |
|
substr( $buf, 318, 128 ), |
92 |
|
substr( $buf, 446, 32 ), |
93 |
|
substr( $buf, 574, 128 ) |
94 |
|
); |
95 |
|
print |
96 |
|
"include_md5:\nSystem: \t$system\nVolume: \t$volume\nPublisher: \t$publisher\nData preparer: \t$prep\nApplication: \t$app\nISO size: \t$size\n" |
97 |
|
if $verbose; |
98 |
seek $ISO, $offset + $INFO_OFFSET, 0; |
seek $ISO, $offset + $INFO_OFFSET, 0; |
99 |
read $ISO, $buf,512; |
read $ISO, $buf, 512; |
100 |
my ($md5sum) = $buf =~ /.md5 = (\S+)/; |
my ($md5sum) = $buf =~ /.md5 = (\S+)/; |
101 |
$msg .= "include_md5: previous data $buf\n"; |
$msg .= "include_md5: previous data $buf\n"; |
102 |
seek $ISO, 0, 0; |
seek $ISO, 0, 0; |
105 |
$md5->add($buf); |
$md5->add($buf); |
106 |
seek $ISO, 512, 1; |
seek $ISO, 512, 1; |
107 |
$read += 512; |
$read += 512; |
108 |
$|=1; |
$| = 1; |
109 |
my $val = int $size/2048/100; |
my $val = int $size / 2048 / 100; |
110 |
$verbose and print "\rReading: 0 %"; |
$verbose and print "\rReading: 0 %"; |
111 |
my ($i, $j); |
my ( $i, $j ); |
112 |
# skip last $SKIP bytes that sometimes are not correctly burned by some drives |
|
113 |
|
# skip last $SKIP bytes that sometimes are not correctly burned by some drives |
114 |
my $n = 1; |
my $n = 1; |
115 |
while ($n && $read < $size - $SKIP * 2048) { |
while ( $n && $read < $size - $SKIP * 2048 ) { |
116 |
$n = read $ISO, $buf,2048; |
$n = read $ISO, $buf, 2048; |
117 |
print "\rReading: ", $j++, " %" if ($verbose && !($i++ % $val)); |
print "\rReading: ", $j++, " %" if ( $verbose && !( $i++ % $val ) ); |
118 |
$md5->add($buf); |
$md5->add($buf); |
119 |
$read += $n; |
$read += $n; |
120 |
} |
} |
121 |
print "\n"; |
print "\n"; |
122 |
my $digest = $md5->hexdigest; |
my $digest = $md5->hexdigest; |
123 |
$msg .= "include_md5: computed md5 $digest\n"; |
$msg .= "include_md5: computed md5 $digest\n"; |
124 |
my $res = $md5sum eq $digest; |
my $res = $md5sum eq $digest; |
125 |
if ($md5sum) { |
if ($md5sum) { |
126 |
$msg .= "include_md5: previous md5 $md5sum\ninclude_md5: md5sum check "; |
$msg .= "include_md5: previous md5 $md5sum\ninclude_md5: md5sum check "; |
127 |
$msg .= $res ? "OK\n" : "FAILED\n" |
$msg .= $res ? "OK\n" : "FAILED\n"; |
128 |
} |
} |
129 |
print $msg if $verbose; |
print $msg if $verbose; |
130 |
$write or return $res; |
$write or return $res; |
131 |
seek $ISO, $offset + $INFO_OFFSET, 0; |
seek $ISO, $offset + $INFO_OFFSET, 0; |
132 |
my $str = substr "$volume.md5 = $digest", 0, 512; |
my $str = substr "$volume.md5 = $digest", 0, 512; |
133 |
my $l = length $str; |
my $l = length $str; |
134 |
print $ISO ($l > 512 ? substr $str, -1, 512 : $str . ' ' x (512 - $l)); |
print $ISO ( $l > 512 ? substr $str, -1, 512 : $str . ' ' x ( 512 - $l ) ); |
135 |
close $ISO |
close $ISO; |
136 |
} |
} |
137 |
|
|
138 |
1 |
1 |