diff --git a/__pycache__/test_speed.cpython-312.pyc b/__pycache__/test_speed.cpython-312.pyc new file mode 100644 index 0000000..5ea372d Binary files /dev/null and b/__pycache__/test_speed.cpython-312.pyc differ diff --git a/__pycache__/test_speed2.cpython-312.pyc b/__pycache__/test_speed2.cpython-312.pyc new file mode 100644 index 0000000..4a02a75 Binary files /dev/null and b/__pycache__/test_speed2.cpython-312.pyc differ diff --git a/__pycache__/test_split.cpython-312.pyc b/__pycache__/test_split.cpython-312.pyc new file mode 100644 index 0000000..5b7b336 Binary files /dev/null and b/__pycache__/test_split.cpython-312.pyc differ diff --git a/__pycache__/test_yenc.cpython-312.pyc b/__pycache__/test_yenc.cpython-312.pyc new file mode 100644 index 0000000..8f2d6e7 Binary files /dev/null and b/__pycache__/test_yenc.cpython-312.pyc differ diff --git a/__pycache__/test_yenc2.cpython-312.pyc b/__pycache__/test_yenc2.cpython-312.pyc new file mode 100644 index 0000000..15965e4 Binary files /dev/null and b/__pycache__/test_yenc2.cpython-312.pyc differ diff --git a/__pycache__/verify_nzb.cpython-312.pyc b/__pycache__/verify_nzb.cpython-312.pyc new file mode 100644 index 0000000..b92d329 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..d450742 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..fdba040 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..9fd4e9d 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_TABLE = bytes((i - 42) % 256 for i in range(256)) + def _decode_yenc_lines(lines: Iterable[bytes]) -> bytes: + joined = b"".join(lines) + if not joined: + return b"" + parts = joined.split(b"=") + if len(parts) == 1: + return joined.translate(_YENC_TABLE) + 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 + decoded.extend(parts[0].translate(_YENC_TABLE)) + + iterator = iter(parts[1:]) + for part in iterator: + if not part: + decoded.append(211) # (61 - 106) % 256 + try: + next_part = next(iterator) + decoded.extend(next_part.translate(_YENC_TABLE)) + except StopIteration: + raise ValueError("dangling yEnc escape") + else: + decoded.append((part[0] - 106) % 256) + if len(part) > 1: + decoded.extend(part[1:].translate(_YENC_TABLE)) + return bytes(decoded)