From 97e713249292eb4eb7ac6ba9fd05abbd1eb00fa5 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 4 Dec 2015 01:05:14 -0500 Subject: simplify signature checking --- lib/Spreadsheet/ParseXLSX.pm | 47 +++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/lib/Spreadsheet/ParseXLSX.pm b/lib/Spreadsheet/ParseXLSX.pm index dd247b7..3d1ebd6 100644 --- a/lib/Spreadsheet/ParseXLSX.pm +++ b/lib/Spreadsheet/ParseXLSX.pm @@ -65,29 +65,12 @@ sub parse { my $workbook = Spreadsheet::ParseExcel::Workbook->new; - my $signature = ''; my $tempfile; - if (openhandle($file)) { - if (ref($file) eq 'GLOB') { - read($file, $signature, 2); - seek($file, -2, IO::File::SEEK_CUR); - } else { - $file->read($signature, 2); - $file->seek(-2, IO::File::SEEK_CUR); - } - $workbook->{File} = undef; - } elsif (!ref($file)) { - my $fh = IO::File->new(); - if ($fh->open("<$file")) { - $workbook->{File} = $file; - $fh->read($signature, 2); - $fh->seek(-2, IO::File::SEEK_CUR); - $file = $fh; - } - } - - if ($signature eq "\xd0\xcf") { - $tempfile = $file = Spreadsheet::ParseXLSX::Decryptor->open($file, $self->{Password}); + if ($self->_check_signature($file)) { + $tempfile = $file = Spreadsheet::ParseXLSX::Decryptor->open( + $file, + $self->{Password} + ); } eval { @@ -96,6 +79,7 @@ sub parse { bless $file, 'IO::File' if ref($file) eq 'GLOB'; # sigh $zip->readFromFileHandle($file) == Archive::Zip::AZ_OK or die "Can't open filehandle as a zip file"; + $workbook->{File} = undef; } elsif (!ref($file)) { $zip->read($file) == Archive::Zip::AZ_OK @@ -116,6 +100,25 @@ sub parse { return $workbook; } +sub _check_signature { + my $self = shift; + my ($file) = @_; + + my $signature = ''; + if (openhandle($file)) { + bless $file, 'IO::File' if ref($file) eq 'GLOB'; # sigh + $file->read($signature, 2); + $file->seek(-2, IO::File::SEEK_CUR); + } + elsif (!ref($file)) { + my $fh = IO::File->new($file, 'r'); + $fh->read($signature, 2); + $fh->close; + } + + return $signature eq "\xd0\xcf"; +} + sub _parse_workbook { my $self = shift; my ($zip, $workbook, $formatter) = @_; -- cgit v1.2.3-54-g00ecf