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 |
# function copied from Mkcd::Tools |
56 |
# TODO must add some check of maximum authorized size |
57 |
sub include_md5 { |
58 |
my ( $iso, $write, $verbose ) = @_; |
59 |
my $ISO; |
60 |
if ($write) { |
61 |
open $ISO, "+<$iso" |
62 |
or return "ERROR include_md5: unable to open $iso ($!)\n"; |
63 |
} |
64 |
else { |
65 |
open $ISO, $iso |
66 |
or return "ERROR include_md5: unable to open $iso ($!)\n"; |
67 |
} |
68 |
binmode $ISO; |
69 |
my $offset = 16 * 2048; |
70 |
|
71 |
# blank header |
72 |
seek $ISO, $offset, 0; |
73 |
my ( $buf, $msg ); |
74 |
while (1) { |
75 |
read $ISO, $buf, 2048; |
76 |
my $c = ord $buf; |
77 |
last if $c == 1; |
78 |
return "ERROR include_md5: could not find primary volume descriptor\n" |
79 |
if $c == 255; |
80 |
$offset += 2048; |
81 |
} |
82 |
my $size = |
83 |
( ( ord substr $buf, $SIZE_OFFSET, 1 ) * 0x1000000 + |
84 |
( ord substr $buf, $SIZE_OFFSET + 1, 1 ) * 0x10000 + |
85 |
( ord substr $buf, $SIZE_OFFSET + 2, 1 ) * 0x100 + |
86 |
( ord substr $buf, $SIZE_OFFSET + 3, 1 ) ) * 2048; |
87 |
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; |
99 |
read $ISO, $buf, 512; |
100 |
my ($md5sum) = $buf =~ /.md5 = (\S+)/; |
101 |
$msg .= "include_md5: previous data $buf\n"; |
102 |
seek $ISO, 0, 0; |
103 |
my $md5 = new Digest::MD5; |
104 |
my $read = read $ISO, $buf, $offset + $INFO_OFFSET; |
105 |
$md5->add($buf); |
106 |
seek $ISO, 512, 1; |
107 |
$read += 512; |
108 |
$| = 1; |
109 |
my $val = int $size / 2048 / 100; |
110 |
$verbose and print "\rReading: 0 %"; |
111 |
my ( $i, $j ); |
112 |
|
113 |
# skip last $SKIP bytes that sometimes are not correctly burned by some drives |
114 |
my $n = 1; |
115 |
while ( $n && $read < $size - $SKIP * 2048 ) { |
116 |
$n = read $ISO, $buf, 2048; |
117 |
print "\rReading: ", $j++, " %" if ( $verbose && !( $i++ % $val ) ); |
118 |
$md5->add($buf); |
119 |
$read += $n; |
120 |
} |
121 |
print "\n"; |
122 |
my $digest = $md5->hexdigest; |
123 |
$msg .= "include_md5: computed md5 $digest\n"; |
124 |
my $res = $md5sum eq $digest; |
125 |
if ($md5sum) { |
126 |
$msg .= "include_md5: previous md5 $md5sum\ninclude_md5: md5sum check "; |
127 |
$msg .= $res ? "OK\n" : "FAILED\n"; |
128 |
} |
129 |
print $msg if $verbose; |
130 |
$write or return $res; |
131 |
seek $ISO, $offset + $INFO_OFFSET, 0; |
132 |
my $str = substr "$volume.md5 = $digest", 0, 512; |
133 |
my $l = length $str; |
134 |
print $ISO ( $l > 512 ? substr $str, -1, 512 : $str . ' ' x ( 512 - $l ) ); |
135 |
close $ISO; |
136 |
} |
137 |
|
138 |
1 |
139 |
|