diff --git a/__pycache__/verify_nzb.cpython-312.pyc b/__pycache__/verify_nzb.cpython-312.pyc new file mode 100644 index 0000000..59fde49 Binary files /dev/null and b/__pycache__/verify_nzb.cpython-312.pyc differ diff --git a/tests/__pycache__/__init__.cpython-312.pyc b/tests/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000..e03b4c4 Binary files /dev/null and b/tests/__pycache__/__init__.cpython-312.pyc differ diff --git a/tests/__pycache__/test_verify_nzb.cpython-312.pyc b/tests/__pycache__/test_verify_nzb.cpython-312.pyc new file mode 100644 index 0000000..e6c326c Binary files /dev/null and b/tests/__pycache__/test_verify_nzb.cpython-312.pyc differ diff --git a/verify_nzb.py b/verify_nzb.py index 953dccd..629fc9a 100644 --- a/verify_nzb.py +++ b/verify_nzb.py @@ -115,20 +115,26 @@ def _parse_yenc_attrs(line: bytes) -> dict[str, str]: return attrs +_YENC_TRANSLATE_TABLE = bytes((i - 42) % 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 - return bytes(decoded) + # Process escapes on a per-line basis + start = 0 + while True: + pos = line.find(b'=', start) + if pos == -1: + decoded.extend(line[start:]) + break + decoded.extend(line[start:pos]) + if pos + 1 >= len(line): + raise ValueError("dangling yEnc escape") + decoded.append((line[pos + 1] - 64) % 256) + start = pos + 2 + + # Utilize C-backed built-in methods for significant performance gain + return bytes(decoded.translate(_YENC_TRANSLATE_TABLE)) def validate_yenc_body(lines: Iterable[bytes | str]) -> YencValidationResult: