-
Notifications
You must be signed in to change notification settings - Fork 0
β‘ Bolt: Optimize yEnc decoding performance #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+131
to
+144
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use explicit exception chaining with When converting Proposed fix except StopIteration:
- raise ValueError("dangling yEnc escape")
+ raise ValueError("dangling yEnc escape") from Noneπ Committable suggestion
Suggested change
π§° Toolsπͺ Ruff (0.15.13)[warning] 139-139: Within an (B904) π€ Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return bytes(decoded) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parse escapes per source line instead of on
b"".join(lines): the new split-based logic allows a line ending with=to consume the first byte of the next line, so malformed input no longer triggersdangling yEnc escape. This changes validation semantics and can produce false positives invalidate_yenc_bodywhen CRC is absent (e.g.,=ybegin size=1, data linesb"="+b"k",=yend size=1now returnsok=Trueinstead of rejecting the malformed body).Useful? React with πΒ / π.