From 7c03bc6ba7af39439e96e87a6082cb8791b8dfcc Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 10 Jun 2026 00:32:32 +0000 Subject: [PATCH] Optimize yEnc decoding using bytes.translate and bytes.find Co-authored-by: xbmc4lyfe <273732874+xbmc4lyfe@users.noreply.github.com> --- verify_nzb.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/verify_nzb.py b/verify_nzb.py index 953dccd..4a28b36 100644 --- a/verify_nzb.py +++ b/verify_nzb.py @@ -115,19 +115,33 @@ 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() for line in lines: + if b"=" not in line: + decoded.extend(line.translate(_YENC_DECODE_TABLE)) + continue + 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 + length = len(line) + while index < length: + pos = line.find(b"=", index) + if pos == -1: + decoded.extend(line[index:].translate(_YENC_DECODE_TABLE)) + break + + if pos > index: + decoded.extend(line[index:pos].translate(_YENC_DECODE_TABLE)) + + if pos + 1 >= length: + raise ValueError("dangling yEnc escape") + + decoded.append((line[pos+1] - 106) % 256) + index = pos + 2 + return bytes(decoded)