|
| 1 | +from typing import Union |
| 2 | + |
| 3 | + |
| 4 | +class B91: |
| 5 | + ALPHABET = [ |
| 6 | + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
| 7 | + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
| 8 | + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
| 9 | + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
| 10 | + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '#', '$', |
| 11 | + '%', '&', '(', ')', '*', '+', ',', '.', '/', ':', ';', '<', '=', |
| 12 | + '>', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~', '"' |
| 13 | + ] |
| 14 | + |
| 15 | + DECODE_TABLE = {char: idx for idx, char in enumerate(ALPHABET)} |
| 16 | + |
| 17 | + @classmethod |
| 18 | + def decode(cls, encoded_data: Union[str, bytes], encoding: str = "utf-8") -> bytes: |
| 19 | + """ |
| 20 | + Decodes a Base91-encoded string into its original binary form. |
| 21 | +
|
| 22 | + Args: |
| 23 | + encoded_data (Union[str, bytes]): Base91-encoded input data. If `bytes`, it is decoded as UTF-8. |
| 24 | + encoding (str): The encoding to use if `encoded_data` is provided as a string. Default is 'utf-8'. |
| 25 | +
|
| 26 | + Returns: |
| 27 | + bytes: The decoded binary data. |
| 28 | +
|
| 29 | + Raises: |
| 30 | + ValueError: If the input contains invalid Base91 characters. |
| 31 | + """ |
| 32 | + if isinstance(encoded_data, bytes): |
| 33 | + encoded_data = encoded_data.decode(encoding) |
| 34 | + |
| 35 | + v = -1 |
| 36 | + b = 0 |
| 37 | + n = 0 |
| 38 | + out = bytearray() |
| 39 | + |
| 40 | + for char in encoded_data: |
| 41 | + if char not in cls.DECODE_TABLE: |
| 42 | + raise ValueError(f"Invalid Base91 character: {char}") |
| 43 | + c = cls.DECODE_TABLE[char] |
| 44 | + if v < 0: |
| 45 | + v = c |
| 46 | + else: |
| 47 | + v += c * 91 |
| 48 | + b |= v << n |
| 49 | + n += 13 if (v & 8191) > 88 else 14 |
| 50 | + while n >= 8: |
| 51 | + out.append(b & 255) |
| 52 | + b >>= 8 |
| 53 | + n -= 8 |
| 54 | + v = -1 |
| 55 | + |
| 56 | + if v >= 0: |
| 57 | + out.append((b | v << n) & 255) |
| 58 | + |
| 59 | + return bytes(out) |
| 60 | + |
| 61 | + @classmethod |
| 62 | + def encode(cls, data: Union[bytes, str], encoding: str = "utf-8") -> bytes: |
| 63 | + """ |
| 64 | + Encodes binary data into a Base91-encoded string. |
| 65 | +
|
| 66 | + Args: |
| 67 | + data (Union[bytes, str]): Input binary data to encode. If `str`, it is encoded as UTF-8. |
| 68 | + encoding (str): The encoding to use if `data` is provided as a string. Default is 'utf-8'. |
| 69 | +
|
| 70 | + Returns: |
| 71 | + str: The Base91-encoded string. |
| 72 | + """ |
| 73 | + if isinstance(data, str): |
| 74 | + data = data.encode(encoding) |
| 75 | + |
| 76 | + b = 0 |
| 77 | + n = 0 |
| 78 | + out = [] |
| 79 | + |
| 80 | + for byte in data: |
| 81 | + b |= byte << n |
| 82 | + n += 8 |
| 83 | + if n > 13: |
| 84 | + v = b & 8191 |
| 85 | + if v > 88: |
| 86 | + b >>= 13 |
| 87 | + n -= 13 |
| 88 | + else: |
| 89 | + v = b & 16383 |
| 90 | + b >>= 14 |
| 91 | + n -= 14 |
| 92 | + out.append(cls.ALPHABET[v % 91]) |
| 93 | + out.append(cls.ALPHABET[v // 91]) |
| 94 | + |
| 95 | + if n: |
| 96 | + out.append(cls.ALPHABET[b % 91]) |
| 97 | + if n > 7 or b > 90: |
| 98 | + out.append(cls.ALPHABET[b // 91]) |
| 99 | + |
| 100 | + return ''.join(out).encode(encoding) |
0 commit comments