|
1 |
| -import base64 |
| 1 | +""" |
| 2 | +Base85 (Ascii85) encoding and decoding |
2 | 3 |
|
| 4 | +https://en.wikipedia.org/wiki/Ascii85 |
| 5 | +""" |
3 | 6 |
|
4 |
| -def base85_encode(string: str) -> bytes: |
| 7 | + |
| 8 | +def _base10_to_85(d: int) -> str: |
| 9 | + return "".join(chr(d % 85 + 33)) + _base10_to_85(d // 85) if d > 0 else "" |
| 10 | + |
| 11 | + |
| 12 | +def _base85_to_10(digits: list) -> int: |
| 13 | + return sum(char * 85**i for i, char in enumerate(reversed(digits))) |
| 14 | + |
| 15 | + |
| 16 | +def ascii85_encode(data: bytes) -> bytes: |
5 | 17 | """
|
6 |
| - >>> base85_encode("") |
| 18 | + >>> ascii85_encode(b"") |
7 | 19 | b''
|
8 |
| - >>> base85_encode("12345") |
| 20 | + >>> ascii85_encode(b"12345") |
9 | 21 | b'0etOA2#'
|
10 |
| - >>> base85_encode("base 85") |
| 22 | + >>> ascii85_encode(b"base 85") |
11 | 23 | b'@UX=h+?24'
|
12 | 24 | """
|
13 |
| - # encoded the input to a bytes-like object and then a85encode that |
14 |
| - return base64.a85encode(string.encode("utf-8")) |
| 25 | + binary_data = "".join(bin(ord(d))[2:].zfill(8) for d in data.decode("utf-8")) |
| 26 | + null_values = (32 * ((len(binary_data) // 32) + 1) - len(binary_data)) // 8 |
| 27 | + binary_data = binary_data.ljust(32 * ((len(binary_data) // 32) + 1), "0") |
| 28 | + b85_chunks = [int(_s, 2) for _s in map("".join, zip(*[iter(binary_data)] * 32))] |
| 29 | + result = "".join(_base10_to_85(chunk)[::-1] for chunk in b85_chunks) |
| 30 | + return bytes(result[:-null_values] if null_values % 4 != 0 else result, "utf-8") |
15 | 31 |
|
16 | 32 |
|
17 |
| -def base85_decode(a85encoded: bytes) -> str: |
| 33 | +def ascii85_decode(data: bytes) -> bytes: |
18 | 34 | """
|
19 |
| - >>> base85_decode(b"") |
20 |
| - '' |
21 |
| - >>> base85_decode(b"0etOA2#") |
22 |
| - '12345' |
23 |
| - >>> base85_decode(b"@UX=h+?24") |
24 |
| - 'base 85' |
| 35 | + >>> ascii85_decode(b"") |
| 36 | + b'' |
| 37 | + >>> ascii85_decode(b"0etOA2#") |
| 38 | + b'12345' |
| 39 | + >>> ascii85_decode(b"@UX=h+?24") |
| 40 | + b'base 85' |
25 | 41 | """
|
26 |
| - # a85decode the input into bytes and decode that into a human readable string |
27 |
| - return base64.a85decode(a85encoded).decode("utf-8") |
| 42 | + null_values = 5 * ((len(data) // 5) + 1) - len(data) |
| 43 | + binary_data = data.decode("utf-8") + "u" * null_values |
| 44 | + b85_chunks = map("".join, zip(*[iter(binary_data)] * 5)) |
| 45 | + b85_segments = [[ord(_s) - 33 for _s in chunk] for chunk in b85_chunks] |
| 46 | + results = [bin(_base85_to_10(chunk))[2::].zfill(32) for chunk in b85_segments] |
| 47 | + char_chunks = [ |
| 48 | + [chr(int(_s, 2)) for _s in map("".join, zip(*[iter(r)] * 8))] for r in results |
| 49 | + ] |
| 50 | + result = "".join("".join(char) for char in char_chunks) |
| 51 | + offset = int(null_values % 5 == 0) |
| 52 | + return bytes(result[: offset - null_values], "utf-8") |
28 | 53 |
|
29 | 54 |
|
30 | 55 | if __name__ == "__main__":
|
|
0 commit comments