Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions verify_nzb.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,25 @@ def _parse_yenc_attrs(line: bytes) -> dict[str, str]:
return attrs


# Precomputed translation table for unescaped bytes: (byte - 42) % 256
_YENC_DECODE_TABLE = bytes((i - 42) % 256 for i in range(256))

def _decode_yenc_lines(lines: Iterable[bytes]) -> bytes:
"""
Optimized yEnc decoding. Replaces per-byte Python iteration with C-level
bytes.translate() and split() which is roughly 10x faster.
"""
decoded = bytearray()
for line in lines:
index = 0
while index < len(line):
byte = line[index]
if byte == 61:
index += 1
if index >= len(line):
raise ValueError("dangling yEnc escape")
byte = (line[index] - 64) % 256
decoded.append((byte - 42) % 256)
index += 1
parts = line.split(b"=")
decoded.extend(parts[0].translate(_YENC_DECODE_TABLE))
for part in parts[1:]:
if not part:
raise ValueError("dangling yEnc escape")
# Escaped byte decoding simplifies to (byte - 106) % 256
decoded.append((part[0] - 106) % 256)
if len(part) > 1:
decoded.extend(part[1:].translate(_YENC_DECODE_TABLE))
return bytes(decoded)


Expand Down