| 5767 |
alewis |
1 |
package Archive::Zip::Member;
|
|
|
2 |
|
|
|
3 |
# A generic member of an archive
|
|
|
4 |
|
|
|
5 |
use strict;
|
|
|
6 |
use vars qw( $VERSION @ISA );
|
|
|
7 |
|
|
|
8 |
BEGIN {
|
|
|
9 |
$VERSION = '1.57';
|
|
|
10 |
@ISA = qw( Archive::Zip );
|
|
|
11 |
|
|
|
12 |
if ($^O eq 'MSWin32') {
|
|
|
13 |
require Win32;
|
|
|
14 |
require Encode;
|
|
|
15 |
Encode->import(qw{ decode_utf8 });
|
|
|
16 |
}
|
|
|
17 |
}
|
|
|
18 |
|
|
|
19 |
use Archive::Zip qw(
|
|
|
20 |
:CONSTANTS
|
|
|
21 |
:MISC_CONSTANTS
|
|
|
22 |
:ERROR_CODES
|
|
|
23 |
:PKZIP_CONSTANTS
|
|
|
24 |
:UTILITY_METHODS
|
|
|
25 |
);
|
|
|
26 |
|
|
|
27 |
use Time::Local ();
|
|
|
28 |
use Compress::Raw::Zlib qw( Z_OK Z_STREAM_END MAX_WBITS );
|
|
|
29 |
use File::Path;
|
|
|
30 |
use File::Basename;
|
|
|
31 |
|
|
|
32 |
# Unix perms for default creation of files/dirs.
|
|
|
33 |
use constant DEFAULT_DIRECTORY_PERMISSIONS => 040755;
|
|
|
34 |
use constant DEFAULT_FILE_PERMISSIONS => 0100666;
|
|
|
35 |
use constant DIRECTORY_ATTRIB => 040000;
|
|
|
36 |
use constant FILE_ATTRIB => 0100000;
|
|
|
37 |
|
|
|
38 |
# Returns self if successful, else undef
|
|
|
39 |
# Assumes that fh is positioned at beginning of central directory file header.
|
|
|
40 |
# Leaves fh positioned immediately after file header or EOCD signature.
|
|
|
41 |
sub _newFromZipFile {
|
|
|
42 |
my $class = shift;
|
|
|
43 |
my $self = Archive::Zip::ZipFileMember->_newFromZipFile(@_);
|
|
|
44 |
return $self;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
sub newFromString {
|
|
|
48 |
my $class = shift;
|
|
|
49 |
|
|
|
50 |
my ($stringOrStringRef, $fileName);
|
|
|
51 |
if (ref($_[0]) eq 'HASH') {
|
|
|
52 |
$stringOrStringRef = $_[0]->{string};
|
|
|
53 |
$fileName = $_[0]->{zipName};
|
|
|
54 |
} else {
|
|
|
55 |
($stringOrStringRef, $fileName) = @_;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
my $self =
|
|
|
59 |
Archive::Zip::StringMember->_newFromString($stringOrStringRef, $fileName);
|
|
|
60 |
return $self;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
sub newFromFile {
|
|
|
64 |
my $class = shift;
|
|
|
65 |
|
|
|
66 |
my ($fileName, $zipName);
|
|
|
67 |
if (ref($_[0]) eq 'HASH') {
|
|
|
68 |
$fileName = $_[0]->{fileName};
|
|
|
69 |
$zipName = $_[0]->{zipName};
|
|
|
70 |
} else {
|
|
|
71 |
($fileName, $zipName) = @_;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
my $self =
|
|
|
75 |
Archive::Zip::NewFileMember->_newFromFileNamed($fileName, $zipName);
|
|
|
76 |
return $self;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
sub newDirectoryNamed {
|
|
|
80 |
my $class = shift;
|
|
|
81 |
|
|
|
82 |
my ($directoryName, $newName);
|
|
|
83 |
if (ref($_[0]) eq 'HASH') {
|
|
|
84 |
$directoryName = $_[0]->{directoryName};
|
|
|
85 |
$newName = $_[0]->{zipName};
|
|
|
86 |
} else {
|
|
|
87 |
($directoryName, $newName) = @_;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
my $self =
|
|
|
91 |
Archive::Zip::DirectoryMember->_newNamed($directoryName, $newName);
|
|
|
92 |
return $self;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
sub new {
|
|
|
96 |
my $class = shift;
|
|
|
97 |
my $self = {
|
|
|
98 |
'lastModFileDateTime' => 0,
|
|
|
99 |
'fileAttributeFormat' => FA_UNIX,
|
|
|
100 |
'versionMadeBy' => 20,
|
|
|
101 |
'versionNeededToExtract' => 20,
|
|
|
102 |
'bitFlag' => ($Archive::Zip::UNICODE ? 0x0800 : 0),
|
|
|
103 |
'compressionMethod' => COMPRESSION_STORED,
|
|
|
104 |
'desiredCompressionMethod' => COMPRESSION_STORED,
|
|
|
105 |
'desiredCompressionLevel' => COMPRESSION_LEVEL_NONE,
|
|
|
106 |
'internalFileAttributes' => 0,
|
|
|
107 |
'externalFileAttributes' => 0, # set later
|
|
|
108 |
'fileName' => '',
|
|
|
109 |
'cdExtraField' => '',
|
|
|
110 |
'localExtraField' => '',
|
|
|
111 |
'fileComment' => '',
|
|
|
112 |
'crc32' => 0,
|
|
|
113 |
'compressedSize' => 0,
|
|
|
114 |
'uncompressedSize' => 0,
|
|
|
115 |
'isSymbolicLink' => 0,
|
|
|
116 |
'password' => undef, # password for encrypted data
|
|
|
117 |
'crc32c' => -1, # crc for decrypted data
|
|
|
118 |
@_
|
|
|
119 |
};
|
|
|
120 |
bless($self, $class);
|
|
|
121 |
$self->unixFileAttributes($self->DEFAULT_FILE_PERMISSIONS);
|
|
|
122 |
return $self;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
sub _becomeDirectoryIfNecessary {
|
|
|
126 |
my $self = shift;
|
|
|
127 |
$self->_become('Archive::Zip::DirectoryMember')
|
|
|
128 |
if $self->isDirectory();
|
|
|
129 |
return $self;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
# Morph into given class (do whatever cleanup I need to do)
|
|
|
133 |
sub _become {
|
|
|
134 |
return bless($_[0], $_[1]);
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
sub versionMadeBy {
|
|
|
138 |
shift->{'versionMadeBy'};
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
sub fileAttributeFormat {
|
|
|
142 |
my $self = shift;
|
|
|
143 |
|
|
|
144 |
if (@_) {
|
|
|
145 |
$self->{fileAttributeFormat} =
|
|
|
146 |
(ref($_[0]) eq 'HASH') ? $_[0]->{format} : $_[0];
|
|
|
147 |
} else {
|
|
|
148 |
return $self->{fileAttributeFormat};
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
sub versionNeededToExtract {
|
|
|
153 |
shift->{'versionNeededToExtract'};
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
sub bitFlag {
|
|
|
157 |
my $self = shift;
|
|
|
158 |
|
|
|
159 |
# Set General Purpose Bit Flags according to the desiredCompressionLevel setting
|
|
|
160 |
if ( $self->desiredCompressionLevel == 1
|
|
|
161 |
|| $self->desiredCompressionLevel == 2) {
|
|
|
162 |
$self->{'bitFlag'} |= DEFLATING_COMPRESSION_FAST;
|
|
|
163 |
} elsif ($self->desiredCompressionLevel == 3
|
|
|
164 |
|| $self->desiredCompressionLevel == 4
|
|
|
165 |
|| $self->desiredCompressionLevel == 5
|
|
|
166 |
|| $self->desiredCompressionLevel == 6
|
|
|
167 |
|| $self->desiredCompressionLevel == 7) {
|
|
|
168 |
$self->{'bitFlag'} |= DEFLATING_COMPRESSION_NORMAL;
|
|
|
169 |
} elsif ($self->desiredCompressionLevel == 8
|
|
|
170 |
|| $self->desiredCompressionLevel == 9) {
|
|
|
171 |
$self->{'bitFlag'} |= DEFLATING_COMPRESSION_MAXIMUM;
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
if ($Archive::Zip::UNICODE) {
|
|
|
175 |
$self->{'bitFlag'} |= 0x0800;
|
|
|
176 |
}
|
|
|
177 |
$self->{'bitFlag'};
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
sub password {
|
|
|
181 |
my $self = shift;
|
|
|
182 |
$self->{'password'} = shift if @_;
|
|
|
183 |
$self->{'password'};
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
sub compressionMethod {
|
|
|
187 |
shift->{'compressionMethod'};
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
sub desiredCompressionMethod {
|
|
|
191 |
my $self = shift;
|
|
|
192 |
my $newDesiredCompressionMethod =
|
|
|
193 |
(ref($_[0]) eq 'HASH') ? shift->{compressionMethod} : shift;
|
|
|
194 |
my $oldDesiredCompressionMethod = $self->{'desiredCompressionMethod'};
|
|
|
195 |
if (defined($newDesiredCompressionMethod)) {
|
|
|
196 |
$self->{'desiredCompressionMethod'} = $newDesiredCompressionMethod;
|
|
|
197 |
if ($newDesiredCompressionMethod == COMPRESSION_STORED) {
|
|
|
198 |
$self->{'desiredCompressionLevel'} = 0;
|
|
|
199 |
$self->{'bitFlag'} &= ~GPBF_HAS_DATA_DESCRIPTOR_MASK
|
|
|
200 |
if $self->uncompressedSize() == 0;
|
|
|
201 |
} elsif ($oldDesiredCompressionMethod == COMPRESSION_STORED) {
|
|
|
202 |
$self->{'desiredCompressionLevel'} = COMPRESSION_LEVEL_DEFAULT;
|
|
|
203 |
}
|
|
|
204 |
}
|
|
|
205 |
return $oldDesiredCompressionMethod;
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
sub desiredCompressionLevel {
|
|
|
209 |
my $self = shift;
|
|
|
210 |
my $newDesiredCompressionLevel =
|
|
|
211 |
(ref($_[0]) eq 'HASH') ? shift->{compressionLevel} : shift;
|
|
|
212 |
my $oldDesiredCompressionLevel = $self->{'desiredCompressionLevel'};
|
|
|
213 |
if (defined($newDesiredCompressionLevel)) {
|
|
|
214 |
$self->{'desiredCompressionLevel'} = $newDesiredCompressionLevel;
|
|
|
215 |
$self->{'desiredCompressionMethod'} = (
|
|
|
216 |
$newDesiredCompressionLevel
|
|
|
217 |
? COMPRESSION_DEFLATED
|
|
|
218 |
: COMPRESSION_STORED
|
|
|
219 |
);
|
|
|
220 |
}
|
|
|
221 |
return $oldDesiredCompressionLevel;
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
sub fileName {
|
|
|
225 |
my $self = shift;
|
|
|
226 |
my $newName = shift;
|
|
|
227 |
if (defined $newName) {
|
|
|
228 |
$newName =~ s{[\\/]+}{/}g; # deal with dos/windoze problems
|
|
|
229 |
$self->{'fileName'} = $newName;
|
|
|
230 |
}
|
|
|
231 |
return $self->{'fileName'};
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
sub fileNameAsBytes {
|
|
|
235 |
my $self = shift;
|
|
|
236 |
my $bytes = $self->{'fileName'};
|
|
|
237 |
if($self->{'bitFlag'} & 0x800){
|
|
|
238 |
$bytes = Encode::encode_utf8($bytes);
|
|
|
239 |
}
|
|
|
240 |
return $bytes;
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
sub lastModFileDateTime {
|
|
|
244 |
my $modTime = shift->{'lastModFileDateTime'};
|
|
|
245 |
$modTime =~ m/^(\d+)$/; # untaint
|
|
|
246 |
return $1;
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
sub lastModTime {
|
|
|
250 |
my $self = shift;
|
|
|
251 |
return _dosToUnixTime($self->lastModFileDateTime());
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
sub setLastModFileDateTimeFromUnix {
|
|
|
255 |
my $self = shift;
|
|
|
256 |
my $time_t = shift;
|
|
|
257 |
$self->{'lastModFileDateTime'} = _unixToDosTime($time_t);
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
sub internalFileAttributes {
|
|
|
261 |
shift->{'internalFileAttributes'};
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
sub externalFileAttributes {
|
|
|
265 |
shift->{'externalFileAttributes'};
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
# Convert UNIX permissions into proper value for zip file
|
|
|
269 |
# Usable as a function or a method
|
|
|
270 |
sub _mapPermissionsFromUnix {
|
|
|
271 |
my $self = shift;
|
|
|
272 |
my $mode = shift;
|
|
|
273 |
my $attribs = $mode << 16;
|
|
|
274 |
|
|
|
275 |
# Microsoft Windows Explorer needs this bit set for directories
|
|
|
276 |
if ($mode & DIRECTORY_ATTRIB) {
|
|
|
277 |
$attribs |= 16;
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
return $attribs;
|
|
|
281 |
|
|
|
282 |
# TODO: map more MS-DOS perms
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
# Convert ZIP permissions into Unix ones
|
|
|
286 |
#
|
|
|
287 |
# This was taken from Info-ZIP group's portable UnZip
|
|
|
288 |
# zipfile-extraction program, version 5.50.
|
|
|
289 |
# http://www.info-zip.org/pub/infozip/
|
|
|
290 |
#
|
|
|
291 |
# See the mapattr() function in unix/unix.c
|
|
|
292 |
# See the attribute format constants in unzpriv.h
|
|
|
293 |
#
|
|
|
294 |
# XXX Note that there's one situation that is not implemented
|
|
|
295 |
# yet that depends on the "extra field."
|
|
|
296 |
sub _mapPermissionsToUnix {
|
|
|
297 |
my $self = shift;
|
|
|
298 |
|
|
|
299 |
my $format = $self->{'fileAttributeFormat'};
|
|
|
300 |
my $attribs = $self->{'externalFileAttributes'};
|
|
|
301 |
|
|
|
302 |
my $mode = 0;
|
|
|
303 |
|
|
|
304 |
if ($format == FA_AMIGA) {
|
|
|
305 |
$attribs = $attribs >> 17 & 7; # Amiga RWE bits
|
|
|
306 |
$mode = $attribs << 6 | $attribs << 3 | $attribs;
|
|
|
307 |
return $mode;
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
if ($format == FA_THEOS) {
|
|
|
311 |
$attribs &= 0xF1FFFFFF;
|
|
|
312 |
if (($attribs & 0xF0000000) != 0x40000000) {
|
|
|
313 |
$attribs &= 0x01FFFFFF; # not a dir, mask all ftype bits
|
|
|
314 |
} else {
|
|
|
315 |
$attribs &= 0x41FFFFFF; # leave directory bit as set
|
|
|
316 |
}
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
if ( $format == FA_UNIX
|
|
|
320 |
|| $format == FA_VAX_VMS
|
|
|
321 |
|| $format == FA_ACORN
|
|
|
322 |
|| $format == FA_ATARI_ST
|
|
|
323 |
|| $format == FA_BEOS
|
|
|
324 |
|| $format == FA_QDOS
|
|
|
325 |
|| $format == FA_TANDEM) {
|
|
|
326 |
$mode = $attribs >> 16;
|
|
|
327 |
return $mode if $mode != 0 or not $self->localExtraField;
|
|
|
328 |
|
|
|
329 |
# warn("local extra field is: ", $self->localExtraField, "\n");
|
|
|
330 |
|
|
|
331 |
# XXX This condition is not implemented
|
|
|
332 |
# I'm just including the comments from the info-zip section for now.
|
|
|
333 |
|
|
|
334 |
# Some (non-Info-ZIP) implementations of Zip for Unix and
|
|
|
335 |
# VMS (and probably others ??) leave 0 in the upper 16-bit
|
|
|
336 |
# part of the external_file_attributes field. Instead, they
|
|
|
337 |
# store file permission attributes in some extra field.
|
|
|
338 |
# As a work-around, we search for the presence of one of
|
|
|
339 |
# these extra fields and fall back to the MSDOS compatible
|
|
|
340 |
# part of external_file_attributes if one of the known
|
|
|
341 |
# e.f. types has been detected.
|
|
|
342 |
# Later, we might implement extraction of the permission
|
|
|
343 |
# bits from the VMS extra field. But for now, the work-around
|
|
|
344 |
# should be sufficient to provide "readable" extracted files.
|
|
|
345 |
# (For ASI Unix e.f., an experimental remap from the e.f.
|
|
|
346 |
# mode value IS already provided!)
|
|
|
347 |
}
|
|
|
348 |
|
|
|
349 |
# PKWARE's PKZip for Unix marks entries as FA_MSDOS, but stores the
|
|
|
350 |
# Unix attributes in the upper 16 bits of the external attributes
|
|
|
351 |
# field, just like Info-ZIP's Zip for Unix. We try to use that
|
|
|
352 |
# value, after a check for consistency with the MSDOS attribute
|
|
|
353 |
# bits (see below).
|
|
|
354 |
if ($format == FA_MSDOS) {
|
|
|
355 |
$mode = $attribs >> 16;
|
|
|
356 |
}
|
|
|
357 |
|
|
|
358 |
# FA_MSDOS, FA_OS2_HPFS, FA_WINDOWS_NTFS, FA_MACINTOSH, FA_TOPS20
|
|
|
359 |
$attribs = !($attribs & 1) << 1 | ($attribs & 0x10) >> 4;
|
|
|
360 |
|
|
|
361 |
# keep previous $mode setting when its "owner"
|
|
|
362 |
# part appears to be consistent with DOS attribute flags!
|
|
|
363 |
return $mode if ($mode & 0700) == (0400 | $attribs << 6);
|
|
|
364 |
$mode = 0444 | $attribs << 6 | $attribs << 3 | $attribs;
|
|
|
365 |
return $mode;
|
|
|
366 |
}
|
|
|
367 |
|
|
|
368 |
sub unixFileAttributes {
|
|
|
369 |
my $self = shift;
|
|
|
370 |
my $oldPerms = $self->_mapPermissionsToUnix;
|
|
|
371 |
|
|
|
372 |
my $perms;
|
|
|
373 |
if (@_) {
|
|
|
374 |
$perms = (ref($_[0]) eq 'HASH') ? $_[0]->{attributes} : $_[0];
|
|
|
375 |
|
|
|
376 |
if ($self->isDirectory) {
|
|
|
377 |
$perms &= ~FILE_ATTRIB;
|
|
|
378 |
$perms |= DIRECTORY_ATTRIB;
|
|
|
379 |
} else {
|
|
|
380 |
$perms &= ~DIRECTORY_ATTRIB;
|
|
|
381 |
$perms |= FILE_ATTRIB;
|
|
|
382 |
}
|
|
|
383 |
$self->{externalFileAttributes} =
|
|
|
384 |
$self->_mapPermissionsFromUnix($perms);
|
|
|
385 |
}
|
|
|
386 |
|
|
|
387 |
return $oldPerms;
|
|
|
388 |
}
|
|
|
389 |
|
|
|
390 |
sub localExtraField {
|
|
|
391 |
my $self = shift;
|
|
|
392 |
|
|
|
393 |
if (@_) {
|
|
|
394 |
$self->{localExtraField} =
|
|
|
395 |
(ref($_[0]) eq 'HASH') ? $_[0]->{field} : $_[0];
|
|
|
396 |
} else {
|
|
|
397 |
return $self->{localExtraField};
|
|
|
398 |
}
|
|
|
399 |
}
|
|
|
400 |
|
|
|
401 |
sub cdExtraField {
|
|
|
402 |
my $self = shift;
|
|
|
403 |
|
|
|
404 |
if (@_) {
|
|
|
405 |
$self->{cdExtraField} = (ref($_[0]) eq 'HASH') ? $_[0]->{field} : $_[0];
|
|
|
406 |
} else {
|
|
|
407 |
return $self->{cdExtraField};
|
|
|
408 |
}
|
|
|
409 |
}
|
|
|
410 |
|
|
|
411 |
sub extraFields {
|
|
|
412 |
my $self = shift;
|
|
|
413 |
return $self->localExtraField() . $self->cdExtraField();
|
|
|
414 |
}
|
|
|
415 |
|
|
|
416 |
sub fileComment {
|
|
|
417 |
my $self = shift;
|
|
|
418 |
|
|
|
419 |
if (@_) {
|
|
|
420 |
$self->{fileComment} =
|
|
|
421 |
(ref($_[0]) eq 'HASH')
|
|
|
422 |
? pack('C0a*', $_[0]->{comment})
|
|
|
423 |
: pack('C0a*', $_[0]);
|
|
|
424 |
} else {
|
|
|
425 |
return $self->{fileComment};
|
|
|
426 |
}
|
|
|
427 |
}
|
|
|
428 |
|
|
|
429 |
sub hasDataDescriptor {
|
|
|
430 |
my $self = shift;
|
|
|
431 |
if (@_) {
|
|
|
432 |
my $shouldHave = shift;
|
|
|
433 |
if ($shouldHave) {
|
|
|
434 |
$self->{'bitFlag'} |= GPBF_HAS_DATA_DESCRIPTOR_MASK;
|
|
|
435 |
} else {
|
|
|
436 |
$self->{'bitFlag'} &= ~GPBF_HAS_DATA_DESCRIPTOR_MASK;
|
|
|
437 |
}
|
|
|
438 |
}
|
|
|
439 |
return $self->{'bitFlag'} & GPBF_HAS_DATA_DESCRIPTOR_MASK;
|
|
|
440 |
}
|
|
|
441 |
|
|
|
442 |
sub crc32 {
|
|
|
443 |
shift->{'crc32'};
|
|
|
444 |
}
|
|
|
445 |
|
|
|
446 |
sub crc32String {
|
|
|
447 |
sprintf("%08x", shift->{'crc32'});
|
|
|
448 |
}
|
|
|
449 |
|
|
|
450 |
sub compressedSize {
|
|
|
451 |
shift->{'compressedSize'};
|
|
|
452 |
}
|
|
|
453 |
|
|
|
454 |
sub uncompressedSize {
|
|
|
455 |
shift->{'uncompressedSize'};
|
|
|
456 |
}
|
|
|
457 |
|
|
|
458 |
sub isEncrypted {
|
|
|
459 |
shift->{'bitFlag'} & GPBF_ENCRYPTED_MASK;
|
|
|
460 |
}
|
|
|
461 |
|
|
|
462 |
sub isTextFile {
|
|
|
463 |
my $self = shift;
|
|
|
464 |
my $bit = $self->internalFileAttributes() & IFA_TEXT_FILE_MASK;
|
|
|
465 |
if (@_) {
|
|
|
466 |
my $flag = (ref($_[0]) eq 'HASH') ? shift->{flag} : shift;
|
|
|
467 |
$self->{'internalFileAttributes'} &= ~IFA_TEXT_FILE_MASK;
|
|
|
468 |
$self->{'internalFileAttributes'} |=
|
|
|
469 |
($flag ? IFA_TEXT_FILE : IFA_BINARY_FILE);
|
|
|
470 |
}
|
|
|
471 |
return $bit == IFA_TEXT_FILE;
|
|
|
472 |
}
|
|
|
473 |
|
|
|
474 |
sub isBinaryFile {
|
|
|
475 |
my $self = shift;
|
|
|
476 |
my $bit = $self->internalFileAttributes() & IFA_TEXT_FILE_MASK;
|
|
|
477 |
if (@_) {
|
|
|
478 |
my $flag = shift;
|
|
|
479 |
$self->{'internalFileAttributes'} &= ~IFA_TEXT_FILE_MASK;
|
|
|
480 |
$self->{'internalFileAttributes'} |=
|
|
|
481 |
($flag ? IFA_BINARY_FILE : IFA_TEXT_FILE);
|
|
|
482 |
}
|
|
|
483 |
return $bit == IFA_BINARY_FILE;
|
|
|
484 |
}
|
|
|
485 |
|
|
|
486 |
sub extractToFileNamed {
|
|
|
487 |
my $self = shift;
|
|
|
488 |
|
|
|
489 |
# local FS name
|
|
|
490 |
my $name = (ref($_[0]) eq 'HASH') ? $_[0]->{name} : $_[0];
|
|
|
491 |
$self->{'isSymbolicLink'} = 0;
|
|
|
492 |
|
|
|
493 |
# Check if the file / directory is a symbolic link or not
|
|
|
494 |
if ($self->{'externalFileAttributes'} == 0xA1FF0000) {
|
|
|
495 |
$self->{'isSymbolicLink'} = 1;
|
|
|
496 |
$self->{'newName'} = $name;
|
|
|
497 |
my ($status, $fh) = _newFileHandle($name, 'r');
|
|
|
498 |
my $retval = $self->extractToFileHandle($fh);
|
|
|
499 |
$fh->close();
|
|
|
500 |
} else {
|
|
|
501 |
|
|
|
502 |
#return _writeSymbolicLink($self, $name) if $self->isSymbolicLink();
|
|
|
503 |
|
|
|
504 |
my ($status, $fh);
|
|
|
505 |
if ($^O eq 'MSWin32' && $Archive::Zip::UNICODE) {
|
|
|
506 |
$name = decode_utf8(Win32::GetFullPathName($name));
|
|
|
507 |
mkpath_win32($name);
|
|
|
508 |
Win32::CreateFile($name);
|
|
|
509 |
($status, $fh) = _newFileHandle(Win32::GetANSIPathName($name), 'w');
|
|
|
510 |
} else {
|
|
|
511 |
mkpath(dirname($name)); # croaks on error
|
|
|
512 |
($status, $fh) = _newFileHandle($name, 'w');
|
|
|
513 |
}
|
|
|
514 |
return _ioError("Can't open file $name for write") unless $status;
|
|
|
515 |
my $retval = $self->extractToFileHandle($fh);
|
|
|
516 |
$fh->close();
|
|
|
517 |
chmod($self->unixFileAttributes(), $name)
|
|
|
518 |
or return _error("Can't chmod() ${name}: $!");
|
|
|
519 |
utime($self->lastModTime(), $self->lastModTime(), $name);
|
|
|
520 |
return $retval;
|
|
|
521 |
}
|
|
|
522 |
}
|
|
|
523 |
|
|
|
524 |
sub mkpath_win32 {
|
|
|
525 |
my $path = shift;
|
|
|
526 |
use File::Spec;
|
|
|
527 |
|
|
|
528 |
my ($volume, @path) = File::Spec->splitdir($path);
|
|
|
529 |
$path = File::Spec->catfile($volume, shift @path);
|
|
|
530 |
pop @path;
|
|
|
531 |
while (@path) {
|
|
|
532 |
$path = File::Spec->catfile($path, shift @path);
|
|
|
533 |
Win32::CreateDirectory($path);
|
|
|
534 |
}
|
|
|
535 |
}
|
|
|
536 |
|
|
|
537 |
sub _writeSymbolicLink {
|
|
|
538 |
my $self = shift;
|
|
|
539 |
my $name = shift;
|
|
|
540 |
my $chunkSize = $Archive::Zip::ChunkSize;
|
|
|
541 |
|
|
|
542 |
#my ( $outRef, undef ) = $self->readChunk($chunkSize);
|
|
|
543 |
my $fh;
|
|
|
544 |
my $retval = $self->extractToFileHandle($fh);
|
|
|
545 |
my ($outRef, undef) = $self->readChunk(100);
|
|
|
546 |
}
|
|
|
547 |
|
|
|
548 |
sub isSymbolicLink {
|
|
|
549 |
my $self = shift;
|
|
|
550 |
if ($self->{'externalFileAttributes'} == 0xA1FF0000) {
|
|
|
551 |
$self->{'isSymbolicLink'} = 1;
|
|
|
552 |
} else {
|
|
|
553 |
return 0;
|
|
|
554 |
}
|
|
|
555 |
1;
|
|
|
556 |
}
|
|
|
557 |
|
|
|
558 |
sub isDirectory {
|
|
|
559 |
return 0;
|
|
|
560 |
}
|
|
|
561 |
|
|
|
562 |
sub externalFileName {
|
|
|
563 |
return undef;
|
|
|
564 |
}
|
|
|
565 |
|
|
|
566 |
# The following are used when copying data
|
|
|
567 |
sub _writeOffset {
|
|
|
568 |
shift->{'writeOffset'};
|
|
|
569 |
}
|
|
|
570 |
|
|
|
571 |
sub _readOffset {
|
|
|
572 |
shift->{'readOffset'};
|
|
|
573 |
}
|
|
|
574 |
|
|
|
575 |
sub writeLocalHeaderRelativeOffset {
|
|
|
576 |
shift->{'writeLocalHeaderRelativeOffset'};
|
|
|
577 |
}
|
|
|
578 |
|
|
|
579 |
sub wasWritten { shift->{'wasWritten'} }
|
|
|
580 |
|
|
|
581 |
sub _dataEnded {
|
|
|
582 |
shift->{'dataEnded'};
|
|
|
583 |
}
|
|
|
584 |
|
|
|
585 |
sub _readDataRemaining {
|
|
|
586 |
shift->{'readDataRemaining'};
|
|
|
587 |
}
|
|
|
588 |
|
|
|
589 |
sub _inflater {
|
|
|
590 |
shift->{'inflater'};
|
|
|
591 |
}
|
|
|
592 |
|
|
|
593 |
sub _deflater {
|
|
|
594 |
shift->{'deflater'};
|
|
|
595 |
}
|
|
|
596 |
|
|
|
597 |
# Return the total size of my local header
|
|
|
598 |
sub _localHeaderSize {
|
|
|
599 |
my $self = shift;
|
|
|
600 |
{
|
|
|
601 |
use bytes;
|
|
|
602 |
return SIGNATURE_LENGTH +
|
|
|
603 |
LOCAL_FILE_HEADER_LENGTH +
|
|
|
604 |
length($self->fileName()) +
|
|
|
605 |
length($self->localExtraField());
|
|
|
606 |
}
|
|
|
607 |
}
|
|
|
608 |
|
|
|
609 |
# Return the total size of my CD header
|
|
|
610 |
sub _centralDirectoryHeaderSize {
|
|
|
611 |
my $self = shift;
|
|
|
612 |
{
|
|
|
613 |
use bytes;
|
|
|
614 |
return SIGNATURE_LENGTH +
|
|
|
615 |
CENTRAL_DIRECTORY_FILE_HEADER_LENGTH +
|
|
|
616 |
length($self->fileName()) +
|
|
|
617 |
length($self->cdExtraField()) +
|
|
|
618 |
length($self->fileComment());
|
|
|
619 |
}
|
|
|
620 |
}
|
|
|
621 |
|
|
|
622 |
# DOS date/time format
|
|
|
623 |
# 0-4 (5) Second divided by 2
|
|
|
624 |
# 5-10 (6) Minute (0-59)
|
|
|
625 |
# 11-15 (5) Hour (0-23 on a 24-hour clock)
|
|
|
626 |
# 16-20 (5) Day of the month (1-31)
|
|
|
627 |
# 21-24 (4) Month (1 = January, 2 = February, etc.)
|
|
|
628 |
# 25-31 (7) Year offset from 1980 (add 1980 to get actual year)
|
|
|
629 |
|
|
|
630 |
# Convert DOS date/time format to unix time_t format
|
|
|
631 |
# NOT AN OBJECT METHOD!
|
|
|
632 |
sub _dosToUnixTime {
|
|
|
633 |
my $dt = shift;
|
|
|
634 |
return time() unless defined($dt);
|
|
|
635 |
|
|
|
636 |
my $year = (($dt >> 25) & 0x7f) + 80;
|
|
|
637 |
my $mon = (($dt >> 21) & 0x0f) - 1;
|
|
|
638 |
my $mday = (($dt >> 16) & 0x1f);
|
|
|
639 |
|
|
|
640 |
my $hour = (($dt >> 11) & 0x1f);
|
|
|
641 |
my $min = (($dt >> 5) & 0x3f);
|
|
|
642 |
my $sec = (($dt << 1) & 0x3e);
|
|
|
643 |
|
|
|
644 |
# catch errors
|
|
|
645 |
my $time_t =
|
|
|
646 |
eval { Time::Local::timelocal($sec, $min, $hour, $mday, $mon, $year); };
|
|
|
647 |
return time() if ($@);
|
|
|
648 |
return $time_t;
|
|
|
649 |
}
|
|
|
650 |
|
|
|
651 |
# Note, this is not exactly UTC 1980, it's 1980 + 12 hours and 1
|
|
|
652 |
# minute so that nothing timezoney can muck us up.
|
|
|
653 |
my $safe_epoch = 315576060;
|
|
|
654 |
|
|
|
655 |
# convert a unix time to DOS date/time
|
|
|
656 |
# NOT AN OBJECT METHOD!
|
|
|
657 |
sub _unixToDosTime {
|
|
|
658 |
my $time_t = shift;
|
|
|
659 |
unless ($time_t) {
|
|
|
660 |
_error("Tried to add member with zero or undef value for time");
|
|
|
661 |
$time_t = $safe_epoch;
|
|
|
662 |
}
|
|
|
663 |
if ($time_t < $safe_epoch) {
|
|
|
664 |
_ioError("Unsupported date before 1980 encountered, moving to 1980");
|
|
|
665 |
$time_t = $safe_epoch;
|
|
|
666 |
}
|
|
|
667 |
my ($sec, $min, $hour, $mday, $mon, $year) = localtime($time_t);
|
|
|
668 |
my $dt = 0;
|
|
|
669 |
$dt += ($sec >> 1);
|
|
|
670 |
$dt += ($min << 5);
|
|
|
671 |
$dt += ($hour << 11);
|
|
|
672 |
$dt += ($mday << 16);
|
|
|
673 |
$dt += (($mon + 1) << 21);
|
|
|
674 |
$dt += (($year - 80) << 25);
|
|
|
675 |
return $dt;
|
|
|
676 |
}
|
|
|
677 |
|
|
|
678 |
sub head {
|
|
|
679 |
my ($self, $mode) = (@_, 0);
|
|
|
680 |
|
|
|
681 |
use bytes;
|
|
|
682 |
return pack LOCAL_FILE_HEADER_FORMAT,
|
|
|
683 |
$self->versionNeededToExtract(),
|
|
|
684 |
$self->{'bitFlag'},
|
|
|
685 |
$self->desiredCompressionMethod(),
|
|
|
686 |
$self->lastModFileDateTime(),
|
|
|
687 |
$self->hasDataDescriptor()
|
|
|
688 |
? (0,0,0) # crc, compr & uncompr all zero if data descriptor present
|
|
|
689 |
: (
|
|
|
690 |
$self->crc32(),
|
|
|
691 |
$mode
|
|
|
692 |
? $self->_writeOffset() # compressed size
|
|
|
693 |
: $self->compressedSize(), # may need to be re-written later
|
|
|
694 |
$self->uncompressedSize(),
|
|
|
695 |
),
|
|
|
696 |
length($self->fileNameAsBytes()),
|
|
|
697 |
length($self->localExtraField());
|
|
|
698 |
}
|
|
|
699 |
|
|
|
700 |
# Write my local header to a file handle.
|
|
|
701 |
# Stores the offset to the start of the header in my
|
|
|
702 |
# writeLocalHeaderRelativeOffset member.
|
|
|
703 |
# Returns AZ_OK on success.
|
|
|
704 |
sub _writeLocalFileHeader {
|
|
|
705 |
my $self = shift;
|
|
|
706 |
my $fh = shift;
|
|
|
707 |
|
|
|
708 |
my $signatureData = pack(SIGNATURE_FORMAT, LOCAL_FILE_HEADER_SIGNATURE);
|
|
|
709 |
$self->_print($fh, $signatureData)
|
|
|
710 |
or return _ioError("writing local header signature");
|
|
|
711 |
|
|
|
712 |
my $header = $self->head(1);
|
|
|
713 |
|
|
|
714 |
$self->_print($fh, $header) or return _ioError("writing local header");
|
|
|
715 |
|
|
|
716 |
# Check for a valid filename or a filename equal to a literal `0'
|
|
|
717 |
if ($self->fileName() || $self->fileName eq '0') {
|
|
|
718 |
$self->_print($fh, $self->fileNameAsBytes())
|
|
|
719 |
or return _ioError("writing local header filename");
|
|
|
720 |
}
|
|
|
721 |
if ($self->localExtraField()) {
|
|
|
722 |
$self->_print($fh, $self->localExtraField())
|
|
|
723 |
or return _ioError("writing local extra field");
|
|
|
724 |
}
|
|
|
725 |
|
|
|
726 |
return AZ_OK;
|
|
|
727 |
}
|
|
|
728 |
|
|
|
729 |
sub _writeCentralDirectoryFileHeader {
|
|
|
730 |
my $self = shift;
|
|
|
731 |
my $fh = shift;
|
|
|
732 |
|
|
|
733 |
my $sigData =
|
|
|
734 |
pack(SIGNATURE_FORMAT, CENTRAL_DIRECTORY_FILE_HEADER_SIGNATURE);
|
|
|
735 |
$self->_print($fh, $sigData)
|
|
|
736 |
or return _ioError("writing central directory header signature");
|
|
|
737 |
|
|
|
738 |
my ($fileNameLength, $extraFieldLength, $fileCommentLength);
|
|
|
739 |
{
|
|
|
740 |
use bytes;
|
|
|
741 |
$fileNameLength = length($self->fileNameAsBytes());
|
|
|
742 |
$extraFieldLength = length($self->cdExtraField());
|
|
|
743 |
$fileCommentLength = length($self->fileComment());
|
|
|
744 |
}
|
|
|
745 |
|
|
|
746 |
my $header = pack(
|
|
|
747 |
CENTRAL_DIRECTORY_FILE_HEADER_FORMAT,
|
|
|
748 |
$self->versionMadeBy(),
|
|
|
749 |
$self->fileAttributeFormat(),
|
|
|
750 |
$self->versionNeededToExtract(),
|
|
|
751 |
$self->bitFlag(),
|
|
|
752 |
$self->desiredCompressionMethod(),
|
|
|
753 |
$self->lastModFileDateTime(),
|
|
|
754 |
$self->crc32(), # these three fields should have been updated
|
|
|
755 |
$self->_writeOffset(), # by writing the data stream out
|
|
|
756 |
$self->uncompressedSize(), #
|
|
|
757 |
$fileNameLength,
|
|
|
758 |
$extraFieldLength,
|
|
|
759 |
$fileCommentLength,
|
|
|
760 |
0, # {'diskNumberStart'},
|
|
|
761 |
$self->internalFileAttributes(),
|
|
|
762 |
$self->externalFileAttributes(),
|
|
|
763 |
$self->writeLocalHeaderRelativeOffset());
|
|
|
764 |
|
|
|
765 |
$self->_print($fh, $header)
|
|
|
766 |
or return _ioError("writing central directory header");
|
|
|
767 |
if ($fileNameLength) {
|
|
|
768 |
$self->_print($fh, $self->fileNameAsBytes())
|
|
|
769 |
or return _ioError("writing central directory header signature");
|
|
|
770 |
}
|
|
|
771 |
if ($extraFieldLength) {
|
|
|
772 |
$self->_print($fh, $self->cdExtraField())
|
|
|
773 |
or return _ioError("writing central directory extra field");
|
|
|
774 |
}
|
|
|
775 |
if ($fileCommentLength) {
|
|
|
776 |
$self->_print($fh, $self->fileComment())
|
|
|
777 |
or return _ioError("writing central directory file comment");
|
|
|
778 |
}
|
|
|
779 |
|
|
|
780 |
return AZ_OK;
|
|
|
781 |
}
|
|
|
782 |
|
|
|
783 |
# This writes a data descriptor to the given file handle.
|
|
|
784 |
# Assumes that crc32, writeOffset, and uncompressedSize are
|
|
|
785 |
# set correctly (they should be after a write).
|
|
|
786 |
# Further, the local file header should have the
|
|
|
787 |
# GPBF_HAS_DATA_DESCRIPTOR_MASK bit set.
|
|
|
788 |
sub _writeDataDescriptor {
|
|
|
789 |
my $self = shift;
|
|
|
790 |
my $fh = shift;
|
|
|
791 |
my $header = pack(
|
|
|
792 |
SIGNATURE_FORMAT . DATA_DESCRIPTOR_FORMAT,
|
|
|
793 |
DATA_DESCRIPTOR_SIGNATURE,
|
|
|
794 |
$self->crc32(),
|
|
|
795 |
$self->_writeOffset(), # compressed size
|
|
|
796 |
$self->uncompressedSize());
|
|
|
797 |
|
|
|
798 |
$self->_print($fh, $header)
|
|
|
799 |
or return _ioError("writing data descriptor");
|
|
|
800 |
return AZ_OK;
|
|
|
801 |
}
|
|
|
802 |
|
|
|
803 |
# Re-writes the local file header with new crc32 and compressedSize fields.
|
|
|
804 |
# To be called after writing the data stream.
|
|
|
805 |
# Assumes that filename and extraField sizes didn't change since last written.
|
|
|
806 |
sub _refreshLocalFileHeader {
|
|
|
807 |
my $self = shift;
|
|
|
808 |
my $fh = shift;
|
|
|
809 |
|
|
|
810 |
my $here = $fh->tell();
|
|
|
811 |
$fh->seek($self->writeLocalHeaderRelativeOffset() + SIGNATURE_LENGTH,
|
|
|
812 |
IO::Seekable::SEEK_SET)
|
|
|
813 |
or return _ioError("seeking to rewrite local header");
|
|
|
814 |
|
|
|
815 |
my $header = $self->head(1);
|
|
|
816 |
|
|
|
817 |
$self->_print($fh, $header)
|
|
|
818 |
or return _ioError("re-writing local header");
|
|
|
819 |
$fh->seek($here, IO::Seekable::SEEK_SET)
|
|
|
820 |
or return _ioError("seeking after rewrite of local header");
|
|
|
821 |
|
|
|
822 |
return AZ_OK;
|
|
|
823 |
}
|
|
|
824 |
|
|
|
825 |
sub readChunk {
|
|
|
826 |
my $self = shift;
|
|
|
827 |
my $chunkSize = (ref($_[0]) eq 'HASH') ? $_[0]->{chunkSize} : $_[0];
|
|
|
828 |
|
|
|
829 |
if ($self->readIsDone()) {
|
|
|
830 |
$self->endRead();
|
|
|
831 |
my $dummy = '';
|
|
|
832 |
return (\$dummy, AZ_STREAM_END);
|
|
|
833 |
}
|
|
|
834 |
|
|
|
835 |
$chunkSize = $Archive::Zip::ChunkSize if not defined($chunkSize);
|
|
|
836 |
$chunkSize = $self->_readDataRemaining()
|
|
|
837 |
if $chunkSize > $self->_readDataRemaining();
|
|
|
838 |
|
|
|
839 |
my $buffer = '';
|
|
|
840 |
my $outputRef;
|
|
|
841 |
my ($bytesRead, $status) = $self->_readRawChunk(\$buffer, $chunkSize);
|
|
|
842 |
return (\$buffer, $status) unless $status == AZ_OK;
|
|
|
843 |
|
|
|
844 |
$buffer && $self->isEncrypted and $buffer = $self->_decode($buffer);
|
|
|
845 |
$self->{'readDataRemaining'} -= $bytesRead;
|
|
|
846 |
$self->{'readOffset'} += $bytesRead;
|
|
|
847 |
|
|
|
848 |
if ($self->compressionMethod() == COMPRESSION_STORED) {
|
|
|
849 |
$self->{'crc32'} = $self->computeCRC32($buffer, $self->{'crc32'});
|
|
|
850 |
}
|
|
|
851 |
|
|
|
852 |
($outputRef, $status) = &{$self->{'chunkHandler'}}($self, \$buffer);
|
|
|
853 |
$self->{'writeOffset'} += length($$outputRef);
|
|
|
854 |
|
|
|
855 |
$self->endRead()
|
|
|
856 |
if $self->readIsDone();
|
|
|
857 |
|
|
|
858 |
return ($outputRef, $status);
|
|
|
859 |
}
|
|
|
860 |
|
|
|
861 |
# Read the next raw chunk of my data. Subclasses MUST implement.
|
|
|
862 |
# my ( $bytesRead, $status) = $self->_readRawChunk( \$buffer, $chunkSize );
|
|
|
863 |
sub _readRawChunk {
|
|
|
864 |
my $self = shift;
|
|
|
865 |
return $self->_subclassResponsibility();
|
|
|
866 |
}
|
|
|
867 |
|
|
|
868 |
# A place holder to catch rewindData errors if someone ignores
|
|
|
869 |
# the error code.
|
|
|
870 |
sub _noChunk {
|
|
|
871 |
my $self = shift;
|
|
|
872 |
return (\undef, _error("trying to copy chunk when init failed"));
|
|
|
873 |
}
|
|
|
874 |
|
|
|
875 |
# Basically a no-op so that I can have a consistent interface.
|
|
|
876 |
# ( $outputRef, $status) = $self->_copyChunk( \$buffer );
|
|
|
877 |
sub _copyChunk {
|
|
|
878 |
my ($self, $dataRef) = @_;
|
|
|
879 |
return ($dataRef, AZ_OK);
|
|
|
880 |
}
|
|
|
881 |
|
|
|
882 |
# ( $outputRef, $status) = $self->_deflateChunk( \$buffer );
|
|
|
883 |
sub _deflateChunk {
|
|
|
884 |
my ($self, $buffer) = @_;
|
|
|
885 |
my ($status) = $self->_deflater()->deflate($buffer, my $out);
|
|
|
886 |
|
|
|
887 |
if ($self->_readDataRemaining() == 0) {
|
|
|
888 |
my $extraOutput;
|
|
|
889 |
($status) = $self->_deflater()->flush($extraOutput);
|
|
|
890 |
$out .= $extraOutput;
|
|
|
891 |
$self->endRead();
|
|
|
892 |
return (\$out, AZ_STREAM_END);
|
|
|
893 |
} elsif ($status == Z_OK) {
|
|
|
894 |
return (\$out, AZ_OK);
|
|
|
895 |
} else {
|
|
|
896 |
$self->endRead();
|
|
|
897 |
my $retval = _error('deflate error', $status);
|
|
|
898 |
my $dummy = '';
|
|
|
899 |
return (\$dummy, $retval);
|
|
|
900 |
}
|
|
|
901 |
}
|
|
|
902 |
|
|
|
903 |
# ( $outputRef, $status) = $self->_inflateChunk( \$buffer );
|
|
|
904 |
sub _inflateChunk {
|
|
|
905 |
my ($self, $buffer) = @_;
|
|
|
906 |
my ($status) = $self->_inflater()->inflate($buffer, my $out);
|
|
|
907 |
my $retval;
|
|
|
908 |
$self->endRead() unless $status == Z_OK;
|
|
|
909 |
if ($status == Z_OK || $status == Z_STREAM_END) {
|
|
|
910 |
$retval = ($status == Z_STREAM_END) ? AZ_STREAM_END : AZ_OK;
|
|
|
911 |
return (\$out, $retval);
|
|
|
912 |
} else {
|
|
|
913 |
$retval = _error('inflate error', $status);
|
|
|
914 |
my $dummy = '';
|
|
|
915 |
return (\$dummy, $retval);
|
|
|
916 |
}
|
|
|
917 |
}
|
|
|
918 |
|
|
|
919 |
sub rewindData {
|
|
|
920 |
my $self = shift;
|
|
|
921 |
my $status;
|
|
|
922 |
|
|
|
923 |
# set to trap init errors
|
|
|
924 |
$self->{'chunkHandler'} = $self->can('_noChunk');
|
|
|
925 |
|
|
|
926 |
# Work around WinZip bug with 0-length DEFLATED files
|
|
|
927 |
$self->desiredCompressionMethod(COMPRESSION_STORED)
|
|
|
928 |
if $self->uncompressedSize() == 0;
|
|
|
929 |
|
|
|
930 |
# assume that we're going to read the whole file, and compute the CRC anew.
|
|
|
931 |
$self->{'crc32'} = 0
|
|
|
932 |
if ($self->compressionMethod() == COMPRESSION_STORED);
|
|
|
933 |
|
|
|
934 |
# These are the only combinations of methods we deal with right now.
|
|
|
935 |
if ( $self->compressionMethod() == COMPRESSION_STORED
|
|
|
936 |
and $self->desiredCompressionMethod() == COMPRESSION_DEFLATED) {
|
|
|
937 |
($self->{'deflater'}, $status) = Compress::Raw::Zlib::Deflate->new(
|
|
|
938 |
'-Level' => $self->desiredCompressionLevel(),
|
|
|
939 |
'-WindowBits' => -MAX_WBITS(), # necessary magic
|
|
|
940 |
'-Bufsize' => $Archive::Zip::ChunkSize,
|
|
|
941 |
@_
|
|
|
942 |
); # pass additional options
|
|
|
943 |
return _error('deflateInit error:', $status)
|
|
|
944 |
unless $status == Z_OK;
|
|
|
945 |
$self->{'chunkHandler'} = $self->can('_deflateChunk');
|
|
|
946 |
} elsif ($self->compressionMethod() == COMPRESSION_DEFLATED
|
|
|
947 |
and $self->desiredCompressionMethod() == COMPRESSION_STORED) {
|
|
|
948 |
($self->{'inflater'}, $status) = Compress::Raw::Zlib::Inflate->new(
|
|
|
949 |
'-WindowBits' => -MAX_WBITS(), # necessary magic
|
|
|
950 |
'-Bufsize' => $Archive::Zip::ChunkSize,
|
|
|
951 |
@_
|
|
|
952 |
); # pass additional options
|
|
|
953 |
return _error('inflateInit error:', $status)
|
|
|
954 |
unless $status == Z_OK;
|
|
|
955 |
$self->{'chunkHandler'} = $self->can('_inflateChunk');
|
|
|
956 |
} elsif ($self->compressionMethod() == $self->desiredCompressionMethod()) {
|
|
|
957 |
$self->{'chunkHandler'} = $self->can('_copyChunk');
|
|
|
958 |
} else {
|
|
|
959 |
return _error(
|
|
|
960 |
sprintf(
|
|
|
961 |
"Unsupported compression combination: read %d, write %d",
|
|
|
962 |
$self->compressionMethod(),
|
|
|
963 |
$self->desiredCompressionMethod()));
|
|
|
964 |
}
|
|
|
965 |
|
|
|
966 |
$self->{'readDataRemaining'} =
|
|
|
967 |
($self->compressionMethod() == COMPRESSION_STORED)
|
|
|
968 |
? $self->uncompressedSize()
|
|
|
969 |
: $self->compressedSize();
|
|
|
970 |
$self->{'dataEnded'} = 0;
|
|
|
971 |
$self->{'readOffset'} = 0;
|
|
|
972 |
|
|
|
973 |
return AZ_OK;
|
|
|
974 |
}
|
|
|
975 |
|
|
|
976 |
sub endRead {
|
|
|
977 |
my $self = shift;
|
|
|
978 |
delete $self->{'inflater'};
|
|
|
979 |
delete $self->{'deflater'};
|
|
|
980 |
$self->{'dataEnded'} = 1;
|
|
|
981 |
$self->{'readDataRemaining'} = 0;
|
|
|
982 |
return AZ_OK;
|
|
|
983 |
}
|
|
|
984 |
|
|
|
985 |
sub readIsDone {
|
|
|
986 |
my $self = shift;
|
|
|
987 |
return ($self->_dataEnded() or !$self->_readDataRemaining());
|
|
|
988 |
}
|
|
|
989 |
|
|
|
990 |
sub contents {
|
|
|
991 |
my $self = shift;
|
|
|
992 |
my $newContents = shift;
|
|
|
993 |
|
|
|
994 |
if (defined($newContents)) {
|
|
|
995 |
|
|
|
996 |
# change our type and call the subclass contents method.
|
|
|
997 |
$self->_become('Archive::Zip::StringMember');
|
|
|
998 |
return $self->contents(pack('C0a*', $newContents)); # in case of Unicode
|
|
|
999 |
} else {
|
|
|
1000 |
my $oldCompression =
|
|
|
1001 |
$self->desiredCompressionMethod(COMPRESSION_STORED);
|
|
|
1002 |
my $status = $self->rewindData(@_);
|
|
|
1003 |
if ($status != AZ_OK) {
|
|
|
1004 |
$self->endRead();
|
|
|
1005 |
return $status;
|
|
|
1006 |
}
|
|
|
1007 |
my $retval = '';
|
|
|
1008 |
while ($status == AZ_OK) {
|
|
|
1009 |
my $ref;
|
|
|
1010 |
($ref, $status) = $self->readChunk($self->_readDataRemaining());
|
|
|
1011 |
|
|
|
1012 |
# did we get it in one chunk?
|
|
|
1013 |
if (length($$ref) == $self->uncompressedSize()) {
|
|
|
1014 |
$retval = $$ref;
|
|
|
1015 |
} else {
|
|
|
1016 |
$retval .= $$ref
|
|
|
1017 |
}
|
|
|
1018 |
}
|
|
|
1019 |
$self->desiredCompressionMethod($oldCompression);
|
|
|
1020 |
$self->endRead();
|
|
|
1021 |
$status = AZ_OK if $status == AZ_STREAM_END;
|
|
|
1022 |
$retval = undef unless $status == AZ_OK;
|
|
|
1023 |
return wantarray ? ($retval, $status) : $retval;
|
|
|
1024 |
}
|
|
|
1025 |
}
|
|
|
1026 |
|
|
|
1027 |
sub extractToFileHandle {
|
|
|
1028 |
my $self = shift;
|
|
|
1029 |
my $fh = (ref($_[0]) eq 'HASH') ? shift->{fileHandle} : shift;
|
|
|
1030 |
_binmode($fh);
|
|
|
1031 |
my $oldCompression = $self->desiredCompressionMethod(COMPRESSION_STORED);
|
|
|
1032 |
my $status = $self->rewindData(@_);
|
|
|
1033 |
$status = $self->_writeData($fh) if $status == AZ_OK;
|
|
|
1034 |
$self->desiredCompressionMethod($oldCompression);
|
|
|
1035 |
$self->endRead();
|
|
|
1036 |
return $status;
|
|
|
1037 |
}
|
|
|
1038 |
|
|
|
1039 |
# write local header and data stream to file handle
|
|
|
1040 |
sub _writeToFileHandle {
|
|
|
1041 |
my $self = shift;
|
|
|
1042 |
my $fh = shift;
|
|
|
1043 |
my $fhIsSeekable = shift;
|
|
|
1044 |
my $offset = shift;
|
|
|
1045 |
|
|
|
1046 |
return _error("no member name given for $self")
|
|
|
1047 |
if $self->fileName() eq '';
|
|
|
1048 |
|
|
|
1049 |
$self->{'writeLocalHeaderRelativeOffset'} = $offset;
|
|
|
1050 |
$self->{'wasWritten'} = 0;
|
|
|
1051 |
|
|
|
1052 |
# Determine if I need to write a data descriptor
|
|
|
1053 |
# I need to do this if I can't refresh the header
|
|
|
1054 |
# and I don't know compressed size or crc32 fields.
|
|
|
1055 |
my $headerFieldsUnknown = (
|
|
|
1056 |
($self->uncompressedSize() > 0)
|
|
|
1057 |
and ($self->compressionMethod() == COMPRESSION_STORED
|
|
|
1058 |
or $self->desiredCompressionMethod() == COMPRESSION_DEFLATED));
|
|
|
1059 |
|
|
|
1060 |
my $shouldWriteDataDescriptor =
|
|
|
1061 |
($headerFieldsUnknown and not $fhIsSeekable);
|
|
|
1062 |
|
|
|
1063 |
$self->hasDataDescriptor(1)
|
|
|
1064 |
if ($shouldWriteDataDescriptor);
|
|
|
1065 |
|
|
|
1066 |
$self->{'writeOffset'} = 0;
|
|
|
1067 |
|
|
|
1068 |
my $status = $self->rewindData();
|
|
|
1069 |
($status = $self->_writeLocalFileHeader($fh))
|
|
|
1070 |
if $status == AZ_OK;
|
|
|
1071 |
($status = $self->_writeData($fh))
|
|
|
1072 |
if $status == AZ_OK;
|
|
|
1073 |
if ($status == AZ_OK) {
|
|
|
1074 |
$self->{'wasWritten'} = 1;
|
|
|
1075 |
if ($self->hasDataDescriptor()) {
|
|
|
1076 |
$status = $self->_writeDataDescriptor($fh);
|
|
|
1077 |
} elsif ($headerFieldsUnknown) {
|
|
|
1078 |
$status = $self->_refreshLocalFileHeader($fh);
|
|
|
1079 |
}
|
|
|
1080 |
}
|
|
|
1081 |
|
|
|
1082 |
return $status;
|
|
|
1083 |
}
|
|
|
1084 |
|
|
|
1085 |
# Copy my (possibly compressed) data to given file handle.
|
|
|
1086 |
# Returns C<AZ_OK> on success
|
|
|
1087 |
sub _writeData {
|
|
|
1088 |
my $self = shift;
|
|
|
1089 |
my $writeFh = shift;
|
|
|
1090 |
|
|
|
1091 |
# If symbolic link, just create one if the operating system is Linux, Unix, BSD or VMS
|
|
|
1092 |
# TODO: Add checks for other operating systems
|
|
|
1093 |
if ($self->{'isSymbolicLink'} == 1 && $^O eq 'linux') {
|
|
|
1094 |
my $chunkSize = $Archive::Zip::ChunkSize;
|
|
|
1095 |
my ($outRef, $status) = $self->readChunk($chunkSize);
|
|
|
1096 |
symlink $$outRef, $self->{'newName'};
|
|
|
1097 |
} else {
|
|
|
1098 |
return AZ_OK if ($self->uncompressedSize() == 0);
|
|
|
1099 |
my $status;
|
|
|
1100 |
my $chunkSize = $Archive::Zip::ChunkSize;
|
|
|
1101 |
while ($self->_readDataRemaining() > 0) {
|
|
|
1102 |
my $outRef;
|
|
|
1103 |
($outRef, $status) = $self->readChunk($chunkSize);
|
|
|
1104 |
return $status if ($status != AZ_OK and $status != AZ_STREAM_END);
|
|
|
1105 |
|
|
|
1106 |
if (length($$outRef) > 0) {
|
|
|
1107 |
$self->_print($writeFh, $$outRef)
|
|
|
1108 |
or return _ioError("write error during copy");
|
|
|
1109 |
}
|
|
|
1110 |
|
|
|
1111 |
last if $status == AZ_STREAM_END;
|
|
|
1112 |
}
|
|
|
1113 |
}
|
|
|
1114 |
return AZ_OK;
|
|
|
1115 |
}
|
|
|
1116 |
|
|
|
1117 |
# Return true if I depend on the named file
|
|
|
1118 |
sub _usesFileNamed {
|
|
|
1119 |
return 0;
|
|
|
1120 |
}
|
|
|
1121 |
|
|
|
1122 |
# ##############################################################################
|
|
|
1123 |
#
|
|
|
1124 |
# Decrypt section
|
|
|
1125 |
#
|
|
|
1126 |
# H.Merijn Brand (Tux) 2011-06-28
|
|
|
1127 |
#
|
|
|
1128 |
# ##############################################################################
|
|
|
1129 |
|
|
|
1130 |
# This code is derived from the crypt source of unzip-6.0 dated 05 Jan 2007
|
|
|
1131 |
# Its license states:
|
|
|
1132 |
#
|
|
|
1133 |
# --8<---
|
|
|
1134 |
# Copyright (c) 1990-2007 Info-ZIP. All rights reserved.
|
|
|
1135 |
|
|
|
1136 |
# See the accompanying file LICENSE, version 2005-Feb-10 or later
|
|
|
1137 |
# (the contents of which are also included in (un)zip.h) for terms of use.
|
|
|
1138 |
# If, for some reason, all these files are missing, the Info-ZIP license
|
|
|
1139 |
# also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
|
|
|
1140 |
#
|
|
|
1141 |
# crypt.c (full version) by Info-ZIP. Last revised: [see crypt.h]
|
|
|
1142 |
|
|
|
1143 |
# The main encryption/decryption source code for Info-Zip software was
|
|
|
1144 |
# originally written in Europe. To the best of our knowledge, it can
|
|
|
1145 |
# be freely distributed in both source and object forms from any country,
|
|
|
1146 |
# including the USA under License Exception TSU of the U.S. Export
|
|
|
1147 |
# Administration Regulations (section 740.13(e)) of 6 June 2002.
|
|
|
1148 |
|
|
|
1149 |
# NOTE on copyright history:
|
|
|
1150 |
# Previous versions of this source package (up to version 2.8) were
|
|
|
1151 |
# not copyrighted and put in the public domain. If you cannot comply
|
|
|
1152 |
# with the Info-Zip LICENSE, you may want to look for one of those
|
|
|
1153 |
# public domain versions.
|
|
|
1154 |
#
|
|
|
1155 |
# This encryption code is a direct transcription of the algorithm from
|
|
|
1156 |
# Roger Schlafly, described by Phil Katz in the file appnote.txt. This
|
|
|
1157 |
# file (appnote.txt) is distributed with the PKZIP program (even in the
|
|
|
1158 |
# version without encryption capabilities).
|
|
|
1159 |
# -->8---
|
|
|
1160 |
|
|
|
1161 |
# As of January 2000, US export regulations were amended to allow export
|
|
|
1162 |
# of free encryption source code from the US. As of June 2002, these
|
|
|
1163 |
# regulations were further relaxed to allow export of encryption binaries
|
|
|
1164 |
# associated with free encryption source code. The Zip 2.31, UnZip 5.52
|
|
|
1165 |
# and Wiz 5.02 archives now include full crypto source code. As of the
|
|
|
1166 |
# Zip 2.31 release, all official binaries include encryption support; the
|
|
|
1167 |
# former "zcr" archives ceased to exist.
|
|
|
1168 |
# (Note that restrictions may still exist in other countries, of course.)
|
|
|
1169 |
|
|
|
1170 |
# For now, we just support the decrypt stuff
|
|
|
1171 |
# All below methods are supposed to be private
|
|
|
1172 |
|
|
|
1173 |
# use Data::Peek;
|
|
|
1174 |
|
|
|
1175 |
my @keys;
|
|
|
1176 |
my @crct = do {
|
|
|
1177 |
my $xor = 0xedb88320;
|
|
|
1178 |
my @crc = (0) x 1024;
|
|
|
1179 |
|
|
|
1180 |
# generate a crc for every 8-bit value
|
|
|
1181 |
foreach my $n (0 .. 255) {
|
|
|
1182 |
my $c = $n;
|
|
|
1183 |
$c = $c & 1 ? $xor ^ ($c >> 1) : $c >> 1 for 1 .. 8;
|
|
|
1184 |
$crc[$n] = _revbe($c);
|
|
|
1185 |
}
|
|
|
1186 |
|
|
|
1187 |
# generate crc for each value followed by one, two, and three zeros */
|
|
|
1188 |
foreach my $n (0 .. 255) {
|
|
|
1189 |
my $c = ($crc[($crc[$n] >> 24) ^ 0] ^ ($crc[$n] << 8)) & 0xffffffff;
|
|
|
1190 |
$crc[$_ * 256 + $n] = $c for 1 .. 3;
|
|
|
1191 |
}
|
|
|
1192 |
map { _revbe($crc[$_]) } 0 .. 1023;
|
|
|
1193 |
};
|
|
|
1194 |
|
|
|
1195 |
sub _crc32 {
|
|
|
1196 |
my ($c, $b) = @_;
|
|
|
1197 |
return ($crct[($c ^ $b) & 0xff] ^ ($c >> 8));
|
|
|
1198 |
} # _crc32
|
|
|
1199 |
|
|
|
1200 |
sub _revbe {
|
|
|
1201 |
my $w = shift;
|
|
|
1202 |
return (($w >> 24) +
|
|
|
1203 |
(($w >> 8) & 0xff00) +
|
|
|
1204 |
(($w & 0xff00) << 8) +
|
|
|
1205 |
(($w & 0xff) << 24));
|
|
|
1206 |
} # _revbe
|
|
|
1207 |
|
|
|
1208 |
sub _update_keys {
|
|
|
1209 |
use integer;
|
|
|
1210 |
my $c = shift; # signed int
|
|
|
1211 |
$keys[0] = _crc32($keys[0], $c);
|
|
|
1212 |
$keys[1] = (($keys[1] + ($keys[0] & 0xff)) * 0x08088405 + 1) & 0xffffffff;
|
|
|
1213 |
my $keyshift = $keys[1] >> 24;
|
|
|
1214 |
$keys[2] = _crc32($keys[2], $keyshift);
|
|
|
1215 |
} # _update_keys
|
|
|
1216 |
|
|
|
1217 |
sub _zdecode ($) {
|
|
|
1218 |
my $c = shift;
|
|
|
1219 |
my $t = ($keys[2] & 0xffff) | 2;
|
|
|
1220 |
_update_keys($c ^= ((($t * ($t ^ 1)) >> 8) & 0xff));
|
|
|
1221 |
return $c;
|
|
|
1222 |
} # _zdecode
|
|
|
1223 |
|
|
|
1224 |
sub _decode {
|
|
|
1225 |
my $self = shift;
|
|
|
1226 |
my $buff = shift;
|
|
|
1227 |
|
|
|
1228 |
$self->isEncrypted or return $buff;
|
|
|
1229 |
|
|
|
1230 |
my $pass = $self->password;
|
|
|
1231 |
defined $pass or return "";
|
|
|
1232 |
|
|
|
1233 |
@keys = (0x12345678, 0x23456789, 0x34567890);
|
|
|
1234 |
_update_keys($_) for unpack "C*", $pass;
|
|
|
1235 |
|
|
|
1236 |
# DDumper { uk => [ @keys ] };
|
|
|
1237 |
|
|
|
1238 |
my $head = substr $buff, 0, 12, "";
|
|
|
1239 |
my @head = map { _zdecode($_) } unpack "C*", $head;
|
|
|
1240 |
my $x =
|
|
|
1241 |
$self->{externalFileAttributes}
|
|
|
1242 |
? ($self->{lastModFileDateTime} >> 8) & 0xff
|
|
|
1243 |
: $self->{crc32} >> 24;
|
|
|
1244 |
$head[-1] == $x or return ""; # Password fail
|
|
|
1245 |
|
|
|
1246 |
# Worth checking ...
|
|
|
1247 |
$self->{crc32c} = (unpack LOCAL_FILE_HEADER_FORMAT, pack "C*", @head)[3];
|
|
|
1248 |
|
|
|
1249 |
# DHexDump ($buff);
|
|
|
1250 |
$buff = pack "C*" => map { _zdecode($_) } unpack "C*" => $buff;
|
|
|
1251 |
|
|
|
1252 |
# DHexDump ($buff);
|
|
|
1253 |
return $buff;
|
|
|
1254 |
} # _decode
|
|
|
1255 |
|
|
|
1256 |
1;
|