[Bps-public-commit] r14221 - in Data-Plist: .

alexmv at bestpractical.com alexmv at bestpractical.com
Thu Jul 17 12:48:49 EDT 2008


Author: alexmv
Date: Thu Jul 17 12:48:48 2008
New Revision: 14221

Modified:
   Data-Plist/   (props changed)
   Data-Plist/lib/Data/Plist/BinaryReader.pm

Log:
 r34292 at kohr-ah:  chmrr | 2008-07-17 12:48:45 -0400
  * Error wording cleanups
  * perltidy


Modified: Data-Plist/lib/Data/Plist/BinaryReader.pm
==============================================================================
--- Data-Plist/lib/Data/Plist/BinaryReader.pm	(original)
+++ Data-Plist/lib/Data/Plist/BinaryReader.pm	Thu Jul 17 12:48:48 2008
@@ -31,23 +31,23 @@
     my $self = shift;
     my ($size) = @_;
 
-    die "Integer > 8 bytes = $size" if ( $size > 3 );
-
     my ( $buf, $val );
     read( $self->{fh}, $buf, 1 << $size );
-    if ( $size == 0 ) {
+    if ( $size == 0 ) {         # 8 bit
         $val = unpack( "C", $buf );
-    } elsif ( $size == 1 ) {
+    } elsif ( $size == 1 ) {    # 16 bit
         $val = unpack( "n", $buf );
-    } elsif ( $size == 2 ) {
+    } elsif ( $size == 2 ) {    # 32 bit
         $val = unpack( "N", $buf );
-    } elsif ( $size == 3 ) {
+    } elsif ( $size == 3 ) {    # 64 bit
 
         my ( $hw, $lw ) = unpack( "NN", $buf );
         $val = Math::BigInt->new($hw)->blsft(32)->bior($lw);
         if ( $val->bcmp( Math::BigInt->new(2)->bpow(63) ) > 0 ) {
             $val -= Math::BigInt->new(2)->bpow(64);
         }
+    } else {
+        die "Invalid size for integer ($size)";
     }
 
     return [ "integer", $val ];
@@ -56,18 +56,15 @@
 sub read_real {
     my $self = shift;
     my ($size) = @_;
-    die "Real > 8 bytes" if ( $size > 3 );
 
     my ( $buf, $val );
     read( $self->{fh}, $buf, 1 << $size );
-    if ( $size == 0 ) {    # 1 byte float = error?
-        die "1 byte real found";
-    } elsif ( $size == 1 ) {    # 2 byte float???
-        die "2 byte real found";
-    } elsif ( $size == 2 ) {
+    if ( $size == 2 ) {    # 32 bit
         $val = unpack( "f", reverse $buf );
-    } elsif ( $size == 3 ) {
+    } elsif ( $size == 3 ) {    # 64 bit
         $val = unpack( "d", reverse $buf );
+    } else {
+        die "Invalid size for real ($size)";
     }
 
     return [ "real", $val ];
@@ -76,8 +73,8 @@
 sub read_date {
     my $self = shift;
     my ($size) = @_;
-    die "Date > 8 bytes" if ( $size > 3 );
-    die "Date < 4 bytes" if ( $size < 2 );
+    die "Invalid size for date ($size)"
+        if ( $size > 3 or $size < 2 );
 
     # Dates are just stored as floats
     return [ "date", $self->read_real($size)->[1] ];
@@ -148,7 +145,7 @@
 
     for my $j ( 0 .. $#keys ) {
         my $key = $self->binary_read( $keys[$j] );
-        die "Type isn't string!" unless $key->[0] eq "string";
+        die "Key of hash isn't a string!" unless $key->[0] eq "string";
         $key = $key->[1];
         my $obj = $self->binary_read( $objs[$j] );
         $dict{$key} = $obj;
@@ -171,10 +168,8 @@
     my ($objNum) = @_;
 
     if ( defined $objNum ) {
-        unless ( $objNum < @{ $self->{offsets} } ) {
-            warn "Bad offset: $objNum\n";
-            return;
-        }
+        die "Bad offset: $objNum"
+            unless $objNum < @{ $self->{offsets} };
         seek( $self->{fh}, $self->{offsets}[$objNum], SEEK_SET );
     }
 
@@ -185,9 +180,8 @@
 
     my $size    = unpack( "C*", $buf ) & 0x0F;    # Low nybble is size
     my $objType = unpack( "C*", $buf ) >> 4;      # High nybble is type
-    if ( $objType != 0 and $objType != 8 and $size == 15 ) {
-        $size = $self->binary_read->[1];
-    }
+    $size = $self->binary_read->[1]
+        if $objType != 0 and $size == 15;
 
     my %types = (
         0  => "misc",
@@ -202,7 +196,7 @@
         13 => "dict",
     );
 
-    return [ "??? $objType ???", undef ] unless $types{$objType};
+    die "Unknown type $objType" unless $types{$objType};
     my $method = "read_" . $types{$objType};
     die "Can't $method" unless $self->can($method);
     return $self->$method($size);
@@ -216,11 +210,11 @@
     # with "Out of memory" or "panic: memory wrap"; Do some
     # error-proofing here.
     die "Not a binary plist file\n"
-      unless length $str >= 8 and substr($str, 0, 8) eq "bplist00";
+        unless length $str >= 8 and substr( $str, 0, 8 ) eq "bplist00";
     die "Read of plist trailer failed\n"
-      unless length $str >= 40;
+        unless length $str >= 40;
     die "Invalid top object identifier\n"
-      unless length $str > 40;
+        unless length $str > 40;
 
     return $self->SUPER::open_string($str);
 }
@@ -240,12 +234,12 @@
     }
 
     # get trailer
-    eval {seek( $self->{fh}, -32, SEEK_END )}
-      or die "Read of plist trailer failed\n";
+    eval { seek( $self->{fh}, -32, SEEK_END ) }
+        or die "Read of plist trailer failed\n";
     my $end = tell( $self->{fh} );
 
     die "Read of plist trailer failed\n"
-      unless $end >= 8;
+        unless $end >= 8;
 
     unless ( read( $self->{fh}, $buf, 32 ) == 32 ) {
         die "Read of plist trailer failed\n";
@@ -292,7 +286,7 @@
 
     # Catch invalid offset addresses in the offset table
     if (grep {
-            $_ < 8
+                   $_ < 8
                 or $_ >= $end
                 or ($_ >= $OffsetTableOffset
                 and $_ < $OffsetTableOffset + $NumObjects * $OffsetSize )



More information about the Bps-public-commit mailing list