Changeset 32

Show
Ignore:
Timestamp:
01/21/01 21:39:10
Author:
miyagawa
Message:

added validation check

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • Mac-Macbinary/trunk/Changes

    r24 r32  
    11Revision history for Perl extension Mac::Macbinary. 
     2 
     3$Id$ 
     4 
     50.03    - Implemented _validate_header() method to check and see if a file 
     6          is really a Macbinary file or not. Entire code is stolen from 
     7          Mac::Conversion. 
     8 
     90.02  Wed Nov 15 01:52:58 JST 2000 
     10        - Removed 05_fail.t, which has caused failure on some platforms. 
     11          Improved document. 
    212 
    3130.01  Wed Oct 25 17:35:45 2000 
    414        - original version; created by h2xs 1.19 
    515 
    6 0.02  Wed Nov 15 01:52:58 JST 2000 
    7         - Removed 05_fail.t, which has caused failure on some platforms. 
    8           Improved document. 
  • Mac-Macbinary/trunk/MANIFEST

    r23 r32  
    99t/03_path.t 
    1010t/04_filename.t 
     11t/05_validate.t 
    1112t/test.mb 
  • Mac-Macbinary/trunk/Macbinary.pm

    r31 r32  
    33use strict; 
    44use vars qw($VERSION $AUTOLOAD); 
    5 $VERSION = '0.02'; 
    6  
    7 use Carp
     5$VERSION = '0.03'; 
     6 
     7use Carp ()
    88 
    99sub new { 
    10     my ($class, $thingy) = @_; 
    11     my $self = bless { }, $class; 
     10    my($class, $thingy, $attr) = @_; 
     11    my $self = bless { 
     12        validate => $attr->{validate}, 
     13    }, $class; 
    1214 
    1315    my $fh = _make_handle($thingy); 
     
    1820sub _parse_handle { 
    1921    my $self = shift; 
    20     my ($fh) = @_; 
     22    my($fh) = @_; 
    2123 
    2224    read $fh, my ($header), 128; 
    23     $self->{header} = Mac::Macbinary::Header->new($header); 
     25    $self->{header} = Mac::Macbinary::Header->new($header, { 
     26        validate => $self->{validate}, 
     27    }); 
    2428    read $fh, $self->{data}, $self->header->dflen; 
    2529    read $fh, $self->{resource}, $self->header->rflen; 
     
    5963 
    6064sub new { 
    61     my ($class, $h) = @_; 
     65    my($class, $h, $attr) = @_; 
    6266    my $self = bless { }, $class; 
     67    if ($attr->{validate}) { 
     68        $self->_validate_header($h) 
     69            or Carp::croak "Macbinary validation failed."; 
     70    } 
    6371    $self->_parse_header($h); 
    6472    return $self; 
    6573} 
    6674 
     75sub _validate_header { 
     76    my $self = shift; 
     77    my($h) = @_; 
     78 
     79    #  stolen from Mac::Conversions 
     80    # 
     81    #  Use a crude heuristic to decide whether or not a file is MacBinary.  The 
     82    #  first byte of any MacBinary file must be zero.  The second has to be 
     83    #  <= 63 according to the MacBinary II standard.  The 122nd and 123rd 
     84    #  each have to be >= 129.  This has about a 1/8000 chance of failing on 
     85    #  random bytes.  This seems to be all that mcvert does.  Unfortunately 
     86    #  we can't also check the checksum because the standard software (Stuffit 
     87    #  Deluxe, etc.) doesn't seem to checksum. 
     88     
     89    my($zero, 
     90       $namelength, 
     91       $filename, 
     92       $type, 
     93       $creator, 
     94       $highflag, 
     95       $dum1, 
     96       $dum2, 
     97       $dum3, 
     98       $datalength, 
     99       $reslength, 
     100       $dum4, 
     101       $dum5, 
     102       $dum6, 
     103       $lowflag, 
     104       $dum7, 
     105       $dum8, 
     106       $version_this, 
     107       $version_needed, 
     108       $crc) = unpack("CCA63a4a4CxNnCxNNNNnCx14NnCCN", $h); 
     109 
     110    return (!$zero && (($namelength - 1)< 63) 
     111            && $version_this >= 129 && $version_needed >= 129); 
     112} 
     113 
    67114sub _parse_header { 
    68115    my $self = shift; 
    69     my ($h) = @_; 
     116    my($h) = @_; 
    70117 
    71118    $self->{name}       = unpack("A*", substr($h, 2, 63)); 
     
    100147  use Mac::Macbinary; 
    101148 
    102   $mb = new Mac::Macbinary(\*FH);     # filehandle 
    103   $mb = new Mac::Macbinary($fh);      # IO::* instance 
    104   $mb = new Mac::Macbinary("/path/to/file"); 
     149  $mb = Mac::Macbinary->new(\*FH);    # filehandle 
     150  $mb = Mac::Macbinary->new($fh);     # IO::* instance 
     151  $mb = Mac::Macbinary->new("/path/to/file"); 
    105152 
    106153  $header = $mb->header;                # Mac::Macbinary::Header instance 
     
    131178 
    132179  open FH, "path/to/file"; 
    133   $mb = new Mac::Macbinary(\*FH); 
    134  
    135   $fh = new FileHandle "path/to/file"
    136   $mb = new Mac::Macbinary($fh); 
    137  
    138   $io = new IO::File "path/to/file"
    139   $mb = new Mac::Macbinary($io); 
    140  
    141   $mb = new Mac::Macbinary "path/to/file"
     180  $mb = Mac::Macbinary->new(\*FH); 
     181 
     182  $fh = FileHandle->new("path/to/file")
     183  $mb = Mac::Macbinary->new($fh); 
     184 
     185  $io = IO::File->new("path/to/file")
     186  $mb = Mac::Macbinary->new($io); 
     187 
     188  $mb = Mac::Macbinary->new("path/to/file")
    142189 
    143190C<new()> throws an exception "Can't read blahblah" if the given 
     
    205252  }  
    206253 
    207  
    208 =head1 TODO 
    209  
    210 should add C<is_macbinary()>, to detect if a file is a Macbinary file 
    211 or not. 
    212  
    213254=head1 COPYRIGHT 
    214255 
     
    227268Paul J. Schinder and Eryq, authors of those ones. 
    228269 
     270Macbinary validation is almost a replication of B<is_macbinary> in 
     271Mac::Conversions. 
     272 
    229273=head1 SEE ALSO 
    230274