/[soft]/mgatools/trunk/pm/MGATools/iso.pm
ViewVC logotype

Diff of /mgatools/trunk/pm/MGATools/iso.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 6051 by kharec, Tue Sep 25 09:53:36 2012 UTC revision 6053 by kharec, Tue Sep 25 12:18:24 2012 UTC
# Line 5  use Digest::MD5; Line 5  use Digest::MD5;
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    
# Line 52  md5 code highly inspired from Redhat ana Line 52  md5 code highly inspired from Redhat ana
52    
53  =cut  =cut
54    
55    
56  # function copied from Mkcd::Tools  # function copied from Mkcd::Tools
57  # TODO must add some check of maximum authorized size  # TODO must add some check of maximum authorized size
58  sub include_md5 {  sub include_md5 {
59      my ( $iso, $write, $verbose ) = @_;      my ($iso, $write, $verbose) = @_;
60      my $ISO;      my $ISO;
61      if ($write) {      if ($write) {
62          open $ISO, "+<$iso"          open $ISO, "+<$iso" or return "ERROR include_md5: unable to open $iso ($!)\n";  
63            or return "ERROR include_md5: unable to open $iso ($!)\n";      } else {
64      }          open $ISO, $iso or return "ERROR include_md5: unable to open $iso ($!)\n";      
     else {  
         open $ISO, $iso  
           or return "ERROR include_md5: unable to open $iso ($!)\n";  
65      }      }
66      binmode $ISO;      binmode $ISO;
67      my $offset = 16 * 2048;      my $offset = 16*2048;
   
68      # blank header      # blank header
69      seek $ISO, $offset, 0;      seek $ISO, $offset, 0;
70      my ( $buf, $msg );      my ($buf, $msg);
71      while (1) {      while (1) {
72          read $ISO, $buf, 2048;          read $ISO,$buf,2048;
73          my $c = ord $buf;          my $c = ord $buf;
74          last if $c == 1;          last if $c == 1;
75          return "ERROR include_md5: could not find primary volume descriptor\n"          return "ERROR include_md5: could not find primary volume descriptor\n" if $c == 255;
76            if $c == 255;          $offset += 2048
         $offset += 2048;  
77      }      }
78      my $size =      my $size = ((ord substr $buf, $SIZE_OFFSET, 1) * 0x1000000 +
79        ( ( ord substr $buf, $SIZE_OFFSET, 1 ) * 0x1000000 +                  (ord substr $buf, $SIZE_OFFSET + 1, 1) * 0x10000 +
80            ( ord substr $buf, $SIZE_OFFSET + 1, 1 ) * 0x10000 +                  (ord substr $buf, $SIZE_OFFSET + 2, 1) * 0x100 +
81            ( ord substr $buf, $SIZE_OFFSET + 2, 1 ) * 0x100 +                  (ord substr $buf, $SIZE_OFFSET + 3, 1)) * 2048;
82            ( ord substr $buf, $SIZE_OFFSET + 3, 1 ) ) * 2048;      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      my ( $system, $volume, $publisher, $prep, $app ) =      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;
       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 )  
       );  
     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;      seek $ISO, $offset + $INFO_OFFSET, 0;
85      read $ISO, $buf, 512;      read $ISO, $buf,512;
86      my ($md5sum) = $buf =~ /.md5 = (\S+)/;      my ($md5sum) = $buf =~ /.md5 = (\S+)/;
87      $msg .= "include_md5: previous data $buf\n";      $msg .= "include_md5: previous data $buf\n";
88      seek $ISO, 0, 0;      seek $ISO, 0, 0;
# Line 105  sub include_md5 { Line 91  sub include_md5 {
91      $md5->add($buf);      $md5->add($buf);
92      seek $ISO, 512, 1;      seek $ISO, 512, 1;
93      $read += 512;      $read += 512;
94      $| = 1;      $|=1;
95      my $val = int $size / 2048 / 100;      my $val = int $size/2048/100;
96      $verbose and print "\rReading: 0 %";      $verbose and print "\rReading: 0 %";
97      my ( $i, $j );      my ($i, $j);
98        # skip last $SKIP bytes that sometimes are not correctly burned by some drives
   # skip last $SKIP bytes that sometimes are not correctly burned by some drives  
99      my $n = 1;      my $n = 1;
100      while ( $n && $read < $size - $SKIP * 2048 ) {      while ($n && $read < $size - $SKIP * 2048) {
101          $n = read $ISO, $buf, 2048;          $n = read $ISO, $buf,2048;
102          print "\rReading: ", $j++, " %" if ( $verbose && !( $i++ % $val ) );          print "\rReading: ", $j++, " %" if ($verbose && !($i++ % $val));
103          $md5->add($buf);          $md5->add($buf);
104          $read += $n;          $read += $n;
105      }      }
106      print "\n";      print "\n";
107      my $digest = $md5->hexdigest;      my $digest = $md5->hexdigest;
108      $msg .= "include_md5: computed md5 $digest\n";      $msg .= "include_md5: computed md5 $digest\n";
109      my $res = $md5sum eq $digest;      my $res = $md5sum eq $digest;
110      if ($md5sum) {      if ($md5sum) {
111          $msg .= "include_md5: previous md5 $md5sum\ninclude_md5: md5sum check ";          $msg .= "include_md5: previous md5 $md5sum\ninclude_md5: md5sum check ";
112          $msg .= $res ? "OK\n" : "FAILED\n";          $msg .= $res ? "OK\n" : "FAILED\n"
113      }      }
114      print $msg if $verbose;      print $msg if $verbose;
115      $write or return $res;      $write or return $res;
116      seek $ISO, $offset + $INFO_OFFSET, 0;      seek $ISO, $offset + $INFO_OFFSET, 0;
117      my $str = substr "$volume.md5 = $digest", 0, 512;      my $str = substr "$volume.md5 = $digest", 0, 512;
118      my $l = length $str;      my $l = length $str;
119      print $ISO ( $l > 512 ? substr $str, -1, 512 : $str . ' ' x ( 512 - $l ) );      print $ISO ($l > 512 ? substr $str, -1, 512 : $str . ' ' x (512 - $l));
120      close $ISO;      close $ISO
121  }  }
122    
123  1  1

Legend:
Removed from v.6051  
changed lines
  Added in v.6053

  ViewVC Help
Powered by ViewVC 1.1.30