Cleaing up for production release
[archive-zip.git] / lib / Archive / Zip / ZipFileMember.pm
bloba5bd28b73b598c1b5fac1dd9c98a312e7ead9fc7
1 package Archive::Zip::ZipFileMember;
3 use strict;
4 use vars qw( $VERSION @ISA );
6 BEGIN {
7 $VERSION = '1.28';
8 @ISA = qw ( Archive::Zip::FileMember );
11 use Archive::Zip qw(
12 :CONSTANTS
13 :ERROR_CODES
14 :PKZIP_CONSTANTS
15 :UTILITY_METHODS
18 # Create a new Archive::Zip::ZipFileMember
19 # given a filename and optional open file handle
21 sub _newFromZipFile {
22 my $class = shift;
23 my $fh = shift;
24 my $externalFileName = shift;
25 my $possibleEocdOffset = shift; # normally 0
27 my $self = $class->new(
28 'crc32' => 0,
29 'diskNumberStart' => 0,
30 'localHeaderRelativeOffset' => 0,
31 'dataOffset' => 0, # localHeaderRelativeOffset + header length
34 $self->{'externalFileName'} = $externalFileName;
35 $self->{'fh'} = $fh;
36 $self->{'possibleEocdOffset'} = $possibleEocdOffset;
37 return $self;
40 sub isDirectory {
41 my $self = shift;
42 return (
43 substr( $self->fileName, -1, 1 ) eq '/'
44 and
45 $self->uncompressedSize == 0
49 # Seek to the beginning of the local header, just past the signature.
50 # Verify that the local header signature is in fact correct.
51 # Update the localHeaderRelativeOffset if necessary by adding the possibleEocdOffset.
52 # Returns status.
54 sub _seekToLocalHeader {
55 my $self = shift;
56 my $where = shift; # optional
57 my $previousWhere = shift; # optional
59 $where = $self->localHeaderRelativeOffset() unless defined($where);
61 # avoid loop on certain corrupt files (from Julian Field)
62 return _formatError("corrupt zip file")
63 if defined($previousWhere) && $where == $previousWhere;
65 my $status;
66 my $signature;
68 $status = $self->fh()->seek( $where, IO::Seekable::SEEK_SET );
69 return _ioError("seeking to local header") unless $status;
71 ( $status, $signature ) =
72 _readSignature( $self->fh(), $self->externalFileName(),
73 LOCAL_FILE_HEADER_SIGNATURE );
74 return $status if $status == AZ_IO_ERROR;
76 # retry with EOCD offset if any was given.
77 if ( $status == AZ_FORMAT_ERROR && $self->{'possibleEocdOffset'} ) {
78 $status = $self->_seekToLocalHeader(
79 $self->localHeaderRelativeOffset() + $self->{'possibleEocdOffset'},
80 $where
82 if ( $status == AZ_OK ) {
83 $self->{'localHeaderRelativeOffset'} +=
84 $self->{'possibleEocdOffset'};
85 $self->{'possibleEocdOffset'} = 0;
89 return $status;
92 # Because I'm going to delete the file handle, read the local file
93 # header if the file handle is seekable. If it isn't, I assume that
94 # I've already read the local header.
95 # Return ( $status, $self )
97 sub _become {
98 my $self = shift;
99 my $newClass = shift;
100 return $self if ref($self) eq $newClass;
102 my $status = AZ_OK;
104 if ( _isSeekable( $self->fh() ) ) {
105 my $here = $self->fh()->tell();
106 $status = $self->_seekToLocalHeader();
107 $status = $self->_readLocalFileHeader() if $status == AZ_OK;
108 $self->fh()->seek( $here, IO::Seekable::SEEK_SET );
109 return $status unless $status == AZ_OK;
112 delete( $self->{'eocdCrc32'} );
113 delete( $self->{'diskNumberStart'} );
114 delete( $self->{'localHeaderRelativeOffset'} );
115 delete( $self->{'dataOffset'} );
117 return $self->SUPER::_become($newClass);
120 sub diskNumberStart {
121 shift->{'diskNumberStart'};
124 sub localHeaderRelativeOffset {
125 shift->{'localHeaderRelativeOffset'};
128 sub dataOffset {
129 shift->{'dataOffset'};
132 # Skip local file header, updating only extra field stuff.
133 # Assumes that fh is positioned before signature.
134 sub _skipLocalFileHeader {
135 my $self = shift;
136 my $header;
137 my $bytesRead = $self->fh()->read( $header, LOCAL_FILE_HEADER_LENGTH );
138 if ( $bytesRead != LOCAL_FILE_HEADER_LENGTH ) {
139 return _ioError("reading local file header");
141 my $fileNameLength;
142 my $extraFieldLength;
143 my $bitFlag;
145 undef, # $self->{'versionNeededToExtract'},
146 $bitFlag,
147 undef, # $self->{'compressionMethod'},
148 undef, # $self->{'lastModFileDateTime'},
149 undef, # $crc32,
150 undef, # $compressedSize,
151 undef, # $uncompressedSize,
152 $fileNameLength,
153 $extraFieldLength
154 ) = unpack( LOCAL_FILE_HEADER_FORMAT, $header );
156 if ($fileNameLength) {
157 $self->fh()->seek( $fileNameLength, IO::Seekable::SEEK_CUR )
158 or return _ioError("skipping local file name");
161 if ($extraFieldLength) {
162 $bytesRead =
163 $self->fh()->read( $self->{'localExtraField'}, $extraFieldLength );
164 if ( $bytesRead != $extraFieldLength ) {
165 return _ioError("reading local extra field");
169 $self->{'dataOffset'} = $self->fh()->tell();
171 if ( $bitFlag & GPBF_HAS_DATA_DESCRIPTOR_MASK ) {
173 # Read the crc32, compressedSize, and uncompressedSize from the
174 # extended data descriptor, which directly follows the compressed data.
176 # Skip over the compressed file data (assumes that EOCD compressedSize
177 # was correct)
178 $self->fh()->seek( $self->{'compressedSize'}, IO::Seekable::SEEK_CUR )
179 or return _ioError("seeking to extended local header");
181 # these values should be set correctly from before.
182 my $oldCrc32 = $self->{'eocdCrc32'};
183 my $oldCompressedSize = $self->{'compressedSize'};
184 my $oldUncompressedSize = $self->{'uncompressedSize'};
186 my $status = $self->_readDataDescriptor();
187 return $status unless $status == AZ_OK;
189 return _formatError(
190 "CRC or size mismatch while skipping data descriptor")
191 if ( $oldCrc32 != $self->{'crc32'}
192 || $oldUncompressedSize != $self->{'uncompressedSize'} );
195 return AZ_OK;
198 # Read from a local file header into myself. Returns AZ_OK if successful.
199 # Assumes that fh is positioned after signature.
200 # Note that crc32, compressedSize, and uncompressedSize will be 0 if
201 # GPBF_HAS_DATA_DESCRIPTOR_MASK is set in the bitFlag.
203 sub _readLocalFileHeader {
204 my $self = shift;
205 my $header;
206 my $bytesRead = $self->fh()->read( $header, LOCAL_FILE_HEADER_LENGTH );
207 if ( $bytesRead != LOCAL_FILE_HEADER_LENGTH ) {
208 return _ioError("reading local file header");
210 my $fileNameLength;
211 my $crc32;
212 my $compressedSize;
213 my $uncompressedSize;
214 my $extraFieldLength;
216 $self->{'versionNeededToExtract'}, $self->{'bitFlag'},
217 $self->{'compressionMethod'}, $self->{'lastModFileDateTime'},
218 $crc32, $compressedSize,
219 $uncompressedSize, $fileNameLength,
220 $extraFieldLength
221 ) = unpack( LOCAL_FILE_HEADER_FORMAT, $header );
223 if ($fileNameLength) {
224 my $fileName;
225 $bytesRead = $self->fh()->read( $fileName, $fileNameLength );
226 if ( $bytesRead != $fileNameLength ) {
227 return _ioError("reading local file name");
229 $self->fileName($fileName);
232 if ($extraFieldLength) {
233 $bytesRead =
234 $self->fh()->read( $self->{'localExtraField'}, $extraFieldLength );
235 if ( $bytesRead != $extraFieldLength ) {
236 return _ioError("reading local extra field");
240 $self->{'dataOffset'} = $self->fh()->tell();
242 if ( $self->hasDataDescriptor() ) {
244 # Read the crc32, compressedSize, and uncompressedSize from the
245 # extended data descriptor.
246 # Skip over the compressed file data (assumes that EOCD compressedSize
247 # was correct)
248 $self->fh()->seek( $self->{'compressedSize'}, IO::Seekable::SEEK_CUR )
249 or return _ioError("seeking to extended local header");
251 my $status = $self->_readDataDescriptor();
252 return $status unless $status == AZ_OK;
254 else {
255 return _formatError(
256 "CRC or size mismatch after reading data descriptor")
257 if ( $self->{'crc32'} != $crc32
258 || $self->{'uncompressedSize'} != $uncompressedSize );
261 return AZ_OK;
264 # This will read the data descriptor, which is after the end of compressed file
265 # data in members that that have GPBF_HAS_DATA_DESCRIPTOR_MASK set in their
266 # bitFlag.
267 # The only reliable way to find these is to rely on the EOCD compressedSize.
268 # Assumes that file is positioned immediately after the compressed data.
269 # Returns status; sets crc32, compressedSize, and uncompressedSize.
270 sub _readDataDescriptor {
271 my $self = shift;
272 my $signatureData;
273 my $header;
274 my $crc32;
275 my $compressedSize;
276 my $uncompressedSize;
278 my $bytesRead = $self->fh()->read( $signatureData, SIGNATURE_LENGTH );
279 return _ioError("reading header signature")
280 if $bytesRead != SIGNATURE_LENGTH;
281 my $signature = unpack( SIGNATURE_FORMAT, $signatureData );
283 # unfortunately, the signature appears to be optional.
284 if ( $signature == DATA_DESCRIPTOR_SIGNATURE
285 && ( $signature != $self->{'crc32'} ) )
287 $bytesRead = $self->fh()->read( $header, DATA_DESCRIPTOR_LENGTH );
288 return _ioError("reading data descriptor")
289 if $bytesRead != DATA_DESCRIPTOR_LENGTH;
291 ( $crc32, $compressedSize, $uncompressedSize ) =
292 unpack( DATA_DESCRIPTOR_FORMAT, $header );
294 else {
295 $bytesRead =
296 $self->fh()->read( $header, DATA_DESCRIPTOR_LENGTH_NO_SIG );
297 return _ioError("reading data descriptor")
298 if $bytesRead != DATA_DESCRIPTOR_LENGTH_NO_SIG;
300 $crc32 = $signature;
301 ( $compressedSize, $uncompressedSize ) =
302 unpack( DATA_DESCRIPTOR_FORMAT_NO_SIG, $header );
305 $self->{'eocdCrc32'} = $self->{'crc32'}
306 unless defined( $self->{'eocdCrc32'} );
307 $self->{'crc32'} = $crc32;
308 $self->{'compressedSize'} = $compressedSize;
309 $self->{'uncompressedSize'} = $uncompressedSize;
311 return AZ_OK;
314 # Read a Central Directory header. Return AZ_OK on success.
315 # Assumes that fh is positioned right after the signature.
317 sub _readCentralDirectoryFileHeader {
318 my $self = shift;
319 my $fh = $self->fh();
320 my $header = '';
321 my $bytesRead = $fh->read( $header, CENTRAL_DIRECTORY_FILE_HEADER_LENGTH );
322 if ( $bytesRead != CENTRAL_DIRECTORY_FILE_HEADER_LENGTH ) {
323 return _ioError("reading central dir header");
325 my ( $fileNameLength, $extraFieldLength, $fileCommentLength );
327 $self->{'versionMadeBy'},
328 $self->{'fileAttributeFormat'},
329 $self->{'versionNeededToExtract'},
330 $self->{'bitFlag'},
331 $self->{'compressionMethod'},
332 $self->{'lastModFileDateTime'},
333 $self->{'crc32'},
334 $self->{'compressedSize'},
335 $self->{'uncompressedSize'},
336 $fileNameLength,
337 $extraFieldLength,
338 $fileCommentLength,
339 $self->{'diskNumberStart'},
340 $self->{'internalFileAttributes'},
341 $self->{'externalFileAttributes'},
342 $self->{'localHeaderRelativeOffset'}
343 ) = unpack( CENTRAL_DIRECTORY_FILE_HEADER_FORMAT, $header );
345 $self->{'eocdCrc32'} = $self->{'crc32'};
347 if ($fileNameLength) {
348 $bytesRead = $fh->read( $self->{'fileName'}, $fileNameLength );
349 if ( $bytesRead != $fileNameLength ) {
350 _ioError("reading central dir filename");
353 if ($extraFieldLength) {
354 $bytesRead = $fh->read( $self->{'cdExtraField'}, $extraFieldLength );
355 if ( $bytesRead != $extraFieldLength ) {
356 return _ioError("reading central dir extra field");
359 if ($fileCommentLength) {
360 $bytesRead = $fh->read( $self->{'fileComment'}, $fileCommentLength );
361 if ( $bytesRead != $fileCommentLength ) {
362 return _ioError("reading central dir file comment");
366 # NK 10/21/04: added to avoid problems with manipulated headers
367 if ( $self->{'uncompressedSize'} != $self->{'compressedSize'}
368 and $self->{'compressionMethod'} == COMPRESSION_STORED )
370 $self->{'uncompressedSize'} = $self->{'compressedSize'};
373 $self->desiredCompressionMethod( $self->compressionMethod() );
375 return AZ_OK;
378 sub rewindData {
379 my $self = shift;
381 my $status = $self->SUPER::rewindData(@_);
382 return $status unless $status == AZ_OK;
384 return AZ_IO_ERROR unless $self->fh();
386 $self->fh()->clearerr();
388 # Seek to local file header.
389 # The only reason that I'm doing this this way is that the extraField
390 # length seems to be different between the CD header and the LF header.
391 $status = $self->_seekToLocalHeader();
392 return $status unless $status == AZ_OK;
394 # skip local file header
395 $status = $self->_skipLocalFileHeader();
396 return $status unless $status == AZ_OK;
398 # Seek to beginning of file data
399 $self->fh()->seek( $self->dataOffset(), IO::Seekable::SEEK_SET )
400 or return _ioError("seeking to beginning of file data");
402 return AZ_OK;
405 # Return bytes read. Note that first parameter is a ref to a buffer.
406 # my $data;
407 # my ( $bytesRead, $status) = $self->readRawChunk( \$data, $chunkSize );
408 sub _readRawChunk {
409 my ( $self, $dataRef, $chunkSize ) = @_;
410 return ( 0, AZ_OK ) unless $chunkSize;
411 my $bytesRead = $self->fh()->read( $$dataRef, $chunkSize )
412 or return ( 0, _ioError("reading data") );
413 return ( $bytesRead, AZ_OK );