From 3cae64a35a670f644f21ceddb9a59cd6db9bc49e Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 27 May 2026 00:45:10 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20yEnc=20decoding?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replaced slow byte-by-byte Python while loop with `bytes.translate` and `bytes.split` in `_decode_yenc_lines`. - Added `__pycache__/` to `.gitignore`. Co-authored-by: xbmc4lyfe <273732874+xbmc4lyfe@users.noreply.github.com> --- .gitignore | 1 + verify_nzb.py | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 4320982..880d07d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ config.ini *.txt .* !.gitignore +__pycache__/ diff --git a/verify_nzb.py b/verify_nzb.py index 953dccd..b2b7678 100644 --- a/verify_nzb.py +++ b/verify_nzb.py @@ -115,19 +115,24 @@ def _parse_yenc_attrs(line: bytes) -> dict[str, str]: return attrs +_DECODE_TABLE = bytes((i - 42) % 256 for i in range(256)) +_ESCAPED_DECODE_TABLE = bytes((i - 106) % 256 for i in range(256)) + def _decode_yenc_lines(lines: Iterable[bytes]) -> bytes: 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(_DECODE_TABLE)) + continue + + parts = line.split(b"=") + decoded.extend(parts[0].translate(_DECODE_TABLE)) + for part in parts[1:]: + if not part: + raise ValueError("dangling yEnc escape") + decoded.append(_ESCAPED_DECODE_TABLE[part[0]]) + if len(part) > 1: + decoded.extend(part[1:].translate(_DECODE_TABLE)) return bytes(decoded)