From 12da81e49901f7e15377f4771f8f539c19bcaed1 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 16 Jun 2026 00:31:49 +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 Decodes lines of yEnc encoded data using fast byte array translations instead of manual loops. Co-authored-by: xbmc4lyfe <273732874+xbmc4lyfe@users.noreply.github.com> --- verify_nzb.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/verify_nzb.py b/verify_nzb.py index 953dccd..4b893b1 100644 --- a/verify_nzb.py +++ b/verify_nzb.py @@ -115,19 +115,29 @@ def _parse_yenc_attrs(line: bytes) -> dict[str, str]: return attrs +UNESCAPED_TABLE = bytes((i - 42) % 256 for i in range(256)) + def _decode_yenc_lines(lines: Iterable[bytes]) -> bytes: + """Decodes lines of yEnc encoded data using fast byte array translations.""" 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 + escape_pos = line.find(b"=") + if escape_pos == -1: + decoded.extend(line.translate(UNESCAPED_TABLE)) + continue + + start = 0 + while escape_pos != -1: + decoded.extend(line[start:escape_pos].translate(UNESCAPED_TABLE)) + if escape_pos + 1 >= len(line): + raise ValueError("dangling yEnc escape") + decoded.append((line[escape_pos + 1] - 106) % 256) + start = escape_pos + 2 + escape_pos = line.find(b"=", start) + + if start < len(line): + decoded.extend(line[start:].translate(UNESCAPED_TABLE)) + return bytes(decoded)