1 |
package MGATools::iso; |
2 |
|
3 |
use strict; |
4 |
use Digest::MD5; |
5 |
|
6 |
require Exporter; |
7 |
use URPM; |
8 |
our @ISA = qw(Exporter); |
9 |
our @EXPORT = qw(include_md5); |
10 |
our ($INFO_OFFSET, $SIZE_OFFSET, $SKIP); |
11 |
$INFO_OFFSET = 883; |
12 |
$SIZE_OFFSET = 84; |
13 |
$SKIP = 15; |
14 |
|
15 |
=head1 NAME |
16 |
|
17 |
Mageia iso tools |
18 |
|
19 |
=head1 SYNOPSYS |
20 |
|
21 |
require MGATools::iso; |
22 |
|
23 |
=head1 DESCRIPTION |
24 |
|
25 |
<MGATools::iso> includes Mageia iso tools. |
26 |
|
27 |
=head1 SEE ALSO |
28 |
|
29 |
mkcd |
30 |
|
31 |
=head1 COPYRIGHT |
32 |
|
33 |
Copyright (C) 2000,2001,2002,2003,2004 Mandriva <warly@mandriva.com> |
34 |
|
35 |
This program is free software; you can redistribute it and/or modify |
36 |
it under the terms of the GNU General Public License as published by |
37 |
the Free Software Foundation; either version 2, or (at your option) |
38 |
any later version. |
39 |
|
40 |
This program is distributed in the hope that it will be useful, |
41 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
42 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
43 |
GNU General Public License for more details. |
44 |
|
45 |
You should have received a copy of the GNU General Public License |
46 |
along with this program; if not, write to the Free Software |
47 |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
48 |
|
49 |
=head1 CREDITS |
50 |
|
51 |
md5 code highly inspired from Redhat anaconda md5 in ISO code |
52 |
|
53 |
=cut |
54 |
|
55 |
|
56 |
# function copied from Mkcd::Tools |
57 |
# TODO must add some check of maximum authorized size |
58 |
sub include_md5 { |
59 |
my ($iso, $write, $verbose) = @_; |
60 |
my $ISO; |
61 |
if ($write) { |
62 |
open $ISO, "+<$iso" or return "ERROR include_md5: unable to open $iso ($!)\n"; |
63 |
} else { |
64 |
open $ISO, $iso or return "ERROR include_md5: unable to open $iso ($!)\n"; |
65 |
} |
66 |
binmode $ISO; |
67 |
my $offset = 16*2048; |
68 |
# blank header |
69 |
seek $ISO, $offset, 0; |
70 |
my ($buf, $msg); |
71 |
while (1) { |
72 |
read $ISO,$buf,2048; |
73 |
my $c = ord $buf; |
74 |
last if $c == 1; |
75 |
return "ERROR include_md5: could not find primary volume descriptor\n" if $c == 255; |
76 |
$offset += 2048 |
77 |
} |
78 |
my $size = ((ord substr $buf, $SIZE_OFFSET, 1) * 0x1000000 + |
79 |
(ord substr $buf, $SIZE_OFFSET + 1, 1) * 0x10000 + |
80 |
(ord substr $buf, $SIZE_OFFSET + 2, 1) * 0x100 + |
81 |
(ord substr $buf, $SIZE_OFFSET + 3, 1)) * 2048; |
82 |
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)); |
83 |
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; |
84 |
seek $ISO, $offset + $INFO_OFFSET, 0; |
85 |
read $ISO, $buf,512; |
86 |
my ($md5sum) = $buf =~ /.md5 = (\S+)/; |
87 |
$msg .= "include_md5: previous data $buf\n"; |
88 |
seek $ISO, 0, 0; |
89 |
my $md5 = new Digest::MD5; |
90 |
my $read = read $ISO, $buf, $offset + $INFO_OFFSET; |
91 |
$md5->add($buf); |
92 |
seek $ISO, 512, 1; |
93 |
$read += 512; |
94 |
$|=1; |
95 |
my $val = int $size/2048/100; |
96 |
$verbose and print "\rReading: 0 %"; |
97 |
my ($i, $j); |
98 |
# skip last $SKIP bytes that sometimes are not correctly burned by some drives |
99 |
my $n = 1; |
100 |
while ($n && $read < $size - $SKIP * 2048) { |
101 |
$n = read $ISO, $buf,2048; |
102 |
print "\rReading: ", $j++, " %" if ($verbose && !($i++ % $val)); |
103 |
$md5->add($buf); |
104 |
$read += $n; |
105 |
} |
106 |
print "\n"; |
107 |
my $digest = $md5->hexdigest; |
108 |
$msg .= "include_md5: computed md5 $digest\n"; |
109 |
my $res = $md5sum eq $digest; |
110 |
if ($md5sum) { |
111 |
$msg .= "include_md5: previous md5 $md5sum\ninclude_md5: md5sum check "; |
112 |
$msg .= $res ? "OK\n" : "FAILED\n" |
113 |
} |
114 |
print $msg if $verbose; |
115 |
$write or return $res; |
116 |
seek $ISO, $offset + $INFO_OFFSET, 0; |
117 |
my $str = substr "$volume.md5 = $digest", 0, 512; |
118 |
my $l = length $str; |
119 |
print $ISO ($l > 512 ? substr $str, -1, 512 : $str . ' ' x (512 - $l)); |
120 |
close $ISO |
121 |
} |
122 |
|
123 |
1 |
124 |
|