Skip to content

Commit

Permalink
Fixed ZIP64 compatibility, now local header filesizes are always set …
Browse files Browse the repository at this point in the history
…to 0xFFFFFFFF to force reading from extra field, extra field size is now written correctly (was always 0 before)
  • Loading branch information
fservida committed Sep 1, 2023
1 parent 4a59a62 commit 30226af
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions pyaff4/zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ def empty(self):

def Pack(self):
# Size of extra less the header.
#self.Set("data_size", self.sizeof() - 4)
# Data size needs to be set for a zip64 extra field to be compliant with zip specification.
self.Set("data_size", self.sizeof() - 4)
# Don't think the value set below is used anywhere, might be removable.
self.data_size = self.sizeof()
return struct.pack(self.format_string(),
*[v for t, _, v in self.fields if v is not None])
Expand Down Expand Up @@ -311,22 +313,21 @@ def WriteFileHeader(self, backing_store):
if USE_UNICODE:
header.flags = header.flags | (1 << 11)

# For local header force usage of ZIP64 even when not needed as we do not know the file size, nor what it would
# compress to, before writing the header the first time
# (similar to how zip works in command line when compressing from stdin)
# Always calculate and reserve the zip64 header size
# Alternatively, as the size of the file is not passed on first header creation
# a file larger than 4GB would triggers creation of the header only after file has been written and would get
# the first bytes overwritten creating a corrupted container.
# Only set header to 0xFFFFFFFF if really needed to look into extra header.

extra_header_64 = Zip64FileHeaderExtensibleField()
if self.file_size > ZIP32_MAX_SIZE:
header.file_size = 0xFFFFFFFF

header.file_size = 0xFFFFFFFF
extra_header_64.Set("file_size", self.file_size)

if self.compress_size > ZIP32_MAX_SIZE:
header.compress_size = 0xFFFFFFFF
header.compress_size = 0xFFFFFFFF
extra_header_64.Set("compress_size", self.compress_size)

# Write the extra header in any case
header.extra_field_len = extra_header_64.sizeof()

backing_store.SeekWrite(self.file_header_offset)
Expand Down

0 comments on commit 30226af

Please sign in to comment.