mirror of
https://github.com/Mayuri-Chan/pyrofork.git
synced 2025-12-29 20:14:51 +00:00
37 lines
643 B
Python
37 lines
643 B
Python
from base64 import b64decode, b64encode
|
|
|
|
|
|
def decode(s: str) -> bytes:
|
|
s = b64decode(s + "=" * (-len(s) % 4), "-_")
|
|
r = b""
|
|
|
|
assert s[-1] == 2
|
|
|
|
i = 0
|
|
while i < len(s) - 1:
|
|
if s[i] != 0:
|
|
r += bytes([s[i]])
|
|
else:
|
|
r += b"\x00" * s[i + 1]
|
|
i += 1
|
|
|
|
i += 1
|
|
|
|
return r
|
|
|
|
|
|
def encode(s: bytes) -> str:
|
|
r = b""
|
|
n = 0
|
|
|
|
for i in s + bytes([2]):
|
|
if i == 0:
|
|
n += 1
|
|
else:
|
|
if n:
|
|
r += b"\x00" + bytes([n])
|
|
n = 0
|
|
|
|
r += bytes([i])
|
|
|
|
return b64encode(r, b"-_").decode().rstrip("=")
|