| 1 | package Archive::Zip::Parser; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use Data::ParseBinary; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 3 | ▶ | use Archive::Zip::Parser::Entry; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use Archive::Zip::Parser::CentralDirectoryEnd; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 3 | ▶ | use base qw( Archive::Zip::Parser::Exception ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 3 | ▶ | use version; our $VERSION = qv( '0.0.3' ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 15 | 2 | my ( $class, $arg_ref ) = @_; | |||||||||
| 16 | |||||||||||
| 17 | 2 | my $new_object = bless {}, $class; | |||||||||
| 18 | |||||||||||
| 19 | 2 | ▶ | if ( !exists $arg_ref->{'file_name'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 1 | $new_object->_croak('Missing "file_name"'); | |||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | 1 | open my $file_handle, '<', $arg_ref->{'file_name'}; | |||||||||
| 24 | 1 | $new_object->{'_file_handle'} = $file_handle; | |||||||||
| 25 | $new_object->{'_bit_stream'} | ||||||||||
| 26 | 1 | = CreateStreamReader( File => $new_object->{'_file_handle'} ); | |||||||||
| 27 | |||||||||||
| 28 | 1 | return $new_object; | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 2 | ▶ | sub verify_signature { | ||||||||
|
Subroutine
verify_signature: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 32 | 2 | my $self = shift; | |||||||||
| 33 | |||||||||||
| 34 | 2 | my $previous_position_in_file; | |||||||||
| 35 | eval { | ||||||||||
| 36 | 2 | $previous_position_in_file = tell $self->{'_file_handle'}; | |||||||||
| 37 | 2 | $self->_set_position_in_file( 0, 0 ); | |||||||||
| 38 | |||||||||||
| 39 | 2 | Magic("PK\x03\x04")->parse( $self->{'_bit_stream'} ); | |||||||||
| 40 | 2 | ▶ | } or do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 0 | return 0; | |||||||||
| 42 | }; | ||||||||||
| 43 | |||||||||||
| 44 | 2 | $self->_set_position_in_file( $previous_position_in_file, 0 ); | |||||||||
| 45 | 2 | return 1; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 1 | ▶ | sub parse { | ||||||||
|
Subroutine
parse: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||
| 49 | 1 | my $self = shift; | |||||||||
| 50 | |||||||||||
| 51 | 1 | ▶ | return 1 if $self->{'_is_parsed'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 1 | ▶ | if ( !$self->verify_signature() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 0 | $self->_croak('Not a valid .ZIP archive'); | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 1 | my @parsed_entry_struct; | |||||||||
| 57 | 1 | while (1) { | |||||||||
| 58 | 6 | ▶ | last if $self->_check_signature('04034b50'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | |||||||||||
| 60 | my $local_file_header_struct | ||||||||||
| 61 | = Struct( | ||||||||||
| 62 | '_entry_struct', | ||||||||||
| 63 | Struct( | ||||||||||
| 64 | '_local_file_header', | ||||||||||
| 65 | ULInt32('_signature' ), | ||||||||||
| 66 | ULInt16('_version_needed'), | ||||||||||
| 67 | BitStruct( '_gp_bit', | ||||||||||
| 68 | Flag('_bit_7' ), | ||||||||||
| 69 | Flag('_bit_6' ), | ||||||||||
| 70 | Flag('_bit_5' ), | ||||||||||
| 71 | Flag('_bit_4' ), | ||||||||||
| 72 | Flag('_bit_3' ), | ||||||||||
| 73 | Flag('_bit_2' ), | ||||||||||
| 74 | Flag('_bit_1' ), | ||||||||||
| 75 | Flag('_bit_0' ), | ||||||||||
| 76 | Flag('_bit_15'), | ||||||||||
| 77 | Flag('_bit_14'), | ||||||||||
| 78 | Flag('_bit_13'), | ||||||||||
| 79 | Flag('_bit_12'), | ||||||||||
| 80 | Flag('_bit_11'), | ||||||||||
| 81 | Flag('_bit_10'), | ||||||||||
| 82 | Flag('_bit_9' ), | ||||||||||
| 83 | Flag('_bit_8' ), | ||||||||||
| 84 | ), | ||||||||||
| 85 | ULInt16('_compression_method'), | ||||||||||
| 86 | ULInt16('_last_mod_time' ), | ||||||||||
| 87 | ULInt16('_last_mod_date' ), | ||||||||||
| 88 | ULInt32('_crc_32' ), | ||||||||||
| 89 | ULInt32('_compressed_size' ), | ||||||||||
| 90 | ULInt32('_uncompressed_size' ), | ||||||||||
| 91 | ULInt16('_file_name_length' ), | ||||||||||
| 92 | ULInt16('_extra_field_length'), | ||||||||||
| 93 | String( | ||||||||||
| 94 | '_file_name', | ||||||||||
| 95 | 5 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 96 | 5 | $_->ctx->{'_file_name_length'}; | |||||||||
| 97 | }, | ||||||||||
| 98 | ), | ||||||||||
| 99 | Field( | ||||||||||
| 100 | '_extra_field', | ||||||||||
| 101 | 5 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 102 | 5 | $_->ctx->{'_extra_field_length'}; | |||||||||
| 103 | }, | ||||||||||
| 104 | ), | ||||||||||
| 105 | ), | ||||||||||
| 106 | Field( | ||||||||||
| 107 | '_file_data', | ||||||||||
| 108 | 5 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 109 | 5 | $_->ctx->{'_local_file_header'}->{'_compressed_size'}; | |||||||||
| 110 | } | ||||||||||
| 111 | ), | ||||||||||
| 112 | If( | ||||||||||
| 113 | 5 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 114 | 5 | $_->ctx->{'_local_file_header'}->{'_gp_bit'}->{'_bit_3'}; | |||||||||
| 115 | }, | ||||||||||
| 116 | 5 | Struct( | |||||||||
| 117 | '_data_descriptor', | ||||||||||
| 118 | ULInt32('_crc_32' ), | ||||||||||
| 119 | ULInt32('_compressed_size' ), | ||||||||||
| 120 | ULInt32('_uncompressed_size'), | ||||||||||
| 121 | ), | ||||||||||
| 122 | ), | ||||||||||
| 123 | ); | ||||||||||
| 124 | |||||||||||
| 125 | push @parsed_entry_struct, | ||||||||||
| 126 | 5 | $local_file_header_struct->parse( $self->{'_bit_stream'} ); | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | 1 | my $entry_count = 0; | |||||||||
| 130 | 1 | while (1) { | |||||||||
| 131 | 6 | ▶ | last if $self->_check_signature('02014b50'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | |||||||||||
| 133 | my $central_directory_struct | ||||||||||
| 134 | = Struct( | ||||||||||
| 135 | '_entry_struct', | ||||||||||
| 136 | Struct( | ||||||||||
| 137 | '_central_directory', | ||||||||||
| 138 | ULInt32('_signature' ), | ||||||||||
| 139 | Field( '_version_made_by', 2 ), | ||||||||||
| 140 | ULInt16('_version_needed' ), | ||||||||||
| 141 | BitStruct( | ||||||||||
| 142 | '_gp_bit', | ||||||||||
| 143 | Flag('_bit_7' ), | ||||||||||
| 144 | Flag('_bit_6' ), | ||||||||||
| 145 | Flag('_bit_5' ), | ||||||||||
| 146 | Flag('_bit_4' ), | ||||||||||
| 147 | Flag('_bit_3' ), | ||||||||||
| 148 | Flag('_bit_2' ), | ||||||||||
| 149 | Flag('_bit_1' ), | ||||||||||
| 150 | Flag('_bit_0' ), | ||||||||||
| 151 | Flag('_bit_15'), | ||||||||||
| 152 | Flag('_bit_14'), | ||||||||||
| 153 | Flag('_bit_13'), | ||||||||||
| 154 | Flag('_bit_12'), | ||||||||||
| 155 | Flag('_bit_11'), | ||||||||||
| 156 | Flag('_bit_10'), | ||||||||||
| 157 | Flag('_bit_9' ), | ||||||||||
| 158 | Flag('_bit_8' ), | ||||||||||
| 159 | ), | ||||||||||
| 160 | ULInt16('_compression_method' ), | ||||||||||
| 161 | ULInt16('_last_mod_time' ), | ||||||||||
| 162 | ULInt16('_last_mod_date' ), | ||||||||||
| 163 | ULInt32('_crc_32' ), | ||||||||||
| 164 | ULInt32('_compressed_size' ), | ||||||||||
| 165 | ULInt32('_uncompressed_size' ), | ||||||||||
| 166 | ULInt16('_file_name_length' ), | ||||||||||
| 167 | ULInt16('_extra_field_length' ), | ||||||||||
| 168 | ULInt16('_file_comment_length' ), | ||||||||||
| 169 | ULInt16('_start_disk_number' ), | ||||||||||
| 170 | ULInt16('_internal_file_attr' ), | ||||||||||
| 171 | ULInt32('_external_file_attr' ), | ||||||||||
| 172 | ULInt32('_rel_offset_local_header'), | ||||||||||
| 173 | String( | ||||||||||
| 174 | '_file_name', | ||||||||||
| 175 | 5 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 176 | 5 | $_->ctx->{'_file_name_length'}; | |||||||||
| 177 | }, | ||||||||||
| 178 | ), | ||||||||||
| 179 | Field( | ||||||||||
| 180 | '_extra_field', | ||||||||||
| 181 | 5 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 182 | 5 | $_->ctx->{'_extra_field_length'}; | |||||||||
| 183 | }, | ||||||||||
| 184 | ), | ||||||||||
| 185 | Field( | ||||||||||
| 186 | '_file_comment', | ||||||||||
| 187 | 5 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 188 | 5 | $_->ctx->{'_file_comment_length'}; | |||||||||
| 189 | } | ||||||||||
| 190 | 5 | ), | |||||||||
| 191 | ), | ||||||||||
| 192 | ); | ||||||||||
| 193 | |||||||||||
| 194 | 5 | %{ $parsed_entry_struct[$entry_count] } = ( | |||||||||
| 195 | 5 | %{ $parsed_entry_struct[$entry_count] }, | |||||||||
| 196 | 5 | %{ $central_directory_struct->parse( | |||||||||
| 197 | 5 | $self->{'_bit_stream'} | |||||||||
| 198 | ) | ||||||||||
| 199 | } | ||||||||||
| 200 | ); | ||||||||||
| 201 | 5 | $entry_count++; | |||||||||
| 202 | } | ||||||||||
| 203 | |||||||||||
| 204 | 1 | ▶ | last if $self->_check_signature('06054b50'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 205 | |||||||||||
| 206 | my $central_directory_end_struct | ||||||||||
| 207 | = Struct( | ||||||||||
| 208 | '_central_directory_end', | ||||||||||
| 209 | ULInt32('_signature' ), | ||||||||||
| 210 | ULInt16('_disk_number' ), | ||||||||||
| 211 | ULInt16('_start_disk_number' ), | ||||||||||
| 212 | ULInt16('_total_disk_entries'), | ||||||||||
| 213 | ULInt16('_total_entries' ), | ||||||||||
| 214 | ULInt32('_size' ), | ||||||||||
| 215 | ULInt32('_start_offset' ), | ||||||||||
| 216 | ULInt16('_zip_comment_length'), | ||||||||||
| 217 | String( | ||||||||||
| 218 | '_zip_comment', | ||||||||||
| 219 | 1 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 220 | 1 | $_->ctx->{'_zip_comment_length'}; | |||||||||
| 221 | }, | ||||||||||
| 222 | 1 | ), | |||||||||
| 223 | ); | ||||||||||
| 224 | |||||||||||
| 225 | my $parsed_central_directory_end_struct | ||||||||||
| 226 | 1 | = $central_directory_end_struct->parse( $self->{'_bit_stream'} ); | |||||||||
| 227 | 1 | $self->{'_central_directory_end'} = $parsed_central_directory_end_struct; | |||||||||
| 228 | 1 | push @{ $self->{'_entry'} }, @parsed_entry_struct; | |||||||||
| 229 | |||||||||||
| 230 | 1 | $self->{'_is_parsed'} = 1; | |||||||||
| 231 | 1 | return 1; | |||||||||
| 232 | } | ||||||||||
| 233 | |||||||||||
| 234 | 4 | ▶ | sub get_entry { | ||||||||
|
Subroutine
get_entry: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 235 | 4 | my ( $self, $entry_number ) = @_; | |||||||||
| 236 | |||||||||||
| 237 | 4 | ▶ | if ( !defined $entry_number ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 238 | 2 | my @entry_objects; | |||||||||
| 239 | 2 | for ( @{ $self->{'_entry'} } ) { | |||||||||
| 240 | 10 | push @entry_objects, bless $_, 'Archive::Zip::Parser::Entry'; | |||||||||
| 241 | } | ||||||||||
| 242 | |||||||||||
| 243 | 2 | return @entry_objects; | |||||||||
| 244 | } | ||||||||||
| 245 | |||||||||||
| 246 | 2 | return bless $self->{'_entry'}->[$entry_number], | |||||||||
| 247 | 'Archive::Zip::Parser::Entry'; | ||||||||||
| 248 | } | ||||||||||
| 249 | |||||||||||
| 250 | 4 | ▶ | sub _set_position_in_file { | ||||||||
|
Subroutine
_set_position_in_file: called CC=1 SCAR=0.0
| |||||||||||
| 251 | 4 | my ( $self, $position_in_file, $whence ) = @_; | |||||||||
| 252 | 4 | my $file_handle = $self->{'_file_handle'}; | |||||||||
| 253 | |||||||||||
| 254 | 4 | seek $file_handle, $position_in_file, $whence; | |||||||||
| 255 | |||||||||||
| 256 | 4 | return; | |||||||||
| 257 | } | ||||||||||
| 258 | |||||||||||
| 259 | 1 | ▶ | sub get_central_directory_end { | ||||||||
|
Subroutine
get_central_directory_end: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 260 | 1 | my $self = shift; | |||||||||
| 261 | 1 | return bless $self->{'_central_directory_end'}, | |||||||||
| 262 | 'Archive::Zip::Parser::CentralDirectoryEnd'; | ||||||||||
| 263 | } | ||||||||||
| 264 | |||||||||||
| 265 | 13 | ▶ | sub _check_signature { | ||||||||
|
Subroutine
_check_signature: called CC=2 SCAR=6.9
| |||||||||||
| 266 | 13 | my ( $self, $next_signature ) = @_; | |||||||||
| 267 | |||||||||||
| 268 | 13 | my $signature_struct = | |||||||||
| 269 | Struct( | ||||||||||
| 270 | '_signature_struct', | ||||||||||
| 271 | Peek( | ||||||||||
| 272 | ULInt32('_signature') | ||||||||||
| 273 | ), | ||||||||||
| 274 | ); | ||||||||||
| 275 | my $parsed_signature_struct | ||||||||||
| 276 | 13 | = $signature_struct->parse( $self->{'_bit_stream'} ); | |||||||||
| 277 | 13 | my $signature = unpack( 'H*', pack( 'N', $parsed_signature_struct->{'_signature'} ) ); | |||||||||
| 278 | 13 | ▶ | if ( $signature ne $next_signature ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 279 | 2 | return 1; | |||||||||
| 280 | } | ||||||||||
| 281 | 11 | return; | |||||||||
| 282 | } | ||||||||||
| 283 | |||||||||||
| 284 | 3 | 1; | |||||||||
| 285 | __END__ | ||||||||||