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
35 changes: 25 additions & 10 deletions verify_nzb.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,34 @@ def _parse_yenc_attrs(line: bytes) -> dict[str, str]:
return attrs


_YENC_NORMAL_TABLE = bytes((i - 42) % 256 for i in range(256))


def _decode_yenc_lines(lines: Iterable[bytes]) -> bytes:
"""
Decodes yEnc lines efficiently by leveraging C-level operations like `bytes.translate`
and `bytes.find`. This avoids slow byte-by-byte Python iteration, making it significantly
faster for deep-checks.
"""
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
if b"=" not in line:
decoded.extend(line.translate(_YENC_NORMAL_TABLE))
continue

start = 0
while True:
pos = line.find(b"=", start)
if pos == -1:
decoded.extend(line[start:].translate(_YENC_NORMAL_TABLE))
break

decoded.extend(line[start:pos].translate(_YENC_NORMAL_TABLE))
if pos + 1 >= len(line):
raise ValueError("dangling yEnc escape")

decoded.append((line[pos + 1] - 106) % 256)
start = pos + 2
return bytes(decoded)


Expand Down