Skip to content

Commit 1dbac02

Browse files
committed
🗓 Oct 19, 2025 3:24:12 PM
🐙 improvde uuencode/decode
1 parent 1191ddf commit 1dbac02

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

chepy/modules/internal/helpers.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,41 @@ def uuencode(self):
232232

233233
def uudecode(self):
234234
lines = self.data.strip().split(b"\n")
235-
if len(lines) < 3 or b"begin 644" not in lines[0].lower(): # pragma: no cover
236-
raise ValueError("Invalid UUencode format. Missing header")
237235

238-
data_lines = lines[1:-1] # Remove header and footer
236+
# More flexible header check - just look for "begin"
237+
if len(lines) < 3 or not lines[0].lower().startswith(b"begin"):
238+
raise ValueError(
239+
"Invalid UUencode format. Missing header"
240+
) # pragma: no cover
241+
242+
# Find where data actually starts (skip header)
243+
data_start = 1
244+
245+
# Find where data ends (before 'end' or backtick line)
246+
data_end = len(lines)
247+
for i in range(len(lines) - 1, 0, -1):
248+
if lines[i].strip().lower() == b"end":
249+
data_end = i
250+
break
251+
elif (
252+
lines[i].strip() == b"`"
253+
): # Handle backtick before end # pragma: no cover
254+
data_end = i
255+
break
256+
257+
data_lines = lines[data_start:data_end]
239258

240259
decoded_data = []
241260
for line in data_lines:
242-
decoded_chunk = binascii.a2b_uu(line)
243-
decoded_data.append(decoded_chunk)
261+
# Skip empty lines and backticks
262+
if not line.strip() or line.strip() == b"`":
263+
continue
264+
try:
265+
decoded_chunk = binascii.a2b_uu(line)
266+
decoded_data.append(decoded_chunk)
267+
except binascii.Error: # pragma: no cover
268+
# Skip lines that can't be decoded (like extra backticks)
269+
continue
244270

245271
return b"".join(decoded_data)
246272

tests/test_dataformat.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,9 +684,10 @@ def test_base62():
684684
)
685685
assert Chepy("3bfP0XZgTym6SsUKeZS5Z6qoKa").from_base62().o == b"hello !!123!! world"
686686

687+
687688
def test_base65536():
688-
assert Chepy('驳𓍣鹲驳ᕣ').from_base65536().o == b'securisec'
689-
assert Chepy('securisec').to_base65536().o.decode() == '驳𓍣鹲驳ᕣ'
689+
assert Chepy("驳𓍣鹲驳ᕣ").from_base65536().o == b"securisec"
690+
assert Chepy("securisec").to_base65536().o.decode() == "驳𓍣鹲驳ᕣ"
690691

691692

692693
def test_cut():

0 commit comments

Comments
 (0)