Skip to content

Commit 8de25cb

Browse files
committed
gh-141968: Inline traditional ZIP key updates
1 parent c4f4c75 commit 8de25cb

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

Lib/zipfile/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,13 +752,21 @@ def update_keys(c):
752752

753753
def decrypter(data):
754754
"""Decrypt a bytes object."""
755+
nonlocal key0, key1, key2
756+
k0, k1, k2 = key0, key1, key2
755757
result = bytearray()
756758
append = result.append
759+
crc_table = crctable
760+
# Inline update_keys() to avoid a function call for every byte.
757761
for c in data:
758-
k = key2 | 2
762+
k = k2 | 2
759763
c ^= ((k * (k^1)) >> 8) & 0xFF
760-
update_keys(c)
764+
k0 = (k0 >> 8) ^ crc_table[(k0 ^ c) & 0xFF]
765+
k1 = (k1 + (k0 & 0xFF)) & 0xFFFFFFFF
766+
k1 = (k1 * 134775813 + 1) & 0xFFFFFFFF
767+
k2 = (k2 >> 8) ^ crc_table[(k2 ^ (k1 >> 24)) & 0xFF]
761768
append(c)
769+
key0, key1, key2 = k0, k1, k2
762770
return result.take_bytes()
763771

764772
return decrypter

0 commit comments

Comments
 (0)