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
41 changes: 30 additions & 11 deletions verify_nzb.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,39 @@ def _parse_yenc_attrs(line: bytes) -> dict[str, str]:
return attrs


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

def _decode_yenc_lines(lines: Iterable[bytes]) -> bytes:
decoded = bytearray()
# ⚑ Bolt Optimization: Use C-backed bytes built-ins (find/translate)
# instead of a manual Python loop over every byte. This accelerates
# yEnc decoding by processing chunks in native C.
decoded_chunks = []

for line in lines:
if b'=' not in line:
decoded_chunks.append(line.translate(_YENC_DECODE_TABLE))
continue

decoded = bytearray()
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
return bytes(decoded)
length = len(line)
while index < length:
next_eq = line.find(b'=', index)
if next_eq == -1:
decoded.extend(line[index:])
break

decoded.extend(line[index:next_eq])
if next_eq + 1 >= length:
raise ValueError("dangling yEnc escape")

byte = (line[next_eq + 1] - 64) % 256
decoded.append(byte)
index = next_eq + 2

decoded_chunks.append(bytes(decoded).translate(_YENC_DECODE_TABLE))

return b"".join(decoded_chunks)


def validate_yenc_body(lines: Iterable[bytes | str]) -> YencValidationResult:
Expand Down