Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added __pycache__/verify_nzb.cpython-312.pyc
Binary file not shown.
Binary file added tests/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file added tests/__pycache__/test_verify_nzb.cpython-312.pyc
Binary file not shown.
35 changes: 23 additions & 12 deletions verify_nzb.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ def normalize_message_id(message_id: str) -> str:
return f"<{text.strip('<>')}>"


_YENC_TRANS_TABLE = bytes((i - 42) % 256 for i in range(256))


def _parse_yenc_attrs(line: bytes) -> dict[str, str]:
attrs: dict[str, str] = {}
for token in line.decode("latin-1", errors="replace").split()[1:]:
Expand All @@ -116,18 +119,26 @@ def _parse_yenc_attrs(line: bytes) -> dict[str, str]:


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
"""
Decode yEnc data lines into bytes.
Optimized by joining the lines and using bytes.translate(),
which is significantly faster than byte-by-byte iteration.
"""
data = b"".join(lines)
parts = data.split(b"=")
Comment on lines +127 to +128
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve dangling-escape checks per yEnc line

Joining all data lines before splitting on = changes the decoder’s validation semantics: a data line that ends with a bare yEnc escape (=) is no longer reported as dangling yEnc escape when the next line starts with any byte, because the first byte of the next line is consumed as the escape payload. In deep validation, a malformed article with a self-consistent =yend size/CRC can therefore be accepted even though the yEnc syntax is invalid; the previous implementation caught this per line before advancing to the next line.

Useful? React with 👍 / 👎.


if len(parts) == 1:
return data.translate(_YENC_TRANS_TABLE)

decoded = bytearray(parts[0].translate(_YENC_TRANS_TABLE))
for i in range(1, len(parts)):
part = parts[i]
if not part:
raise ValueError("dangling yEnc escape")
decoded.append((part[0] - 106) % 256)
if len(part) > 1:
decoded.extend(part[1:].translate(_YENC_TRANS_TABLE))

return bytes(decoded)


Expand Down