mirror of
https://github.com/rembo10/headphones.git
synced 2026-01-10 07:17:59 -05:00
Mostly just updating libraries, removing string encoding/decoding, fixing some edge cases. No new functionality was added in this commit.
36 lines
977 B
Python
36 lines
977 B
Python
"""Code, for converting bencoded data to string and back."""
|
|
|
|
|
|
def be_to_str(data: bytes) -> str:
|
|
"""Convert bencoded data from bytes to string"""
|
|
result = []
|
|
for num in data:
|
|
# Non-printable characters, double quotes, square brackets, accent
|
|
if num < 32 or num in [34, 91, 92, 93] or num > 126:
|
|
result.append("[%0.2x]" % num)
|
|
else:
|
|
result.append(chr(num))
|
|
return "".join(result)
|
|
|
|
|
|
def str_to_be(data: str) -> bytes:
|
|
"""Convert bencoded data from string to bytes"""
|
|
result = bytearray()
|
|
seq_marker = False
|
|
seq_chars = ""
|
|
|
|
for char in data:
|
|
if char == "[":
|
|
seq_marker = True
|
|
continue
|
|
if char == "]":
|
|
result.append(int(seq_chars, 16))
|
|
seq_marker = False
|
|
seq_chars = ""
|
|
continue
|
|
if seq_marker:
|
|
seq_chars += char
|
|
else:
|
|
result.append(ord(char))
|
|
return bytes(result)
|