Skip to content

Commit

Permalink
refactor: default no-encryption generator
Browse files Browse the repository at this point in the history
  • Loading branch information
michalc committed Jan 3, 2024
1 parent 46e634d commit d7710bb
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions stream_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ def _raise_if_beyond(offset, maximum, exception_class):
if offset > maximum:
raise exception_class()

def _zip_64_local_header_and_data(name_encoded, mod_at_ms_dos, mod_at_unix_extra, aes_extra, external_attr, uncompressed_size, crc_32, _get_compress_obj, chunks):
def _no_encryption(chunks):
return (yield from chunks)

def _zip_64_local_header_and_data(name_encoded, mod_at_ms_dos, mod_at_unix_extra, aes_extra, external_attr, uncompressed_size, crc_32, _get_compress_obj, encryption_func, chunks):
file_offset = offset

_raise_if_beyond(file_offset, maximum=0xffffffffffffffff, exception_class=OffsetOverflowError)
Expand Down Expand Up @@ -205,7 +208,7 @@ def _zip_64_local_header_and_data(name_encoded, mod_at_ms_dos, mod_at_unix_extra
0xffffffff, # Offset of local header - since zip64
), name_encoded, extra

def _zip_32_local_header_and_data(name_encoded, mod_at_ms_dos, mod_at_unix_extra, aes_extra, external_attr, uncompressed_size, crc_32, _get_compress_obj, chunks):
def _zip_32_local_header_and_data(name_encoded, mod_at_ms_dos, mod_at_unix_extra, aes_extra, external_attr, uncompressed_size, crc_32, _get_compress_obj, encryption_func, chunks):
file_offset = offset

_raise_if_beyond(file_offset, maximum=0xffffffff, exception_class=OffsetOverflowError)
Expand All @@ -232,12 +235,12 @@ def _zip_32_local_header_and_data(name_encoded, mod_at_ms_dos, mod_at_unix_extra
yield from _(name_encoded)
yield from _(extra)

uncompressed_size, compressed_size, crc_32 = yield from _zip_data(
uncompressed_size, compressed_size, crc_32 = yield from encryption_func(_zip_data(
chunks,
_get_compress_obj,
max_uncompressed_size=0xffffffff,
max_compressed_size=0xffffffff,
)
))

yield from _(data_descriptor_signature)
yield from _(data_descriptor_zip_32_struct.pack(crc_32, compressed_size, uncompressed_size))
Expand Down Expand Up @@ -289,7 +292,7 @@ def _zip_data(chunks, _get_compress_obj, max_uncompressed_size, max_compressed_s

return uncompressed_size, compressed_size, crc_32

def _no_compression_64_local_header_and_data(name_encoded, mod_at_ms_dos, mod_at_unix_extra, aes_extra, external_attr, uncompressed_size, crc_32, _get_compress_obj, chunks):
def _no_compression_64_local_header_and_data(name_encoded, mod_at_ms_dos, mod_at_unix_extra, aes_extra, external_attr, uncompressed_size, crc_32, _get_compress_obj, encryption_func, chunks):
file_offset = offset

_raise_if_beyond(file_offset, maximum=0xffffffffffffffff, exception_class=OffsetOverflowError)
Expand Down Expand Up @@ -323,7 +326,7 @@ def _no_compression_64_local_header_and_data(name_encoded, mod_at_ms_dos, mod_at
yield from _(name_encoded)
yield from _(extra)

for chunk in chunks:
for chunk in encryption_func(chunks):
yield from _(chunk)

extra = zip_64_central_directory_extra_struct.pack(
Expand Down Expand Up @@ -354,7 +357,7 @@ def _no_compression_64_local_header_and_data(name_encoded, mod_at_ms_dos, mod_at
), name_encoded, extra


def _no_compression_32_local_header_and_data(name_encoded, mod_at_ms_dos, mod_at_unix_extra, aes_extra, external_attr, uncompressed_size, crc_32, _get_compress_obj, chunks):
def _no_compression_32_local_header_and_data(name_encoded, mod_at_ms_dos, mod_at_unix_extra, aes_extra, external_attr, uncompressed_size, crc_32, _get_compress_obj, encryption_func, chunks):
file_offset = offset

_raise_if_beyond(file_offset, maximum=0xffffffff, exception_class=OffsetOverflowError)
Expand Down Expand Up @@ -382,7 +385,7 @@ def _no_compression_32_local_header_and_data(name_encoded, mod_at_ms_dos, mod_at
yield from _(name_encoded)
yield from _(extra)

for chunk in chunks:
for chunk in encryption_func(chunks):
yield from _(chunk)

return central_directory_header_struct.pack(
Expand Down Expand Up @@ -424,7 +427,7 @@ def _chunks():

return chunks, size, crc_32

def _no_compression_streamed_64_local_header_and_data(name_encoded, mod_at_ms_dos, mod_at_unix_extra, aes_extra, external_attr, uncompressed_size, crc_32, _get_compress_obj, chunks):
def _no_compression_streamed_64_local_header_and_data(name_encoded, mod_at_ms_dos, mod_at_unix_extra, aes_extra, external_attr, uncompressed_size, crc_32, _get_compress_obj, encryption_func, chunks):
file_offset = offset

_raise_if_beyond(file_offset, maximum=0xffffffffffffffff, exception_class=OffsetOverflowError)
Expand Down Expand Up @@ -456,7 +459,7 @@ def _no_compression_streamed_64_local_header_and_data(name_encoded, mod_at_ms_do
yield from _(name_encoded)
yield from _(extra)

yield from _no_compression_streamed_data(chunks, uncompressed_size, crc_32, 0xffffffffffffffff)
yield from encryption_func(_no_compression_streamed_data(chunks, uncompressed_size, crc_32, 0xffffffffffffffff))

extra = zip_64_central_directory_extra_struct.pack(
zip_64_extra_signature,
Expand Down Expand Up @@ -486,7 +489,7 @@ def _no_compression_streamed_64_local_header_and_data(name_encoded, mod_at_ms_do
), name_encoded, extra


def _no_compression_streamed_32_local_header_and_data(name_encoded, mod_at_ms_dos, mod_at_unix_extra, aes_extra, external_attr, uncompressed_size, crc_32, _get_compress_obj, chunks):
def _no_compression_streamed_32_local_header_and_data(name_encoded, mod_at_ms_dos, mod_at_unix_extra, aes_extra, external_attr, uncompressed_size, crc_32, _get_compress_obj, encryption_func, chunks):
file_offset = offset

_raise_if_beyond(file_offset, maximum=0xffffffff, exception_class=OffsetOverflowError)
Expand All @@ -513,7 +516,7 @@ def _no_compression_streamed_32_local_header_and_data(name_encoded, mod_at_ms_do
yield from _(name_encoded)
yield from _(extra)

yield from _no_compression_streamed_data(chunks, uncompressed_size, crc_32, 0xffffffff)
yield from encryption_func(_no_compression_streamed_data(chunks, uncompressed_size, crc_32, 0xffffffff))

return central_directory_header_struct.pack(
20, # Version made by
Expand Down Expand Up @@ -586,9 +589,9 @@ def _no_compression_streamed_data(chunks, uncompressed_size, crc_32, maximum_siz
_no_compression_streamed_64_local_header_and_data if _method is _NO_COMPRESSION_STREAMED_64 else \
_no_compression_streamed_32_local_header_and_data

aes_extra = b''
aes_extra, encryption_func = (b'', _no_encryption)

central_directory_header_entry, name_encoded, extra = yield from data_func(name_encoded, mod_at_ms_dos, mod_at_unix_extra, aes_extra, external_attr, uncompressed_size, crc_32, _get_compress_obj, evenly_sized(chunks))
central_directory_header_entry, name_encoded, extra = yield from data_func(name_encoded, mod_at_ms_dos, mod_at_unix_extra, aes_extra, external_attr, uncompressed_size, crc_32, _get_compress_obj, encryption_func, evenly_sized(chunks))
central_directory_size += len(central_directory_header_signature) + len(central_directory_header_entry) + len(name_encoded) + len(extra)
central_directory.append((central_directory_header_entry, name_encoded, extra))

Expand Down

0 comments on commit d7710bb

Please sign in to comment.